Skip to content

Commit cf93aa3

Browse files
authored
feat: support search (#17)
* feat: support search * fix: code block placeholder bugs
1 parent a63e6ff commit cf93aa3

File tree

6 files changed

+66
-55
lines changed

6 files changed

+66
-55
lines changed

src/components/app/SideBar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import React, { lazy } from 'react';
44
import { Workspaces } from '@/components/app/workspaces';
55
import Outline from 'src/components/app/outline/Outline';
66
import { UIVariant } from '@/application/types';
7-
// import { Search } from 'src/components/app/search';
7+
import { Search } from 'src/components/app/search';
88

99
const SideBarBottom = lazy(() => import('@/components/app/SideBarBottom'));
1010

@@ -44,7 +44,7 @@ function SideBar ({
4444
<div
4545
className={'px-[10px] bg-bg-base z-[1] flex-col gap-2 justify-around items-center sticky top-12'}
4646
>
47-
{/*<Search />*/}
47+
<Search />
4848
<div
4949
style={{
5050
borderColor: scrollTop > 10 ? 'var(--line-divider)' : undefined,

src/components/editor/components/blocks/code/Code.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { useCodeBlock } from '@/components/editor/components/blocks/code/Code.hooks';
22
import CodeToolbar from './CodeToolbar';
33
import { CodeNode, EditorElementProps } from '@/components/editor/editor.type';
4-
import React, { forwardRef, memo, useState, lazy, Suspense } from 'react';
4+
import React, { forwardRef, useState, lazy, Suspense } from 'react';
55
import { ReactEditor, useReadOnly, useSlateStatic } from 'slate-react';
66
import LanguageSelect from './SelectLanguage';
77
import { Element } from 'slate';
88

99
const MermaidChat = lazy(() => import('./MermaidChat'));
1010

11-
export const CodeBlock = memo(
11+
export const CodeBlock =
1212
forwardRef<HTMLDivElement, EditorElementProps<CodeNode>>(({ node, children, ...attributes }, ref) => {
1313
const { language, handleChangeLanguage } = useCodeBlock(node);
1414
const [showToolbar, setShowToolbar] = useState(false);
@@ -64,7 +64,5 @@ export const CodeBlock = memo(
6464
{showToolbar && <CodeToolbar node={node} />}
6565
</div>
6666
);
67-
}),
68-
(prevProps, nextProps) => JSON.stringify(prevProps.node) === JSON.stringify(nextProps.node),
69-
);
67+
});
7068
export default CodeBlock;

src/components/editor/components/blocks/code/CodeToolbar.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import { notify } from '@/components/_shared/notify';
22
import ActionButton from '@/components/editor/components/toolbar/selection-toolbar/actions/ActionButton';
33
import { CodeNode } from '@/components/editor/editor.type';
44
import { copyTextToClipboard } from '@/utils/copy';
5-
import React from 'react';
5+
import React, { memo } from 'react';
66
import { useTranslation } from 'react-i18next';
77
import { ReactEditor, useSlateStatic } from 'slate-react';
88
import { ReactComponent as CopyIcon } from '@/assets/copy.svg';
99

10-
function CodeToolbar ({ node }: {
10+
function CodeToolbar({ node }: {
1111
node: CodeNode
1212
}) {
1313
const { t } = useTranslation();
1414
const editor = useSlateStatic();
15-
const onCopy = async () => {
15+
const onCopy = async() => {
1616
const at = ReactEditor.findPath(editor, node);
1717
const text = editor.string(at);
1818

@@ -34,4 +34,4 @@ function CodeToolbar ({ node }: {
3434
);
3535
}
3636

37-
export default CodeToolbar;
37+
export default memo(CodeToolbar);

src/components/editor/components/blocks/code/SelectLanguage.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { Popover } from '@/components/_shared/popover';
33
import { supportLanguages } from '@/components/editor/components/blocks/code/constants';
44
import { createHotkey, HOT_KEY_NAME } from '@/utils/hotkeys';
55
import { Button, TextField } from '@mui/material';
6-
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
6+
import React, { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
77
import { useTranslation } from 'react-i18next';
88

9-
function SelectLanguage ({
9+
function SelectLanguage({
1010
readOnly,
1111
language = 'Auto',
1212
onChangeLanguage,
@@ -54,18 +54,18 @@ function SelectLanguage ({
5454
}, [language]);
5555

5656
useEffect(() => {
57-
if (!open) return;
57+
if(!open) return;
5858
searchRef.current?.focus();
5959
}, [open]);
6060

6161
useEffect(() => {
6262
const container = scrollRef.current;
6363

64-
if (!container) return;
64+
if(!container) return;
6565

6666
const el = container.querySelector(`[data-key="${selectLanguage}"]`);
6767

68-
if (!el) return;
68+
if(!el) return;
6969

7070
el.scrollIntoView({ block: 'nearest' });
7171
}, [selectLanguage]);
@@ -82,7 +82,7 @@ function SelectLanguage ({
8282
className={'px-4'}
8383
variant={'text'}
8484
onClick={() => {
85-
if (readOnly) return;
85+
if(readOnly) return;
8686
setOpen(true);
8787
}}
8888
>
@@ -112,19 +112,19 @@ function SelectLanguage ({
112112
className={'px-3 py-1 text-xs'}
113113
placeholder={t('search.label')}
114114
onKeyDown={(e) => {
115-
if (createHotkey(HOT_KEY_NAME.ENTER)(e.nativeEvent)) {
115+
if(createHotkey(HOT_KEY_NAME.ENTER)(e.nativeEvent)) {
116116
e.preventDefault();
117117
handleConfirm(selectLanguage);
118118
}
119119

120-
if (createHotkey(HOT_KEY_NAME.UP)(e.nativeEvent)) {
120+
if(createHotkey(HOT_KEY_NAME.UP)(e.nativeEvent)) {
121121
const index = options.findIndex((item) => item.key === selectLanguage);
122122
const prevIndex = (index - 1 + options.length) % options.length;
123123

124124
setSelectLanguage(options[prevIndex].key);
125125
}
126126

127-
if (createHotkey(HOT_KEY_NAME.DOWN)(e.nativeEvent)) {
127+
if(createHotkey(HOT_KEY_NAME.DOWN)(e.nativeEvent)) {
128128
const index = options.findIndex((item) => item.key === selectLanguage);
129129
const nextIndex = (index + 1) % options.length;
130130

@@ -158,4 +158,4 @@ function SelectLanguage ({
158158
);
159159
}
160160

161-
export default SelectLanguage;
161+
export default memo(SelectLanguage);

src/components/editor/components/blocks/text/Placeholder.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ReactEditor, useFocused, useSelected, useSlate } from 'slate-react';
66
import { Editor, Element, Range } from 'slate';
77
import { useTranslation } from 'react-i18next';
88

9-
function Placeholder ({ node, ...attributes }: { node: Element; className?: string; style?: CSSProperties }) {
9+
function Placeholder({ node, ...attributes }: { node: Element; className?: string; style?: CSSProperties }) {
1010
const { t } = useTranslation();
1111
const { readOnly } = useEditorContext();
1212
const editor = useSlate();
@@ -24,7 +24,7 @@ function Placeholder ({ node, ...attributes }: { node: Element; className?: stri
2424
at: path,
2525
});
2626

27-
if (!match) return null;
27+
if(!match) return null;
2828

2929
return match[0] as Element;
3030
}, [editor, node]);
@@ -40,15 +40,15 @@ function Placeholder ({ node, ...attributes }: { node: Element; className?: stri
4040
}, [attributes.className]);
4141

4242
const unSelectedPlaceholder = useMemo(() => {
43-
switch (block?.type) {
43+
switch(block?.type) {
4444
case BlockType.Paragraph: {
4545
return '';
4646
}
4747

4848
case BlockType.ToggleListBlock: {
4949
const level = (block as ToggleListNode).data.level;
5050

51-
switch (level) {
51+
switch(level) {
5252
case 1:
5353
return t('editor.mobileHeading1');
5454
case 2:
@@ -77,7 +77,7 @@ function Placeholder ({ node, ...attributes }: { node: Element; className?: stri
7777
case BlockType.HeadingBlock: {
7878
const level = (block as HeadingNode).data.level;
7979

80-
switch (level) {
80+
switch(level) {
8181
case 1:
8282
return t('editor.mobileHeading1');
8383
case 2:
@@ -104,11 +104,11 @@ function Placeholder ({ node, ...attributes }: { node: Element; className?: stri
104104

105105
const selectedPlaceholder = useMemo(() => {
106106

107-
if (block?.type === BlockType.ToggleListBlock && (block?.data as ToggleListBlockData).level) {
107+
if(block?.type === BlockType.ToggleListBlock && (block?.data as ToggleListBlockData).level) {
108108
return unSelectedPlaceholder;
109109
}
110110

111-
switch (block?.type) {
111+
switch(block?.type) {
112112
case BlockType.HeadingBlock:
113113
return unSelectedPlaceholder;
114114
case BlockType.ToggleListBlock:
@@ -125,7 +125,7 @@ function Placeholder ({ node, ...attributes }: { node: Element; className?: stri
125125
}, [block?.data, block?.type, t, unSelectedPlaceholder]);
126126

127127
useEffect(() => {
128-
if (!selected) return;
128+
if(!selected) return;
129129

130130
const handleCompositionStart = () => {
131131
setIsComposing(true);
@@ -148,7 +148,7 @@ function Placeholder ({ node, ...attributes }: { node: Element; className?: stri
148148
};
149149
}, [editor, selected]);
150150

151-
if (isComposing) {
151+
if(isComposing) {
152152
return null;
153153
}
154154

0 commit comments

Comments
 (0)