@contrail/documents 1.15.0-alpha.567dcf0 → 1.15.0-alpha.a6bee29

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,15 +5,12 @@ 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.15.0] - 2026-08-18
8
+ ## [1.15.0] - 2026-08-21
9
9
 
10
10
  ### Added
11
11
 
12
- - `isValidSortIndex` — whether a value is a usable fractional-indexing key. `fractional-indexing` keeps its own `validateOrderKey` private, so callers that need to check a key before storing it had no way to do so beyond a non-empty-string test. A key that passes that test but breaks the grammar still sorts, so ordering looks fine; the failure surfaces later, when the next insert beside it calls `generateKeyBetween` and throws. One point is stricter than the library: every character must be in the base-62 digit set, since a key holding anything else can sort below the integer part it claims.
13
-
14
- ### Changed
15
-
16
- - `SortOrderErrorMessage.SORT_INDEX_NOT_USABLE` now reads "sortIndex must be a valid fractional-indexing key". The code is unchanged; the old text described only the non-empty-string half of the condition a caller can now check with `isValidSortIndex`.
12
+ - `sortElementsByArrayOrder(elements, sortOrder)` and `assignSortIndexes(orderedElements)` — the two ordering transforms that convert, restore and the document read path had each grown their own copy of. `sortElementsByArrayOrder` puts elements missing from the array last, keeping their relative order.
13
+ - `SORT_MODE` — the named constants for `DocumentSortMode`, alongside the existing `DOCUMENT_SORT_MODES` tuple.
17
14
 
18
15
  ## [1.14.0] - 2026-07-29
19
16
 
@@ -90,7 +90,7 @@ class DocumentElementFactory {
90
90
  return element;
91
91
  }
