Unverified Commit c42ee58c authored by Henrik Wenz's avatar Henrik Wenz Committed by GitHub

Update with-slate example (#16959)

## Changelog

- Updated slate.js to version 0.58.4
- Removed unused packages (immutable & slate-plain-serializer)
- Simplified example (we don’t need to demonstrate the multi editor case anymore, since this issue is now handled by slate internally)
- Remove deprecated `KeyUtils`
- Removed deprecated Components

## Related:

- https://github.com/ianstormtaylor/slate/issues/870
parent 99998f0b
import React, { useState } from 'react'
import Plain from 'slate-plain-serializer'
import { Editor } from 'slate-react'
import useCustomKeygen from '../lib/useCustomKeygen'
const NextEditor = ({ slateKey, defaultValue, ...props }) => {
useCustomKeygen(slateKey)
const [state, setState] = useState(() => Plain.deserialize(defaultValue))
return (
<Editor
placeholder="Enter some plain text..."
value={state}
onChange={({ value }) => setState(value)}
/>
)
}
export default NextEditor
import { useRef } from 'react'
import { KeyUtils } from 'slate'
const useCustomKeygen = (uniqueKey) => {
const ref = useRef(null)
if (!ref.current || ref.current !== uniqueKey) {
let n = 0
const keygen = () => `${uniqueKey}${n++}`
KeyUtils.setGenerator(keygen)
ref.current = uniqueKey
}
}
export default useCustomKeygen
...@@ -7,13 +7,12 @@ ...@@ -7,13 +7,12 @@
"start": "next start" "start": "next start"
}, },
"dependencies": { "dependencies": {
"immutable": "^3.8.2",
"next": "latest", "next": "latest",
"react": "^16.7.0", "react": "^16.7.0",
"react-dom": "^16.7.0", "react-dom": "^16.7.0",
"slate": "^0.37.0", "slate": "^0.58.4",
"slate-plain-serializer": "^0.5.27", "slate-history": "0.58.4",
"slate-react": "^0.15.0" "slate-react": "^0.58.4"
}, },
"license": "MIT" "license": "MIT"
} }
import Link from 'next/link' import React, { useState, useMemo } from 'react'
import NextEditor from '../components/NextEditor' import { createEditor } from 'slate'
import { Slate, Editable, withReact } from 'slate-react'
import { withHistory } from 'slate-history'
const IndexPage = (props) => { const IndexPage = () => {
const [value, setValue] = useState(initialValue)
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
return ( return (
<> <Slate editor={editor} value={value} onChange={(value) => setValue(value)}>
<Link href="/multiple"> <Editable placeholder="Enter some plain text..." />
<a>Go to multiple</a> </Slate>
</Link>
<hr />
<NextEditor
slateKey="someUniqueKey"
defaultValue="This is editable plain text, just like a <textarea>!"
/>
</>
) )
} }
const initialValue = [
{
children: [
{ text: 'This is editable plain text, just like a <textarea>!' },
],
},
]
export default IndexPage export default IndexPage
import Link from 'next/link'
import Editor from '../components/NextEditor'
const MultipleEditors = () => {
return (
<>
<Link href="/">
<a>Go to Home</a>
</Link>
<hr />
<Editor slateKey="foo" defaultValue="Foo" />
<hr />
<Editor slateKey="bar" defaultValue="Bar" />
</>
)
}
export default MultipleEditors
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment