From 768a494579175d29e62cac974a730ddd4eb04cc2 Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 23 Aug 2021 18:36:19 +0200 Subject: [PATCH] fix iframe plugin api --- apps/remix-ide/src/app/tabs/compile-tab.js | 113 ------------------ .../solidity-compiler/src/app/compiler-api.ts | 22 +++- apps/solidity-compiler/src/app/compiler.ts | 26 +++- libs/remix-ui/solidity-compiler/src/index.ts | 3 +- .../src/lib/actions/compiler.ts | 10 +- .../src/lib/compiler-container.tsx | 2 +- .../src/lib/icompiler-api.ts | 27 ----- .../src/lib/solidity-compiler.tsx | 6 +- .../solidity-compiler/src/lib/types/index.ts | 56 +++++---- 9 files changed, 82 insertions(+), 183 deletions(-) delete mode 100644 libs/remix-ui/solidity-compiler/src/lib/icompiler-api.ts diff --git a/apps/remix-ide/src/app/tabs/compile-tab.js b/apps/remix-ide/src/app/tabs/compile-tab.js index 6a69f99ad9..39c66d3ec6 100644 --- a/apps/remix-ide/src/app/tabs/compile-tab.js +++ b/apps/remix-ide/src/app/tabs/compile-tab.js @@ -48,119 +48,6 @@ class CompileTab extends CompilerApiMixin(ViewPlugin) { this.renderComponent() } - /************ - * EVENTS - */ - - listenToEvents () { - this.data.eventHandlers.onContentChanged = () => { - this.emit('statusChanged', { key: 'edited', title: 'the content has changed, needs recompilation', type: 'info' }) - } - this.editor.event.register('contentChanged', this.data.eventHandlers.onContentChanged) - - this.data.eventHandlers.onLoadingCompiler = () => { - this.data.loading = true - this.emit('statusChanged', { key: 'loading', title: 'loading compiler...', type: 'info' }) - } - this.compiler.event.register('loadingCompiler', this.data.eventHandlers.onLoadingCompiler) - - this.data.eventHandlers.onCompilerLoaded = () => { - this.data.loading = false - this.emit('statusChanged', { key: 'none' }) - } - this.compiler.event.register('compilerLoaded', this.data.eventHandlers.onCompilerLoaded) - - this.data.eventHandlers.onStartingCompilation = () => { - this.emit('statusChanged', { key: 'loading', title: 'compiling...', type: 'info' }) - } - - this.data.eventHandlers.onRemoveAnnotations = () => { - this.call('editor', 'clearAnnotations') - } - - const resetView = (isLocalhost) => { - this.compileTabLogic.isHardhatProject().then((result) => { - if (result && isLocalhost) this.isHardHatProject = true - else this.isHardHatProject = false - this.renderComponent() - }) - this.resetResults() - } - - this.on('filePanel', 'setWorkspace', (workspace) => { - resetView(workspace.isLocalhost) - }) - - this.on('remixd', 'rootFolderChanged', () => { - resetView(true) - }) - - this.compileTabLogic.event.on('startingCompilation', this.data.eventHandlers.onStartingCompilation) - this.compileTabLogic.event.on('removeAnnotations', this.data.eventHandlers.onRemoveAnnotations) - - this.data.eventHandlers.onCurrentFileChanged = (name) => { - this.currentFile = name - this.renderComponent() - } - this.fileManager.events.on('currentFileChanged', this.data.eventHandlers.onCurrentFileChanged) - - this.data.eventHandlers.onNoFileSelected = () => { - this.currentFile = '' - this.renderComponent() - } - this.fileManager.events.on('noFileSelected', this.data.eventHandlers.onNoFileSelected) - - this.data.eventHandlers.onCompilationFinished = (success, data, source) => { - this.setCompileErrors(data) - if (success) { - // forwarding the event to the appManager infra - this.emit('compilationFinished', source.target, source, 'soljson', data) - if (data.errors && data.errors.length > 0) { - this.emit('statusChanged', { - key: data.errors.length, - title: `compilation finished successful with warning${data.errors.length > 1 ? 's' : ''}`, - type: 'warning' - }) - } else this.emit('statusChanged', { key: 'succeed', title: 'compilation successful', type: 'success' }) - // Store the contracts - this.contractsDetails = {} - this.compiler.visitContracts((contract) => { - this.contractsDetails[contract.name] = parseContracts( - contract.name, - contract.object, - this.compiler.getSource(contract.file) - ) - }) - } else { - const count = (data.errors ? data.errors.filter(error => error.severity === 'error').length : 0) + data.error ? 1 : 0 - this.emit('statusChanged', { key: count, title: `compilation failed with ${count} error${count.length > 1 ? 's' : ''}`, type: 'error' }) - } - // Update contract Selection - this.contractMap = {} - if (success) this.compiler.visitContracts((contract) => { this.contractMap[contract.name] = contract }) - this.renderComponent() - } - this.compiler.event.register('compilationFinished', this.data.eventHandlers.onCompilationFinished) - - this.data.eventHandlers.onThemeChanged = (theme) => { - const invert = theme.quality === 'dark' ? 1 : 0 - const img = document.getElementById('swarmLogo') - if (img) { - img.style.filter = `invert(${invert})` - } - } - globalRegistry.get('themeModule').api.events.on('themeChanged', this.data.eventHandlers.onThemeChanged) - - // Run the compiler instead of trying to save the website - $(window).keydown((e) => { - // ctrl+s or command+s - if ((e.metaKey || e.ctrlKey) && e.keyCode === 83) { - e.preventDefault() - this.compileTabLogic.runCompiler(this.hhCompilation) - } - }) - } - onResetResults () { this.renderComponent() } diff --git a/apps/solidity-compiler/src/app/compiler-api.ts b/apps/solidity-compiler/src/app/compiler-api.ts index e29981c14e..edc69528cd 100644 --- a/apps/solidity-compiler/src/app/compiler-api.ts +++ b/apps/solidity-compiler/src/app/compiler-api.ts @@ -123,8 +123,7 @@ export const CompilerApiMixin = (Base) => class extends Base { 'currentFile': () => this.currentFile, 'hideWarnings': () => false, 'autoCompile': () => false, - 'includeNightlies': () => false, - 'optimise': () => false + 'includeNightlies': () => false } return conf[name]() } @@ -186,7 +185,20 @@ export const CompilerApiMixin = (Base) => class extends Base { this.on('filePanel', 'setWorkspace', (workspace) => { this.resetResults() - if (this.onSetWorkspace) this.onSetWorkspace(workspace) + if (this.onSetWorkspace) this.onSetWorkspace(workspace.isLocalhost) + }) + + this.on('remixd', 'rootFolderChanged', () => { + this.resetResults() + if (this.onSetWorkspace) this.onSetWorkspace(true) + }) + + this.on('editor', 'sessionSwitched', () => { + if (this.onSessionSwitched) this.onSessionSwitched() + }) + + this.on('editor', 'contentChanged', () => { + if (this.onContentChanged) this.onContentChanged() }) this.compileTabLogic.event.on('startingCompilation', this.data.eventHandlers.onStartingCompilation) @@ -226,8 +238,8 @@ export const CompilerApiMixin = (Base) => class extends Base { ) }) } else { - const count = (data.errors ? data.errors.filter(error => error.severity === 'error').length : 0 + data.error ? 1 : 0) - this.emit('statusChanged', { key: count, title: `compilation failed with ${count} error${count.length > 1 ? 's' : ''}`, type: 'error' }) + const count = (data.errors ? data.errors.filter(error => error.severity === 'error').length : 0) + data.error ? 1 : 0 + this.emit('statusChanged', { key: count, title: `compilation failed with ${count} error${count > 1 ? 's' : ''}`, type: 'error' }) } // Update contract Selection this.contractMap = {} diff --git a/apps/solidity-compiler/src/app/compiler.ts b/apps/solidity-compiler/src/app/compiler.ts index 201a680085..7e6deb8c98 100644 --- a/apps/solidity-compiler/src/app/compiler.ts +++ b/apps/solidity-compiler/src/app/compiler.ts @@ -24,26 +24,40 @@ export interface ConfigurationSettings { } export class CompilerClientApi extends CompilerApiMixin(PluginClient) { + // interface matches libs/remix-ui/solidity-compiler/types/index.ts : ICompilerApi + currentFile: string contractMap: { file: string } | Record compileErrors: any compileTabLogic: any contractsDetails: Record - contentImport: any - call: (...args) => void - on: (...args) => void - setSelectedVersion: (value: string) => void configurationSettings: ConfigurationSettings + + setHardHatCompilation: (value: boolean) => void + getParameters: () => ConfigurationSettings + setParameters: (params: Partial) => void + setCompilerConfig: (settings: ConfigurationSettings) => void + getConfiguration: (value: string) => string setConfiguration: (name: string, value: string) => void - currentFile: string + getFileManagerMode: () => string + + + getCompilationResult: () => any onCurrentFileChanged: (fileName: string) => void onResetResults: () => void - onSetWorkspace: (workspace: any) => void + onSetWorkspace: (isLocalhost: boolean) => void onNoFileSelected: () => void onCompilationFinished: (contractsDetails: any, contractMap: any) => void + onSessionSwitched: () => void + onContentChanged: () => void + + fileExists: (file: string) => Promise + writeFile: (file: string, content: string) => Promise + readFile: (file: string) => Promise + open: (file: string) => void constructor () { super() diff --git a/libs/remix-ui/solidity-compiler/src/index.ts b/libs/remix-ui/solidity-compiler/src/index.ts index 073b7eeacc..30a13c42f4 100644 --- a/libs/remix-ui/solidity-compiler/src/index.ts +++ b/libs/remix-ui/solidity-compiler/src/index.ts @@ -1,3 +1,2 @@ export * from './lib/solidity-compiler' -export * from './lib/logic' -export * from './lib/icompiler-api' +export * from './lib/logic' \ No newline at end of file diff --git a/libs/remix-ui/solidity-compiler/src/lib/actions/compiler.ts b/libs/remix-ui/solidity-compiler/src/lib/actions/compiler.ts index e0e2a46498..96316c8bd5 100644 --- a/libs/remix-ui/solidity-compiler/src/lib/actions/compiler.ts +++ b/libs/remix-ui/solidity-compiler/src/lib/actions/compiler.ts @@ -27,9 +27,9 @@ export const resetCompilerMode = () => (dispatch: React.Dispatch) => { } export const listenToEvents = (compileTabLogic, api) => (dispatch: React.Dispatch) => { - api.on('editor', 'sessionSwitched', () => { + api.onSessionSwitched = () => { dispatch(setEditorMode('sessionSwitched')) - }) + } compileTabLogic.event.on('startingCompilation', () => { dispatch(setCompilerMode('startingCompilation')) @@ -39,10 +39,10 @@ export const listenToEvents = (compileTabLogic, api) => (dispatch: React.Dispatc dispatch(setCompilerMode('compilationDuration', speed)) }) - api.on('editor', 'contentChanged', () => { + api.onContentChanged = () => { dispatch(setEditorMode('contentChanged')) - }) - + } + compileTabLogic.compiler.event.register('loadingCompiler', () => { dispatch(setCompilerMode('loadingCompiler')) }) diff --git a/libs/remix-ui/solidity-compiler/src/lib/compiler-container.tsx b/libs/remix-ui/solidity-compiler/src/lib/compiler-container.tsx index 80b1004ab6..3bd1032c76 100644 --- a/libs/remix-ui/solidity-compiler/src/lib/compiler-container.tsx +++ b/libs/remix-ui/solidity-compiler/src/lib/compiler-container.tsx @@ -76,7 +76,7 @@ export const CompilerContainer = (props: CompilerContainerProps) => { hideWarnings: api.getConfiguration('hideWarnings') || false, autoCompile: typeof autoCompile === 'boolean' ? autoCompile : api.getConfiguration('autoCompile') || false, includeNightlies: api.getConfiguration('includeNightlies') || false, - optimise: typeof optimize === 'boolean' ? optimize : api.getConfiguration('optimise') || false, + optimize: (optimize !== null) && (optimize !== undefined) ? optimize : false, runs: (runs !== null) && (runs !== 'null') && (runs !== undefined) && (runs !== 'undefined') ? runs : 200, evmVersion: (evmVersion !== null) && (evmVersion !== 'null') && (evmVersion !== undefined) && (evmVersion !== 'undefined') ? evmVersion : 'default' } diff --git a/libs/remix-ui/solidity-compiler/src/lib/icompiler-api.ts b/libs/remix-ui/solidity-compiler/src/lib/icompiler-api.ts deleted file mode 100644 index 7bac190abe..0000000000 --- a/libs/remix-ui/solidity-compiler/src/lib/icompiler-api.ts +++ /dev/null @@ -1,27 +0,0 @@ -export type onCurrentFileChanged = (fileName: string) => void - -export interface ICompilerApi { - contractMap: { - file: string - } | Record - - compileErrors:any - - currentFile: string - configurationSettings: any - setHardHatCompilation(value: boolean): void - setSelectedVersion(version: string): void - getCompilationResult(): any - setCompilerConfig: (settings: any) => void - getParameters: () => any - setParameters: (params) => void - getConfiguration: (name: string) => string - setConfiguration: (name: string, value: string) => void - fileProviderOf: (file: string) => string - getFileManagerMode: () => string - fileExists: (file: string) => Promise - writeFile: (file: string, content: string) => Promise - readFile: (file: string) => Promise - open: (file: string) => void - onCurrentFileChanged: (listener: onCurrentFileChanged) => void -} diff --git a/libs/remix-ui/solidity-compiler/src/lib/solidity-compiler.tsx b/libs/remix-ui/solidity-compiler/src/lib/solidity-compiler.tsx index 57acbbd18b..792beb8829 100644 --- a/libs/remix-ui/solidity-compiler/src/lib/solidity-compiler.tsx +++ b/libs/remix-ui/solidity-compiler/src/lib/solidity-compiler.tsx @@ -44,8 +44,8 @@ export const SolidityCompiler = (props: SolidityCompilerProps) => { }) } - plugin.onSetWorkspace = async (workspace: any) => { - const isHardhat = workspace.isLocalhost && await compileTabLogic.isHardhatProject() + plugin.onSetWorkspace = async (isLocalhost: boolean) => { + const isHardhat = isLocalhost && await compileTabLogic.isHardhatProject() setState(prevState => { return { ...prevState, currentFile, isHardhatProject: isHardhat } }) @@ -71,7 +71,7 @@ export const SolidityCompiler = (props: SolidityCompilerProps) => { const updateCurrentVersion = (value) => { setCurrentVersion(value) - plugin.setSelectedVersion(value) + plugin.setParameters({ version: value }) } const modal = async (title: string, message: string | JSX.Element, okLabel: string, okFn: () => void, cancelLabel?: string, cancelFn?: () => void) => { diff --git a/libs/remix-ui/solidity-compiler/src/lib/types/index.ts b/libs/remix-ui/solidity-compiler/src/lib/types/index.ts index 5aae51ab93..b477a8df85 100644 --- a/libs/remix-ui/solidity-compiler/src/lib/types/index.ts +++ b/libs/remix-ui/solidity-compiler/src/lib/types/index.ts @@ -1,27 +1,41 @@ export type onCurrentFileChanged = (fileName: string) => void export interface SolidityCompilerProps { - plugin: { - currentFile: string - contractMap: { - file: string - } | Record - compileErrors: any, - compileTabLogic: any, - contractsDetails: Record, - contentImport: any, - call: (...args) => void - on: (...args) => void, - setSelectedVersion: (value: string) => void, - configurationSettings: ConfigurationSettings, - getConfiguration: (value: string) => string, - setConfiguration: (name: string, value: string) => void, - onCurrentFileChanged: (fileName: string) => void, - onResetResults: () => void, - onSetWorkspace: (workspace: any) => void, - onNoFileSelected: () => void, - onCompilationFinished: (contractsDetails: any, contractMap: any) => void - }, + plugin: ICompilerApi +} + +export interface ICompilerApi { + currentFile: string + contractMap: { + file: string + } | Record + compileErrors: any + compileTabLogic: any + contractsDetails: Record + configurationSettings: ConfigurationSettings + + setHardHatCompilation: (value: boolean) => void + getParameters: () => any + setParameters: (params) => void + getConfiguration: (value: string) => string + setConfiguration: (name: string, value: string) => void + getFileManagerMode: () => string + setCompilerConfig: (settings: any) => void + + getCompilationResult: () => any + + onCurrentFileChanged: (fileName: string) => void + onResetResults: () => void, + onSetWorkspace: (isLocalhost: boolean) => void + onNoFileSelected: () => void + onCompilationFinished: (contractsDetails: any, contractMap: any) => void + onSessionSwitched: () => void + onContentChanged: () => void + + fileExists: (file: string) => Promise + writeFile: (file: string, content: string) => Promise + readFile: (file: string) => Promise + open: (file: string) => void } export interface CompilerContainerProps {