Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import {
tooltips,
} from '@codemirror/view';
import { classHighlighter } from '@lezer/highlight';
import { useRef } from 'react';
import { useMemo } from 'react';

import { GlobalConstants, isObjectsEqual } from '@cloudbeaver/core-utils';
import { GlobalConstants } from '@cloudbeaver/core-utils';
import { clsx } from '@dbeaver/ui-kit';

// @TODO allow to configure bindings outside of the component
Expand Down Expand Up @@ -119,16 +119,67 @@ const DEFAULT_EXTENSIONS_COMPARTMENT = new Compartment();

/** Provides the necessary extensions to establish a basic editor */
export function useEditorDefaultExtensions(options?: IDefaultExtensions): [Compartment, Extension] {
const previousOptions = useRef(options);
const isOptionsChanged = !isObjectsEqual(options, previousOptions.current);
const extensions = useRef<[Compartment, Extension] | null>(null);
const {
lineNumbers,
tooltips,
highlightSpecialChars,
syntaxHighlighting,
bracketMatching,
dropCursor,
crosshairCursor,
foldGutter,
highlightActiveLineGutter,
highlightSelectionMatches,
highlightActiveLine,
indentOnInput,
rectangularSelection,
keymap,
lineWrapping,
search,
tabIndentation,
} = options ?? {};

if (isOptionsChanged || extensions.current === null) {
previousOptions.current = options;
extensions.current = createExtensions(options);
}

return extensions.current;
return useMemo(
() =>
createExtensions({
lineNumbers,
tooltips,
highlightSpecialChars,
syntaxHighlighting,
bracketMatching,
dropCursor,
crosshairCursor,
foldGutter,
highlightActiveLineGutter,
highlightSelectionMatches,
highlightActiveLine,
indentOnInput,
rectangularSelection,
keymap,
lineWrapping,
search,
tabIndentation,
}),
[
lineNumbers,
tooltips,
highlightSpecialChars,
syntaxHighlighting,
bracketMatching,
dropCursor,
crosshairCursor,
foldGutter,
highlightActiveLineGutter,
highlightSelectionMatches,
highlightActiveLine,
indentOnInput,
rectangularSelection,
keymap,
lineWrapping,
search,
tabIndentation,
],
);
}

function createExtensions(options?: IDefaultExtensions): [Compartment, Extension] {
Expand Down
Loading