Merge pull request #4254 from BookStackApp/code_active_line

Updated code view block line highlighting to only show on focus
This commit is contained in:
Dan Brown 2023-05-22 20:44:05 +01:00 committed by GitHub
commit 8a03442b5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,7 @@ import {bracketMatching} from '@codemirror/language';
import { import {
defaultKeymap, history, historyKeymap, indentWithTab, defaultKeymap, history, historyKeymap, indentWithTab,
} from '@codemirror/commands'; } from '@codemirror/commands';
import {EditorState} from '@codemirror/state'; import {Compartment, EditorState} from '@codemirror/state';
import {getTheme} from './themes'; import {getTheme} from './themes';
/** /**
@ -17,12 +17,37 @@ function common(parentEl) {
return [ return [
getTheme(parentEl), getTheme(parentEl),
lineNumbers(), lineNumbers(),
highlightActiveLineGutter(),
drawSelection(), drawSelection(),
dropCursor(), dropCursor(),
bracketMatching(), bracketMatching(),
rectangularSelection(), rectangularSelection(),
];
}
/**
* @returns {({extension: Extension}|readonly Extension[])[]}
*/
function getDynamicActiveLineHighlighter() {
const highlightingCompartment = new Compartment();
const domEvents = {
focus(event, view) {
view.dispatch({
effects: highlightingCompartment.reconfigure([
highlightActiveLineGutter(),
highlightActiveLine(), 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) { export function viewerExtensions(parentEl) {
return [ return [
...common(parentEl), ...common(parentEl),
getDynamicActiveLineHighlighter(),
keymap.of([ keymap.of([
...defaultKeymap, ...defaultKeymap,
]), ]),
@ -47,6 +73,8 @@ export function viewerExtensions(parentEl) {
export function editorExtensions(parentEl) { export function editorExtensions(parentEl) {
return [ return [
...common(parentEl), ...common(parentEl),
highlightActiveLineGutter(),
highlightActiveLine(),
history(), history(),
keymap.of([ keymap.of([
...defaultKeymap, ...defaultKeymap,