Replace all PlateJS editor contents with another one

870 views Asked by At

I've got pre-saved Nodes[] ([{ children: [{ text: 'bla' }] },{ children: [{ text: 'bla2' }] }]) and I want to replace current editor content with my pre-saved one, but programmatically (not using initialValue). I've tried using transforms, but got an error:

 103 |   useEffect(() => {
  104 |     setTimeout(() => {
> 105 |       Transforms.insertFragment(editorRef, [{ text: '1231231231231231231312313223', children: [{ text: '2222222222222' }] }]);
      |      ^
  106 |     }, 5000);
  107 |   }, []);

TypeError: Invalid value used as weak map key

Also, I've tried to clear editor content using code described here: https://stackoverflow.com/a/68810943/4434887

But got another error: Cannot get the end point in the node at path [] because it has no end text node.

Tried: getPlateActions(id).resetEditor() but it does nothing.

Anyone has a working example of similar functionality?

1

There are 1 answers

0
Oleh Vdovenko On

Okay, I figured it out (partially). To insert a content I needed to set a selection first or it'll isert nothing.

Transforms.select(editorRef, { anchor: { path: [0, 0], offset: 0 }, focus: { path: [0, 0], offset: 0 } });

Then you can insert your content:

const editorRef = usePlateEditorRef(id);
editorRef.insertFragment(savedJobDescription)

Not sure about clearing editor content yet.