mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-06-14 15:56:38 +08:00
JS Events: Added CM pre/post init events
To allow hacking of all CodeMirror instances. Closes #4639.
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import {Compartment} from '@codemirror/state';
|
||||
import {Compartment, EditorState} from '@codemirror/state';
|
||||
import {EditorView} from '@codemirror/view';
|
||||
import {getLanguageExtension} from './languages';
|
||||
|
||||
@ -7,17 +7,31 @@ const viewLangCompartments = new WeakMap();
|
||||
/**
|
||||
* Create a new editor view.
|
||||
*
|
||||
* @param {String} usageType
|
||||
* @param {{parent: Element, doc: String, extensions: Array}} config
|
||||
* @returns {EditorView}
|
||||
*/
|
||||
export function createView(config) {
|
||||
export function createView(usageType, config) {
|
||||
const langCompartment = new Compartment();
|
||||
config.extensions.push(langCompartment.of([]));
|
||||
|
||||
const ev = new EditorView(config);
|
||||
const commonEventData = {
|
||||
usage: usageType,
|
||||
editorViewConfig: config,
|
||||
libEditorView: EditorView,
|
||||
libEditorState: EditorState,
|
||||
libCompartment: Compartment,
|
||||
};
|
||||
|
||||
// Emit a pre-init public event so the user can tweak the config before instance creation
|
||||
window.$events.emitPublic(config.parent, 'library-cm6::pre-init', commonEventData);
|
||||
|
||||
const ev = new EditorView(config);
|
||||
viewLangCompartments.set(ev, langCompartment);
|
||||
|
||||
// Emit a post-init public event so the user can gain a reference to the EditorView
|
||||
window.$events.emitPublic(config.parent, 'library-cm6::post-init', {editorView: ev, ...commonEventData});
|
||||
|
||||
return ev;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user