Skip to content

Improvement: Omit initialValue for pre-initialized editor instances #6013

@mkoskim

Description

@mkoskim

Problem

SlateJS forces initialValue for editors, which have been initialized already, e.g.

...
const editor = withReact(createEditor())
editor.children = buffer
...

Omitting unnecessary initial value gives error:

...
return <Slate editor={editor}>...</Slate>
...

Error: [Slate] initialValue is invalid! Expected a list of elements but got: undefined

Solution

In case initialValue is omitted (undefined), use editor.children directly. If it contains valid value (list of elements), go forward.

Workaround

The workaround is to give dummy initial value:

<Slate editor={editor} initialValue={editor.children}>

Context

I'm working with text editor: https://github.com/mkoskim/mawejs/tree/master

What I need, is programmatically attach buffers and onChange callbacks to editor instances, and then render them in a loop - or that is the current plan. The reason is that I have pressure to add more editable buffers, and thus I'd like to bind the editors to the buffers when loading them. Thus I'd like to omit the initialValue properties when rendering editors, they already have the buffer they need to edit.

Furthermore, I need editor instances also for programmatically modify buffers, e.g. for drag-and-drop. Using editor instances to modify their corresponding buffers is almost mandatory to trigger correct re-rendering.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions