From 48e5c6e04b0742fbac8a78e0dbd1d1b5887013ab Mon Sep 17 00:00:00 2001 From: Jinxuan Zhu Date: Tue, 14 Aug 2018 13:19:56 -0400 Subject: [PATCH] Refactor code with immutable-js in Model (#2077) * Ugly Commit * Small Styling issues about immutable-js * Remove an unnecessary List --- packages/slate/src/models/node.js | 52 +++++++++++-------------------- 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/packages/slate/src/models/node.js b/packages/slate/src/models/node.js index c8aac64bf..9794315f4 100644 --- a/packages/slate/src/models/node.js +++ b/packages/slate/src/models/node.js @@ -389,7 +389,7 @@ class Node { getBlocks() { const array = this.getBlocksAsArray() - return new List(array) + return List(array) } /** @@ -417,7 +417,7 @@ class Node { getBlocksAtRange(range) { const array = this.getBlocksAtRangeAsArray(range) // Eliminate duplicates by converting to an `OrderedSet` first. - return new List(new OrderedSet(array)) + return List(OrderedSet(array)) } /** @@ -454,7 +454,7 @@ class Node { getBlocksByType(type) { const array = this.getBlocksByTypeAsArray(type) - return new List(array) + return List(array) } /** @@ -607,16 +607,9 @@ class Node { path = this.resolvePath(path) if (!path) return null - const array = path.toArray() - let descendant = this - - for (const index of array) { - if (!descendant) return null - if (!descendant.nodes) return null - descendant = descendant.nodes.get(index) - } - - return descendant + const deep = path.flatMap(x => ['nodes', x]) + const ret = this.getIn(deep) + return ret } /** @@ -782,7 +775,7 @@ class Node { getInlines() { const array = this.getInlinesAsArray() - const list = new List(array) + const list = List(array) return list } @@ -818,7 +811,7 @@ class Node { getInlinesAtRange(range) { const array = this.getInlinesAtRangeAsArray(range) // Remove duplicates by converting it to an `OrderedSet` first. - const list = new List(new OrderedSet(array)) + const list = List(OrderedSet(array)) return list } @@ -849,7 +842,7 @@ class Node { getInlinesByType(type) { const array = this.getInlinesByTypeAsArray(type) - const list = new List(array) + const list = List(array) return list } @@ -953,8 +946,7 @@ class Node { getMarks() { const array = this.getMarksAsArray() - const set = new Set(array) - return set + return Set(array) } /** @@ -1012,7 +1004,7 @@ class Node { */ getMarksAtRange(range) { - const marks = new Set(this.getOrderedMarksAtRange(range)) + const marks = Set(this.getOrderedMarksAtRange(range)) return marks } @@ -1025,8 +1017,7 @@ class Node { getMarksByType(type) { const array = this.getMarksByTypeAsArray(type) - const set = new Set(array) - return set + return Set(array) } /** @@ -1195,8 +1186,7 @@ class Node { getOrderedMarks() { const array = this.getMarksAsArray() - const set = new OrderedSet(array) - return set + return OrderedSet(array) } /** @@ -1272,8 +1262,7 @@ class Node { getOrderedMarksByType(type) { const array = this.getMarksByTypeAsArray(type) - const set = new OrderedSet(array) - return set + return OrderedSet(array) } /** @@ -1502,8 +1491,7 @@ class Node { getTexts() { const array = this.getTextsAsArray() - const list = new List(array) - return list + return List(array) } /** @@ -1537,9 +1525,7 @@ class Node { range = this.resolveRange(range) if (range.isUnset) return List() const { start, end } = range - const list = new List( - this.getTextsBetweenPositionsAsArray(start.key, end.key) - ) + const list = List(this.getTextsBetweenPositionsAsArray(start.key, end.key)) return list } @@ -1914,7 +1900,7 @@ class Node { removeNode(path) { this.assertDescendant(path) path = this.resolvePath(path) - const deep = path.flatMap(x => List(['nodes', x])) + const deep = path.flatMap(x => ['nodes', x]) const ret = this.deleteIn(deep) return ret } @@ -1954,7 +1940,7 @@ class Node { if (!path.size) return node this.assertNode(path) - const deep = path.flatMap(x => List(['nodes', x])) + const deep = path.flatMap(x => ['nodes', x]) const ret = this.setIn(deep, node) return ret } @@ -2120,7 +2106,7 @@ class Node { logger.deprecate(`0.35.0`, 'The `Node.getKeys` method is deprecated.') const keys = this.getKeysAsArray() - return new Set(keys) + return Set(keys) } getKeysAsArray() {