From dcdf2fd0ab7c571f05b5e309c9dbce0bb41f5c41 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sat, 31 Oct 2020 13:36:49 +0100 Subject: [PATCH] Move cursor up and down inside textarea --- list-editor/cursor.js | 9 +++++++++ list-editor/index.js | 39 +++++++++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/list-editor/cursor.js b/list-editor/cursor.js index 78a2f56..17914ba 100644 --- a/list-editor/cursor.js +++ b/list-editor/cursor.js @@ -1,5 +1,6 @@ function createCursor(start) { let cursor = start; + let lastMovement = 0; return { get() { @@ -35,9 +36,11 @@ function createCursor(start) { }, moveUp(store) { cursor = store.prevCursorPosition(cursor) + lastMovement = -1 }, moveDown(store) { cursor = store.nextCursorPosition(cursor, true) + lastMovement = 1 }, remove(store) { let id = store.currentID(cursor) @@ -65,6 +68,12 @@ function createCursor(start) { forwardToNextVisible(store) { cursor = store.nextCursorPosition(cursor, false) }, + lastDir() { + return lastMovement + }, + resetLastMove() { + lastMovement = 0 + } }; } diff --git a/list-editor/index.js b/list-editor/index.js index 88cfcd1..5425ddc 100644 --- a/list-editor/index.js +++ b/list-editor/index.js @@ -6,9 +6,26 @@ import textareaAutosizeInit from "./textarea.autosize" import createCursor from './cursor' import createSelection from './selection' import Store from './store' +import getCaretCoordinates from "../editor/src/caret-position"; textareaAutosizeInit($) +function textareaCursorInfo(target, dir) { + const minLine = 0; + let pos = getCaretCoordinates(target, target.selectionEnd, {}) + let line = (pos.top - 3) / pos.height; + let maxLine = Math.round(target.clientHeight / pos.height) + let nextLine = line + nextLine += dir + return { + leaving: !(nextLine >= minLine && nextLine < maxLine), + min: minLine, + max: maxLine, + current: line, + next: nextLine + }; +} + function editor(root, inputData, options) { let cursor = createCursor() let selection = createSelection() @@ -72,7 +89,6 @@ function editor(root, inputData, options) { }); } - function on(evt, handler) { events[evt].push(handler) } @@ -342,10 +358,12 @@ function editor(root, inputData, options) { function startEditing(rootElement, store, cursor) { if (editing) return editing = true - let elements = $(rootElement).children('div.list-item'); let $textarea = $('