From 2a1da72c709cf4cebd6e941cd33582c15a09ade5 Mon Sep 17 00:00:00 2001 From: Jinxuan Zhu Date: Tue, 30 Jan 2018 21:05:43 -0500 Subject: [PATCH] Faster (but also safe) method about re-find node by path (#1548) --- packages/slate/src/models/node.js | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/packages/slate/src/models/node.js b/packages/slate/src/models/node.js index 05fbe6041..261e731d6 100644 --- a/packages/slate/src/models/node.js +++ b/packages/slate/src/models/node.js @@ -1351,6 +1351,41 @@ class Node { return path } + /** + * Refind the path of node if path is changed. + * + * @param {Array} path + * @param {String} key + * @return {Array} + */ + + refindPath(path, key) { + const node = this.getDescendantAtPath(path) + if (node && node.key === key) { + return path + } + + return this.getPath(key) + } + + /** + * + * Refind the node with the same node.key after change. + * + * @param {Array} path + * @param {String} key + * @return {Node|Void} + */ + + refindNode(path, key) { + const node = this.getDescendantAtPath(path) + if (node && node.key === key) { + return node + } + + return this.getDescendant(key) + } + /** * Get the placeholder for the node from a `schema`. *