2023-04-18 17:20:02 -04:00
|
|
|
import {updateViewLanguage} from './views';
|
2023-04-17 08:24:29 -04:00
|
|
|
|
|
|
|
export class SimpleEditorInterface {
|
2023-04-18 17:20:02 -04:00
|
|
|
|
2023-04-17 08:24:29 -04:00
|
|
|
/**
|
|
|
|
* @param {EditorView} editorView
|
|
|
|
*/
|
|
|
|
constructor(editorView) {
|
|
|
|
this.ev = editorView;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the contents of an editor instance.
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
getContent() {
|
|
|
|
return this.ev.state.doc.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the contents of an editor instance.
|
|
|
|
* @param content
|
|
|
|
*/
|
|
|
|
setContent(content) {
|
2023-04-18 17:20:02 -04:00
|
|
|
const {doc} = this.ev.state;
|
2023-04-17 08:24:29 -04:00
|
|
|
this.ev.dispatch({
|
2023-04-18 17:20:02 -04:00
|
|
|
changes: {from: 0, to: doc.length, insert: content},
|
2023-04-17 08:24:29 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return focus to the editor instance.
|
|
|
|
*/
|
|
|
|
focus() {
|
|
|
|
this.ev.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the language mode of the editor instance.
|
|
|
|
* @param {String} mode
|
|
|
|
* @param {String} content
|
|
|
|
*/
|
|
|
|
setMode(mode, content = '') {
|
|
|
|
updateViewLanguage(this.ev, mode, content);
|
|
|
|
}
|
2023-04-18 17:20:02 -04:00
|
|
|
|
|
|
|
}
|