Move cursor up and down inside textarea

This commit is contained in:
Peter Stuifzand 2020-10-31 13:36:49 +01:00
parent c92adb32d3
commit dcdf2fd0ab
2 changed files with 42 additions and 6 deletions

View File

@ -1,5 +1,6 @@
function createCursor(start) { function createCursor(start) {
let cursor = start; let cursor = start;
let lastMovement = 0;
return { return {
get() { get() {
@ -35,9 +36,11 @@ function createCursor(start) {
}, },
moveUp(store) { moveUp(store) {
cursor = store.prevCursorPosition(cursor) cursor = store.prevCursorPosition(cursor)
lastMovement = -1
}, },
moveDown(store) { moveDown(store) {
cursor = store.nextCursorPosition(cursor, true) cursor = store.nextCursorPosition(cursor, true)
lastMovement = 1
}, },
remove(store) { remove(store) {
let id = store.currentID(cursor) let id = store.currentID(cursor)
@ -65,6 +68,12 @@ function createCursor(start) {
forwardToNextVisible(store) { forwardToNextVisible(store) {
cursor = store.nextCursorPosition(cursor, false) cursor = store.nextCursorPosition(cursor, false)
}, },
lastDir() {
return lastMovement
},
resetLastMove() {
lastMovement = 0
}
}; };
} }

View File

@ -6,9 +6,26 @@ import textareaAutosizeInit from "./textarea.autosize"
import createCursor from './cursor' import createCursor from './cursor'
import createSelection from './selection' import createSelection from './selection'
import Store from './store' import Store from './store'
import getCaretCoordinates from "../editor/src/caret-position";
textareaAutosizeInit($) 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) { function editor(root, inputData, options) {
let cursor = createCursor() let cursor = createCursor()
let selection = createSelection() let selection = createSelection()
@ -72,7 +89,6 @@ function editor(root, inputData, options) {
}); });
} }
function on(evt, handler) { function on(evt, handler) {
events[evt].push(handler) events[evt].push(handler)
} }
@ -342,10 +358,12 @@ function editor(root, inputData, options) {
function startEditing(rootElement, store, cursor) { function startEditing(rootElement, store, cursor) {
if (editing) return if (editing) return
editing = true editing = true
let elements = $(rootElement).children('div.list-item'); let elements = $(rootElement).children('div.list-item');
let $textarea = $('<textarea rows=1 class="input-line" spellcheck="false">'); let $textarea = $('<textarea rows=1 class="input-line" spellcheck="false">');
$textarea.val(cursor.getCurrent(store).text).trigger('input') $textarea.val(cursor.getCurrent(store).text);
if (cursor.lastDir() > 0) {
$textarea[0].selectionStart = $textarea[0].selectionEnd = 0
}
let currentElement = cursor.getCurrentElement(elements); let currentElement = cursor.getCurrentElement(elements);
currentElement.find('.content').replaceWith($textarea) currentElement.find('.content').replaceWith($textarea)
currentElement.addClass('editor'); currentElement.addClass('editor');
@ -400,12 +418,20 @@ function editor(root, inputData, options) {
}); });
$(root).on('keydown', function (event) { $(root).on('keydown', function (event) {
let tag = event.target.tagName.toLowerCase(); console.log('keydown')
if (tag === 'textarea' && currentEditor[0].value.substring(0, 3) === '```') { let target = event.target;
let dir = 0;
if (event.key === 'ArrowUp') dir = -1
if (event.key === 'ArrowDown') dir = 1
let cursorInfo = textareaCursorInfo(target, dir);
if (!cursorInfo.leaving) {
return true return true
} }
let next = true let next = true
let prevSelected = cursor.save(); let prevSelected = cursor.save()
if (event.key === 'ArrowUp') { if (event.key === 'ArrowUp') {
cursor.moveUp(store); cursor.moveUp(store);
if (event.shiftKey) { if (event.shiftKey) {
@ -477,6 +503,7 @@ function editor(root, inputData, options) {
} else if (selection.hasSelection()) { } else if (selection.hasSelection()) {
stopEditing(root, store, currentEditor); stopEditing(root, store, currentEditor);
} }
cursor.resetLastMove()
} }
if (event.key === 'Enter') { if (event.key === 'Enter') {