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

properly search in each view and update accordingly

parent 82d575ec
No related branches found
No related tags found
1 merge request!190Find and replace
...@@ -78,7 +78,6 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => { ...@@ -78,7 +78,6 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
app, app,
view: { main }, view: { main },
view, view,
activeViewId,
} = useContext(WaxContext); } = useContext(WaxContext);
const { const {
...@@ -97,7 +96,7 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => { ...@@ -97,7 +96,7 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
const delayedSearch = useCallback( const delayedSearch = useCallback(
debounce(() => searchDocument(), 300), debounce(() => searchDocument(), 300),
[searchValue, JSON.stringify(allStates)], [searchValue],
); );
const findAndReplacePlugin = app.PmPlugins.get('findAndReplacePlugin'); const findAndReplacePlugin = app.PmPlugins.get('findAndReplacePlugin');
...@@ -111,19 +110,17 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => { ...@@ -111,19 +110,17 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
}, [searchValue, delayedSearch, JSON.stringify(allStates)]); }, [searchValue, delayedSearch, JSON.stringify(allStates)]);
const searchDocument = () => { const searchDocument = () => {
console.log(activeViewId); setCounterText('0 of 0');
if (searchRef.current !== document.activeElement) { let counter = 0;
} else { findAndReplacePlugin.props.setSearchText(searchValue);
setCounterText('0 of 0'); counter = helpers.getMatchesByView(view, searchValue);
const results = helpers.getMatchesByView(
view, if (counter > 0) setCounterText(`1 of ${counter}`);
searchValue,
findAndReplacePlugin, each(view, (singleView, viewId) => {
); singleView.dispatch(singleView.state.tr);
if (results > 0) { });
setCounterText(`1 of ${results}`); // if (searchRef.current === document.activeElement)
}
}
}; };
const closeFind = () => { const closeFind = () => {
......
...@@ -52,16 +52,12 @@ const findMatches = (doc, searchValue) => { ...@@ -52,16 +52,12 @@ const findMatches = (doc, searchValue) => {
return results; return results;
}; };
const getMatchesByView = (views, searchValue, findAndReplacePlugin) => { const getMatchesByView = (views, searchValue) => {
let allResults = 0; let allResults = 0;
eachRight(views, (singleView, viewId) => { each(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);
singleView.state.tr.setMeta('search', true);
singleView.dispatch(singleView.state.tr);
}); });
views.main.dispatch(views.main.state.tr);
return allResults; return allResults;
}; };
......
/* eslint-disable */
import { Plugin, PluginKey } from 'prosemirror-state'; import { Plugin, PluginKey } from 'prosemirror-state';
import { Decoration, DecorationSet } from 'prosemirror-view'; import { Decoration, DecorationSet } from 'prosemirror-view';
import { eachRight } from 'lodash';
const findAndReplacePlugin = new PluginKey('findAndReplacePlugin'); const findAndReplacePlugin = new PluginKey('findAndReplacePlugin');
let allResults = []; let searchText = '';
const findMatches = (doc, searchValue) => {
const allNodes = [];
doc.descendants((node, pos) => {
allNodes.push({ node, pos });
});
eachRight(allNodes, (node, index) => {
if (node.node.type.name === 'footnote') {
allNodes.splice(index + 1, node.node.childCount);
}
});
const results = [];
const mergedTextNodes = [];
let index = 0;
allNodes.forEach((node, i) => {
if (node.node.isText) {
if (mergedTextNodes[index]) {
mergedTextNodes[index] = {
text: mergedTextNodes[index].text + node.node.text,
pos: mergedTextNodes[index].pos,
};
} else {
mergedTextNodes[index] = {
text: node.node.text,
pos: node.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;
};
export default props => { export default props => {
return new Plugin({ return new Plugin({
key: findAndReplacePlugin, key: findAndReplacePlugin,
...@@ -13,12 +66,11 @@ export default props => { ...@@ -13,12 +66,11 @@ export default props => {
return DecorationSet.empty; return DecorationSet.empty;
}, },
apply(tr, prev, _, newState) { apply(tr, prev, _, newState) {
let createDecoration;
let decorations; let decorations;
let createdDecorations = DecorationSet.empty; let createdDecorations = DecorationSet.empty;
const allMatches = findMatches(newState.doc, searchText);
if (allResults.length > 0) { if (allMatches.length > 0) {
decorations = allResults.map((result, index) => { decorations = allMatches.map((result, index) => {
return Decoration.inline(result.from, result.to, { return Decoration.inline(result.from, result.to, {
class: 'search-result', class: 'search-result',
}); });
...@@ -27,6 +79,7 @@ export default props => { ...@@ -27,6 +79,7 @@ export default props => {
} }
return { return {
createdDecorations, createdDecorations,
allMatches,
}; };
}, },
}, },
...@@ -39,6 +92,9 @@ export default props => { ...@@ -39,6 +92,9 @@ export default props => {
setResults: results => { setResults: results => {
allResults = results; allResults = results;
}, },
setSearchText: text => {
searchText = text;
},
}, },
view(editorState) { view(editorState) {
return { return {
......
...@@ -43,9 +43,7 @@ export default ({ node, view }) => { ...@@ -43,9 +43,7 @@ export default ({ node, view }) => {
transformPasted: slice => { transformPasted: slice => {
return transformPasted(slice, noteView); return transformPasted(slice, noteView);
}, },
handleKeyPress: (noteEditorView, from, to, content) => {
updateMainView = false;
},
attributes: { attributes: {
spellcheck: 'false', spellcheck: 'false',
}, },
...@@ -88,11 +86,9 @@ export default ({ node, view }) => { ...@@ -88,11 +86,9 @@ export default ({ node, view }) => {
}); });
// TODO Remove timeout and use state to check if noteView has changed // TODO Remove timeout and use state to check if noteView has changed
if (updateMainView) { setTimeout(() => {
setTimeout(() => { context.updateView({}, noteId);
context.updateView({}, noteId); }, 20);
}, 20);
}
if (!tr.getMeta('fromOutside')) { if (!tr.getMeta('fromOutside')) {
const outerTr = view.state.tr; const outerTr = view.state.tr;
...@@ -105,7 +101,6 @@ export default ({ node, view }) => { ...@@ -105,7 +101,6 @@ export default ({ node, view }) => {
if (outerTr.docChanged) if (outerTr.docChanged)
view.dispatch(outerTr.setMeta('outsideView', 'notes')); view.dispatch(outerTr.setMeta('outsideView', 'notes'));
updateMainView = true;
} }
}; };
......
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