]> BookStack Code Mirror - bookstack/commitdiff
Updated code view block line highlighting to only show on focus 4254/head
authorDan Brown <redacted>
Mon, 22 May 2023 13:05:07 +0000 (14:05 +0100)
committerDan Brown <redacted>
Mon, 22 May 2023 13:05:07 +0000 (14:05 +0100)
The default 1st line highlighting confused users when existing on
read-only blocks as it was not clear this represented the active line.
This changes the highlight to only show when the block is focused upon.

resources/js/code/setups.js

index 52b9cc199a5b8870eb80e30e11d408441c7e57bc..270d58274347d27a18475a3f97c56c43719e573e 100644 (file)
@@ -6,7 +6,7 @@ import {bracketMatching} from '@codemirror/language';
 import {
     defaultKeymap, history, historyKeymap, indentWithTab,
 } from '@codemirror/commands';
-import {EditorState} from '@codemirror/state';
+import {Compartment, EditorState} from '@codemirror/state';
 import {getTheme} from './themes';
 
 /**
@@ -17,12 +17,37 @@ function common(parentEl) {
     return [
         getTheme(parentEl),
         lineNumbers(),
-        highlightActiveLineGutter(),
         drawSelection(),
         dropCursor(),
         bracketMatching(),
         rectangularSelection(),
-        highlightActiveLine(),
+    ];
+}
+
+/**
+ * @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),
     ];
 }
 
@@ -33,6 +58,7 @@ function common(parentEl) {
 export function viewerExtensions(parentEl) {
     return [
         ...common(parentEl),
+        getDynamicActiveLineHighlighter(),
         keymap.of([
             ...defaultKeymap,
         ]),
@@ -47,6 +73,8 @@ export function viewerExtensions(parentEl) {
 export function editorExtensions(parentEl) {
     return [
         ...common(parentEl),
+        highlightActiveLineGutter(),
+        highlightActiveLine(),
         history(),
         keymap.of([
             ...defaultKeymap,
Morty Proxy This is a proxified and sanitized view of the page, visit original site.