mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-21 06:31:28 +02:00
Merge pull request #3 from GitbookIO/schema-normalize-optims-node
Quick improvements for filterDescendants
This commit is contained in:
@@ -109,9 +109,7 @@ class Block extends new Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
get text() {
|
get text() {
|
||||||
return this.nodes
|
return this.getText()
|
||||||
.map(node => node.text)
|
|
||||||
.join('')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -83,9 +83,7 @@ class Document extends new Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
get text() {
|
get text() {
|
||||||
return this.nodes
|
return this.getText()
|
||||||
.map(node => node.text)
|
|
||||||
.join('')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -109,9 +109,7 @@ class Inline extends new Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
get text() {
|
get text() {
|
||||||
return this.nodes
|
return this.getText()
|
||||||
.map(node => node.text)
|
|
||||||
.join('')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -29,13 +29,24 @@ const Node = {
|
|||||||
getKeys() {
|
getKeys() {
|
||||||
const keys = []
|
const keys = []
|
||||||
|
|
||||||
this.filterDescendants(desc => {
|
this.forEachDescendant(desc => {
|
||||||
keys.push(desc.key)
|
keys.push(desc.key)
|
||||||
})
|
})
|
||||||
|
|
||||||
return Set(keys)
|
return Set(keys)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the concatenated text `string` of all child nodes.
|
||||||
|
*
|
||||||
|
* @return {String} text
|
||||||
|
*/
|
||||||
|
|
||||||
|
getText() {
|
||||||
|
return this.nodes
|
||||||
|
.reduce((result, node) => result + node.text, '')
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assert that a node has a child by `key` and return it.
|
* Assert that a node has a child by `key` and return it.
|
||||||
*
|
*
|
||||||
@@ -148,18 +159,31 @@ const Node = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
findDescendantDeep(iterator) {
|
findDescendantDeep(iterator) {
|
||||||
let descendantFound = null
|
let found
|
||||||
|
|
||||||
const found = this.nodes.find(node => {
|
this.forEachDescendant(node => {
|
||||||
if (node.kind != 'text') {
|
if (iterator(node)) {
|
||||||
descendantFound = node.findDescendantDeep(iterator)
|
found = node
|
||||||
return descendantFound || iterator(node)
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return iterator(node) ? node : null
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return descendantFound || found
|
return found
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Recursively iterate over all descendant nodes with `iterator`.
|
||||||
|
*
|
||||||
|
* @param {Function} iterator
|
||||||
|
*/
|
||||||
|
|
||||||
|
forEachDescendant(iterator) {
|
||||||
|
return this.nodes.forEach((child, i, nodes) => {
|
||||||
|
if (iterator(child, i, nodes) === false) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (child.kind != 'text') child.forEachDescendant(iterator)
|
||||||
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -170,11 +194,13 @@ const Node = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
filterDescendants(iterator) {
|
filterDescendants(iterator) {
|
||||||
return this.nodes.reduce((matches, child, i, nodes) => {
|
const matches = []
|
||||||
if (iterator(child, i, nodes)) matches = matches.push(child)
|
|
||||||
if (child.kind != 'text') matches = matches.concat(child.filterDescendants(iterator))
|
this.forEachDescendant((child, i, nodes) => {
|
||||||
return matches
|
if (iterator(child, i, nodes)) matches.push(child)
|
||||||
}, Block.createList())
|
})
|
||||||
|
|
||||||
|
return List(matches)
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -924,7 +950,7 @@ const Node = {
|
|||||||
return node.kind == 'text'
|
return node.kind == 'text'
|
||||||
? texts.push(node)
|
? texts.push(node)
|
||||||
: texts.concat(node.getTexts())
|
: texts.concat(node.getTexts())
|
||||||
}, Block.createList())
|
}, List())
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1298,6 +1324,7 @@ const Node = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
memoize(Node, [
|
memoize(Node, [
|
||||||
|
'getText',
|
||||||
'getAncestors',
|
'getAncestors',
|
||||||
'getBlocks',
|
'getBlocks',
|
||||||
'getBlocksAtRange',
|
'getBlocksAtRange',
|
||||||
|
@@ -112,8 +112,7 @@ class Text extends new Record(DEFAULTS) {
|
|||||||
|
|
||||||
get text() {
|
get text() {
|
||||||
return this.characters
|
return this.characters
|
||||||
.map(char => char.text)
|
.reduce((result, char) => result + char.text, '')
|
||||||
.join('')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user