]> BookStack Code Mirror - bookstack/blob - resources/js/code/setups.js
Merge pull request #5626 from BookStackApp/rubentalstra-development
[bookstack] / resources / js / code / setups.js
1 import {
2     EditorView, keymap, drawSelection, highlightActiveLine, dropCursor,
3     rectangularSelection, lineNumbers, highlightActiveLineGutter,
4 } from '@codemirror/view';
5 import {bracketMatching} from '@codemirror/language';
6 import {
7     defaultKeymap, history, historyKeymap, indentWithTab,
8 } from '@codemirror/commands';
9 import {Compartment, EditorState} from '@codemirror/state';
10 import {getTheme} from './themes';
11
12 /**
13  * @param {Element} parentEl
14  * @return {(Extension[]|{extension: Extension}|readonly Extension[])[]}
15  */
16 function common(parentEl) {
17     return [
18         getTheme(parentEl),
19         lineNumbers(),
20         drawSelection(),
21         dropCursor(),
22         bracketMatching(),
23         rectangularSelection(),
24     ];
25 }
26
27 /**
28  * @returns {({extension: Extension}|readonly Extension[])[]}
29  */
30 function getDynamicActiveLineHighlighter() {
31     const highlightingCompartment = new Compartment();
32     const domEvents = {
33         focus(event, view) {
34             view.dispatch({
35                 effects: highlightingCompartment.reconfigure([
36                     highlightActiveLineGutter(),
37                     highlightActiveLine(),
38                 ]),
39             });
40         },
41         blur(event, view) {
42             view.dispatch({
43                 effects: highlightingCompartment.reconfigure([]),
44             });
45         },
46     };
47
48     return [
49         highlightingCompartment.of([]),
50         EditorView.domEventHandlers(domEvents),
51     ];
52 }
53
54 /**
55  * @param {Element} parentEl
56  * @return {*[]}
57  */
58 export function viewerExtensions(parentEl) {
59     return [
60         ...common(parentEl),
61         getDynamicActiveLineHighlighter(),
62         keymap.of([
63             ...defaultKeymap,
64         ]),
65         EditorState.readOnly.of(true),
66     ];
67 }
68
69 /**
70  * @param {Element} parentEl
71  * @return {*[]}
72  */
73 export function editorExtensions(parentEl) {
74     return [
75         ...common(parentEl),
76         highlightActiveLineGutter(),
77         highlightActiveLine(),
78         history(),
79         keymap.of([
80             ...defaultKeymap,
81             ...historyKeymap,
82             indentWithTab,
83         ]),
84         EditorView.lineWrapping,
85     ];
86 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.