Skip to content

Commit

Permalink
Make the source setter not reset the editor state.
Browse files Browse the repository at this point in the history
It now only changes the source code in the editor, but maintains
undo/redo history.

A new `setSource` method is added that will reset the editor state
entirely (as the source setter did before).
  • Loading branch information
drgrice1 committed Oct 12, 2024
1 parent 8eb69c8 commit cd8a566
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>`

Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion public/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
});
Expand Down
12 changes: 8 additions & 4 deletions src/pg-codemirror-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit cd8a566

Please sign in to comment.