Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code editor tab behavior #1520

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
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
12 changes: 1 addition & 11 deletions querybook/webapp/components/QueryEditor/QueryEditor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,12 @@
height: 100%;
border-radius: var(--border-radius-sm);
overflow: hidden;
background-color: var(--bg-query-editor);

&.cm-theme-light {
.cm-editor {
.cm-scroller {
.cm-gutters {
background-color: var(--bg-query-editor-gutter);
}
}
}
}
// dark theme
&.cm-theme {
.cm-editor {
.cm-panels {
background-color: var(--bg-query-editor);
background-color: var(--bg-hover);
}
}
}
Expand Down
17 changes: 13 additions & 4 deletions querybook/webapp/components/QueryEditor/QueryEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { acceptCompletion, startCompletion } from '@codemirror/autocomplete';
import { indentService } from '@codemirror/language';
import { EditorView } from '@codemirror/view';
import CodeMirror, { ReactCodeMirrorRef } from '@uiw/react-codemirror';
import CodeMirror, {
BasicSetupOptions,
ReactCodeMirrorRef,
} from '@uiw/react-codemirror';
import clsx from 'clsx';
import React, {
useCallback,
Expand Down Expand Up @@ -378,7 +381,12 @@ export const QueryEditor: React.FC<
searchExtension,
selectionExtension,
sqlCompleteExtension,
indentService.of((context, pos) => context.lineIndent(pos - 1)),
indentService.of((context, pos) => {
if (pos === 0) {
return 0;
}
return context.lineIndent(pos - 1);
}),
],
[
language,
Expand All @@ -395,12 +403,12 @@ export const QueryEditor: React.FC<
]
);

const basicSetup = useMemo(
const basicSetup = useMemo<BasicSetupOptions>(
() => ({
drawSelection: true,
highlightSelectionMatches: true,
searchKeymap: false,
foldGutter: false,
foldGutter: true,
allowMultipleSelections: true,
}),
[]
Expand Down Expand Up @@ -456,6 +464,7 @@ export const QueryEditor: React.FC<
editable={!readOnly}
autoFocus={false}
onChange={onChangeHandler}
indentWithTab={false}
/>
</div>
);
Expand Down
8 changes: 5 additions & 3 deletions querybook/webapp/components/QueryEditor/themes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { tags as t } from '@lezer/highlight';
import { xcodeLightInit } from '@uiw/codemirror-theme-xcode';

export const CustomMonokaiDarkTheme = monokaiInit({
settings: {},
settings: {
gutterBackground: 'var(--bg-lightest)',
},
styles: [
{ tag: [t.name], color: 'var(--text-dark)' },
{ tag: [t.constant(t.name), t.standard(t.name)], color: '#FD971F' },
Expand All @@ -12,8 +14,8 @@ export const CustomMonokaiDarkTheme = monokaiInit({

export const CustomXcodeTheme = xcodeLightInit({
settings: {
background: 'var(--bg-color)',
gutterBackground: 'var(--bg-color)',
background: 'var(--bg-lightest)',
gutterBackground: 'var(--bg-light)',
},
styles: [
{ tag: [t.special(t.propertyName)], color: '#005cc5' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { KeyBinding, keymap, Prec } from '@uiw/react-codemirror';
import { useCallback, useMemo } from 'react';

import { CodeMirrorKeyMap } from 'lib/codemirror';
import { indentLess, insertTab } from '@codemirror/commands';

export const useKeyMapExtension = ({
keyMap = {},
Expand All @@ -12,7 +13,7 @@ export const useKeyMapExtension = ({
}) => {
// Transform keys like Cmd-F to Cmd-f as codemirror6 expects
const transformKey = useCallback((key: string) => {
let parts = key.split('-');
const parts = key.split('-');
const lastPart = parts[parts.length - 1];

// Check if the last part is a single alphabetical character
Expand All @@ -23,8 +24,8 @@ export const useKeyMapExtension = ({
return parts.join('-');
}, []);

const extension = useMemo(
() =>
const extensions = useMemo(
() => [
Prec.highest(
keymap.of([
...keyBindings.map(({ key, run }) => ({
Expand All @@ -40,8 +41,10 @@ export const useKeyMapExtension = ({
})),
])
),
[keyMap, keyBindings]
keymap.of([{ key: 'Tab', run: insertTab, shift: indentLess }]),
],
[keyBindings, keyMap, transformKey]
);

return extension;
return extensions;
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { indentWithTab } from '@codemirror/commands';
import { indentUnit } from '@codemirror/language';
import { EditorState, EditorView, keymap } from '@uiw/react-codemirror';
import { EditorView } from '@uiw/react-codemirror';
import { useMemo } from 'react';
import { EditorState, Extension } from '@codemirror/state';

export const useOptionsExtension = ({
lineWrapping = true,
Expand All @@ -11,11 +11,10 @@ export const useOptionsExtension = ({
options: Record<string, any>;
}) => {
const extension = useMemo(() => {
const extensions = [];
const extensions: Extension[] = [];

if (options.indentWithTabs) {
extensions.push(indentUnit.of('\t'));
extensions.push(keymap.of([indentWithTab]));
extensions.push(EditorState.tabSize.of(options.tabSize));
} else {
extensions.push(indentUnit.of(' '.repeat(options.indentUnit)));
Expand Down
Loading