diff --git a/packages/editor/src/lib/Workspace.svelte.ts b/packages/editor/src/lib/Workspace.svelte.ts index 565c2bc2b6..317fcf0bc3 100644 --- a/packages/editor/src/lib/Workspace.svelte.ts +++ b/packages/editor/src/lib/Workspace.svelte.ts @@ -286,18 +286,7 @@ export class Workspace { const file = files.find((file) => file.name === name) as File; if (file) { - const existing = state.doc.toString(); - if (file.contents !== existing) { - const transaction = state.update({ - changes: { - from: 0, - to: existing.length, - insert: file.contents - } - }); - - this.states.set(file.name, transaction.state); - } + this.#update_state(file, state); } else { this.states.delete(name); } @@ -312,6 +301,10 @@ export class Workspace { } update_file(file: File) { + if (file.name === this.#current.name) { + this.#current = file; + } + this.#files = this.#files.map((old) => { if (old.name === file.name) { return file; @@ -327,6 +320,11 @@ export class Workspace { }); } + const state = this.states.get(file.name); + if (state) { + this.#update_state(file, state); + } + this.#onupdate(file); } @@ -448,4 +446,24 @@ export class Workspace { this.#current = file as File; this.#view?.setState(this.#get_state(this.#current)); } + + #update_state(file: File, state: EditorState) { + const existing = state.doc.toString(); + + if (file.contents !== existing) { + const transaction = state.update({ + changes: { + from: 0, + to: existing.length, + insert: file.contents + } + }); + + this.states.set(file.name, transaction.state); + + if (file === this.#current) { + this.#view?.setState(transaction.state); + } + } + } }