@bpmnkit/core 1.0.0 → 1.2.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.
Files changed (73) hide show
  1. package/README.md +30 -3
  2. package/dist/bpmn/agentic.js +3 -1
  3. package/dist/bpmn/auto-layout.js +184 -15
  4. package/dist/bpmn/bpmn-builder.js +69 -7
  5. package/dist/bpmn/bpmn-model.d.ts +78 -0
  6. package/dist/bpmn/bpmn-parser.js +195 -37
  7. package/dist/bpmn/bpmn-serializer.js +121 -72
  8. package/dist/bpmn/bpmnlint.d.ts +153 -0
  9. package/dist/bpmn/bpmnlint.js +364 -0
  10. package/dist/bpmn/camunda-compat-data.d.ts +165 -0
  11. package/dist/bpmn/camunda-compat-data.js +423 -0
  12. package/dist/bpmn/camunda-compat-feel.d.ts +68 -0
  13. package/dist/bpmn/camunda-compat-feel.js +338 -0
  14. package/dist/bpmn/camunda-compat.d.ts +120 -0
  15. package/dist/bpmn/camunda-compat.js +1677 -0
  16. package/dist/bpmn/camunda7/juel.d.ts +41 -0
  17. package/dist/bpmn/camunda7/juel.js +333 -0
  18. package/dist/bpmn/camunda7/migrate.d.ts +72 -0
  19. package/dist/bpmn/camunda7/migrate.js +1152 -0
  20. package/dist/bpmn/compact.d.ts +4 -0
  21. package/dist/bpmn/compact.js +80 -0
  22. package/dist/bpmn/full-operations.js +5 -2
  23. package/dist/bpmn/label-style.d.ts +51 -0
  24. package/dist/bpmn/label-style.js +116 -0
  25. package/dist/bpmn/lint.d.ts +23 -3
  26. package/dist/bpmn/lint.js +47 -10
  27. package/dist/bpmn/optimize/bpmnlint-rules.d.ts +31 -0
  28. package/dist/bpmn/optimize/bpmnlint-rules.js +916 -0
  29. package/dist/bpmn/optimize/deploy.d.ts +8 -2
  30. package/dist/bpmn/optimize/deploy.js +16 -3
  31. package/dist/bpmn/optimize/index.js +11 -2
  32. package/dist/bpmn/optimize/patterns.js +3 -0
  33. package/dist/bpmn/optimize/types.d.ts +14 -0
  34. package/dist/bpmn/optimize/variable-flow.js +4 -1
  35. package/dist/bpmn/process-delta.d.ts +88 -0
  36. package/dist/bpmn/process-delta.js +236 -0
  37. package/dist/bpmn/process-text-writer.d.ts +53 -0
  38. package/dist/bpmn/process-text-writer.js +193 -0
  39. package/dist/bpmn/process-text.d.ts +175 -0
  40. package/dist/bpmn/process-text.js +1255 -0
  41. package/dist/bpmn/process-types.d.ts +112 -0
  42. package/dist/bpmn/process-types.js +490 -0
  43. package/dist/bpmn/svg.js +51 -22
  44. package/dist/doc/docx.d.ts +20 -0
  45. package/dist/doc/docx.js +253 -0
  46. package/dist/doc/html.d.ts +21 -0
  47. package/dist/doc/html.js +303 -0
  48. package/dist/doc/index.d.ts +7 -0
  49. package/dist/doc/index.js +5 -0
  50. package/dist/doc/markdown.d.ts +11 -0
  51. package/dist/doc/markdown.js +160 -0
  52. package/dist/doc/model.d.ts +129 -0
  53. package/dist/doc/model.js +617 -0
  54. package/dist/doc/zip.d.ts +5 -0
  55. package/dist/doc/zip.js +75 -0
  56. package/dist/index.d.ts +22 -0
  57. package/dist/index.js +11 -0
  58. package/dist/layout/annotations.d.ts +4 -2
  59. package/dist/layout/annotations.js +31 -2
  60. package/dist/layout/collaboration/ordering.d.ts +8 -0
  61. package/dist/layout/collaboration/ordering.js +57 -18
  62. package/dist/layout/orthogonal.d.ts +32 -0
  63. package/dist/layout/orthogonal.js +367 -0
  64. package/dist/layout/repair.d.ts +31 -0
  65. package/dist/layout/repair.js +220 -0
  66. package/dist/layout/semantic/route.js +45 -3
  67. package/dist/node/bpmnlint.d.ts +109 -0
  68. package/dist/node/bpmnlint.js +198 -0
  69. package/dist/node/index.d.ts +2 -0
  70. package/dist/node/index.js +1 -0
  71. package/dist/xml/xml-patch.d.ts +21 -0
  72. package/dist/xml/xml-patch.js +27 -1
  73. package/package.json +2 -2
