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

Page Builder - New PB Element Plugins #4342

Merged
merged 17 commits into from
Oct 18, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { PbEditorPageElementAdvancedSettingsPlugin as BasePbEditorPageElementAdvancedSettingsPlugin } from "~/types";
import { useRegisterLegacyPlugin } from "@webiny/app/hooks/useRegisterLegacyPlugin";

export interface PbEditorPageElementAdvancedSettingsPluginParams
extends Pick<BasePbEditorPageElementAdvancedSettingsPlugin, "elementType" | "onSave"> {
element: JSX.Element;
}

export const PbEditorPageElementAdvancedSettingsPlugin = (
props: PbEditorPageElementAdvancedSettingsPluginParams
) => {
useRegisterLegacyPlugin<BasePbEditorPageElementAdvancedSettingsPlugin>({
...props,
render: () => props.element,
type: "pb-editor-page-element-advanced-settings"
});

return null;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { PbEditorPageElementPlugin as BasePbEditorPageElementPlugin } from "~/types";
import { legacyPluginToReactComponent } from "@webiny/app/utils";

export const PbEditorPageElementPlugin = legacyPluginToReactComponent<
Pick<
BasePbEditorPageElementPlugin,
| "elementType"
| "toolbar"
| "help"
| "target"
| "settings"
| "create"
| "render"
| "canDelete"
| "canReceiveChildren"
| "onReceived"
| "onChildDeleted"
| "onCreate"
| "renderElementPreview"
>
>({
pluginType: "pb-editor-page-element",
componentDisplayName: "PbEditorPageElementPlugin"
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { PbRenderElementPlugin as BasePbRenderElementPlugin } from "~/types";
import { legacyPluginToReactComponent } from "@webiny/app/utils";

export const PbRenderElementPlugin = legacyPluginToReactComponent<
Pick<BasePbRenderElementPlugin, "elementType" | "render">
>({
pluginType: "pb-render-page-element",
componentDisplayName: "PbRenderElementPlugin"
});
3 changes: 3 additions & 0 deletions packages/app-page-builder/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from "./PbPageLayoutPlugin";
export * from "./PbEditorPageElementAdvancedSettingsPlugin";
export * from "./PbRenderElementPlugin";
export * from "./PbEditorPageElementPlugin";
1 change: 1 addition & 0 deletions packages/app-website/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export * from "./Page/WebsiteScripts";
export * from "./Page/ErrorPage";
export * from "./Menu";
export * from "./LinkPreload";
export * from "./plugins";

// Exporting chosen utils from `@webiny/app` package.
export * from "@webiny/app/utils/getApiUrl";
Expand Down
9 changes: 9 additions & 0 deletions packages/app-website/src/plugins/PbRenderElementPlugin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { PbRenderElementPlugin as BasePbRenderElementPlugin } from "@webiny/app-page-builder/types";
import { legacyPluginToReactComponent } from "@webiny/app/utils";

export const PbRenderElementPlugin = legacyPluginToReactComponent<
Pick<BasePbRenderElementPlugin, "elementType" | "render">
>({
pluginType: "pb-render-page-element",
componentDisplayName: "PbRenderElementPlugin"
});
1 change: 1 addition & 0 deletions packages/app-website/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./PbRenderElementPlugin";
11 changes: 11 additions & 0 deletions packages/app/src/hooks/useRegisterLegacyPlugin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useRef } from "react";
import { plugins } from "@webiny/plugins";
import { type Plugin } from "@webiny/plugins/types";

export function useRegisterLegacyPlugin<TPlugin extends Plugin>(plugin: TPlugin) {
const pluginRegistered = useRef<boolean>(false);
if (!pluginRegistered.current) {
pluginRegistered.current = true;
plugins.register(plugin);
}
}
1 change: 1 addition & 0 deletions packages/app/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from "./getPrerenderId";
export * from "./getTenantId";
export * from "./isLocalhost";
export * from "./isPrerendering";
export * from "./legacyPluginToReactComponent";
20 changes: 20 additions & 0 deletions packages/app/src/utils/legacyPluginToReactComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from "react";
import { useRegisterLegacyPlugin } from "~/hooks/useRegisterLegacyPlugin";

export interface LegacyPluginToReactComponentParams {
pluginType: string;
componentDisplayName: string;
}

export const legacyPluginToReactComponent = function <TProps extends Record<string, any>>(
params: LegacyPluginToReactComponentParams
) {
const Component: React.ComponentType<TProps> = props => {
useRegisterLegacyPlugin({ ...props, type: params.pluginType });
return null;
};

Component.displayName = params.componentDisplayName;

return Component;
};
Loading