mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-31 10:51:44 +02:00
Add schema rule to ensure that inline nodes have a space as content
This commit is contained in:
@@ -51,7 +51,10 @@ class State extends new Record(DEFAULTS) {
|
|||||||
selection = selection.collapseToStartOf(text)
|
selection = selection.collapseToStartOf(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
return new State({ document, selection })
|
const state = new State({ document, selection })
|
||||||
|
return state.transform()
|
||||||
|
.normalize()
|
||||||
|
.apply({ save: false })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -44,6 +44,24 @@ class Text extends new Record(DEFAULTS) {
|
|||||||
return new Text(properties)
|
return new Text(properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new `Text` from a string
|
||||||
|
*
|
||||||
|
* @param {String} content
|
||||||
|
* @return {Text}
|
||||||
|
*/
|
||||||
|
|
||||||
|
static createFromString(content) {
|
||||||
|
return Text.create({
|
||||||
|
characters: Character.createList(
|
||||||
|
content.split('')
|
||||||
|
.map(c => {
|
||||||
|
return { text: c }
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a list of `Texts` from an array.
|
* Create a list of `Texts` from an array.
|
||||||
*
|
*
|
||||||
|
@@ -61,7 +61,7 @@ const MIN_TEXT_RULE = {
|
|||||||
return nodes.size === 0 ? true : null
|
return nodes.size === 0 ? true : null
|
||||||
},
|
},
|
||||||
normalize: (transform, node) => {
|
normalize: (transform, node) => {
|
||||||
return transform.insertTextByKey(node.key, 0, '')
|
return transform.insertNodeByKey(node.key, 0, Text.create())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,12 +86,34 @@ const INLINE_CHILDREN_RULE = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A default schema rule to ensure that inline void nodes are surrounded with text nodes
|
* A default schema rule to ensure that void nodes contain a single space of content.
|
||||||
*
|
*
|
||||||
* @type {Object}
|
* @type {Object}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const INLINE_VOID_TEXT_RULE = {
|
const INLINE_VOID_TEXT_RULE = {
|
||||||
|
match: (object) => {
|
||||||
|
return (object.kind == 'inline' || object.kind == 'block') && object.isVoid
|
||||||
|
},
|
||||||
|
validate: (node) => {
|
||||||
|
return node.text !== ' ' || node.nodes.size !== 1
|
||||||
|
},
|
||||||
|
normalize: (transform, node) => {
|
||||||
|
transform = node.nodes.reduce((t, child) => {
|
||||||
|
return t.removeNodeByKey(child.key)
|
||||||
|
}, transform)
|
||||||
|
|
||||||
|
return transform.insertNodeByKey(node.key, 0, Text.createFromString(' '))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A default schema rule to ensure that inline void nodes are surrounded with text nodes
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const INLINE_VOID_TEXTS_AROUND_RULE = {
|
||||||
match: (object) => {
|
match: (object) => {
|
||||||
return object.kind == 'block'
|
return object.kind == 'block'
|
||||||
},
|
},
|
||||||
@@ -174,6 +196,7 @@ const schema = Schema.create({
|
|||||||
MIN_TEXT_RULE,
|
MIN_TEXT_RULE,
|
||||||
INLINE_CHILDREN_RULE,
|
INLINE_CHILDREN_RULE,
|
||||||
INLINE_VOID_TEXT_RULE,
|
INLINE_VOID_TEXT_RULE,
|
||||||
|
INLINE_VOID_TEXTS_AROUND_RULE,
|
||||||
NO_ADJACENT_TEXT_RULE
|
NO_ADJACENT_TEXT_RULE
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user