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

Consuming block-container from the editor #32

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/editor-sample/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/editor-sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@mui/material": "^5.15.10",
"@usewaypoint/block-avatar": "^0.0.1",
"@usewaypoint/block-button": "^0.0.1",
"@usewaypoint/block-container": "^0.0.1",
"@usewaypoint/block-divider": "^0.0.3",
"@usewaypoint/block-heading": "^0.0.2",
"@usewaypoint/block-html": "^0.0.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useState } from 'react';

import { ContainerProps } from '../../../../documents/blocks/Container';
import { ContainerPropsSchema } from '../../../../documents/blocks/Container/ContainerPropsSchema';
import { ContainerProps, ContainerPropsSchema } from '../../../../documents/blocks/Container/ContainerPropsSchema';

import BaseSidebarPanel from './helpers/BaseSidebarPanel';
import MultiStylePropertyPanel from './helpers/style-inputs/MultiStylePropertyPanel';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import { z } from 'zod';

import { zColor, zPadding } from '../helpers/zod';
import { ContainerPropsSchema as BaseContainerPropsSchema } from '@usewaypoint/block-container';

export const ContainerPropsSchema = z.object({
style: z
style: BaseContainerPropsSchema.shape.style,
props: z
.object({
backgroundColor: zColor().nullable().default(null),
borderColor: zColor().optional().nullable().default(null),
borderRadius: z.number().optional().nullable().default(0),
padding: zPadding().optional().default({
top: 16,
bottom: 16,
left: 24,
right: 24,
}),
childrenIds: z.array(z.string()).optional().nullable(),
})
.default({}),
props: z.object({
childrenIds: z.array(z.string()),
}),
.optional()
.nullable(),
});

export type ContainerProps = z.infer<typeof ContainerPropsSchema>;
28 changes: 17 additions & 11 deletions packages/editor-sample/src/documents/blocks/Container/index.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import React from 'react';
import { z } from 'zod';

import { Container as BaseContainer } from '@usewaypoint/block-container';

import { TEditorBlock } from '../../editor/core';
import { useCurrentBlockId } from '../../editor/EditorBlock';
import { useEditorState } from '../../editor/EditorContext';
import ReaderBlock from '../../reader/ReaderBlock';
import EditorChildrenIds from '../helpers/EditorChildrenIds';

import { ContainerPropsSchema } from './ContainerPropsSchema';

export type ContainerProps = z.infer<typeof ContainerPropsSchema>;
import { ContainerProps } from './ContainerPropsSchema';

export function Container({ props: { childrenIds } }: ContainerProps) {
export function Container({ style, props }: ContainerProps) {
const childrenIds = props?.childrenIds ?? [];
return (
<>
<BaseContainer style={style}>
{childrenIds.map((childId) => (
<ReaderBlock key={childId} id={childId} />
))}
</>
</BaseContainer>
);
}

export function EditorContainer({ props: { childrenIds } }: ContainerProps) {
export function EditorContainer({ style, props }: ContainerProps) {
const childrenIds = props?.childrenIds ?? [];

const [{ document }, setEditorState] = useEditorState();
const blockId = useCurrentBlockId();

Expand All @@ -33,6 +35,7 @@ export function EditorContainer({ props: { childrenIds } }: ContainerProps) {
} else {
nChildrenIds = [...childrenIds.slice(0, i), id, ...childrenIds.slice(i)];
}

setEditorState({
selectedBlockId: id,
document: {
Expand All @@ -44,11 +47,14 @@ export function EditorContainer({ props: { childrenIds } }: ContainerProps) {
...document[blockId].data,
props: { childrenIds: nChildrenIds },
},
} as TEditorBlock,
},
},
});
setEditorState({});
};

return <EditorChildrenIds childrenIds={childrenIds} insertBlock={insertBlock} />;
return (
<BaseContainer style={style}>
<EditorChildrenIds childrenIds={childrenIds} insertBlock={insertBlock} />
</BaseContainer>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {

import { TEditorBlock } from '../../../../editor/core';
import ColumnsContainerPropsSchema from '../../../ColumnsContainer/ColumnsContainerPropsSchema';
import { ContainerPropsSchema } from '../../../Container/ContainerPropsSchema';

type TButtonProps = {
label: string;
Expand Down Expand Up @@ -139,9 +138,7 @@ export const BUTTONS: TButtonProps[] = [
icon: <LibraryAddOutlined />,
block: () => ({
type: 'Container',
data: ContainerPropsSchema.parse({
props: { childrenIds: [] },
}),
data: {},
}),
},

Expand Down
8 changes: 6 additions & 2 deletions packages/editor-sample/src/documents/editor/core.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { buildBlockComponent, buildBlockConfigurationSchema } from '@usewaypoint
import { EditorColumnsContainer } from '../blocks/ColumnsContainer';
import ColumnsContainerPropsSchema from '../blocks/ColumnsContainer/ColumnsContainerPropsSchema';
import { EditorContainer } from '../blocks/Container';
import { ContainerPropsSchema } from '../blocks/Container/ContainerPropsSchema';
import { ContainerProps, ContainerPropsSchema } from '../blocks/Container/ContainerPropsSchema';
import { EditorEmailLayout, EmailLayoutProps } from '../blocks/EmailLayout';
import { EmailLayoutPropsSchema } from '../blocks/EmailLayout/EmailLayoutPropsSchema';
import { addEditorBlockWrapper } from '../blocks/helpers/block-wrappers';
Expand All @@ -39,7 +39,11 @@ const EDITOR_DICTIONARY = {
},
Container: {
schema: ContainerPropsSchema,
Component: addEditorBlockWrapper(EditorContainer),
Component: (props: ContainerProps) => (
<EditorBlockWrapper>
<EditorContainer {...props} />
</EditorBlockWrapper>
),
},
ColumnsContainer: {
schema: ColumnsContainerPropsSchema,
Expand Down
2 changes: 1 addition & 1 deletion packages/editor-sample/src/documents/reader/core.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const READER_DICTIONARY = {
},
Container: {
schema: ContainerPropsSchema,
Component: addReaderBlockWrapper(Container),
Component: Container,
},
Divider: {
schema: DividerPropsSchema,
Expand Down
Loading