ducjs 4.0.3 → 4.1.0

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.
Binary file
@@ -56,14 +56,14 @@ export const rust_sqlite_wasm_localtime: (a: number) => number;
56
56
  export const rust_sqlite_wasm_realloc: (a: number, b: number) => number;
57
57
  export const sqlite3_os_end: () => number;
58
58
  export const sqlite3_os_init: () => number;
59
+ export const wasm_bindgen_adb4849040331bf2___convert__closures_____invoke___wasm_bindgen_adb4849040331bf2___JsValue__core_ed718c3d60ebd546___result__Result_____wasm_bindgen_adb4849040331bf2___JsError___true_: (a: number, b: number, c: any) => [number, number];
60
+ export const wasm_bindgen_adb4849040331bf2___convert__closures_____invoke___js_sys_495b1fea2679fcbe___Function_fn_wasm_bindgen_adb4849040331bf2___JsValue_____wasm_bindgen_adb4849040331bf2___sys__Undefined___js_sys_495b1fea2679fcbe___Function_fn_wasm_bindgen_adb4849040331bf2___JsValue_____wasm_bindgen_adb4849040331bf2___sys__Undefined_______true_: (a: number, b: number, c: any, d: any) => void;
59
61
  export const __wbindgen_malloc: (a: number, b: number) => number;
60
62
  export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
61
63
  export const __wbindgen_exn_store: (a: number) => void;
62
64
  export const __externref_table_alloc: () => number;
63
- export const __wbindgen_export_4: WebAssembly.Table;
65
+ export const __wbindgen_externrefs: WebAssembly.Table;
66
+ export const __wbindgen_destroy_closure: (a: number, b: number) => void;
64
67
  export const __wbindgen_free: (a: number, b: number, c: number) => void;
65
- export const __wbindgen_export_6: WebAssembly.Table;
66
68
  export const __externref_table_dealloc: (a: number) => void;
67
- export const closure101_externref_shim: (a: number, b: number, c: any) => void;
68
- export const closure82_externref_shim: (a: number, b: number, c: any, d: any) => void;
69
69
  export const __wbindgen_start: () => void;
@@ -1,2 +1,3 @@
1
1
  export * from "./restoreDataState";
2
2
  export * from "./restoreElements";
3
+ export * from "./updateModelCode";
@@ -1,2 +1,3 @@
1
1
  export * from "./restoreDataState";
2
2
  export * from "./restoreElements";
3
+ export * from "./updateModelCode";
@@ -1,4 +1,4 @@
1
- import { nanoid } from "nanoid";
1
+ import { randomId } from "../utils/math/random";
2
2
  import tinycolor from "tinycolor2";
3
3
  import { BEZIER_MIRRORING, BLENDING, BOOLEAN_OPERATION, ELEMENT_CONTENT_PREFERENCE, IMAGE_STATUS, LINE_HEAD, STROKE_CAP, STROKE_JOIN, STROKE_PLACEMENT, STROKE_PREFERENCE, STROKE_SIDE_PREFERENCE, TEXT_ALIGN, VERTICAL_ALIGN, } from "../enums";
4
4
  import { getPrecisionScope } from "../technical/measurements";
