From 898c63e3e71beaf1258d566cab7996ff6b2e648f Mon Sep 17 00:00:00 2001 From: Ofek Shilon Date: Sat, 17 Aug 2024 22:55:42 +0300 Subject: [PATCH] Final (?) pane-renaming fix --- static/multifile-service.ts | 14 -------------- static/panes/tree.ts | 27 +++++++++++++++++++++------ 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/static/multifile-service.ts b/static/multifile-service.ts index 58f9372c136..f189ee4e8a9 100644 --- a/static/multifile-service.ts +++ b/static/multifile-service.ts @@ -273,17 +273,7 @@ export class MultifileService { } } - private static isValidFile(file: MultifileFile): boolean { - return file.editorId > 0 || !!file.filename; - } - - private filterOutNonsense() { - this.files = this.files.filter((file: MultifileFile) => MultifileService.isValidFile(file)); - } - public getFiles(): Array { - this.filterOutNonsense(); - const filtered = this.files.filter((file: MultifileFile) => { return !file.isMainSource && file.isIncluded; }); @@ -435,8 +425,6 @@ export class MultifileService { } public forEachOpenFile(callback: (File) => void) { - this.filterOutNonsense(); - for (const file of this.files) { if (file.isOpen && file.editorId > 0) { callback(file); @@ -445,8 +433,6 @@ export class MultifileService { } public forEachFile(callback: (File) => void) { - this.filterOutNonsense(); - for (const file of this.files) { callback(file); } diff --git a/static/panes/tree.ts b/static/panes/tree.ts index 0a3a82e1445..126ce54e33b 100644 --- a/static/panes/tree.ts +++ b/static/panes/tree.ts @@ -190,6 +190,13 @@ export class Tree { this.updateButtons(state); } + private paneRenamedExternally() { + this.multifileService.forEachFile( + (file: MultifileFile) => (file.filename = this.hub.getEditorById(file.editorId)?.getPaneName() ?? ''), + ); + this.refresh(); + } + private initCallbacks() { this.container.on('resize', this.resize, this); this.container.on('shown', this.resize, this); @@ -198,7 +205,7 @@ export class Tree { }); this.container.on('destroy', this.close, this); - this.eventHub.on('renamePane', this.updateState.bind(this)); + this.eventHub.on('renamePane', this.paneRenamedExternally, this); this.eventHub.on('editorOpen', this.onEditorOpen, this); this.eventHub.on('editorClose', this.onEditorClose, this); @@ -281,11 +288,11 @@ export class Tree { private onEditorOpen(editorId: number) { const file = this.multifileService.getFileByEditorId(editorId); + this.refresh(); + this.sendChangesToAllEditors(); if (file) return; this.multifileService.addFileForEditorId(editorId); - this.refresh(); - this.sendChangesToAllEditors(); } private onEditorClose(editorId: number) { @@ -445,6 +452,13 @@ export class Tree { dragSource.on('click', () => { this.hub.addInEditorStackIfPossible(dragConfig.bind(this)); + // at this point the editor is initialized with default contents + const mfsState = this.multifileService.getState(); + const newFile = mfsState.files.find(file => file.fileId === mfsState.newFileId - 1); + if (newFile) { + newFile.content = this.hub.getEditorById(newFile.editorId)?.getSource() ?? ''; + newFile.filename = this.hub.getEditorById(newFile.editorId)?.getPaneName() ?? ''; + } }); } @@ -460,7 +474,10 @@ export class Tree { let editor; const editorId = this.hub.nextEditorId(); - if (file) { + if (!file) { + this.multifileService.addFileForEditorId(editorId); + editor = Components.getEditor(this.multifileService.getLanguageId(), editorId); + } else { file.editorId = editorId; editor = Components.getEditor(file.langId, editorId); @@ -468,8 +485,6 @@ export class Tree { if (file.filename) { editor.componentState.filename = file.filename; } - } else { - editor = Components.getEditor(this.multifileService.getLanguageId(), editorId); } return editor;