Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Core Plugin QueryCacheToSate #3221

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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">
Copy link
Member

Choose a reason for hiding this comment

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

is it needed?

Copy link
Collaborator Author

@felixfeng33 felixfeng33 May 28, 2024

Choose a reason for hiding this comment

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

toc Plugin need the container dom node to scroll to heading.I haven't thought of a good method to obtain container nodes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can get Slate's root contenteditable by passing the editor to toDOMNode.

{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: {
Copy link
Member

Choose a reason for hiding this comment

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

cache may be better. What do you think @12joan ?

aboveEntries?: Generator<TNodeEntry<ENode<V>>, void, undefined>;
Copy link
Member

@zbeyens zbeyens May 28, 2024

Choose a reason for hiding this comment

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

let's reuse the same naming convention than Slate queries. For example Transforms.above -> above. For Plate exclusive, just try to follow the same pattern

aboveNode?: TNodeEntry<AncestorOf<TEditor<V>>>;
ancestorNode?: TNodeEntry<ENode<V>>;
blockAbove?: TNodeEntry<AncestorOf<TEditor<V>>>;
};
};

export const withQueryCachToState = <
Copy link
Member

Choose a reason for hiding this comment

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

Let's name the plugin cache instead of queryCacheToState

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) => {
Copy link
Member

Choose a reason for hiding this comment

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

I think editor.onChange is a better fit

Copy link
Collaborator

Choose a reason for hiding this comment

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

I've left some comments pertaining to this on Discord. https://discord.com/channels/1026227597115396188/1237021058067075072

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,
Copy link
Member

Choose a reason for hiding this comment

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

let's add a boolean option for each query: don't run if false

Copy link
Collaborator

Choose a reason for hiding this comment

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

I've left some comments pertaining to this on Discord. https://discord.com/channels/1026227597115396188/1237021058067075072

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>;
Loading