Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfeng33 committed Dec 2, 2024
1 parent 4b1ea0d commit aca891d
Show file tree
Hide file tree
Showing 9 changed files with 365 additions and 153 deletions.
93 changes: 71 additions & 22 deletions apps/www/src/registry/default/example/playground-demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ import {
import { CaptionPlugin } from '@udecode/plate-caption/react';
import { CodeBlockPlugin } from '@udecode/plate-code-block/react';
import { CommentsPlugin } from '@udecode/plate-comments/react';
import { ParagraphPlugin, Plate } from '@udecode/plate-common/react';
import {
ParagraphPlugin,
Plate,
PlateLeaf,
PlateView,
usePlateEditor,
} from '@udecode/plate-common/react';
import { DndPlugin } from '@udecode/plate-dnd';
import { DocxPlugin } from '@udecode/plate-docx';
import { EmojiPlugin } from '@udecode/plate-emoji/react';
Expand All @@ -35,10 +41,10 @@ import {
FontColorPlugin,
FontSizePlugin,
} from '@udecode/plate-font/react';
import { HEADING_KEYS } from '@udecode/plate-heading';
import { HeadingPlugin } from '@udecode/plate-heading/react';
import { HighlightPlugin } from '@udecode/plate-highlight/react';
import { HorizontalRulePlugin } from '@udecode/plate-horizontal-rule/react';
import { serializeHtml } from '@udecode/plate-html/react';
import { IndentPlugin } from '@udecode/plate-indent/react';
import { IndentListPlugin } from '@udecode/plate-indent-list/react';
import { JuicePlugin } from '@udecode/plate-juice';
Expand All @@ -52,7 +58,6 @@ import { ImagePlugin, MediaEmbedPlugin } from '@udecode/plate-media/react';
import { MentionPlugin } from '@udecode/plate-mention/react';
import { NodeIdPlugin } from '@udecode/plate-node-id';
import { NormalizeTypesPlugin } from '@udecode/plate-normalizers';
import { PlaywrightPlugin } from '@udecode/plate-playwright';
import { ResetNodePlugin } from '@udecode/plate-reset-node/react';
import { DeletePlugin, SelectOnBackspacePlugin } from '@udecode/plate-select';
import {
Expand All @@ -65,8 +70,6 @@ import { TogglePlugin } from '@udecode/plate-toggle/react';
import { TrailingBlockPlugin } from '@udecode/plate-trailing-block';

import { settingsStore } from '@/components/context/settings-store';
import { copilotPlugins } from '@/registry/default/components/editor/plugins/copilot-plugins';
import { editorPlugins } from '@/registry/default/components/editor/plugins/editor-plugins';
import { useCreateEditor } from '@/registry/default/components/editor/use-create-editor';
import { aiValue } from '@/registry/default/example/values/ai-value';
import { alignValue } from '@/registry/default/example/values/align-value';
Expand Down Expand Up @@ -110,6 +113,22 @@ import { tocPlaygroundValue } from '@/registry/default/example/values/toc-value'
import { toggleValue } from '@/registry/default/example/values/toggle-value';
import { Editor, EditorContainer } from '@/registry/default/plate-ui/editor';

import { basicNodesPlugins } from '../components/editor/plugins/basic-nodes-plugins';
import { Button } from '../plate-ui/button';
import { basicNodesValue } from './values/basic-nodes-value';
import { withProps } from '@udecode/cn';
import {
HeadingElement,
HeadingElementView,
} from '../plate-ui/heading-element';
import { HEADING_KEYS } from '@udecode/plate-heading';
import {
ParagraphElement,
ParagraphElementView,
PlateViewLeaf,
} from '../plate-ui/paragraph-element';


export default function PlaygroundDemo({ className }: { className?: string }) {
const value = usePlaygroundValue();
const enabled = usePlaygroundEnabled();
Expand All @@ -118,31 +137,61 @@ export default function PlaygroundDemo({ className }: { className?: string }) {
{
override: { enabled },
plugins: [
...copilotPlugins,
...editorPlugins,
// HtmlPlugin,
...basicNodesPlugins,
// ...copilotPlugins,
// ...editorPlugins,

NormalizeTypesPlugin.configure({
options: {
rules: [{ path: [0], strictType: HEADING_KEYS.h1 }],
},
}),
// NormalizeTypesPlugin.configure({
// options: {
// rules: [{ path: [0], strictType: HEADING_KEYS.h1 }],
// },
// }),

// Testing
PlaywrightPlugin.configure({
enabled: process.env.NODE_ENV !== 'production',
}),
// // Testing
// PlaywrightPlugin.configure({
// enabled: process.env.NODE_ENV !== 'production',
// }),
],
value: value,
value: basicNodesValue,
},
[]
);

const editorView = usePlateEditor({
override: {
components: {
// [HEADING_KEYS.h1]: withProps(HeadingElementView, { variant: 'h1' }),
[ParagraphPlugin.key]: ParagraphElementView,
[BoldPlugin.key]: withProps(PlateViewLeaf, { as: 'strong' }),
},
},
plugins: [ParagraphPlugin, BoldPlugin],
value: [
{
type: 'p',
children: [{ text: 'Playground' }, { bold: true, text: 'paragraph' }],
},
],
});

return (
<Plate editor={editor}>
<EditorContainer className={className}>
<Editor variant="demo" className="pb-[20vh]" spellCheck={false} />
</EditorContainer>
</Plate>
<>
{/* <Plate editor={editor}>
<Button
onClick={() =>
console.log(serializeHtml(editor, { nodes: editor.children }))
}
>
serialize
</Button>
<EditorContainer className={className}>
<Editor variant="demo" className="pb-[20vh]" spellCheck={false} />
</EditorContainer>
</Plate> */}

<PlateView editor={editorView} />
</>
);
}

Expand Down
17 changes: 17 additions & 0 deletions apps/www/src/registry/default/plate-ui/heading-element.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,20 @@ export const HeadingElement = withRef<typeof HeadingElementVariants>(
);
}
);


