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

plugin.withComponent disappears after calling overrideEditor #3958

Closed
yf-yang opened this issue Jan 11, 2025 · 0 comments · Fixed by #3964
Closed

plugin.withComponent disappears after calling overrideEditor #3958

yf-yang opened this issue Jan 11, 2025 · 0 comments · Fixed by #3964
Labels
bug Something isn't working

Comments

@yf-yang
Copy link
Collaborator

yf-yang commented Jan 11, 2025

Description

Root cause:

withComponent is defined here:

plugin.withComponent = (component) => {
return plugin.extend({
node: { component },
render: { node: component },
}) as any;
};

overrideEditor is defined here:

plugin.overrideEditor = (extension) => {
const newPlugin = { ...plugin };
newPlugin.__apiExtensions = [
...(newPlugin.__apiExtensions as any),
{
extension,
isOverride: true,
isPluginSpecific: false,
isTransform: true,
},
];
return createSlatePlugin(newPlugin) as any;
};

createPlatePlugin is defined here:

export const createPlatePlugin = <
K extends string = any,
O = {},
A = {},
T = {},
>(
config:
| ((editor: PlateEditor) => PlatePluginConfig<K, O, A, T>)
| PlatePluginConfig<K, O, A, T> = {}
): PlatePlugin<PluginConfig<K, O, A, T>> => {
const plugin = createSlatePlugin(config as any);
return toPlatePlugin(plugin as any) as any;
};

Note that toPlatePlugin called from createPlatePlugin makes a copy of the plugin.

Since overrideEditor is an arrow function, the plugin is actually retrieved from the outer scope, which is the slate plugin that not yet returned from createSlatePlugin, not the one copied and created by createPlatePlugin. Therefore, the plugin returned after overrideEditor call will not contain withComponent

Reproduction URL

No response

Reproduction steps

console.log(createTPlatePlugin({ key: 'mykey' }).overrideEditor(() => ({})).withComponent === undefined)
// true

Plate version

42

Slate React version

not related

Screenshots

No response

Logs

No response

Browsers

No response

@yf-yang yf-yang added the bug Something isn't working label Jan 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant