diff --git a/apps/remix-ide/src/app/plugins/remix-templates.ts b/apps/remix-ide/src/app/plugins/remix-templates.ts index e7846fbf47..d0aab28ecf 100644 --- a/apps/remix-ide/src/app/plugins/remix-templates.ts +++ b/apps/remix-ide/src/app/plugins/remix-templates.ts @@ -5,7 +5,7 @@ const profile = { name: 'remix-templates', displayName: 'remix-templates', description: 'Remix Templates plugin', - methods: ['getTemplate', 'loadTemplateInNewWindow'], + methods: ['getTemplate', 'loadTemplateInNewWindow', 'loadFilesInNewWindow'], } export class TemplatesPlugin extends Plugin { @@ -26,5 +26,9 @@ export class TemplatesPlugin extends Plugin { const files = await this.getTemplate(template, opts) this.call('electronTemplates', 'loadTemplateInNewWindow', files) } + + async loadFilesInNewWindow (files: any) { + this.call('electronTemplates', 'loadTemplateInNewWindow', files) + } } diff --git a/apps/remixdesktop/src/plugins/fsPlugin.ts b/apps/remixdesktop/src/plugins/fsPlugin.ts index 110ac98624..ea091e3429 100644 --- a/apps/remixdesktop/src/plugins/fsPlugin.ts +++ b/apps/remixdesktop/src/plugins/fsPlugin.ts @@ -200,6 +200,7 @@ class FSPluginClient extends ElectronBasePluginClient { } async exists(path: string): Promise { + if (this.workingDir === '') return false return fs .access(this.fixPath(path)) .then(() => true) @@ -398,12 +399,14 @@ class FSPluginClient extends ElectronBasePluginClient { } async setWorkingDir(path: string): Promise { + console.log('setWorkingDir', path) this.workingDir = path await this.updateRecentFolders(path) await this.updateOpenedFolders(path) this.window.setTitle(getBaseName(this.workingDir)) this.watch() this.emit('workingDirChanged', path) + return } async revealInExplorer(action: customAction, isAbsolutePath: boolean = false): Promise { diff --git a/apps/remixdesktop/src/plugins/templates.ts b/apps/remixdesktop/src/plugins/templates.ts index aefadcae1b..4998137437 100644 --- a/apps/remixdesktop/src/plugins/templates.ts +++ b/apps/remixdesktop/src/plugins/templates.ts @@ -42,20 +42,26 @@ class TemplatesPluginClient extends ElectronBasePluginClient { super(webContentsId, profile) } - async loadTemplateInNewWindow (files: any) { + async loadTemplateInNewWindow(files: any) { - let folder = await this.call('fs' as any, 'selectFolder', null ,'Select or create a folder to load the template in', 'Set as destination folder for the template') + let folder = await this.call('fs' as any, 'selectFolder', null, 'Select or create a folder to load the files in', 'Set as destination folder for the files') if (!folder || folder === '') return // @ts-ignore for (const file in files) { try { - if(!folder.endsWith('/')) folder += '/' - - await fs.mkdir(path.dirname(folder + file), { recursive: true}) - await fs.writeFile(folder + file, files[file], { - encoding: 'utf8' - }) + if (!folder.endsWith('/')) folder += '/' + + await fs.mkdir(path.dirname(folder + file), { recursive: true }) + if (typeof files[file] !== 'string' && files[file].content) { + await fs.writeFile(folder + files[file].filename, files[file].content, { + encoding: 'utf8', + }) + } else { + await fs.writeFile(folder + file, files[file], { + encoding: 'utf8' + }) + } } catch (error) { console.error(error) } @@ -63,7 +69,7 @@ class TemplatesPluginClient extends ElectronBasePluginClient { createWindow(folder) } - async openTemplate(){ + async openTemplate() { this.call('filePanel' as any, 'loadTemplate') } diff --git a/libs/remix-core-plugin/src/lib/gist-handler.ts b/libs/remix-core-plugin/src/lib/gist-handler.ts index 83c932d4c6..afe415fc6b 100644 --- a/libs/remix-core-plugin/src/lib/gist-handler.ts +++ b/libs/remix-core-plugin/src/lib/gist-handler.ts @@ -2,6 +2,7 @@ 'use strict' import { Plugin } from '@remixproject/engine' import isElectron from 'is-electron' +import { Registry } from '@remix-project/remix-lib' interface StringByString { [key: string]: string; @@ -17,12 +18,16 @@ const profile = { type GistCallBackFn = (gistId: string) => void export class GistHandler extends Plugin { - constructor () { + isDesktop: boolean = false + constructor() { super(profile) + if (Registry.getInstance().get('platform').api.isDesktop()) { + this.isDesktop = true + } } - async handleLoad (gistId: string | null, cb: GistCallBackFn) { - if (!cb) cb = () => {} + async handleLoad(gistId: string | null, cb: GistCallBackFn) { + if (!cb) cb = () => { } let loadingFromGist = false if (!gistId) { @@ -36,7 +41,7 @@ export class GistHandler extends Plugin { title: 'Load a Gist', message: 'Enter the ID of the Gist or URL you would like to load.', modalType: 'prompt', - okLabel: 'OK', + okLabel: (this.isDesktop ? 'Load and select destination' : 'OK'), cancelLabel: 'Cancel', okFn: (value) => { setTimeout(() => resolve(value), 0) @@ -86,7 +91,7 @@ export class GistHandler extends Plugin { return loadingFromGist } - load (gistId: string | null) { + load(gistId: string | null) { const self = this return self.handleLoad(gistId, async (gistId: string | null) => { let data: any @@ -115,34 +120,39 @@ export class GistHandler extends Plugin { } const gistIdWorkspace = 'gist ' + gistId - const workspaces = await this.call('filePanel', 'getWorkspaces') - const found = workspaces.find((workspace) => workspace.name === gistIdWorkspace) - if (found) { - await this.call('notification', 'alert', { - id: 'gistAlert', - message: `workspace "${gistIdWorkspace}" already exists`, - }) - return - } - await this.call('filePanel', 'createWorkspace', 'gist ' + gistId, '', true) - await this.call('filePanel', 'switchToWorkspace', { name: 'gist ' + gistId, isLocalHost: false }) - const obj: StringByString = {} Object.keys(data.files).forEach((element) => { const path = element.replace(/\.\.\./g, '/') obj['/' + path] = data.files[element] }) - this.call('fileManager', 'setBatchFiles', obj, isElectron()? 'electron':'workspace', true, async (errorSavingFiles: any) => { - if (errorSavingFiles) { - const modalContent = { - id: 'gisthandler', - title: 'Gist load error', - message: errorSavingFiles.message || errorSavingFiles - } - this.call('notification', 'alert', modalContent) + + if (this.isDesktop) { + await this.call('remix-templates', 'loadFilesInNewWindow', obj) + } else { + const workspaces = await this.call('filePanel', 'getWorkspaces') + const found = workspaces.find((workspace) => workspace.name === gistIdWorkspace) + if (found) { + await this.call('notification', 'alert', { + id: 'gistAlert', + message: `workspace "${gistIdWorkspace}" already exists`, + }) + return } - }) + await this.call('filePanel', 'createWorkspace', 'gist ' + gistId, '', true) + await this.call('filePanel', 'switchToWorkspace', { name: 'gist ' + gistId, isLocalHost: false }) + this.call('fileManager', 'setBatchFiles', obj, isElectron() ? 'electron' : 'workspace', true, async (errorSavingFiles: any) => { + if (errorSavingFiles) { + const modalContent = { + id: 'gisthandler', + title: 'Gist load error', + message: errorSavingFiles.message || errorSavingFiles + + } + this.call('notification', 'alert', modalContent) + } + }) + } }) } }