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, {
useCallback,
useEffect,
} from 'react';
import { debounce } from 'lodash';
import { debounce, each } from 'lodash';
import styled from 'styled-components';
import { grid, th } from '@pubsweet/ui-toolkit';
import { WaxContext } from 'wax-prosemirror-core';
......@@ -78,6 +78,7 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
app,
view: { main },
view,
activeViewId,
} = useContext(WaxContext);
const {
......@@ -88,9 +89,15 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
const [searchValue, setSearchValue] = useState('');
const [counterText, setCounterText] = useState('0 of 0');
const allStates = [];
each(view, (singleView, viewId) => {
allStates.push(singleView.state);
});
const delayedSearch = useCallback(
debounce(() => searchDocument(), 300),
[searchValue],
[searchValue, JSON.stringify(allStates)],
);
const findAndReplacePlugin = app.PmPlugins.get('findAndReplacePlugin');
......@@ -101,18 +108,21 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
useEffect(() => {
delayedSearch();
}, [searchValue, delayedSearch, JSON.stringify(main.state)]);
}, [searchValue, delayedSearch, JSON.stringify(allStates)]);
const searchDocument = () => {
setCounterText('0 of 0');
const results = helpers.getMatchesByView(
view,
searchValue,
findAndReplacePlugin,
);
if (results > 0) {
setCounterText(`1 of ${results}`);
console.log(activeViewId);
if (searchRef.current !== document.activeElement) {
} else {
setCounterText('0 of 0');
const results = helpers.getMatchesByView(
view,
searchValue,
findAndReplacePlugin,
);
if (results > 0) {
setCounterText(`1 of ${results}`);
}
}
};
......@@ -123,10 +133,6 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
close();
};
if (searchRef.current !== document.activeElement) {
console.log('not input');
}
const showExpanded = () => {
expand();
setPreviousSearcValue(searchValue);
......
......@@ -54,14 +54,14 @@ const findMatches = (doc, searchValue) => {
const getMatchesByView = (views, searchValue, findAndReplacePlugin) => {
let allResults = 0;
each(views, (singleView, viewId) => {
eachRight(views, (singleView, viewId) => {
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;
});
views.main.dispatch(views.main.state.tr);
return allResults;
};
......
......@@ -15,8 +15,8 @@ export default props => {
apply(tr, prev, _, newState) {
let createDecoration;
let decorations;
let createdDecorations;
DecorationSet.empty;
let createdDecorations = DecorationSet.empty;
if (allResults.length > 0) {
decorations = allResults.map((result, index) => {
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