@seed-hypermedia/client 0.0.132 → 0.0.133

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,10 @@
1
+ import {
2
+ parseMarkdown
3
+ } from "./chunk-4ELQ4LJL.mjs";
4
+ import {
5
+ blocksToMarkdown
6
+ } from "./chunk-QFIEZZYJ.mjs";
7
+
1
8
  // src/block-diff.ts
2
9
  function createBlocksMap(nodes, parentId = "") {
3
10
  const result = {};
@@ -370,6 +377,144 @@ function hmBlockNodeToBlockNode(node) {
370
377
  children: (node.children || []).map(hmBlockNodeToBlockNode)
371
378
  };
372
379
  }
380
+ var TABLE_PARTS = /* @__PURE__ */ new Set(["Table", "TableColumn", "TableRow"]);
381
+ function stableJson(value) {
382
+ if (Array.isArray(value)) return "[" + value.map(stableJson).join(",") + "]";
383
+ if (value && typeof value === "object") {
384
+ const map = value;
385
+ const keys = Object.keys(map).filter((key) => map[key] !== void 0).sort();
386
+ return "{" + keys.map((key) => JSON.stringify(key) + ":" + stableJson(map[key])).join(",") + "}";
387
+ }
388
+ return JSON.stringify(value) ?? "null";
389
+ }
390
+ function nativeIpfsLink(link) {
391
+ const gateway = "https://ipfs.io/ipfs/";
392
+ return link.startsWith(gateway) ? "ipfs://" + link.slice(gateway.length) : link;
393
+ }
394
+ function blockKey(block, own = false) {
395
+ const annotations = (block.annotations || []).map(canonicalAnnotation).filter(hasRange).map((annotation) => stableJson(annotation)).sort();
396
+ return stableJson([
397
+ block.type,
398
+ block.text || "",
399
+ nativeIpfsLink(block.link || ""),
400
+ annotations,
401
+ own ? "" : block.childrenType || "",
402
+ block.language || "",
403
+ block.attributes || {}
404
+ ]);
405
+ }
406
+ function eachBlock(nodes, visit) {
407
+ for (const node of nodes) {
408
+ visit(node.block);
409
+ eachBlock(node.children, visit);
410
+ }
411
+ }
412
+ function markdownUpdateTree(oldContent, written) {
413
+ const oldNodes = oldContent.map(toAPIBlockNode);
414
+ const tree = rebindTableIdentities(oldNodes, written);
415
+ const stored = /* @__PURE__ */ new Map();
416
+ eachBlock(oldContent.map(hmBlockNodeToBlockNode), (block) => {
417
+ if (block.id && !stored.has(block.id)) stored.set(block.id, block);
418
+ });
419
+ const readsAs = /* @__PURE__ */ new Map();
420
+ const readsAsOwn = /* @__PURE__ */ new Map();
421
+ for (const mentionLabel of [() => "", void 0]) {
422
+ try {
423
+ const markdown = blocksToMarkdown({ metadata: {}, content: oldContent }, {
424
+ ipfsGateway: false,
425
+ mentionLabel
426
+ });
427
+ eachBlock(rebindTableIdentities(oldNodes, parseMarkdown(markdown).tree), (block) => {
428
+ if (!stored.has(block.id)) return;
429
+ readsAs.set(block.id, (readsAs.get(block.id) || /* @__PURE__ */ new Set()).add(blockKey(block)));
430
+ readsAsOwn.set(block.id, (readsAsOwn.get(block.id) || /* @__PURE__ */ new Set()).add(blockKey(block, true)));
431
+ });
432
+ } catch {
433
+ }
434
+ }
435
+ const present = /* @__PURE__ */ new Set();
436
+ eachBlock(tree, (block) => present.add(block.id));
437
+ const matchable = (block) => !TABLE_PARTS.has(block.type) && !!(block.text || block.link);
438
+ const unclaimed = /* @__PURE__ */ new Map();
439
+ for (const [id, block] of Array.from(stored)) {
440
+ if (present.has(id) || !matchable(block)) continue;
441
+ for (const key of Array.from(readsAsOwn.get(id) || [])) {
442
+ const ids = unclaimed.get(key);
443
+ if (ids) ids.push(id);
444
+ else unclaimed.set(key, [id]);
445
+ }
446
+ }
447
+ const claim = (key) => {
448
+ const ids = unclaimed.get(key) || [];
449
+ while (ids.length) {
450
+ const id = ids.shift();
451
+ if (!present.has(id)) {
452
+ present.add(id);
453
+ return id;
454
+ }
455
+ }
456
+ return void 0;
457
+ };
458
+ const walk = (nodes) => nodes.map((node) => {
459
+ var _a;
460
+ const key = blockKey(node.block);
461
+ let id = node.block.id;
462
+ if (!stored.has(id) && matchable(node.block)) id = claim(blockKey(node.block, true)) ?? id;
463
+ const block = ((_a = readsAs.get(id)) == null ? void 0 : _a.has(key)) ? stored.get(id) : id === node.block.id ? node.block : { ...node.block, id };
464
+ return { block, children: walk(node.children) };
465
+ });
466
+ return walk(tree);
467
+ }
468
+ function longestOrdered(ids, rank) {
469
+ const tails = [];
470
+ for (const id of ids) {
471
+ const value = rank.get(id);
472
+ let low = 0;
473
+ let high = tails.length;
474
+ while (low < high) {
475
+ const mid = low + high >> 1;
476
+ if (tails[mid] < value) low = mid + 1;
477
+ else high = mid;
478
+ }
479
+ tails[low] = value;
480
+ }
481
+ return tails.length;
482
+ }
483
+ function describeContentChange(oldNodes, newTree, ops) {
484
+ const place = (nodes) => {
485
+ const placed = /* @__PURE__ */ new Map();
486
+ const walk = (list, parent) => list.forEach((node, index) => {
487
+ var _a;
488
+ if (!((_a = node.block) == null ? void 0 : _a.id) || placed.has(node.block.id)) return;
489
+ placed.set(node.block.id, { parent, index });
490
+ walk(node.children || [], node.block.id);
491
+ });
492
+ walk(nodes, "");
493
+ return placed;
494
+ };
495
+ const before = place(oldNodes);
496
+ const after = place(newTree);
497
+ let added = 0;
498
+ let moved = 0;
499
+ const kept = /* @__PURE__ */ new Map();
500
+ for (const [id, now] of Array.from(after)) {
501
+ const was = before.get(id);
502
+ if (!was) added++;
503
+ else if (was.parent !== now.parent) moved++;
504
+ else kept.set(now.parent, [...kept.get(now.parent) || [], id]);
505
+ }
506
+ for (const ids of Array.from(kept.values())) {
507
+ const rank = new Map(ids.map((id) => [id, before.get(id).index]));
508
+ moved += ids.length - longestOrdered(ids, rank);
509
+ }
510
+ let deleted = 0;
511
+ for (const id of Array.from(before.keys())) if (!after.has(id)) deleted++;
512
+ let rewritten = 0;
513
+ for (const op of ops) {
514
+ if (op.type === "ReplaceBlock" && before.has(String(op.block.id))) rewritten++;
515
+ }
516
+ return { blocks: before.size, added, rewritten, moved, deleted };
517
+ }
373
518
 
374
519
  export {
375
520
  createBlocksMap,
@@ -377,5 +522,7 @@ export {
377
522
  rebindTableIdentities,
378
523
  computeReplaceOps,
379
524
  toAPIBlockNode,
380
- hmBlockNodeToBlockNode
525
+ hmBlockNodeToBlockNode,
526
+ markdownUpdateTree,
527
+ describeContentChange
381
528
  };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  documentUpdateBlobs
3
- } from "./chunk-6QBB3P3Q.mjs";
3
+ } from "./chunk-KAZNQFXV.mjs";
4
4
  import {
5
5
  resolveDocumentState
6
6
  } from "./chunk-QS6PIRUE.mjs";