export const HeadingElementView = withRef<typeof HeadingElementVariants>(
({ children, variant = 'h1', className, ...props }, ref) => {
console.log("🚀 ~ ({children,variant ~ props:", props)
const Tag = variant;

return (
<Tag
ref={ref}
className={headingVariants({ variant, className })}
>
{children}
</Tag>
);
}
);
24 changes: 23 additions & 1 deletion apps/www/src/registry/default/plate-ui/paragraph-element.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React from 'react';

import { cn } from '@udecode/cn';
import { withRef } from '@udecode/plate-common/react';
import { PlateLeaf, withRef } from '@udecode/plate-common/react';

import { PlateElement } from './plate-element';

Expand All @@ -20,3 +20,25 @@ export const ParagraphElement = withRef<typeof PlateElement>(
);
}
);

export const ParagraphElementView = withRef<typeof ParagraphElement>(
({ children, className, ...props }, ref) => {
return (
<div ref={ref} className={cn('m-0 px-0 py-1', className)} {...props}>
{children}
</div>
);
}
);

export const PlateViewLeaf = withRef<typeof PlateLeaf>(
({ children, className, as }, ref) => {
const Component = as ?? 'span';

return (
<Component ref={ref} className={cn(className)}>
{children}
</Component>
);
}
);
121 changes: 121 additions & 0 deletions packages/core/src/react/components/PlateView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* eslint-disable react/no-children-prop */
import React from 'react';

import type { RenderElementFn, RenderLeafFn } from '@udecode/slate-react';
import type {
EditableProps,
RenderElementProps,
RenderLeafProps,
} from 'slate-react/dist/components/editable';

import { isObject } from 'lodash';
import { TDescendant, TElement, TText } from '@udecode/slate';
import { pipeRenderElement, pipeRenderLeaf } from '../utils';
import { PlateEditor } from '../editor';

