UNPKG

emulate-key-in-browser

Version:
163 lines (162 loc) 6.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.emulateArrow = void 0; var emulate_key_events_1 = require("./emulate-key-events"); var text_input_element_type_1 = require("./text-input-element.type"); var noEventThatCouldGetCanceled = true; exports.emulateArrow = { up: function () { return emulateArrowUp(); }, right: function () { return emulateArrowRight(); }, down: function () { return emulateArrowDown(); }, left: function () { return emulateArrowLeft(); }, shift: { up: function () { return emulateShiftArrowUp(); }, right: function () { return emulateShiftArrowRight(); }, down: function () { return emulateShiftArrowDown(); }, left: function () { return emulateShiftArrowLeft(); }, } }; function emulateArrowUp() { return emulate_key_events_1.emulateKeyEvents('ArrowUp', function (target) { var lineLength = lineLengthOf(target); var selectionStart = text_input_element_type_1.findSelectionStart(target); setCursorOf(target, Math.max(selectionStart - lineLength, 0)); return noEventThatCouldGetCanceled; }); } function setCursorOf(target, position) { target.setSelectionRange(position, position, 'none'); } function lineLengthOf(target, valueLength) { if (valueLength === void 0) { valueLength = valueLengthOf(target); } return target instanceof HTMLTextAreaElement ? target.cols : valueLength; } function valueLengthOf(target) { return target.value && target.value.length || 0; } function emulateArrowRight() { return emulate_key_events_1.emulateKeyEvents('ArrowRight', function (target) { if (target.selectionStart !== target.selectionEnd) { setCursorOf(target, text_input_element_type_1.findSelectionEnd(target)); return noEventThatCouldGetCanceled; } var valueLength = valueLengthOf(target); var selectionEnd = text_input_element_type_1.findSelectionEnd(target); if (selectionEnd < valueLength) { setCursorOf(target, selectionEnd + 1); } return noEventThatCouldGetCanceled; }); } function emulateArrowDown() { return emulate_key_events_1.emulateKeyEvents('ArrowDown', function (target) { var valueLength = valueLengthOf(target); var lineLength = lineLengthOf(target, valueLength); var selectionEnd = text_input_element_type_1.findSelectionEnd(target); setCursorOf(target, Math.min(selectionEnd + lineLength, valueLength)); return noEventThatCouldGetCanceled; }); } function emulateArrowLeft() { return emulate_key_events_1.emulateKeyEvents('ArrowLeft', function (target) { if (target.selectionStart !== target.selectionEnd) { setCursorOf(target, text_input_element_type_1.findSelectionStart(target)); target.selectionEnd = target.selectionStart; return noEventThatCouldGetCanceled; } var selectionStart = text_input_element_type_1.findSelectionStart(target); if (selectionStart > 0) { setCursorOf(target, selectionStart - 1); } return noEventThatCouldGetCanceled; }); } function emulateShiftArrowUp() { return emulate_key_events_1.emulateKeyEvents('ArrowUp', function (target) { var lineLength = lineLengthOf(target); if (isSelectionDirectionOf(target, 'forward')) { reduceSelectionAtSelectionEnd(target, lineLength); } else { extendSelectionAtSelectionStart(target, lineLength); } return noEventThatCouldGetCanceled; }); } function isSelectionDirectionOf(target, direction) { return target.selectionDirection === direction && (target.selectionStart !== target.selectionEnd); } function extendSelectionAtSelectionStart(target, lengthToExtend) { var selectionStart = text_input_element_type_1.findSelectionStart(target); var newSelectionStart = Math.max(selectionStart - lengthToExtend, 0); target.selectionStart = newSelectionStart; if (target.selectionDirection !== 'backward') { target.selectionDirection = 'backward'; } } function reduceSelectionAtSelectionEnd(target, lengthToReduce) { var selectionStart = text_input_element_type_1.findSelectionStart(target); var selectionEnd = text_input_element_type_1.findSelectionEnd(target); var newSelectionEnd = selectionEnd - lengthToReduce; if (selectionStart < newSelectionEnd) { target.selectionEnd = newSelectionEnd; } else { target.selectionEnd = selectionStart; target.selectionDirection = 'none'; } } function emulateShiftArrowRight() { return emulate_key_events_1.emulateKeyEvents('ArrowRight', function (target) { if (isSelectionDirectionOf(target, 'backward')) { reduceSelectionAtSelectionStart(target, 1); } else { extendSelectionAtSelectionEnd(target, 1); } return noEventThatCouldGetCanceled; }); } function extendSelectionAtSelectionEnd(target, lengthToExtend) { var valueLength = valueLengthOf(target); var selectionEnd = text_input_element_type_1.findSelectionEnd(target, valueLength); target.selectionEnd = Math.min(selectionEnd + lengthToExtend, valueLength); if (target.selectionDirection !== 'forward') { target.selectionDirection = 'forward'; } } function reduceSelectionAtSelectionStart(target, lengthToReduce) { var selectionStart = text_input_element_type_1.findSelectionStart(target); var selectionEnd = text_input_element_type_1.findSelectionEnd(target); var newSelectionStart = selectionStart + lengthToReduce; if (newSelectionStart < selectionEnd) { target.selectionStart = newSelectionStart; } else { target.selectionStart = selectionEnd; target.selectionDirection = 'none'; } } function emulateShiftArrowDown() { return emulate_key_events_1.emulateKeyEvents('ArrowDown', function (target) { var lineLength = lineLengthOf(target); if (isSelectionDirectionOf(target, 'backward')) { reduceSelectionAtSelectionStart(target, lineLength); } else { extendSelectionAtSelectionEnd(target, lineLength); } return noEventThatCouldGetCanceled; }); } function emulateShiftArrowLeft() { return emulate_key_events_1.emulateKeyEvents('ArrowLeft', function (target) { if (isSelectionDirectionOf(target, 'forward')) { reduceSelectionAtSelectionEnd(target, 1); } else { extendSelectionAtSelectionStart(target, 1); } return noEventThatCouldGetCanceled; }); }