Skip to content

Commit 9e236f6

Browse files
committed
Merge remote-tracking branch 'origin/feat/1.7.2/ui' into test
2 parents cf7f601 + 762e8a7 commit 9e236f6

35 files changed

+1627
-1039
lines changed

ui/.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
22

33
# dependencies
4-
/node_modules
4+
node_modules
55
/.pnp
66
.pnp.js
77

@@ -36,6 +36,4 @@ package-lock.json
3636
/src/plugins/*
3737
!/src/plugins/builtin
3838
!/src/plugins/Demo
39-
!/src/plugins/answer-chart
40-
!/src/plugins/answer-formula
4139
/src/plugins/*/*.go

ui/pnpm-lock.yaml

Lines changed: 261 additions & 110 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui/src/components/Editor/EditorContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919

2020
import React from 'react';
2121

22-
import { IEditorContext } from './types';
22+
import { Editor } from './types';
2323

24-
export const EditorContext = React.createContext<IEditorContext | any>({});
24+
export const EditorContext = React.createContext<Editor | null>(null);
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { useEffect, useRef } from 'react';
21+
22+
import { EditorView } from '@codemirror/view';
23+
24+
import { BaseEditorProps } from './types';
25+
import { useEditor } from './utils';
26+
27+
interface MarkdownEditorProps extends BaseEditorProps {}
28+
29+
const MarkdownEditor: React.FC<MarkdownEditorProps> = ({
30+
value,
31+
onChange,
32+
onFocus,
33+
onBlur,
34+
placeholder,
35+
autoFocus,
36+
onEditorReady,
37+
}) => {
38+
const editorRef = useRef<HTMLDivElement>(null);
39+
const lastSyncedValueRef = useRef<string>(value);
40+
const isInitializedRef = useRef<boolean>(false);
41+
42+
const editor = useEditor({
43+
editorRef,
44+
onChange,
45+
onFocus,
46+
onBlur,
47+
placeholder,
48+
autoFocus,
49+
initialValue: value,
50+
});
51+
52+
useEffect(() => {
53+
if (!editor || isInitializedRef.current) {
54+
return;
55+
}
56+
57+
isInitializedRef.current = true;
58+
onEditorReady?.(editor);
59+
}, [editor, onEditorReady]);
60+
61+
useEffect(() => {
62+
if (!editor || value === lastSyncedValueRef.current) {
63+
return;
64+
}
65+
66+
const currentValue = editor.getValue();
67+
if (currentValue !== value) {
68+
editor.setValue(value || '');
69+
lastSyncedValueRef.current = value || '';
70+
}
71+
}, [editor, value]);
72+
73+
useEffect(() => {
74+
lastSyncedValueRef.current = value;
75+
isInitializedRef.current = false;
76+
77+
return () => {
78+
if (editor) {
79+
const view = editor as unknown as EditorView;
80+
if (view.destroy) {
81+
view.destroy();
82+
}
83+
}
84+
isInitializedRef.current = false;
85+
};
86+
}, []);
87+
88+
return (
89+
<div className="content-wrap">
90+
<div
91+
className="md-editor position-relative w-100 h-100"
92+
ref={editorRef}
93+
/>
94+
</div>
95+
);
96+
};
97+
98+
export default MarkdownEditor;

ui/src/components/Editor/ToolBars/blockquote.tsx

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ import { memo } from 'react';
2121
import { useTranslation } from 'react-i18next';
2222

2323
import ToolItem from '../toolItem';
24-
import { IEditorContext } from '../types';
24+
import { Editor } from '../types';
2525

26-
let context: IEditorContext;
2726
const BlockQuote = () => {
2827
const { t } = useTranslation('translation', { keyPrefix: 'editor' });
2928

@@ -33,21 +32,9 @@ const BlockQuote = () => {
3332
tip: `${t('blockquote.text')} (Ctrl+Q)`,
3433
};
3534

36-
const handleClick = (ctx) => {
37-
context = ctx;
38-
context.replaceLines((line) => {
39-
const FIND_BLOCKQUOTE_RX = /^>\s+?/g;
40-
41-
if (line === `> ${t('blockquote.text')}`) {
42-
line = '';
43-
} else if (line.match(FIND_BLOCKQUOTE_RX)) {
44-
line = line.replace(FIND_BLOCKQUOTE_RX, '');
45-
} else {
46-
line = `> ${line || t('blockquote.text')}`;
47-
}
48-
return line;
49-
}, 2);
50-
context.editor?.focus();
35+
const handleClick = (editor: Editor) => {
36+
editor.insertBlockquote(t('blockquote.text'));
37+
editor.focus();
5138
};
5239

5340
return <ToolItem {...item} onClick={handleClick} />;

ui/src/components/Editor/ToolBars/bold.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ import { memo } from 'react';
2121
import { useTranslation } from 'react-i18next';
2222

2323
import ToolItem from '../toolItem';
24-
import { IEditorContext } from '../types';
24+
import { Editor } from '../types';
2525

26-
let context: IEditorContext;
2726
const Bold = () => {
2827
const { t } = useTranslation('translation', { keyPrefix: 'editor' });
2928
const item = {
@@ -33,10 +32,9 @@ const Bold = () => {
3332
};
3433
const DEFAULTTEXT = t('bold.text');
3534

36-
const handleClick = (ctx) => {
37-
context = ctx;
38-
context.wrapText('**', '**', DEFAULTTEXT);
39-
context.editor?.focus();
35+
const handleClick = (editor: Editor) => {
36+
editor.insertBold(DEFAULTTEXT);
37+
editor.focus();
4038
};
4139

4240
return <ToolItem {...item} onClick={handleClick} />;

ui/src/components/Editor/ToolBars/chart.tsx

Lines changed: 0 additions & 181 deletions
This file was deleted.

0 commit comments

Comments
 (0)