@miragon/wardley-dsl 0.6.0 → 0.7.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.
package/README.md CHANGED
@@ -49,6 +49,19 @@ The result always reads back as the same map as `serializeDSL(map)`. When an edi
49
49
  safely into the source layout, `patchDSL` falls back to the canonical `serializeDSL(map)` text (in
50
50
  the source's line break style). It never throws.
51
51
 
52
+ ### Ids in the document
53
+
54
+ The OWM DSL writes no ids: the parser derives them from names (`cmp_cup_of_tea`), note texts and
55
+ annotation numbers, with a `_2` suffix for a repeat in line order. An editor that creates or renames
56
+ an element holds an id the text does not give it until the text is read again. `readBackIds(map)`
57
+ tells, by the ids of `map`, which id each element carries once `serializeDSL(map)` is read back:
58
+
59
+ ```ts
60
+ import { readBackIds } from '@miragon/wardley-dsl';
61
+
62
+ readBackIds(editedMap).get('shape_42'); // 'cmp_hot_water'
63
+ ```
64
+
52
65
  Part of the [Wardley Maps](https://github.com/Miragon/wardley-maps-modeler) monorepo.
53
66
 
54
67
  ## License
package/dist/index.cjs CHANGED
@@ -595,7 +595,7 @@ var MapReader = class {
595
595
  }
596
596
  /** For a line that looks like a known construct but cannot be parsed. */
597
597
  failed(line) {
598
- this.keep(line, "Line could not be interpreted (kept losslessly in rawPassthrough)");
598
+ this.keep(line, "Line not understood \u2014 kept as written");
599
599
  }
600
600
  // A reference binds to the name as written first, so `X 2` stays bound to an explicit "X 2" even
601
601
  // when a duplicate "X" was renamed to "X 2"; the serializer then writes that reference by the
@@ -2485,6 +2485,28 @@ function fingerprint(map) {
2485
2485
  return entries.sort().join("\n");
2486
2486
  }
2487
2487
 
2488
+ // src/read-back-ids.ts
2489
+ var ELEMENT_KEY = /^(?:element|child):(.*)$/s;
2490
+ function elementIdOf(key) {
2491
+ return ELEMENT_KEY.exec(key)?.[1];
2492
+ }
2493
+ function readBackIds(map) {
2494
+ const lines = serializeKeyedLines(map);
2495
+ const { sourceMap } = parseDSLWithSourceMap([...lines.map((line) => line.text), ""].join("\n"));
2496
+ const readIdByLine = /* @__PURE__ */ new Map();
2497
+ for (const [key, line] of sourceMap.primaryLineOf) {
2498
+ const id = elementIdOf(key);
2499
+ if (id !== void 0) readIdByLine.set(line, id);
2500
+ }
2501
+ const ids = /* @__PURE__ */ new Map();
2502
+ lines.forEach((line, index) => {
2503
+ const id = elementIdOf(line.key);
2504
+ const readId = readIdByLine.get(index + 1);
2505
+ if (id !== void 0 && readId !== void 0) ids.set(id, readId);
2506
+ });
2507
+ return ids;
2508
+ }
2509
+
2488
2510
  // src/raw-passthrough.ts
2489
2511
  function withoutRawStatements(rawPassthrough, keyword) {
2490
2512
  if (!rawPassthrough) return rawPassthrough;
@@ -2524,6 +2546,7 @@ exports.parseDSLWithDiagnostics = parseDSLWithDiagnostics;
2524
2546
  exports.parseDecorators = parseDecorators;
2525
2547
  exports.parseLabelOffset = parseLabelOffset;
2526
2548
  exports.patchDSL = patchDSL;
2549
+ exports.readBackIds = readBackIds;
2527
2550
  exports.serializeDSL = serializeDSL;
2528
2551
  exports.slug = slug;
2529
2552
  exports.withoutRawStatements = withoutRawStatements;