@contrail/documents 2.4.0-alpha.c01727c → 2.4.0-alpha.ce67280

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
@@ -13,67 +13,75 @@ Also shipped on the 1.x line as 1.21.0.
13
13
 
14
14
  - `DocumentContainerService.buildIndex` and `DocumentContainerIndex` — which containers hold which
15
15
  elements, for one document, resolved once and read back by lookup. A container is a frame or a
16
- section, described by a `ContainerKind` carrying `elementType`, `nests` and `layer`. An element
17
- belongs to the container whose box covers its anchor point and which is drawn below it. Nesting
18
- gives a path with one container per depth, the later-drawn one winning where two cross. A
19
- container is only ever content of a kind layered behind it, so a frame belongs to a section and a
20
- section never to a frame. Shapes wider and taller than the container, and groups holding it, are
21
- excluded.
22
- - `SECTION_ELEMENT_TYPE`, `FRAME_ELEMENT_TYPE` and `SHAPE_ELEMENT_TYPES` in
23
- `document-element-constants`, so the rule above has one definition rather than one per app.
24
- - `DocumentContainerUtil` — the container geometry as statics, with `anchorFor` for a caller
25
- holding an element rather than a measured box.
26
- - `positionKeepingAnchor` — where an element has to sit for its drawn corner to land where it did
27
- before a resize. Rotation turns about the centre and the centre moves with the size, so a rotated
28
- element resized about its stored position drags its corner along with it. The board already makes
29
- this move; a server recomputing a dynamic text has to make the same one, or the corner is not the
30
- fixed point the anchor rule needs.
16
+ section; membership is geometry, draw order and kind, with nothing inherited and nothing stored.
17
+ The rule is written out in `src/containers/DOCUMENT_CONTAINERS_SPEC.md`, which the code is
18
+ measured against.
19
+ - `DocumentContainerUtil` — the container geometry as statics. `anchorFor` answers "which point is
20
+ this element placed by" for a caller holding an element rather than a measured box, and
21
+ `resizeKeepingAnchor` returns the size a new value needs with the position that keeps that point
22
+ still. Applying one without the other drags the corner membership reads.
23
+ - `SECTION_ELEMENT_TYPE` and `FRAME_ELEMENT_TYPE` in `document-element-constants`, alongside
24
+ `SHAPE_ELEMENT_TYPES`, which moved here for the board's shape handling.
31
25
  - `DocumentElementFactory.getBoundingClientRectScaled` — the space an element covers on the canvas,
32
- with `scale` applied. `getBoundingClientRect` keeps returning the element's own box before scale,
33
- which is what layout works in: `@contrail/document-table` places table members from it, and
34
- scaling it moves a scaled member into a different cell. Anything deciding what an element overlaps
35
- or sits on wants the scaled one.
26
+ with `scale` applied. `getBoundingClientRect` still returns the unscaled box, which is what layout
27
+ works in: `@contrail/document-table` places table members from it, and scaling it moves a scaled
28
+ member into a different cell. Anything deciding what an element overlaps or sits on wants the
29
+ scaled one.
30
+
31
+ ### Removed
32
+
33
+ - `DocumentContainerIndex.containerPathAtPoint`. It applied the anchor rule alone, because a bare
34
+ point has nothing to compare against a container, and returned the same shape as `containerIdsOf`,
35
+ so it read as a second way to ask one question. `containerPathFor(element, kind)` replaces it and
36
+ applies every rule. Callers that hold a position pass the element with that position on it. This
37
+ is the one removal a consumer has to act on.
38
+ - `ContainerKind.layer`, `DocumentContainer.name` and `DocumentContainer.depth`, and
39
+ `DocumentContainerIndex.maxDepth`. The container API has not shipped in a released version, so
40
+ nothing outside this repository can be holding them. Read a container's name from
41
+ `documentElement.name`.
36
42
 
37
43
  ### Deprecated
38
44
 
39
45
  - `isContainerBinding()` in favour of `DynamicTextUtil.takesValueFromContainer()`. Same rule, named
40
- for what it decides: whether the binding's value comes from the container rather than a type
41
- property. The old name still works and delegates to the new one.
46
+ for what it decides. The old name still works and delegates to the new one.
42
47
 
43
48
  ### Fixed
44
49
 
45
- - A dynamic text is now anchored at its drawn top-left corner rather than its centre when deciding
46
- which container it belongs to, and `DynamicTextUtil.isDynamicText` is the shared test for one. Its
47
- size comes from the value it resolves to, and that value depends on the container, so letting its
48
- width feed membership meant its own text could choose its container: near an edge the two answers
49
- disagreed indefinitely, flipping between a value and "Move to a frame" on every read. Everything
50
- else still resolves on its centre.
51
- - The corner is the rotated one (VIBE-9567). A 200x20 text turned on its side draws as a 20x200
52
- column, and its unrotated `position` can sit well clear of the frame it is visibly on, which
53
- reported "Move to a frame" for a vertical text the user could see was placed correctly.
54
- - A dynamic text is also exempt from the oversized-shape exclusion, which keeps a shape wider and
55
- taller than a container off it. That rule reads size too, so a dynamic text larger than its frame
56
- in both directions had two self-consistent answers and kept whichever it started from. The rule
57
- exists to keep a backdrop shape off a frame it covers, which is not what a dynamic text is doing.
58
- - `min` and `max` rendered an infinity sign when every member value was empty. The early return
59
- checked the list length before the empty values were filtered out, so `Math.min` of nothing
60
- reached the formatter. Both now fall through to `n/a`.
61
- - `DynamicTextUtil.calculateSize` threw on an element that reached it without a `size`, which on
62
- the server failed the read for the whole document. It now measures from a zero size instead.
50
+ - A dynamic text is anchored at its drawn top-left corner rather than its centre. Its size comes
51
+ from the value it resolves to and that value comes from its container, so an anchor that moved
52
+ with the size let the two choose each other: near an edge they never settled, flipping between a
53
+ value and "Move to a frame" on every read. The corner is the rotated one, so a text turned on its
54
+ side anchors where it appears (VIBE-9567).
55
+ - `isDynamicText` requires a `propertyBindings.text` binding, the one the value is read from. The
56
+ text tool creates an element with empty bindings and empty metadata, both truthy, so an
57
+ unconfigured text was anchored at its corner while the canvas centred it.
58
+ - An element covering a container is not inside it, whatever its type. The rule it replaces looked
59
+ only at shapes wider and taller than the container, so a background image joined the frame it
60
+ covered.
61
+ - A line or callout is measured from the elements it connects even with no stored `position`, where
62
+ it was previously left out of the index entirely.
63
+ - `min` and `max` rendered an infinity sign when every member value was empty; both fall through to
64
+ `n/a`. `calculateSize` no longer throws on an element that reaches it without a `size`, which on
65
+ the server failed the read for the whole document.
63
66
 
