Skip to content

Commit 8a03442

Browse files
authored
Merge pull request #4254 from BookStackApp/code_active_line
Updated code view block line highlighting to only show on focus
2 parents e591f48 + 924f517 commit 8a03442

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

resources/js/code/setups.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {bracketMatching} from '@codemirror/language';
66
import {
77
defaultKeymap, history, historyKeymap, indentWithTab,
88
} from '@codemirror/commands';
9-
import {EditorState} from '@codemirror/state';
9+
import {Compartment, EditorState} from '@codemirror/state';
1010
import {getTheme} from './themes';
1111

1212
/**
@@ -17,12 +17,37 @@ function common(parentEl) {
1717
return [
1818
getTheme(parentEl),
1919
lineNumbers(),
20-
highlightActiveLineGutter(),
2120
drawSelection(),
2221
dropCursor(),
2322
bracketMatching(),
2423
rectangularSelection(),
25-
highlightActiveLine(),
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),
2651
];
2752
}
2853

@@ -33,6 +58,7 @@ function common(parentEl) {
3358
export function viewerExtensions(parentEl) {
3459
return [
3560
...common(parentEl),
61+
getDynamicActiveLineHighlighter(),
3662
keymap.of([
3763
...defaultKeymap,
3864
]),
@@ -47,6 +73,8 @@ export function viewerExtensions(parentEl) {
4773
export function editorExtensions(parentEl) {
4874
return [
4975
...common(parentEl),
76+
highlightActiveLineGutter(),
77+
highlightActiveLine(),
5078
history(),
5179
keymap.of([
5280
...defaultKeymap,

0 commit comments

Comments
 (0)