Skip to content

Commit

Permalink
feat
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfeng33 committed May 28, 2024
1 parent a1ab7d7 commit b80d8b3
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/core/src/client/components/Plate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface PlateProps<
insertData?: boolean;
length?: boolean;
nodeFactory?: boolean;
queryCachToState?: boolean;
react?: boolean;
selection?: boolean;
}
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/client/components/PlateEffects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ export function PlateEffects<
>({ children, ...props }: PlateEffectsProps<V, E>) {
usePlateEffects<V, E>(props);

return <>{children}</>;
return (
<div data-id={props.id} id="plate-editor">
{children}
</div>
);
}
1 change: 0 additions & 1 deletion packages/core/src/client/utils/createPlateEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export const createPlateEditor = <
components,
overrideByKey,
});

const e = withPlate<V>(editor, {
plugins,
...withPlateOptions,
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/client/utils/setPlatePlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
KEY_LENGTH,
KEY_NODE_FACTORY,
KEY_PREV_SELECTION,
KEY_QUERY_CACHE_TO_STATE,
createDeserializeAstPlugin,
createDeserializeHtmlPlugin,
createEditorProtocolPlugin,
Expand All @@ -26,6 +27,7 @@ import {
createLengthPlugin,
createNodeFactoryPlugin,
createPrevSelectionPlugin,
createQueryCachToStatePlugin,
} from '../../shared/plugins';
import { flattenDeepPlugins } from '../../shared/utils/flattenDeepPlugins';
import { overridePluginsByKey } from '../../shared/utils/overridePluginsByKey';
Expand Down Expand Up @@ -114,6 +116,12 @@ export const setPlatePlugins = <
createEditorProtocolPlugin()
);
}
if (typeof dcp !== 'object' || !dcp?.queryCachToState) {
plugins.push(
(editor?.pluginsByKey?.[KEY_QUERY_CACHE_TO_STATE] as any) ??
createQueryCachToStatePlugin()
);
}
}

plugins = [...plugins, ..._plugins] as any;
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/server/setPlatePlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
KEY_LENGTH,
KEY_NODE_FACTORY,
KEY_PREV_SELECTION,
KEY_QUERY_CACHE_TO_STATE,
createDeserializeAstPlugin,
createDeserializeHtmlPlugin,
createEditorProtocolPlugin,
Expand All @@ -25,6 +26,7 @@ import {
createLengthPlugin,
createNodeFactoryPlugin,
createPrevSelectionPlugin,
createQueryCachToStatePlugin,
} from '../shared/plugins';
import { flattenDeepPlugins } from '../shared/utils/flattenDeepPlugins';
import { overridePluginsByKey } from '../shared/utils/overridePluginsByKey';
Expand Down Expand Up @@ -112,6 +114,12 @@ export const setPlatePlugins = <
createEditorProtocolPlugin()
);
}
if (typeof dcp !== 'object' || !dcp?.queryCachToState) {
plugins.push(
(editor?.pluginsByKey?.[KEY_QUERY_CACHE_TO_STATE] as any) ??
createQueryCachToStatePlugin()
);
}
}

plugins = [...plugins, ..._plugins] as any;
Expand Down
64 changes: 64 additions & 0 deletions packages/core/src/shared/plugins/createQueryCacheToState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {
type AncestorOf,
type ENode,
type TEditor,
type TNodeEntry,
type Value,
getAboveNode,
getNodeEntries,
getNodeEntry,
} from '@udecode/slate';
import { getBlockAbove } from '@udecode/slate-utils';

import type { PlateEditor } from '../types/PlateEditor';

import { createPluginFactory } from '../utils/createPluginFactory';

export type QueryCachToSateEditor<V extends Value> = {
state: {
aboveEntries?: Generator<TNodeEntry<ENode<V>>, void, undefined>;
aboveNode?: TNodeEntry<AncestorOf<TEditor<V>>>;
ancestorNode?: TNodeEntry<ENode<V>>;
blockAbove?: TNodeEntry<AncestorOf<TEditor<V>>>;
};
};

export const withQueryCachToState = <
V extends Value = Value,
E extends PlateEditor<V> = PlateEditor<V>,
>(
editor: E
) => {
if (typeof editor.state !== 'object') editor.state = {};

const { apply } = editor;

editor.apply = (operation) => {
apply(operation);

const aboveNode = getAboveNode(editor);
const aboveEntries = getNodeEntries(editor);
const blockAbove = getBlockAbove(editor);

editor.state.aboveNode = aboveNode;
editor.state.blockAbove = blockAbove;
editor.state.aboveEntries = aboveEntries;

const { selection } = editor;

if (selection?.focus?.path) {
editor.state.ancestorNode = getNodeEntry(editor, [
selection.focus.path[0],
]);
}
};

return editor;
};

export const KEY_QUERY_CACHE_TO_STATE = 'queryCacheToSate';

export const createQueryCachToStatePlugin = createPluginFactory({
key: KEY_QUERY_CACHE_TO_STATE,
withOverrides: withQueryCachToState,
});
1 change: 1 addition & 0 deletions packages/core/src/shared/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export * from './createInsertDataPlugin';
export * from './createLengthPlugin';
export * from './createNodeFactoryPlugin';
export * from './createPrevSelectionPlugin';
export * from './createQueryCacheToState';
export * from './event-editor/index';
export * from './html-deserializer/index';
14 changes: 10 additions & 4 deletions packages/core/src/shared/transforms/toggleNodeType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export const toggleNodeType = <V extends Value>(
const { activeType, inactiveType = getPluginType(editor, ELEMENT_DEFAULT) } =
options;

if (!activeType || !editor.selection) return;
const at = editorNodesOptions?.at ?? editor.selection;

if (!activeType || !at) return;

const isActive = someNode(editor, {
...editorNodesOptions,
Expand All @@ -47,7 +49,11 @@ export const toggleNodeType = <V extends Value>(

if (isActive && activeType === inactiveType) return;

setElements(editor, {
type: isActive ? inactiveType : activeType,
});
setElements(
editor,
{
type: isActive ? inactiveType : activeType,
},
{ at: at as any }
);
};
2 changes: 2 additions & 0 deletions packages/core/src/shared/types/PlateEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
import type { TReactEditor } from '@udecode/slate-react';
import type { Path } from 'slate';

import type { QueryCachToSateEditor } from '../plugins';
import type { PlateEditorMethods } from './PlateEditorMethods';
import type { WithPlatePlugin } from './plugin/PlatePlugin';
import type { PluginKey } from './plugin/PlatePluginKey';
Expand Down Expand Up @@ -45,6 +46,7 @@ export type PlateEditor<V extends Value = Value> = {

prevSelection: TRange | null;
} & PlateEditorMethods<V> &
QueryCachToSateEditor<V> &
TEditor<V> &
THistoryEditor<V> &
TReactEditor<V>;
2 changes: 2 additions & 0 deletions packages/core/src/shared/types/plugin/KeyboardHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ export type KeyboardHandler<

export type KeyboardHandlerReturnType =
DOMHandlerReturnType<React.KeyboardEvent>;

export type MouseHandlerReturnType = DOMHandlerReturnType<React.MouseEvent>;

0 comments on commit b80d8b3

Please sign in to comment.