fixed all type issues
This commit is contained in:
@@ -19,3 +19,6 @@ export { YXmlElement as XmlElement, YXmlFragment as XmlFragment } from './types/
|
||||
export { createRelativePosition, createRelativePositionByOffset, createAbsolutePosition, compareRelativePositions, writeRelativePosition, readRelativePosition, AbsolutePosition, RelativePosition } from './utils/relativePosition.js'
|
||||
export { ID, createID, compareIDs } from './utils/ID.js'
|
||||
export { isParentOf } from './utils/isParentOf.js'
|
||||
|
||||
export { writeStructs, writeStructsFromTransaction, readStructs } from './utils/structEncoding.js'
|
||||
export { getState, getStates, readStatesAsMap, writeStates } from './utils/StructStore.js'
|
||||
|
||||
@@ -3,15 +3,13 @@
|
||||
*/
|
||||
|
||||
import { Transaction } from '../utils/Transaction.js' // eslint-disable-line
|
||||
import { YMap } from './YMap.js'
|
||||
import * as encoding from 'lib0/encoding.js'
|
||||
import * as decoding from 'lib0/decoding.js'
|
||||
import { Y } from '../utils/Y.js' // eslint-disable-line
|
||||
import { YXmlEvent } from './YXmlEvent.js'
|
||||
import { ItemType } from '../structs/ItemType.js' // eslint-disable-line
|
||||
import { YXmlText } from './YXmlText.js' // eslint-disable-line
|
||||
import { YXmlHook } from './YXmlHook.js' // eslint-disable-line
|
||||
import { AbstractType, typeArrayMap, typeArrayForEach, typeMapGet, typeMapGetAll, typeArrayInsertGenerics } from './AbstractType.js'
|
||||
import { AbstractType, typeArrayMap, typeArrayForEach, typeMapGet, typeMapGetAll, typeArrayInsertGenerics, typeArrayDelete, typeMapSet, typeMapDelete } from './AbstractType.js'
|
||||
import { Snapshot } from '../utils/Snapshot.js' // eslint-disable-line
|
||||
|
||||
/**
|
||||
@@ -237,6 +235,37 @@ export class YXmlElement extends YXmlFragment {
|
||||
constructor (nodeName = 'UNDEFINED') {
|
||||
super()
|
||||
this.nodeName = nodeName.toUpperCase()
|
||||
/**
|
||||
* @type {Array<any>|null}
|
||||
*/
|
||||
this._prelimContent = []
|
||||
/**
|
||||
* @type {Map<string, any>|null}
|
||||
*/
|
||||
this._prelimAttrs = new Map()
|
||||
}
|
||||
|
||||
/**
|
||||
* Integrate this type into the Yjs instance.
|
||||
*
|
||||
* * Save this struct in the os
|
||||
* * This type is sent to other client
|
||||
* * Observer functions are fired
|
||||
*
|
||||
* @param {Transaction} transaction The Yjs instance
|
||||
* @param {ItemType} item
|
||||
* @private
|
||||
*/
|
||||
_integrate (transaction, item) {
|
||||
super._integrate(transaction, item)
|
||||
// @ts-ignore
|
||||
this.insert(0, this._prelimContent)
|
||||
this._prelimContent = null
|
||||
// @ts-ignore
|
||||
this._prelimAttrs.forEach((value, key) => {
|
||||
this.setAttribute(key, value)
|
||||
})
|
||||
this._prelimContent = null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -301,7 +330,14 @@ export class YXmlElement extends YXmlFragment {
|
||||
* @public
|
||||
*/
|
||||
removeAttribute (attributeName) {
|
||||
return YMap.prototype.delete.call(this, attributeName)
|
||||
if (this._y !== null) {
|
||||
this._y.transact(transaction => {
|
||||
typeMapDelete(transaction, this, attributeName)
|
||||
})
|
||||
} else {
|
||||
// @ts-ignore
|
||||
this._prelimAttrs.delete(attributeName)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -313,7 +349,14 @@ export class YXmlElement extends YXmlFragment {
|
||||
* @public
|
||||
*/
|
||||
setAttribute (attributeName, attributeValue) {
|
||||
return YMap.prototype.set.call(this, attributeName, attributeValue)
|
||||
if (this._y !== null) {
|
||||
this._y.transact(transaction => {
|
||||
typeMapSet(transaction, this, attributeName, attributeValue)
|
||||
})
|
||||
} else {
|
||||
// @ts-ignore
|
||||
this._prelimAttrs.set(attributeName, attributeValue)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -363,6 +406,23 @@ export class YXmlElement extends YXmlFragment {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes elements starting from an index.
|
||||
*
|
||||
* @param {number} index Index at which to start deleting elements
|
||||
* @param {number} [length=1] The number of elements to remove. Defaults to 1.
|
||||
*/
|
||||
delete (index, length = 1) {
|
||||
if (this._y !== null) {
|
||||
this._y.transact(transaction => {
|
||||
typeArrayDelete(transaction, this, index, length)
|
||||
})
|
||||
} else {
|
||||
// @ts-ignore _prelimContent is defined because this is not yet integrated
|
||||
this._prelimContent.splice(index, length)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: outsource the binding property.
|
||||
/**
|
||||
* Creates a Dom Element that mirrors this YXmlElement.
|
||||
|
||||
@@ -6,6 +6,8 @@ import { Transaction } from './Transaction.js' // eslint-disable-line
|
||||
import * as map from 'lib0/map.js'
|
||||
import * as math from 'lib0/math.js'
|
||||
import * as error from 'lib0/error.js'
|
||||
import * as encoding from 'lib0/encoding.js'
|
||||
import * as decoding from 'lib0/decoding.js'
|
||||
|
||||
export class StructStore {
|
||||
constructor () {
|
||||
@@ -227,3 +229,36 @@ export const replaceStruct = (store, struct, newStruct) => {
|
||||
* @return {boolean}
|
||||
*/
|
||||
export const exists = (store, id) => id.clock < getState(store, id.client)
|
||||
|
||||
/**
|
||||
* Read StateMap from Decoder and return as Map
|
||||
*
|
||||
* @param {decoding.Decoder} decoder
|
||||
* @return {Map<number,number>}
|
||||
*/
|
||||
export const readStatesAsMap = decoder => {
|
||||
const ss = new Map()
|
||||
const ssLength = decoding.readVarUint(decoder)
|
||||
for (let i = 0; i < ssLength; i++) {
|
||||
const client = decoding.readVarUint(decoder)
|
||||
const clock = decoding.readVarUint(decoder)
|
||||
ss.set(client, clock)
|
||||
}
|
||||
return ss
|
||||
}
|
||||
|
||||
/**
|
||||
* Write StateMap to Encoder
|
||||
*
|
||||
* @param {encoding.Encoder} encoder
|
||||
* @param {StructStore} store
|
||||
*/
|
||||
export const writeStates = (encoder, store) => {
|
||||
encoding.writeVarUint(encoder, store.clients.size)
|
||||
store.clients.forEach((structs, client) => {
|
||||
const id = structs[structs.length - 1].id
|
||||
encoding.writeVarUint(encoder, id.client)
|
||||
encoding.writeVarUint(encoder, id.clock)
|
||||
})
|
||||
return encoder
|
||||
}
|
||||
|
||||
@@ -199,9 +199,6 @@ export class Y extends Observable {
|
||||
* comments: y.getArray('comments')
|
||||
* }
|
||||
*
|
||||
* @TODO: implement getText, getArray, ..
|
||||
* @TODO: Decide wether to use define() or get() and then use it consistently
|
||||
*
|
||||
* @param {string} name
|
||||
* @param {Function} TypeConstructor The constructor of the type definition
|
||||
* @return {AbstractType<any>} The created type. Constructed with TypeConstructor
|
||||
|
||||
@@ -13,18 +13,18 @@ import { isDeleted } from './DeleteSet.js'
|
||||
*/
|
||||
export class YEvent {
|
||||
/**
|
||||
* @param {AbstractType<any> target The changed type.
|
||||
* @param {AbstractType<any>} target The changed type.
|
||||
* @param {Transaction} transaction
|
||||
*/
|
||||
constructor (target, transaction) {
|
||||
/**
|
||||
* The type on which this event was created on.
|
||||
* @type {AbstractType<any>
|
||||
* @type {AbstractType<any>}
|
||||
*/
|
||||
this.target = target
|
||||
/**
|
||||
* The current target on which the observe callback is called.
|
||||
* @type {AbstractType<any>
|
||||
* @type {AbstractType<any>}
|
||||
*/
|
||||
this.currentTarget = target
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user