Skip to content

Commit

Permalink
feat: on paint event
Browse files Browse the repository at this point in the history
  • Loading branch information
WindRunnerMax committed Aug 16, 2024
1 parent 325785d commit 6c79a4a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
4 changes: 3 additions & 1 deletion packages/core/src/event/bus/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export type Handler<T extends EventType> = {
priority: number;
listener: Listener<T>;
};
export type WithStop<T> = T & { stop: () => void; prevent: () => void; _raw: T; _key: string };
export type Listener<T extends EventType> = (value: WithStop<EventMap[T]>) => void;
export type Listeners = { [T in EventType]?: Handler<T>[] };
export type WithStop<T> = T extends Object.Any
? T & { stop: () => void; prevent: () => void; _raw: T; _key: string }
: T;
4 changes: 2 additions & 2 deletions packages/core/src/event/bus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class EventBus {
/**
* @param key
* @param listener
* @param priority
* @param priority 默认 100
*/
public on<T extends EventType>(key: T, listener: Listener<T>, priority = DEFAULT_PRIORITY) {
this.addEventListener(key, listener, priority, false);
Expand All @@ -17,7 +17,7 @@ export class EventBus {
/**
* @param key
* @param listener
* @param priority
* @param priority 默认 100
*/
public once<T extends EventType>(key: T, listener: Listener<T>, priority = DEFAULT_PRIORITY) {
this.addEventListener(key, listener, priority, true);
Expand Down
35 changes: 19 additions & 16 deletions packages/core/src/preset/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { INIT_NODE } from "../editor/constant";
import { EDITOR_EVENT } from "../event/bus/action";
import type { ApplyPlugins } from "../plugin/types/apply";
import { EDITOR_STATE } from "../state/types";
import { WithContext } from "./with-context";

type EditableProps = {
readonly?: boolean;
Expand Down Expand Up @@ -86,22 +87,24 @@ export class Editable extends React.PureComponent<EditableProps, EditableState>
value={this.props.init || this.props.editor.options.init || INIT_NODE}
onChange={this.props.onChange}
>
<EditableFC
decorate={this.state.renderModule.decorate}
renderElement={this.state.renderModule.renderBlock}
renderLeaf={this.state.renderModule.renderLeaf}
readOnly={this.props.readonly}
placeholder={this.props.placeholder}
onKeyDown={this.onKeyDown}
onKeyPress={this.onKeyPress}
onMouseDown={this.onMouseDown}
onMouseUp={this.onMouseUp}
onCopy={this.onCopy}
onCut={this.onCut}
onPaste={this.onPaste}
onFocus={this.onFocus}
onBlur={this.onBlur}
/>
<WithContext editor={this.props.editor}>
<EditableFC
decorate={this.state.renderModule.decorate}
renderElement={this.state.renderModule.renderBlock}
renderLeaf={this.state.renderModule.renderLeaf}
readOnly={this.props.readonly}
placeholder={this.props.placeholder}
onKeyDown={this.onKeyDown}
onKeyPress={this.onKeyPress}
onMouseDown={this.onMouseDown}
onMouseUp={this.onMouseUp}
onCopy={this.onCopy}
onCut={this.onCut}
onPaste={this.onPaste}
onFocus={this.onFocus}
onBlur={this.onBlur}
/>
</WithContext>
</EditorProvider>
);
}
Expand Down
35 changes: 35 additions & 0 deletions packages/core/src/preset/with-context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useSlate } from "doc-editor-delta";
import type { FC } from "react";
import { useEffect, useRef } from "react";

import type { EditorKit } from "../editor";
import { EDITOR_EVENT } from "../event/bus/action";

export const WithContext: FC<{ editor: EditorKit }> = props => {
const { editor, children } = props;
const isNeedPaint = useRef(true);
// 保证每次触发 Apply 时都会重新渲染
// https://github.com/ianstormtaylor/slate/blob/25be3b/packages/slate-react/src/components/slate.tsx#L29
useSlate();

useEffect(() => {
const onContentChange = () => {
isNeedPaint.current = true;
};
editor.event.on(EDITOR_EVENT.CONTENT_CHANGE, onContentChange, 1);
return () => {
editor.event.off(EDITOR_EVENT.CONTENT_CHANGE, onContentChange);
};
}, [editor]);

useEffect(() => {
if (isNeedPaint.current) {
Promise.resolve().then(() => {
editor.event.trigger(EDITOR_EVENT.PAINT, {});
});
}
isNeedPaint.current = false;
});

return children as JSX.Element;
};

0 comments on commit 6c79a4a

Please sign in to comment.