Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions packages/editor/src/lib/Workspace.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
Expand All @@ -327,6 +320,11 @@ export class Workspace {
});
}

const state = this.states.get(file.name);
if (state) {
this.#update_state(file, state);
}

this.#onupdate(file);
}

Expand Down Expand Up @@ -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);
}
}
}
}
Loading