From af2ccc741fbb9d1a2cfa86aec3cd9002135be1af Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Tue, 29 Apr 2025 23:29:02 +0200 Subject: [PATCH] add an simple attributions example --- src/types/YXmlFragment.js | 9 +++-- tests/y-xml.tests.js | 71 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/src/types/YXmlFragment.js b/src/types/YXmlFragment.js index c60f11ce..3d33c9da 100644 --- a/src/types/YXmlFragment.js +++ b/src/types/YXmlFragment.js @@ -23,6 +23,7 @@ import { typeListGetContent } from '../internals.js' +import * as delta from '../utils/Delta.js' import * as error from 'lib0/error' import * as array from 'lib0/array' @@ -397,11 +398,15 @@ export class YXmlFragment extends AbstractType { * @return {{ children: import('../utils/Delta.js').ArrayDelta> }} */ getContentDeep (am) { - const { children: origChildren } = this.getContent() + const { children: origChildren } = this.getContent(am) /** * @type {import('../utils/Delta.js').ArrayDelta>} */ - 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 } } diff --git a/tests/y-xml.tests.js b/tests/y-xml.tests.js index c3d0fd33..08c2326e 100644 --- a/tests/y-xml.tests.js +++ b/tests/y-xml.tests.js @@ -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 tag was inserted + } + } + ] + } +} +*/ +} +