Skip to content

Commit

Permalink
refactor: editor action (#35)
Browse files Browse the repository at this point in the history
* chore: remove funnelPageId

* chore: remove LOAD_DATA

* refactor: editor action

* feat: UPDATE_ELEMENT_STYLE

* fix: EditorElement type safe

* style: sort attribute

* refactor: rename InferEditorElement

* fix: typo

* fix: typo
  • Loading branch information
bepyan authored Aug 11, 2024
1 parent 7d309e7 commit 66c4e2c
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 366 deletions.
526 changes: 217 additions & 309 deletions src/components/editor/action.ts

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions src/components/editor/constant.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { EditorHistory, EditorState } from "~/components/editor/type";
import type {
EditorElement,
EditorHistory,
EditorState,
} from "~/components/editor/type";

export const defaultStyles: React.CSSProperties = {
backgroundPosition: "center",
Expand All @@ -19,8 +23,8 @@ export const emptyElement = {
content: [],
name: "",
styles: {},
type: null,
};
type: "empty",
} satisfies EditorElement;

export const initialEditorState: EditorState = {
elements: [
Expand All @@ -36,7 +40,6 @@ export const initialEditorState: EditorState = {
currentTabValue: editorTabValue.ELEMENTS,
device: "Mobile",
isPreviewMode: false,
funnelPageId: "",
};

export const initialEditorHistory: EditorHistory = {
Expand Down
32 changes: 16 additions & 16 deletions src/components/editor/elements/container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ export default function Container({ element }: Props) {
payload: {
containerId: id,
elementDetails: {
content: [],
type: "container",
id: nanoid(),
name: "Container",
styles: { ...defaultStyles },
type: "container",
content: [],
},
},
});
Expand All @@ -57,26 +57,26 @@ export default function Container({ element }: Props) {
payload: {
containerId: id,
elementDetails: {
type: "2Col",
id: nanoid(),
name: "Two Columns",
styles: { ...defaultStyles, display: "flex" },
content: [
{
content: [],
type: "container",
id: nanoid(),
name: "Container",
styles: { ...defaultStyles, width: "100%" },
type: "container",
content: [],
},
{
content: [],
type: "container",
id: nanoid(),
name: "Container",
styles: { ...defaultStyles, width: "100%" },
type: "container",
content: [],
},
],
id: nanoid(),
name: "Two Columns",
styles: { ...defaultStyles, display: "flex" },
type: "2Col",
},
},
});
Expand All @@ -87,14 +87,14 @@ export default function Container({ element }: Props) {
payload: {
containerId: id,
elementDetails: {
content: { innerText: "Text Element" },
type: "text",
id: nanoid(),
name: "Text",
styles: {
color: "black",
...defaultStyles,
},
type: "text",
content: { innerText: "Text Element" },
},
},
});
Expand All @@ -105,16 +105,16 @@ export default function Container({ element }: Props) {
payload: {
containerId: id,
elementDetails: {
content: {
address: "",
},
type: "map",
id: nanoid(),
name: "map",
styles: {
color: "black",
...defaultStyles,
},
type: "map",
content: {
address: "",
},
},
},
});
Expand Down
4 changes: 2 additions & 2 deletions src/components/editor/elements/map-share.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Image from "next/image";
import { toast } from "sonner";
import ElementWrapper from "~/components/editor/elements/element-wrapper";
import type { EditorElement } from "~/components/editor/type";
import type { InferEditorElement } from "~/components/editor/type";

type MapType = "naver" | "kakao";
type MapUrls = Record<MapType, string>;

type Props = {
element: EditorElement;
element: InferEditorElement<"map">;
};

const MapShareComponents = ({ element }: Props) => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/editor/elements/text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import ElementWrapper from "~/components/editor/elements/element-wrapper";
import { useEditor } from "~/components/editor/provider";
import type { EditorElement } from "~/components/editor/type";
import type { InferEditorElement } from "~/components/editor/type";

type Props = {
element: EditorElement;
element: InferEditorElement<"text">;
};

export default function Text({ element }: Props) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,12 @@ export default function AdvanceSetting() {

const handleOnChanges = (e: any) => {
const styleSettings = e.target.id;
let value = e.target.value;
const styleObject = {
[styleSettings]: value,
};
const styleValue = e.target.value;

dispatch({
type: "UPDATE_ELEMENT",
type: "UPDATE_ELEMENT_STYLE",
payload: {
elementDetails: {
...editor.state.selectedElement,
styles: {
...editor.state.selectedElement.styles,
...styleObject,
},
},
[styleSettings]: styleValue,
},
});
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use client";

import { useEditor } from "~/components/editor/provider";
import type { InferEditorElement } from "~/components/editor/type";
import { Input } from "~/components/ui/input";

// TODO: Fix type safety
type Props = { element: any };
type Props = { element: InferEditorElement<"map"> };

export default function MapSetting({ element }: Props) {
const { dispatch } = useEditor();
Expand Down
44 changes: 25 additions & 19 deletions src/components/editor/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,42 @@ import { editorTabValue } from "~/components/editor/constant";

export type DeviceType = "Desktop" | "Mobile" | "Tablet";

export type EditorElementType =
| "__body"
| "container"
| "2Col"
| "text"
| "section"
| "image"
| "map"
| null;

export type EditorTabTypeValue =
(typeof editorTabValue)[keyof typeof editorTabValue];

export type EditorElement = {
id: string;
styles: React.CSSProperties;
name: string;
type: EditorElementType;
content:
| EditorElement[]
| { href?: string; innerText?: string; src?: string; address?: string };
type EditorElementContentMap = {
__body: EditorElement[];
container: EditorElement[];
"2Col": EditorElement[];
text: { innerText: string };
image: { src: string; alt?: string };
map: { address: string };
empty: [];
};

export type EditorElementType = keyof EditorElementContentMap;

export type EditorElement = {
[K in EditorElementType]: {
type: K;
id: string;
name: string;
styles: React.CSSProperties;
content: EditorElementContentMap[K];
};
}[EditorElementType];

export type InferEditorElement<K extends EditorElementType> = Extract<
EditorElement,
{ type: K }
>;

export type EditorState = {
elements: EditorElement[];
selectedElement: EditorElement;
currentTabValue: EditorTabTypeValue;
device: DeviceType;
isPreviewMode: boolean;
funnelPageId: string;
};

export type EditorHistory = {
Expand Down

0 comments on commit 66c4e2c

Please sign in to comment.