implement some of the commented todos
This commit is contained in:
@@ -22,6 +22,29 @@ import * as iterator from 'lib0/iterator.js'
|
||||
import * as error from 'lib0/error.js'
|
||||
import * as encoding from 'lib0/encoding.js' // eslint-disable-line
|
||||
|
||||
/**
|
||||
* Call event listeners with an event. This will also add an event to all
|
||||
* parents (for `.observeDeep` handlers).
|
||||
* @private
|
||||
*
|
||||
* @template EventType
|
||||
* @param {AbstractType<EventType>} type
|
||||
* @param {Transaction} transaction
|
||||
* @param {EventType} event
|
||||
*/
|
||||
export const callTypeObservers = (type, transaction, event) => {
|
||||
callEventHandlerListeners(type._eH, [event, transaction])
|
||||
const changedParentTypes = transaction.changedParentTypes
|
||||
while (true) {
|
||||
// @ts-ignore
|
||||
map.setIfUndefined(changedParentTypes, type, () => []).push(event)
|
||||
if (type._item === null) {
|
||||
break
|
||||
}
|
||||
type = type._item.parent
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @template EventType
|
||||
* Abstract Yjs Type class
|
||||
@@ -100,42 +123,14 @@ export class AbstractType {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates YEvent and calls _callEventHandler.
|
||||
* Creates YEvent and calls all type observers.
|
||||
* Must be implemented by each type.
|
||||
* @todo Rename to _createEvent
|
||||
* @private
|
||||
*
|
||||
* @param {Transaction} transaction
|
||||
* @param {Set<null|string>} parentSubs Keys changed on this type. `null` if list was modified.
|
||||
*/
|
||||
_callObserver (transaction, parentSubs) {
|
||||
throw error.methodUnimplemented()
|
||||
}
|
||||
|
||||
/**
|
||||
* Call event listeners with an event. This will also add an event to all
|
||||
* parents (for `.observeDeep` handlers).
|
||||
* @private
|
||||
*
|
||||
* @param {Transaction} transaction
|
||||
* @param {any} event
|
||||
*/
|
||||
_callEventHandler (transaction, event) {
|
||||
callEventHandlerListeners(this._eH, [event, transaction])
|
||||
const changedParentTypes = transaction.changedParentTypes
|
||||
/**
|
||||
* @type {AbstractType<EventType>}
|
||||
*/
|
||||
let type = this
|
||||
while (true) {
|
||||
// @ts-ignore
|
||||
map.setIfUndefined(changedParentTypes, type, () => []).push(event)
|
||||
if (type._item === null) {
|
||||
break
|
||||
}
|
||||
type = type._item.parent
|
||||
}
|
||||
}
|
||||
_callObserver (transaction, parentSubs) { /* skip if no type is specified */ }
|
||||
|
||||
/**
|
||||
* Observe all events that are created on this type.
|
||||
|
||||
@@ -13,6 +13,8 @@ import {
|
||||
typeArrayDelete,
|
||||
typeArrayMap,
|
||||
YArrayRefID,
|
||||
callTypeObservers,
|
||||
transact,
|
||||
Y, Transaction, ItemType, // eslint-disable-line
|
||||
} from '../internals.js'
|
||||
|
||||
@@ -75,7 +77,7 @@ export class YArray extends AbstractType {
|
||||
* @param {Set<null|string>} parentSubs Keys changed on this type. `null` if list was modified.
|
||||
*/
|
||||
_callObserver (transaction, parentSubs) {
|
||||
this._callEventHandler(transaction, new YArrayEvent(this, transaction))
|
||||
callTypeObservers(this, transaction, new YArrayEvent(this, transaction))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -141,7 +143,7 @@ export class YArray extends AbstractType {
|
||||
*/
|
||||
delete (index, length = 1) {
|
||||
if (this._y !== null) {
|
||||
this._y.transact(transaction => {
|
||||
transact(this._y, transaction => {
|
||||
typeArrayDelete(transaction, this, index, length)
|
||||
})
|
||||
} else {
|
||||
@@ -168,7 +170,7 @@ export class YArray extends AbstractType {
|
||||
*/
|
||||
insert (index, content) {
|
||||
if (this._y !== null) {
|
||||
this._y.transact(transaction => {
|
||||
transact(this._y, transaction => {
|
||||
typeArrayInsertGenerics(transaction, this, index, content)
|
||||
})
|
||||
} else {
|
||||
|
||||
@@ -11,6 +11,8 @@ import {
|
||||
typeMapHas,
|
||||
createMapIterator,
|
||||
YMapRefID,
|
||||
callTypeObservers,
|
||||
transact,
|
||||
Y, Transaction, ItemType, // eslint-disable-line
|
||||
} from '../internals.js'
|
||||
|
||||
@@ -75,7 +77,7 @@ export class YMap extends AbstractType {
|
||||
* @param {Set<null|string>} parentSubs Keys changed on this type. `null` if list was modified.
|
||||
*/
|
||||
_callObserver (transaction, parentSubs) {
|
||||
this._callEventHandler(transaction, new YMapEvent(this, transaction, parentSubs))
|
||||
callTypeObservers(this, transaction, new YMapEvent(this, transaction, parentSubs))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,7 +127,7 @@ export class YMap extends AbstractType {
|
||||
*/
|
||||
delete (key) {
|
||||
if (this._y !== null) {
|
||||
this._y.transact(transaction => {
|
||||
transact(this._y, transaction => {
|
||||
typeMapDelete(transaction, this, key)
|
||||
})
|
||||
} else {
|
||||
@@ -142,7 +144,7 @@ export class YMap extends AbstractType {
|
||||
*/
|
||||
set (key, value) {
|
||||
if (this._y !== null) {
|
||||
this._y.transact(transaction => {
|
||||
transact(this._y, transaction => {
|
||||
typeMapSet(transaction, this, key, value)
|
||||
})
|
||||
} else {
|
||||
|
||||
@@ -13,6 +13,8 @@ import {
|
||||
getItemCleanStart,
|
||||
isVisible,
|
||||
YTextRefID,
|
||||
callTypeObservers,
|
||||
transact,
|
||||
Y, ItemType, AbstractItem, Snapshot, StructStore, Transaction // eslint-disable-line
|
||||
} from '../internals.js'
|
||||
|
||||
@@ -295,7 +297,6 @@ const deleteText = (transaction, parent, left, right, currentAttributes, length)
|
||||
return { left, right }
|
||||
}
|
||||
|
||||
// TODO: In the quill delta representation we should also use the format {ops:[..]}
|
||||
/**
|
||||
* The Quill Delta format represents changes on a text document with
|
||||
* formatting information. For mor information visit {@link https://quilljs.com/docs/delta/|Quill Delta}
|
||||
@@ -354,7 +355,7 @@ class YTextEvent extends YEvent {
|
||||
if (this._delta === null) {
|
||||
const y = this.target._y
|
||||
// @ts-ignore
|
||||
y.transact(transaction => {
|
||||
transact(y, transaction => {
|
||||
/**
|
||||
* @type {Array<{delete:number|undefined,retain:number|undefined,insert:string|undefined,attributes:Object<string,any>}>}
|
||||
*/
|
||||
@@ -586,7 +587,7 @@ export class YText extends AbstractType {
|
||||
* @param {Set<null|string>} parentSubs Keys changed on this type. `null` if list was modified.
|
||||
*/
|
||||
_callObserver (transaction, parentSubs) {
|
||||
this._callEventHandler(transaction, new YTextEvent(this, transaction))
|
||||
callTypeObservers(this, transaction, new YTextEvent(this, transaction))
|
||||
}
|
||||
|
||||
toDom () {
|
||||
@@ -657,7 +658,7 @@ export class YText extends AbstractType {
|
||||
*/
|
||||
applyDelta (delta) {
|
||||
if (this._y !== null) {
|
||||
this._y.transact(transaction => {
|
||||
transact(this._y, transaction => {
|
||||
/**
|
||||
* @type {{left:AbstractItem|null,right:AbstractItem|null}}
|
||||
*/
|
||||
@@ -772,7 +773,7 @@ export class YText extends AbstractType {
|
||||
}
|
||||
const y = this._y
|
||||
if (y !== null) {
|
||||
y.transact(transaction => {
|
||||
transact(y, transaction => {
|
||||
const {left, right, currentAttributes} = findPosition(transaction, y.store, this, index)
|
||||
insertText(transaction, this, left, right, currentAttributes, text, attributes)
|
||||
})
|
||||
@@ -795,7 +796,7 @@ export class YText extends AbstractType {
|
||||
}
|
||||
const y = this._y
|
||||
if (y !== null) {
|
||||
y.transact(transaction => {
|
||||
transact(y, transaction => {
|
||||
const { left, right, currentAttributes } = findPosition(transaction, y.store, this, index)
|
||||
insertText(transaction, this, left, right, currentAttributes, embed, attributes)
|
||||
})
|
||||
@@ -816,7 +817,7 @@ export class YText extends AbstractType {
|
||||
}
|
||||
const y = this._y
|
||||
if (y !== null) {
|
||||
y.transact(transaction => {
|
||||
transact(y, transaction => {
|
||||
const { left, right, currentAttributes } = findPosition(transaction, y.store, this, index)
|
||||
deleteText(transaction, this, left, right, currentAttributes, length)
|
||||
})
|
||||
@@ -836,7 +837,7 @@ export class YText extends AbstractType {
|
||||
format (index, length, attributes) {
|
||||
const y = this._y
|
||||
if (y !== null) {
|
||||
y.transact(transaction => {
|
||||
transact(y, transaction => {
|
||||
let { left, right, currentAttributes } = findPosition(transaction, y.store, this, index)
|
||||
if (right === null) {
|
||||
return
|
||||
|
||||
@@ -14,6 +14,8 @@ import {
|
||||
typeMapSet,
|
||||
typeMapDelete,
|
||||
YXmlElementRefID,
|
||||
callTypeObservers,
|
||||
transact,
|
||||
Y, Transaction, ItemType, YXmlText, YXmlHook, Snapshot // eslint-disable-line
|
||||
} from '../internals.js'
|
||||
|
||||
@@ -192,7 +194,7 @@ export class YXmlFragment extends AbstractType {
|
||||
* @param {Set<null|string>} parentSubs Keys changed on this type. `null` if list was modified.
|
||||
*/
|
||||
_callObserver (transaction, parentSubs) {
|
||||
this._callEventHandler(transaction, new YXmlEvent(this, parentSubs, transaction))
|
||||
callTypeObservers(this, transaction, new YXmlEvent(this, parentSubs, transaction))
|
||||
}
|
||||
|
||||
toString () {
|
||||
@@ -329,7 +331,7 @@ export class YXmlElement extends YXmlFragment {
|
||||
*/
|
||||
removeAttribute (attributeName) {
|
||||
if (this._y !== null) {
|
||||
this._y.transact(transaction => {
|
||||
transact(this._y, transaction => {
|
||||
typeMapDelete(transaction, this, attributeName)
|
||||
})
|
||||
} else {
|
||||
@@ -348,7 +350,7 @@ export class YXmlElement extends YXmlFragment {
|
||||
*/
|
||||
setAttribute (attributeName, attributeValue) {
|
||||
if (this._y !== null) {
|
||||
this._y.transact(transaction => {
|
||||
transact(this._y, transaction => {
|
||||
typeMapSet(transaction, this, attributeName, attributeValue)
|
||||
})
|
||||
} else {
|
||||
@@ -395,7 +397,7 @@ export class YXmlElement extends YXmlFragment {
|
||||
*/
|
||||
insert (index, content) {
|
||||
if (this._y !== null) {
|
||||
this._y.transact(transaction => {
|
||||
transact(this._y, transaction => {
|
||||
typeArrayInsertGenerics(transaction, this, index, content)
|
||||
})
|
||||
} else {
|
||||
@@ -412,7 +414,7 @@ export class YXmlElement extends YXmlFragment {
|
||||
*/
|
||||
delete (index, length = 1) {
|
||||
if (this._y !== null) {
|
||||
this._y.transact(transaction => {
|
||||
transact(this._y, transaction => {
|
||||
typeArrayDelete(transaction, this, index, length)
|
||||
})
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user