@contrail/documents 1.20.0-alpha.5ed9686 → 1.20.0-alpha.6beca3a

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.
package/CHANGELOG.md CHANGED
@@ -5,20 +5,25 @@ All notable changes to `@contrail/documents` are documented here.
5
5
  Format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
6
6
  Versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
- ## [1.20.0] - 2026-09-20
8
+ ## [1.20.0] - 2026-09-25
9
+
10
+ A maintenance release on the 1.x line, published under the `v1-maintenance` tag. Carries the
11
+ same container work as 2.4.0, for consumers still on `@contrail/types@^3.4` and Node 20.
9
12
 
10
13
  ### Added
11
14
 
12
15
  - `DocumentContainerService.buildIndex` and `DocumentContainerIndex` — which containers hold which
13
16
  elements, for one document, resolved once and read back by lookup. A container is a frame or a
14
17
  section, described by a `ContainerKind` carrying `elementType`, `nests` and `layer`. An element
15
- belongs to the container whose box covers its measured centre and which is drawn below it.
16
- Nesting gives a path with one container per depth, the later-drawn one winning where two cross.
17
- A container is only ever content of a kind layered behind it, so a frame belongs to a section and
18
- a section never to a frame. Shapes wider and taller than the container, and groups holding it,
19
- are excluded.
18
+ belongs to the container whose box covers its anchor point and which is drawn below it. Nesting
19
+ gives a path with one container per depth, the later-drawn one winning where two cross. A
20
+ container is only ever content of a kind layered behind it, so a frame belongs to a section and a
21
+ section never to a frame. Shapes wider and taller than the container, and groups holding it, are
22
+ excluded.
20
23
  - `SECTION_ELEMENT_TYPE`, `FRAME_ELEMENT_TYPE` and `SHAPE_ELEMENT_TYPES` in
21
24
  `document-element-constants`, so the rule above has one definition rather than one per app.
25
+ - `DocumentContainerUtil` — the container geometry as statics, with `anchorFor` for a caller
26
+ holding an element rather than a measured box.
22
27
  - `positionKeepingAnchor` — where an element has to sit for its drawn corner to land where it did
23
28
  before a resize. Rotation turns about the centre and the centre moves with the size, so a rotated
24
29
  element resized about its stored position drags its corner along with it. The board already makes
@@ -63,6 +68,17 @@ Versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
63
68
  `getBoundingClientRectScaled` above. A scaled element previously measured at its unscaled size
64
69
  and could resolve to the wrong container.
65
70
 
71
+ ## [2.3.0] - 2026-09-21
72
+
73
+ ### Added
74
+
75
+ - `isValidSortIndex(sortIndex)` — whether a value is a key `fractional-indexing` could have produced, so every client writing a sort index is held to one definition of valid rather than each caller inventing its own. `fractional-indexing` keeps its `validateOrderKey` private, so the check asks the generator to mint a key after the candidate: that path validates its bound first and cannot fail for any other reason. The generator's own grammar check ignores the digit alphabet, and accepts `a0!` or `a0 `, so the candidate is screened against the exported `BASE_62_DIGITS` first. Costs ~130ns for a short key and ~350ns for a 387-character one.
76
+
77
+ ### Changed
78
+
79
+ - `allElementsHaveSortIndex` now requires each element's key to be valid, not merely a non-empty string. Callers that treat an unkeyed collection as needing fresh keys — board snapshot restore is the one today — now also re-key a collection whose keys are present but malformed, instead of restoring ordering that cannot sort.
80
+ - `SortOrderErrorMessage.SORT_INDEX_NOT_USABLE` describes the real rule ("sortIndex must be a valid fractional index") rather than "must be a non-empty string". The `SORT_INDEX_NOT_USABLE` code is unchanged.
81
+
66
82
  ## [2.2.1] - 2026-09-15
67
83
 
68
84
  ### Changed
@@ -35,8 +35,8 @@ class DocumentContainerIndex {
35
35
  return members;
36
36
  }
37
37
  containerPathAtPoint(point, elementType) {
38
- const covering = this.containersOfType(elementType).filter((container) => (0, document_container_1.containsPoint)(container.box, point));
39
- return (0, document_container_1.topmostPerDepth)(covering).map((container) => container.id);
38
+ const covering = this.containersOfType(elementType).filter((container) => document_container_1.DocumentContainerUtil.containsPoint(container.box, point));
39
+ return document_container_1.DocumentContainerUtil.topmostPerDepth(covering).map((container) => container.id);
40
40
  }
