implemented undo 🙌

This commit is contained in:
Kevin Jahns
2017-10-30 11:33:00 +01:00
parent c545118637
commit 0208d83f91
16 changed files with 302 additions and 109 deletions

View File

@@ -52,6 +52,19 @@ export default class Item {
this._parentSub = null
this._deleted = false
}
/**
* Copy the effect of struct
*/
_copy () {
let struct = new this.constructor()
struct._origin = this._left
struct._left = this._left
struct._right = this
struct._right_origin = this
struct._parent = this._parent
struct._parentSub = this._parentSub
return struct
}
get _lastId () {
return new ID(this._id.user, this._id.clock + this._length - 1)
}
@@ -83,6 +96,7 @@ export default class Item {
del._integrate(y, true)
}
transactionTypeChanged(y, this._parent, this._parentSub)
y._transaction.deletedStructs.add(this)
}
/**
* This is called right before this struct receives any children.

View File

@@ -6,6 +6,11 @@ export default class ItemJSON extends Item {
super()
this._content = null
}
_copy () {
let struct = super._copy()
struct._content = this._content
return struct
}
get _length () {
return this._content.length
}

View File

@@ -6,6 +6,11 @@ export default class ItemString extends Item {
super()
this._content = null
}
_copy () {
let struct = super._copy()
struct._content = this._content
return struct
}
get _length () {
return this._content.length
}

View File

@@ -37,6 +37,48 @@ export default class Type extends Item {
this._start = null
this._y = null
this._eventHandler = new EventHandler()
this._deepEventHandler = new EventHandler()
}
_callEventHandler (event) {
this._eventHandler.callEventListeners(event)
let type = this
while (type !== this._y) {
type._deepEventHandler.callEventListeners(event)
type = type._parent
}
}
_copy (undeleteChildren) {
let copy = super._copy()
let map = new Map()
copy._map = map
for (let [key, value] of this._map) {
if (undeleteChildren.has(value) || !value.deleted) {
let _item = value._copy(undeleteChildren)
_item._parent = copy
map.set(key, value._copy(undeleteChildren))
}
}
let prevUndeleted = null
copy._start = null
let item = this._start
while (item !== null) {
if (undeleteChildren.has(item) || !item.deleted) {
let _item = item._copy(undeleteChildren)
_item._left = prevUndeleted
_item._origin = prevUndeleted
_item._right = null
_item._right_origin = null
_item._parent = copy
if (prevUndeleted === null) {
copy._start = _item
} else {
prevUndeleted._right = _item
}
prevUndeleted = _item
}
item = item._right
}
return copy
}
_transact (f) {
const y = this._y
@@ -49,9 +91,15 @@ export default class Type extends Item {
observe (f) {
this._eventHandler.addEventListener(f)
}
observeDeep (f) {
this._deepEventHandler.addEventListener(f)
}
unobserve (f) {
this._eventHandler.removeEventListener(f)
}
unobserveDeep (f) {
this._deepEventHandler.removeEventListener(f)
}
_integrate (y) {
y._transaction.newTypes.add(this)
super._integrate(y)