Skip to content

Commit

Permalink
fix: fix inconsistent pb element save-behavior in editor
Browse files Browse the repository at this point in the history
  • Loading branch information
neatbyte-vnobis committed Sep 20, 2023
1 parent a239a47 commit b28739a
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from "react";
import styled from "@emotion/styled";

const EmptyElementWrapper = styled.div`
display: flex;
align-items: center;
justify-content: center;
height: 100%;
min-height: 100px;
${props => props.theme.styles.typography.paragraphs.stylesById("paragraph1")};
color: ${props => props.theme.styles.colors["color4"]};
`;

type EmptyElementProps = {
message: string;
};

export const EmptyElement = ({ message }: EmptyElementProps) => {
return <EmptyElementWrapper>{message}</EmptyElementWrapper>;
};
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./DefaultLinkComponent";
export * from "./EmptyElement";
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React, { useEffect, useCallback } from "react";
import { Element } from "~/types";
import styled from "@emotion/styled";

import { EmptyElement } from "~/renderers/components";
import { Element } from "~/types";

export interface OEmbedPropsInitCallableParams {
props: OEmbedProps;
node: HTMLElement;
Expand Down Expand Up @@ -74,5 +76,9 @@ export const OEmbed: React.FC<OEmbedProps> = props => {
appendSDK(props).then(() => initEmbed(props));
});

return url ? renderer() : null;
if (!url) {
return <EmptyElement message="Please provide a link for this embed element." />;
}

return renderer();
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import { createRenderer } from "~/createRenderer";
import { useRenderer } from "~/hooks/useRenderer";
import { EmptyElement } from "~/renderers/components";

export interface IFrameElementData {
iframe: {
Expand All @@ -13,6 +14,11 @@ export const createIFrame = () =>
const { getElement } = useRenderer();

const element = getElement<IFrameElementData>();
const url = element.data.iframe.url;

return <iframe src={element.data.iframe.url} width="100%" height="100%" />;
if (!url) {
return <EmptyElement message="Please provide a link for this iframe element." />;
}

return <iframe src={url} width="100%" height="100%" />;
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useEffect } from "react";
import { Element, Renderer } from "~/types";
import { createRenderer } from "~/createRenderer";
import { useRenderer } from "~/hooks/useRenderer";
import { EmptyElement } from "~/renderers/components";

declare global {
interface Window {
Expand Down Expand Up @@ -52,7 +53,7 @@ const Pinterest: PinterestComponent = ({ element }) => {
return <a data-pin-do="embedPin" data-pin-width={data.size || "small"} href={data.url} />;
}

return null;
return <EmptyElement message="Please provide a link for this embed element." />;
};

export const createPinterest = () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/app-page-builder-elements/src/renderers/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styled from "@emotion/styled";
import { createRenderer, CreateRendererOptions } from "~/createRenderer";
import { useRenderer } from "~/hooks/useRenderer";
import { LinkComponent as LinkComponentType } from "~/types";
import { DefaultLinkComponent } from "~/renderers/components";
import { DefaultLinkComponent, EmptyElement } from "~/renderers/components";

export interface ImageElementData {
image?: {
Expand Down Expand Up @@ -80,7 +80,7 @@ export const ImageRendererComponent: React.FC<ImageRendererComponentProps> = ({

content = <PbImg alt={title} title={title} src={src} srcSet={srcSet} onClick={onClick} />;
} else {
content = renderEmpty || null;
content = renderEmpty || <EmptyElement message="Please select an image." />;
}

const linkProps = link || element.data?.link;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React, { useReducer } from "react";
import { ImagesListComponent } from "../types";
import { EmptyElement } from "~/renderers/components";

/**
* Package react-columned does not have types.
*/
// @ts-ignore
import Columned from "react-columned";
import Lightbox from "react-images";
import styled from "@emotion/styled";

const COLUMNS = { 320: 1, 480: 2, 800: 3, 1366: 4 };

Expand Down Expand Up @@ -57,17 +57,6 @@ const useLightbox = () => {
};
};

const NoImagesToDisplay = styled.div`
display: flex;
justify-content: center;
align-items: center;
height: 100px;
width: 100%;
border: 1px dashed var(--mdc-theme-secondary);
color: var(--mdc-theme-text-secondary-on-background);
pointer-events: none;
`;

export const createDefaultImagesListComponent = (): ImagesListComponent =>
function DefaultImagesListComponent(props) {
const { opened, open, close, next, prev, currentIndex } = useLightbox();
Expand Down Expand Up @@ -99,5 +88,5 @@ export const createDefaultImagesListComponent = (): ImagesListComponent =>
);
}

return <NoImagesToDisplay>No images to display.</NoImagesToDisplay>;
return <EmptyElement message="No images to display." />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ const ActionSettingsComponent: React.FC<ActionSettingsPropsType> = ({
}) => {
const updateElement = useUpdateElement();

const { actionType } = element.data?.action || { actionType: "link" };
const { actionType } = element.data?.action || {};

const updateSettings: FormOnSubmit = data => {
// Skip update if nothing is changed.
if (data.actionType === actionType) {
return;
}

const attrKey = `data.action`;
const newElement: PbEditorElement = merge(element, attrKey, data);
updateElement(newElement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ const buttonElementPluginsFactory = (args: PbEditorElementPluginArgs = {}) => {
"center",
DisplayMode.DESKTOP
) as unknown as "flex-start" | "center" | "flex-end"
}
},
action: { actionType: "link", href: "" }
},
...options
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { UpdateDocumentActionEvent } from "~/editor/recoil/actions/updateDocument";
import { CreateElementEventActionArgsType } from "./types";
import { EventActionCallable } from "~/types";

export const createElementAction: EventActionCallable<CreateElementEventActionArgsType> = () => {
return {
actions: []
actions: [new UpdateDocumentActionEvent()]
};
};
10 changes: 5 additions & 5 deletions packages/app-page-builder/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ export interface PbElementDataTypeSource {

export type PbElementDataType = {
action?: {
href: string;
newTab: boolean;
clickHandler: string;
href?: string;
newTab?: boolean;
clickHandler?: string;
actionType: string;
variables: PbButtonElementClickHandlerVariable[];
scrollToElement: string;
variables?: PbButtonElementClickHandlerVariable[];
scrollToElement?: string;
[key: string]: any;
};
settings?: PbElementDataSettingsType;
Expand Down

0 comments on commit b28739a

Please sign in to comment.