Skip to content

Commit f5941a7

Browse files
authored
fix: code block disables continuous input of characters (AppFlowy-IO#12)
1 parent 3d58cc8 commit f5941a7

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function MermaidChat ({ node }: {
6969
const isDark = useContext(ThemeModeContext)?.isDark;
7070
const [error, setError] = React.useState<string | null>(null);
7171

72-
const updateMermaid = useCallback(async () => {
72+
const updateMermaid = useCallback(async (diagram: string) => {
7373
const sanitizedDiagram = sanitizeDiagram(diagram);
7474
const theme = isDark ? darkTheme : lightTheme;
7575

@@ -91,17 +91,23 @@ function MermaidChat ({ node }: {
9191
// @ts-ignore
9292
setError(e.message);
9393
}
94-
}, [diagram, id, isDark]);
94+
}, [id, isDark]);
9595

9696
const deboucenUpdateMermaid = useMemo(() => {
9797
return debounce(updateMermaid, 1000);
9898
}, [updateMermaid]);
9999

100100
useEffect(() => {
101-
void deboucenUpdateMermaid();
102-
}, [deboucenUpdateMermaid]);
101+
if (!diagram.trim()) {
102+
setError(null);
103+
setInnerHtml('');
104+
return;
105+
}
106+
107+
void deboucenUpdateMermaid(diagram);
108+
}, [deboucenUpdateMermaid, diagram]);
103109

104-
if (error) {
110+
if (error && diagram) {
105111
return (
106112
<div
107113
contentEditable={false}
@@ -121,7 +127,6 @@ function MermaidChat ({ node }: {
121127
display: 'flex',
122128
flexDirection: 'row',
123129
placeContent: 'center',
124-
minHeight: '250px',
125130
}}
126131
contentEditable={false}
127132
ref={ref}

src/components/editor/utils/markdown.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type Rule = {
5454
filter?: (editor: YjsEditor, match: RegExpMatchArray) => boolean
5555
}
5656

57-
function deletePrefix(editor: YjsEditor, offset: number) {
57+
function deletePrefix (editor: YjsEditor, offset: number) {
5858
const [, path] = getBlockEntry(editor);
5959

6060
const { selection } = editor;
@@ -67,19 +67,19 @@ function deletePrefix(editor: YjsEditor, offset: number) {
6767
editor.delete();
6868
}
6969

70-
function getNodeType(editor: YjsEditor) {
70+
function getNodeType (editor: YjsEditor) {
7171
const [node] = getBlockEntry(editor);
7272

7373
return node.type as BlockType;
7474
}
7575

76-
function getBlockData(editor: YjsEditor) {
76+
function getBlockData (editor: YjsEditor) {
7777
const [node] = getBlockEntry(editor);
7878

7979
return node.data as BlockData;
8080
}
8181

82-
function getLineText(editor: YjsEditor) {
82+
function getLineText (editor: YjsEditor) {
8383
const [node] = getBlockEntry(editor);
8484
const sharedRoot = getSharedRoot(editor);
8585
const block = getBlock(node.blockId as string, sharedRoot);
@@ -331,7 +331,6 @@ const rules: Rule[] = [
331331
match: /--/,
332332
format: SpecialSymbol.EM_DASH,
333333
transform: (editor) => {
334-
335334
editor.delete({
336335
unit: 'character',
337336
reverse: true,
@@ -422,6 +421,9 @@ export const applyMarkdown = (editor: YjsEditor, insertText: string): boolean =>
422421
}
423422

424423
} else if (rule.type === 'symbol') {
424+
const block = getBlockEntry(editor)[0];
425+
426+
if (block.type === BlockType.CodeBlock) return false;
425427
const path = selection.anchor.path;
426428
const text = editor.string({
427429
anchor: {

0 commit comments

Comments
 (0)