mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-07-31 20:40:19 +02:00
fix fragment pasting, remove <b> and <i> from paste html marks
This commit is contained in:
@@ -10,6 +10,11 @@ p {
|
|||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pre {
|
||||||
|
padding: 10px;
|
||||||
|
background-color: #eee;
|
||||||
|
}
|
||||||
|
|
||||||
img {
|
img {
|
||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
}
|
}
|
||||||
|
@@ -31,9 +31,7 @@ const BLOCKS = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const MARKS = {
|
const MARKS = {
|
||||||
b: 'bold',
|
|
||||||
strong: 'bold',
|
strong: 'bold',
|
||||||
i: 'italic',
|
|
||||||
em: 'italic',
|
em: 'italic',
|
||||||
u: 'underline',
|
u: 'underline',
|
||||||
s: 'strikethrough',
|
s: 'strikethrough',
|
||||||
@@ -48,7 +46,7 @@ const MARKS = {
|
|||||||
|
|
||||||
const RULES = [
|
const RULES = [
|
||||||
{
|
{
|
||||||
deserialize(el) {
|
deserialize(el, next) {
|
||||||
const block = BLOCKS[el.tagName]
|
const block = BLOCKS[el.tagName]
|
||||||
if (!block) return
|
if (!block) return
|
||||||
return {
|
return {
|
||||||
@@ -74,10 +72,14 @@ const RULES = [
|
|||||||
deserialize(el, next) {
|
deserialize(el, next) {
|
||||||
if (el.tagName != 'pre') return
|
if (el.tagName != 'pre') return
|
||||||
const code = el.children[0]
|
const code = el.children[0]
|
||||||
|
const children = code && code.tagName == 'code'
|
||||||
|
? code.children
|
||||||
|
: el.children
|
||||||
|
|
||||||
return {
|
return {
|
||||||
kind: 'block',
|
kind: 'block',
|
||||||
type: 'code-block',
|
type: 'code',
|
||||||
nodes: next(code.children)
|
nodes: next(children)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -149,14 +151,18 @@ class PasteHtml extends React.Component {
|
|||||||
|
|
||||||
renderNode(node) {
|
renderNode(node) {
|
||||||
switch (node.type) {
|
switch (node.type) {
|
||||||
case 'code': return (props) => <pre><code>{props.chidlren}</code></pre>
|
case 'bulleted-list': return (props) => <ul>{props.children}</ul>
|
||||||
case 'quote': return (props) => <blockquote>{props.children}</blockquote>
|
case 'code': return (props) => <pre><code>{props.children}</code></pre>
|
||||||
case 'bulleted-list': return (props) => <ul>{props.chidlren}</ul>
|
|
||||||
case 'heading-one': return (props) => <h1>{props.children}</h1>
|
case 'heading-one': return (props) => <h1>{props.children}</h1>
|
||||||
case 'heading-two': return (props) => <h2>{props.children}</h2>
|
case 'heading-two': return (props) => <h2>{props.children}</h2>
|
||||||
case 'list-item': return (props) => <li>{props.chidlren}</li>
|
case 'heading-three': return (props) => <h3>{props.children}</h3>
|
||||||
|
case 'heading-four': return (props) => <h4>{props.children}</h4>
|
||||||
|
case 'heading-five': return (props) => <h5>{props.children}</h5>
|
||||||
|
case 'heading-six': return (props) => <h6>{props.children}</h6>
|
||||||
|
case 'list-item': return (props) => <li>{props.children}</li>
|
||||||
case 'numbered-list': return (props) => <ol>{props.children}</ol>
|
case 'numbered-list': return (props) => <ol>{props.children}</ol>
|
||||||
case 'paragraph': return (props) => <p>{props.children}</p>
|
case 'paragraph': return (props) => <p>{props.children}</p>
|
||||||
|
case 'quote': return (props) => <blockquote>{props.children}</blockquote>
|
||||||
case 'link': return (props) => {
|
case 'link': return (props) => {
|
||||||
const { data } = props.node
|
const { data } = props.node
|
||||||
const href = data.get('href')
|
const href = data.get('href')
|
||||||
|
@@ -125,7 +125,7 @@ class Editor extends React.Component {
|
|||||||
if (!plugin.renderNode) continue
|
if (!plugin.renderNode) continue
|
||||||
const component = plugin.renderNode(node, this.props.state, this)
|
const component = plugin.renderNode(node, this.props.state, this)
|
||||||
if (component) return component
|
if (component) return component
|
||||||
throw new Error('No renderer found for node.')
|
throw new Error(`No renderer found for node with type "${node.type}".`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,7 +141,7 @@ class Editor extends React.Component {
|
|||||||
if (!plugin.renderMark) continue
|
if (!plugin.renderMark) continue
|
||||||
const style = plugin.renderMark(mark, this.props.state, this)
|
const style = plugin.renderMark(mark, this.props.state, this)
|
||||||
if (style) return style
|
if (style) return style
|
||||||
throw new Error('No renderer found for mark.')
|
throw new Error(`No renderer found for mark with type "${mark.type}".`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -209,11 +209,20 @@ const Transforms = {
|
|||||||
nextChild = block.getNextSibling(startChild)
|
nextChild = block.getNextSibling(startChild)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert the contents of the first block into the block at the cursor.
|
// Get the first and last block of the fragment.
|
||||||
const blocks = fragment.getDeepestBlocks()
|
const blocks = fragment.getDeepestBlocks()
|
||||||
const firstBlock = blocks.first()
|
const firstBlock = blocks.first()
|
||||||
let lastBlock = blocks.last()
|
let lastBlock = blocks.last()
|
||||||
|
|
||||||
|
// If the block is empty, merge in the first block's type and data.
|
||||||
|
if (block.length == 0) {
|
||||||
|
block = block.merge({
|
||||||
|
type: firstBlock.type,
|
||||||
|
data: firstBlock.data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert the first blocks nodes into the starting block.
|
||||||
if (startChild) {
|
if (startChild) {
|
||||||
block = block.insertChildrenAfter(startChild, firstBlock.nodes)
|
block = block.insertChildrenAfter(startChild, firstBlock.nodes)
|
||||||
} else {
|
} else {
|
||||||
|
Reference in New Issue
Block a user