2023-04-19 05:20:02 +08: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 21:05:07 +08:00
|
|
|
import {Compartment, EditorState} from '@codemirror/state';
|
2023-04-19 05:20:02 +08:00
|
|
|
import {getTheme} from './themes';
|
2022-08-03 03:11:02 +08:00
|
|
|
|
2023-04-16 23:05:16 +08:00
|
|
|
/**
|
|
|
|
* @param {Element} parentEl
|
|
|
|
* @return {(Extension[]|{extension: Extension}|readonly Extension[])[]}
|
|
|
|
*/
|
|
|
|
function common(parentEl) {
|
2022-08-03 03:11:02 +08:00
|
|
|
return [
|
2023-04-16 23:05:16 +08:00
|
|
|
getTheme(parentEl),
|
2022-08-03 03:11:02 +08:00
|
|
|
lineNumbers(),
|
|
|
|
drawSelection(),
|
|
|
|
dropCursor(),
|
|
|
|
bracketMatching(),
|
|
|
|
rectangularSelection(),
|
2023-05-22 21:05:07 +08: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 23:05:16 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Element} parentEl
|
|
|
|
* @return {*[]}
|
|
|
|
*/
|
2023-04-17 20:24:29 +08:00
|
|
|
export function viewerExtensions(parentEl) {
|
2023-04-16 23:05:16 +08:00
|
|
|
return [
|
|
|
|
...common(parentEl),
|
2023-05-22 21:05:07 +08:00
|
|
|
getDynamicActiveLineHighlighter(),
|
2022-08-03 03:11:02 +08:00
|
|
|
keymap.of([
|
|
|
|
...defaultKeymap,
|
|
|
|
]),
|
|
|
|
EditorState.readOnly.of(true),
|
|
|
|
];
|
2023-04-10 22:01:44 +08:00
|
|
|
}
|
|
|
|
|
2023-04-16 23:05:16 +08:00
|
|
|
/**
|
|
|
|
* @param {Element} parentEl
|
|
|
|
* @return {*[]}
|
|
|
|
*/
|
2023-04-17 20:24:29 +08:00
|
|
|
export function editorExtensions(parentEl) {
|
2023-04-10 22:01:44 +08:00
|
|
|
return [
|
2023-04-16 23:05:16 +08:00
|
|
|
...common(parentEl),
|
2023-05-22 21:05:07 +08:00
|
|
|
highlightActiveLineGutter(),
|
|
|
|
highlightActiveLine(),
|
2023-04-10 22:01:44 +08:00
|
|
|
history(),
|
|
|
|
keymap.of([
|
|
|
|
...defaultKeymap,
|
|
|
|
...historyKeymap,
|
2023-04-18 20:41:28 +08:00
|
|
|
indentWithTab,
|
2023-04-10 22:01:44 +08:00
|
|
|
]),
|
2023-04-14 21:08:40 +08:00
|
|
|
EditorView.lineWrapping,
|
2023-04-10 22:01:44 +08:00
|
|
|
];
|
2023-04-19 05:20:02 +08:00
|
|
|
}
|