@@ -26,7 +26,7 @@ export const restore = (data, elementsConfig, restoreConfig = {}) => {
26
26
  const restoredIssues = restoreIssues(data === null || data === void 0 ? void 0 : data.issues);
27
27
  // Generate a new ID if none exists or if it's empty
28
28
  const parsedId = data === null || data === void 0 ? void 0 : data.id;
29
- const restoredId = (parsedId && parsedId.trim().length > 0) ? parsedId : nanoid();
29
+ const restoredId = (parsedId && parsedId.trim().length > 0) ? parsedId : randomId();
30
30
  return {
31
31
  dictionary: restoredDictionary,
32
32
  thumbnail: isValidUint8Array(data === null || data === void 0 ? void 0 : data.thumbnail),
@@ -3,6 +3,7 @@ import { isElbowArrow, isLinearElement, isTextElement, } from "../types";
3
3
  import { arrayToMap, bumpVersion, DEFAULT_ELEMENT_PROPS, DEFAULT_ELLIPSE_ELEMENT, DEFAULT_FONT_FAMILY, DEFAULT_FONT_SIZE, DEFAULT_FREEDRAW_ELEMENT, detectLineHeight, FONT_FAMILY, getContainerElement, getDefaultTableData, getNormalizedDimensions, getNormalizedPoints, getSizeFromPoints, getUpdatedTimestamp, isInvisiblySmallElement, LINE_CONFIRM_THRESHOLD, mergeOverlappingPoints, migratePoints, normalizeFixedPoint, normalizeLink, randomId, refreshTextDimensions, validateClosedPath } from "../utils";
4
4
  import { isValidBezierMirroringValue, isValidBlendingValue, isValidBoolean, isValidBoolean as isValidBooleanValue, // Renaming to avoid conflict
5
5
  isValidColor, isValidDucHead, isValidFunction, isValidImageScaleValue, isValidImageStatusValue, isValidPercentageValue, isValidPolygonSides, isValidRadianValue, isValidString, isValidTextAlignValue, isValidVerticalAlignValue, restoreDucStackProperties, restorePrecisionValue, validateBackground, validateStroke } from "./restoreDataState";
6
+ import { updateModelCodeElementIds } from "./updateModelCode";
6
7
  import { LINE_SPACING_TYPE, } from "../enums";
7
8
  const PARAMETRIC_SOURCE_TYPE = { CODE: 10, FILE: 11 };
8
9
  import tinycolor from "tinycolor2";
@@ -588,6 +589,7 @@ export const migrateArrayOrderToZIndex = (elements) => {
588
589
  };
589
590
  export const restoreElements = (elements, currentScope, restoredBlocks, opts) => {
590
591
  const existingIds = new Set();
592
+ const idMap = new Map();
591
593
  // First restore all elements with their original properties
592
594
  const restored = (elements || []).reduce((elements, element) => {
593
595
  if (element.type !== "selection") {
@@ -606,7 +608,10 @@ export const restoreElements = (elements, currentScope, restoredBlocks, opts) =>
606
608
  if (migratedElement &&
607
609
  (isPassThrough || !isInvisiblySmallElement(migratedElement))) {
608
610
  if (existingIds.has(migratedElement.id)) {
609
- migratedElement = Object.assign(Object.assign({}, migratedElement), { id: randomId() });
611
+ const oldId = migratedElement.id;
612
+ const newId = randomId();
613
+ migratedElement = Object.assign(Object.assign({}, migratedElement), { id: newId });
614
+ idMap.set(oldId, newId);
610
615
  }
611
616
  existingIds.add(migratedElement.id);
612
617
  elements.push(migratedElement);
@@ -614,6 +619,13 @@ export const restoreElements = (elements, currentScope, restoredBlocks, opts) =>
614
619
  }
615
620
  return elements;
616
621
  }, []);
622
+ if (idMap.size > 0) {
623
+ for (const el of restored) {
624
+ if (el.type === "model" && typeof el.code === "string" && el.code) {
625
+ el.code = updateModelCodeElementIds(el.code, idMap);
626
+ }
627
+ }
628
+ }
617
629
  let restoredElements = (opts === null || opts === void 0 ? void 0 : opts.syncInvalidIndices)
618
630
  ? opts.syncInvalidIndices(restored, currentScope)
619
631
  : restored;
@@ -0,0 +1,11 @@
1
+ export declare const getEncodedPythonModelModuleName: (elementId: string) => string;
2
+ export declare const getPythonModelModuleName: (elementId: string) => string;
3
+ /**
4
+ * Updates Python code referencing model element IDs when IDs are restored or duplicated.
5
+ * Replaces references in:
6
+ * - `from duc_model.<id_or_module> import ...`
7
+ * - `import duc_model.<id_or_module>`
8
+ * - `duc_model.<id_or_module>`
9
+ * - `from duc_model import <id_or_module>`
10
+ */
11
+ export declare const updateModelCodeElementIds: (code: string, idMap: Map<string, string> | Record<string, string>) => string;
@@ -0,0 +1,63 @@
1
+ const PYTHON_IDENTIFIER_PATTERN = /^[A-Za-z_]\w*$/u;
2
+ export const getEncodedPythonModelModuleName = (elementId) => {
3
+ const encodedId = Array.from(new TextEncoder().encode(elementId))
4
+ .map((byte) => byte.toString(16).padStart(2, "0"))
5
+ .join("");
6
+ return `element_${encodedId || "00"}`;
7
+ };
8
+ export const getPythonModelModuleName = (elementId) => (PYTHON_IDENTIFIER_PATTERN.test(elementId)
9
+ ? elementId
10
+ : getEncodedPythonModelModuleName(elementId));
11
+ const escapeRegex = (value) => (value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
12
+ /**
13
+ * Updates Python code referencing model element IDs when IDs are restored or duplicated.
14
+ * Replaces references in:
15
+ * - `from duc_model.<id_or_module> import ...`
16
+ * - `import duc_model.<id_or_module>`
17
+ * - `duc_model.<id_or_module>`
18
+ * - `from duc_model import <id_or_module>`
19
+ */
20
+ export const updateModelCodeElementIds = (code, idMap) => {
21
+ if (!code || typeof code !== "string") {
22
+ return code;
23
+ }
24
+ const entries = [];
25
+ const entriesIterable = idMap instanceof Map ? idMap.entries() : Object.entries(idMap);
26
+ for (const [oldId, newId] of entriesIterable) {
27
+ if (!oldId || !newId || oldId === newId) {
28
+ continue;
29
+ }
30
+ const oldModuleName = getPythonModelModuleName(oldId);
31
+ const newModuleName = getPythonModelModuleName(newId);
32
+ const oldEncoded = getEncodedPythonModelModuleName(oldId);
33
+ const targets = new Set();
34
+ targets.add(oldId);
35
+ targets.add(oldModuleName);
36
+ targets.add(oldEncoded);
37
+ entries.push({
38
+ targets: Array.from(targets).sort((a, b) => b.length - a.length),
39
+ replacement: newModuleName,
40
+ });
41
+ }
42
+ if (entries.length === 0) {
43
+ return code;
44
+ }
45
+ let updatedCode = code;
46
+ for (const { targets, replacement } of entries) {
47
+ for (const target of targets) {
48
+ const escaped = escapeRegex(target);
49
+ const boundary = /^\w+$/.test(target) ? "\\b" : "(?=[^A-Za-z0-9_-]|$)";
50
+ // 1. Replace `duc_model.<target>`
51
+ const ducModelRegex = new RegExp(`(duc_model\\.)(?:${escaped})${boundary}`, "g");
52
+ updatedCode = updatedCode.replace(ducModelRegex, `$1${replacement}`);
53
+ // 2. Replace `from duc_model import ...` items
54
+ const fromImportRegex = /(from\s+duc_model\s+import\s+)([^#\n\r]+)/g;
55
+ updatedCode = updatedCode.replace(fromImportRegex, (_full, prefix, importedList) => {
56
+ const itemRegex = new RegExp(`(^|[,\\s])${escaped}(\\b|(?=[\\s,]|$))`, "g");
57
+ const replacedList = importedList.replace(itemRegex, `$1${replacement}$2`);
58
+ return `${prefix}${replacedList}`;
59
+ });
60
+ }
61
+ }
62
+ return updatedCode;
63
+ };
@@ -1,4 +1,4 @@
1
- import { nanoid } from "nanoid";
1
+ import { customAlphabet } from "nanoid";
2
2
  export function randomSeed() {
3
3
  return Math.floor(Math.random() * Math.pow(2, 31));
4
4
  }
@@ -22,4 +22,9 @@ export const reseed = (seed) => {
22
22
  random = new Random(seed);
23
23
  testIdBase = 0;
24
24
  };
25
- export const randomId = () => nanoid();
25
+ // Python-identifier-safe alphabet: letters, digits, underscore (no hyphens)
26
+ const ID_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
27
+ const ID_INITIAL_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
28
+ const generateBody = customAlphabet(ID_ALPHABET, 20);
29
+ const generateInitial = customAlphabet(ID_INITIAL_ALPHABET, 1);
30
+ export const randomId = () => `${generateInitial()}${generateBody()}`;
@@ -22,7 +22,7 @@ export const getZoom = (value, mainScope, scopeExponentThreshold) => {
22
22
  };
23
23
  export const getDefaultGlobalState = () => {
24
24
  return {
25
- viewBackgroundColor: typeof window !== "undefined" ? (window.matchMedia("(prefers-color-scheme: dark)").matches ? COLOR_PALETTE.night : COLOR_PALETTE.white) : COLOR_PALETTE.white,
25
+ viewBackgroundColor: COLOR_PALETTE.white,
26
26
  scopeExponentThreshold: 3,
27
27
  mainScope: NEUTRAL_SCOPE,
28
28
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ducjs",
3
- "version": "4.0.3",
3
+ "version": "4.1.0",
4
4
  "description": "SQLite-backed project-state format for physical-engineering work before execution.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -50,10 +50,10 @@
50
50
  "undici-types": "^6.21.0"
51
51
  },
52
52
  "dependencies": {
53
- "@braintree/sanitize-url": "6.0.2",
54
- "browser-fs-access": "0.35.0",
55
- "fflate": "^0.8.2",
56
- "nanoid": "5.1.5",
53
+ "@braintree/sanitize-url": "^7.1.1",
54
+ "browser-fs-access": "^0.38.0",
55
+ "fflate": "^0.8.3",
56
+ "nanoid": "^5.1.6",
57
57
  "perfect-freehand": "1.2.2",
58
58
  "tinycolor2": "1.6.0"
59
59
  },