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

Add extending for plugins with custom rendering #204

Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"private": true,
"scripts": {
"start": "yarn lerna run start --scope @yoopta/editor --parallel --ignore development",
"start": "yarn lerna run start --scope @yoopta/editor --scope @yoopta/paragraph --scope @yoopta/lists --scope @yoopta/video --scope @yoopta/accordion --scope @yoopta/embed --scope @yoopta/image --scope @yoopta/link --scope @yoopta/video --scope @yoopta/file --parallel --ignore development",
Darginec05 marked this conversation as resolved.
Show resolved Hide resolved
"build": "yarn clean && yarn lerna run build --parallel --ignore development",
"clean": "find ./packages -type d -name dist ! -path './packages/development/*' -exec rm -rf {} +",
"serve": "yarn lerna run dev --scope=development",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/editor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yoopta/editor",
"version": "4.6.2",
"version": "4.6.3-rc.0",
"license": "MIT",
"private": false,
"main": "dist/index.js",
Expand Down
11 changes: 9 additions & 2 deletions packages/core/editor/src/plugins/createYooptaPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type ExtendPluginRender<TKeys extends string> = {
};

export type ExtendPlugin<TKeys extends string, TOptions> = {
renders?: ExtendPluginRender<TKeys>;
renders?: Partial<ExtendPluginRender<TKeys>>;
options?: Partial<PluginOptions<TOptions>>;
};

Expand All @@ -30,8 +30,15 @@ export class YooptaPlugin<TKeys extends string = string, TProps = Descendant, TO
if (renders) {
Object.keys(renders).forEach((elementType) => {
const element = elements[elementType];

if (element && element.render) {
elements[elementType].render = (props) => renders[elementType](props);
const customRenderFn = renders[elementType];

let elementRender = element.render;

element.render = (props) => {
return elementRender({ ...props, extendRender: customRenderFn });
};
}
});
}
Expand Down
6 changes: 5 additions & 1 deletion packages/core/editor/src/plugins/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ export type PluginElementOptions = {
draggable?: boolean;
};

export type PluginElementRenderProps = RenderSlateElementProps & {
export type PluginElementExtendRenderProps = RenderSlateElementProps & {
blockId: string;
HTMLAttributes?: HTMLAttributes<HTMLElement>;
};

export type PluginElementRenderProps = PluginElementExtendRenderProps & {
extendRender?: (props: PluginElementExtendRenderProps) => JSX.Element;
};

export type PluginCustomEditorRenderProps = {
blockId: string;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/core/exports/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yoopta/exports",
"version": "4.6.2",
"version": "4.6.3-rc.0",
"description": "Serialize/deserialize exports in different formats for Yoopta-Editor",
"author": "Darginec05 <[email protected]>",
"homepage": "https://github.com/Darginec05/Editor-Yoopta#readme",
Expand Down
3 changes: 3 additions & 0 deletions packages/development/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const nextConfig = {
typescript: {
ignoreBuildErrors: true,
},
images: {
domains: ['res.cloudinary.com'],
},
};

export default nextConfig;
5 changes: 3 additions & 2 deletions packages/development/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
},
"dependencies": {
"@floating-ui/react": "^0.26.11",
"@radix-ui/react-checkbox": "^1.1.1",
"@yoopta/accordion": "*",
"@yoopta/action-menu-list": "*",
"@yoopta/blockquote": "*",
"@yoopta/callout": "*",
"@yoopta/code": "*",
"@yoopta/editor": "*",
"@yoopta/embed": "*",
"@yoopta/exports": "*",
"@yoopta/file": "*",
"@yoopta/headings": "*",
"@yoopta/image": "*",
Expand All @@ -27,8 +30,6 @@
"@yoopta/table": "*",
"@yoopta/toolbar": "*",
"@yoopta/video": "*",
"@yoopta/exports": "*",
"@yoopta/accordion": "*",
"classnames": "^2.5.1",
"katex": "^0.16.10",
"lucide-react": "^0.378.0",
Expand Down
41 changes: 41 additions & 0 deletions packages/development/src/components/Extends/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as React from 'react';
import cn from 'classnames';
import * as CheckboxPrimitive from '@radix-ui/react-checkbox';
import { CheckIcon } from '@radix-ui/react-icons';

type CheckboxPrimitiveProps = React.ComponentProps<typeof CheckboxPrimitive.Root>;

const CheckboxShadcn = ({ className, ...props }: CheckboxPrimitiveProps) => (
<CheckboxPrimitive.Root
autoFocus={false}
className={cn(
'peer h-4 w-4 shrink-0 rounded-sm border border-[#18181b] shadow focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-[#18181b] data-[state=checked]:text-[#fafafa]',
className,
)}
{...props}
>
<CheckboxPrimitive.Indicator className={cn('flex items-center justify-center text-current')}>
<CheckIcon className="h-4 w-4" />
</CheckboxPrimitive.Indicator>
</CheckboxPrimitive.Root>
);

CheckboxShadcn.displayName = CheckboxPrimitive.Root.displayName;

type Props = React.ComponentProps<typeof CheckboxShadcn>;

export function Checkbox({ name, checked, onCheckedChange: onChange, value, children, attributes, ...props }: Props) {
return (
<div className="flex flex-row items-start space-x-3 mt-4" {...attributes}>
<CheckboxShadcn id={name} checked={checked} value={value} onCheckedChange={onChange} {...props} />
<div className="grid gap-1.5 leading-none">
<span
// htmlFor={name}
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
>
{children}
</span>
</div>
</div>
);
}
36 changes: 36 additions & 0 deletions packages/development/src/components/Extends/Image/Image.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { PluginElementRenderProps, useBlockData } from '@yoopta/editor';
import NextImage, { ImageProps } from 'next/image';

export const YooptaWithNextImage = (props: PluginElementRenderProps) => {
const { children, element, blockId, attributes } = props;
const block = useBlockData(blockId);
const isFill = element.props.fit === 'fill';

const imageProps: Pick<ImageProps, 'fill' | 'style' | 'width' | 'height'> = {
fill: isFill,
style: {
objectFit: isFill ? 'cover' : 'contain',
},
};

if (!imageProps.fill) {
imageProps.width = element.props.sizes.width;
imageProps.height = element.props.sizes.height;
imageProps.style!.width = element.props.sizes.width;
imageProps.style!.height = element.props.sizes.height;
}

return (
<div contentEditable={false} {...attributes}>
<NextImage
draggable={false}
src={element.props.src}
alt={element.props.alt}
priority={block.meta.order === 0}
objectFit={element.props.fit}
{...imageProps}
/>
{children}
</div>
);
};
Loading
Loading