From 5585e89ef62308de026024f6f4f2e70264a1681c Mon Sep 17 00:00:00 2001
From: chris <kokosias@yahoo.gr>
Date: Thu, 22 Oct 2020 17:03:48 +0300
Subject: [PATCH] search all views

---
 .../findAndReplace/FindComponent.js           |  6 +-
 .../src/components/findAndReplace/helpers.js  | 59 ++-----------------
 .../findAndReplace/FindAndReplacePlugin.js    |  1 -
 3 files changed, 10 insertions(+), 56 deletions(-)

diff --git a/wax-prosemirror-components/src/components/findAndReplace/FindComponent.js b/wax-prosemirror-components/src/components/findAndReplace/FindComponent.js
index 638dcc677..8a1f1a8ca 100644
--- a/wax-prosemirror-components/src/components/findAndReplace/FindComponent.js
+++ b/wax-prosemirror-components/src/components/findAndReplace/FindComponent.js
@@ -105,7 +105,7 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
 
   const searchDocument = () => {
     setCounterText('0 of 0');
-    console.log('search');
+
     const results = helpers.getMatchesByView(
       view,
       searchValue,
@@ -123,6 +123,10 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
     close();
   };
 
+  if (searchRef.current !== document.activeElement) {
+    console.log('not input');
+  }
+
   const showExpanded = () => {
     expand();
     setPreviousSearcValue(searchValue);
diff --git a/wax-prosemirror-components/src/components/findAndReplace/helpers.js b/wax-prosemirror-components/src/components/findAndReplace/helpers.js
index 46eb1b2a8..7d2b73281 100644
--- a/wax-prosemirror-components/src/components/findAndReplace/helpers.js
+++ b/wax-prosemirror-components/src/components/findAndReplace/helpers.js
@@ -1,47 +1,6 @@
 import { each, eachRight } from 'lodash';
 
 const findMatches = (doc, searchValue) => {
-  const results = [];
-  const mergedTextNodes = [];
-  let index = 0;
-
-  doc.descendants((node, pos) => {
-    if (node.isText) {
-      if (mergedTextNodes[index]) {
-        mergedTextNodes[index] = {
-          text: mergedTextNodes[index].text + node.text,
-          pos: mergedTextNodes[index].pos,
-        };
-      } else {
-        mergedTextNodes[index] = {
-          text: node.text,
-          pos,
-        };
-      }
-    } else {
-      index += 1;
-    }
-  });
-
-  mergedTextNodes.forEach(({ text, pos }) => {
-    const search = RegExp(searchValue, 'gui');
-    let m;
-    // eslint-disable-next-line no-cond-assign
-    while ((m = search.exec(text))) {
-      if (m[0] === '') {
-        break;
-      }
-
-      results.push({
-        from: pos + m.index,
-        to: pos + m.index + m[0].length,
-      });
-    }
-  });
-  return results;
-};
-
-const findMatchesOnMain = (doc, searchValue) => {
   const allNodes = [];
 
   doc.descendants((node, pos) => {
@@ -96,19 +55,11 @@ const findMatchesOnMain = (doc, searchValue) => {
 const getMatchesByView = (views, searchValue, findAndReplacePlugin) => {
   let allResults = 0;
   each(views, (singleView, viewId) => {
-    if (viewId === 'main') {
-      const results = findMatchesOnMain(singleView.state.doc, searchValue);
-      allResults += results.length;
-      findAndReplacePlugin.props.setResults(results);
-      singleView.state.tr.setMeta('search', true);
-      singleView.dispatch(singleView.state.tr);
-    } else {
-      const results = findMatches(singleView.state.doc, searchValue);
-      allResults += results.length;
-      findAndReplacePlugin.props.setResults(results);
-      singleView.state.tr.setMeta('search', true);
-      singleView.dispatch(singleView.state.tr);
-    }
+    const results = findMatches(singleView.state.doc, searchValue);
+    allResults += results.length;
+    findAndReplacePlugin.props.setResults(results);
+    singleView.state.tr.setMeta('search', true);
+    singleView.dispatch(singleView.state.tr);
     return allResults;
   });
   return allResults;
diff --git a/wax-prosemirror-plugins/src/findAndReplace/FindAndReplacePlugin.js b/wax-prosemirror-plugins/src/findAndReplace/FindAndReplacePlugin.js
index 1033f1da6..95b122f03 100644
--- a/wax-prosemirror-plugins/src/findAndReplace/FindAndReplacePlugin.js
+++ b/wax-prosemirror-plugins/src/findAndReplace/FindAndReplacePlugin.js
@@ -13,7 +13,6 @@ export default props => {
         return DecorationSet.empty;
       },
       apply(tr, prev, _, newState) {
-        console.log('again in apply', allResults);
         let createDecoration;
         let decorations;
         let createdDecorations;
-- 
GitLab