Compare commits

...

2 Commits

Author SHA1 Message Date
Kevin Jahns
1d4f2e5435 13.6.10 2023-11-21 12:29:49 +01:00
Kevin Jahns
2c0daeb071 implement snapshot API for yxml.getAttributes. implements #543 2023-11-21 12:24:21 +01:00
6 changed files with 52 additions and 6 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "yjs", "name": "yjs",
"version": "13.6.9", "version": "13.6.10",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "yjs", "name": "yjs",
"version": "13.6.9", "version": "13.6.10",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"lib0": "^0.2.86" "lib0": "^0.2.86"

View File

@@ -1,6 +1,6 @@
{ {
"name": "yjs", "name": "yjs",
"version": "13.6.9", "version": "13.6.10",
"description": "Shared Editing Library", "description": "Shared Editing Library",
"main": "./dist/yjs.cjs", "main": "./dist/yjs.cjs",
"module": "./dist/yjs.mjs", "module": "./dist/yjs.mjs",

View File

@@ -52,6 +52,7 @@ export {
getItem, getItem,
typeListToArraySnapshot, typeListToArraySnapshot,
typeMapGetSnapshot, typeMapGetSnapshot,
typeMapGetAllSnapshot,
createDocFromSnapshot, createDocFromSnapshot,
iterateDeletedStructs, iterateDeletedStructs,
applyUpdate, applyUpdate,

View File

@@ -925,6 +925,34 @@ export const typeMapGetSnapshot = (parent, key, snapshot) => {
return v !== null && isVisible(v, snapshot) ? v.content.getContent()[v.length - 1] : undefined return v !== null && isVisible(v, snapshot) ? v.content.getContent()[v.length - 1] : undefined
} }
/**
* @param {AbstractType<any>} parent
* @param {Snapshot} snapshot
* @return {Object<string,Object<string,any>|number|null|Array<any>|string|Uint8Array|AbstractType<any>|undefined>}
*
* @private
* @function
*/
export const typeMapGetAllSnapshot = (parent, snapshot) => {
/**
* @type {Object<string,any>}
*/
const res = {}
parent._map.forEach((value, key) => {
/**
* @type {Item|null}
*/
let v = value
while (v !== null && (!snapshot.sv.has(v.id.client) || v.id.clock >= (snapshot.sv.get(v.id.client) || 0))) {
v = v.left
}
if (v !== null && isVisible(v, snapshot)) {
res[key] = v.content.getContent()[v.length - 1]
}
})
return res
}
/** /**
* @param {Map<string,Item>} map * @param {Map<string,Item>} map
* @return {IterableIterator<Array<any>>} * @return {IterableIterator<Array<any>>}

View File

@@ -8,9 +8,10 @@ import {
typeMapSet, typeMapSet,
typeMapGet, typeMapGet,
typeMapGetAll, typeMapGetAll,
typeMapGetAllSnapshot,
typeListForEach, typeListForEach,
YXmlElementRefID, YXmlElementRefID,
YXmlText, ContentType, AbstractType, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Item // eslint-disable-line Snapshot, YXmlText, ContentType, AbstractType, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Item // eslint-disable-line
} from '../internals.js' } from '../internals.js'
/** /**
@@ -192,12 +193,13 @@ export class YXmlElement extends YXmlFragment {
/** /**
* Returns all attribute name/value pairs in a JSON Object. * Returns all attribute name/value pairs in a JSON Object.
* *
* @param {Snapshot} [snapshot]
* @return {{ [Key in Extract<keyof KV,string>]?: KV[Key]}} A JSON Object that describes the attributes. * @return {{ [Key in Extract<keyof KV,string>]?: KV[Key]}} A JSON Object that describes the attributes.
* *
* @public * @public
*/ */
getAttributes () { getAttributes (snapshot) {
return /** @type {any} */ (typeMapGetAll(this)) return /** @type {any} */ (snapshot ? typeMapGetAllSnapshot(this, snapshot) : typeMapGetAll(this))
} }
/** /**

View File

@@ -14,6 +14,21 @@ export const testBasic = _tc => {
t.assert(restored.getText().toString() === 'world!') t.assert(restored.getText().toString() === 'world!')
} }
/**
* @param {t.TestCase} _tc
*/
export const testBasicXmlAttributes = _tc => {
const ydoc = new Y.Doc({ gc: false })
const yxml = ydoc.getMap().set('el', new Y.XmlElement('div'))
const snapshot1 = Y.snapshot(ydoc)
yxml.setAttribute('a', '1')
const snapshot2 = Y.snapshot(ydoc)
yxml.setAttribute('a', '2')
t.compare(yxml.getAttributes(), { a: '2' })
t.compare(yxml.getAttributes(snapshot2), { a: '1' })
t.compare(yxml.getAttributes(snapshot1), {})
}
/** /**
* @param {t.TestCase} _tc * @param {t.TestCase} _tc
*/ */