Skip to content

Commit

Permalink
fix: 更新.gitignore,移除不必要的历史记录,优化编辑器状态管理和选区处理逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuai2144 committed Jan 30, 2025
1 parent a80be6b commit 3a2790f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 34 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ node_modules
.dumi/tmp-test
.dumi/tmp-production
.DS_Store
/docs-dist
/docs-dist
tsconfig.tsbuildinfo
4 changes: 0 additions & 4 deletions src/MarkdownEditor/editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ export const MEditor = observer(
editorProps.initSchemaValue?.length
? editorProps.initSchemaValue
: undefined,
instance.history || true,
);
clearAllCodeCache(markdownEditorRef.current);
} catch (e) {
Expand Down Expand Up @@ -115,9 +114,6 @@ export const MEditor = observer(
}
value.current = v;
onChange(v, markdownEditorRef.current.operations);
if (instance) {
instance.history = markdownEditorRef.current.history;
}

if (
!markdownEditorRef.current.operations?.every(
Expand Down
54 changes: 31 additions & 23 deletions src/MarkdownEditor/editor/plugins/useOnchange.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { useDebounceFn } from '@ant-design/pro-components';
import { runInAction } from 'mobx';
import React, { useRef } from 'react';
import { Subject } from 'rxjs';
Expand Down Expand Up @@ -36,35 +37,47 @@ export function useOnchange(
onChange?: (value: string, schema: Elements[]) => void,
) {
const rangeContent = useRef('');
const onChangeDebounce = useDebounceFn(async () => {
if (!onChange) return;
onChange?.(schemaToMarkdown(editor.children), editor.children);
}, 300);

const setSelDebounce = useDebounceFn(async () => {
runInAction(() => {
if (typeof window === 'undefined') return;
if (typeof window.matchMedia === 'undefined') return;
store.sel = editor.selection;
});
}, 300);
const selChange = useDebounceFn(
async (changeSel: { sel: BaseSelection; node: NodeEntry<any> }) => {
selChange$.next(changeSel);
},
160,
);
return React.useMemo(() => {
return (_value: any, _operations: BaseOperation[]) => {
if (onChange) {
onChange(schemaToMarkdown(_value), _value);
if (onChangeDebounce) {
onChangeDebounce.cancel();
onChangeDebounce?.run(schemaToMarkdown(_value), _value);
}
const sel = editor.selection;
const [node] = Editor.nodes<Element>(editor, {
match: (n) => Element.isElement(n),
mode: 'lowest',
});
setTimeout(() => {
selChange$.next({
sel,
node,
});
});

runInAction(() => {
if (typeof window === 'undefined') return;
if (typeof window.matchMedia === 'undefined') return;
store.sel = sel;
// 选区变化
selChange.cancel();
selChange.run({
sel,
node,
});
setSelDebounce.cancel();
setSelDebounce.run();
// ------选区变化end----------

if (!node) return;
setTimeout(() => {
selChange$.next({
sel,
node,
});
});

if (
sel &&
Expand All @@ -89,11 +102,6 @@ export function useOnchange(
store.setState((state) => (state.domRect = null));
}

if (node && node[0].type === 'media') {
store.mediaNode$.next(node);
} else {
store.mediaNode$.next(null);
}
if (node && node[0].type === 'table-cell') {
store.setState((state) => {
state.tableCellNode = node;
Expand Down
11 changes: 7 additions & 4 deletions src/MarkdownEditor/editor/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ReactEditor } from './slate-react';
import { parse } from 'querystring';
import { HistoryEditor } from 'slate-history';
import { CommentDataType, MarkdownEditorProps } from '..';
import { Elements, ListNode, MediaNode, TableCellNode } from '../el';
import { Elements, ListNode, TableCellNode } from '../el';
import { parserMdToSchema } from './parser/parserMdToSchema';
import { KeyboardTask, Methods, schemaToMarkdown } from './utils';
import { getOffsetLeft, getOffsetTop } from './utils/dom';
Expand Down Expand Up @@ -79,7 +79,6 @@ export class EditorStore {
insertCompletionText$ = new Subject<string>();
highlightCache = new Map<object, Range[]>();
refreshFloatBar = false;
mediaNode$ = new Subject<NodeEntry<MediaNode> | null>();
openInsertLink$ = new Subject<Selection>();
openLinkPanel = false;
tableCellNode: null | NodeEntry<TableCellNode> = null;
Expand Down Expand Up @@ -134,10 +133,14 @@ export class EditorStore {
this._editor = _editor;
makeAutoObservable(this, {
_editor: false,
editor: false,
sel: false,
preSelection: false,
container: false,
domRect: false,
domRange: false,
tableCellNode: false,
editor: false,
inputComposition: false,
container: false,
highlightCache: false,
draggedElement: false,
manual: false,
Expand Down
2 changes: 0 additions & 2 deletions src/MarkdownEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ export type CommentDataType = {
export type IEditor = {
children?: IEditor[];
expand?: boolean;
history?: any;
};

/**
Expand All @@ -101,7 +100,6 @@ export type IEditor = {
export interface MarkdownEditorInstance {
range?: Range;
store: EditorStore;
history?: any;
}

/**
Expand Down

1 comment on commit 3a2790f

@vercel
Copy link

@vercel vercel bot commented on 3a2790f Jan 30, 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.