64
67
  ### Changed
65
68
 
66
- - Container membership measures a scaled element from its scaled box, through the new
67
- `getBoundingClientRectScaled` above. Only a sticky note, a regular iframe, a text-tool text or a
68
- table carries `scale` at all; for this work the one that matters is a text-tool dynamic text,
69
- which previously measured at its unscaled size and could resolve to the wrong container.
70
- - `DocumentContainerUtil.anchorFor` measures through the same factory call the index uses, so a
71
- caller holding an element gets the answer the index would give it. Reading `size` directly
72
- disagreed for the types whose box is not position plus size: a component's box is built from the
73
- rows it draws, and a line's comes from its `lineDefinition`.
74
- - Container depth counts nesting levels rather than enclosing boxes. Two containers that merely
75
- cross both enclose anything inside the overlap, so counting ancestors put that inner container a
76
- level too deep and made `maxDepth` report a level no path could reach.
69
+ - `anchorFor` and the index measure through the same factory call, so a caller holding an element
70
+ gets the answer the index would give it. Reading `size` directly disagreed for the types whose box
71
+ is not position plus size.
72
+ - A container path is a containment chain, walked outward from the container the element sits on, so
73
+ two containers that merely cross no longer both claim it.
74
+ - `buildIndex` scans element types before measuring, so a document with no containers of the kinds
75
+ asked for returns an empty index without a geometry pass.
76
+ - **A section nests inside another only when it is drawn above it.** A section drawn over one it
77
+ encloses hides it, so the hidden section holds nothing and a `section.count` on it reads zero.
78
+ This changes existing boards wherever the outer section was drawn last.
79
+ - `containerIdsOf` and `memberIdsOf` return copies. `readonly` is erased at runtime, and a caller
80
+ sorting what it got back reordered the index's own list for every later read.
81
+ - A member list is a set of ids, so two elements sharing an id count once.
82
+ - `min` and `max` no longer read an empty member as zero, and `uniqueArray` no longer emits one as
83
+ a blank entry. A text box narrower than its own padding measures without a fixed width.
84
+ - A nullish entry in the element list is dropped rather than throwing.
77
85
 
78
86
  ## [2.3.0] - 2026-09-21
79
87
 
@@ -3,7 +3,6 @@ export type ContainerElementType = typeof FRAME_ELEMENT_TYPE | typeof SECTION_EL
3
3
  export interface ContainerKind {
4
4
  elementType: ContainerElementType;
5
5
  nests: boolean;
6
- layer: number;
7
6
  }
8
7
  export declare const SECTION_KIND: ContainerKind;
9
8
  export declare const FRAME_KIND: ContainerKind;
@@ -2,6 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CONTAINER_KINDS = exports.FRAME_KIND = exports.SECTION_KIND = void 0;
4
4
  const document_element_constants_1 = require("../document-element-constants");
5
- exports.SECTION_KIND = { elementType: document_element_constants_1.SECTION_ELEMENT_TYPE, nests: true, layer: 0 };
6
- exports.FRAME_KIND = { elementType: document_element_constants_1.FRAME_ELEMENT_TYPE, nests: false, layer: 1 };
5
+ exports.SECTION_KIND = { elementType: document_element_constants_1.SECTION_ELEMENT_TYPE, nests: true };
6
+ exports.FRAME_KIND = { elementType: document_element_constants_1.FRAME_ELEMENT_TYPE, nests: false };
7
7
  exports.CONTAINER_KINDS = [exports.SECTION_KIND, exports.FRAME_KIND];
