add an simple attributions example

This commit is contained in:
Kevin Jahns
2025-04-29 23:29:02 +02:00
parent 527e382f8a
commit af2ccc741f
2 changed files with 78 additions and 2 deletions

View File

@@ -23,6 +23,7 @@ import {
typeListGetContent typeListGetContent
} from '../internals.js' } from '../internals.js'
import * as delta from '../utils/Delta.js'
import * as error from 'lib0/error' import * as error from 'lib0/error'
import * as array from 'lib0/array' import * as array from 'lib0/array'
@@ -397,11 +398,15 @@ export class YXmlFragment extends AbstractType {
* @return {{ children: import('../utils/Delta.js').ArrayDelta<Array<import('./AbstractType.js').YXmlDeepContent>> }} * @return {{ children: import('../utils/Delta.js').ArrayDelta<Array<import('./AbstractType.js').YXmlDeepContent>> }}
*/ */
getContentDeep (am) { getContentDeep (am) {
const { children: origChildren } = this.getContent() const { children: origChildren } = this.getContent(am)
/** /**
* @type {import('../utils/Delta.js').ArrayDelta<Array<import('./AbstractType.js').YXmlDeepContent>>} * @type {import('../utils/Delta.js').ArrayDelta<Array<import('./AbstractType.js').YXmlDeepContent>>}
*/ */
const children = origChildren.map(d => /** @type {any} */ (d instanceof AbstractType ? d.getContentDeep(am) : d)) const children = origChildren.map(d => /** @type {any} */ (
d instanceof delta.InsertOp && d.insert instanceof Array
? new delta.InsertOp(d.insert.map(e => e instanceof AbstractType ? e.getContentDeep(am) : e), d.attributes, d.attribution)
: d
))
return { children } return { children }
} }

View File

@@ -342,3 +342,74 @@ export const testElementAttributedContentViaDiffer = _tc => {
}) })
}) })
} }
/**
* @param {t.TestCase} _tc
*/
export const testAttributionManagerSimpleExample = _tc => {
const ydoc = new Y.Doc()
// create some initial content
ydoc.getXmlFragment().insert(0, [new Y.XmlText('hello world')])
const ydocFork = new Y.Doc()
Y.applyUpdate(ydocFork, Y.encodeStateAsUpdate(ydoc))
// modify the fork
// append a span element
ydocFork.getXmlFragment().insert(1, [new Y.XmlElement('span')])
const ytext = /** @type {Y.XmlText} */ (ydocFork.getXmlFragment().get(0))
// make "hello" italic
ytext.format(0, 5, { italic: true })
ytext.insert(11, '!')
// highlight the changes
console.log(JSON.stringify(ydocFork.getXmlFragment().getContentDeep(Y.createAttributionManagerFromDiff(ydoc, ydocFork)), null, 2))
/* =>
{
"children": {
"ops": [
{
"insert": [
{
"ops": [
{
"insert": "hello",
"attributes": {
"italic": true
},
"attribution": {
"attributes": {
"italic": [] -- the attribute "italic" was changed
}
}
},
{
"insert": " world" -- "world" remains unchanged
},
{
"insert": "!",
"attribution": {
"insert": [] -- "!" was inserted
}
}
]
}
]
},
{
"insert": [
{
"nodeName": "span",
"children": {
"ops": []
},
"attributes": {}
}
],
"attribution": {
"insert": [] -- A <span/> tag was inserted
}
}
]
}
}
*/
}