2023-04-18 17:20:02 -04:00
|
|
|
import {
|
|
|
|
EditorView, keymap, drawSelection, highlightActiveLine, dropCursor,
|
|
|
|
rectangularSelection, lineNumbers, highlightActiveLineGutter,
|
|
|
|
} from '@codemirror/view';
|
|
|
|
import {bracketMatching} from '@codemirror/language';
|
|
|
|
import {
|
|
|
|
defaultKeymap, history, historyKeymap, indentWithTab,
|
|
|
|
} from '@codemirror/commands';
|
2023-05-22 09:05:07 -04:00
|
|
|
import {Compartment, EditorState} from '@codemirror/state';
|
2023-04-18 17:20:02 -04:00
|
|
|
import {getTheme} from './themes';
|
2022-08-02 15:11:02 -04:00
|
|
|
|
2023-04-16 11:05:16 -04:00
|
|
|
/**
|
|
|
|
* @param {Element} parentEl
|
|
|
|
* @return {(Extension[]|{extension: Extension}|readonly Extension[])[]}
|
|
|
|
*/
|
|
|
|
function common(parentEl) {
|
2022-08-02 15:11:02 -04:00
|
|
|
return [
|
2023-04-16 11:05:16 -04:00
|
|
|
getTheme(parentEl),
|
2022-08-02 15:11:02 -04:00
|
|
|
lineNumbers(),
|
|
|
|
drawSelection(),
|
|
|
|
dropCursor(),
|
|
|
|
bracketMatching(),
|
|
|
|
rectangularSelection(),
|
2023-05-22 09:05:07 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {({extension: Extension}|readonly Extension[])[]}
|
|
|
|
*/
|
|
|
|
function getDynamicActiveLineHighlighter() {
|
|
|
|
const highlightingCompartment = new Compartment();
|
|
|
|
const domEvents = {
|
|
|
|
focus(event, view) {
|
|
|
|
view.dispatch({
|
|
|
|
effects: highlightingCompartment.reconfigure([
|
|
|
|
highlightActiveLineGutter(),
|
|
|
|
highlightActiveLine(),
|
|
|
|
]),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
blur(event, view) {
|
|
|
|
view.dispatch({
|
|
|
|
effects: highlightingCompartment.reconfigure([]),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return [
|
|
|
|
highlightingCompartment.of([]),
|
|
|
|
EditorView.domEventHandlers(domEvents),
|
2023-04-16 11:05:16 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Element} parentEl
|
|
|
|
* @return {*[]}
|
|
|
|
*/
|
2023-04-17 08:24:29 -04:00
|
|
|
export function viewerExtensions(parentEl) {
|
2023-04-16 11:05:16 -04:00
|
|
|
return [
|
|
|
|
...common(parentEl),
|
2023-05-22 09:05:07 -04:00
|
|
|
getDynamicActiveLineHighlighter(),
|
2022-08-02 15:11:02 -04:00
|
|
|
keymap.of([
|
|
|
|
...defaultKeymap,
|
|
|
|
]),
|
|
|
|
EditorState.readOnly.of(true),
|
|
|
|
];
|
2023-04-10 10:01:44 -04:00
|
|
|
}
|
|
|
|
|
2023-04-16 11:05:16 -04:00
|
|
|
/**
|
|
|
|
* @param {Element} parentEl
|
|
|
|
* @return {*[]}
|
|
|
|
*/
|
2023-04-17 08:24:29 -04:00
|
|
|
export function editorExtensions(parentEl) {
|
2023-04-10 10:01:44 -04:00
|
|
|
return [
|
2023-04-16 11:05:16 -04:00
|
|
|
...common(parentEl),
|
2023-05-22 09:05:07 -04:00
|
|
|
highlightActiveLineGutter(),
|
|
|
|
highlightActiveLine(),
|
2023-04-10 10:01:44 -04:00
|
|
|
history(),
|
|
|
|
keymap.of([
|
|
|
|
...defaultKeymap,
|
|
|
|
...historyKeymap,
|
2023-04-18 08:41:28 -04:00
|
|
|
indentWithTab,
|
2023-04-10 10:01:44 -04:00
|
|
|
]),
|
2023-04-14 09:08:40 -04:00
|
|
|
EditorView.lineWrapping,
|
2023-04-10 10:01:44 -04:00
|
|
|
];
|
2023-04-18 17:20:02 -04:00
|
|
|
}
|