92
92
  static copyElements(elements, documentId, commonOptions = {}, positionOffset, lastFrameNumber) {
93
- var _a, _b;
93
+ var _a, _b, _c;
94
94
  const oldIdToNewId = new Map();
95
95
  const newElements = [];
96
96
  const groupElements = [];
@@ -118,11 +118,10 @@ class DocumentElementFactory {
118
118
  const oldChildElements = elements.filter((e) => e.tableId === element.id && ['cell', 'column', 'row'].indexOf(e.type) !== -1);
119
119
  let newTableElement, newChildElements = [];
120
120
  try {
121
- [newTableElement, ...newChildElements] = document_table_1.TableCopyService.copy(newElement, oldChildElements);
121
+ [newTableElement, ...newChildElements] = document_table_1.TableCopyService.copy(element, oldChildElements);
122
122
  }
123
- catch (e) { }
124
- if (oldChildElements.length > newChildElements.length || oldChildElements.length === 0) {
125
- telemetry_1.logger.warn({ newElement, oldChildElements, newChildElements }, 'Invalid table element to copy');
123
+ catch (e) {
124
+ telemetry_1.logger.warn({ element, oldChildElements, newChildElements }, (_b = e === null || e === void 0 ? void 0 : e.message) !== null && _b !== void 0 ? _b : 'Invalid table element to copy');
126
125
  continue;
127
126
  }
128
127
  newElement = newTableElement;
@@ -159,7 +158,7 @@ class DocumentElementFactory {
159
158
  newElement.position.x = newElement.position.x + positionOffset.x;
160
159
  newElement.position.y = newElement.position.y + positionOffset.y;
161
160
  }
162
- if (['line', 'callout'].includes(newElement.type) && ((_b = newElement.points) === null || _b === void 0 ? void 0 : _b.length) > 0) {
161
+ if (['line', 'callout'].includes(newElement.type) && ((_c = newElement.points) === null || _c === void 0 ? void 0 : _c.length) > 0) {
163
162
  newElement.points = newElement.points.map(([x, y]) => [x + positionOffset.x, y + positionOffset.y]);
164
163
  }
165
164
  }
@@ -0,0 +1,6 @@
1
+ export interface OrderableElement {
2
+ id?: string;
3
+ sortIndex?: string;
4
+ }
5
+ export declare function sortElementsByArrayOrder<T extends OrderableElement>(elements: T[], sortOrder?: string[]): T[];
6
+ export declare function assignSortIndexes<T extends OrderableElement>(orderedElements: T[]): T[];
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sortElementsByArrayOrder = sortElementsByArrayOrder;
4
+ exports.assignSortIndexes = assignSortIndexes;
5
+ const sort_index_1 = require("./sort-index");
6
+ function sortElementsByArrayOrder(elements, sortOrder) {
7
+ if (!(sortOrder === null || sortOrder === void 0 ? void 0 : sortOrder.length)) {
8
+ return [...elements];
9
+ }
10
+ const positionById = new Map(sortOrder.map((id, index) => [id, index]));
11
+ const unorderedPosition = sortOrder.length;
12
+ return [...elements].sort((a, b) => { var _a, _b; return ((_a = positionById.get(a.id)) !== null && _a !== void 0 ? _a : unorderedPosition) - ((_b = positionById.get(b.id)) !== null && _b !== void 0 ? _b : unorderedPosition); });
13
+ }
14
+ function assignSortIndexes(orderedElements) {
15
+ const sortIndexes = (0, sort_index_1.buildNKeysBetween)(null, null, orderedElements.length);
16
+ return orderedElements.map((element, index) => (Object.assign(Object.assign({}, element), { sortIndex: sortIndexes[index] })));
17
+ }
package/lib/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './types';
2
2
  export * from './sort-index';
3
+ export * from './element-order';
3
4
  export * from './sort-order-errors';
4
5
  export * from './components';
5
6
  export * from './document-element-factory';
package/lib/index.js CHANGED
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./types"), exports);
18
18
  __exportStar(require("./sort-index"), exports);
19
+ __exportStar(require("./element-order"), exports);
19
20
  __exportStar(require("./sort-order-errors"), exports);
20
21
  __exportStar(require("./components"), exports);
21
22
  __exportStar(require("./document-element-factory"), exports);
@@ -1,6 +1,5 @@
1
1
  import { DocumentElement } from './types';
2
2
  export declare function allElementsHaveSortIndex(elements: unknown[]): boolean;
3
- export declare function isValidSortIndex(sortIndex: unknown): sortIndex is string;
4
3
  export declare function buildKeyBetween(a: string | null, b: string | null): string;
5
4
  export declare function buildNKeysBetween(a: string | null, b: string | null, n: number): string[];
6
5
  export declare function compareSortIndex(a: DocumentElement, b: DocumentElement): number;
package/lib/sort-index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.allElementsHaveSortIndex = allElementsHaveSortIndex;
4
- exports.isValidSortIndex = isValidSortIndex;
5
4
  exports.buildKeyBetween = buildKeyBetween;
6
5
  exports.buildNKeysBetween = buildNKeysBetween;
7
6
  exports.compareSortIndex = compareSortIndex;
@@ -10,26 +9,6 @@ function allElementsHaveSortIndex(elements) {
10
9
  return elements.every((element) => typeof element.sortIndex === 'string' &&
11
10
  element.sortIndex.length > 0);
12
11
  }
13
- const SMALLEST_INTEGER = 'A00000000000000000000000000';
14
- function getIntegerPartLength(head) {
15
- if (head >= 'a' && head <= 'z')
16
- return head.charCodeAt(0) - 'a'.charCodeAt(0) + 2;
17
- if (head >= 'A' && head <= 'Z')
18
- return 'Z'.charCodeAt(0) - head.charCodeAt(0) + 2;
19
- return null;
20
- }
21
- function isValidSortIndex(sortIndex) {
22
- if (typeof sortIndex !== 'string' || sortIndex.length === 0)
23
- return false;
24
- if (sortIndex === SMALLEST_INTEGER)
25
- return false;
26
- if (![...sortIndex].every((character) => fractional_indexing_1.BASE_62_DIGITS.includes(character)))
27
- return false;
28
- const integerPartLength = getIntegerPartLength(sortIndex[0]);
29
- if (integerPartLength === null || integerPartLength > sortIndex.length)
30
- return false;
31
- return !sortIndex.slice(integerPartLength).endsWith('0');
32
- }
33
12
  function buildKeyBetween(a, b) {
34
13
  return (0, fractional_indexing_1.generateKeyBetween)(a, b);
35
14
  }
@@ -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 valid fractional-indexing key"
9
+ SORT_INDEX_NOT_USABLE = "sortIndex must be a non-empty string"
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 valid fractional-indexing key";
14
+ SortOrderErrorMessage["SORT_INDEX_NOT_USABLE"] = "sortIndex must be a non-empty string";
15
15
  })(SortOrderErrorMessage || (exports.SortOrderErrorMessage = SortOrderErrorMessage = {}));
@@ -7,6 +7,11 @@ export interface ImportReferenceDetail {
7
7
  export type DocumentWriteMode = 'transactional' | 'legacy';
8
8
  export declare const DOCUMENT_SORT_MODES: readonly ["array", "elementIndexing", "migrating"];
9
9
  export type DocumentSortMode = (typeof DOCUMENT_SORT_MODES)[number];
10
+ export declare const SORT_MODE: {
11
+ readonly ARRAY: "array";
12
+ readonly ELEMENT_INDEXING: "elementIndexing";
13
+ readonly MIGRATING: "migrating";
14
+ };
10
15
  export interface Document {
11
16
  id?: string;
12
17
  name?: string;
@@ -1,4 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DOCUMENT_SORT_MODES = void 0;
3
+ exports.SORT_MODE = exports.DOCUMENT_SORT_MODES = void 0;
4
4
  exports.DOCUMENT_SORT_MODES = ['array', 'elementIndexing', 'migrating'];
5
+ exports.SORT_MODE = {
6
+ ARRAY: 'array',
7
+ ELEMENT_INDEXING: 'elementIndexing',
8
+ MIGRATING: 'migrating',
9
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/documents",
3
- "version": "1.15.0-alpha.567dcf0",
3
+ "version": "1.15.0-alpha.a6bee29",
4
4
  "description": "Documents library for contrail platform",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -40,7 +40,7 @@
40
40
  },
41
41
  "dependencies": {
42
42
  "@contrail/actions": "^1.0.18",
43
- "@contrail/document-table": "^1.1.0",
43
+ "@contrail/document-table": "^1.2.3",
44
44
  "@contrail/document-util": "^1.2.0",
45
45
  "@contrail/types": "^3.4.0",
46
46
  "fractional-indexing": "^2.1.0",