MCPcopy Create free account
hub / github.com/breck7/scroll / makeChangeFromHistory

Function makeChangeFromHistory

external/.scrollLibs.js:7188–7272  ·  view source on GitHub ↗
(doc, type, allowSelectionOnly)

Source from the content-addressed store, hash-verified

7186
7187 // Revert a change stored in a document's history.
7188 function makeChangeFromHistory(doc, type, allowSelectionOnly) {
7189 var suppress = doc.cm && doc.cm.state.suppressEdits
7190 if (suppress && !allowSelectionOnly) {
7191 return
7192 }
7193
7194 var hist = doc.history,
7195 event,
7196 selAfter = doc.sel
7197 var source = type == "undo" ? hist.done : hist.undone,
7198 dest = type == "undo" ? hist.undone : hist.done
7199
7200 // Verify that there is a useable event (so that ctrl-z won't
7201 // needlessly clear selection events)
7202 var i = 0
7203 for (; i < source.length; i++) {
7204 event = source[i]
7205 if (allowSelectionOnly ? event.ranges && !event.equals(doc.sel) : !event.ranges) {
7206 break
7207 }
7208 }
7209 if (i == source.length) {
7210 return
7211 }
7212 hist.lastOrigin = hist.lastSelOrigin = null
7213
7214 for (;;) {
7215 event = source.pop()
7216 if (event.ranges) {
7217 pushSelectionToHistory(event, dest)
7218 if (allowSelectionOnly && !event.equals(doc.sel)) {
7219 setSelection(doc, event, { clearRedo: false })
7220 return
7221 }
7222 selAfter = event
7223 } else if (suppress) {
7224 source.push(event)
7225 return
7226 } else {
7227 break
7228 }
7229 }
7230
7231 // Build up a reverse change object to add to the opposite history
7232 // stack (redo when undoing, and vice versa).
7233 var antiChanges = []
7234 pushSelectionToHistory(selAfter, dest)
7235 dest.push({ changes: antiChanges, generation: hist.generation })
7236 hist.generation = event.generation || ++hist.maxGeneration
7237
7238 var filter = hasHandler(doc, "beforeChange") || (doc.cm && hasHandler(doc.cm, "beforeChange"))
7239
7240 var loop = function (i) {
7241 var change = event.changes[i]
7242 change.origin = type
7243 if (filter && !filterChange(doc, change, false)) {
7244 source.length = 0
7245 return {}

Callers 1

.scrollLibs.jsFile · 0.85

Calls 4

pushSelectionToHistoryFunction · 0.85
setSelectionFunction · 0.85
hasHandlerFunction · 0.85
loopFunction · 0.85

Tested by

no test coverage detected