From 327a013bff1f6a82b988a33eac0ecb96a07e711e Mon Sep 17 00:00:00 2001
From: Samuel John <40059405+samueljd@users.noreply.github.com>
Date: Fri, 19 Jan 2024 16:02:53 +0530
Subject: [PATCH] versewise navigation with arrow keys. (#300)
* versewise navigation with arrow keys.
* highlight text, navigate on same verse multi click
* fixed scroll lock issue
* removed highlight text
---
.../EditorPage/TextEditor/Editor.jsx | 6 +-
.../EditorPage/TextEditor/RecursiveBlock.jsx | 59 +++++++++--------
.../TextEditor/utils/IntersectionObserver.js | 31 ++++++---
.../TextEditor/utils/getReferences.js | 64 ++++++++++++++-----
styles/globals.css | 4 ++
5 files changed, 109 insertions(+), 55 deletions(-)
diff --git a/renderer/src/components/EditorPage/TextEditor/Editor.jsx b/renderer/src/components/EditorPage/TextEditor/Editor.jsx
index 03c1ad86e..9423e062c 100644
--- a/renderer/src/components/EditorPage/TextEditor/Editor.jsx
+++ b/renderer/src/components/EditorPage/TextEditor/Editor.jsx
@@ -1,5 +1,5 @@
import React, {
- useContext, useEffect, useLayoutEffect, useRef,
+ useContext, useEffect, useLayoutEffect, useRef,
} from 'react';
import { HtmlPerfEditor } from '@xelah/type-perf-html';
@@ -123,7 +123,7 @@ export default function Editor(props) {
function onReferenceSelected({ chapter, verse }) {
chapter && setChapterNumber(chapter);
verse && setVerseNumber(verse);
- scrollReference(chapter);
+ !scrollLock && scrollReference(chapter, verse);
}
const observer = new IntersectionObserver((entries) => onIntersection({
@@ -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: {
diff --git a/renderer/src/components/EditorPage/TextEditor/RecursiveBlock.jsx b/renderer/src/components/EditorPage/TextEditor/RecursiveBlock.jsx
index 17e7f36f3..ce97f24a2 100644
--- a/renderer/src/components/EditorPage/TextEditor/RecursiveBlock.jsx
+++ b/renderer/src/components/EditorPage/TextEditor/RecursiveBlock.jsx
@@ -3,7 +3,9 @@
import React, { useEffect, useState } from 'react';
import { HtmlPerfEditor } from '@xelah/type-perf-html';
import { getCurrentCursorPosition, pasteTextAtCursorPosition } from '@/util/cursorUtils';
-import { getCurrentVerse, getCurrentChapter } from '@/components/EditorPage/TextEditor/utils/getReferences';
+import {
+ getCurrentVerse, getCurrentChapter,
+} from '@/components/EditorPage/TextEditor/utils/getReferences';
import { on } from 'ws';
const getTarget = ({ content }) => {
@@ -31,6 +33,7 @@ export default function RecursiveBlock({
onReferenceSelected,
setCaretPosition,
setSelectedText,
+ scrollLock,
...props
}) {
const [currentVerse, setCurrentVerse] = useState(null);
@@ -40,27 +43,6 @@ export default function RecursiveBlock({
setCaretPosition(cursorPosition);
};
- const checkReturnKeyPress = (event) => {
- const activeTextArea = document.activeElement;
- if (event.key === 'Enter') {
- if (activeTextArea.children.length > 1) {
- const lineBreak = activeTextArea.children[1]?.outerHTML;
- activeTextArea.children[1].outerHTML = lineBreak.replace(/
/gi, ' ');
- }
- }
- // BACKSPACE DISABLE
- if (event.keyCode === 8) {
- const range = document.getSelection().getRangeAt(0);
- const selectedNode = range.startContainer;
- const prevNode = selectedNode.previousSibling;
- if (prevNode && prevNode.dataset.attsNumber !== currentVerse) {
- event.preventDefault();
- }
- prevNode ? setCurrentVerse(prevNode.dataset.attsNumber) : {};
- }
- updateCursorPosition();
- };
-
function handleSelection() {
let selectedText = '';
if (window.getSelection) {
@@ -72,19 +54,44 @@ 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 { verse } = getCurrentVerse(selectedNode);
const chapter = getCurrentChapter(selectedNode);
onReferenceSelected({ bookId, chapter, verse });
+ // !scrollLock && hightlightRefVerse(chapter, verse);
}
updateCursorPosition();
handleSelection();
};
+ const keyStrokeHandler = (event) => {
+ const activeTextArea = document.activeElement;
+ // Replace line break with space
+ if (event.key === 'Enter') {
+ if (activeTextArea.children.length > 1) {
+ const lineBreak = activeTextArea.children[1]?.outerHTML;
+ activeTextArea.children[1].outerHTML = lineBreak.replace(/
/gi, ' ');
+ }
+ }
+ // Disable backspace if the previous node is not the same verse
+ if (event.keyCode === 8) {
+ const range = document.getSelection().getRangeAt(0);
+ const selectedNode = range.startContainer;
+ const prevNode = selectedNode.previousSibling;
+ if (prevNode && prevNode.dataset.attsNumber !== currentVerse) {
+ event.preventDefault();
+ }
+ prevNode ? setCurrentVerse(prevNode.dataset.attsNumber) : {};
+ }
+ if ([37, 38, 39, 40].includes(event.keyCode)) {
+ checkCurrentVerse();
+ updateCursorPosition();
+ }
+ };
+
function onPasteHandler(event) {
const cursorPosition = getCurrentCursorPosition('editor');
const paste = (event.clipboardData || window.clipboardData).getData('text');
@@ -101,11 +108,11 @@ export default function RecursiveBlock({