Skip to content
Snippets Groups Projects

Connect funcionality

Merged Christos requested to merge connect-funcionality into master
Compare and Show latest version
3 files
+ 115
20
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -7,6 +7,7 @@ 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';
@@ -93,11 +94,13 @@ const Svg = styled.svg.attrs(() => ({
width: 24px;
`;
var lastActiveViewId;
var lastSelection;
const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
const { app, view, activeViewId } = useContext(WaxContext);
const searchRef = useRef(null);
const [lastActiveViewId, setlastActiveViewId] = useState();
const [lastSelection, setLastSelection] = useState();
// const [lastActiveViewId, setlastActiveViewId] = useState();
// const [lastSelection, setLastSelection] = useState();
const [searchValue, setSearchValue] = useState('');
const [counterText, setCounterText] = useState('0 of 0');
@@ -122,9 +125,10 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
useEffect(() => {
if (view[activeViewId].state.selection.from !== 0) {
setLastSelection(view[activeViewId].state.selection);
setlastActiveViewId(activeViewId);
lastSelection = view[activeViewId].state.selection;
lastActiveViewId = activeViewId;
}
delayedSearch();
if (isFirstRun) {
setTimeout(() => {
@@ -168,8 +172,19 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
searchRef.current.focus();
};
const findViewWithMatches = results => {
const notesIds = helpers.getNotesIds(view.main);
if (lastActiveViewId === 'main') {
for (let i = 0; i < notesIds.length; i++) {
if (results[notesIds[i]].length > 0) {
return notesIds[i];
break;
}
}
}
};
const findNext = () => {
view[lastActiveViewId].focus();
const results = helpers.getAllResultsByView(
view,
searchValue,
@@ -178,21 +193,67 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
const resultsFrom = helpers.getResultsFrom(results);
const notesIds = helpers.getNotesIds(view.main);
console.log(notesIds);
/* if no matches are found on focused view */
if (!resultsFrom[lastActiveViewId]) {
view[findViewWithMatches(results)].dispatch(
view[findViewWithMatches(results)].state.tr.setSelection(
new TextSelection(
view[findViewWithMatches(results)].state.tr.doc.resolve(1),
),
),
);
view[findViewWithMatches(results)].focus();
lastActiveViewId = findViewWithMatches(results);
lastSelection = view[lastActiveViewId].state.selection;
}
const found = helpers.getClosestMatch(
lastSelection.from,
resultsFrom[lastActiveViewId],
);
let position = resultsFrom[lastActiveViewId].indexOf(found);
const position = resultsFrom[lastActiveViewId].indexOf(found);
/* User selection lesser than found */
if (lastSelection.from < found) {
helpers.moveToMatch(view, lastActiveViewId, results, position);
}
/* User selection greater than found move to next if not already at the end of results for the view */
if (
lastSelection.from >= found &&
position < resultsFrom[lastActiveViewId].length - 1
)
position += 1;
) {
helpers.moveToMatch(view, lastActiveViewId, results, position + 1);
}
helpers.moveToMatch(view, lastActiveViewId, results, position);
/* Last result of the specific view. Move to next view */
if (
lastSelection.from === found &&
position === resultsFrom[lastActiveViewId].length - 1
) {
/* End of results in notes move to main if results exist */
if (
notesIds.indexOf(lastActiveViewId) === notesIds.length - 1 &&
results.main.length > 0
) {
lastSelection = view[activeViewId].state.selection;
lastActiveViewId = activeViewId;
helpers.moveToMatch(view, 'main', results, 0);
helpers.clearViewSelection(view, lastActiveViewId);
} else {
for (let i = 0; i < notesIds.length; i++) {
if (
results[notesIds[i]].length > 0 &&
notesIds[i] !== lastActiveViewId
) {
helpers.moveToMatch(view, notesIds[i], results, 0);
lastSelection = view[activeViewId].state.selection;
lastActiveViewId = activeViewId;
helpers.clearViewSelection(view, lastActiveViewId);
break;
}
}
}
}
};
const findPrevious = () => {
@@ -205,18 +266,39 @@ const FindComponent = ({ close, expand, setPreviousSearcValue }) => {
const resultsFrom = helpers.getResultsFrom(results);
const notesIds = helpers.getNotesIds(view.main);
console.log(notesIds);
const found = helpers.getClosestMatch(
lastSelection.from,
resultsFrom[lastActiveViewId],
false,
);
let position = resultsFrom[lastActiveViewId].indexOf(found);
const position = resultsFrom[lastActiveViewId].indexOf(found);
if (lastSelection.from <= found) position -= 1;
/* User selection lesser than found */
if (lastSelection.from > found) {
helpers.moveToMatch(view, lastActiveViewId, results, position);
}
if (lastSelection.from <= found && position !== 0) {
helpers.moveToMatch(view, lastActiveViewId, results, position - 1);
}
helpers.moveToMatch(view, lastActiveViewId, results, position);
if (lastSelection.from === found && position === 0) {
if (lastActiveViewId === 'main') {
for (let i = notesIds.length - 1; i >= 0; i--) {
if (
results[notesIds[i]].length > 0 &&
notesIds[i] !== lastActiveViewId
) {
helpers.moveToMatch(
view,
notesIds[i],
results,
results[notesIds[i]].length - 1,
);
}
}
}
}
};
return (