1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-13 18:53:59 +02:00

Perf get blocks (#678)

* improve perf of getBlocks and getInlines

* add getBlocksByType, getInlinesByType, fix getMarksByType
This commit is contained in:
Ian Storm Taylor
2017-03-21 23:15:58 -07:00
committed by GitHub
parent f432aac6e2
commit d8812a7f20
5 changed files with 1463 additions and 16 deletions

View File

@@ -0,0 +1,10 @@
import { __clear } from '../../../../lib/utils/memoize'
export default function (state) {
state.document.getBlocks()
}
export function after() {
__clear()
}

View File

@@ -0,0 +1,683 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
key: _cursor_
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'

View File

@@ -0,0 +1,10 @@
import { __clear } from '../../../../lib/utils/memoize'
export default function (state) {
state.document.getInlines()
}
export function after() {
__clear()
}

View File

@@ -0,0 +1,683 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
key: _cursor_
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'This is editable '
- text: 'rich'
marks:
- type: bold
- text: ' text, '
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'much'
marks:
- type: italic
- text: ' better than a '
- text: '<textarea>'
marks:
- type: code
- text: '!'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: 'Since it''s rich text, you can do things like turn a selection of text '
- text: 'bold'
marks:
- type: bold
- text: ', or add a semantically rendered block quote in the middle of the page,
like this:'
- kind: block
type: block-quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'A wise quote.'
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: 'Try it out for yourself!'

View File

@@ -208,22 +208,22 @@ const Node = {
}, },
/** /**
* Get the closest block nodes for each text node in the node. * Get the leaf block descendants of the node.
* *
* @return {List<Node>} * @return {List<Node>}
*/ */
getBlocks() { getBlocks() {
return this return this.nodes.reduce((blocks, node) => {
.getTexts() if (node.kind != 'block') return blocks
.map(text => this.getClosestBlock(text.key)) return node.isLeafBlock()
// Eliminate duplicates by converting to a `Set` first. ? blocks.push(node)
.toOrderedSet() : blocks.concat(node.getBlocks())
.toList() }, new List())
}, },
/** /**
* Get the closest block nodes for each text node in a `range`. * Get the leaf block descendants in a `range`.
* *
* @param {Selection} range * @param {Selection} range
* @return {List<Node>} * @return {List<Node>}
@@ -238,6 +238,21 @@ const Node = {
.toList() .toList()
}, },
/**
* Get all of the leaf blocks that match a `type`.
*
* @param {String} type
* @return {List<Node>}
*/
getBlocksByType(type) {
return this.nodes.reduce((blocks, node) => {
if (node.kind != 'block') return blocks
if (node.isLeafBlock() && node.type == type) return blocks.push(node)
return blocks.concat(node.getBlocksByType(type))
}, new List())
},
/** /**
* Get all of the characters for every text node. * Get all of the characters for every text node.
* *
@@ -620,12 +635,12 @@ const Node = {
*/ */
getInlines() { getInlines() {
return this return this.nodes.reduce((inlines, node) => {
.getTexts() if (node.kind == 'text') return inlines
.map(text => this.getClosestInline(text.key)) return node.isLeafInline()
.filter(exists => exists) ? inlines.push(node)
.toOrderedSet() : inlines.concat(node.getInlines())
.toList() }, new List())
}, },
/** /**
@@ -644,6 +659,21 @@ const Node = {
.toList() .toList()
}, },
/**
* Get all of the leaf inline nodes that match a `type`.
*
* @param {String} type
* @return {List<Node>}
*/
getInlinesByType(type) {
return this.nodes.reduce((inlines, node) => {
if (node.kind == 'text') return inlines
if (node.isLeafInline() && node.type == type) return inlines.push(node)
return inlines.concat(node.getInlinesByType(type))
}, new List())
},
/** /**
* Return a set of all keys in the node. * Return a set of all keys in the node.
* *
@@ -732,8 +762,8 @@ const Node = {
getMarksByType(type) { getMarksByType(type) {
return this.nodes.reduce((marks, node) => { return this.nodes.reduce((marks, node) => {
return node.kind == 'text' return node.kind == 'text'
? node.getMarks().filter(m => m.type == type) ? marks.union(node.getMarks().filter(m => m.type == type))
: node.getMarksByType(type) : marks.union(node.getMarksByType(type))
}, new OrderedSet()) }, new OrderedSet())
}, },
@@ -1109,6 +1139,32 @@ const Node = {
return this.merge({ nodes }) return this.merge({ nodes })
}, },
/**
* Check whether the node is a leaf block.
*
* @return {Boolean}
*/
isLeafBlock() {
return (
this.kind == 'block' &&
this.nodes.every(n => n.kind != 'block')
)
},
/**
* Check whether the node is a leaf inline.
*
* @return {Boolean}
*/
isLeafInline() {
return (
this.kind == 'inline' &&
this.nodes.every(n => n.kind != 'inline')
)
},
/** /**
* Join a children node `first` with another children node `second`. * Join a children node `first` with another children node `second`.
* `first` and `second` will be concatenated in that order. * `first` and `second` will be concatenated in that order.
@@ -1565,6 +1621,7 @@ memoize(Node, [
'getAncestors', 'getAncestors',
'getBlocks', 'getBlocks',
'getBlocksAtRange', 'getBlocksAtRange',
'getBlocksByType',
'getCharacters', 'getCharacters',
'getCharactersAtRange', 'getCharactersAtRange',
'getChild', 'getChild',
@@ -1589,10 +1646,12 @@ memoize(Node, [
'getFurthestOnlyChildAncestor', 'getFurthestOnlyChildAncestor',
'getInlines', 'getInlines',
'getInlinesAtRange', 'getInlinesAtRange',
'getInlinesByType',
'getKeys', 'getKeys',
'getLastText', 'getLastText',
'getMarks', 'getMarks',
'getMarksAtRange', 'getMarksAtRange',
'getMarksByType',
'getNextBlock', 'getNextBlock',
'getNextSibling', 'getNextSibling',
'getNextText', 'getNextText',
@@ -1614,6 +1673,8 @@ memoize(Node, [
'hasNode', 'hasNode',
'hasVoidParent', 'hasVoidParent',
'isInlineSplitAtRange', 'isInlineSplitAtRange',
'isLeafBlock',
'isLeafInline',
'validate', 'validate',
]) ])