Skip to content

Commit

Permalink
fixed scroll lock issue
Browse files Browse the repository at this point in the history
  • Loading branch information
samueljd committed Jan 18, 2024
1 parent 3ae86ee commit 83b771e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
4 changes: 2 additions & 2 deletions renderer/src/components/EditorPage/TextEditor/Editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export default function Editor(props) {
function onReferenceSelected({ chapter, verse }) {
chapter && setChapterNumber(chapter);
verse && setVerseNumber(verse);
scrollReference(chapter, verse);
!scrollLock && scrollReference(chapter, verse);
}

const observer = new IntersectionObserver((entries) => onIntersection({
Expand All @@ -147,7 +147,7 @@ export default function Editor(props) {
addSequenceId,
components: {
block: (__props) => RecursiveBlock({
htmlPerf, onHtmlPerf: saveHtmlPerf, sequenceIds, addSequenceId, onReferenceSelected, setCaretPosition, setSelectedText, ...__props,
htmlPerf, onHtmlPerf: saveHtmlPerf, sequenceIds, addSequenceId, onReferenceSelected, setCaretPosition, setSelectedText, scrollLock, ...__props,
}),
},
options: {
Expand Down
10 changes: 6 additions & 4 deletions renderer/src/components/EditorPage/TextEditor/RecursiveBlock.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default function RecursiveBlock({
onReferenceSelected,
setCaretPosition,
setSelectedText,
scrollLock,
...props
}) {
const [currentVerse, setCurrentVerse] = useState(null);
Expand All @@ -53,15 +54,14 @@ export default function RecursiveBlock({
setSelectedText(selectedText);
}
}

const checkCurrentVerse = () => {
if (document.getSelection().rangeCount >= 1 && onReferenceSelected) {
const range = document.getSelection().getRangeAt(0);
const selectedNode = range.startContainer;
const { verse } = getCurrentVerse(selectedNode);
const chapter = getCurrentChapter(selectedNode);
onReferenceSelected({ bookId, chapter, verse });
hightlightRefVerse(chapter, verse);
!scrollLock && hightlightRefVerse(chapter, verse);
}
updateCursorPosition();
handleSelection();
Expand All @@ -86,8 +86,10 @@ export default function RecursiveBlock({
}
prevNode ? setCurrentVerse(prevNode.dataset.attsNumber) : {};
}
checkCurrentVerse();
updateCursorPosition();
if ([37, 38, 39, 40].includes(event.keyCode)) {
checkCurrentVerse();
updateCursorPosition();
}
};

function onPasteHandler(event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@ export const getCurrentVerse = (currentNode) => {
return { verse, verseText };
};

// export const hightlightRefVerse = (chapter, verse) => {
// const refEditor = document.getElementById('ref-editor');
// const verseInView = refEditor.querySelector(`#ch${chapter}v${verse}`);
// const { verseText } = getCurrentVerse(verseInView);

// const range = document.createRange();
// range.setStart(verseInView, 0);
// range.setEnd(verseText, verseText.textContent.length);
// const selection = document.getSelection();
// selection.removeAllRanges();
// selection.addRange(range);
// };
export const removeHighlightFromRefVerse = ({ c, v }) => {
const refEditors = document.getElementsByClassName('ref-editor');
refEditors.length > 0 && Array.prototype.filter.call(refEditors, (refEditor) => {
const prevHighlight = refEditor.querySelector(`#ch${c}v${v}`).nextElementSibling;
const hightlightText = prevHighlight && prevHighlight.innerHTML;
prevHighlight && prevHighlight.replaceWith(hightlightText);
});
};

export const hightlightRefVerse = (() => {
let prevCV;
Expand All @@ -33,12 +29,15 @@ export const hightlightRefVerse = (() => {
if (!(prevCV && prevCV.c !== c)) {
const verseInView = refEditor.querySelector(`#ch${c}v${v}`);
const { verseText } = getCurrentVerse(verseInView);
// highlight verse
const range = document.createRange();
range.setStart(verseInView, 0);
range.setStart(verseText, 0);
range.setEnd(verseText, verseText.textContent.length);
const selection = document.getSelection();
selection.removeAllRanges();
selection.addRange(range);
const newSpan = document.createElement('span');
newSpan.classList.add('bg-primary-50');
range.surroundContents(newSpan);
// remove highlight from previous verse
prevCV && removeHighlightFromRefVerse({ ...prevCV });
}
});
prevCV = { c, v };
Expand Down

0 comments on commit 83b771e

Please sign in to comment.