fix some tests, implement event classes for types, and re-implement logging
This commit is contained in:
@@ -1,9 +1,16 @@
|
||||
import Type from '../Struct/Type.js'
|
||||
import ItemJSON from '../Struct/ItemJSON.js'
|
||||
|
||||
class YArrayEvent {
|
||||
constructor (yarray, remote) {
|
||||
this.target = yarray
|
||||
this.remote = remote
|
||||
}
|
||||
}
|
||||
|
||||
export default class YArray extends Type {
|
||||
_callObserver () {
|
||||
this._eventHandler.callEventListeners({})
|
||||
_callObserver (parentSubs, remote) {
|
||||
this._eventHandler.callEventListeners(new YArrayEvent(this, remote))
|
||||
}
|
||||
get (i) {
|
||||
// TODO: This can be improved!
|
||||
@@ -107,12 +114,13 @@ export default class YArray extends Type {
|
||||
}
|
||||
item = item._right
|
||||
}
|
||||
if (length > 0) {
|
||||
throw new Error('Delete exceeds the range of the YArray')
|
||||
}
|
||||
})
|
||||
if (length > 0) {
|
||||
throw new Error('Delete exceeds the range of the YArray')
|
||||
}
|
||||
}
|
||||
insertAfter (left, content) {
|
||||
const y = this._y
|
||||
const apply = () => {
|
||||
let right
|
||||
if (left === null) {
|
||||
@@ -123,10 +131,13 @@ export default class YArray extends Type {
|
||||
let prevJsonIns = null
|
||||
for (let i = 0; i < content.length; i++) {
|
||||
let c = content[i]
|
||||
if (typeof c === 'function') {
|
||||
c = new c() // eslint-disable-line new-cap
|
||||
}
|
||||
if (c instanceof Type) {
|
||||
if (prevJsonIns !== null) {
|
||||
if (this._y !== null) {
|
||||
prevJsonIns._integrate(this._y)
|
||||
if (y !== null) {
|
||||
prevJsonIns._integrate(y)
|
||||
}
|
||||
left = prevJsonIns
|
||||
prevJsonIns = null
|
||||
@@ -136,8 +147,8 @@ export default class YArray extends Type {
|
||||
c._right = right
|
||||
c._right_origin = right
|
||||
c._parent = this
|
||||
if (this._y !== null) {
|
||||
c._integrate(this._y)
|
||||
if (y !== null) {
|
||||
c._integrate(y)
|
||||
} else if (left === null) {
|
||||
this._start = c
|
||||
}
|
||||
@@ -155,12 +166,12 @@ export default class YArray extends Type {
|
||||
prevJsonIns._content.push(c)
|
||||
}
|
||||
}
|
||||
if (prevJsonIns !== null && this._y !== null) {
|
||||
prevJsonIns._integrate(this._y)
|
||||
if (prevJsonIns !== null && y !== null) {
|
||||
prevJsonIns._integrate(y)
|
||||
}
|
||||
}
|
||||
if (this._y !== null) {
|
||||
this._y.transact(apply)
|
||||
if (y !== null) {
|
||||
y.transact(apply)
|
||||
} else {
|
||||
apply()
|
||||
}
|
||||
@@ -170,13 +181,19 @@ export default class YArray extends Type {
|
||||
let left = null
|
||||
let right = this._start
|
||||
let count = 0
|
||||
const y = this._y
|
||||
while (right !== null) {
|
||||
if (count <= pos && pos < count + right._length) {
|
||||
right = right._splitAt(this._y, pos - count)
|
||||
const rightLen = right._deleted ? 0 : (right._length - 1)
|
||||
if (count <= pos && pos <= count + rightLen) {
|
||||
const splitDiff = pos - count
|
||||
right = right._splitAt(y, splitDiff)
|
||||
left = right._left
|
||||
count += splitDiff
|
||||
break
|
||||
}
|
||||
count += right._length
|
||||
if (!right._deleted) {
|
||||
count += right._length
|
||||
}
|
||||
left = right
|
||||
right = right._right
|
||||
}
|
||||
|
||||
@@ -2,11 +2,17 @@ import Type from '../Struct/Type.js'
|
||||
import Item from '../Struct/Item.js'
|
||||
import ItemJSON from '../Struct/ItemJSON.js'
|
||||
|
||||
class YMapEvent {
|
||||
constructor (ymap, subs, remote) {
|
||||
this.target = ymap
|
||||
this.keysChanged = subs
|
||||
this.remote = remote
|
||||
}
|
||||
}
|
||||
|
||||
export default class YMap extends Type {
|
||||
_callObserver (parentSub) {
|
||||
this._eventHandler.callEventListeners({
|
||||
name: parentSub
|
||||
})
|
||||
_callObserver (parentSubs, remote) {
|
||||
this._eventHandler.callEventListeners(new YMapEvent(this, parentSubs, remote))
|
||||
}
|
||||
toJSON () {
|
||||
const map = {}
|
||||
@@ -36,13 +42,21 @@ export default class YMap extends Type {
|
||||
})
|
||||
}
|
||||
set (key, value) {
|
||||
this._y.transact(() => {
|
||||
const y = this._y
|
||||
y.transact(() => {
|
||||
const old = this._map.get(key) || null
|
||||
if (old !== null) {
|
||||
old._delete(this._y)
|
||||
if (old instanceof ItemJSON && old._content[0] === value) {
|
||||
// Trying to overwrite with same value
|
||||
// break here
|
||||
return value
|
||||
}
|
||||
old._delete(y)
|
||||
}
|
||||
let v
|
||||
if (value instanceof Item) {
|
||||
if (typeof value === 'function') {
|
||||
v = new value() // eslint-disable-line new-cap
|
||||
} else if (value instanceof Item) {
|
||||
v = value
|
||||
} else {
|
||||
v = new ItemJSON()
|
||||
@@ -52,7 +66,7 @@ export default class YMap extends Type {
|
||||
v._right_origin = old
|
||||
v._parent = this
|
||||
v._parentSub = key
|
||||
v._integrate(this._y)
|
||||
v._integrate(y)
|
||||
})
|
||||
return value
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { defaultDomFilter, applyChangesFromDom, reflectChangesOnDom } from './ut
|
||||
|
||||
import YArray from '../YArray.js'
|
||||
import YXmlText from './YXmlText.js'
|
||||
import YXmlEvent from './YXmlEvent.js'
|
||||
|
||||
function domToYXml (parent, doms) {
|
||||
const types = []
|
||||
@@ -65,22 +66,8 @@ export default class YXmlFragment extends YArray {
|
||||
xml.setDomFilter(f)
|
||||
})
|
||||
}
|
||||
_callObserver (parentSub) {
|
||||
let event
|
||||
if (parentSub !== null) {
|
||||
event = {
|
||||
type: 'attributeChanged',
|
||||
name: parentSub,
|
||||
value: this.getAttribute(parentSub),
|
||||
target: this
|
||||
}
|
||||
} else {
|
||||
event = {
|
||||
type: 'contentChanged',
|
||||
target: this
|
||||
}
|
||||
}
|
||||
this._eventHandler.callEventListeners(event)
|
||||
_callObserver (parentSubs, remote) {
|
||||
this._eventHandler.callEventListeners(new YXmlEvent(this, parentSubs, remote))
|
||||
}
|
||||
toString () {
|
||||
return this.map(xml => xml.toString()).join('')
|
||||
|
||||
@@ -131,13 +131,17 @@ export function reflectChangesOnDom (event) {
|
||||
yxml._mutualExclude(() => {
|
||||
// TODO: do this once before applying stuff
|
||||
// let anchorViewPosition = getAnchorViewPosition(yxml._scrollElement)
|
||||
if (event.type === 'attributeChanged') {
|
||||
if (event.value === undefined) {
|
||||
dom.removeAttribute(event.name)
|
||||
|
||||
// update attributes
|
||||
event.attributesChanged.forEach(attributeName => {
|
||||
const value = yxml.getAttribute(attributeName)
|
||||
if (value === undefined) {
|
||||
dom.remoteAttribute(attributeName)
|
||||
} else {
|
||||
dom.setAttribute(event.name, event.value)
|
||||
dom.setAttribute(attributeName, value)
|
||||
}
|
||||
} else if (event.type === 'contentChanged') {
|
||||
})
|
||||
if (event.childListChanged) {
|
||||
// create fragment of undeleted nodes
|
||||
const fragment = document.createDocumentFragment()
|
||||
yxml.forEach(function (t) {
|
||||
|
||||
Reference in New Issue
Block a user