1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-22 08:02:25 +01:00
slate/site/examples/forced-layout.tsx
Ivan Voskoboinyk e0f41514a1
Improve props argument type in Transforms.setNodes() (#4638)
* Fix `setNodes()` props argument type

Because Typescript can know which type of nodes we are modifying thanks to the `T` inferred from `match` function, 
it can also properly narrow down the `props` argument type.

* Fix TS errors in examples

* Add a changeset
2021-11-04 04:43:18 -07:00

111 lines
2.9 KiB
TypeScript

import React, { useState, useCallback, useMemo } from 'react'
import { Slate, Editable, withReact } from 'slate-react'
import {
Transforms,
createEditor,
Node,
Element as SlateElement,
Descendant,
} from 'slate'
import { withHistory } from 'slate-history'
import { ParagraphElement, TitleElement } from './custom-types'
const withLayout = editor => {
const { normalizeNode } = editor
editor.normalizeNode = ([node, path]) => {
if (path.length === 0) {
if (editor.children.length < 1) {
const title: TitleElement = {
type: 'title',
children: [{ text: 'Untitled' }],
}
Transforms.insertNodes(editor, title, { at: path.concat(0) })
}
if (editor.children.length < 2) {
const paragraph: ParagraphElement = {
type: 'paragraph',
children: [{ text: '' }],
}
Transforms.insertNodes(editor, paragraph, { at: path.concat(1) })
}
for (const [child, childPath] of Node.children(editor, path)) {
let type: string
const slateIndex = childPath[0]
const enforceType = type => {
if (SlateElement.isElement(child) && child.type !== type) {
const newProperties: Partial<SlateElement> = { type }
Transforms.setNodes<SlateElement>(editor, newProperties, {
at: childPath,
})
}
}
switch (slateIndex) {
case 0:
type = 'title'
enforceType(type)
break
case 1:
type = 'paragraph'
enforceType(type)
default:
break
}
}
}
return normalizeNode([node, path])
}
return editor
}
const ForcedLayoutExample = () => {
const [value, setValue] = useState<Descendant[]>(initialValue)
const renderElement = useCallback(props => <Element {...props} />, [])
const editor = useMemo(
() => withLayout(withHistory(withReact(createEditor()))),
[]
)
return (
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
<Editable
renderElement={renderElement}
placeholder="Enter a title…"
spellCheck
autoFocus
/>
</Slate>
)
}
const Element = ({ attributes, children, element }) => {
switch (element.type) {
case 'title':
return <h2 {...attributes}>{children}</h2>
case 'paragraph':
return <p {...attributes}>{children}</p>
}
}
const initialValue: Descendant[] = [
{
type: 'title',
children: [{ text: 'Enforce Your Layout!' }],
},
{
type: 'paragraph',
children: [
{
text:
'This example shows how to enforce your layout with domain-specific constraints. This document will always have a title block at the top and at least one paragraph in the body. Try deleting them and see what happens!',
},
],
},
]
export default ForcedLayoutExample