Slate: setValue(blankSlateValue) would cause focus error

Created on 31 Jan 2020  路  4Comments  路  Source: ianstormtaylor/slate

Do you want to request a _feature_ or report a _bug_?

bug

What's the current behavior?

if you have multiline content in the editor, and you use setValue(blankSlateValue), it causes

Cannot find a descendant at path [1,0] in node: {"children":[{"type":"paragraph","children":[{"text":""}]}],"operations":[],"selection":{"anchor":{"path":[1,0],"offset":6},"focus":{"path":[1,0],"offset":6}},"marks":null,"history":{"undos":[[{"type":"set_selection","properties":null,"newProperties":{"anchor":{"path":[0,0],"offset":0},"focus":{"path":[0,0],"offset":0}}}],[{"type":"insert_text","path":[0,0],"offset":0,"text":"e"},{"type":"insert_text","path":[0,0],"offset":1,"text":"a"},{"type":"insert_text","path":[0,0],"offset":2,"text":"f"},{"type":"insert_text","path":[0,0],"offset":3,"text":"e"},{"type":"insert_text","path":[0,0],"offset":4,"text":"a"},{"type":"insert_text","path":[0,0],"offset":5,"text":"f"},{"type":"insert_text","path":[0,0],"offset":6,"text":"e"},{"type":"insert_text","path":[0,0],"offset":7,"text":"a"}],[{"type":"split_node","path":[0,0],"position":8,"target":null,"properties":{}},{"type":"split_node","path":[0],"position":1,"target":8,"properties":{"type":"paragraph"}}],[{"type":"insert_text","path":[1,0],"offset":0,"text":"f"},{"type":"insert_text","path":[1,0],"offset":1,"text":"e"},{"type":"insert_text","path":[1,0],"offset":2,"text":"a"},{"type":"insert_text","path":[1,0],"offset":3,"text":"f"},{"type":"insert_text","path":[1,0],"offset":4,"text":"a"},{"type":"insert_text","path":[1,0],"offset":5,"text":"e"}]],"redos":[]}}
Slate: 0.57.1
Browser: Chrome
OS: Mac

What's the expected behavior?

focus should be set automatically and not cause this error.

i have a quick hack that does

editor.selection = { anchor: { path: [0,0], offset:0 }, focus: { path: [0,0], offset: 0 } }
before i setValue(blankSlateValue) and it works as expected

bug

Most helpful comment

This is caused by react Fast Refresh. I got rid of the crash by adding // @refresh reset to the top of the file according to https://reactnative.dev/docs/fast-refresh#tips

All 4 comments

Just confirming this bug, I hit it and resetting the anchor and focus avoids the error when resetting to a simple state like:

const initialValue = [
  {
    type: 'paragraph',
    children: [{ text: '' }],
  }
] as Node[]

Hi @hanselke , incidentally the "hack" you mentioned is the solution to this behavior. The selection state for slate is controlled separately from the value state, and if you pass it a value and the selection state is no longer valid, it will throw an exception. Setting the selection to a neutral state is necessary before resetting the value.

This is caused by react Fast Refresh. I got rid of the crash by adding // @refresh reset to the top of the file according to https://reactnative.dev/docs/fast-refresh#tips

This is caused by react Fast Refresh. I got rid of the crash by adding // @refresh reset to the top of the file according to https://reactnative.dev/docs/fast-refresh#tips

Thank you, this is very useful to me

Was this page helpful?
0 / 5 - 0 ratings

Related issues

varoot picture varoot  路  3Comments

bengotow picture bengotow  路  3Comments

chrpeter picture chrpeter  路  3Comments

adrianclay picture adrianclay  路  3Comments

vdms picture vdms  路  3Comments