1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-20 14:11:35 +02:00

remove the void text content restriction, closes #1504 (#1663)

This commit is contained in:
Ian Storm Taylor
2018-02-21 18:03:41 -08:00
committed by GitHub
parent c5f0626a05
commit 514f3de1be
33 changed files with 82 additions and 150 deletions

View File

@@ -116,28 +116,38 @@ class Leaf extends React.Component {
renderText() {
const { block, node, parent, text, index, leaves } = this.props
// COMPAT: If the text is empty and the only child, we need to render a
// line break when copying and pasting to support expected plain text.
if (text == '' && parent.kind == 'block' && parent.text == '') {
return <span data-slate-zero-width="n">{'\u200B'}</span>
}
// COMPAT: Render text inside void nodes with a zero-width space.
// So the node can contain selection but the text is not visible.
if (parent.isVoid) return <span data-slate-zero-width="z">{'\u200B'}</span>
if (parent.isVoid) {
return <span data-slate-zero-width="z">{'\u200B'}</span>
}
// COMPAT: If this is the last text node in an empty block, render a zero-
// width space that will convert into a line break when copying and pasting
// to support expected plain text.
if (
text === '' &&
parent.kind === 'block' &&
parent.text === '' &&
parent.nodes.size === 1
) {
return <span data-slate-zero-width="n">{'\u200B'}</span>
}
// COMPAT: If the text is empty, it's because it's on the edge of an inline
// void node, so we render a zero-width space so that the selection can be
// inserted next to it still.
if (text == '') return <span data-slate-zero-width="z">{'\u200B'}</span>
if (text === '') {
return <span data-slate-zero-width="z">{'\u200B'}</span>
}
// COMPAT: Browsers will collapse trailing new lines at the end of blocks,
// so we need to add an extra trailing new lines to prevent that.
const lastText = block.getLastText()
const lastChar = text.charAt(text.length - 1)
const isLastText = node == lastText
const isLastLeaf = index == leaves.size - 1
if (isLastText && isLastLeaf && lastChar == '\n') return `${text}\n`
const isLastText = node === lastText
const isLastLeaf = index === leaves.size - 1
if (isLastText && isLastLeaf && lastChar === '\n') return `${text}\n`
// Otherwise, just return the text.
return text