Skip to content

Commit 42db5ed

Browse files
committed
Cursor focus
1 parent 6ee4272 commit 42db5ed

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

src/NoteEditor.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,13 @@ function NoteEditor(props) {
131131
// Handle Input
132132
const [bodytxt, setBodyTxt] = useState(note.notebody);
133133
const [title, setTitle] = useState(note.notetitle);
134-
const textareaRef = useRef();
135134
const titleRef = useRef();
136135

137136
const [cusor, setCursor] = useState({
138-
start: textareaRef.selectionStart,
139-
end: textareaRef.selectionEnd,
137+
start: 0,
138+
end: 0,
140139
});
141-
const handleBlurEvent = () => {
142-
textareaRef.current.setSelectionRange(cusor.start, cusor.end);
143-
};
140+
144141
const handleBodyChange = (e) => {
145142
// props.handleNoteEditor(e);
146143
setBodyTxt(e.target.value);
@@ -186,7 +183,6 @@ function NoteEditor(props) {
186183
};
187184

188185
// Button Inputs
189-
190186
const processInput = (eventcode) => {
191187
// obtain the object reference for the textarea>
192188
var txtarea = document.querySelector("textarea");
@@ -195,7 +191,7 @@ function NoteEditor(props) {
195191
// obtain the index of the last selected character
196192
var finish = txtarea.selectionEnd;
197193
//obtain all Text
198-
var allText = txtarea.value;
194+
var allText = bodytxt;
199195
// obtain the selected text
200196
var sel = allText.substring(start, finish);
201197
var img = `![alt text](${sel})`;
@@ -259,9 +255,8 @@ function NoteEditor(props) {
259255
});
260256
}
261257
}
262-
txtarea.blur();
263-
txtarea.focus();
264258
};
259+
265260
// Paste Event
266261
const handlePaste = (e) => {
267262
// Prevent the default action
@@ -309,7 +304,6 @@ function NoteEditor(props) {
309304
};
310305

311306
// Handle Cancel Button
312-
313307
const handleCancel = () => {
314308
if (note.action === "updatenote") {
315309
return document.getElementById(note.noteid).click();
@@ -321,7 +315,6 @@ function NoteEditor(props) {
321315
};
322316

323317
// Handle Save Noye Button Click
324-
325318
const handleSave = (e) => {
326319
note.notetitle = title;
327320
note.notebody = bodytxt;
@@ -480,8 +473,8 @@ function NoteEditor(props) {
480473
value={bodytxt}
481474
id="notebody"
482475
data-action={note.action}
483-
ref={textareaRef}
484-
onBlur={(e) => handleBlurEvent(e)}
476+
selectionEnd={cusor.end}
477+
selectionStart={cusor.start}
485478
style={
486479
toggleState.theme === "vs-light"
487480
? { ...styles.textarea, ...styles.light }

0 commit comments

Comments
 (0)