1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-04-22 14:21:54 +02:00

Fix rule INLINE_VOID_TEXTS_AROUND

Added an additional test. Was working previously because the
normalization was called several times recursively.
This commit is contained in:
Soreine 2016-10-25 21:11:31 +02:00
parent b7693213a0
commit b2b4a4d565
4 changed files with 60 additions and 9 deletions

View File

@ -1,5 +1,6 @@
import Schema from '../models/schema'
import Text from '../models/text'
import { List } from 'immutable'
/*
This module contains the default schema to normalize documents
@ -157,22 +158,32 @@ const INLINE_VOID_TEXTS_AROUND_RULE = {
const prevNode = index > 0 ? block.nodes.get(index - 1) : null
const nextNode = block.nodes.get(index + 1)
const prev = (!prevNode || isInlineVoid(prevNode))
const next = (!nextNode || isInlineVoid(nextNode.kind))
const prev = !prevNode
const next = (!nextNode || isInlineVoid(nextNode))
if (next || prev) {
accu.push({ next, prev, index })
return accu.push({ next, prev, index })
} else {
return accu
}
}, new List())
return accu
}, [])
return invalids.length ? invalids : null
return !invalids.isEmpty() ? invalids : null
},
normalize: (transform, block, invalids) => {
// Shift for every text node inserted previously
let shift = 0
return invalids.reduce((t, { index, next, prev }) => {
if (prev) t = transform.insertNodeByKey(block.key, index, Text.create(), { normalize: false })
if (next) t = transform.insertNodeByKey(block.key, index + 1, Text.create(), { normalize: false })
if (prev) {
t = t.insertNodeByKey(block.key, shift + index, Text.create(), { normalize: false })
shift = shift + 1
}
if (next) {
t = t.insertNodeByKey(block.key, shift + index + 1, Text.create(), { normalize: false })
shift = shift + 1
}
return t
}, transform)
}

View File

@ -0,0 +1,2 @@
export default {}

View File

@ -0,0 +1,16 @@
nodes:
- kind: block
type: default
nodes:
- kind: inline
isVoid: true
type: image
- kind: text
text: " Hello"
- kind: inline
isVoid: true
type: image
- kind: inline
isVoid: true
type: image

View File

@ -0,0 +1,22 @@
nodes:
- kind: block
type: default
nodes:
- kind: text
text: ""
- kind: inline
isVoid: true
type: image
- kind: text
text: " Hello"
- kind: inline
isVoid: true
type: image
- kind: text
text: ""
- kind: inline
isVoid: true
type: image
- kind: text
text: ""