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

Rich Text Support #710

Open
bakikucukcakiroglu opened this issue Nov 19, 2024 · 2 comments
Open

Rich Text Support #710

bakikucukcakiroglu opened this issue Nov 19, 2024 · 2 comments

Comments

@bakikucukcakiroglu
Copy link

bakikucukcakiroglu commented Nov 19, 2024

Hello,

I need rich text features like bold, italic, underline etc. I tried to use puck-rich-text but it is not working with the current version of Puck. Is it possible to add this as a built in component since it is a very fundamental need for every developer?

If there is a way that I'm missing, please let me know.

@thopedam
Copy link

thopedam commented Nov 20, 2024

you can download the puck-rich-text lib, copy the useSelected hook and use something like tiptap

const RichTextEditor = ({ content, id }: RichTextEditorProps) => {
  const { isSelected, onChange } = useSelected(id);
  const editor = useEditor({
    content,
    // [...] more tiptap settings
    onUpdate: ({ editor }) => {
      onChange({ content: editor.getHTML() });
    },
  });

  return (
    <div
      style={{
        cursor: isSelected ? "auto" : "grab",
        pointerEvents: "auto",
      }}
    >
      <EditorContent editor={editor} />
      {editor && <RichTextToolbar editor={editor} id={id} />}
    </div>
  );
};

Add a Toolbar (toolbar override check puck docu)

type Props = {
  editor: Editor;
  id: string;
};

const RichTextToolbar = ({ editor, id }: Props) => {
  return (
    <ActionToolbar id={id}>
      <ActionBarButton
        isActive={editor.isActive("bold")}
        onClick={() => editor.chain().focus().toggleBold().run()}
      >
        <Bold size={16} />
      </ActionBarButton>
      <ActionBarButton
        isActive={editor.isActive("italic")}
        onClick={() => editor.chain().focus().toggleItalic().run()}
      >
        <Italic size={16} />
      </ActionBarButton>
      <ActionBarButton
        isActive={editor.isActive("underline")}
        onClick={() => editor.chain().focus().toggleUnderline().run()}
      >
        <Underline size={16} />
      </ActionBarButton>
    </ActionToolbar>
  );
};

And for the Puck Config you have similar to puck-rich-text

export const RichText: ComponentConfig<RichTextProps> = {
    fields: {
        content: {
             type: "custom",
             label: "Text",
             render: () => <></>,
        },
    }
    render: ({
    content,
    id,
    puck: { isEditing },
  }) => (
  <RichTextBlock
        id={id}
        content={content}
        isEditing={isEditing}
      />
    )
}

@bakikucukcakiroglu
Copy link
Author

Thanks for your insight! I made it work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants