1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-04-20 05:11:53 +02:00

Layout example fix (#4463)

* Update forced-layout example

Enforce layout to an explicit block index to allow for other block types

* Update site/examples/forced-layout.tsx

* fix layout example to comply with linting rules

Co-authored-by: Lukas Murdock <lukas.murdock@gmail.com>
This commit is contained in:
Dylan Schiemann 2021-08-21 04:32:04 -07:00 committed by GitHub
parent 95c759a19c
commit 3e7ff3bb0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -32,11 +32,25 @@ const withLayout = editor => {
}
for (const [child, childPath] of Node.children(editor, path)) {
const type = childPath[0] === 0 ? 'title' : 'paragraph'
let type: string
const slateIndex = childPath[0]
const enforceType = type => {
if (SlateElement.isElement(child) && child.type !== type) {
const newProperties: Partial<SlateElement> = { type }
Transforms.setNodes(editor, newProperties, { at: childPath })
}
}
if (SlateElement.isElement(child) && child.type !== type) {
const newProperties: Partial<SlateElement> = { type }
Transforms.setNodes(editor, newProperties, { at: childPath })
switch (slateIndex) {
case 0:
type = 'title'
enforceType(type)
break
case 1:
type = 'paragraph'
enforceType(type)
default:
break
}
}
}