@contrail/documents 1.21.0-alpha.0c990fd → 1.21.0-alpha.1378239

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,10 +1,11 @@
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[]>;
@@ -17,7 +18,7 @@ export declare class DocumentContainerIndex {
17
18
  innermostContainerIdOf(documentElementId: string, elementType: ContainerElementType): string | null;
18
19
  memberIdsOf(containerId: string): readonly string[];
19
20
  memberDocumentElementsOf(containerId: string): DocumentElement[];
20
- containerPathAtPoint(point: PositionDefinition, elementType: ContainerElementType): readonly string[];
21
+ containerPathFor(documentElement: DocumentElement, kind: ContainerKind): readonly string[];
21
22
  private containersOfType;
22
23
  private resolveAll;
23
24
  private containerPath;
@@ -9,6 +9,7 @@ class DocumentContainerIndex {
9
9
  static empty() {
10
10
  return new DocumentContainerIndex({
11
11
  documentElementsById: new Map(),
12
+ measuredById: new Map(),
12
13
  containersById: new Map(),
13
14
  containerPathsByDocumentElementId: new Map(),
14
15
  memberIdsByContainerId: new Map(),
@@ -43,13 +44,17 @@ class DocumentContainerIndex {
43
44
  }
44
45
  return members;
45
46
  }
46
- containerPathAtPoint(point, elementType) {
47
+ containerPathFor(documentElement, kind) {
47
48
  var _a, _b;
48
- const containers = this.containersOfType(elementType);
49
- const covering = containers.filter((container) => document_container_1.DocumentContainerUtil.containsPoint(container.box, point));
50
- const nests = (_b = (_a = containers[0]) === null || _a === void 0 ? void 0 : _a.kind.nests) !== null && _b !== void 0 ? _b : true;
51
- const path = nests ? document_container_1.DocumentContainerUtil.chain(covering) : document_container_1.DocumentContainerUtil.topmost(covering);
52
- return path.map((container) => container.id);
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);
53
58
  }
54
59
  containersOfType(elementType) {
55
60
  var _a;
@@ -25,5 +25,9 @@ export declare class DocumentContainerUtil {
25
25
  static containsPoint(box: CoordinateBox, point: PositionDefinition): boolean;
26
26
  static encloses(outer: CoordinateBox, inner: CoordinateBox): boolean;
27
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[];
28
32
  static topmost(containers: DocumentContainer[]): DocumentContainer[];
29
33
  }
@@ -83,6 +83,56 @@ class DocumentContainerUtil {
83
83
  path.unshift(topmostOf(enclosing));
84
84
  }
85
85
  }
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 && !documentElement.position) {
93
+ return null;
94
+ }
95
+ return DocumentContainerUtil.toCoordinateBox(x, y, width, height);
96
+ }
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
+ }
86
136
  static topmost(containers) {
87
137
  return containers.length
88
138
  ? [containers.reduce((topmost, container) => (container.drawOrder > topmost.drawOrder ? container : topmost))]
@@ -5,9 +5,5 @@ export declare class DocumentContainerService {
5
5
  static buildIndex(documentElements: readonly DocumentElement[], kinds?: ContainerKind[]): DocumentContainerIndex;
6
6
  static isContainer(documentElement: DocumentElement, kinds?: ContainerKind[]): boolean;
7
7
  private static measureDocumentElements;
8
- private static measure;
9
8
  private static collectContainers;
10
- private static resolvePath;
11
- private static enclosersOf;
12
- private static candidateContainers;
13
9
  }
@@ -1,7 +1,6 @@
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
4
  const container_kind_1 = require("./container-kind");
6
5
  const document_container_index_1 = require("./document-container-index");
7
6
  const document_container_1 = require("./document-container");
@@ -26,7 +25,7 @@ class DocumentContainerService {
26
25
  for (const measured of measuredDocumentElements) {
27
26
  const byElementType = new Map();
28
27
  for (const kind of kinds) {
29
- byElementType.set(kind.elementType, this.resolvePath(measured, kind, (_a = containersByElementType.get(kind.elementType)) !== null && _a !== void 0 ? _a : []));
28
+ byElementType.set(kind.elementType, document_container_1.DocumentContainerUtil.pathFor(measured, kind, (_a = containersByElementType.get(kind.elementType)) !== null && _a !== void 0 ? _a : []));
30
29
  }
31
30
  pathsByMeasured.set(measured, byElementType);
32
31
  }
@@ -52,8 +51,13 @@ class DocumentContainerService {
52
51
  }
53
52
  containerPathsByDocumentElementId.set(documentElementId, pathsByElementType);
54
53
  }
54
+ const measuredById = new Map();
55
+ for (const measured of measuredDocumentElements) {
56
+ measuredById.set(measured.documentElement.id, measured);
57
+ }
55
58
  return new document_container_index_1.DocumentContainerIndex({
56
59
  documentElementsById,
60
+ measuredById,
57
61
  containersById,
58
62
  containerPathsByDocumentElementId,
59
63
  memberIdsByContainerId,
@@ -67,24 +71,9 @@ class DocumentContainerService {
67
71
  return documentElements.map((documentElement, drawOrder) => ({
68
72
  documentElement,
69
73
  drawOrder,
70
- box: this.measure(documentElement, documentElementsById),
74
+ box: document_container_1.DocumentContainerUtil.measureBox(documentElement, documentElementsById),
71
75
  }));
72
76
  }
73
- static measure(documentElement, documentElementsById) {
74
- var _a, _b;
75
- try {
76
- 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;
77
- 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;
78
- const { x, y, width, height } = document_element_factory_1.DocumentElementFactory.getBoundingClientRectScaled(documentElement, start, end);
79
- if (!width && !height && !documentElement.position) {
80
- return null;
81
- }
82
- return document_container_1.DocumentContainerUtil.toCoordinateBox(x, y, width, height);
83
- }
84
- catch (_c) {
85
- return null;
86
- }
87
- }
88
77
  static collectContainers(measuredDocumentElements, kind) {
89
78
  return measuredDocumentElements
90
79
  .filter((measured) => measured.documentElement.type === kind.elementType && measured.box)
@@ -96,40 +85,5 @@ class DocumentContainerService {
96
85
  drawOrder: measured.drawOrder,
97
86
  }));
98
87
  }
99
- static resolvePath(measured, kind, containers) {
100
- if (!containers.length || !measured.box) {
101
- return [];
102
- }
103
- if (measured.documentElement.type === kind.elementType) {
104
- return kind.nests ? document_container_1.DocumentContainerUtil.chain(this.enclosersOf(measured, containers)) : [];
105
- }
106
- const candidates = this.candidateContainers(measured, containers);
107
- return kind.nests ? document_container_1.DocumentContainerUtil.chain(candidates) : document_container_1.DocumentContainerUtil.topmost(candidates);
108
- }
109
- static enclosersOf(measured, containers) {
110
- return containers.filter((container) => {
111
- var _a;
112
- return container.id !== measured.documentElement.id &&
113
- document_container_1.DocumentContainerUtil.encloses(container.box, measured.box) &&
114
- measured.drawOrder > container.drawOrder &&
115
- !((_a = measured.documentElement.elementIds) === null || _a === void 0 ? void 0 : _a.includes(container.id));
116
- });
117
- }
118
- static candidateContainers(measured, containers) {
119
- const anchor = document_container_1.DocumentContainerUtil.anchorOf(measured.documentElement, measured.box);
120
- return containers.filter((container) => {
121
- var _a;
122
- if (container.id === measured.documentElement.id || measured.drawOrder <= container.drawOrder) {
123
- return false;
124
- }
125
- if (!document_container_1.DocumentContainerUtil.containsPoint(container.box, anchor)) {
126
- return false;
127
- }
128
- if (document_container_1.DocumentContainerUtil.encloses(measured.box, container.box)) {
129
- return false;
130
- }
131
- return !((_a = measured.documentElement.elementIds) === null || _a === void 0 ? void 0 : _a.includes(container.id));
132
- });
133
- }
134
88
  }
135
89
  exports.DocumentContainerService = DocumentContainerService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/documents",
3
- "version": "1.21.0-alpha.0c990fd",
3
+ "version": "1.21.0-alpha.1378239",
4
4
  "description": "Documents library for contrail platform",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",