Skip to content

Commit e7e96b2

Browse files
committed
Refactor undo/redo handling logic and add beforeinput event support
1 parent 047f8a1 commit e7e96b2

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

index.html

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,8 @@
502502
on('keydown', event => {
503503
if (event.defaultPrevented) return
504504
prev = toString()
505-
handleUndoRedo(event)
505+
if (isUndo(event)) doUndo(event)
506+
if (isRedo(event)) doRedo(event)
506507
if (shouldRecord(event) && !recording) {
507508
recordHistory()
508509
recording = true
@@ -536,6 +537,11 @@
536537
recordHistory()
537538
})
538539

540+
on('beforeinput', event => {
541+
if (event.inputType === 'historyUndo') doUndo(event)
542+
if (event.inputType === 'historyRedo') doRedo(event)
543+
})
544+
539545
function save() {
540546
const s = getSelection()
541547
const pos = {start: 0, end: 0, dir: undefined}
@@ -671,27 +677,26 @@
671677
}
672678
}
673679

674-
function handleUndoRedo(event) {
675-
if (isUndo(event)) {
676-
preventDefault(event)
677-
at--
678-
const record = history[at]
679-
if (record) {
680-
element.innerHTML = record.html
681-
restore(record.pos)
682-
}
683-
if (at < 0) at = 0
680+
function doUndo(event) {
681+
preventDefault(event)
682+
at--
683+
const record = history[at]
684+
if (record) {
685+
element.innerHTML = record.html
686+
restore(record.pos)
684687
}
685-
if (isRedo(event)) {
686-
preventDefault(event)
687-
at++
688-
const record = history[at]
689-
if (record) {
690-
element.innerHTML = record.html
691-
restore(record.pos)
692-
}
693-
if (at >= history.length) at--
688+
if (at < 0) at = 0
689+
}
690+
691+
function doRedo(event) {
692+
preventDefault(event)
693+
at++
694+
const record = history[at]
695+
if (record) {
696+
element.innerHTML = record.html
697+
restore(record.pos)
694698
}
699+
if (at >= history.length) at--
695700
}
696701

697702
function recordHistory() {

0 commit comments

Comments
 (0)