From 91c2cb4e9e519cde7027834e22141d20d53638fe Mon Sep 17 00:00:00 2001 From: whyour Date: Sun, 10 Nov 2019 06:58:53 +0800 Subject: [PATCH] Fix when clear search text, all text split by blank (#3072) in search-highlighting example, when search text is empty, no need to split text and addAnnotation --- examples/search-highlighting/index.js | 32 ++++++++++++++------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/examples/search-highlighting/index.js b/examples/search-highlighting/index.js index 1152c02db..7e9e79532 100644 --- a/examples/search-highlighting/index.js +++ b/examples/search-highlighting/index.js @@ -185,23 +185,25 @@ class SearchHighlighting extends React.Component { } }) - for (const [node, path] of document.texts()) { - const { key, text } = node - const parts = text.split(string) - let offset = 0 + if (string) { + for (const [node, path] of document.texts()) { + const { key, text } = node + const parts = text.split(string) + let offset = 0 - parts.forEach((part, i) => { - if (i !== 0) { - editor.addAnnotation({ - key: getHighlightKey(), - type: 'highlight', - anchor: { path, key, offset: offset - string.length }, - focus: { path, key, offset }, - }) - } + parts.forEach((part, i) => { + if (i !== 0) { + editor.addAnnotation({ + key: getHighlightKey(), + type: 'highlight', + anchor: { path, key, offset: offset - string.length }, + focus: { path, key, offset }, + }) + } - offset = offset + part.length + string.length - }) + offset = offset + part.length + string.length + }) + } } }) }