export type ChildrenProps = {
children: TDescendant[];
};

export type ElementProps = {
element: TElement;
};

export type LeafProps = {
leaf: TText;
};

export type PlateViewContextProps = {
editor: PlateEditor;
renderElement: RenderElementFn;
renderLeaf: RenderLeafFn;
};

export type PlateViewProps = {
renderElement?: EditableProps['renderElement'];
renderLeaf?: EditableProps['renderLeaf'];
editor: PlateEditor;
};

const PlateViewContext = React.createContext<PlateViewContextProps | null>(
null
);

export function usePlateView() {
return React.useContext(PlateViewContext) as PlateViewContextProps;
}

function Element({ element = { children: [], type: '' } }: ElementProps) {
const { editor } = usePlateView();

const renderElement = pipeRenderElement(editor);

return (
<React.Fragment>
{renderElement?.({
attributes: {} as any,
children: <PlateViewContent children={element.children} />,
element,
})}
</React.Fragment>
);
}

function Leaf({ leaf = { text: '' } }: LeafProps) {
const { editor } = usePlateView();

const renderLeaf = pipeRenderLeaf(editor);

return renderLeaf!({
attributes: {} as any,
children: <span>{leaf.text === '' ? '\uFEFF' : leaf.text}</span>,
leaf,
text: leaf,
});
}

function PlateViewContent({ children = [] }: ChildrenProps) {
return (
<React.Fragment>
{children.map((child, i) => {
return isElement(child) ? (
<Element key={i} element={child} />
) : (
<Leaf key={i} leaf={child} />
);
})}
</React.Fragment>
);
}

export function PlateView(props: PlateViewProps) {
const {
renderElement = (props) => <DefaultElement {...props} />,
renderLeaf = (props) => <DefaultLeaf {...props} />,
editor,
} = props;

return (
<PlateViewContext.Provider value={{ editor, renderElement, renderLeaf }}>
<PlateViewContent children={editor.children} />
</PlateViewContext.Provider>
);
}

function DefaultElement({ children }: RenderElementProps) {
return <div>{children}</div>;
}

function DefaultLeaf({ children }: RenderLeafProps) {
return <span>{children}</span>;
}

export function isText(value: any): value is Text {
return isObject(value) && 'text' in value && typeof value.text === 'string';
}

export function isElement(value: any): value is TElement {
return (
isObject(value) && 'children' in value && Array.isArray(value.children)
);
}
1 change: 1 addition & 0 deletions packages/core/src/react/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export * from './PlateContent';
export * from './PlateControllerEffect';
export * from './PlateSlate';
export * from './PlateTest';
export * from './PlateView';
export * from './withHOC';
18 changes: 10 additions & 8 deletions packages/core/src/react/stores/element/useElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { TElement } from '@udecode/slate';

import { useEditorRef } from '../plate';
import { SCOPE_ELEMENT, useElementStore } from './useElementStore';
import { usePlateView } from '../../components';

/**
* Get the element by plugin key. If no element is found in the context, it will
Expand All @@ -10,17 +11,18 @@ import { SCOPE_ELEMENT, useElementStore } from './useElementStore';
export const useElement = <T extends TElement = TElement>(
pluginKey = SCOPE_ELEMENT
): T => {
const editor = useEditorRef();
// const editor = useEditorRef();
const value = useElementStore(pluginKey).get.element();

if (!value) {
editor.api.debug.warn(
`useElement(${pluginKey}) hook must be used inside the node component's context`,
'USE_ELEMENT_CONTEXT'
);
// if (!value) {
// editor.api.debug.warn(
// `useElement(${pluginKey}) hook must be used inside the node component's context`,
// 'USE_ELEMENT_CONTEXT'
// );

return {} as T;
}
// return {} as T;
// }

return value as T;
};

Loading

0 comments on commit aca891d

Please sign in to comment.