Skip to content

Commit

Permalink
feat
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfeng33 committed Dec 5, 2024
1 parent 5b8072d commit ff2db0c
Show file tree
Hide file tree
Showing 23 changed files with 336 additions and 452 deletions.
30 changes: 16 additions & 14 deletions apps/www/src/app/(app)/dev/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use client';

import { withProps } from '@udecode/cn';
import {
BaseBoldPlugin,
Expand All @@ -25,18 +23,18 @@ import { BaseHeadingPlugin, HEADING_KEYS } from '@udecode/plate-heading';
import { serializeHtml } from '@udecode/plate-html';

import { basicNodesValue } from '@/registry/default/example/values/basic-nodes-value';
import { BlockquoteStaticElement } from '@/registry/default/plate-ui/blockquote-element';
import { CodeBlockElementStatic } from '@/registry/default/plate-ui/code-block-element';
import { CodeStaticLeaf } from '@/registry/default/plate-ui/code-leaf';
import { CodeLineStaticElement } from '@/registry/default/plate-ui/code-line-element';
import { CodeSyntaxStaticLeaf } from '@/registry/default/plate-ui/code-syntax-leaf';
import { HeadingStaticElement } from '@/registry/default/plate-ui/heading-element';
import { BlockquoteStaticElement } from '@/registry/default/plate-static-ui/blockquote-element';
import { CodeBlockElementStatic } from '@/registry/default/plate-static-ui/code-block-element';
import { CodeStaticLeaf } from '@/registry/default/plate-static-ui/code-leaf';
import { CodeLineStaticElement } from '@/registry/default/plate-static-ui/code-line-element';
import { CodeSyntaxStaticLeaf } from '@/registry/default/plate-static-ui/code-syntax-leaf';
import { HeadingStaticElement } from '@/registry/default/plate-static-ui/heading-element';
import {
ParagraphStaticElement,
PlateStaticLeaf,
} from '@/registry/default/plate-ui/paragraph-element';
} from '@/registry/default/plate-static-ui/paragraph-element';

