Пожалуйста, ознакомьтесь с моим кодом, для демонстрации в реальном времени https://codepen.io/libin-prasanth/pen/dyyjJbG
window.onload = function() {
var selPressed = false;
var length = 0;
var sStart = 0;
var target = document.getElementById('txtOutput');
var clipBoard = '';
// Select click event
document.getElementById('selBtn').addEventListener('click', function(e) {
length = target.selectionStart;
sStart = target.selectionStart;
target.focus();
selPressed = true;
});
// right button
document.getElementById('rtBtn').addEventListener('click', function(e) {
var textLength = target.value.length;
if (!selPressed) return;
length++;
length = (length <= textLength) ? length : textLength;
target.focus();
target.setSelectionRange(sStart, length);
});
// left button
document.getElementById('ltBtn').addEventListener('click', function(e) {
var textLength = target.value.length;
if (!selPressed) return;
length--;
length = (length >= 0) ? length : 0;
target.focus();
target.setSelectionRange(sStart, length);
});
// down button
document.getElementById('dnBtn').addEventListener('click', function(e) {
var textLength = target.value.length;
if (!selPressed) return;
target.focus();
length = length + getCharacterPerLine(target);
length = (length > textLength) ? textLength : length;
target.setSelectionRange(sStart, length);
});
// up button
document.getElementById('upBtn').addEventListener('click', function(e) {
if (!selPressed) return;
target.focus();
length = length - getCharacterPerLine(target);
length = (length <= 0) ? 0 : length;
target.setSelectionRange(sStart, length);
});
// copy event
document.getElementById('cpBtn').addEventListener('click', function(e) {
var text = target.value
if (!selPressed) return;
target.focus();
target.setSelectionRange(sStart, length);
document.execCommand("copy");
clipBoard = target.value.substr(sStart, length);
length = 0;
sStart = 0;
});
// copy cut
document.getElementById('ctBtn').addEventListener('click', function(e) {
var text = target.value
if (!selPressed) return;
target.focus();
target.setSelectionRange(sStart, length);
clipBoard = target.value.substr(sStart, length);
document.execCommand("cut");
length = 0;
sStart = 0;
});
// paste cut
document.getElementById('psBtn').addEventListener('click', function(e) {
if (!selPressed) return;
insertAtCursor(target, clipBoard);
length = 0;
sStart = 0;
});
function insertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA and others
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos) +
myValue +
myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
myField.focus();
}
function getCharacterPerLine(target) {
var w = target.clientWidth;
var fSize = window.getComputedStyle(target, null).getPropertyValue('font-size');
fSize = parseFloat(fSize);
return (w / fSize) * 2;
}
}
* {
margin: 0;
padding: 0;
}
body {
display: flex;
flex-direction: column;
width: 100vw;
height: 100vh;
}
textarea,
#btnWrap button {
display: flex;
flex-grow: 1;
}
textarea::selection {
color: white;
background: black;
}
#btnWrap {
height: 50px;
display: flex;
flex-direction: row;
}
#btnWrap button {
align-items: center;
justify-content: center;
}
<textarea id="txtOutput">
Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book.</textarea>
<div id="btnWrap">
<button id="selBtn">Select</button>
<button id="upBtn">▲</button>
<button id="ltBtn">◀</button>
<button id="rtBtn">▶</button>
<button id="dnBtn">▼</button>
<button id="ctBtn">Cut</button>
<button id="psBtn">Paste</button>
<button id="cpBtn">Copy</button>
</div>