Skip to content

Commit

Permalink
feat: 添加 Markdown 编辑器事件常量,更新编辑器引用和状态管理
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuai2144 committed Feb 1, 2025
1 parent 67bd4bf commit 7c92740
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 100 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"diacritics": "^1.3.0",
"direction": "^1.0.4",
"dumi-theme-antd-style": "^0.31.1",
"event-emitter": "^0.3.5",
"framer-motion": "^11.18.2",
"glob": "^11.0.1",
"is-hotkey": "^0.2.0",
Expand Down Expand Up @@ -97,7 +96,6 @@
"@testing-library/react": "^16.2.0",
"@types/classnames": "^2.3.4",
"@types/diacritics": "^1.3.3",
"@types/event-emitter": "^0.3.5",
"@types/is-hotkey": "^0.1.10",
"@types/jsdom": "^21.1.7",
"@types/mdast": "^4.0.4",
Expand Down
85 changes: 0 additions & 85 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions src/MarkdownEditor/demos/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { MarkdownEditor } from '@ant-design/md-editor';
import {
MARKDOWN_EDITOR_EVENTS,
MarkdownEditor,
MarkdownEditorInstance,
} from '@ant-design/md-editor';
import { Tooltip } from 'antd';
import React, { useEffect, useState } from 'react';
const defaultValue = `<!-- {"MarkdownType": "report", "id": "8", "section_ids": " [15, 16, 17] "} -->
Expand Down Expand Up @@ -137,7 +141,7 @@ const defaultValue = `<!-- {"MarkdownType": "report", "id": "8", "section_ids":
`;
export default () => {
const editorRef = React.useRef<any>();
const editorRef = React.useRef<MarkdownEditorInstance>();
const [list, setList] = useState([
{
selection: {
Expand Down Expand Up @@ -194,6 +198,12 @@ export default () => {
useEffect(() => {
// @ts-ignore
window.editorRef = editorRef;
editorRef.current?.markdownContainerRef?.current?.addEventListener(
MARKDOWN_EDITOR_EVENTS.SELECTIONCHANGE,
(e) => {
console.log('selectionchange', e);
},
);
}, []);
return (
<MarkdownEditor
Expand Down
23 changes: 20 additions & 3 deletions src/MarkdownEditor/editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import { message } from 'antd';
import classNames from 'classnames';
import { observer } from 'mobx-react';
import React, { useEffect, useMemo, useRef } from 'react';
import { BaseRange, Editor, Element, Node, Range, Transforms } from 'slate';
import {
BaseRange,
BaseSelection,
Editor,
Element,
Node,
Range,
Transforms,
} from 'slate';
import {
CommentDataType,
Elements,
Expand All @@ -29,7 +37,7 @@ import {
} from './slate-react';
import { useEditorStore } from './store';
import { useStyle } from './style';
import { isMarkdown } from './utils';
import { isMarkdown, MARKDOWN_EDITOR_EVENTS } from './utils';
import { getMediaType } from './utils/dom';
import {
EditorUtils,
Expand Down Expand Up @@ -57,7 +65,8 @@ export const MEditor = observer(
reportMode?: MarkdownEditorProps['reportMode'];
titlePlaceholderContent?: string;
} & MarkdownEditorProps) => {
const { store, markdownEditorRef, readonly } = useEditorStore();
const { store, markdownEditorRef, markdownContainerRef, readonly } =
useEditorStore();
const changedMark = useRef(false);
const value = useRef<any[]>([EditorUtils.p]);
const nodeRef = useRef<MarkdownEditorInstance>();
Expand Down Expand Up @@ -616,6 +625,14 @@ export const MEditor = observer(
}
onSelect={() => {
if (store.focus) {
// 选中时,更新选区,并且触发选区变化事件
const event = new CustomEvent<BaseSelection>(
MARKDOWN_EDITOR_EVENTS.SELECTIONCHANGE,
{
detail: markdownEditorRef.current.selection,
},
);
markdownContainerRef?.current?.dispatchEvent(event);
markdownEditorRef.current.selection =
getSelectionFromDomSelection(
markdownEditorRef.current,
Expand Down
1 change: 1 addition & 0 deletions src/MarkdownEditor/editor/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const EditorStoreContext = createContext<{
markdownEditorRef: React.MutableRefObject<
BaseEditor & ReactEditor & HistoryEditor
>;
markdownContainerRef: React.MutableRefObject<HTMLDivElement | null>;
} | null>(null);

export const useEditorStore = () => {
Expand Down
17 changes: 17 additions & 0 deletions src/MarkdownEditor/editor/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,23 @@ export const debugLog = (path: string, ...msg: any[]) => {
}
};

/**
* Markdown 编辑器事件常量对象
* @constant
* @description 包含编辑器的主要事件名称
* @property {string} SELECTIONCHANGE - 当编辑器选区发生变化时触发的事件名
* @property {string} FOCUS - 当编辑器获得焦点时触发的事件名
* @property {string} BLUR - 当编辑器失去焦点时触发的事件名
*/
export const MARKDOWN_EDITOR_EVENTS = {
// 编辑器选区发生变化时触发的事件名
SELECTIONCHANGE: 'md-editor-selectionchange',
// 编辑器获得焦点时触发的事件名
FOCUS: 'md-editor-focus',
// 编辑器失去焦点时触发的事件名
BLUR: 'md-editor-blur',
};

export * from './editorUtils';
export * from './keyboard';
export * from './media';
Expand Down
Loading

1 comment on commit 7c92740

@vercel
Copy link

@vercel vercel bot commented on 7c92740 Feb 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.