mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-03-06 05:49:47 +01:00
#### Is this adding or improving a _feature_ or fixing a _bug_? Improvement / debt. #### What's the new behavior? This pull request removes the `Change` object as we know it, and folds all of its behaviors into the new `Editor` controller instead, simplifying a lot of the confusion around what is a "change vs. editor" and when to use which. It makes the standard API a **lot** nicer to use I think. --- ###### NEW **The `editor.command` and `editor.query` methods can take functions.** Previously they only accepted a `type` string and would look up the command or query by type. Now, they also accept a custom function. This is helpful for plugin authors, who want to accept a "command option", since it gives users more flexibility to write one-off commands or queries. For example a plugin could be passed either: ```js Hotkey({ hotkey: 'cmd+b', command: 'addBoldMark', }) ``` Or a custom command function: ```js Hotkey({ hotkey: 'cmd+b', command: editor => editor.addBoldMark().moveToEnd() }) ``` ###### BREAKING **The `Change` object has been removed.** The `Change` object as we know it previously has been removed, and all of its behaviors have been folded into the `Editor` controller. This includes the top-level commands and queries methods, as well as methods like `applyOperation` and `normalize`. _All places that used to receive `change` now receive `editor`, which is API equivalent._ **Changes are now flushed to `onChange` asynchronously.** Previously this was done synchronously, which resulted in some strange race conditions in React environments. Now they will always be flushed asynchronously, just like `setState`. **The `render*` and `decorate*` middleware signatures have changed!** Previously the `render*` and `decorate*` middleware was passed `(props, next)`. However now, for consistency with the other middleware they are all passed `(props, editor, next)`. This way, all middleware always receive `editor` and `next` as their final two arguments. **The `normalize*` and `validate*` middleware signatures have changed!** Previously the `normalize*` and `validate*` middleware was passed `(node, next)`. However now, for consistency with the other middleware they are all passed `(node, editor, next)`. This way, all middleware always receive `editor` and `next` as their final two arguments. **The `editor.event` method has been removed.** Previously this is what you'd use when writing tests to simulate events being fired—which were slightly different to other running other middleware. With the simplification to the editor and to the newly-consistent middleware signatures, you can now use `editor.run` directly to simulate events: ```js editor.run('onKeyDown', { key: 'Tab', ... }) ``` ###### DEPRECATED **The `editor.change` method is deprecated.** With the removal of the `Change` object, there's no need anymore to create the small closures with `editor.change()`. Instead you can directly invoke commands on the editor in series, and all of the changes will be emitted asynchronously on the next tick. ```js editor .insertText('word') .moveFocusForward(10) .addMark('bold') ``` **The `applyOperations` method is deprecated.** Instead you can loop a set of operations and apply each one using `applyOperation`. This is to reduce the number of methods exposed on the `Editor` to keep it simpler. **The `change.call` method is deprecated.** Previously this was used to call a one-off function as a change method. Now this behavior is equivalent to calling `editor.command(fn)` instead. --- Fixes: https://github.com/ianstormtaylor/slate/issues/2334 Fixes: https://github.com/ianstormtaylor/slate/issues/2282
310 lines
6.0 KiB
JavaScript
310 lines
6.0 KiB
JavaScript
import Html from 'slate-html-serializer'
|
|
import { Editor, getEventTransfer } from 'slate-react'
|
|
import { Value } from 'slate'
|
|
|
|
import React from 'react'
|
|
import initialValue from './value.json'
|
|
import styled from 'react-emotion'
|
|
|
|
/**
|
|
* Tags to blocks.
|
|
*
|
|
* @type {Object}
|
|
*/
|
|
|
|
const BLOCK_TAGS = {
|
|
p: 'paragraph',
|
|
li: 'list-item',
|
|
ul: 'bulleted-list',
|
|
ol: 'numbered-list',
|
|
blockquote: 'quote',
|
|
pre: 'code',
|
|
h1: 'heading-one',
|
|
h2: 'heading-two',
|
|
h3: 'heading-three',
|
|
h4: 'heading-four',
|
|
h5: 'heading-five',
|
|
h6: 'heading-six',
|
|
}
|
|
|
|
/**
|
|
* Tags to marks.
|
|
*
|
|
* @type {Object}
|
|
*/
|
|
|
|
const MARK_TAGS = {
|
|
strong: 'bold',
|
|
em: 'italic',
|
|
u: 'underline',
|
|
s: 'strikethrough',
|
|
code: 'code',
|
|
}
|
|
|
|
/**
|
|
* A styled image block component.
|
|
*
|
|
* @type {Component}
|
|
*/
|
|
|
|
const Image = styled('img')`
|
|
display: block;
|
|
max-width: 100%;
|
|
max-height: 20em;
|
|
box-shadow: ${props => (props.selected ? '0 0 0 2px blue;' : 'none')};
|
|
`
|
|
|
|
/**
|
|
* Serializer rules.
|
|
*
|
|
* @type {Array}
|
|
*/
|
|
|
|
const RULES = [
|
|
{
|
|
deserialize(el, next) {
|
|
const block = BLOCK_TAGS[el.tagName.toLowerCase()]
|
|
|
|
if (block) {
|
|
return {
|
|
object: 'block',
|
|
type: block,
|
|
nodes: next(el.childNodes),
|
|
}
|
|
}
|
|
},
|
|
},
|
|
{
|
|
deserialize(el, next) {
|
|
const mark = MARK_TAGS[el.tagName.toLowerCase()]
|
|
|
|
if (mark) {
|
|
return {
|
|
object: 'mark',
|
|
type: mark,
|
|
nodes: next(el.childNodes),
|
|
}
|
|
}
|
|
},
|
|
},
|
|
{
|
|
// Special case for code blocks, which need to grab the nested childNodes.
|
|
deserialize(el, next) {
|
|
if (el.tagName.toLowerCase() == 'pre') {
|
|
const code = el.childNodes[0]
|
|
const childNodes =
|
|
code && code.tagName.toLowerCase() == 'code'
|
|
? code.childNodes
|
|
: el.childNodes
|
|
|
|
return {
|
|
object: 'block',
|
|
type: 'code',
|
|
nodes: next(childNodes),
|
|
}
|
|
}
|
|
},
|
|
},
|
|
{
|
|
// Special case for images, to grab their src.
|
|
deserialize(el, next) {
|
|
if (el.tagName.toLowerCase() == 'img') {
|
|
return {
|
|
object: 'block',
|
|
type: 'image',
|
|
nodes: next(el.childNodes),
|
|
data: {
|
|
src: el.getAttribute('src'),
|
|
},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
{
|
|
// Special case for links, to grab their href.
|
|
deserialize(el, next) {
|
|
if (el.tagName.toLowerCase() == 'a') {
|
|
return {
|
|
object: 'inline',
|
|
type: 'link',
|
|
nodes: next(el.childNodes),
|
|
data: {
|
|
href: el.getAttribute('href'),
|
|
},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
]
|
|
|
|
/**
|
|
* Create a new HTML serializer with `RULES`.
|
|
*
|
|
* @type {Html}
|
|
*/
|
|
|
|
const serializer = new Html({ rules: RULES })
|
|
|
|
/**
|
|
* The pasting html example.
|
|
*
|
|
* @type {Component}
|
|
*/
|
|
|
|
class PasteHtml extends React.Component {
|
|
/**
|
|
* Deserialize the raw initial value.
|
|
*
|
|
* @type {Object}
|
|
*/
|
|
|
|
state = {
|
|
value: Value.fromJSON(initialValue),
|
|
}
|
|
|
|
/**
|
|
* The editor's schema.
|
|
*
|
|
* @type {Object}
|
|
*/
|
|
|
|
schema = {
|
|
blocks: {
|
|
image: {
|
|
isVoid: true,
|
|
},
|
|
},
|
|
}
|
|
|
|
/**
|
|
* Render.
|
|
*
|
|
* @return {Component}
|
|
*/
|
|
|
|
render() {
|
|
return (
|
|
<Editor
|
|
placeholder="Paste in some HTML..."
|
|
value={this.state.value}
|
|
schema={this.schema}
|
|
onPaste={this.onPaste}
|
|
onChange={this.onChange}
|
|
renderNode={this.renderNode}
|
|
renderMark={this.renderMark}
|
|
/>
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Render a Slate node.
|
|
*
|
|
* @param {Object} props
|
|
* @return {Element}
|
|
*/
|
|
|
|
renderNode = (props, editor, next) => {
|
|
const { attributes, children, node, isFocused } = props
|
|
|
|
switch (node.type) {
|
|
case 'quote':
|
|
return <blockquote {...attributes}>{children}</blockquote>
|
|
case 'code':
|
|
return (
|
|
<pre>
|
|
<code {...attributes}>{children}</code>
|
|
</pre>
|
|
)
|
|
case 'bulleted-list':
|
|
return <ul {...attributes}>{children}</ul>
|
|
case 'heading-one':
|
|
return <h1 {...attributes}>{children}</h1>
|
|
case 'heading-two':
|
|
return <h2 {...attributes}>{children}</h2>
|
|
case 'heading-three':
|
|
return <h3 {...attributes}>{children}</h3>
|
|
case 'heading-four':
|
|
return <h4 {...attributes}>{children}</h4>
|
|
case 'heading-five':
|
|
return <h5 {...attributes}>{children}</h5>
|
|
case 'heading-six':
|
|
return <h6 {...attributes}>{children}</h6>
|
|
case 'list-item':
|
|
return <li {...attributes}>{children}</li>
|
|
case 'numbered-list':
|
|
return <ol {...attributes}>{children}</ol>
|
|
case 'link': {
|
|
const { data } = node
|
|
const href = data.get('href')
|
|
return (
|
|
<a href={href} {...attributes}>
|
|
{children}
|
|
</a>
|
|
)
|
|
}
|
|
case 'image': {
|
|
const src = node.data.get('src')
|
|
return <Image src={src} selected={isFocused} {...attributes} />
|
|
}
|
|
|
|
default: {
|
|
return next()
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Render a Slate mark.
|
|
*
|
|
* @param {Object} props
|
|
* @return {Element}
|
|
*/
|
|
|
|
renderMark = (props, editor, next) => {
|
|
const { children, mark, attributes } = props
|
|
|
|
switch (mark.type) {
|
|
case 'bold':
|
|
return <strong {...attributes}>{children}</strong>
|
|
case 'code':
|
|
return <code {...attributes}>{children}</code>
|
|
case 'italic':
|
|
return <em {...attributes}>{children}</em>
|
|
case 'underlined':
|
|
return <u {...attributes}>{children}</u>
|
|
default:
|
|
return next()
|
|
}
|
|
}
|
|
|
|
/**
|
|
* On change, save the new value.
|
|
*
|
|
* @param {Editor} editor
|
|
*/
|
|
|
|
onChange = ({ value }) => {
|
|
this.setState({ value })
|
|
}
|
|
|
|
/**
|
|
* On paste, deserialize the HTML and then insert the fragment.
|
|
*
|
|
* @param {Event} event
|
|
* @param {Editor} editor
|
|
*/
|
|
|
|
onPaste = (event, editor, next) => {
|
|
const transfer = getEventTransfer(event)
|
|
if (transfer.type != 'html') return next()
|
|
const { document } = serializer.deserialize(transfer.html)
|
|
editor.insertFragment(document)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Export.
|
|
*/
|
|
|
|
export default PasteHtml
|