package/dist/index.d.ts CHANGED
@@ -6,8 +6,16 @@ export { Bpmn, SAMPLE_BPMN_XML } from "./bpmn/index.js";
6
6
  export { applyAutoLayout } from "./bpmn/auto-layout.js";
7
7
  export { diffSemantics, projectSemantics, semanticHash } from "./bpmn/semantic-hash.js";
8
8
  export type { JsonValue, SemanticDiff, SemanticProjection } from "./bpmn/semantic-hash.js";
9
+ export { extractProcessContract, generateProcessTypes } from "./bpmn/process-types.js";
10
+ export type { CodeContract, ContractElementRef, ContractHeader, ContractVariable, JobContract, MessageContract, ProcessContract, ProcessTypesOptions, } from "./bpmn/process-types.js";
9
11
  export { detectExecutionPlatform, lintCategories, lintDiagram } from "./bpmn/lint.js";
10
12
  export type { DetectedPlatform, ExecutionPlatform, LintDiagnostic, LintOptions, LintReport, } from "./bpmn/lint.js";
13
+ export { BPMNLINT_RULE_MAP, applyBpmnlintConfig, bpmnlintRuleForFinding, normalizeBpmnlintRuleName, parseBpmnlintConfig, resolveBpmnlintConfig, } from "./bpmn/bpmnlint.js";
14
+ export type { ApplyBpmnlintOptions, BpmnlintApplication, BpmnlintConfig, BpmnlintMatch, BpmnlintRuleMapping, BpmnlintRuleSetting, BpmnlintRuleValue, BpmnlintSeverity, ResolvedBpmnlintConfig, UnsupportedBpmnlintRule, } from "./bpmn/bpmnlint.js";
15
+ export { analyzeCamundaCompat, applyCamundaCompatConfig, isCamundaCompatFinding, normalizeCamundaVersion, splitCamundaCompatConfig, } from "./bpmn/camunda-compat.js";
16
+ export type { CamundaCompatConfig } from "./bpmn/camunda-compat.js";
17
+ export { CAMUNDA_COMPAT_PLUGIN_VERSION, CAMUNDA_COMPAT_RULES, CAMUNDA_COMPAT_VERSIONS, } from "./bpmn/camunda-compat-data.js";
18
+ export type { CamundaCompatCoverage, CamundaCompatRule, CamundaCompatVersion, } from "./bpmn/camunda-compat-data.js";
11
19
  export { diffDiagram } from "./bpmn/diagram-diff.js";
12
20
  export { exportPreserving, exportPreservingResult, preserveBpmnFormatting, } from "./bpmn/preserving-writer.js";
13
21
  export type { PreservingWriteResult } from "./bpmn/preserving-writer.js";
@@ -45,6 +53,8 @@ export { exportFormPreserving, preserveFormFormatting } from "./form/preserving-
45
53
  export type { JsonKind, JsonMember, JsonNode, PreservedJson } from "./json/index.js";
46
54
  export { readDiColor, writeDiColor, BIOC_NS, COLOR_NS } from "./bpmn/di-color.js";
47
55
  export type { DiColor } from "./bpmn/di-color.js";
56
+ export { collectLabelStyles, labelFontCss, resolveLabelFont } from "./bpmn/label-style.js";
57
+ export type { BpmnLabelFont, LabelFontCss } from "./bpmn/label-style.js";
48
58
  export { optimize } from "./bpmn/optimize/index.js";
49
59
  export { renderStoryHtml } from "./bpmn/story.js";
50
60
  export type { StoryRenderOptions } from "./bpmn/story.js";
@@ -59,6 +69,12 @@ export { ELEMENT_SIZES, GRID_CELL_HEIGHT, GRID_CELL_WIDTH } from "./layout/index
59
69
  export { compactify, expand } from "./bpmn/compact.js";
60
70
  export { createCompactStream } from "./bpmn/compact-stream.js";
