aham
aham

Reputation: 137

Adding a custom block at the end of the editor content in slatejs react

I have created an editor using Slate js in react. I am trying to insert a block at the end of the editor content. I came across a method to insert block at the range. How to specify the range of the document such that my custom blocks gets added at the end of the content but focus stays at the current selection and not at the end of the document.

function insertFile(editor, src, target) {
  editor.insertBlock({
    type: 'file',
    data: { src },
  })
}

My schema looks like this

const schema = {
  blocks: {
    file:{
      isVoid: true
    }
  }
}

Upvotes: 2

Views: 1625

Answers (1)

EthanSnow
EthanSnow

Reputation: 41

Using the Transforms object, it behaves just like you wished.

import { Transforms } from 'slate'

Transforms.insertNodes(
    editor,
    { type: 'paragraph', children: [{ text: 'xxxx' }] },
    { at: [editor.children.length] }
)

To see the document for this: Link1 Link2

Upvotes: 4

Related Questions