diff --git a/README.md b/README.md index 3958b29..ddcb80b 100644 --- a/README.md +++ b/README.md @@ -68,8 +68,14 @@ The options are described below. ### `source: string` -Set the source code to be edited with the `source` getter, and get the current source with the `source` setter. Note -that setting the source will reset the editor state (including undo and redo history). +Get the source code to be edited with the `source` getter, and set the current source with the `source` setter. Note +that setting the source will only update the code shown with the new contents, and the editor state will be maintained +(including undo and redo history). If the intent is to reset the state with a new document use the `newSource` method +instead. + +### `newSource(doc: string): void` + +Set the source code to be edited in the editor. This resets the editor state, and loads the new code. ### `setLanguage(languageName: string): Promise` diff --git a/package-lock.json b/package-lock.json index 5b19271..370b6ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@openwebwork/pg-codemirror-editor", - "version": "0.0.1-beta.9", + "version": "0.0.1-beta.10", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@openwebwork/pg-codemirror-editor", - "version": "0.0.1-beta.9", + "version": "0.0.1-beta.10", "license": "MIT", "dependencies": { "@codemirror/lang-html": "^6.4.9", @@ -5715,9 +5715,9 @@ } }, "node_modules/typescript": { - "version": "5.6.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.2.tgz", - "integrity": "sha512-NW8ByodCSNCwZeghjN3o+JX5OFH0Ojg6sadjEKY4huZ52TqbJTJnDo5+Tw98lSy63NZvi4n+ez5m2u5d4PkZyw==", + "version": "5.5.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.4.tgz", + "integrity": "sha512-Mtq29sKDAEYP7aljRgtPOpTvOfbwRWlS6dPRzwjdE+C0R4brX/GUyhHSecbHMFLNBLcJIPt9nl9yG5TZ1weH+Q==", "dev": true, "license": "Apache-2.0", "bin": { diff --git a/package.json b/package.json index ad1f42b..e7069ef 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@openwebwork/pg-codemirror-editor", - "version": "0.0.1-beta.9", + "version": "0.0.1-beta.10", "description": "PG CodeMirror Editor", "author": "The WeBWorK Project", "license": "MIT", diff --git a/public/editor.js b/public/editor.js index 9ef062d..1bdd324 100644 --- a/public/editor.js +++ b/public/editor.js @@ -17,7 +17,7 @@ if (codeMirrorElt instanceof HTMLElement) { reader.readAsText(file); reader.addEventListener('load', () => { sourceInput.value = reader.result; - pgEditor.source = sourceInput.value; + pgEditor.newSource(sourceInput.value); }); } }); diff --git a/src/pg-codemirror-editor.ts b/src/pg-codemirror-editor.ts index 623f922..7e7dd00 100644 --- a/src/pg-codemirror-editor.ts +++ b/src/pg-codemirror-editor.ts @@ -330,16 +330,20 @@ export class View { } set source(doc: string) { - this.view.setState(EditorState.create({ doc, extensions: this.extensions })); - void this.setLanguage(this.currentLanguage); - void this.setTheme(this.currentTheme); - void this.setKeyMap(this.currentKeyMap); + this.view.dispatch({ changes: { from: 0, to: this.view.state.doc.length, insert: doc } }); } get source() { return this.view.state.doc.toString(); } + newSource(doc: string) { + this.view.setState(EditorState.create({ doc, extensions: this.extensions })); + void this.setLanguage(this.currentLanguage); + void this.setTheme(this.currentTheme); + void this.setKeyMap(this.currentKeyMap); + } + async setLanguage(languageName: string) { const language = this.languages.get(languageName); if (language) {