export default function DevPage() {
export default async function DevPage() {
const editorStatic = createSlateEditor({
plugins: [
BaseParagraphPlugin,
Expand Down Expand Up @@ -74,16 +72,20 @@ export default function DevPage() {
value: [...basicNodesValue],
});

const html = serializeHtml(editorStatic, {
convertNewLinesToHtmlBr: true,
// eslint-disable-next-line @typescript-eslint/await-thenable
const html = await serializeHtml(editorStatic, {
nodes: editorStatic.children,
stripWhitespace: true,
});
console.log('🚀 ~ DevPage ~ html:', html);

return (
<div className="mx-auto w-1/2">
<h1>Plate Static :</h1>
<PlateStatic editor={editorStatic} />

<br />
<br />
<h1>HTML :</h1>
<div dangerouslySetInnerHTML={{ __html: html }} />
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { StaticElementProps } from '@udecode/plate-core';

export const BlockquoteStaticElement = ({
attributes,
children,
}: StaticElementProps) => {
return (
<blockquote className="my-1 border-l-2 pl-6 italic" {...attributes}>
{children}
</blockquote>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { TCodeBlockElement } from '@udecode/plate-code-block';
import type { StaticElementProps } from '@udecode/plate-core';

import { cn } from '../lib/utils';

export const CodeBlockElementStatic = (
props: StaticElementProps<TCodeBlockElement>
) => {
const { attributes, children, element } = props;

const codeClassName = element?.lang
? `${element.lang} language-${element.lang}`
: '';

return (
<div className={cn('relative py-1', codeClassName)} {...attributes}>
<pre className="overflow-x-auto rounded-md bg-muted px-6 py-8 font-mono text-sm leading-[normal] [tab-size:2]">
<code>{children}</code>
</pre>
</div>
);
};
11 changes: 11 additions & 0 deletions apps/www/src/registry/default/plate-static-ui/code-leaf.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { StaticLeafProps } from '@udecode/plate-core';

export const CodeStaticLeaf = ({ attributes, children }: StaticLeafProps) => {
return (
<span {...attributes}>
<code className="whitespace-pre-wrap rounded-md bg-muted px-[0.3em] py-[0.2em] font-mono text-sm">
{children}
</code>
</span>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { StaticElementProps } from '@udecode/plate-core';

export const CodeLineStaticElement = (props: StaticElementProps) => {
const { children } = props;

return <div>{children}</div>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { StaticLeafProps } from '@udecode/plate-core';

export function CodeSyntaxStaticLeaf({ children, ...props }: StaticLeafProps) {
return <div {...props}>{children}</div>;
}
21 changes: 21 additions & 0 deletions apps/www/src/registry/default/plate-static-ui/heading-element.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { StaticElementProps } from '@udecode/plate-core';

import { headingVariants } from '../plate-ui/heading-element';

interface HeadingElementViewProps extends StaticElementProps {
variant?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
}

export const HeadingStaticElement = ({
attributes,
children,
variant = 'h1',
}: HeadingElementViewProps) => {
const Component = variant as any;

return (
<Component className={headingVariants({ variant })} {...attributes}>
{children}
</Component>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { StaticElementProps, StaticLeafProps } from '@udecode/plate-core';

export const ParagraphStaticElement = ({
attributes,
children,
}: StaticElementProps) => {
return (
<div className="m-0 px-0 py-1" {...attributes}>
{children}
</div>
);
};

export function PlateStaticLeaf({ as, attributes, children }: StaticLeafProps) {
const Leaf = (as ?? 'span') as any;

return <Leaf {...attributes}>{children}</Leaf>;
}
14 changes: 2 additions & 12 deletions apps/www/src/registry/default/plate-ui/blockquote-element.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use client';

import React from 'react';

import type { StaticElementProps } from '@udecode/plate-common';

import { cn, withRef } from '@udecode/cn';
import { cn } from '@udecode/cn';
import { withRef } from '@udecode/react-utils';

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

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

export const BlockquoteStaticElement = (props: StaticElementProps) => {
return (
<blockquote className="my-1 border-l-2 pl-6 italic">
{props.children}
</blockquote>
);
};
82 changes: 32 additions & 50 deletions apps/www/src/registry/default/plate-ui/code-block-element.tsx
Original file line number Diff line number Diff line change
@@ -1,57 +1,39 @@
'use client';

import React from 'react';

import type { TCodeBlockElement } from '@udecode/plate-code-block';
import type { StaticElementProps } from '@udecode/plate-common';
import { cn, withRef } from '@udecode/cn';
import { useCodeBlockElementState } from '@udecode/plate-code-block/react';

import { cn } from '@udecode/cn';
import { CodeBlockCombobox } from './code-block-combobox';
import { PlateElement } from './plate-element';

import './code-block-element.css';

// export const CodeBlockElement = withRef<typeof PlateElement>(
// ({ children, className, ...props }, ref) => {
// const { element } = props;

// const state = useCodeBlockElementState({ element });

// return (
// <PlateElement
// ref={ref}
// className={cn('relative py-1', className)}
// {...props}
// >
// <pre className="overflow-x-auto rounded-md bg-muted px-6 py-8 font-mono text-sm leading-[normal] [tab-size:2]">
// <code>{children}</code>
// </pre>

// {state.syntax && (
// <div
// className="absolute right-2 top-2 z-10 select-none"
// contentEditable={false}
// >
// <CodeBlockCombobox />
// </div>
// )}
// </PlateElement>
// );
// }
// );

export const CodeBlockElementStatic = (
props: StaticElementProps<TCodeBlockElement>
) => {
const { attributes, children, element } = props;

const codeClassName = element?.lang
? `${element.lang} language-${element.lang}`
: '';

return (
<div className={cn('relative py-1', codeClassName)} {...attributes}>
<pre className="overflow-x-auto rounded-md bg-muted px-6 py-8 font-mono text-sm leading-[normal] [tab-size:2]">
<code>{children}</code>
</pre>
</div>
);
};
export const CodeBlockElement = withRef<typeof PlateElement>(
({ children, className, ...props }, ref) => {
const { element } = props;

const state = useCodeBlockElementState({ element });

return (
<PlateElement
ref={ref}
className={cn('relative py-1', className)}
{...props}
>
<pre className="overflow-x-auto rounded-md bg-muted px-6 py-8 font-mono text-sm leading-[normal] [tab-size:2]">
<code>{children}</code>
</pre>

{state.syntax && (
<div
className="absolute right-2 top-2 z-10 select-none"
contentEditable={false}
>
<CodeBlockCombobox />
</div>
)}
</PlateElement>
);
}
);
18 changes: 3 additions & 15 deletions apps/www/src/registry/default/plate-ui/code-leaf.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use client';

import React from 'react';

import type { StaticLeafProps } from '@udecode/plate-common';

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

export const CodeLeaf = withRef<typeof PlateLeaf>(
({ children, className, ...props }, ref) => {
Expand All @@ -24,13 +22,3 @@ export const CodeLeaf = withRef<typeof PlateLeaf>(
);
}
);

export const CodeStaticLeaf = (props: StaticLeafProps) => {
return (
<span>
<code className="whitespace-pre-wrap rounded-md bg-muted px-[0.3em] py-[0.2em] font-mono text-sm">
{props.children}
</code>
</span>
);
};
11 changes: 1 addition & 10 deletions apps/www/src/registry/default/plate-ui/code-line-element.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
'use client';

import React from 'react';

import type { StaticElementProps } from '@udecode/plate-common';

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

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

export const CodeLineElement = withRef<typeof PlateElement>((props, ref) => (
<PlateElement ref={ref} {...props} />
));

export const CodeLineStaticElement = (props: StaticElementProps) => {
const { children } = props;

return <div>{children}</div>;
};
11 changes: 2 additions & 9 deletions apps/www/src/registry/default/plate-ui/code-syntax-leaf.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
'use client';

import React from 'react';

import type { StaticLeafProps } from '@udecode/plate-core';

import { withRef } from '@udecode/cn';
import { useCodeSyntaxLeaf } from '@udecode/plate-code-block/react';
import { PlateLeaf } from '@udecode/plate-common/react';
import { PlateLeaf } from '@udecode/plate-utils/react';
import { withRef } from '@udecode/react-utils';

export const CodeSyntaxLeaf = withRef<typeof PlateLeaf>(
({ children, ...props }, ref) => {
Expand All @@ -21,7 +18,3 @@ export const CodeSyntaxLeaf = withRef<typeof PlateLeaf>(
);
}
);

export function CodeSyntaxStaticLeaf({ children, ...props }: StaticLeafProps) {
return <div {...props}>{children}</div>;
}
24 changes: 3 additions & 21 deletions apps/www/src/registry/default/plate-ui/heading-element.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
'use client';

import React from 'react';

import type { StaticElementProps } from '@udecode/plate-common';

import { withRef, withVariants } from '@udecode/cn';
import { withVariants } from '@udecode/cn';
import { withRef } from '@udecode/react-utils';
import { cva } from 'class-variance-authority';

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

const headingVariants = cva('relative mb-1', {
export const headingVariants = cva('relative mb-1', {
variants: {
variant: {
h1: 'mt-[1.6em] pb-1 font-heading text-4xl font-bold',
Expand Down Expand Up @@ -40,18 +37,3 @@ export const HeadingElement = withRef<typeof HeadingElementVariants>(
);
}
);

interface HeadingElementViewProps extends StaticElementProps {
variant?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
}

export const HeadingStaticElement = ({
children,
variant = 'h1',
}: HeadingElementViewProps) => {
const Component = variant as any;

return (
<Component className={headingVariants({ variant })}>{children}</Component>
);
};
Loading

0 comments on commit ff2db0c

Please sign in to comment.