mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-06-18 02:12:30 +08:00
Added handling of codemirror 6 code languages
This commit is contained in:
38
resources/js/code/views.js
Normal file
38
resources/js/code/views.js
Normal file
@ -0,0 +1,38 @@
|
||||
import {getLanguageExtension} from "./languages";
|
||||
import {Compartment} from "@codemirror/state"
|
||||
import {EditorView} from "@codemirror/view"
|
||||
|
||||
const viewLangCompartments = new WeakMap();
|
||||
|
||||
/**
|
||||
* Create a new editor view.
|
||||
*
|
||||
* @param {Object} config
|
||||
* @returns {EditorView}
|
||||
*/
|
||||
export function createView(config) {
|
||||
const langCompartment = new Compartment();
|
||||
config.extensions.push(langCompartment.of([]));
|
||||
|
||||
const ev = new EditorView(config);
|
||||
|
||||
viewLangCompartments.set(ev, langCompartment);
|
||||
|
||||
return ev;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the language mode of an EditorView.
|
||||
*
|
||||
* @param {EditorView} ev
|
||||
* @param {string} modeSuggestion
|
||||
* @param {string} content
|
||||
*/
|
||||
export function updateViewLanguage(ev, modeSuggestion, content) {
|
||||
const compartment = viewLangCompartments.get(ev);
|
||||
const language = getLanguageExtension(modeSuggestion, content);
|
||||
|
||||
ev.dispatch({
|
||||
effects: compartment.reconfigure(language ? language : [])
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user