61
71
  export type { CompactStream, CompactStreamOptions } from "./bpmn/compact-stream.js";
72
+ export { PROCESS_TEXT_GUIDE, conditionOrLabel, createProcessTextStream, parseProcessText, } from "./bpmn/process-text.js";
73
+ export type { ProcessTextProblem, ProcessTextQuestion, ProcessTextResult, ProcessTextStream, } from "./bpmn/process-text.js";
74
+ export { PROCESS_DELTA_GUIDE, parseProcessDelta } from "./bpmn/process-delta.js";
75
+ export type { DeltaFlow, DeltaNode, ProcessDelta } from "./bpmn/process-delta.js";
76
+ export { writableCondition, writableLabel, writableName, writeProcessText, } from "./bpmn/process-text-writer.js";
77
+ export type { WrittenProcessText } from "./bpmn/process-text-writer.js";
62
78
  export { ELEMENT_GROUP_ORDER, ELEMENT_TYPE_GROUPS, allElementTypes, elementTypesInGroup, } from "./bpmn/element-catalog.js";
63
79
  export type { ElementTypeGroup } from "./bpmn/element-catalog.js";
64
80
  export { createFlowElement, retypeElement } from "./bpmn/element-shape.js";
@@ -68,8 +84,14 @@ export { applyBpmnOperations, reconcileCompact, OperationError } from "./bpmn/fu
68
84
  export type { ApplyBpmnOperationsOptions, ApplyBpmnOperationsResult, OperationProblem, } from "./bpmn/full-operations.js";
69
85
  export { buildValidationDmn, findValidationStructure, getValidationInputNames, insertValidationStructure, removeValidationStructure, validationDecisionId, } from "./bpmn/input-validation.js";
70
86
  export type { InputVariableDef, ValidationStructure, ValidationVariableType, } from "./bpmn/input-validation.js";
87
+ export { analyzeCamunda7, convertCamunda7 } from "./bpmn/camunda7/migrate.js";
88
+ export type { Camunda7Conversion, Camunda7ConvertOptions, Camunda7Finding, Camunda7Report, Camunda7Severity, } from "./bpmn/camunda7/migrate.js";
89
+ export { translateJuelToFeel } from "./bpmn/camunda7/juel.js";
90
+ export type { JuelToFeelOptions, JuelToFeelResult } from "./bpmn/camunda7/juel.js";
71
91
  export { exportSvg } from "./bpmn/svg.js";
72
92
  export type { SvgExportOptions } from "./bpmn/svg.js";
93
+ export { buildProcessDocumentation, documentationToDocx, documentationToHtml, documentationToMarkdown, renderDocumentationDocx, renderDocumentationHtml, renderDocumentationMarkdown, } from "./doc/index.js";
94
+ export type { DocumentationLink, DocumentationOptions, DocumentedDecision, DocumentedElement, DocumentedForm, DocumentedFormField, DocumentedLane, DocumentedMessageFlow, DocumentedProcess, DocumentedProperty, DocxDocumentationOptions, ProcessDocumentation, } from "./doc/index.js";
73
95
  export type { CompactDiagram, CompactElement, CompactFlow, CompactProcess, } from "./bpmn/compact.js";
74
96
  export { compilePlan, extractPlan, mergePlan, slugify, uniqueId, } from "./plan/index.js";
75
97
  export type { CompilePlanOptions, ConnectorApplyResult, ConnectorResolver, ExtractResult, PlanAgentTool, PlanAiAgentStep, PlanBranch, PlanBusinessRuleTaskStep, PlanCallActivityStep, PlanConnectorRef, PlanConnectorStep, PlanEndStep, PlanErrorBoundary, PlanGatewayStep, PlanInputVariable, PlanProblem, PlanRawStep, PlanReceiveTaskStep, PlanScenario, PlanScriptTaskStep, PlanSendTaskStep, PlanServiceTaskStep, PlanStartStep, PlanStep, PlanSubProcessStep, PlanTimerBoundary, PlanUserTaskStep, PlanWaitStep, ProcessPlan, SynthResult, UnsupportedElement, } from "./plan/index.js";