@@ -1,23 +1,24 @@
1
- import { DocumentElement, PositionDefinition } from '../types';
2
- import { ContainerElementType } from './container-kind';
3
- import { DocumentContainer } from './document-container';
1
+ import { DocumentElement } from '../types';
2
+ import { ContainerElementType, ContainerKind } from './container-kind';
3
+ import { DocumentContainer, MeasuredDocumentElement } from './document-container';
4
4
  export declare class DocumentContainerIndex {
5
5
  private readonly data;
6
6
  constructor(data: {
7
7
  documentElementsById: Map<string, DocumentElement>;
8
+ measuredById: Map<string, MeasuredDocumentElement>;
8
9
  containersById: Map<string, DocumentContainer>;
9
10
  containerPathsByDocumentElementId: Map<string, Map<string, string[]>>;
10
11
  memberIdsByContainerId: Map<string, string[]>;
11
12
  containerIdsByElementType: Map<string, string[]>;
12
13
  });
14
+ static empty(): DocumentContainerIndex;
13
15
  getContainer(containerId: string): DocumentContainer | null;
14
16
  containersOf(documentElementId: string, elementType: ContainerElementType): DocumentContainer[];
15
17
  containerIdsOf(documentElementId: string, elementType: ContainerElementType): readonly string[];
16
18
  innermostContainerIdOf(documentElementId: string, elementType: ContainerElementType): string | null;
17
19
  memberIdsOf(containerId: string): readonly string[];
18
20
  memberDocumentElementsOf(containerId: string): DocumentElement[];
19
- containerPathAtPoint(point: PositionDefinition, elementType: ContainerElementType): readonly string[];
20
- maxDepth(elementType: ContainerElementType): number;
21
+ containerPathFor(documentElement: DocumentElement, kind: ContainerKind): readonly string[];
21
22
  private containersOfType;
22
23
  private resolveAll;
23
24
  private containerPath;
@@ -6,6 +6,16 @@ class DocumentContainerIndex {
6
6
  constructor(data) {
7
7
  this.data = data;
8
8
  }
9
+ static empty() {
10
+ return new DocumentContainerIndex({
11
+ documentElementsById: new Map(),
12
+ measuredById: new Map(),
13
+ containersById: new Map(),
14
+ containerPathsByDocumentElementId: new Map(),
15
+ memberIdsByContainerId: new Map(),
16
+ containerIdsByElementType: new Map(),
17
+ });
18
+ }
9
19
  getContainer(containerId) {
10
20
  var _a;
11
21
  return (_a = this.data.containersById.get(containerId)) !== null && _a !== void 0 ? _a : null;
@@ -14,7 +24,7 @@ class DocumentContainerIndex {
14
24
  return this.resolveAll(this.containerIdsOf(documentElementId, elementType));
15
25
  }
16
26
  containerIdsOf(documentElementId, elementType) {
17
- return this.containerPath(documentElementId, elementType);
27
+ return [...this.containerPath(documentElementId, elementType)];
18
28
  }
19
29
  innermostContainerIdOf(documentElementId, elementType) {
20
30
  const path = this.containerPath(documentElementId, elementType);
@@ -22,7 +32,7 @@ class DocumentContainerIndex {
22
32
  }
23
33
  memberIdsOf(containerId) {
24
34
  var _a;
25
- return (_a = this.data.memberIdsByContainerId.get(containerId)) !== null && _a !== void 0 ? _a : [];
35
+ return [...((_a = this.data.memberIdsByContainerId.get(containerId)) !== null && _a !== void 0 ? _a : [])];
26
36
  }
27
37
  memberDocumentElementsOf(containerId) {
28
38
  const members = [];
@@ -34,12 +44,17 @@ class DocumentContainerIndex {
34
44
  }
35
45
  return members;
36
46
  }
37
- containerPathAtPoint(point, elementType) {
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
- }
41
- maxDepth(elementType) {
42
- return this.containersOfType(elementType).reduce((max, container) => Math.max(max, container.depth + 1), 0);
47
+ containerPathFor(documentElement, kind) {
48
+ var _a, _b;
49
+ if (!documentElement) {
50
+ return [];
51
+ }
52
+ const measured = {
53
+ documentElement,
54
+ box: document_container_1.DocumentContainerUtil.measureBox(documentElement, this.data.documentElementsById),
55
+ drawOrder: (_b = (_a = this.data.measuredById.get(documentElement.id)) === null || _a === void 0 ? void 0 : _a.drawOrder) !== null && _b !== void 0 ? _b : Number.MAX_SAFE_INTEGER,
56
+ };
57
+ return document_container_1.DocumentContainerUtil.pathFor(measured, kind, this.containersOfType(kind.elementType)).map((container) => container.id);
43
58
  }
44
59
  containersOfType(elementType) {
45
60
  var _a;
@@ -3,10 +3,8 @@ import { ContainerKind } from './container-kind';
3
3
  export interface DocumentContainer {
4
4
  id: string;
5
5
  kind: ContainerKind;
6
- name: string;
7
6
  documentElement: DocumentElement;
8
7
  box: CoordinateBox;
9
- depth: number;
10
8
  drawOrder: number;
11
9
  }
12
10
  export interface MeasuredDocumentElement {
@@ -19,9 +17,17 @@ export declare class DocumentContainerUtil {
19
17
  static centerOf(box: CoordinateBox): PositionDefinition;
20
18
  static anchorOf(documentElement: DocumentElement, box: CoordinateBox): PositionDefinition;
21
19
  static positionKeepingAnchor(documentElement: DocumentElement, newSize: SizeDefinition): PositionDefinition;
20
+ static resizeKeepingAnchor(dynamicTextElement: DocumentElement, entityType: any, text: string): {
21
+ size: SizeDefinition;
22
+ position: PositionDefinition;
23
+ };
22
24
  static anchorFor(documentElement: DocumentElement, position?: PositionDefinition, startElement?: DocumentElement, endElement?: DocumentElement): PositionDefinition;
23
25
  static containsPoint(box: CoordinateBox, point: PositionDefinition): boolean;
24
26
  static encloses(outer: CoordinateBox, inner: CoordinateBox): boolean;
25
- static isWiderAndTallerThan(box: CoordinateBox, other: CoordinateBox): boolean;
26
- static topmostPerDepth(containers: DocumentContainer[]): DocumentContainer[];
27
+ static chain(candidates: DocumentContainer[]): DocumentContainer[];
28
+ static measureBox(documentElement: DocumentElement, documentElementsById: Map<string, DocumentElement>): CoordinateBox | null;
29
+ static pathFor(measured: MeasuredDocumentElement, kind: ContainerKind, containers: DocumentContainer[]): DocumentContainer[];
30
+ static enclosersOf(measured: MeasuredDocumentElement, containers: DocumentContainer[]): DocumentContainer[];
31
+ static candidatesFor(measured: MeasuredDocumentElement, containers: DocumentContainer[]): DocumentContainer[];
32
+ static topmost(containers: DocumentContainer[]): DocumentContainer[];
27
33
  }
@@ -24,9 +24,12 @@ class DocumentContainerUtil {
24
24
  var _a, _b, _c, _d, _e;
25
25
  const position = documentElement === null || documentElement === void 0 ? void 0 : documentElement.position;
26
26
  const angle = (_a = documentElement === null || documentElement === void 0 ? void 0 : documentElement.rotate) === null || _a === void 0 ? void 0 : _a.angle;
27
- if (!position || !angle || !newSize || !documentElement.size) {
27
+ if (!position) {
28
28
  return position;
29
29
  }
30
+ if (!angle || !newSize || !documentElement.size) {
31
+ return Object.assign({}, position);
32
+ }
30
33
  const scaleX = (_c = (_b = documentElement.scale) === null || _b === void 0 ? void 0 : _b.x) !== null && _c !== void 0 ? _c : 1;
31
34
  const scaleY = (_e = (_d = documentElement.scale) === null || _d === void 0 ? void 0 : _d.y) !== null && _e !== void 0 ? _e : 1;
32
35
  const cornerOffset = (size) => {
@@ -36,23 +39,23 @@ class DocumentContainerUtil {
36
39
  };
37
40
  const before = cornerOffset(documentElement.size);
38
41
  const after = cornerOffset(newSize);
39
- return { x: position.x + before.x - after.x, y: position.y + before.y - after.y };
42
+ const moved = { x: position.x + before.x - after.x, y: position.y + before.y - after.y };
43
+ return Number.isFinite(moved.x) && Number.isFinite(moved.y) ? moved : Object.assign({}, position);
44
+ }
45
+ static resizeKeepingAnchor(dynamicTextElement, entityType, text) {
46
+ const size = dynamic_text_util_1.DynamicTextUtil.calculateSize(dynamicTextElement, entityType, text);
47
+ return { size, position: DocumentContainerUtil.positionKeepingAnchor(dynamicTextElement, size) };
40
48
  }
41
49
  static anchorFor(documentElement, position, startElement, endElement) {
42
- var _a, _b, _c, _d, _e;
43
50
  const at = position !== null && position !== void 0 ? position : documentElement === null || documentElement === void 0 ? void 0 : documentElement.position;
44
- if (!at) {
45
- return null;
46
- }
47
- const size = (_a = documentElement === null || documentElement === void 0 ? void 0 : documentElement.size) !== null && _a !== void 0 ? _a : { width: 0, height: 0 };
48
- let 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));
49
51
  try {
50
- const measured = document_element_factory_1.DocumentElementFactory.getBoundingClientRectScaled(Object.assign(Object.assign({}, documentElement), { position: at }), startElement, endElement);
51
- box = DocumentContainerUtil.toCoordinateBox(measured.x, measured.y, measured.width, measured.height);
52
+ const measured = document_element_factory_1.DocumentElementFactory.getBoundingClientRectScaled(at ? Object.assign(Object.assign({}, documentElement), { position: at }) : documentElement, startElement, endElement);
53
+ const box = DocumentContainerUtil.toCoordinateBox(measured.x, measured.y, measured.width, measured.height);
54
+ return DocumentContainerUtil.anchorOf(documentElement, box);
52
55
  }
53
- catch (_f) {
56
+ catch (_a) {
57
+ return null;
54
58
  }
55
- return DocumentContainerUtil.anchorOf(documentElement, box);
56
59
  }
57
60
  static containsPoint(box, point) {
58
61
  return point.x >= box.left && point.x <= box.right && point.y >= box.top && point.y <= box.bottom;
@@ -65,18 +68,75 @@ class DocumentContainerUtil {
65
68
  outer.bottom === inner.bottom;
66
69
  return covers && !identical;
67
70
  }
68
- static isWiderAndTallerThan(box, other) {
69
- return box.width > other.width && box.height > other.height;
71
+ static chain(candidates) {
72
+ if (!candidates.length) {
73
+ return [];
74
+ }
75
+ const topmostOf = (of) => of.reduce((topmost, container) => (container.drawOrder > topmost.drawOrder ? container : topmost));
76
+ const path = [topmostOf(candidates)];
77
+ for (;;) {
78
+ const current = path[0];
79
+ const enclosing = candidates.filter((container) => !path.includes(container) && this.encloses(container.box, current.box));
80
+ if (!enclosing.length) {
81
+ return path;
82
+ }
83
+ path.unshift(topmostOf(enclosing));
84
+ }
70
85
  }
71
- static topmostPerDepth(containers) {
72
- const byDepth = [];
73
- for (const container of containers) {
74
- const existing = byDepth[container.depth];
75
- if (!existing || container.drawOrder > existing.drawOrder) {
76
- byDepth[container.depth] = container;
86
+ static measureBox(documentElement, documentElementsById) {
87
+ var _a, _b;
88
+ try {
89
+ const start = ((_a = documentElement === null || documentElement === void 0 ? void 0 : documentElement.start) === null || _a === void 0 ? void 0 : _a.id) ? documentElementsById.get(documentElement.start.id) : undefined;
90
+ const end = ((_b = documentElement === null || documentElement === void 0 ? void 0 : documentElement.end) === null || _b === void 0 ? void 0 : _b.id) ? documentElementsById.get(documentElement.end.id) : undefined;
91
+ const { x, y, width, height } = document_element_factory_1.DocumentElementFactory.getBoundingClientRectScaled(documentElement, start, end);
92
+ if (!width && !height && !x && !y) {
93
+ return null;
77
94
  }
95
+ return DocumentContainerUtil.toCoordinateBox(x, y, width, height);
78
96
  }
79
- return byDepth.filter(Boolean);
97
+ catch (_c) {
98
+ return null;
99
+ }
100
+ }
101
+ static pathFor(measured, kind, containers) {
102
+ if (!containers.length || !measured.box) {
103
+ return [];
104
+ }
105
+ if (measured.documentElement.type === kind.elementType) {
106
+ return kind.nests ? DocumentContainerUtil.chain(DocumentContainerUtil.enclosersOf(measured, containers)) : [];
107
+ }
108
+ const candidates = DocumentContainerUtil.candidatesFor(measured, containers);
109
+ return kind.nests ? DocumentContainerUtil.chain(candidates) : DocumentContainerUtil.topmost(candidates);
110
+ }
111
+ static enclosersOf(measured, containers) {
112
+ return containers.filter((container) => {
113
+ var _a;
114
+ return container.id !== measured.documentElement.id &&
115
+ DocumentContainerUtil.encloses(container.box, measured.box) &&
116
+ measured.drawOrder > container.drawOrder &&
117
+ !((_a = measured.documentElement.elementIds) === null || _a === void 0 ? void 0 : _a.includes(container.id));
118
+ });
119
+ }
120
+ static candidatesFor(measured, containers) {
121
+ const anchor = DocumentContainerUtil.anchorOf(measured.documentElement, measured.box);
122
+ return containers.filter((container) => {
123
+ var _a;
124
+ if (container.id === measured.documentElement.id || measured.drawOrder <= container.drawOrder) {
125
+ return false;
126
+ }
127
+ if (!DocumentContainerUtil.containsPoint(container.box, anchor)) {
128
+ return false;
129
+ }
130
+ if (DocumentContainerUtil.encloses(measured.box, container.box)) {
131
+ return false;
132
+ }
133
+ return !((_a = measured.documentElement.elementIds) === null || _a === void 0 ? void 0 : _a.includes(container.id));
134
+ });
135
+ }
136
+ static topmost(containers) {
137
+ return containers.length
138
+ ? [containers.reduce((topmost, container) => (container.drawOrder > topmost.drawOrder ? container : topmost))]
139
+ : [];
80
140
  }
81
141
  }
82
142
  exports.DocumentContainerUtil = DocumentContainerUtil;
@@ -4,12 +4,6 @@ import { DocumentContainerIndex } from './document-container-index';
4
4
  export declare class DocumentContainerService {
5
5
  static buildIndex(documentElements: readonly DocumentElement[], kinds?: ContainerKind[]): DocumentContainerIndex;
6
6
  static isContainer(documentElement: DocumentElement, kinds?: ContainerKind[]): boolean;
7
- private static inherit;
8
7
  private static measureDocumentElements;
9
- private static measure;
10
8
  private static collectContainers;
11
- private static resolveDirectPath;
12
- private static enclosersOf;
13
- private static chain;
14
- private static candidateContainers;
15
9
  }
@@ -1,64 +1,63 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DocumentContainerService = void 0;
4
- const document_element_factory_1 = require("../document-element-factory");
5
- const dynamic_text_util_1 = require("../util/dynamic-text/dynamic-text-util");
6
- const document_element_constants_1 = require("../document-element-constants");
7
4
  const container_kind_1 = require("./container-kind");
8
5
  const document_container_index_1 = require("./document-container-index");
9
6
  const document_container_1 = require("./document-container");
10
7
  class DocumentContainerService {
11
8
  static buildIndex(documentElements, kinds = container_kind_1.CONTAINER_KINDS) {
12
- var _a, _b, _c;
13
- const measuredDocumentElements = this.measureDocumentElements(documentElements !== null && documentElements !== void 0 ? documentElements : []);
14
- const kindsByLayer = [...kinds].sort((first, second) => second.layer - first.layer);
15
- const containersByElementType = new Map(kindsByLayer.map((kind) => [kind.elementType, this.collectContainers(measuredDocumentElements, kind)]));
9
+ var _a, _b;
10
+ const elements = (documentElements !== null && documentElements !== void 0 ? documentElements : []).filter((element) => !!element);
11
+ const containerElementTypes = new Set(kinds.map((kind) => kind.elementType));
12
+ if (!elements.some((element) => containerElementTypes.has(element.type))) {
13
+ return document_container_index_1.DocumentContainerIndex.empty();
14
+ }
15
+ const documentElementsById = new Map(elements.map((element) => [element.id, element]));
16
+ const measuredDocumentElements = this.measureDocumentElements(elements, documentElementsById);
17
+ const containersByElementType = new Map(kinds.map((kind) => [kind.elementType, this.collectContainers(measuredDocumentElements, kind)]));
16
18
  const containersById = new Map();
17
19
  const containerIdsByElementType = new Map();
18
20
  containersByElementType.forEach((containers, elementType) => {
19
21
  containerIdsByElementType.set(elementType, containers.map((container) => container.id));
20
22
  containers.forEach((container) => containersById.set(container.id, container));
21
23
  });
22
- const directPaths = new Map();
24
+ const pathsByMeasured = new Map();
23
25
  for (const measured of measuredDocumentElements) {
24
26
  const byElementType = new Map();
25
- for (const kind of kindsByLayer) {
26
- byElementType.set(kind.elementType, this.resolveDirectPath(measured, kind, (_a = containersByElementType.get(kind.elementType)) !== null && _a !== void 0 ? _a : []));
27
- }
28
- directPaths.set(measured, byElementType);
29
- }
30
- for (const measured of measuredDocumentElements) {
31
- const container = containersById.get(measured.documentElement.id);
32
- if (container && container.documentElement === measured.documentElement) {
33
- container.depth = ((_c = (_b = directPaths.get(measured)) === null || _b === void 0 ? void 0 : _b.get(container.kind.elementType)) !== null && _c !== void 0 ? _c : []).length;
34
- }
35
- }
36
- const measuredById = new Map();
37
- for (const measured of measuredDocumentElements) {
38
- if (!measuredById.has(measured.documentElement.id)) {
39
- measuredById.set(measured.documentElement.id, measured);
27
+ for (const kind of kinds) {
28
+ byElementType.set(kind.elementType, document_container_1.DocumentContainerUtil.pathFor(measured, kind, (_a = containersByElementType.get(kind.elementType)) !== null && _a !== void 0 ? _a : []));
40
29
  }
30
+ pathsByMeasured.set(measured, byElementType);
41
31
  }
42
32
  const containerPathsByDocumentElementId = new Map();
43
33
  const memberIdsByContainerId = new Map();
34
+ const memberIdsSeen = new Map();
44
35
  for (const measured of measuredDocumentElements) {
45
36
  const documentElementId = measured.documentElement.id;
46
- const pathsByElementType = this.inherit(measured, kindsByLayer, directPaths, measuredById, new Set());
37
+ const pathsByElementType = new Map();
38
+ ((_b = pathsByMeasured.get(measured)) !== null && _b !== void 0 ? _b : new Map()).forEach((path, elementType) => pathsByElementType.set(elementType, path.map((container) => container.id)));
47
39
  for (const path of pathsByElementType.values()) {
48
40
  for (const containerId of path) {
49
- const members = memberIdsByContainerId.get(containerId);
50
- if (members) {
51
- members.push(documentElementId);
52
- }
53
- else {
41
+ const seen = memberIdsSeen.get(containerId);
42
+ if (!seen) {
43
+ memberIdsSeen.set(containerId, new Set([documentElementId]));
54
44
  memberIdsByContainerId.set(containerId, [documentElementId]);
55
45
  }
46
+ else if (!seen.has(documentElementId)) {
47
+ seen.add(documentElementId);
48
+ memberIdsByContainerId.get(containerId).push(documentElementId);
49
+ }
56
50
  }
57
51
  }
58
52
  containerPathsByDocumentElementId.set(documentElementId, pathsByElementType);
59
53
  }
54
+ const measuredById = new Map();
55
+ for (const measured of measuredDocumentElements) {
56
+ measuredById.set(measured.documentElement.id, measured);
57
+ }
60
58
  return new document_container_index_1.DocumentContainerIndex({
61
- documentElementsById: new Map([...measuredById].map(([id, measured]) => [id, measured.documentElement])),
59
+ documentElementsById,
60
+ measuredById,
62
61
  containersById,
63
62
  containerPathsByDocumentElementId,
64
63
  memberIdsByContainerId,
@@ -68,122 +67,23 @@ class DocumentContainerService {
68
67
  static isContainer(documentElement, kinds = container_kind_1.CONTAINER_KINDS) {
69
68
  return kinds.some((kind) => kind.elementType === (documentElement === null || documentElement === void 0 ? void 0 : documentElement.type));
70
69
  }
71
- static inherit(measured, kindsByLayer, directPaths, measuredById, resolving) {
72
- var _a, _b, _c;
73
- const direct = (_a = directPaths.get(measured)) !== null && _a !== void 0 ? _a : new Map();
74
- const paths = new Map();
75
- let inheritFromId = null;
76
- for (const kind of kindsByLayer) {
77
- if (inheritFromId && !resolving.has(inheritFromId)) {
78
- const container = measuredById.get(inheritFromId);
79
- const inherited = container
80
- ? this.inherit(container, kindsByLayer, directPaths, measuredById, new Set(resolving).add(inheritFromId))
81
- : null;
82
- paths.set(kind.elementType, (_b = inherited === null || inherited === void 0 ? void 0 : inherited.get(kind.elementType)) !== null && _b !== void 0 ? _b : []);
83
- continue;
84
- }
85
- const path = (_c = direct.get(kind.elementType)) !== null && _c !== void 0 ? _c : [];
86
- paths.set(kind.elementType, path.map((container) => container.id));
87
- if (path.length) {
88
- inheritFromId = path[path.length - 1].id;
89
- }
90
- }
91
- return paths;
92
- }
93
- static measureDocumentElements(documentElements) {
94
- const documentElementsById = new Map(documentElements.map((documentElement) => [documentElement.id, documentElement]));
70
+ static measureDocumentElements(documentElements, documentElementsById) {
95
71
  return documentElements.map((documentElement, drawOrder) => ({
96
72
  documentElement,
97
73
  drawOrder,
98
- box: this.measure(documentElement, documentElementsById),
74
+ box: document_container_1.DocumentContainerUtil.measureBox(documentElement, documentElementsById),
99
75
  }));
100
76
  }
101
- static measure(documentElement, documentElementsById) {
102
- var _a, _b;
103
- if (!(documentElement === null || documentElement === void 0 ? void 0 : documentElement.position)) {
104
- return null;
105
- }
106
- try {
107
- const start = ((_a = documentElement.start) === null || _a === void 0 ? void 0 : _a.id) ? documentElementsById.get(documentElement.start.id) : undefined;
108
- const end = ((_b = documentElement.end) === null || _b === void 0 ? void 0 : _b.id) ? documentElementsById.get(documentElement.end.id) : undefined;
109
- const { x, y, width, height } = document_element_factory_1.DocumentElementFactory.getBoundingClientRectScaled(documentElement, start, end);
110
- return document_container_1.DocumentContainerUtil.toCoordinateBox(x, y, width, height);
111
- }
112
- catch (_c) {
113
- return null;
114
- }
115
- }
116
77
  static collectContainers(measuredDocumentElements, kind) {
117
78
  return measuredDocumentElements
118
79
  .filter((measured) => measured.documentElement.type === kind.elementType && measured.box)
119
80
  .map((measured) => ({
120
81
  id: measured.documentElement.id,
121
82
  kind,
122
- name: measured.documentElement.name,
123
83
  documentElement: measured.documentElement,
124
84
  box: measured.box,
125
- depth: 0,
126
85
  drawOrder: measured.drawOrder,
127
86
  }));
128
87
  }
129
- static resolveDirectPath(measured, kind, containers) {
130
- if (!containers.length || !measured.box) {
131
- return [];
132
- }
133
- const ownKind = container_kind_1.CONTAINER_KINDS.find((candidate) => candidate.elementType === measured.documentElement.type);
134
- if (ownKind) {
135
- if (ownKind.elementType === kind.elementType) {
136
- return kind.nests ? this.chain(this.enclosersOf(measured, containers)) : [];
137
- }
138
- if (kind.layer > ownKind.layer) {
139
- return [];
140
- }
141
- }
142
- return this.chain(this.candidateContainers(measured, containers));
143
- }
144
- static enclosersOf(measured, containers) {
145
- return containers.filter((container) => {
146
- var _a;
147
- return container.documentElement !== measured.documentElement &&
148
- document_container_1.DocumentContainerUtil.encloses(container.box, measured.box) &&
149
- measured.drawOrder > container.drawOrder &&
150
- !((_a = measured.documentElement.elementIds) === null || _a === void 0 ? void 0 : _a.includes(container.id));
151
- });
152
- }
153
- static chain(candidates) {
154
- if (!candidates.length) {
155
- return [];
156
- }
157
- const innermostOf = (of) => of
158
- .filter((container) => !of.some((other) => other !== container && document_container_1.DocumentContainerUtil.encloses(container.box, other.box)))
159
- .reduce((topmost, container) => (container.drawOrder > topmost.drawOrder ? container : topmost));
160
- const path = [innermostOf(candidates)];
161
- for (;;) {
162
- const current = path[0];
163
- const enclosing = candidates.filter((container) => !path.includes(container) && document_container_1.DocumentContainerUtil.encloses(container.box, current.box));
164
- if (!enclosing.length) {
165
- return path;
166
- }
167
- path.unshift(innermostOf(enclosing));
168
- }
169
- }
170
- static candidateContainers(measured, containers) {
171
- const anchor = document_container_1.DocumentContainerUtil.anchorOf(measured.documentElement, measured.box);
172
- const isShape = document_element_constants_1.SHAPE_ELEMENT_TYPES.includes(measured.documentElement.type) &&
173
- !dynamic_text_util_1.DynamicTextUtil.isDynamicText(measured.documentElement);
174
- return containers.filter((container) => {
175
- var _a;
176
- if (container.documentElement === measured.documentElement || measured.drawOrder <= container.drawOrder) {
177
- return false;
178
- }
179
- if (!document_container_1.DocumentContainerUtil.containsPoint(container.box, anchor)) {
180
- return false;
181
- }
182
- if (isShape && document_container_1.DocumentContainerUtil.isWiderAndTallerThan(measured.box, container.box)) {
183
- return false;
184
- }
185
- return !((_a = measured.documentElement.elementIds) === null || _a === void 0 ? void 0 : _a.includes(container.id));
186
- });
187
- }
188
88
  }
189
89
  exports.DocumentContainerService = DocumentContainerService;
@@ -27,6 +27,8 @@ export declare enum DynamicTextDisplayFunction {
27
27
  export declare const MOVE_TO_FRAME_TEXT = "Move to a frame";
28
28
  export declare const MOVE_TO_SECTION_TEXT = "Move to a section";
29
29
  export declare const CLICK_TO_CONFIGURE = "Click to configure";
30
+ export declare function placeholderForMissingContainer(entityType: string): string;
31
+ export declare function isPlaceholderText(text: string): boolean;
30
32
  export declare const HARD_CODED_TEXT: {
31
33
  'project.name': string;
32
34
  'project.createdBy.email': string;
@@ -53,7 +55,10 @@ export declare class DynamicTextUtil {
53
55
  static isDynamicText(documentElement: DocumentElement): boolean;
54
56
  static takesValueFromContainer(bindingProperty: string | null | undefined): boolean;
55
57
  static getDynamicTextValue(dynamicTextElement: DocumentElement, property: TypeProperty, propertyLevel: PropertyLevel, container: DynamicTextContainer | null, componentModels: any[], ownerReferenceModel?: any): string;
56
- static calculateSize(dynamicTextElement: DocumentElement, entityType: any, text: string): import("../..").SizeDefinition;
58
+ static calculateSize(dynamicTextElement: DocumentElement, entityType: any, text: string): {
59
+ width: number;
60
+ height: number;
61
+ };
57
62
  static getTextFromFrameMemberValues(displayFunction: string, property: any, memberValues: any[]): string;
58
63
  private static getItemCount;
59
64
  }
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DynamicTextUtil = exports.HARD_CODED_TEXT = exports.CLICK_TO_CONFIGURE = exports.MOVE_TO_SECTION_TEXT = exports.MOVE_TO_FRAME_TEXT = exports.DynamicTextDisplayFunction = exports.DYNAMIC_TEXT_DATA_SOURCE = void 0;
4
+ exports.placeholderForMissingContainer = placeholderForMissingContainer;
5
+ exports.isPlaceholderText = isPlaceholderText;
4
6
  exports.isContainerBinding = isContainerBinding;
5
7
  const types_1 = require("@contrail/types");
6
8
  const util_1 = require("@contrail/util");
@@ -28,6 +30,12 @@ var DynamicTextDisplayFunction;
28
30
  exports.MOVE_TO_FRAME_TEXT = 'Move to a frame';
29
31
  exports.MOVE_TO_SECTION_TEXT = 'Move to a section';
30
32
  exports.CLICK_TO_CONFIGURE = 'Click to configure';
33
+ function placeholderForMissingContainer(entityType) {
34
+ return entityType === DYNAMIC_TEXT_DATA_SOURCE.SECTION ? exports.MOVE_TO_SECTION_TEXT : exports.MOVE_TO_FRAME_TEXT;
35
+ }
36
+ function isPlaceholderText(text) {
37
+ return text === exports.MOVE_TO_FRAME_TEXT || text === exports.MOVE_TO_SECTION_TEXT || text === exports.CLICK_TO_CONFIGURE;
38
+ }
31
39
  exports.HARD_CODED_TEXT = {
32
40
  'project.name': 'Project name',
33
41
  'project.createdBy.email': 'Project created by',
@@ -57,7 +65,9 @@ function isContainerBinding(bindingProperty) {
57
65
  class DynamicTextUtil {
58
66
  static isDynamicText(documentElement) {
59
67
  var _a;
60
- return (documentElement === null || documentElement === void 0 ? void 0 : documentElement.type) === 'text' && !!((_a = documentElement.propertyBindings) === null || _a === void 0 ? void 0 : _a.text);
68
+ return ((documentElement === null || documentElement === void 0 ? void 0 : documentElement.type) === 'text' &&
69
+ !!((_a = documentElement.propertyBindings) === null || _a === void 0 ? void 0 : _a.text) &&
70
+ !!documentElement.propertyBindingsMetaData);
61
71
  }
62
72
  static takesValueFromContainer(bindingProperty) {
63
73
  if (!bindingProperty) {
@@ -96,7 +106,7 @@ class DynamicTextUtil {
96
106
  }
97
107
  else {
98
108
  if (!container && !isSpecRelated) {
99
- text = entityType === DYNAMIC_TEXT_DATA_SOURCE.SECTION ? exports.MOVE_TO_SECTION_TEXT : exports.MOVE_TO_FRAME_TEXT;
109
+ text = placeholderForMissingContainer(entityType);
100
110
  }
101
111
  else {
102
112
  if (CONTAINER_SOURCES.includes(entityType)) {
@@ -139,7 +149,7 @@ class DynamicTextUtil {
139
149
  text = exports.HARD_CODED_TEXT[bindingProperty] || (property === null || property === void 0 ? void 0 : property.label);
140
150
  }
141
151
  else if (!container) {
142
- text = exports.MOVE_TO_FRAME_TEXT;
152
+ text = placeholderForMissingContainer(entityType);
143
153
  }
144
154
  else {
145
155
  text = property === null || property === void 0 ? void 0 : property.label;
@@ -149,7 +159,7 @@ class DynamicTextUtil {
149
159
  text = 'n/a';
150
160
  }
151
161
  if (displayLabel &&
152
- ![exports.MOVE_TO_FRAME_TEXT, exports.CLICK_TO_CONFIGURE].includes(text) &&
162
+ !isPlaceholderText(text) &&
153
163
  displayFunction !== DynamicTextDisplayFunction.LABEL &&
154
164
  text !== bindingProperty) {
155
165
  text = `${exports.HARD_CODED_TEXT[bindingProperty] || (property === null || property === void 0 ? void 0 : property.label) || ''}: ${text}`;
@@ -157,16 +167,19 @@ class DynamicTextUtil {
157
167
  return text;
158
168
  }
159
169
  static calculateSize(dynamicTextElement, entityType, text) {
160
- var _a, _b, _c, _d, _e;
161
- let size = (_a = dynamicTextElement.size) !== null && _a !== void 0 ? _a : { width: 0, height: 0 };
162
- const fontFamily = (_b = document_util_1.DocumentStyleUtil.getFirstFontFamily(dynamicTextElement === null || dynamicTextElement === void 0 ? void 0 : dynamicTextElement.text)) !== null && _b !== void 0 ? _b : document_util_1.DEFAULT_TEXT_FONT_FAMILY;
163
- const fontSize = (_e = (dynamicTextElement.isTextTool
164
- ? (_d = (_c = dynamicTextElement.style) === null || _c === void 0 ? void 0 : _c.font) === null || _d === void 0 ? void 0 : _d.size
165
- : document_util_1.DocumentStyleUtil.getFirstFontSize(dynamicTextElement === null || dynamicTextElement === void 0 ? void 0 : dynamicTextElement.text, true))) !== null && _e !== void 0 ? _e : document_util_1.DEFAULT_TEXT_FONT_SIZE;
170
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
171
+ let size = {
172
+ width: (_b = (_a = dynamicTextElement.size) === null || _a === void 0 ? void 0 : _a.width) !== null && _b !== void 0 ? _b : 0,
173
+ height: (_d = (_c = dynamicTextElement.size) === null || _c === void 0 ? void 0 : _c.height) !== null && _d !== void 0 ? _d : 0,
174
+ };
175
+ const fontFamily = (_f = document_util_1.DocumentStyleUtil.getFirstFontFamily((_e = dynamicTextElement === null || dynamicTextElement === void 0 ? void 0 : dynamicTextElement.text) !== null && _e !== void 0 ? _e : '')) !== null && _f !== void 0 ? _f : document_util_1.DEFAULT_TEXT_FONT_FAMILY;
176
+ const fontSize = (_j = (dynamicTextElement.isTextTool
177
+ ? (_h = (_g = dynamicTextElement.style) === null || _g === void 0 ? void 0 : _g.font) === null || _h === void 0 ? void 0 : _h.size
178
+ : document_util_1.DocumentStyleUtil.getFirstFontSize(dynamicTextElement === null || dynamicTextElement === void 0 ? void 0 : dynamicTextElement.text, true))) !== null && _j !== void 0 ? _j : document_util_1.DEFAULT_TEXT_FONT_SIZE;
166
179
  const fontSizePx = document_util_1.DocumentStyleUtil.toPx(fontSize, 4);
167
180
  const padding = dynamicTextElement.isTextTool ? document_util_1.TEXT_TOOL_PADDING : document_util_1.TEXT_BOX_PADDING;
168
- const bold = document_util_1.DocumentStyleUtil.isBold(dynamicTextElement === null || dynamicTextElement === void 0 ? void 0 : dynamicTextElement.text, fontFamily);
169
- const italic = document_util_1.DocumentStyleUtil.isItalic(dynamicTextElement === null || dynamicTextElement === void 0 ? void 0 : dynamicTextElement.text);
181
+ const bold = document_util_1.DocumentStyleUtil.isBold((_k = dynamicTextElement === null || dynamicTextElement === void 0 ? void 0 : dynamicTextElement.text) !== null && _k !== void 0 ? _k : '', fontFamily);
182
+ const italic = document_util_1.DocumentStyleUtil.isItalic((_l = dynamicTextElement === null || dynamicTextElement === void 0 ? void 0 : dynamicTextElement.text) !== null && _l !== void 0 ? _l : '');
170
183
  const measureTextSettings = {
171
184
  fontFamily,
172
185
  fontSize: fontSizePx,
@@ -207,7 +220,7 @@ class DynamicTextUtil {
207
220
  }
208
221
  else if (displayFunction === DynamicTextDisplayFunction.UNIQUE_ARRAY) {
209
222
  text = Array.from(new Set(memberValues
210
- .filter((e) => e !== null && e !== undefined)
223
+ .filter((e) => e !== null && e !== undefined && e !== '')
211
224
  .flat()
212
225
  .map((e) => formatter.formatValueForProperty(e, property))))
213
226
  .sort()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/documents",
3
- "version": "2.4.0-alpha.c01727c",
3
+ "version": "2.4.0-alpha.ce67280",
4
4
  "description": "Documents library for contrail platform",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",