mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-02-23 16:55:23 +01:00
* Experimental release to see if CustomTypes holds up through a publish * Add experimental release script * Fix lint * v0.60.5-alpha.0 * Allow null properties in setNodes * v0.60.6-alpha.0 * Revert null properties on Transforms.setNodes * v0.60.7-alpha.0 * Update examples to use custom Element and Text with discriminated unions * Add documentation for using TypeScript improvements * Be explicit about typescript version in package.json * Force lerna bootstrap to fix build issues on CI and fix a few type examples * Add slate devDependencies with * back * v0.60.7 * Switch to a non prerelease version to fix lerna not linking in root * Add documentation for not using prerelease versions and on how to create experimental releases * Try removing lerna bootstrap and see if it works
95 lines
2.5 KiB
TypeScript
95 lines
2.5 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)) {
|
|
const type = childPath[0] === 0 ? 'title' : 'paragraph'
|
|
|
|
if (SlateElement.isElement(child) && child.type !== type) {
|
|
const newProperties: Partial<SlateElement> = { type }
|
|
Transforms.setNodes(editor, newProperties, { at: childPath })
|
|
}
|
|
}
|
|
}
|
|
|
|
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: SlateElement[] = [
|
|
{
|
|
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
|