2023-04-18 23:20:02 +02:00
|
|
|
import {updateViewLanguage} from './views';
|
2023-04-17 14:24:29 +02:00
|
|
|
|
|
|
|
export class SimpleEditorInterface {
|
2023-04-18 23:20:02 +02:00
|
|
|
|
2023-04-17 14:24:29 +02: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 23:20:02 +02:00
|
|
|
const {doc} = this.ev.state;
|
2023-04-17 14:24:29 +02:00
|
|
|
this.ev.dispatch({
|
2023-04-18 23:20:02 +02:00
|
|
|
changes: {from: 0, to: doc.length, insert: content},
|
2023-04-17 14:24:29 +02: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 23:20:02 +02:00
|
|
|
|
|
|
|
}
|