Skip to content
Snippets Groups Projects
Commit 82d575ec authored by chris's avatar chris
Browse files

temp

parent 5585e89e
No related branches found
No related tags found
1 merge request!190Find and replace
...@@ -7,7 +7,7 @@ import React, { ...@@ -7,7 +7,7 @@ import React, {
useCallback, useCallback,
useEffect, useEffect,
} from 'react'; } from 'react';
import { debounce } from 'lodash'; import { debounce, each } from 'lodash';
import styled from 'styled-components'; import styled from 'styled-components';
import { grid, th } from '@pubsweet/ui-toolkit'; import { grid, th } from '@pubsweet/ui-toolkit';
import { WaxContext } from 'wax-prosemirror-core'; import { WaxContext } from 'wax-prosemirror-core';
...@@ -78,6 +78,7 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => { ...@@ -78,6 +78,7 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
app, app,
view: { main }, view: { main },
view, view,
activeViewId,
} = useContext(WaxContext); } = useContext(WaxContext);
const { const {
...@@ -88,9 +89,15 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => { ...@@ -88,9 +89,15 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
const [searchValue, setSearchValue] = useState(''); const [searchValue, setSearchValue] = useState('');
const [counterText, setCounterText] = useState('0 of 0'); const [counterText, setCounterText] = useState('0 of 0');
const allStates = [];
each(view, (singleView, viewId) => {
allStates.push(singleView.state);
});
const delayedSearch = useCallback( const delayedSearch = useCallback(
debounce(() => searchDocument(), 300), debounce(() => searchDocument(), 300),
[searchValue], [searchValue, JSON.stringify(allStates)],
); );
const findAndReplacePlugin = app.PmPlugins.get('findAndReplacePlugin'); const findAndReplacePlugin = app.PmPlugins.get('findAndReplacePlugin');
...@@ -101,18 +108,21 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => { ...@@ -101,18 +108,21 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
useEffect(() => { useEffect(() => {
delayedSearch(); delayedSearch();
}, [searchValue, delayedSearch, JSON.stringify(main.state)]); }, [searchValue, delayedSearch, JSON.stringify(allStates)]);
const searchDocument = () => { const searchDocument = () => {
setCounterText('0 of 0'); console.log(activeViewId);
if (searchRef.current !== document.activeElement) {
const results = helpers.getMatchesByView( } else {
view, setCounterText('0 of 0');
searchValue, const results = helpers.getMatchesByView(
findAndReplacePlugin, view,
); searchValue,
if (results > 0) { findAndReplacePlugin,
setCounterText(`1 of ${results}`); );
if (results > 0) {
setCounterText(`1 of ${results}`);
}
} }
}; };
...@@ -123,10 +133,6 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => { ...@@ -123,10 +133,6 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
close(); close();
}; };
if (searchRef.current !== document.activeElement) {
console.log('not input');
}
const showExpanded = () => { const showExpanded = () => {
expand(); expand();
setPreviousSearcValue(searchValue); setPreviousSearcValue(searchValue);
......
...@@ -54,14 +54,14 @@ const findMatches = (doc, searchValue) => { ...@@ -54,14 +54,14 @@ const findMatches = (doc, searchValue) => {
const getMatchesByView = (views, searchValue, findAndReplacePlugin) => { const getMatchesByView = (views, searchValue, findAndReplacePlugin) => {
let allResults = 0; let allResults = 0;
each(views, (singleView, viewId) => { eachRight(views, (singleView, viewId) => {
const results = findMatches(singleView.state.doc, searchValue); const results = findMatches(singleView.state.doc, searchValue);
allResults += results.length; allResults += results.length;
findAndReplacePlugin.props.setResults(results); findAndReplacePlugin.props.setResults(results);
singleView.state.tr.setMeta('search', true); singleView.state.tr.setMeta('search', true);
singleView.dispatch(singleView.state.tr); singleView.dispatch(singleView.state.tr);
return allResults;
}); });
views.main.dispatch(views.main.state.tr);
return allResults; return allResults;
}; };
......
...@@ -15,8 +15,8 @@ export default props => { ...@@ -15,8 +15,8 @@ export default props => {
apply(tr, prev, _, newState) { apply(tr, prev, _, newState) {
let createDecoration; let createDecoration;
let decorations; let decorations;
let createdDecorations; let createdDecorations = DecorationSet.empty;
DecorationSet.empty;
if (allResults.length > 0) { if (allResults.length > 0) {
decorations = allResults.map((result, index) => { decorations = allResults.map((result, index) => {
return Decoration.inline(result.from, result.to, { return Decoration.inline(result.from, result.to, {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment