Skip to content

A free and open-source block-based email template builder.

Using zustand for EditorState (#41)

cohitre5872c47afac5
diff --git a/packages/editor-sample/package-lock.json b/packages/editor-sample/package-lock.json
index feec7f6..2bc7b08 100644
--- a/packages/editor-sample/package-lock.json
+++ b/packages/editor-sample/package-lock.json
@@ -18,15 +18,16 @@
"@usewaypoint/block-container": "^0.0.1",
"@usewaypoint/block-divider": "^0.0.3",
"@usewaypoint/block-heading": "^0.0.2",
- "@usewaypoint/block-html": "^0.0.1",
- "@usewaypoint/block-image": "^0.0.3",
+ "@usewaypoint/block-html": "^0.0.2",
+ "@usewaypoint/block-image": "^0.0.4",
"@usewaypoint/block-spacer": "^0.0.2",
- "@usewaypoint/block-text": "^0.0.1",
+ "@usewaypoint/block-text": "^0.0.2",
"@usewaypoint/document-core": "^0.0.1",
"react": "^18.2.0",
"react-colorful": "^5.6.1",
"react-dom": "^18.2.0",
- "zod": "^3.22.4"
+ "zod": "^3.22.4",
+ "zustand": "^4.5.1"
},
"devDependencies": {
"@types/react": "^18.2.55",
@@ -1903,18 +1904,18 @@
}
},
"node_modules/@usewaypoint/block-html": {
- "version": "0.0.1",
- "resolved": "https://registry.npmjs.org/@usewaypoint/block-html/-/block-html-0.0.1.tgz",
- "integrity": "sha512-CK/5E/kdiHSWD8OmQFjrkMDw3bperARnJm/iXFv1y3pE98lv+rEH5pfrwuTvMb04GNtD/hcaVk470DvlxErrnQ==",
+ "version": "0.0.2",
+ "resolved": "https://registry.npmjs.org/@usewaypoint/block-html/-/block-html-0.0.2.tgz",
+ "integrity": "sha512-QEaMF1DRk+MgJiC8s4TfQe4eBpJghzriIPyS/+hxRVAiDwbOmRencaPRZ4JpDBi34sVNwLvRJZa5KvUFN4w6hg==",
"peerDependencies": {
"react": "^16 || ^17 || ^18",
"zod": "^1 || ^2 || ^3"
}
},
"node_modules/@usewaypoint/block-image": {
- "version": "0.0.3",
- "resolved": "https://registry.npmjs.org/@usewaypoint/block-image/-/block-image-0.0.3.tgz",
- "integrity": "sha512-lYyYxAc62hRwbRF0ovHGh01prxg01aJNHz6fqFd6s5kRzS6bcgxbFmkMR1be7CSQiwM4s+tiUDP7T4Ix6FcFqA==",
+ "version": "0.0.4",
+ "resolved": "https://registry.npmjs.org/@usewaypoint/block-image/-/block-image-0.0.4.tgz",
+ "integrity": "sha512-QNslGaV4zrgXj+VtbAktT7QBUxoelIyDMOgQtF8HsZzkfl66S28MtwwofodQpzKRtUrtegQ9o9hpL5zgjQUv0w==",
"peerDependencies": {
"react": "^16 || ^17 || ^18",
"zod": "^1 || ^2 || ^3"
@@ -1930,9 +1931,9 @@
}
},
"node_modules/@usewaypoint/block-text": {
- "version": "0.0.1",
- "resolved": "https://registry.npmjs.org/@usewaypoint/block-text/-/block-text-0.0.1.tgz",
- "integrity": "sha512-GySvdkOyCJ/8THEcJEdRl0WsuBgYR2GAVp4fBGuQleduq2sYgGzZxnV5dC/n+jdgQt7z5jYplgKeG1n8j+14oQ==",
+ "version": "0.0.2",
+ "resolved": "https://registry.npmjs.org/@usewaypoint/block-text/-/block-text-0.0.2.tgz",
+ "integrity": "sha512-iX4hxq/Rql5syTuXQYB3BGm6vZDKns83FUHH66y06pQuSLPR7i0YvokOLm2Ez96S+i6NOh3XVTg/wcx2Hg2FgA==",
"peerDependencies": {
"react": "^16 || ^17 || ^18",
"zod": "^1 || ^2 || ^3"
@@ -3655,6 +3656,14 @@
"punycode": "^2.1.0"
}
},
+ "node_modules/use-sync-external-store": {
+ "version": "1.2.0",
+ "resolved": "https://registry.npmjs.org/use-sync-external-store/-/use-sync-external-store-1.2.0.tgz",
+ "integrity": "sha512-eEgnFxGQ1Ife9bzYs6VLi8/4X6CObHMw9Qr9tPY43iKwsPw8xE8+EFsf/2cFZ5S3esXgpWgtSCtLNS41F+sKPA==",
+ "peerDependencies": {
+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0"
+ }
+ },
"node_modules/vite": {
"version": "5.1.4",
"resolved": "https://registry.npmjs.org/vite/-/vite-5.1.4.tgz",
@@ -3764,6 +3773,33 @@
"funding": {
"url": "https://github.com/sponsors/colinhacks"
}
+ },
+ "node_modules/zustand": {
+ "version": "4.5.1",
+ "resolved": "https://registry.npmjs.org/zustand/-/zustand-4.5.1.tgz",
+ "integrity": "sha512-XlauQmH64xXSC1qGYNv00ODaQ3B+tNPoy22jv2diYiP4eoDKr9LA+Bh5Bc3gplTrFdb6JVI+N4kc1DZ/tbtfPg==",
+ "dependencies": {
+ "use-sync-external-store": "1.2.0"
+ },
+ "engines": {
+ "node": ">=12.7.0"
+ },
+ "peerDependencies": {
+ "@types/react": ">=16.8",
+ "immer": ">=9.0.6",
+ "react": ">=16.8"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ },
+ "immer": {
+ "optional": true
+ },
+ "react": {
+ "optional": true
+ }
+ }
}
}
}
diff --git a/packages/editor-sample/package.json b/packages/editor-sample/package.json
index 8d2d7bf..3a470a7 100644
--- a/packages/editor-sample/package.json
+++ b/packages/editor-sample/package.json
@@ -18,15 +18,16 @@
"@usewaypoint/block-container": "^0.0.1",
"@usewaypoint/block-divider": "^0.0.3",
"@usewaypoint/block-heading": "^0.0.2",
- "@usewaypoint/block-html": "^0.0.1",
- "@usewaypoint/block-image": "^0.0.3",
+ "@usewaypoint/block-html": "^0.0.2",
+ "@usewaypoint/block-image": "^0.0.4",
"@usewaypoint/block-spacer": "^0.0.2",
- "@usewaypoint/block-text": "^0.0.1",
+ "@usewaypoint/block-text": "^0.0.2",
"@usewaypoint/document-core": "^0.0.1",
"react": "^18.2.0",
"react-colorful": "^5.6.1",
"react-dom": "^18.2.0",
- "zod": "^3.22.4"
+ "zod": "^3.22.4",
+ "zustand": "^4.5.1"
},
"devDependencies": {
"@types/react": "^18.2.55",
diff --git a/packages/editor-sample/src/App/InspectorDrawer/ConfigurationPanel/index.tsx b/packages/editor-sample/src/App/InspectorDrawer/ConfigurationPanel/index.tsx
index eea3fbd..fc1b52d 100644
--- a/packages/editor-sample/src/App/InspectorDrawer/ConfigurationPanel/index.tsx
+++ b/packages/editor-sample/src/App/InspectorDrawer/ConfigurationPanel/index.tsx
@@ -3,7 +3,7 @@ import React from 'react';
import { Box, Typography } from '@mui/material';
import { TEditorBlock } from '../../../documents/editor/core';
-import { useEditorState } from '../../../documents/editor/EditorContext';
+import { setEditorState, useDocument, useSelectedBlockId } from '../../../documents/editor/EditorContext';
import AvatarSidebarPanel from './input-panels/AvatarSidebarPanel';
import ButtonSidebarPanel from './input-panels/ButtonSidebarPanel';
@@ -26,7 +26,8 @@ function renderMessage(val: string) {
}
export default function ConfigurationPanel() {
- const [{ document, selectedBlockId }, setEditorState] = useEditorState();
+ const document = useDocument();
+ const selectedBlockId = useSelectedBlockId();
if (!selectedBlockId) {
return renderMessage('Click on a block to inspect.');
diff --git a/packages/editor-sample/src/App/InspectorDrawer/StylesPanel.tsx b/packages/editor-sample/src/App/InspectorDrawer/StylesPanel.tsx
index 083e0f3..369eb41 100644
--- a/packages/editor-sample/src/App/InspectorDrawer/StylesPanel.tsx
+++ b/packages/editor-sample/src/App/InspectorDrawer/StylesPanel.tsx
@@ -1,14 +1,12 @@
import React from 'react';
import { TEditorBlock } from '../../documents/editor/core';
-import { useEditorState } from '../../documents/editor/EditorContext';
+import { setEditorState, useDocument } from '../../documents/editor/EditorContext';
import EmailLayoutSidebarPanel from './ConfigurationPanel/input-panels/EmailLayoutSidebarPanel';
export default function StylesPanel() {
- const [{ document }, setEditorState] = useEditorState();
-
- const block = document.root;
+ const block = useDocument().root;
if (!block) {
return <p>Block not found</p>;
}
diff --git a/packages/editor-sample/src/App/InspectorDrawer/ToggleInspectorPanelButton.tsx b/packages/editor-sample/src/App/InspectorDrawer/ToggleInspectorPanelButton.tsx
index 7f10c56..a07b39b 100644
--- a/packages/editor-sample/src/App/InspectorDrawer/ToggleInspectorPanelButton.tsx
+++ b/packages/editor-sample/src/App/InspectorDrawer/ToggleInspectorPanelButton.tsx
@@ -3,10 +3,11 @@ import React from 'react';
import { AppRegistrationOutlined, LastPageOutlined } from '@mui/icons-material';
import { IconButton } from '@mui/material';
-import { useEditorState } from '../../documents/editor/EditorContext';
+import { setEditorState, useInspectorDrawerOpen } from '../../documents/editor/EditorContext';
export default function ToggleInspectorPanelButton() {
- const [{ inspectorDrawerOpen }, setEditorState] = useEditorState();
+ const inspectorDrawerOpen = useInspectorDrawerOpen();
+
const handleClick = () => {
setEditorState({ inspectorDrawerOpen: !inspectorDrawerOpen });
};
diff --git a/packages/editor-sample/src/App/InspectorDrawer/index.tsx b/packages/editor-sample/src/App/InspectorDrawer/index.tsx
index 38e17e1..508557f 100644
--- a/packages/editor-sample/src/App/InspectorDrawer/index.tsx
+++ b/packages/editor-sample/src/App/InspectorDrawer/index.tsx
@@ -2,7 +2,7 @@ import React from 'react';
import { Box, Drawer, Tab, Tabs } from '@mui/material';
-import { useEditorState } from '../../documents/editor/EditorContext';
+import { setEditorState, useInspectorDrawerOpen, useSelectedSidebarTab } from '../../documents/editor/EditorContext';
import ConfigurationPanel from './ConfigurationPanel';
import StylesPanel from './StylesPanel';
@@ -10,7 +10,8 @@ import StylesPanel from './StylesPanel';
export const INSPECTOR_DRAWER_WIDTH = 320;
export default function InspectorDrawer() {
- const [{ selectedSidebarTab, inspectorDrawerOpen }, setEditorState] = useEditorState();
+ const selectedSidebarTab = useSelectedSidebarTab();
+ const inspectorDrawerOpen = useInspectorDrawerOpen();
const renderCurrentSidebarPanel = () => {
switch (selectedSidebarTab) {
diff --git a/packages/editor-sample/src/App/SamplesDrawer/ToggleSamplesPanelButton.tsx b/packages/editor-sample/src/App/SamplesDrawer/ToggleSamplesPanelButton.tsx
index f470c5a..42f847b 100644
--- a/packages/editor-sample/src/App/SamplesDrawer/ToggleSamplesPanelButton.tsx
+++ b/packages/editor-sample/src/App/SamplesDrawer/ToggleSamplesPanelButton.tsx
@@ -3,10 +3,11 @@ import React from 'react';
import { FirstPageOutlined, MenuOutlined } from '@mui/icons-material';
import { IconButton } from '@mui/material';
-import { useEditorState } from '../../documents/editor/EditorContext';
+import { setEditorState, useSamplesDrawerOpen } from '../../documents/editor/EditorContext';
export default function ToggleSamplesPanelButton() {
- const [{ samplesDrawerOpen }, setEditorState] = useEditorState();
+ const samplesDrawerOpen = useSamplesDrawerOpen();
+
const handleClick = () => {
setEditorState({ samplesDrawerOpen: !samplesDrawerOpen });
};
diff --git a/packages/editor-sample/src/App/SamplesDrawer/index.tsx b/packages/editor-sample/src/App/SamplesDrawer/index.tsx
index 2903df3..7f23a70 100644
--- a/packages/editor-sample/src/App/SamplesDrawer/index.tsx
+++ b/packages/editor-sample/src/App/SamplesDrawer/index.tsx
@@ -2,14 +2,14 @@ import React from 'react';
import { Box, Button, Drawer, Link, Stack, Typography } from '@mui/material';
-import { useEditorState } from '../../documents/editor/EditorContext';
+import { useSamplesDrawerOpen } from '../../documents/editor/EditorContext';
import logo from './waypoint.svg';
export const SAMPLES_DRAWER_WIDTH = 240;
export default function SamplesDrawer() {
- const [{ samplesDrawerOpen }] = useEditorState();
+ const samplesDrawerOpen = useSamplesDrawerOpen();
const select = (val: string) => {
window.location.hash = `#sample/${val}`;
diff --git a/packages/editor-sample/src/App/TemplatePanel/HtmlPanel.tsx b/packages/editor-sample/src/App/TemplatePanel/HtmlPanel.tsx
index 852d52a..41d4a81 100644
--- a/packages/editor-sample/src/App/TemplatePanel/HtmlPanel.tsx
+++ b/packages/editor-sample/src/App/TemplatePanel/HtmlPanel.tsx
@@ -1,12 +1,13 @@
import * as React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
-import { useEditorState } from '../../documents/editor/EditorContext';
+import { useDocument } from '../../documents/editor/EditorContext';
import ReaderBlock from '../../documents/reader/ReaderBlock';
import { ReaderProvider } from '../../documents/reader/ReaderContext';
export default function HtmlPanel() {
- const [{ document }] = useEditorState();
+ const document = useDocument();
+
const string = React.useMemo(() => {
return renderToStaticMarkup(
<ReaderProvider value={document}>
diff --git a/packages/editor-sample/src/App/TemplatePanel/ShareButton.tsx b/packages/editor-sample/src/App/TemplatePanel/ShareButton.tsx
index 5e0f71c..d68e2e4 100644
--- a/packages/editor-sample/src/App/TemplatePanel/ShareButton.tsx
+++ b/packages/editor-sample/src/App/TemplatePanel/ShareButton.tsx
@@ -3,10 +3,10 @@ import React, { useState } from 'react';
import { IosShareOutlined } from '@mui/icons-material';
import { IconButton, Snackbar, Tooltip } from '@mui/material';
-import { useEditorState } from '../../documents/editor/EditorContext';
+import { useDocument } from '../../documents/editor/EditorContext';
export default function ShareButton() {
- const [{ document }] = useEditorState();
+ const document = useDocument();
const [message, setMessage] = useState<string | null>(null);
const onClick = async () => {
diff --git a/packages/editor-sample/src/App/TemplatePanel/index.tsx b/packages/editor-sample/src/App/TemplatePanel/index.tsx
index 8d8d7ee..a1596ef 100644
--- a/packages/editor-sample/src/App/TemplatePanel/index.tsx
+++ b/packages/editor-sample/src/App/TemplatePanel/index.tsx
@@ -4,7 +4,7 @@ import { CodeOutlined, DataObjectOutlined, EditOutlined, PreviewOutlined } from
import { Box, Stack, Tab, Tabs, Tooltip } from '@mui/material';
import EditorBlock from '../../documents/editor/EditorBlock';
-import { useEditorState } from '../../documents/editor/EditorContext';
+import { setEditorState, useDocument, useSelectedMainTab } from '../../documents/editor/EditorContext';
import ReaderBlock from '../../documents/reader/ReaderBlock';
import { ReaderProvider } from '../../documents/reader/ReaderContext';
import ToggleInspectorPanelButton from '../InspectorDrawer/ToggleInspectorPanelButton';
@@ -14,7 +14,8 @@ import HtmlPanel from './HtmlPanel';
import ShareButton from './ShareButton';
export default function TemplatePanel() {
- const [{ document, selectedMainTab }, setEditorState] = useEditorState();
+ const document = useDocument();
+ const selectedMainTab = useSelectedMainTab();
const renderMainPanel = () => {
switch (selectedMainTab) {
diff --git a/packages/editor-sample/src/App/index.tsx b/packages/editor-sample/src/App/index.tsx
index 626cfaf..03fd89f 100644
--- a/packages/editor-sample/src/App/index.tsx
+++ b/packages/editor-sample/src/App/index.tsx
@@ -1,8 +1,9 @@
-import React from 'react';
+import React, { useEffect } from 'react';
import { Stack, useTheme } from '@mui/material';
-import { useEditorState } from '../documents/editor/EditorContext';
+import { setEditorState, useInspectorDrawerOpen, useSamplesDrawerOpen } from '../documents/editor/EditorContext';
+import getConfiguration from '../getConfiguration';
import InspectorDrawer, { INSPECTOR_DRAWER_WIDTH } from './InspectorDrawer';
import SamplesDrawer, { SAMPLES_DRAWER_WIDTH } from './SamplesDrawer';
@@ -10,7 +11,17 @@ import TemplatePanel from './TemplatePanel';
export default function App() {
const theme = useTheme();
- const [{ inspectorDrawerOpen, samplesDrawerOpen }] = useEditorState();
+ const inspectorDrawerOpen = useInspectorDrawerOpen();
+ const samplesDrawerOpen = useSamplesDrawerOpen();
+
+ useEffect(() => {
+ function refresh() {
+ setEditorState({ document: getConfiguration(window.location.hash) });
+ }
+ refresh();
+ window.addEventListener('hashchange', refresh);
+ return () => window.removeEventListener('hashchange', refresh);
+ });
return (
<>
diff --git a/packages/editor-sample/src/documents/blocks/ColumnsContainer/index.tsx b/packages/editor-sample/src/documents/blocks/ColumnsContainer/index.tsx
index 4952e4e..ea7415c 100644
--- a/packages/editor-sample/src/documents/blocks/ColumnsContainer/index.tsx
+++ b/packages/editor-sample/src/documents/blocks/ColumnsContainer/index.tsx
@@ -4,7 +4,7 @@ import { ColumnsContainer as BaseColumnsContainer } from '@usewaypoint/block-col
import { TEditorBlock } from '../../editor/core';
import { useCurrentBlockId } from '../../editor/EditorBlock';
-import { useEditorState } from '../../editor/EditorContext';
+import { setEditorState, useDocument } from '../../editor/EditorContext';
import ReaderBlock from '../../reader/ReaderBlock';
import EditorChildrenIds from '../helpers/EditorChildrenIds';
@@ -23,7 +23,7 @@ export function ColumnsContainer({ style, props }: ColumnsContainerProps) {
const EMPTY_COLUMNS = [{ childrenIds: [] }, { childrenIds: [] }, { childrenIds: [] }];
export function EditorColumnsContainer({ style, props }: ColumnsContainerProps) {
- const [{ document }, setEditorState] = useEditorState();
+ const document = useDocument();
const blockId = useCurrentBlockId();
const { columns, ...restProps } = props ?? {};
diff --git a/packages/editor-sample/src/documents/blocks/Container/index.tsx b/packages/editor-sample/src/documents/blocks/Container/index.tsx
index 2e363bb..3f94e77 100644
--- a/packages/editor-sample/src/documents/blocks/Container/index.tsx
+++ b/packages/editor-sample/src/documents/blocks/Container/index.tsx
@@ -4,7 +4,7 @@ import { Container as BaseContainer } from '@usewaypoint/block-container';
import { TEditorBlock } from '../../editor/core';
import { useCurrentBlockId } from '../../editor/EditorBlock';
-import { useEditorState } from '../../editor/EditorContext';
+import { setEditorState, useDocument } from '../../editor/EditorContext';
import ReaderBlock from '../../reader/ReaderBlock';
import EditorChildrenIds from '../helpers/EditorChildrenIds';
@@ -24,7 +24,7 @@ export function Container({ style, props }: ContainerProps) {
export function EditorContainer({ style, props }: ContainerProps) {
const childrenIds = props?.childrenIds ?? [];
- const [{ document }, setEditorState] = useEditorState();
+ const document = useDocument();
const blockId = useCurrentBlockId();
const insertBlock = (blockConfiguration: TEditorBlock, i: number | null) => {
diff --git a/packages/editor-sample/src/documents/blocks/EmailLayout/index.tsx b/packages/editor-sample/src/documents/blocks/EmailLayout/index.tsx
index 3a06ccf..6f7ed4d 100644
--- a/packages/editor-sample/src/documents/blocks/EmailLayout/index.tsx
+++ b/packages/editor-sample/src/documents/blocks/EmailLayout/index.tsx
@@ -3,7 +3,7 @@ import { z } from 'zod';
import { TEditorBlock } from '../../editor/core';
import { useCurrentBlockId } from '../../editor/EditorBlock';
-import { useEditorState } from '../../editor/EditorContext';
+import { setEditorState, useDocument } from '../../editor/EditorContext';
import ReaderBlock from '../../reader/ReaderBlock';
import EditorChildrenIds from '../helpers/EditorChildrenIds';
@@ -37,7 +37,7 @@ export function EmailLayout(props: EmailLayoutProps) {
}
export function EditorEmailLayout(props: EmailLayoutProps) {
- const [{ document }, setEditorState] = useEditorState();
+ const document = useDocument();
const blockId = useCurrentBlockId();
const childrenIds = props.childrenIds;
diff --git a/packages/editor-sample/src/documents/blocks/helpers/block-wrappers/EditorBlockWrapper.tsx b/packages/editor-sample/src/documents/blocks/helpers/block-wrappers/EditorBlockWrapper.tsx
index 46ca343..7564f8c 100644
--- a/packages/editor-sample/src/documents/blocks/helpers/block-wrappers/EditorBlockWrapper.tsx
+++ b/packages/editor-sample/src/documents/blocks/helpers/block-wrappers/EditorBlockWrapper.tsx
@@ -3,7 +3,7 @@ import React, { CSSProperties, useState } from 'react';
import { Box } from '@mui/material';
import { useCurrentBlockId } from '../../../editor/EditorBlock';
-import { useEditorState } from '../../../editor/EditorContext';
+import { setEditorState, useSelectedBlockId } from '../../../editor/EditorContext';
import TuneMenu from './TuneMenu';
@@ -12,7 +12,7 @@ type TEditorBlockWrapperProps = {
};
export default function EditorBlockWrapper({ children }: TEditorBlockWrapperProps) {
- const [{ selectedBlockId }, setEditorState] = useEditorState();
+ const selectedBlockId = useSelectedBlockId();
const [mouseInside, setMouseInside] = useState(false);
const blockId = useCurrentBlockId();
diff --git a/packages/editor-sample/src/documents/blocks/helpers/block-wrappers/TuneMenu.tsx b/packages/editor-sample/src/documents/blocks/helpers/block-wrappers/TuneMenu.tsx
index b086ec5..1a17cb5 100644
--- a/packages/editor-sample/src/documents/blocks/helpers/block-wrappers/TuneMenu.tsx
+++ b/packages/editor-sample/src/documents/blocks/helpers/block-wrappers/TuneMenu.tsx
@@ -4,7 +4,7 @@ import { DeleteOutlined } from '@mui/icons-material';
import { IconButton, Paper, Stack, Tooltip } from '@mui/material';
import { TEditorBlock } from '../../../editor/core';
-import { useEditorState } from '../../../editor/EditorContext';
+import { setEditorState, useDocument } from '../../../editor/EditorContext';
import { ColumnsContainerProps } from '../../ColumnsContainer/ColumnsContainerPropsSchema';
const STYLE: CSSProperties = {
@@ -18,7 +18,7 @@ type Props = {
blockId: string;
};
export default function TuneMenu({ blockId }: Props) {
- const [{ document }, setEditorState] = useEditorState();
+ const document = useDocument();
const handleDeleteClick = () => {
const nDocument: typeof document = { ...document };
diff --git a/packages/editor-sample/src/documents/editor/EditorBlock.tsx b/packages/editor-sample/src/documents/editor/EditorBlock.tsx
index 2471bd4..a600b37 100644
--- a/packages/editor-sample/src/documents/editor/EditorBlock.tsx
+++ b/packages/editor-sample/src/documents/editor/EditorBlock.tsx
@@ -1,7 +1,7 @@
import React, { createContext, useContext } from 'react';
import { EditorBlock as CoreEditorBlock } from './core';
-import { useEditorState } from './EditorContext';
+import { useDocument } from './EditorContext';
const EditorBlockContext = createContext<string | null>(null);
export const useCurrentBlockId = () => useContext(EditorBlockContext)!;
@@ -16,8 +16,8 @@ type EditorBlockProps = {
* @returns EditorBlock component that loads data from the EditorDocumentContext
*/
export default function EditorBlock({ id }: EditorBlockProps) {
- const [state] = useEditorState();
- const block = state.document[id];
+ const document = useDocument();
+ const block = document[id];
if (!block) {
throw new Error('Could not find block');
}
diff --git a/packages/editor-sample/src/documents/editor/EditorContext.tsx b/packages/editor-sample/src/documents/editor/EditorContext.tsx
index 4c2fbc7..1a11df6 100644
--- a/packages/editor-sample/src/documents/editor/EditorContext.tsx
+++ b/packages/editor-sample/src/documents/editor/EditorContext.tsx
@@ -1,4 +1,6 @@
-import React, { createContext, useContext, useMemo, useState } from 'react';
+import { create } from 'zustand';
+
+import EMPTY_EMAIL_MESSAGE from '../../getConfiguration/sample/empty-email-message';
import { TEditorConfiguration } from './core';
@@ -12,40 +14,41 @@ type TValue = {
inspectorDrawerOpen: boolean;
samplesDrawerOpen: boolean;
};
-type TEditorContextState = [state: TValue, setState: (v: Partial<TValue>) => void];
-const DEFAULT_STATE: TValue = {
- document: {},
+const useEditorState = create<TValue>(() => ({
+ document: EMPTY_EMAIL_MESSAGE,
selectedBlockId: null,
selectedSidebarTab: 'styles',
selectedMainTab: 'editor',
inspectorDrawerOpen: true,
samplesDrawerOpen: true,
-};
-const EditorContext = createContext<TEditorContextState>([DEFAULT_STATE, () => {}]);
+}));
-export function useEditorState() {
- return useContext(EditorContext);
+export function useDocument() {
+ return useEditorState((s) => s.document);
}
-type EditorProviderProps = {
- defaultValue: TEditorConfiguration;
- children: Parameters<typeof EditorContext.Provider>[0]['children'];
-};
-export function EditorProvider({ defaultValue, children }: EditorProviderProps) {
- const [state, setState] = useState<TValue>(() => ({
- ...DEFAULT_STATE,
- document: defaultValue,
- }));
- const value = useMemo<TEditorContextState>(
- () => [
- state,
- (s: Partial<TValue>) => {
- setState({ ...state, ...s });
- },
- ],
- [state, setState]
- );
- return <EditorContext.Provider value={value}>{children}</EditorContext.Provider>;
+export function useSelectedBlockId() {
+ return useEditorState((s) => s.selectedBlockId);
+}
+
+export function useSelectedMainTab() {
+ return useEditorState((s) => s.selectedMainTab);
+}
+
+export function useSelectedSidebarTab() {
+ return useEditorState((s) => s.selectedSidebarTab);
+}
+
+export function useInspectorDrawerOpen() {
+ return useEditorState((s) => s.inspectorDrawerOpen);
+}
+
+export function useSamplesDrawerOpen() {
+ return useEditorState((s) => s.samplesDrawerOpen);
+}
+
+export function setEditorState(state: Partial<TValue>) {
+ useEditorState.setState({ ...state });
}
diff --git a/packages/editor-sample/src/getConfiguration/index.tsx b/packages/editor-sample/src/getConfiguration/index.tsx
index 9c17038..d6aea4d 100644
--- a/packages/editor-sample/src/getConfiguration/index.tsx
+++ b/packages/editor-sample/src/getConfiguration/index.tsx
@@ -1,3 +1,4 @@
+import EMPTY_EMAIL_MESSAGE from './sample/empty-email-message';
import ONE_TIME_PASSCODE from './sample/one-time-passcode';
import ORDER_ECOMMERCE from './sample/order-ecommerce';
import POST_METRICS_REPORT from './sample/post-metrics-report';
@@ -46,17 +47,5 @@ export default function getConfiguration(template: string) {
}
}
- return {
- root: {
- type: 'EmailLayout',
- data: {
- backdropColor: '#e5e7e5',
- canvasColor: '#FFFFFF',
- textColor: '#242424',
- accentColor: '#0b5499',
- fontFamily: 'MODERN_SANS',
- childrenIds: [],
- },
- },
- };
+ return EMPTY_EMAIL_MESSAGE;
}
diff --git a/packages/editor-sample/src/getConfiguration/sample/empty-email-message.ts b/packages/editor-sample/src/getConfiguration/sample/empty-email-message.ts
new file mode 100644
index 0000000..d98e7a0
--- /dev/null
+++ b/packages/editor-sample/src/getConfiguration/sample/empty-email-message.ts
@@ -0,0 +1,16 @@
+import { TEditorConfiguration } from '../../documents/editor/core';
+
+const EMPTY_EMAIL_MESSAGE: TEditorConfiguration = {
+ root: {
+ type: 'EmailLayout',
+ data: {
+ backdropColor: '#e5e7e5',
+ canvasColor: '#FFFFFF',
+ textColor: '#242424',
+ fontFamily: 'MODERN_SANS',
+ childrenIds: [],
+ },
+ },
+};
+
+export default EMPTY_EMAIL_MESSAGE;
diff --git a/packages/editor-sample/src/main.tsx b/packages/editor-sample/src/main.tsx
index 14d2ab8..e17649e 100644
--- a/packages/editor-sample/src/main.tsx
+++ b/packages/editor-sample/src/main.tsx
@@ -1,38 +1,17 @@
-import React, { useEffect, useState } from 'react';
+import React from 'react';
import ReactDOM from 'react-dom/client';
import CssBaseline from '@mui/material/CssBaseline';
import { ThemeProvider } from '@mui/material/styles';
import App from './App';
-import { EditorProvider } from './documents/editor/EditorContext';
-import getConfiguration from './getConfiguration';
import theme from './theme';
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<ThemeProvider theme={theme}>
<CssBaseline />
- <ConfigurationLoader />
+ <App />
</ThemeProvider>
</React.StrictMode>
);
-
-function ConfigurationLoader() {
- const [id, setId] = useState(1);
- const [configuration, setConfiguration] = useState(() => getConfiguration(window.location.hash));
-
- useEffect(() => {
- function refresh() {
- setId(id + 1);
- setConfiguration(getConfiguration(window.location.hash));
- }
- window.addEventListener('hashchange', refresh);
- return () => window.removeEventListener('hashchange', refresh);
- });
- return (
- <EditorProvider key={`root-${id}`} defaultValue={configuration}>
- <App />
- </EditorProvider>
- );
-}