From 1c87d33441f2c2c088a6398a08a871391f100067 Mon Sep 17 00:00:00 2001 From: David Himmelstrup Date: Thu, 12 Oct 2017 17:58:42 +0200 Subject: [PATCH 1/2] Change 'lookUpwardForInlineStyle' from O(n^2) to O(n). --- src/model/immutable/EditorState.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/model/immutable/EditorState.js b/src/model/immutable/EditorState.js index 1d833784f7..8069c773f6 100644 --- a/src/model/immutable/EditorState.js +++ b/src/model/immutable/EditorState.js @@ -652,17 +652,13 @@ function lookUpwardForInlineStyle( content: ContentState, fromKey: string, ): DraftInlineStyle { - var previousBlock = content.getBlockBefore(fromKey); - var previousLength; - - while (previousBlock) { - previousLength = previousBlock.getLength(); - if (previousLength) { - return previousBlock.getInlineStyleAt(previousLength - 1); - } - previousBlock = content.getBlockBefore(previousBlock.getKey()); - } + var lastNonEmpty = content.getBlockMap() + .reverse() + .skipUntil((block, k) => k === fromKey && block.getLength()) + .first(); + if (lastNonEmpty) + return lastNonEmpty.getInlineStyleAt(lastNonEmpty.getLength() - 1); return OrderedSet(); } From 9a135472b13afa503c6a1061f277332c7bae9d88 Mon Sep 17 00:00:00 2001 From: David Himmelstrup Date: Thu, 12 Oct 2017 19:32:11 +0200 Subject: [PATCH 2/2] lookUpwardForInlineStyle: Skip the current block to only look at the previous blocks. --- src/model/immutable/EditorState.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/model/immutable/EditorState.js b/src/model/immutable/EditorState.js index 8069c773f6..8f5f547eba 100644 --- a/src/model/immutable/EditorState.js +++ b/src/model/immutable/EditorState.js @@ -654,7 +654,9 @@ function lookUpwardForInlineStyle( ): DraftInlineStyle { var lastNonEmpty = content.getBlockMap() .reverse() - .skipUntil((block, k) => k === fromKey && block.getLength()) + .skipUntil((_, k) => k === fromKey) + .skip(1) + .skipUntil((block, _) => block.getLength()) .first(); if (lastNonEmpty)