package/dist/index.js CHANGED
@@ -4,7 +4,11 @@ export { findElement, findElementInProcess, findProcess, findSequenceFlow, getAl
4
4
  export { Bpmn, SAMPLE_BPMN_XML } from "./bpmn/index.js";
5
5
  export { applyAutoLayout } from "./bpmn/auto-layout.js";
6
6
  export { diffSemantics, projectSemantics, semanticHash } from "./bpmn/semantic-hash.js";
7
+ export { extractProcessContract, generateProcessTypes } from "./bpmn/process-types.js";
7
8
  export { detectExecutionPlatform, lintCategories, lintDiagram } from "./bpmn/lint.js";
9
+ export { BPMNLINT_RULE_MAP, applyBpmnlintConfig, bpmnlintRuleForFinding, normalizeBpmnlintRuleName, parseBpmnlintConfig, resolveBpmnlintConfig, } from "./bpmn/bpmnlint.js";
10
+ export { analyzeCamundaCompat, applyCamundaCompatConfig, isCamundaCompatFinding, normalizeCamundaVersion, splitCamundaCompatConfig, } from "./bpmn/camunda-compat.js";
11
+ export { CAMUNDA_COMPAT_PLUGIN_VERSION, CAMUNDA_COMPAT_RULES, CAMUNDA_COMPAT_VERSIONS, } from "./bpmn/camunda-compat-data.js";
8
12
  export { diffDiagram } from "./bpmn/diagram-diff.js";
9
13
  export { exportPreserving, exportPreservingResult, preserveBpmnFormatting, } from "./bpmn/preserving-writer.js";
10
14
  export { exportDmnPreserving, preserveDmnFormatting } from "./dmn/preserving-writer.js";
@@ -24,6 +28,7 @@ export { parseXml, parseXmlSpans, preserveFormatting, preserveFormattingVerified
24
28
  export { parseJsonSpans, preserveJsonFormatting } from "./json/index.js";
25
29
  export { exportFormPreserving, preserveFormFormatting } from "./form/preserving-writer.js";
26
30
  export { readDiColor, writeDiColor, BIOC_NS, COLOR_NS } from "./bpmn/di-color.js";
31
+ export { collectLabelStyles, labelFontCss, resolveLabelFont } from "./bpmn/label-style.js";
27
32
  export { optimize } from "./bpmn/optimize/index.js";
28
33
  export { renderStoryHtml } from "./bpmn/story.js";
29
34
  export { analyzeVariableFlow, extractFeelIdentifiers } from "./bpmn/optimize/variable-flow.js";
@@ -32,11 +37,17 @@ export { benchmarkLayout, compareLayouts, formatBenchmarkResult, generateAutoLay
32
37
  export { ELEMENT_SIZES, GRID_CELL_HEIGHT, GRID_CELL_WIDTH } from "./layout/index.js";
33
38
  export { compactify, expand } from "./bpmn/compact.js";
34
39
  export { createCompactStream } from "./bpmn/compact-stream.js";
40
+ export { PROCESS_TEXT_GUIDE, conditionOrLabel, createProcessTextStream, parseProcessText, } from "./bpmn/process-text.js";
41
+ export { PROCESS_DELTA_GUIDE, parseProcessDelta } from "./bpmn/process-delta.js";
42
+ export { writableCondition, writableLabel, writableName, writeProcessText, } from "./bpmn/process-text-writer.js";
35
43
  export { ELEMENT_GROUP_ORDER, ELEMENT_TYPE_GROUPS, allElementTypes, elementTypesInGroup, } from "./bpmn/element-catalog.js";
36
44
  export { createFlowElement, retypeElement } from "./bpmn/element-shape.js";
37
45
  export { applyOperations } from "./bpmn/operations.js";
38
46
  export { applyBpmnOperations, reconcileCompact, OperationError } from "./bpmn/full-operations.js";
39
47
  export { buildValidationDmn, findValidationStructure, getValidationInputNames, insertValidationStructure, removeValidationStructure, validationDecisionId, } from "./bpmn/input-validation.js";
48
+ export { analyzeCamunda7, convertCamunda7 } from "./bpmn/camunda7/migrate.js";
49
+ export { translateJuelToFeel } from "./bpmn/camunda7/juel.js";
40
50
  export { exportSvg } from "./bpmn/svg.js";
51
+ export { buildProcessDocumentation, documentationToDocx, documentationToHtml, documentationToMarkdown, renderDocumentationDocx, renderDocumentationHtml, renderDocumentationMarkdown, } from "./doc/index.js";
41
52
  export { compilePlan, extractPlan, mergePlan, slugify, uniqueId, } from "./plan/index.js";
42
53
  //# sourceMappingURL=index.js.map
@@ -5,7 +5,7 @@
5
5
  * shapes. Constants, formulas and control flow match the reference exactly.
6
6
  */
7
7
  import type { BpmnProcess } from "../bpmn/bpmn-model.js";
8
- import type { Bounds, LayoutNode, Waypoint } from "./types.js";
8
+ import type { Bounds, LayoutEdge, LayoutNode, Waypoint } from "./types.js";
9
9
  /**
10
10
  * Sizes and packs text annotations around their linked elements without
11
11
  * overlapping each other or any other layout node (incl. node labels).
@@ -15,7 +15,9 @@ import type { Bounds, LayoutNode, Waypoint } from "./types.js";
15
15
  * origin and pushed clear of everything else already placed, mirroring the
16
16
  * pre-port fallback in auto-layout.ts's computeAnnotationLocalBounds.
17
17
  */
18
- export declare function packAnnotations(process: BpmnProcess, layoutNodes: LayoutNode[]): Map<string, Bounds>;
18
+ export declare function packAnnotations(process: BpmnProcess, layoutNodes: LayoutNode[],
19
+ /** Routed connections: an annotation box must not sit on top of one. */
20
+ layoutEdges?: LayoutEdge[]): Map<string, Bounds>;
19
21
  /**
20
22
  * Edge-to-edge, clamped association waypoints between a linked element and
21
23
  * its annotation. Port of `chooseWaypoints` (tmp/01-annotation-layouting.cjs:365-389).
@@ -10,6 +10,8 @@ const MIN_HEIGHT = 30;
10
10
  const HORIZONTAL_SHIFTS = [0, 60, -60, 120, -120, 180, -180, 240, -240];
11
11
  /** Cost added to a candidate whose association line would cross a shape. */
12
12
  const BLOCKED_LINE_COST = 10_000;
13
+ /** Cost added per routed connection the association line would cut across. */
14
+ const CROSSED_ROUTE_COST = 200;
13
15
  function computeHeight(text, width) {
14
16
  if (!text || !text.trim())
15
17
  return MIN_HEIGHT;
@@ -105,7 +107,9 @@ function overlapsPadded(a, others, padding) {
105
107
  * origin and pushed clear of everything else already placed, mirroring the
106
108
  * pre-port fallback in auto-layout.ts's computeAnnotationLocalBounds.
107
109
  */
108
- export function packAnnotations(process, layoutNodes) {
110
+ export function packAnnotations(process, layoutNodes,
111
+ /** Routed connections: an annotation box must not sit on top of one. */
112
+ layoutEdges = []) {
109
113
  const result = new Map();
110
114
  if (process.textAnnotations.length === 0)
111
115
  return result;
@@ -145,6 +149,23 @@ export function packAnnotations(process, layoutNodes) {
145
149
  if (n.labelBounds)
146
150
  obstacles.push(n.labelBounds);
147
151
  }
152
+ // Each segment of a route as a hairline box; the box packer then keeps
153
+ // annotations off routes the same way it keeps them off shapes.
154
+ const routeObstacles = [];
155
+ for (const edge of layoutEdges) {
156
+ for (let i = 0; i + 1 < edge.waypoints.length; i++) {
157
+ const a = edge.waypoints[i];
158
+ const b = edge.waypoints[i + 1];
159
+ if (!a || !b)
160
+ continue;
161
+ routeObstacles.push({
162
+ x: Math.min(a.x, b.x),
163
+ y: Math.min(a.y, b.y),
164
+ width: Math.abs(a.x - b.x),
165
+ height: Math.abs(a.y - b.y),
166
+ });
167
+ }
168
+ }
148
169
  function packSide(side) {
149
170
  const list = items
150
171
  .filter((it) => it.side === side)
@@ -180,6 +201,11 @@ export function packAnnotations(process, layoutNodes) {
180
201
  continue;
181
202
  intervals.push({ top: sh.y - ELEMENT_GAP, bottom: sh.y + sh.height + ELEMENT_GAP });
182
203
  }
204
+ for (const r of routeObstacles) {
205
+ if (ax2 + ANN_GAP <= r.x || r.x + r.width + ANN_GAP <= ax1)
206
+ continue;
207
+ intervals.push({ top: r.y - ANN_GAP, bottom: r.y + r.height + ANN_GAP });
208
+ }
183
209
  let y = naturalY;
184
210
  let changed = true;
185
211
  while (changed) {
@@ -201,7 +227,10 @@ export function packAnnotations(process, layoutNodes) {
201
227
  };
202
228
  const { pElem, pAnn } = associationWaypoints(linked.bounds, candidate);
203
229
  const crosses = obstacles.some((sh) => sh !== linked.bounds && segmentHitsBox(pElem, pAnn, sh));
204
- const cost = Math.hypot(candidateX - naturalX, y - naturalY) + (crosses ? BLOCKED_LINE_COST : 0);
230
+ const cuts = routeObstacles.filter((r) => segmentHitsBox(pElem, pAnn, r)).length;
231
+ const cost = Math.hypot(candidateX - naturalX, y - naturalY) +
232
+ (crosses ? BLOCKED_LINE_COST : 0) +
233
+ cuts * CROSSED_ROUTE_COST;
205
234
  if (cost < best.cost)
206
235
  best = { x: candidateX, y, cost };
207
236
  }
@@ -12,6 +12,14 @@ export interface PoolLink {
12
12
  from: number;
13
13
  to: number;
14
14
  weight: number;
15
+ /**
16
+ * Where the message leaves inside its pool, 0 at the top to 1 at the bottom.
17
+ * A message whose element sits on the far side of its pool has to cross the
18
+ * whole process to get out, so the order that lets it leave from the near
19
+ * side wins. Absent for pools with no content to cross.
20
+ */
21
+ fromDepth?: number;
22
+ toDepth?: number;
15
23
  }
16
24
  /**
17
25
  * Order pools by their message-flow relationships. Returns indices into the
@@ -9,25 +9,63 @@
9
9
  */
10
10
  /** Above this many pools, permuting every order costs more than it is worth. */
11
11
  const EXHAUSTIVE_LIMIT = 8;
12
+ /** A full pool of process to cross costs this fraction of one pool step. */
13
+ const DEPTH_WEIGHT = 0.5;
14
+ function pairCosts(links) {
15
+ const pairs = new Map();
16
+ for (const link of links) {
17
+ if (link.from === link.to)
18
+ continue;
19
+ const a = Math.min(link.from, link.to);
20
+ const b = Math.max(link.from, link.to);
21
+ const key = `${a}:${b}`;
22
+ let pair = pairs.get(key);
23
+ if (!pair) {
24
+ pair = { a, b, weight: 0, aAbove: 0, aBelow: 0 };
25
+ pairs.set(key, pair);
26
+ }
27
+ pair.weight += link.weight;
28
+ // Leaving downward crosses what lies below the element, upward what lies above.
29
+ for (const [pool, depth] of [
30
+ [link.from, link.fromDepth],
31
+ [link.to, link.toDepth],
32
+ ]) {
33
+ if (depth === undefined)
34
+ continue;
35
+ const down = link.weight * DEPTH_WEIGHT * (1 - depth);
36
+ const up = link.weight * DEPTH_WEIGHT * depth;
37
+ if (pool === a) {
38
+ pair.aAbove += down;
39
+ pair.aBelow += up;
40
+ }
41
+ else {
42
+ pair.aAbove += up;
43
+ pair.aBelow += down;
44
+ }
45
+ }
46
+ }
47
+ return [...pairs.values()];
48
+ }
12
49
  /**
13
- * How far the messages travel in this order, weighted by how many there are.
14
- * The declaration-order term is a tie-break: with nothing to gain from moving,
15
- * the pools stay where the author put them.
50
+ * How far the messages travel in this order, weighted by how many there are,
51
+ * plus how much of their own pools they cross to get out. The declaration-order
52
+ * term is a tie-break: with nothing to gain from moving, the pools stay where
53
+ * the author put them.
16
54
  */
17
- function cost(order, links) {
18
- const position = new Map();
55
+ function cost(order, pairs) {
56
+ const position = [];
19
57
  for (let i = 0; i < order.length; i++) {
20
58
  const id = order[i];
21
59
  if (id !== undefined)
22
- position.set(id, i);
60
+ position[id] = i;
23
61
  }
24
62
  let total = 0;
25
- for (const link of links) {
26
- const from = position.get(link.from);
27
- const to = position.get(link.to);
28
- if (from === undefined || to === undefined)
63
+ for (const pair of pairs) {
64
+ const a = position[pair.a];
65
+ const b = position[pair.b];
66
+ if (a === undefined || b === undefined)
29
67
  continue;
30
- total += link.weight * Math.abs(from - to);
68
+ total += pair.weight * Math.abs(a - b) + (a < b ? pair.aAbove : pair.aBelow);
31
69
  }
32
70
  let drift = 0;
33
71
  for (let i = 0; i < order.length; i++)
@@ -42,15 +80,16 @@ export function orderPools(count, links) {
42
80
  const identity = Array.from({ length: count }, (_, i) => i);
43
81
  if (count < 3 || links.length === 0)
44
82
  return identity;
45
- return count <= EXHAUSTIVE_LIMIT ? exhaustive(identity, links) : refine(identity, links);
83
+ const pairs = pairCosts(links);
84
+ return count <= EXHAUSTIVE_LIMIT ? exhaustive(identity, pairs) : refine(identity, pairs);
46
85
  }
47
86
  /** Every order, best first-found wins — so declaration order survives a tie. */
48
- function exhaustive(identity, links) {
87
+ function exhaustive(identity, pairs) {
49
88
  let best = identity;
50
- let bestCost = cost(identity, links);
89
+ let bestCost = cost(identity, pairs);
51
90
  const permute = (prefix, rest) => {
52
91
  if (rest.length === 0) {
53
- const candidate = cost(prefix, links);
92
+ const candidate = cost(prefix, pairs);
54
93
  if (candidate < bestCost) {
55
94
  best = [...prefix];
56
95
  bestCost = candidate;
@@ -71,9 +110,9 @@ function exhaustive(identity, links) {
71
110
  * Take each pool out and put it back wherever it fits best, repeating until a
72
111
  * full sweep changes nothing.
73
112
  */
74
- function refine(identity, links) {
113
+ function refine(identity, pairs) {
75
114
  let order = [...identity];
76
- let current = cost(order, links);
115
+ let current = cost(order, pairs);
77
116
  for (let sweep = 0; sweep < order.length; sweep++) {
78
117
  let improved = false;
79
118
  for (let from = 0; from < order.length; from++) {
@@ -85,7 +124,7 @@ function refine(identity, links) {
85
124
  if (to === from)
86
125
  continue;
87
126
  const candidate = [...without.slice(0, to), pool, ...without.slice(to)];
88
- const candidateCost = cost(candidate, links);
127
+ const candidateCost = cost(candidate, pairs);
89
128
  if (candidateCost < current) {
90
129
  order = candidate;
91
130
  current = candidateCost;
@@ -0,0 +1,32 @@
1
+ import type { Bounds, Waypoint } from "./types.js";
2
+ /** Which side of a shape a route leaves or enters by. */
3
+ export type Side = "top" | "right" | "bottom" | "left";
4
+ /** A point on a shape's border a route may use, with an optional preference cost. */
5
+ export interface Port {
6
+ point: Waypoint;
7
+ side: Side;
8
+ /** Added to every route through this port, so preferred sides win ties. */
9
+ cost?: number;
10
+ }
11
+ export interface OrthogonalRequest {
12
+ from: Port[];
13
+ to: Port[];
14
+ /** Shapes the route must not pass through; the endpoints themselves excluded. */
15
+ obstacles: Bounds[];
16
+ /** How many already-routed segments the segment a→b would cut across. */
17
+ crossings?: (a: Waypoint, b: Waypoint) => number;
18
+ /** Clearance kept from obstacles where the route has room for it. */
19
+ margin?: number;
20
+ }
21
+ /**
22
+ * Obstacle-aware orthogonal routing: a shortest path, with bends and crossings
23
+ * charged as extra length, over the sparse grid of lines that run just clear of
24
+ * every obstacle edge (an orthogonal visibility graph). Used where the cheap
25
+ * candidate routes all hit something, so its cost only lands on the hard cases.
26
+ *
27
+ * Returns `null` when no clear route exists at all.
28
+ */
29
+ export declare function routeOrthogonal(request: OrthogonalRequest): Waypoint[] | null;
30
+ /** The four side-centre ports of a rectangle, each charged `cost[side]` when given. */
31
+ export declare function sidePorts(b: Bounds, cost?: Partial<Record<Side, number>>): Port[];
32
+ //# sourceMappingURL=orthogonal.d.ts.map