Skip to content

plugin.withComponent disappears after calling overrideEditor #3958

@yf-yang

Description

@yf-yang

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions