Skip to content

Commit

Permalink
fix rerender issue and enable mutlie select
Browse files Browse the repository at this point in the history
  • Loading branch information
jczhong84 committed Nov 19, 2024
1 parent 80ffb22 commit e76750d
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 53 deletions.
10 changes: 2 additions & 8 deletions querybook/webapp/components/QueryEditor/BoundQueryEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useEffect, useMemo } from 'react';
import React, { useContext, useEffect, useMemo, useRef } from 'react';
import { useDispatch } from 'react-redux';

import {
Expand All @@ -7,13 +7,9 @@ import {
QueryEditor,
} from 'components/QueryEditor/QueryEditor';
import { IQueryEngine } from 'const/queryEngine';
import { SearchAndReplaceContext } from 'context/searchAndReplace';
import { useUserQueryEditorConfig } from 'hooks/redux/useUserQueryEditorConfig';
import { useForwardedRef } from 'hooks/useForwardedRef';
import {
fetchDataTableByNameIfNeeded,
fetchFunctionDocumentationIfNeeded,
} from 'redux/dataSources/action';
import { fetchDataTableByNameIfNeeded } from 'redux/dataSources/action';

export const BoundQueryEditor = React.forwardRef<
IQueryEditorHandles,
Expand All @@ -34,7 +30,6 @@ export const BoundQueryEditor = React.forwardRef<
}
>(({ options: propOptions, keyMap, engine, cellId, ...otherProps }, ref) => {
const dispatch = useDispatch();
const searchContext = useContext(SearchAndReplaceContext);
const editorRef = useForwardedRef<IQueryEditorHandles>(ref);

// Code Editor related Props
Expand Down Expand Up @@ -81,7 +76,6 @@ export const BoundQueryEditor = React.forwardRef<
getTableByName={fetchDataTable}
metastoreId={engine?.metastore_id}
language={engine?.language}
searchContext={searchContext}
cellId={cellId}
engineId={engine?.id}
sqlCompleteEnabled={sqlCompleteEnabled}
Expand Down
7 changes: 1 addition & 6 deletions querybook/webapp/components/QueryEditor/QueryEditor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,14 @@
height: 100%;
border-radius: var(--border-radius-sm);
overflow: hidden;
background-color: var(--bg-query-editor);

&.cm-theme-light {
.cm-editor {
.cm-scroller {
background-color: var(--bg-query-editor-gutter);

.cm-gutters {
background-color: var(--bg-query-editor-gutter);
}

.cm-content {
background-color: var(--bg-query-editor);
}
}
}
}
Expand Down
29 changes: 11 additions & 18 deletions querybook/webapp/components/QueryEditor/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import toast from 'react-hot-toast';
import { TDataDocMetaVariables } from 'const/datadoc';
import KeyMap from 'const/keyMap';
import { IDataTable } from 'const/metastore';
import { ISearchAndReplaceContextType } from 'context/searchAndReplace';
import { useAutoCompleteExtension } from 'hooks/queryEditor/extensions/useAutoCompleteExtension';
import { useEventsExtension } from 'hooks/queryEditor/extensions/useEventsExtension';
import { useHoverTooltipExtension } from 'hooks/queryEditor/extensions/useHoverTooltipExtension';
Expand Down Expand Up @@ -55,7 +54,6 @@ export interface IQueryEditorProps {
engineId: number;
templatedVariables?: TDataDocMetaVariables;
cellId?: number;
searchContext?: ISearchAndReplaceContextType;

fontSize?: string;
height?: 'auto' | 'full' | 'fixed';
Expand Down Expand Up @@ -109,7 +107,6 @@ export const QueryEditor: React.FC<
engineId,
cellId,
templatedVariables = [],
searchContext,

hasQueryLint,
height = 'auto',
Expand Down Expand Up @@ -270,13 +267,12 @@ export const QueryEditor: React.FC<

const searchExtension = useSearchExtension({
editorView: editorRef.current?.view,
searchContext,
cellId,
});

const eventsExtension = useEventsExtension({
onFocus: (evt) => onFocus?.(),
onBlur: (evt) => onBlur?.(),
onFocus,
onBlur,
});

const statusBarExtension = useStatusBarExtension({
Expand Down Expand Up @@ -313,9 +309,8 @@ export const QueryEditor: React.FC<
return true;
}, []);

const keyMapExtention = useKeyMapExtension({
keyMap,
keyBindings: [
const keyBindings = useMemo(
() => [
{ key: 'Tab', run: acceptCompletion },
{
key: KeyMap.queryEditor.autocomplete.key,
Expand All @@ -328,18 +323,16 @@ export const QueryEditor: React.FC<
return true;
},
},
{
key: 'Cmd-F',
run: () => {
searchContext?.showSearchAndReplace();
return true;
},
},
{
key: KeyMap.queryEditor.openTable.key,
run: openTableModalCommand,
},
],
[formatQuery, openTableModalCommand]
);
const keyMapExtention = useKeyMapExtension({
keyMap,
keyBindings,
});

const optionsExtension = useOptionsExtension({
Expand Down Expand Up @@ -367,7 +360,6 @@ export const QueryEditor: React.FC<
const extensions = useMemo(
() => [
mixedSQL(),

keyMapExtention,
statusBarExtension,
eventsExtension,
Expand Down Expand Up @@ -395,10 +387,11 @@ export const QueryEditor: React.FC<

const basicSetup = useMemo(
() => ({
drawSelection: false,
drawSelection: true,
highlightSelectionMatches: true,
searchKeymap: false,
foldGutter: false,
allowMultipleSelections: true,
}),
[]
);
Expand Down
34 changes: 23 additions & 11 deletions querybook/webapp/components/SearchAndReplace/SearchAndReplace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, {
useCallback,
useEffect,
useImperativeHandle,
useMemo,
useRef,
useState,
} from 'react';
Expand Down Expand Up @@ -68,16 +69,16 @@ export function useSearchAndReplace({

const reset = useCallback(() => setSearchState(initialSearchState), []);
const performSearch = useCallback(
// Active search happens when user is using the search input
// Passive search is when the search content is changed
(isActiveSearch: boolean = false) => {
if (!showing) {
return;
}

setSearchState((oldSearchState) => {
const searchResults = showing
? getSearchResults(
oldSearchState.searchString,
oldSearchState.searchOptions
)
: [];
const searchResults = getSearchResults(
oldSearchState.searchString,
oldSearchState.searchOptions
);

if (isActiveSearch) {
jumpToResult(
Expand Down Expand Up @@ -195,15 +196,26 @@ export function useSearchAndReplace({
setShowing(false);
}, []);

return {
searchAndReplaceContext: {
const searchAndReplaceContext = useMemo(
() => ({
searchState,
focusSearchBar,

showSearchAndReplace,
hideSearchAndReplace,
showing,
},
}),
[
searchState,
focusSearchBar,
showSearchAndReplace,
hideSearchAndReplace,
showing,
]
);

return {
searchAndReplaceContext,
searchAndReplaceProps: {
onSearchStringChange,
onReplaceStringChange,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import * as events from '@uiw/codemirror-extensions-events';
import { useMemo } from 'react';

export const useEventsExtension = ({ onFocus, onBlur }) => {
export const useEventsExtension = ({
onFocus,
onBlur,
}: {
onFocus?: () => void;
onBlur?: () => void;
}) => {
const extension = useMemo(
() =>
events.content({
focus: onFocus,
blur: onBlur,
focus: (evt) => onFocus?.(),
blur: (evt) => onBlur?.(),
}),
[onFocus, onBlur]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ import {
SearchQuery,
setSearchQuery,
} from '@codemirror/search';
import { EditorSelection, EditorView } from '@uiw/react-codemirror';
import { useEffect, useMemo } from 'react';
import {
EditorSelection,
EditorView,
keymap,
Prec,
} from '@uiw/react-codemirror';
import { useContext, useEffect, useMemo } from 'react';

import { ISearchAndReplaceContextType } from 'context/searchAndReplace';
import { SearchAndReplaceContext } from 'context/searchAndReplace';

export const useSearchExtension = ({
editorView,
cellId,
searchContext,
}: {
editorView: EditorView;
cellId: number;
searchContext?: ISearchAndReplaceContextType;
}) => {
const searchContext = useContext(SearchAndReplaceContext);
useEffect(() => {
if (editorView && searchContext) {
if (searchContext.showing) {
Expand All @@ -30,7 +34,7 @@ export const useSearchExtension = ({
closeSearchPanel(editorView);
}
}
}, [editorView, searchContext]);
}, [editorView, searchContext?.showing]);

const shouldHighlight = useMemo(
() =>
Expand Down Expand Up @@ -80,7 +84,23 @@ export const useSearchExtension = ({
}
}, [shouldHighlight, searchContext]);

const extension = useMemo(() => search(), []);
const extension = useMemo(
() => [
search(),
Prec.highest(
keymap.of([
{
key: 'Cmd-f',
run: () => {
searchContext?.showSearchAndReplace();
return true;
},
},
])
),
],
[]
);

return extension;
};

0 comments on commit e76750d

Please sign in to comment.