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

HtmlReactPlugin serialize error: Cannot resolve a DOM point from Slate point: {"path":[0,0],"offset":1} #3553

Open
carlos19960601 opened this issue Sep 20, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@carlos19960601
Copy link

carlos19960601 commented Sep 20, 2024

Description

I Follow the doc and create a basic editor. Calling htmlReact.serialize causes a error: Cannot resolve a DOM point from Slate point: {"path":[0,0],"offset":1}

import {
  Plate,
  PlateContent,
  usePlateEditor,
} from "@udecode/plate-common/react";

import { HtmlReactPlugin } from "@udecode/plate-html/react";

const PlateEditor = () => {
  const editor = usePlateEditor({
    plugins: [HtmlReactPlugin],
  });
  return (
    <Plate
      editor={editor}
      onChange={({ value }) => {
        const html = editor.api.htmlReact.serialize({
          nodes: editor.children,
        });
        console.log(value, html);
      }}
    >
      <PlateContent placeholder="Type..." />
    </Plate>
  );
};

Reproduction URL

No response

Reproduction steps

just basic editor with HtmlReactPlugin

Plate version

38.0.4

Slate React version

0.110.1

Screenshots

No response

Logs

No response

Browsers

Chrome

Funding

  • You can sponsor this specific effort via a Polar.sh pledge below
  • We receive the pledge once the issue is completed & verified
Fund with Polar
@carlos19960601 carlos19960601 added the bug Something isn't working label Sep 20, 2024
@mrhirapra
Copy link

mrhirapra commented Sep 27, 2024

I'm also facing this problem. I'm find many ways but still not any solution 😞

@FrederikHandberg
Copy link

same here...

@VanThuan76
Copy link

VanThuan76 commented Nov 2, 2024

I have same issue but I saw the issue which can solved #2804

@syedaAnusha
Copy link

Put editor.api.htmlReact.serialize({nodes:editor.children}) in a separate function. as it can't be use inside Plate's onChange handler.
Just a quick code for better understanding!, Hope this will help!

// PLATE COMPONENTS
import { Editor } from "@/components/plate-ui/Editor";
import { FixedToolbar } from "@/components/plate-ui/FixedToolbar";
import { FixedToolbarButtons } from "@/components/plate-ui/fixed-toolbar-buttons";
import { TooltipProvider } from "@/components/plate-ui/Tooltip";
import { useCreateEditor } from "@/hooks/useCreateEditor";
import { useEffect, useRef, useState } from "react";
import { Plate } from "@udecode/plate-common/react";

export const EditorComponent = () => {
  const [content, setContent] = useState<any>("");
  const [_html, setHTML] = useState<any>("");
  const [isClicked, setIsClicked] = useState<boolean>(false);
  const containerRef = useRef(null);
  const editor = useCreateEditor();

  useEffect(() => {
    console.log("CONTENT", content);
  }, [content, _html]);
  const converToHtml = () => {
    const html = editor.api.htmlReact.serialize({
      nodes: content,
      stripDataAttributes: false,
      stripWhitespace: false,
      // preserveClassNames: ["slate-h1", "slate-h2", "slate-h3"],
    });
    setHTML(html);
    console.log("Serialized HTML:", html);
    setIsClicked(true);
    console.log("HTML", _html);
  };

  return (
    <>
      <Plate
        editor={editor}
        onChange={({ value }) => {
          setContent(value);
        }}
      >
        <FixedToolbar className="sticky">
          <FixedToolbarButtons />
        </FixedToolbar>
        <TooltipProvider>
          <Editor ref={containerRef} className="h-auto" />
        </TooltipProvider>
      </Plate>
      <button
        className="border bg-purple-900 text-white text-2xl rounded-md p-2"
        onClick={converToHtml}
      >
        convert to HTML
      </button>

      {isClicked ? (
        <div
          className="w-full border border-black h-[100vh]"
          dangerouslySetInnerHTML={{ __html: _html }}
        />
      ) : null}
    </>
  );
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants