Код: Выделить всё
function actionManager(element, newData, oldData, type) {
let newAction = {};
if (type === 'text-change') {
const selectedText = selectedTextBeforeCursor(element);
const elementActions = undoList.filter(undo => undo.element === element);
const previousAction = elementActions[elementActions.length - 1];
const targetText = `${tempArray[tempArray.length - 1]?.selectedText} `;
if (elementActions.length) {
if (selectedText === targetText) {
previousAction.newData = tempArray[tempArray.length - 1]?.newData;
tempArray = [];
} else {
console.log(selectedText);
console.log(targetText);
tempArray.push({ newData, selectedText });
previousAction.newData = newData;
redoList = [];
return;
}
}
newAction = {
newData,
oldData,
type,
element
};
} else if (type === 'element-change') {
newAction = {
newData,
oldData,
type,
element
};
}
undoList.push(newAction);
redoList = [];
}
function selectedTextBeforeCursor(element) {
const selection = window.getSelection();
if (!selection.rangeCount) return '';
const range = selection.getRangeAt(0);
const preCaretRange = range.cloneRange();
preCaretRange.selectNodeContents(element);
preCaretRange.setEnd(range.endContainer, range.endOffset);
return preCaretRange.toString();
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... me-content