41
41
  maxDepth(elementType) {
42
42
  return this.containersOfType(elementType).reduce((max, container) => Math.max(max, container.depth + 1), 0);
@@ -14,11 +14,14 @@ export interface MeasuredDocumentElement {
14
14
  box: CoordinateBox | null;
15
15
  drawOrder: number;
16
16
  }
17
- export declare function toCoordinateBox(x: number, y: number, width: number, height: number): CoordinateBox;
18
- export declare function centerOf(box: CoordinateBox): PositionDefinition;
19
- export declare function anchorOf(documentElement: DocumentElement, box: CoordinateBox): PositionDefinition;
20
- export declare function positionKeepingAnchor(documentElement: DocumentElement, newSize: SizeDefinition): PositionDefinition;
21
- export declare function containsPoint(box: CoordinateBox, point: PositionDefinition): boolean;
22
- export declare function encloses(outer: CoordinateBox, inner: CoordinateBox): boolean;
23
- export declare function isWiderAndTallerThan(box: CoordinateBox, other: CoordinateBox): boolean;
24
- export declare function topmostPerDepth(containers: DocumentContainer[]): DocumentContainer[];
17
+ export declare class DocumentContainerUtil {
18
+ static toCoordinateBox(x: number, y: number, width: number, height: number): CoordinateBox;
19
+ static centerOf(box: CoordinateBox): PositionDefinition;
20
+ static anchorOf(documentElement: DocumentElement, box: CoordinateBox): PositionDefinition;
21
+ static positionKeepingAnchor(documentElement: DocumentElement, newSize: SizeDefinition): PositionDefinition;
22
+ static anchorFor(documentElement: DocumentElement, position?: PositionDefinition): PositionDefinition;
23
+ static containsPoint(box: CoordinateBox, point: PositionDefinition): boolean;
24
+ static encloses(outer: CoordinateBox, inner: CoordinateBox): boolean;
25
+ static isWiderAndTallerThan(box: CoordinateBox, other: CoordinateBox): boolean;
26
+ static topmostPerDepth(containers: DocumentContainer[]): DocumentContainer[];
27
+ }
@@ -1,69 +1,75 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toCoordinateBox = toCoordinateBox;
4
- exports.centerOf = centerOf;
5
- exports.anchorOf = anchorOf;
6
- exports.positionKeepingAnchor = positionKeepingAnchor;
7
- exports.containsPoint = containsPoint;
8
- exports.encloses = encloses;
9
- exports.isWiderAndTallerThan = isWiderAndTallerThan;
10
- exports.topmostPerDepth = topmostPerDepth;
3
+ exports.DocumentContainerUtil = void 0;
11
4
  const rotation_math_1 = require("../util/mask/rotation-math");
12
5
  const dynamic_text_util_1 = require("../util/dynamic-text/dynamic-text-util");
13
- function toCoordinateBox(x, y, width, height) {
14
- return { x, y, width, height, left: x, right: x + width, top: y, bottom: y + height };
15
- }
16
- function centerOf(box) {
17
- return { x: box.left + box.width / 2, y: box.top + box.height / 2 };
18
- }
19
- function anchorOf(documentElement, box) {
20
- var _a;
21
- if (!dynamic_text_util_1.DynamicTextUtil.isDynamicText(documentElement)) {
22
- return centerOf(box);
6
+ class DocumentContainerUtil {
7
+ static toCoordinateBox(x, y, width, height) {
8
+ return { x, y, width, height, left: x, right: x + width, top: y, bottom: y + height };
23
9
  }
24
- const angle = (_a = documentElement.rotate) === null || _a === void 0 ? void 0 : _a.angle;
25
- const corner = { x: box.left, y: box.top };
26
- return angle ? (0, rotation_math_1.rotatePoint)(corner, angle, centerOf(box)) : corner;
27
- }
28
- function positionKeepingAnchor(documentElement, newSize) {
29
- var _a, _b, _c, _d, _e;
30
- const position = documentElement === null || documentElement === void 0 ? void 0 : documentElement.position;
31
- const angle = (_a = documentElement === null || documentElement === void 0 ? void 0 : documentElement.rotate) === null || _a === void 0 ? void 0 : _a.angle;
32
- if (!position || !angle || !newSize || !documentElement.size) {
33
- return position;
10
+ static centerOf(box) {
11
+ return { x: box.left + box.width / 2, y: box.top + box.height / 2 };
34
12
  }
35
- const scaleX = (_c = (_b = documentElement.scale) === null || _b === void 0 ? void 0 : _b.x) !== null && _c !== void 0 ? _c : 1;
36
- const scaleY = (_e = (_d = documentElement.scale) === null || _d === void 0 ? void 0 : _d.y) !== null && _e !== void 0 ? _e : 1;
37
- const cornerOffset = (size) => {
38
- const half = { x: (size.width * scaleX) / 2, y: (size.height * scaleY) / 2 };
39
- const turned = (0, rotation_math_1.rotatePoint)(half, angle);
40
- return { x: half.x - turned.x, y: half.y - turned.y };
41
- };
42
- const before = cornerOffset(documentElement.size);
43
- const after = cornerOffset(newSize);
44
- return { x: position.x + before.x - after.x, y: position.y + before.y - after.y };
45
- }
46
- function containsPoint(box, point) {
47
- return point.x >= box.left && point.x <= box.right && point.y >= box.top && point.y <= box.bottom;
48
- }
49
- function encloses(outer, inner) {
50
- const covers = outer.left <= inner.left && outer.right >= inner.right && outer.top <= inner.top && outer.bottom >= inner.bottom;
51
- const identical = outer.left === inner.left &&
52
- outer.right === inner.right &&
53
- outer.top === inner.top &&
54
- outer.bottom === inner.bottom;
55
- return covers && !identical;
56
- }
57
- function isWiderAndTallerThan(box, other) {
58
- return box.width > other.width && box.height > other.height;
59
- }
60
- function topmostPerDepth(containers) {
61
- const byDepth = [];
62
- for (const container of containers) {
63
- const existing = byDepth[container.depth];
64
- if (!existing || container.drawOrder > existing.drawOrder) {
65
- byDepth[container.depth] = container;
13
+ static anchorOf(documentElement, box) {
14
+ var _a;
15
+ if (!dynamic_text_util_1.DynamicTextUtil.isDynamicText(documentElement)) {
16
+ return DocumentContainerUtil.centerOf(box);
17
+ }
18
+ const angle = (_a = documentElement.rotate) === null || _a === void 0 ? void 0 : _a.angle;
19
+ const corner = { x: box.left, y: box.top };
20
+ return angle ? (0, rotation_math_1.rotatePoint)(corner, angle, DocumentContainerUtil.centerOf(box)) : corner;
21
+ }
22
+ static positionKeepingAnchor(documentElement, newSize) {
23
+ var _a, _b, _c, _d, _e;
24
+ const position = documentElement === null || documentElement === void 0 ? void 0 : documentElement.position;
25
+ const angle = (_a = documentElement === null || documentElement === void 0 ? void 0 : documentElement.rotate) === null || _a === void 0 ? void 0 : _a.angle;
26
+ if (!position || !angle || !newSize || !documentElement.size) {
27
+ return position;
28
+ }
29
+ const scaleX = (_c = (_b = documentElement.scale) === null || _b === void 0 ? void 0 : _b.x) !== null && _c !== void 0 ? _c : 1;
30
+ const scaleY = (_e = (_d = documentElement.scale) === null || _d === void 0 ? void 0 : _d.y) !== null && _e !== void 0 ? _e : 1;
31
+ const cornerOffset = (size) => {
32
+ const half = { x: (size.width * scaleX) / 2, y: (size.height * scaleY) / 2 };
33
+ const turned = (0, rotation_math_1.rotatePoint)(half, angle);
34
+ return { x: half.x - turned.x, y: half.y - turned.y };
35
+ };
36
+ const before = cornerOffset(documentElement.size);
37
+ const after = cornerOffset(newSize);
38
+ return { x: position.x + before.x - after.x, y: position.y + before.y - after.y };
39
+ }
40
+ static anchorFor(documentElement, position) {
41
+ var _a, _b, _c, _d, _e;
42
+ const at = position !== null && position !== void 0 ? position : documentElement === null || documentElement === void 0 ? void 0 : documentElement.position;
43
+ if (!at) {
44
+ return null;
45
+ }
46
+ const size = (_a = documentElement === null || documentElement === void 0 ? void 0 : documentElement.size) !== null && _a !== void 0 ? _a : { width: 0, height: 0 };
47
+ const box = DocumentContainerUtil.toCoordinateBox(at.x, at.y, size.width * ((_c = (_b = documentElement === null || documentElement === void 0 ? void 0 : documentElement.scale) === null || _b === void 0 ? void 0 : _b.x) !== null && _c !== void 0 ? _c : 1), size.height * ((_e = (_d = documentElement === null || documentElement === void 0 ? void 0 : documentElement.scale) === null || _d === void 0 ? void 0 : _d.y) !== null && _e !== void 0 ? _e : 1));
48
+ return DocumentContainerUtil.anchorOf(documentElement, box);
49
+ }
50
+ static containsPoint(box, point) {
51
+ return point.x >= box.left && point.x <= box.right && point.y >= box.top && point.y <= box.bottom;
52
+ }
53
+ static encloses(outer, inner) {
54
+ const covers = outer.left <= inner.left && outer.right >= inner.right && outer.top <= inner.top && outer.bottom >= inner.bottom;
55
+ const identical = outer.left === inner.left &&
56
+ outer.right === inner.right &&
57
+ outer.top === inner.top &&
58
+ outer.bottom === inner.bottom;
59
+ return covers && !identical;
60
+ }
61
+ static isWiderAndTallerThan(box, other) {
62
+ return box.width > other.width && box.height > other.height;
63
+ }
64
+ static topmostPerDepth(containers) {
65
+ const byDepth = [];
66
+ for (const container of containers) {
67
+ const existing = byDepth[container.depth];
68
+ if (!existing || container.drawOrder > existing.drawOrder) {
69
+ byDepth[container.depth] = container;
70
+ }
66
71
  }
72
+ return byDepth.filter(Boolean);
67
73
  }
68
- return byDepth.filter(Boolean);
69
74
  }
75
+ exports.DocumentContainerUtil = DocumentContainerUtil;
@@ -65,7 +65,7 @@ class DocumentContainerService {
65
65
  const start = ((_a = documentElement.start) === null || _a === void 0 ? void 0 : _a.id) ? documentElementsById.get(documentElement.start.id) : undefined;
66
66
  const end = ((_b = documentElement.end) === null || _b === void 0 ? void 0 : _b.id) ? documentElementsById.get(documentElement.end.id) : undefined;
67
67
  const { x, y, width, height } = document_element_factory_1.DocumentElementFactory.getBoundingClientRectScaled(documentElement, start, end);
68
- return (0, document_container_1.toCoordinateBox)(x, y, width, height);
68
+ return document_container_1.DocumentContainerUtil.toCoordinateBox(x, y, width, height);
69
69
  }
70
70
  catch (_c) {
71
71
  return null;
@@ -86,7 +86,7 @@ class DocumentContainerService {
86
86
  const ancestorsById = new Map();
87
87
  for (const container of containers) {
88
88
  const ancestors = kind.nests
89
- ? containers.filter((other) => other !== container && (0, document_container_1.encloses)(other.box, container.box))
89
+ ? containers.filter((other) => other !== container && document_container_1.DocumentContainerUtil.encloses(other.box, container.box))
90
90
  : [];
91
91
  ancestorsById.set(container.id, ancestors);
92
92
  container.depth = ancestors.length;
@@ -123,10 +123,10 @@ class DocumentContainerService {
123
123
  .reduce((topmost, container) => (container.drawOrder > topmost.drawOrder ? container : topmost));
124
124
  }
125
125
  static toContainerPath(containers) {
126
- return (0, document_container_1.topmostPerDepth)(containers).map((container) => container.id);
126
+ return document_container_1.DocumentContainerUtil.topmostPerDepth(containers).map((container) => container.id);
127
127
  }
128
128
  static candidateContainers(measured, containers) {
129
- const anchor = (0, document_container_1.anchorOf)(measured.documentElement, measured.box);
129
+ const anchor = document_container_1.DocumentContainerUtil.anchorOf(measured.documentElement, measured.box);
130
130
  const isShape = document_element_constants_1.SHAPE_ELEMENT_TYPES.includes(measured.documentElement.type) &&
131
131
  !dynamic_text_util_1.DynamicTextUtil.isDynamicText(measured.documentElement);
132
132
  return containers.filter((container) => {
@@ -134,10 +134,10 @@ class DocumentContainerService {
134
134
  if (container.id === measured.documentElement.id || measured.drawOrder <= container.drawOrder) {
135
135
  return false;
136
136
  }
137
- if (!(0, document_container_1.containsPoint)(container.box, anchor)) {
137
+ if (!document_container_1.DocumentContainerUtil.containsPoint(container.box, anchor)) {
138
138
  return false;
139
139
  }
140
- if (isShape && (0, document_container_1.isWiderAndTallerThan)(measured.box, container.box)) {
140
+ if (isShape && document_container_1.DocumentContainerUtil.isWiderAndTallerThan(measured.box, container.box)) {
141
141
  return false;
142
142
  }
143
143
  return !((_a = measured.documentElement.elementIds) === null || _a === void 0 ? void 0 : _a.includes(container.id));
@@ -1,4 +1,5 @@
1
1
  import { DocumentElement } from './types';
2
+ export declare function isValidSortIndex(sortIndex: unknown): sortIndex is string;
2
3
  export declare function allElementsHaveSortIndex(elements: unknown[]): boolean;
3
4
  export declare function buildKeyBetween(a: string | null, b: string | null): string;
4
5
  export declare function buildNKeysBetween(a: string | null, b: string | null, n: number): string[];
package/lib/sort-index.js CHANGED
@@ -1,13 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isValidSortIndex = isValidSortIndex;
3
4
  exports.allElementsHaveSortIndex = allElementsHaveSortIndex;
4
5
  exports.buildKeyBetween = buildKeyBetween;
5
6
  exports.buildNKeysBetween = buildNKeysBetween;
6
7
  exports.compareSortIndex = compareSortIndex;
7
8
  const fractional_indexing_1 = require("fractional-indexing");
9
+ const SORT_INDEX_DIGITS = new RegExp(`^[${fractional_indexing_1.BASE_62_DIGITS.replace(/[\\\]^-]/g, (digit) => `\\${digit}`)}]+$`);
10
+ function isValidSortIndex(sortIndex) {
11
+ if (typeof sortIndex !== 'string' || !SORT_INDEX_DIGITS.test(sortIndex)) {
12
+ return false;
13
+ }
14
+ try {
15
+ (0, fractional_indexing_1.generateKeyBetween)(sortIndex, null);
16
+ }
17
+ catch (_a) {
18
+ return false;
19
+ }
20
+ return true;
21
+ }
8
22
  function allElementsHaveSortIndex(elements) {
9
- return elements.every((element) => typeof element.sortIndex === 'string' &&
10
- element.sortIndex.length > 0);
23
+ return elements.every((element) => isValidSortIndex(element.sortIndex));
11
24
  }
12
25
  function buildKeyBetween(a, b) {
13
26
  return (0, fractional_indexing_1.generateKeyBetween)(a, b);
@@ -6,5 +6,5 @@ export declare enum SortOrderErrorCode {
6
6
  export declare enum SortOrderErrorMessage {
7
7
  SORT_MIGRATION_IN_PROGRESS = "Sort order is being migrated for this document; editing is briefly locked",
8
8
  SORT_MODE_MISMATCH = "Ordering write does not match the document's sort mode",
9
- SORT_INDEX_NOT_USABLE = "sortIndex must be a non-empty string"
9
+ SORT_INDEX_NOT_USABLE = "sortIndex must be a valid fractional index"
10
10
  }
@@ -11,5 +11,5 @@ var SortOrderErrorMessage;
11
11
  (function (SortOrderErrorMessage) {
12
12
  SortOrderErrorMessage["SORT_MIGRATION_IN_PROGRESS"] = "Sort order is being migrated for this document; editing is briefly locked";
13
13
  SortOrderErrorMessage["SORT_MODE_MISMATCH"] = "Ordering write does not match the document's sort mode";
14
- SortOrderErrorMessage["SORT_INDEX_NOT_USABLE"] = "sortIndex must be a non-empty string";
14
+ SortOrderErrorMessage["SORT_INDEX_NOT_USABLE"] = "sortIndex must be a valid fractional index";
15
15
  })(SortOrderErrorMessage || (exports.SortOrderErrorMessage = SortOrderErrorMessage = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/documents",
3
- "version": "1.20.0-alpha.5ed9686",
3
+ "version": "1.20.0-alpha.6beca3a",
4
4
  "description": "Documents library for contrail platform",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",