expose getPathTo and support custom attributionManager/renderer in relativePosition transformer

This commit is contained in:
Kevin Jahns
2025-12-30 19:18:41 +01:00
parent 95638112d6
commit 2daa2ae18b
4 changed files with 17 additions and 23 deletions

6
package-lock.json generated
View File

@@ -649,7 +649,6 @@
"integrity": "sha512-NZyJarBfL7nWwIq+FDL6Zp/yHEhePMNnnJ0y3qfieCrmNvYct8uvtiV41UvlSe6apAfk0fY1FbWx+NwfmpvtTg==",
"dev": true,
"license": "MIT",
"peer": true,
"bin": {
"acorn": "bin/acorn"
},
@@ -1547,7 +1546,6 @@
"deprecated": "This version is no longer supported. Please see https://eslint.org/version-support for other options.",
"dev": true,
"license": "MIT",
"peer": true,
"dependencies": {
"@eslint-community/eslint-utils": "^4.2.0",
"@eslint-community/regexpp": "^4.6.1",
@@ -1755,7 +1753,6 @@
"integrity": "sha512-whOE1HFo/qJDyX4SnXzP4N6zOWn79WhnCUY/iDR0mPfQZO8wcYE4JClzI2oZrhBnnMUCBCHZhO6VQyoBU95mZA==",
"dev": true,
"license": "MIT",
"peer": true,
"dependencies": {
"@rtsao/scc": "^1.1.0",
"array-includes": "^3.1.9",
@@ -1826,7 +1823,6 @@
"integrity": "sha512-jDex9s7D/Qial8AGVIHq4W7NswpUD5DPDL2RH8Lzd9EloWUuvUkHfv4FRLMipH5q2UtyurorBkPeNi1wVWNh3Q==",
"dev": true,
"license": "MIT",
"peer": true,
"dependencies": {
"builtins": "^5.0.1",
"eslint-plugin-es": "^4.1.0",
@@ -1889,7 +1885,6 @@
"integrity": "sha512-57Zzfw8G6+Gq7axm2Pdo3gW/Rx3h9Yywgn61uE/3elTCOePEHVrn2i5CdfBwA1BLK0Q0WqctICIUSqXZW/VprQ==",
"dev": true,
"license": "ISC",
"peer": true,
"engines": {
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
},
@@ -1906,7 +1901,6 @@
"integrity": "sha512-Qteup0SqU15kdocexFNAJMvCJEfa2xUKNV4CC1xsVMrIIqEy3SQ/rqyxCWNzfrd3/ldy6HMlD2e0JDVpDg2qIA==",
"dev": true,
"license": "MIT",
"peer": true,
"dependencies": {
"array-includes": "^3.1.8",
"array.prototype.findlast": "^1.2.5",

View File

@@ -123,7 +123,8 @@ export {
readIdMap,
readIdSet,
decodeIdMap,
diffDocsToDelta
diffDocsToDelta,
getPathTo
} from './internals.js'
const glo = /** @type {any} */ (typeof globalThis !== 'undefined'

View File

@@ -2,7 +2,10 @@ import {
diffIdSet,
mergeIdSets,
noAttributionsManager,
AbstractAttributionManager, Item, AbstractType, Transaction, AbstractStruct // eslint-disable-line
Doc, AbstractAttributionManager, Item, AbstractType, Transaction, AbstractStruct, // eslint-disable-line
createAbsolutePositionFromRelativePosition,
createRelativePosition,
AbsolutePosition
} from '../internals.js'
import * as map from 'lib0/map'
@@ -187,28 +190,24 @@ export class YEvent {
*
* @param {_YType} parent
* @param {_YType} child target
* @param {AbstractAttributionManager} am
* @return {Array<string|number>} Path to the target
*
* @private
* @function
*/
const getPathTo = (parent, child) => {
export const getPathTo = (parent, child, am = noAttributionsManager) => {
const path = []
const doc = /** @type {Doc} */ (parent.doc)
while (child._item !== null && child !== parent) {
if (child._item.parentSub !== null) {
// parent is map-ish
path.unshift(child._item.parentSub)
} else {
const parent = /** @type {import('../utils/types.js').YType} */ (child._item.parent)
// parent is array-ish
let i = 0
let c = /** @type {import('../utils/types.js').YType} */ (child._item.parent)._start
while (c !== child._item && c !== null) {
if (!c.deleted && c.countable) {
i += c.length
}
c = c.right
}
path.unshift(i)
const apos = /** @type {AbsolutePosition} */ (createAbsolutePositionFromRelativePosition(createRelativePosition(parent, child._item.id), doc, false, am))
path.unshift(apos.index)
}
child = /** @type {_YType} */ (child._item.parent)
}

View File

@@ -2,14 +2,14 @@
<!DOCTYPE html>
<html>
<head>
<title>Testing yjs</title>
<title>Testing @y/y</title>
<script type="importmap">
{
"imports": {
"yjs": "./src/index.js",
"yjs/internals": "./src/internals.js",
"yjs/testHelper": "./tests/testHelper.js",
"yjs/package.json": "./package.json",
"@y/y": "./dist/yjs.js",
"@y/y/internals": "./dist/internals.js",
"@y/y/testHelper": "./dist/testHelper.js",
"@y/y/package.json": "./package.json",
"lib0/package.json": "./node_modules/lib0/package.json",
"lib0": "./node_modules/lib0/index.js",
"lib0/array.js": "./node_modules/lib0/array.js",