Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

refactor(ui): Refactor image toolbar component

robinfc93dad3476b
diff --git a/ui/src/components/Editor/ToolBars/image.tsx b/ui/src/components/Editor/ToolBars/image.tsx
index 1bc429a0..d007408f 100644
--- a/ui/src/components/Editor/ToolBars/image.tsx
+++ b/ui/src/components/Editor/ToolBars/image.tsx
@@ -21,23 +21,27 @@ import { useEffect, useState, memo } from 'react';
import { Button, Form, Modal, Tab, Tabs } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import type { Editor } from 'codemirror';
+import { EditorView } from '@codemirror/view';
+import { EditorState, StateEffect } from '@codemirror/state';
import { Modal as AnswerModal } from '@/components';
import ToolItem from '../toolItem';
import { IEditorContext } from '../types';
import { uploadImage } from '@/services';
+import { ExtendEditor } from '../utils';
let context: IEditorContext;
const Image = ({ editorInstance }) => {
- const [editor, setEditor] = useState<Editor>(editorInstance);
+ const [editor, setEditor] = useState<EditorView & ExtendEditor>(
+ editorInstance,
+ );
const { t } = useTranslation('translation', { keyPrefix: 'editor' });
const loadingText = `![${t('image.uploading')}...]()`;
const item = {
label: 'image',
- keyMap: ['Ctrl-G'],
+ keyMap: ['Ctrl-g'],
tip: `${t('image.text')} (Ctrl+G)`,
};
const [currentTab, setCurrentTab] = useState('localImage');
@@ -94,16 +98,16 @@ const Image = ({ editorInstance }) => {
return Promise.all(promises);
};
- function dragenter(_, e) {
+ function dragenter(e) {
e.stopPropagation();
e.preventDefault();
}
- function dragover(_, e) {
+ function dragover(e) {
e.stopPropagation();
e.preventDefault();
}
- const drop = async (_, e) => {
+ const drop = async (e) => {
const fileList = e.dataTransfer.files;
const bool = verifyImageSize(fileList);
@@ -112,7 +116,9 @@ const Image = ({ editorInstance }) => {
return;
}
+ // const startPos = editor.getCursor(''); codemirror 6
const startPos = editor.getCursor();
+
const endPos = { ...startPos, ch: startPos.ch + loadingText.length };
editor.replaceSelection(loadingText);
@@ -136,26 +142,30 @@ const Image = ({ editorInstance }) => {
}
};
- const paste = async (_, event) => {
+ const paste = async (event) => {
const clipboard = event.clipboardData;
const bool = verifyImageSize(clipboard.files);
if (bool) {
event.preventDefault();
- editor.setOption('readOnly', true);
- const startPos = editor.getCursor('');
+ const startPos = editor.getCursor();
const endPos = { ...startPos, ch: startPos.ch + loadingText.length };
editor.replaceSelection(loadingText);
+ editor.dispatch({
+ effects: StateEffect.appendConfig.of([EditorState.readOnly.of(true)]),
+ });
const urls = await upload(clipboard.files);
const text = urls.map(({ name, url }) => {
return `![${name}](${url})`;
});
editor.replaceRange(text.join('\n'), startPos, endPos);
+ editor.dispatch({
+ effects: StateEffect.appendConfig.of([EditorState.readOnly.of(false)]),
+ });
- editor.setOption('readOnly', false);
return;
}