@y/y
Version:
Shared Editing Library
499 lines (471 loc) • 19.1 kB
JavaScript
import { ObservableV2 } from 'lib0/observable'
import * as encoding from 'lib0/encoding'
import { getItemCleanStart } from './transaction-helpers.js'
import { diffIdSet, createInsertSetFromStructStore, createDeleteSetFromStructStore, insertIntoIdSet, mergeIdSets, intersectSets, createIdSet, writeIdSet, createIdMapFromIdSet, insertIntoIdMap, diffIdMap, createIdMap, mergeIdMaps, intersectMaps, createMaybeAttrRange, createContentAttribute } from './ids.js'
import { ContentDeleted, ContentFormat } from '../structs/Item.js'
import { createID } from './ID.js'
import { writeStructsFromIdSet } from './encoding-helpers.js'
import { applyUpdate, encodeStateAsUpdate } from './encoding.js'
import { UpdateEncoderV1 } from './UpdateEncoder.js'
import { transact } from './Transaction.js'
import { UndoManager, StackItem } from './UndoManager.js'
import { $attributionManager, AttributedContent } from './attribution-manager-helpers.js'
export { noAttributionsManager, NoAttributionsManager, AbstractAttributionManager, $attributionManager } from './attribution-manager-helpers.js'
/**
* @implements AbstractAttributionManager
*
* @extends {ObservableV2<{change:(idset:IdSet,origin:any,local:boolean)=>void}>}
*/
export class TwosetAttributionManager extends ObservableV2 {
/**
* @param {IdMap<any>} inserts
* @param {IdMap<any>} deletes
*/
constructor (inserts, deletes) {
super()
this.inserts = inserts
this.deletes = deletes
}
get $type () { return $attributionManager }
/**
* @param {Array<AttributedContent<any>>} contents - where to write the result
* @param {number} client
* @param {number} clock
* @param {boolean} deleted
* @param {AbstractContent} content
* @param {0|1|2} shouldRender - whether this should render or just result in a `retain` operation
*/
readContent (contents, client, clock, deleted, content, shouldRender) {
const slice = (deleted ? this.deletes : this.inserts).slice(client, clock, content.getLength())
content = slice.length === 1 ? content : content.copy()
slice.forEach(s => {
const c = content
if (s.len < c.getLength()) {
content = c.splice(s.len)
}
if (!deleted || s.attrs != null || shouldRender) {
contents.push(new AttributedContent(c, s.clock, deleted, s.attrs, shouldRender))
}
})
}
/**
* @param {Item} item
* @return {number}
*/
contentLength (item) {
if (!item.content.isCountable()) {
return 0
} else if (!item.deleted) {
return item.length
} else {
return this.deletes.sliceId(item.id, item.length).reduce((len, s) => s.attrs != null ? len + s.len : len, 0)
}
}
}
/**
* @param {StructStore} store
* @param {number} client
* @param {number} clock
* @param {number} len
*/
const getItemContent = (store, client, clock, len) => {
// Retrieved item is never more fragmented than the newer item.
const prevItem = store.getItem(createID(client, clock))
const diffStart = clock - prevItem.id.clock
let content = prevItem.length > 1 ? prevItem.content.copy() : prevItem.content
// trim itemContent to the correct size.
if (diffStart > 0) {
content = content.splice(diffStart)
}
if (len < content.getLength()) {
content.splice(len)
}
return content
}
/**
* @param {Transaction?} tr - only specify this if you want to fill the content of deleted content
* @param {DiffAttributionManager} am
* @param {ID} start
* @param {ID} end
* @param {boolean} collectAll - collect as many items as possible. Accept adding redundant changes.
*/
const collectSuggestedChanges = (tr, am, start, end, collectAll) => {
const inserts = createIdSet()
const deletes = createIdSet()
const store = am._nextDoc.store
/**
* make sure to collect suggestions until all formats are closed
* @type {Set<string>}
*/
const openedCollectedFormats = new Set()
/**
* @type {Item?}
*/
let item = store.getItem(start)
const endItem = start === end ? item : (end == null ? null : store.getItem(end))
// walk to the left and find first un-attributed change that is rendered
while (item.left != null) {
item = item.left
if (item.content instanceof ContentFormat && item.content.value == null) {
item = item.right
break
}
if (!item.deleted) {
const slice = am.inserts.slice(item.id.client, item.id.clock, item.length)
if (slice.some(s => s.attrs === null)) {
for (let i = slice.length - 1; i >= 0; i--) {
const s = slice[i]
if (s.attrs == null) break
inserts.add(item.id.client, s.clock, s.len)
}
item = item.right
break
}
}
}
let foundEndItem = false
// eslint-disable-next-line
itemLoop: while (item != null) {
const itemClient = item.id.client
const slice = (item.deleted ? am.deletes : am.inserts).slice(itemClient, item.id.clock, item.length)
foundEndItem ||= item === endItem
if (item.deleted) {
// item probably gc'd content. Need to split item and fill with content again
for (let i = slice.length - 1; i >= 0; i--) {
const s = slice[i]
if (s.attrs != null || collectAll) {
deletes.add(itemClient, s.clock, s.len)
if (collectAll) {
// in case item has been added and deleted this might be necessary. the forked document
// will automatically filter this if it doesn't have it already.
inserts.add(itemClient, s.clock, s.len)
}
}
if (tr != null) {
const splicedItem = getItemCleanStart(tr, createID(itemClient, s.clock))
if (s.attrs != null) {
splicedItem.content = getItemContent(am._prevDocStore, itemClient, s.clock, s.len)
}
}
}
} else {
if (item.content instanceof ContentFormat) {
const { key, value } = item.content
if (value == null) {
openedCollectedFormats.delete(key)
} else {
openedCollectedFormats.add(key)
}
}
for (let i = 0; i < slice.length; i++) {
const s = slice[i]
if (s.attrs != null) {
inserts.add(itemClient, s.clock, s.len)
} else if (foundEndItem && openedCollectedFormats.size === 0) {
// eslint-disable-next-line
break itemLoop
}
}
}
item = item.right
}
return { inserts, deletes }
}
export class Attributions {
constructor () {
this.inserts = createIdMap()
this.deletes = createIdMap()
}
}
/**
* @param {IdMap<any>|undefined} attrs
* @param {IdSet} slice
*
*/
const extractAttributions = (attrs, slice) => attrs == null ? createIdMapFromIdSet(slice, []) : mergeIdMaps([intersectMaps(attrs, slice), createIdMapFromIdSet(slice, [])])
/**
* @implements AbstractAttributionManager
*
* @extends {ObservableV2<{change:(idset:IdSet,origin:any,local:boolean)=>void}>}
*/
export class DiffAttributionManager extends ObservableV2 {
/**
* @param {Doc} prevDoc
* @param {Doc} nextDoc
* @param {Object} [options] - options for the attribution manager
* @param {Attributions?} [options.attrs] - the attributes to apply to the diff
*/
constructor (prevDoc, nextDoc, { attrs = null } = {}) {
super()
const _nextDocInserts = createInsertSetFromStructStore(nextDoc.store, false) // unmaintained
const _prevDocInserts = createInsertSetFromStructStore(prevDoc.store, false) // unmaintained
const nextDocDeletes = createDeleteSetFromStructStore(nextDoc.store) // maintained
const prevDocDeletes = createDeleteSetFromStructStore(prevDoc.store) // maintained
this.inserts = extractAttributions(attrs?.inserts, diffIdSet(_nextDocInserts, _prevDocInserts))
this.deletes = extractAttributions(attrs?.deletes, diffIdSet(nextDocDeletes, prevDocDeletes))
this._prevDoc = prevDoc
this._prevDocStore = prevDoc.store
this._nextDoc = nextDoc
// update before observer calls fired
this._nextBOH = nextDoc.on('beforeObserverCalls', tr => {
// update inserts
const diffInserts = diffIdSet(tr.insertSet, _prevDocInserts)
insertIntoIdMap(this.inserts, extractAttributions(attrs?.inserts, diffInserts))
// update deletes
const diffDeletes = diffIdSet(diffIdSet(tr.deleteSet, prevDocDeletes), this.inserts)
insertIntoIdMap(this.deletes, extractAttributions(attrs?.deletes, diffDeletes))
// @todo fire update ranges on `diffInserts` and `diffDeletes`
})
this._prevBOH = prevDoc.on('beforeObserverCalls', tr => {
insertIntoIdSet(_prevDocInserts, tr.insertSet)
insertIntoIdSet(prevDocDeletes, tr.deleteSet)
if (tr.insertSet.clients.size < 2) {
tr.insertSet.forEach((attrRange, client) => {
this.inserts.delete(client, attrRange.clock, attrRange.len)
})
} else {
this.inserts = diffIdMap(this.inserts, tr.insertSet)
}
// insertIntoIdMap(this.deletes, createIdMapFromIdSet(intersectSets(tr.deleteSet, this.deletes), [createAttributionItem('acceptDelete', 'unknown')]))
if (tr.deleteSet.clients.size < 2) {
tr.deleteSet.forEach((attrRange, client) => {
this.deletes.delete(client, attrRange.clock, attrRange.len)
})
} else {
this.deletes = diffIdMap(this.deletes, tr.deleteSet)
}
// fire event of "changed" attributions. exclude items that were added & deleted in the same
// transaction
this.emit('change', [diffIdSet(mergeIdSets([tr.insertSet, tr.deleteSet]), intersectSets(tr.insertSet, tr.deleteSet)), tr.origin, tr.local])
})
// changes from prevDoc should always flow into suggestionDoc
// changes from suggestionDoc only flow into ydoc if suggestion-mode is disabled
this._prevUpdateListener = prevDoc.on('update', (update, origin) => {
origin !== this && applyUpdate(nextDoc, update)
})
this._ndUpdateListener = nextDoc.on('update', (update, origin, _doc, tr) => {
// only if event is local and suggestion mode is enabled
if (!this.suggestionMode && tr.local && (this.suggestionOrigins == null || this.suggestionOrigins.some(o => o === origin))) {
applyUpdate(prevDoc, update, this)
}
})
this._afterTrListener = nextDoc.on('afterTransaction', (tr) => {
// apply deletes on attributed deletes (content that is already deleted, but is rendered by
// the attribution manager)
if (!this.suggestionMode && tr.local && (this.suggestionOrigins == null || this.suggestionOrigins.some(o => o === tr.origin))) {
const attributedDeletes = tr.meta.get('attributedDeletes')
if (attributedDeletes != null) {
transact(prevDoc, () => {
// apply attributed deletes if there are any
const ds = new UpdateEncoderV1()
encoding.writeVarUint(ds.restEncoder, 0) // encode 0 structs
writeIdSet(ds, attributedDeletes)
applyUpdate(prevDoc, ds.toUint8Array())
}, this)
}
}
})
this.suggestionMode = true
/**
* Optionally limit origins that may sync changes to the main doc if suggestion-mode is
* disabled.
*
* @type {Array<any>?}
*/
this.suggestionOrigins = null
this._destroyHandler = nextDoc.on('destroy', this.destroy.bind(this))
prevDoc.on('destroy', this._destroyHandler)
}
get $type () { return $attributionManager }
destroy () {
super.destroy()
this._nextDoc.off('destroy', this._destroyHandler)
this._prevDoc.off('destroy', this._destroyHandler)
this._nextDoc.off('beforeObserverCalls', this._nextBOH)
this._prevDoc.off('beforeObserverCalls', this._prevBOH)
this._prevDoc.off('update', this._prevUpdateListener)
this._nextDoc.off('update', this._ndUpdateListener)
this._nextDoc.off('afterTransaction', this._afterTrListener)
}
acceptAllChanges () {
applyUpdate(this._prevDoc, encodeStateAsUpdate(this._nextDoc))
}
rejectAllChanges () {
this._prevDoc.transact(tr => {
applyUpdate(this._prevDoc, encodeStateAsUpdate(this._nextDoc))
const um = new UndoManager(this._prevDoc)
um.undoStack.push(new StackItem(tr.insertSet, tr.deleteSet))
um.undo()
um.destroy()
})
}
/**
* @param {ID} start
* @param {ID} end
*/
acceptChanges (start, end = start) {
const { inserts, deletes } = collectSuggestedChanges(null, this, start, end, true)
const encoder = new UpdateEncoderV1()
writeStructsFromIdSet(encoder, this._nextDoc.store, inserts)
writeIdSet(encoder, deletes)
applyUpdate(this._prevDoc, encoder.toUint8Array())
}
/**
* @param {ID} start
* @param {ID} end
*/
rejectChanges (start, end = start) {
this._nextDoc.transact(tr => {
const { inserts, deletes } = collectSuggestedChanges(tr, this, start, end, false)
const encoder = new UpdateEncoderV1()
writeStructsFromIdSet(encoder, this._nextDoc.store, inserts)
writeIdSet(encoder, deletes)
const um = new UndoManager(this._nextDoc)
um.undoStack.push(new StackItem(inserts, deletes))
um.undo()
um.destroy()
})
this.acceptChanges(start, end)
}
/**
* @param {Array<AttributedContent<any>>} contents - where to write the result
* @param {number} client
* @param {number} clock
* @param {boolean} deleted
* @param {AbstractContent} _content
* @param {0|1|2} shouldRender - whether this should render or just result in a `retain` operation
*/
readContent (contents, client, clock, deleted, _content, shouldRender) {
const slice = (deleted ? this.deletes : this.inserts).slice(client, clock, _content.getLength())
/**
* @type {AbstractContent?}
*/
let content = slice.length === 1 ? _content : _content.copy()
for (let i = 0; i < slice.length; i++) {
const s = slice[i]
if (content == null || content instanceof ContentDeleted) {
if ((!shouldRender && s.attrs == null) || this.inserts.has(client, s.clock)) {
continue
}
// Retrieved item is never more fragmented than the newer item.
const prevItem = this._prevDocStore.getItem(createID(client, s.clock))
const diffStart = s.clock - prevItem.id.clock
content = prevItem.length > 1 ? prevItem.content.copy() : prevItem.content
// trim itemContent to the correct size.
if (diffStart > 0) {
content = content.splice(diffStart)
}
}
const c = /** @type {AbstractContent} */ (content)
const clen = c.getLength()
if (clen < s.len) {
slice.splice(i + 1, 0, createMaybeAttrRange(s.clock + clen, s.len - clen, s.attrs))
s.len = clen
}
content = s.len < clen ? c.splice(s.len) : null
if (shouldRender || !deleted || s.attrs != null) {
contents.push(new AttributedContent(c, s.clock, deleted, s.attrs, shouldRender))
}
}
}
/**
* @param {Item} item
* @return {number}
*/
contentLength (item) {
if (!item.deleted) {
return item.content.isCountable() ? item.length : 0
}
/**
* @type {Array<AttributedContent<any>>}
*/
const cs = []
this.readContent(cs, item.id.client, item.id.clock, true, item.content, 0)
return cs.reduce((cnt, c) => cnt + ((c.attrs != null && c.content.isCountable()) ? c.content.getLength() : 0), 0)
}
}
/**
* Attribute changes from ydoc1 to ydoc2.
*
* @param {Doc} prevDoc
* @param {Doc} nextDoc
* @param {Object} [options] - options for the attribution manager
* @param {import('./meta.js').ContentMap?} [options.attrs] - the attributes to apply to the diff
*/
export const createAttributionManagerFromDiff = (prevDoc, nextDoc, options) => new DiffAttributionManager(prevDoc, nextDoc, options)
/**
* Intended for projects that used the v13 snapshot feature. With this AttributionManager you can
* read content similar to the previous snapshot api. Requires that `ydoc.gc` is turned off.
*
* @implements AbstractAttributionManager
*
* @extends {ObservableV2<{change:(idset:IdSet,origin:any,local:boolean)=>void}>}
*/
export class SnapshotAttributionManager extends ObservableV2 {
/**
* @param {Snapshot} prevSnapshot
* @param {Snapshot} nextSnapshot
* @param {Object} [options] - options for the attribution manager
* @param {Array<ContentAttribute>} [options.attrs] - the attributes to apply to the diff
*/
constructor (prevSnapshot, nextSnapshot) {
super()
this.prevSnapshot = prevSnapshot
this.nextSnapshot = nextSnapshot
const inserts = createIdMap()
const deletes = createIdMapFromIdSet(diffIdSet(nextSnapshot.ds, prevSnapshot.ds), [createContentAttribute('change', '')])
nextSnapshot.sv.forEach((clock, client) => {
const prevClock = prevSnapshot.sv.get(client) || 0
inserts.add(client, 0, prevClock, []) // content is included in prevSnapshot is rendered without attributes
inserts.add(client, prevClock, clock - prevClock, [createContentAttribute('change', '')]) // content is rendered as "inserted"
})
this.attrs = mergeIdMaps([diffIdMap(inserts, prevSnapshot.ds), deletes])
}
get $type () { return $attributionManager }
/**
* @param {Array<AttributedContent<any>>} contents - where to write the result
* @param {number} client
* @param {number} clock
* @param {boolean} _deleted
* @param {AbstractContent} content
* @param {0|1|2} shouldRender - whether this should render or just result in a `retain` operation
*/
readContent (contents, client, clock, _deleted, content, shouldRender) {
if ((this.nextSnapshot.sv.get(client) ?? 0) <= clock) return // future item that should not be displayed
const slice = this.attrs.slice(client, clock, content.getLength())
content = slice.length === 1 ? content : content.copy()
slice.forEach(s => {
const deleted = this.nextSnapshot.ds.has(client, s.clock)
const nonExistend = (this.nextSnapshot.sv.get(client) ?? 0) <= s.clock
const c = content
if (s.len < c.getLength()) {
content = c.splice(s.len)
}
if (nonExistend) return
if (shouldRender || !deleted || (s.attrs != null && s.attrs.length > 0)) {
let attrsWithoutChange = s.attrs?.filter(attr => attr.name !== 'change') ?? null
if (s.attrs?.length === 0) {
attrsWithoutChange = null
}
contents.push(new AttributedContent(c, s.clock, deleted, attrsWithoutChange, shouldRender))
}
})
}
/**
* @param {Item} item
* @return {number}
*/
contentLength (item) {
return item.content.isCountable()
? (item.deleted
? this.attrs.sliceId(item.id, item.length).reduce((len, s) => s.attrs != null ? len + s.len : len, 0)
: item.length
)
: 0
}
}
/**
* @param {Snapshot} prevSnapshot
* @param {Snapshot} nextSnapshot
*/
export const createAttributionManagerFromSnapshots = (prevSnapshot, nextSnapshot = prevSnapshot) => new SnapshotAttributionManager(prevSnapshot, nextSnapshot)