unify Y.Array & Y.Text deltas so event.changes.delta is equal to event.delta
This commit is contained in:
parent
8a8a60efde
commit
77958da657
@ -502,14 +502,6 @@ const deleteText = (transaction, currPos, length) => {
|
|||||||
* @typedef {Object} TextAttributes
|
* @typedef {Object} TextAttributes
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {Object} DeltaItem
|
|
||||||
* @property {number|undefined} DeltaItem.delete
|
|
||||||
* @property {number|undefined} DeltaItem.retain
|
|
||||||
* @property {string|undefined} DeltaItem.insert
|
|
||||||
* @property {Object<string,any>} DeltaItem.attributes
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event that describes the changes on a YText type.
|
* Event that describes the changes on a YText type.
|
||||||
*/
|
*/
|
||||||
@ -521,10 +513,6 @@ export class YTextEvent extends YEvent {
|
|||||||
*/
|
*/
|
||||||
constructor (ytext, transaction, subs) {
|
constructor (ytext, transaction, subs) {
|
||||||
super(ytext, transaction)
|
super(ytext, transaction)
|
||||||
/**
|
|
||||||
* @type {Array<DeltaItem>|null}
|
|
||||||
*/
|
|
||||||
this._delta = null
|
|
||||||
/**
|
/**
|
||||||
* Whether the children changed.
|
* Whether the children changed.
|
||||||
* @type {Boolean}
|
* @type {Boolean}
|
||||||
@ -545,20 +533,41 @@ export class YTextEvent extends YEvent {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {{added:Set<Item>,deleted:Set<Item>,keys:Map<string,{action:'add'|'update'|'delete',oldValue:any}>,delta:Array<{insert?:Array<any>|string, delete?:number, retain?:number}>}}
|
||||||
|
*/
|
||||||
|
get changes () {
|
||||||
|
if (this._changes === null) {
|
||||||
|
/**
|
||||||
|
* @type {{added:Set<Item>,deleted:Set<Item>,keys:Map<string,{action:'add'|'update'|'delete',oldValue:any}>,delta:Array<{insert?:Array<any>|string, delete?:number, retain?:number}>}}
|
||||||
|
*/
|
||||||
|
const changes = {
|
||||||
|
keys: this.keys,
|
||||||
|
delta: this.delta,
|
||||||
|
added: new Set(),
|
||||||
|
deleted: new Set()
|
||||||
|
}
|
||||||
|
this._changes = changes
|
||||||
|
}
|
||||||
|
return /** @type {any} */ (this._changes)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute the changes in the delta format.
|
* Compute the changes in the delta format.
|
||||||
* A {@link https://quilljs.com/docs/delta/|Quill Delta}) that represents the changes on the document.
|
* A {@link https://quilljs.com/docs/delta/|Quill Delta}) that represents the changes on the document.
|
||||||
*
|
*
|
||||||
* @type {Array<DeltaItem>}
|
* @type {Array<{insert?:string, delete?:number, retain?:number, attributes?: Object<string,any>}>}
|
||||||
*
|
*
|
||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
get delta () {
|
get delta () {
|
||||||
if (this._delta === null) {
|
if (this._delta === null) {
|
||||||
const y = /** @type {Doc} */ (this.target.doc)
|
const y = /** @type {Doc} */ (this.target.doc)
|
||||||
this._delta = []
|
/**
|
||||||
|
* @type {Array<{insert?:string, delete?:number, retain?:number, attributes?: Object<string,any>}>}
|
||||||
|
*/
|
||||||
|
const delta = []
|
||||||
transact(y, transaction => {
|
transact(y, transaction => {
|
||||||
const delta = /** @type {Array<DeltaItem>} */ (this._delta)
|
|
||||||
const currentAttributes = new Map() // saves all current attributes for insert
|
const currentAttributes = new Map() // saves all current attributes for insert
|
||||||
const oldAttributes = new Map()
|
const oldAttributes = new Map()
|
||||||
let item = this.target._start
|
let item = this.target._start
|
||||||
@ -728,8 +737,9 @@ export class YTextEvent extends YEvent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
this._delta = delta
|
||||||
}
|
}
|
||||||
return this._delta
|
return /** @type {any} */ (this._delta)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,6 +35,14 @@ export class YEvent {
|
|||||||
* @type {Object|null}
|
* @type {Object|null}
|
||||||
*/
|
*/
|
||||||
this._changes = null
|
this._changes = null
|
||||||
|
/**
|
||||||
|
* @type {null | Map<string, { action: 'add' | 'update' | 'delete', oldValue: any, newValue: any }>}
|
||||||
|
*/
|
||||||
|
this._keys = null
|
||||||
|
/**
|
||||||
|
* @type {null | Array<{ insert?: string | Array<any>, retain?: number, delete?: number, attributes?: Object<string, any> }>}
|
||||||
|
*/
|
||||||
|
this._delta = null
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -67,6 +75,66 @@ export class YEvent {
|
|||||||
return isDeleted(this.transaction.deleteSet, struct.id)
|
return isDeleted(this.transaction.deleteSet, struct.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {Map<string, { action: 'add' | 'update' | 'delete', oldValue: any, newValue: any }>}
|
||||||
|
*/
|
||||||
|
get keys () {
|
||||||
|
if (this._keys === null) {
|
||||||
|
const keys = new Map()
|
||||||
|
const target = this.target
|
||||||
|
const changed = /** @type Set<string|null> */ (this.transaction.changed.get(target))
|
||||||
|
changed.forEach(key => {
|
||||||
|
if (key !== null) {
|
||||||
|
const item = /** @type {Item} */ (target._map.get(key))
|
||||||
|
/**
|
||||||
|
* @type {'delete' | 'add' | 'update'}
|
||||||
|
*/
|
||||||
|
let action
|
||||||
|
let oldValue
|
||||||
|
if (this.adds(item)) {
|
||||||
|
let prev = item.left
|
||||||
|
while (prev !== null && this.adds(prev)) {
|
||||||
|
prev = prev.left
|
||||||
|
}
|
||||||
|
if (this.deletes(item)) {
|
||||||
|
if (prev !== null && this.deletes(prev)) {
|
||||||
|
action = 'delete'
|
||||||
|
oldValue = array.last(prev.content.getContent())
|
||||||
|
} else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (prev !== null && this.deletes(prev)) {
|
||||||
|
action = 'update'
|
||||||
|
oldValue = array.last(prev.content.getContent())
|
||||||
|
} else {
|
||||||
|
action = 'add'
|
||||||
|
oldValue = undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (this.deletes(item)) {
|
||||||
|
action = 'delete'
|
||||||
|
oldValue = array.last(/** @type {Item} */ item.content.getContent())
|
||||||
|
} else {
|
||||||
|
return // nop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
keys.set(key, { action, oldValue })
|
||||||
|
}
|
||||||
|
})
|
||||||
|
this._keys = keys
|
||||||
|
}
|
||||||
|
return this._keys
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {Array<{insert?: string | Array<any>, retain?: number, delete?: number, attributes?: Object<string, any>}>}
|
||||||
|
*/
|
||||||
|
get delta () {
|
||||||
|
return this.changes.delta
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if a struct is added by this event.
|
* Check if a struct is added by this event.
|
||||||
*
|
*
|
||||||
@ -80,7 +148,7 @@ export class YEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return {{added:Set<Item>,deleted:Set<Item>,keys:Map<string,{action:'add'|'update'|'delete',oldValue:any}>,delta:Array<{insert:Array<any>}|{delete:number}|{retain:number}>}}
|
* @type {{added:Set<Item>,deleted:Set<Item>,keys:Map<string,{action:'add'|'update'|'delete',oldValue:any}>,delta:Array<{insert?:Array<any>|string, delete?:number, retain?:number}>}}
|
||||||
*/
|
*/
|
||||||
get changes () {
|
get changes () {
|
||||||
let changes = this._changes
|
let changes = this._changes
|
||||||
@ -92,12 +160,11 @@ export class YEvent {
|
|||||||
* @type {Array<{insert:Array<any>}|{delete:number}|{retain:number}>}
|
* @type {Array<{insert:Array<any>}|{delete:number}|{retain:number}>}
|
||||||
*/
|
*/
|
||||||
const delta = []
|
const delta = []
|
||||||
/**
|
|
||||||
* @type {Map<string,{ action: 'add' | 'update' | 'delete', oldValue: any}>}
|
|
||||||
*/
|
|
||||||
const keys = new Map()
|
|
||||||
changes = {
|
changes = {
|
||||||
added, deleted, delta, keys
|
added,
|
||||||
|
deleted,
|
||||||
|
delta,
|
||||||
|
keys: this.keys
|
||||||
}
|
}
|
||||||
const changed = /** @type Set<string|null> */ (this.transaction.changed.get(target))
|
const changed = /** @type Set<string|null> */ (this.transaction.changed.get(target))
|
||||||
if (changed.has(null)) {
|
if (changed.has(null)) {
|
||||||
@ -141,46 +208,6 @@ export class YEvent {
|
|||||||
packOp()
|
packOp()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
changed.forEach(key => {
|
|
||||||
if (key !== null) {
|
|
||||||
const item = /** @type {Item} */ (target._map.get(key))
|
|
||||||
/**
|
|
||||||
* @type {'delete' | 'add' | 'update'}
|
|
||||||
*/
|
|
||||||
let action
|
|
||||||
let oldValue
|
|
||||||
if (this.adds(item)) {
|
|
||||||
let prev = item.left
|
|
||||||
while (prev !== null && this.adds(prev)) {
|
|
||||||
prev = prev.left
|
|
||||||
}
|
|
||||||
if (this.deletes(item)) {
|
|
||||||
if (prev !== null && this.deletes(prev)) {
|
|
||||||
action = 'delete'
|
|
||||||
oldValue = array.last(prev.content.getContent())
|
|
||||||
} else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (prev !== null && this.deletes(prev)) {
|
|
||||||
action = 'update'
|
|
||||||
oldValue = array.last(prev.content.getContent())
|
|
||||||
} else {
|
|
||||||
action = 'add'
|
|
||||||
oldValue = undefined
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (this.deletes(item)) {
|
|
||||||
action = 'delete'
|
|
||||||
oldValue = array.last(/** @type {Item} */ item.content.getContent())
|
|
||||||
} else {
|
|
||||||
return // nop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
keys.set(key, { action, oldValue })
|
|
||||||
}
|
|
||||||
})
|
|
||||||
this._changes = changes
|
this._changes = changes
|
||||||
}
|
}
|
||||||
return /** @type {any} */ (changes)
|
return /** @type {any} */ (changes)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user