Files
yjs/src/utils/AttributionManager.js

139 lines
3.4 KiB
JavaScript
Raw Normal View History

import {
Item, AbstractContent, IdMap // eslint-disable-line
} from '../internals.js'
import * as error from 'lib0/error'
2025-04-28 02:42:06 +02:00
/**
* @typedef {Object} Attribution
* @property {Array<any>} [Attribution.insert]
* @property {number} [Attribution.insertedAt]
* @property {Array<any>} [Attribution.suggest]
* @property {number} [Attribution.suggestedAt]
* @property {Array<any>} [Attribution.delete]
* @property {number} [Attribution.deletedAt]
* @property {{ [key: string]: Array<any> }} [Attribution.attributes]
* @property {number} [Attribution.attributedAt]
*/
/**
* @param {Array<import('./IdMap.js').AttributionItem<any>>?} attrs
* @param {boolean} deleted - whether the attributed item is deleted
* @return {Attribution?}
*/
export const createAttributionFromAttrs = (attrs, deleted) => {
/**
* @type {Attribution?}
*/
let attribution = null
if (attrs != null) {
attribution = {}
if (deleted) {
attribution.delete = []
} else {
attribution.insert = []
}
attrs.forEach(attr => {
switch (attr.name) {
case 'insert':
case 'delete':
case 'suggest': {
const as = /** @type {import('../utils/Delta.js').Attribution} */ (attribution)
const ls = as[attr.name] = as[attr.name] ?? []
ls.push(attr.val)
break
}
default: {
if (attr.name[0] !== '_') {
/** @type {any} */ (attribution)[attr.name] = attr.val
}
}
}
})
}
return attribution
}
/**
* @template T
*/
export class AttributedContent {
/**
* @param {AbstractContent} content
* @param {boolean} deleted
2025-04-28 02:42:06 +02:00
* @param {Array<import('./IdMap.js').AttributionItem<T>> | null} attrs
*/
constructor (content, deleted, attrs) {
this.content = content
this.deleted = deleted
this.attrs = attrs
}
}
/**
* Abstract class for associating Attributions to content / changes
*/
export class AbstractAttributionManager {
/**
* @param {Array<AttributedContent<any>>} _contents
* @param {Item} _item
*/
readContent (_contents, _item) {
error.methodUnimplemented()
}
}
/**
* Abstract class for associating Attributions to content / changes
*
* @implements AbstractAttributionManager
*/
export class TwosetAttributionManager {
/**
* @param {IdMap<any>} inserts
* @param {IdMap<any>} deletes
*/
constructor (inserts, deletes) {
this.inserts = inserts
this.deletes = deletes
}
/**
* @param {Array<AttributedContent<any>>} contents
* @param {Item} item
*/
readContent (contents, item) {
const deleted = item.deleted
const slice = (deleted ? this.deletes : this.inserts).slice(item.id, item.length)
let content = slice.length === 1 ? item.content : item.content.copy()
slice.forEach(s => {
const c = content
if (s.len < c.getLength()) {
content = c.splice(s.len)
}
if (!deleted || s.attrs != null) {
contents.push(new AttributedContent(c, deleted, s.attrs))
}
})
}
}
/**
* Abstract class for associating Attributions to content / changes
*
* @implements AbstractAttributionManager
*/
export class NoAttributionsManager {
/**
* @param {Array<AttributedContent<any>>} contents
* @param {Item} item
*/
readContent (contents, item) {
if (!item.deleted) {
contents.push(new AttributedContent(item.content, false, null))
}
}
}
export const noAttributionsManager = new NoAttributionsManager()