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

move to next view in progress

parent 5f3c1535
No related branches found
No related tags found
No related merge requests found
......@@ -7,7 +7,6 @@ import React, {
useCallback,
useEffect,
} from 'react';
import { TextSelection } from 'prosemirror-state';
import { debounce, each, eachRight } from 'lodash';
import styled from 'styled-components';
import { grid } from '@pubsweet/ui-toolkit';
......@@ -169,118 +168,55 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
searchRef.current.focus();
};
const getAllResultsByView = () => {
const allResults = {};
each(view, (singleView, viewId) => {
if (!allResults[viewId]) {
allResults[viewId] = helpers.findMatches(
singleView.state.doc,
searchValue,
matchCaseSearch,
);
}
});
return allResults;
};
const closest = (selectionFrom, results, greater = true) => {
return results.reduce((a, b) => {
const greaterSmaller = greater ? a > b : a < b;
const aDiff = Math.abs(a - selectionFrom);
const bDiff = Math.abs(b - selectionFrom);
if (aDiff === bDiff) {
return greaterSmaller ? a : b;
}
return bDiff < aDiff ? b : a;
});
};
const findNext = () => {
view[lastActiveViewId].focus();
const results = getAllResultsByView();
const resultsFrom = {};
each(results, (result, viewId) => {
result.forEach(res => {
if (!resultsFrom[viewId]) {
resultsFrom[viewId] = [res.from];
} else {
resultsFrom[viewId].push(res.from);
}
});
});
const found = closest(lastSelection.from, resultsFrom[lastActiveViewId]);
let position = resultsFrom[lastActiveViewId].indexOf(found);
if (lastSelection.from >= found) position += 1;
const selectionFrom = new TextSelection(
view[lastActiveViewId].state.doc.resolve(
results[lastActiveViewId][position].from,
),
const results = helpers.getAllResultsByView(
view,
searchValue,
matchCaseSearch,
);
const resultsFrom = helpers.getResultsFrom(results);
const notesIds = helpers.getNotesIds(view.main);
const selectionTo = new TextSelection(
view[lastActiveViewId].state.doc.resolve(
results[lastActiveViewId][position].to,
),
);
console.log(notesIds);
view[lastActiveViewId].dispatch(
view[lastActiveViewId].state.tr.setSelection(
TextSelection.between(selectionFrom.$anchor, selectionTo.$head),
),
const found = helpers.getClosestMatch(
lastSelection.from,
resultsFrom[lastActiveViewId],
);
let position = resultsFrom[lastActiveViewId].indexOf(found);
view[lastActiveViewId].dispatch(
view[lastActiveViewId].state.tr.scrollIntoView(),
);
if (
lastSelection.from >= found &&
position < resultsFrom[lastActiveViewId].length - 1
)
position += 1;
helpers.moveToMatch(view, lastActiveViewId, results, position);
};
const findPrevious = () => {
view[lastActiveViewId].focus();
const results = getAllResultsByView();
const resultsFrom = {};
each(results, (result, viewId) => {
result.forEach(res => {
if (!resultsFrom[viewId]) {
resultsFrom[viewId] = [res.from];
} else {
resultsFrom[viewId].push(res.from);
}
});
});
const results = helpers.getAllResultsByView(
view,
searchValue,
matchCaseSearch,
);
const resultsFrom = helpers.getResultsFrom(results);
const notesIds = helpers.getNotesIds(view.main);
console.log(notesIds);
const found = closest(
const found = helpers.getClosestMatch(
lastSelection.from,
resultsFrom[lastActiveViewId],
false,
);
let position = resultsFrom[lastActiveViewId].indexOf(found);
if (lastSelection.from <= found) position -= 1;
const selectionFrom = new TextSelection(
view[lastActiveViewId].state.doc.resolve(
results[lastActiveViewId][position].from,
),
);
const selectionTo = new TextSelection(
view[lastActiveViewId].state.doc.resolve(
results[lastActiveViewId][position].to,
),
);
view[lastActiveViewId].dispatch(
view[lastActiveViewId].state.tr.setSelection(
TextSelection.between(selectionFrom.$anchor, selectionTo.$head),
),
);
if (lastSelection.from <= found) position -= 1;
view[lastActiveViewId].dispatch(
view[lastActiveViewId].state.tr.scrollIntoView(),
);
helpers.moveToMatch(view, lastActiveViewId, results, position);
};
return (
......
import { each, eachRight } from 'lodash';
import { DocumentHelpers } from 'wax-prosemirror-utilities';
import { TextSelection } from 'prosemirror-state';
const findMatches = (doc, searchValue, matchCase) => {
const allNodes = [];
......@@ -61,4 +63,92 @@ const getMatchesByView = (views, searchValue, matchCase) => {
return allResults;
};
export default { findMatches, getMatchesByView };
const getAllResultsByView = (view, searchValue, matchCaseSearch) => {
const allResults = {};
each(view, (singleView, viewId) => {
if (!allResults[viewId]) {
allResults[viewId] = findMatches(
singleView.state.doc,
searchValue,
matchCaseSearch,
);
}
});
return allResults;
};
const getNotesIds = main => {
const notesIds = [];
const notes = DocumentHelpers.findChildrenByType(
main.state.doc,
main.state.schema.nodes.footnote,
true,
);
notes.forEach(note => {
notesIds.push(note.node.attrs.id);
});
return notesIds;
};
const getResultsFrom = results => {
const resultsFrom = {};
each(results, (result, viewId) => {
result.forEach(res => {
if (!resultsFrom[viewId]) {
resultsFrom[viewId] = [res.from];
} else {
resultsFrom[viewId].push(res.from);
}
});
});
return resultsFrom;
};
const getClosestMatch = (selectionFrom, results, greater = true) => {
return results.reduce((a, b) => {
const greaterSmaller = greater ? a > b : a < b;
const aDiff = Math.abs(a - selectionFrom);
const bDiff = Math.abs(b - selectionFrom);
if (aDiff === bDiff) {
return greaterSmaller ? a : b;
}
return bDiff < aDiff ? b : a;
});
};
const moveToMatch = (view, lastActiveViewId, results, position) => {
const selectionFrom = new TextSelection(
view[lastActiveViewId].state.doc.resolve(
results[lastActiveViewId][position].from,
),
);
const selectionTo = new TextSelection(
view[lastActiveViewId].state.doc.resolve(
results[lastActiveViewId][position].to,
),
);
view[lastActiveViewId].dispatch(
view[lastActiveViewId].state.tr.setSelection(
TextSelection.between(selectionFrom.$anchor, selectionTo.$head),
),
);
view[lastActiveViewId].dispatch(
view[lastActiveViewId].state.tr.scrollIntoView(),
);
};
export default {
findMatches,
getMatchesByView,
getAllResultsByView,
getNotesIds,
getResultsFrom,
getClosestMatch,
moveToMatch,
};
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