trebol-dev 0.1.1 → 0.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.
@@ -0,0 +1,340 @@
1
+ Trebol [
2
+ title: "The edits of a machine: each gives the file's new text, changing only the values it must"
3
+ exports: [
4
+ add-state rename-state delete-state set-initial
5
+ add-way delete-way set-way-events set-way-target set-way-code
6
+ set-state-word state-word-value way-code range-span
7
+ apply-changes cut-change value-change into-node target-name
8
+ ]
9
+ ]
10
+
11
+ import %model.trb
12
+ import %machines.trb
13
+ import %machine-places.trb
14
+
15
+ ; As with faces, an edit takes a design and a machine read from it and gives new text, or none when it
16
+ ; cannot be made (a name taken, a way between the states of a parallel state). Comments, spacing and
17
+ ; the order things are written in stay as they were; the designer reads the new text again.
18
+
19
+ range-span: function ["Where the values from one index to another of a loaded block sit, as one span" d [object!] b [block!] from [integer!] to [integer!]] [
20
+ s: span-of d b from
21
+ e: span-of d b to
22
+ as-pair s/x e/x + e/y - s/x
23
+ ]
24
+
25
+ ; Changes are [index length new] in the text, new none to cut; made from the last one back, so the
26
+ ; indexes of those before stay right.
27
+ apply-changes: function [text [string!] changes [block!]] [
28
+ order: copy []
29
+ foreach [x len new] changes [append/only order reduce [x len new]]
30
+ sort/compare order func [a b] [a/1 > b/1]
31
+ foreach c order [
32
+ text: either c/3 [splice text c/1 c/2 c/3] [cut text as-pair c/1 c/2]
33
+ ]
34
+ text
35
+ ]
36
+
37
+ value-change: function [d [object!] b [block!] k [integer!] new [string!]] [
38
+ s: span-of d b k
39
+ reduce [s/x s/y new]
40
+ ]
41
+
42
+ cut-change: function [d [object!] b [block!] from [integer!] to [integer!]] [
43
+ s: range-span d b from to
44
+ reduce [s/x s/y none]
45
+ ]
46
+
47
+ ; New code on its own line after the value at an index of a block, indented as that value's line is.
48
+ after-value: function [d [object!] b [block!] k [integer!] code [string!]] [
49
+ s: span-of d b k
50
+ indent: indent-at d/text s/x
51
+ if more-on-line? d/text s/x + s/y [
52
+ ; a block written on one line, [initial a state a []]: beside the value
53
+ unless find code newline [return splice d/text s/x + s/y 0 rejoin [" " code]]
54
+ ]
55
+ n: after-line d/text s/x + s/y
56
+ splice d/text n 0 rejoin [newline indent replace/all copy code newline rejoin [newline indent]]
57
+ ]
58
+
59
+ ; New code as the first value of a block, before what it holds.
60
+ at-start: function [d [object!] b [block!] code [string!]] [
61
+ if empty? b [return insert-into d b code]
62
+ s: span-of d b 1
63
+ owner: block-span d b
64
+ either all [
65
+ (line-start d/text s/x) > owner/x
66
+ blank? copy/part at d/text line-start d/text s/x s/x - line-start d/text s/x
67
+ ] [
68
+ indent: indent-at d/text s/x
69
+ splice d/text line-start d/text s/x 0 rejoin [indent replace/all copy code newline rejoin [newline indent] newline]
70
+ ] [
71
+ splice d/text s/x 0 rejoin [code " "]
72
+ ]
73
+ ]
74
+
75
+ ; A block for a state written without one, state ready: state ready [code]
76
+ give-block: function [d [object!] n [object!] code [string!]] [
77
+ s: span-of d n/holder n/to
78
+ indent: indent-at d/text s/x
79
+ inner: rejoin [indent " "]
80
+ splice d/text s/x + s/y 0 rejoin [
81
+ " [" newline inner replace/all copy code newline rejoin [newline inner] newline indent "]"
82
+ ]
83
+ ]
84
+
85
+ ; The new code goes in a node's block: after the value at an index of it, or first when there is none.
86
+ into-node: function [d [object!] n [object!] k code [string!]] [
87
+ case [
88
+ none? n/block [give-block d n code]
89
+ k [after-value d n/block k code]
90
+ true [at-start d n/block code]
91
+ ]
92
+ ]
93
+
94
+ add-state: function [
95
+ "The text with a new state inside a node (the machine or a state); kind is state, final or parallel"
96
+ d [object!] m [object!] parent [integer!] name [word!] kind [word!]
97
+ ] [
98
+ p: machine-node m parent
99
+ if any [p/kind = 'final find state-names m name] [return none]
100
+ code: either kind = 'final [rejoin ["final " name]] [rejoin [kind " " name " []"]]
101
+ ; the first state of a level that is no parallel one is its initial
102
+ if all [empty? p/children p/kind <> 'parallel] [code: rejoin ["initial " name newline code]]
103
+ last-child: either empty? p/children [none] [select machine-node m last p/children 'to]
104
+ into-node d p any [last-child if p/initial-at [p/initial-at + 1]] code
105
+ ]
106
+
107
+ ; The ids each value of a path means, as the engine looks it up, or none when it means no state.
108
+ path-ids: function [m [object!] scope [integer!] p [path!]] [
109
+ names: to block! p
110
+ while [scope] [
111
+ id: scope
112
+ ids: copy []
113
+ foreach w names [
114
+ unless id: state-child m id w [break]
115
+ append ids id
116
+ ]
117
+ if id [return ids]
118
+ scope: select machine-node m scope 'parent
119
+ ]
120
+ none
121
+ ]
122
+
123
+ ; Where a way names a state: for each [block index] of its targets that means the state, the change
124
+ target-changes: function [d [object!] m [object!] w [object!] id [integer!] new [word!]] [
125
+ out: copy []
126
+ foreach [b k] w/targets [
127
+ v: pick b k
128
+ case [
129
+ word? :v [
130
+ if id = find-machine-state m w/level v [append out value-change d b k form new]
131
+ ]
132
+ path? :v [
133
+ if all [ids: path-ids m w/level v find ids id] [
134
+ p: copy v
135
+ poke p index? find ids id new
136
+ append out value-change d b k mold p
137
+ ]
138
+ ]
139
+ ]
140
+ ]
141
+ out
142
+ ]
143
+
144
+ rename-state: function [
145
+ "The text with a state called anew, and every way and initial that names it changed too"
146
+ d [object!] m [object!] id [integer!] new [word!]
147
+ ] [
148
+ n: machine-node m id
149
+ if any [id = 1 find state-names m new find machine-keywords new] [return none]
150
+ p: machine-node m n/parent
151
+ changes: value-change d n/holder n/from + 1 form new
152
+ if all [p/initial = n/name p/initial-at] [append changes value-change d p/block p/initial-at + 1 form new]
153
+ foreach w m/ways [
154
+ if all [w/level = n/parent w/source = n/name] [append changes value-change d w/block w/from + 1 form new]
155
+ append changes target-changes d m w id new
156
+ ]
157
+ path: machine-path m id
158
+ rename-places apply-changes d/text changes machine-key d m to path! path to path! append copy/part path (length? path) - 1 new
159
+ ]
160
+
161
+ way-cuts: function ["The cuts that take out of a way what leads to or leaves a state, or the whole way" d [object!] m [object!] w [object!] id [integer!]] [
162
+ src: way-source m w
163
+ if all [src inside-state? m src id] [return cut-change d w/block w/from w/to]
164
+ if w/kind = 'to [
165
+ t: first way-targets m w
166
+ return either all [t inside-state? m t id] [cut-change d w/block w/from w/to] [copy []]
167
+ ]
168
+ out: copy []
169
+ kept: 0
170
+ foreach [from to k] w/choices [
171
+ t: find-machine-state m w/level pick w/branches k
172
+ either all [t inside-state? m t id] [append out cut-change d w/branches from to] [kept: kept + 1]
173
+ ]
174
+ if all [kept = 0 not empty? out] [return cut-change d w/block w/from w/to]
175
+ out
176
+ ]
177
+
178
+ delete-state: function [
179
+ "The text without a state, and without the ways that leave it or lead into it"
180
+ d [object!] m [object!] id [integer!]
181
+ ] [
182
+ if id = 1 [return none]
183
+ n: machine-node m id
184
+ p: machine-node m n/parent
185
+ changes: cut-change d n/holder n/from n/to
186
+ foreach w m/ways [
187
+ ; the ways written inside it go with it
188
+ unless inside-state? m w/level id [append changes way-cuts d m w id]
189
+ ]
190
+ if all [p/initial = n/name p/initial-at] [
191
+ others: exclude copy p/children reduce [id]
192
+ append changes either empty? others [
193
+ cut-change d p/block p/initial-at p/initial-at + 1
194
+ ] [
195
+ value-change d p/block p/initial-at + 1 form select machine-node m first others 'name
196
+ ]
197
+ ]
198
+ drop-places apply-changes d/text changes machine-key d m to path! machine-path m id
199
+ ]
200
+
201
+ set-initial: function [
202
+ "The text with a state as the one its level starts in"
203
+ d [object!] m [object!] id [integer!]
204
+ ] [
205
+ if id = 1 [return none]
206
+ n: machine-node m id
207
+ p: machine-node m n/parent
208
+ if p/kind = 'parallel [return none]
209
+ if p/initial-at [return apply-changes d/text value-change d p/block p/initial-at + 1 form n/name]
210
+ at-start d p/block rejoin ["initial " n/name]
211
+ ]
212
+
213
+ ; How a way written in a level names a state: the shortest name or path that the engine finds there.
214
+ target-name: function [m [object!] level [integer!] id [integer!]] [
215
+ names: machine-path m id
216
+ k: length? names
217
+ while [k >= 1] [
218
+ p: copy skip names k - 1
219
+ t: either 1 = length? p [first p] [to path! p]
220
+ if id = find-machine-state m level t [return t]
221
+ k: k - 1
222
+ ]
223
+ none
224
+ ]
225
+
226
+ add-way: function [
227
+ "The text with a new way from one state to another on an event (none for a way that waits for no event)"
228
+ d [object!] m [object!] from [integer!] to [integer!] event
229
+ ] [
230
+ if from = 1 [return none]
231
+ s: machine-node m from
232
+ level: machine-node m s/parent
233
+ if level/kind = 'parallel [return none]
234
+ unless t: target-name m level/id to [return none]
235
+ code: rejoin ["from " s/name " to " mold t]
236
+ if event [append code rejoin [" on " event]]
237
+ ; after the last way of its level, else after its last state
238
+ last-way: none
239
+ foreach w m/ways [if w/level = level/id [last-way: w]]
240
+ k: either last-way [last-way/to] [select machine-node m last level/children 'to]
241
+ into-node d level k code
242
+ ]
243
+
244
+ delete-way: function ["The text without a way" d [object!] m [object!] w [object!]] [
245
+ apply-changes d/text cut-change d w/block w/from w/to
246
+ ]
247
+
248
+ set-way-events: function [
249
+ "The text with the events a way is taken on: a block of words, empty for none"
250
+ d [object!] m [object!] w [object!] events [block!]
251
+ ] [
252
+ new: either 1 = length? events [form first events] [mold events]
253
+ case [
254
+ all [empty? events w/events-at] [
255
+ ; where needs an event: it goes with them
256
+ stop: either w/where-at [w/where-at + 1] [w/events-at + 1]
257
+ apply-changes d/text cut-change d w/block w/events-at stop
258
+ ]
259
+ empty? events [d/text]
260
+ w/events-at [apply-changes d/text value-change d w/block w/events-at + 1 new]
261
+ true [
262
+ k: either w/kind = 'select [w/select-at] [w/from + 2]
263
+ if w/kind = 'to [k: w/target-at]
264
+ s: span-of d w/block k
265
+ splice d/text s/x + s/y 0 rejoin [" on " new]
266
+ ]
267
+ ]
268
+ ]
269
+
270
+ set-way-target: function [
271
+ "The text with a way of the kind from a to b going to another state"
272
+ d [object!] m [object!] w [object!] to [integer!]
273
+ ] [
274
+ unless w/kind = 'to [return none]
275
+ unless t: target-name m w/level to [return none]
276
+ apply-changes d/text value-change d w/block w/target-at mold t
277
+ ]
278
+
279
+ code-block: function [code [string!]] [rejoin ["[" trim copy code "]"]]
280
+
281
+ way-code: function ["The code of a way's where or set, as written inside its block, or none" d [object!] w [object!] word [word!]] [
282
+ k: select reduce ['where w/where-at 'set w/set-at] word
283
+ unless k [return none]
284
+ s: span-of d w/block k + 1
285
+ copy/part at d/text s/x + 1 s/y - 2
286
+ ]
287
+
288
+ set-way-code: function [
289
+ "The text with a way's where or set code: none or empty takes it out"
290
+ d [object!] m [object!] w [object!] word [word!] code
291
+ ] [
292
+ k: select reduce ['where w/where-at 'set w/set-at] word
293
+ empty: any [none? code empty? trim copy code]
294
+ case [
295
+ all [k empty] [apply-changes d/text cut-change d w/block k k + 1]
296
+ empty [d/text]
297
+ k [apply-changes d/text value-change d w/block k + 1 code-block code]
298
+ word = 'where [
299
+ unless w/events-at [return none]
300
+ s: span-of d w/block w/events-at + 1
301
+ splice d/text s/x + s/y 0 rejoin [" where " code-block code]
302
+ ]
303
+ true [
304
+ s: span-of d w/block w/to
305
+ splice d/text s/x + s/y 0 rejoin [" set " code-block code]
306
+ ]
307
+ ]
308
+ ]
309
+
310
+ state-word-value: function [
311
+ "What follows one of a node's own words as written (on-enter's code inside its block), or none"
312
+ d [object!] m [object!] id [integer!] word [word!]
313
+ ] [
314
+ n: machine-node m id
315
+ unless k: select n/words word [return none]
316
+ s: span-of d n/block k + 1
317
+ v: pick n/block k + 1
318
+ either block? :v [copy/part at d/text s/x + 1 s/y - 2] [copy/part at d/text s/x s/y]
319
+ ]
320
+
321
+ set-state-word: function [
322
+ "The text with one of a node's own words (on-enter, run, timeout, no ...) set to a value as written, or taken out for none or empty"
323
+ d [object!] m [object!] id [integer!] word [word!] value
324
+ ] [
325
+ n: machine-node m id
326
+ k: select n/words word
327
+ if all [value find [on-enter on-exit on-activity on-save run data] word] [value: code-block value]
328
+ empty: any [none? value empty? trim copy value value = "[]"]
329
+ case [
330
+ all [k empty] [apply-changes d/text cut-change d n/block k k + 1]
331
+ empty [d/text]
332
+ k [apply-changes d/text value-change d n/block k + 1 value]
333
+ true [
334
+ ; its own words go first in a state's block, after those it has already
335
+ last-word: none
336
+ foreach [w at] n/words [last-word: max any [last-word 0] at + 1]
337
+ into-node d n any [last-word if n/initial-at [n/initial-at + 1]] rejoin [word " " value]
338
+ ]
339
+ ]
340
+ ]
@@ -0,0 +1,76 @@
1
+ var Et=Object.create;var ot=Object.defineProperty;var Lt=Object.getOwnPropertyDescriptor;var $t=Object.getOwnPropertyNames;var At=Object.getPrototypeOf,Ot=Object.prototype.hasOwnProperty;var R=(r=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(r,{get:(t,i)=>(typeof require<"u"?require:t)[i]}):r)(function(r){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+r+'" is not supported')});var St=(r,t)=>()=>{try{return t||r((t={exports:{}}).exports,t),t.exports}catch(i){throw t=0,i}};var Ct=(r,t,i,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of $t(t))!Ot.call(r,n)&&n!==i&&ot(r,n,{get:()=>t[n],enumerable:!(e=Lt(t,n))||e.enumerable});return r};var jt=(r,t,i)=>(i=r!=null?Et(At(r)):{},Ct(t||!r||!r.__esModule?ot(i,"default",{value:r,enumerable:!0}):i,r));var at=St((rt,Z)=>{(function(r){if(typeof rt=="object"&&typeof Z<"u")Z.exports=r();else if(typeof define=="function"&&define.amd)define([],r);else{var t;typeof window<"u"?t=window:typeof global<"u"?t=global:typeof self<"u"?t=self:t=this,t.ELK=r()}})(function(){var r,t,i;return(function(){function e(n,s,o){function a(p,m){if(!s[p]){if(!n[p]){var x=typeof R=="function"&&R;if(!m&&x)return x(p,!0);if(l)return l(p,!0);var d=new Error("Cannot find module '"+p+"'");throw d.code="MODULE_NOT_FOUND",d}var c=s[p]={exports:{}};n[p][0].call(c.exports,function(f){var u=n[p][1][f];return a(u||f)},c,c.exports,e,n,s,o)}return s[p].exports}for(var l=typeof R=="function"&&R,h=0;h<o.length;h++)a(o[h]);return a}return e})()({1:[function(e,n,s){"use strict";Object.defineProperty(s,"__esModule",{value:!0}),s.default=void 0;function o(c){"@babel/helpers - typeof";return o=typeof Symbol=="function"&&typeof Symbol.iterator=="symbol"?function(f){return typeof f}:function(f){return f&&typeof Symbol=="function"&&f.constructor===Symbol&&f!==Symbol.prototype?"symbol":typeof f},o(c)}function a(c,f){if(!(c instanceof f))throw new TypeError("Cannot call a class as a function")}function l(c,f){for(var u=0;u<f.length;u++){var y=f[u];y.enumerable=y.enumerable||!1,y.configurable=!0,"value"in y&&(y.writable=!0),Object.defineProperty(c,p(y.key),y)}}function h(c,f,u){return f&&l(c.prototype,f),u&&l(c,u),Object.defineProperty(c,"prototype",{writable:!1}),c}function p(c){var f=m(c,"string");return o(f)=="symbol"?f:f+""}function m(c,f){if(o(c)!="object"||!c)return c;var u=c[Symbol.toPrimitive];if(u!==void 0){var y=u.call(c,f||"default");if(o(y)!="object")return y;throw new TypeError("@@toPrimitive must return a primitive value.")}return(f==="string"?String:Number)(c)}var x=s.default=(function(){function c(){var f=this,u=arguments.length>0&&arguments[0]!==void 0?arguments[0]:{},y=u.defaultLayoutOptions,g=y===void 0?{}:y,v=u.algorithms,k=v===void 0?["layered","stress","mrtree","radial","force","disco","sporeOverlap","sporeCompaction","rectpacking"]:v,M=u.workerFactory,$=u.workerUrl;if(a(this,c),this.defaultLayoutOptions=g,this.initialized=!1,typeof $>"u"&&typeof M>"u")throw new Error("Cannot construct an ELK without both 'workerUrl' and 'workerFactory'.");var S=M;typeof $<"u"&&typeof M>"u"&&(S=function(L){return new Worker(L)});var j=S($);if(typeof j.postMessage!="function")throw new TypeError("Created worker does not provide the required 'postMessage' function.");this.worker=new d(j),this.worker.postMessage({cmd:"register",algorithms:k}).then(function(D){return f.initialized=!0}).catch(console.err)}return h(c,[{key:"layout",value:function(u){var y=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{},g=y.layoutOptions,v=g===void 0?this.defaultLayoutOptions:g,k=y.logging,M=k===void 0?!1:k,$=y.measureExecutionTime,S=$===void 0?!1:$;return u?this.worker.postMessage({cmd:"layout",graph:u,layoutOptions:v,options:{logging:M,measureExecutionTime:S}}):Promise.reject(new Error("Missing mandatory parameter 'graph'."))}},{key:"knownLayoutAlgorithms",value:function(){return this.worker.postMessage({cmd:"algorithms"})}},{key:"knownLayoutOptions",value:function(){return this.worker.postMessage({cmd:"options"})}},{key:"knownLayoutCategories",value:function(){return this.worker.postMessage({cmd:"categories"})}},{key:"terminateWorker",value:function(){this.worker&&this.worker.terminate()}}])})(),d=(function(){function c(f){var u=this;if(a(this,c),f===void 0)throw new Error("Missing mandatory parameter 'worker'.");this.resolvers={},this.worker=f,this.worker.onmessage=function(y){setTimeout(function(){u.receive(u,y)},0)}}return h(c,[{key:"postMessage",value:function(u){var y=this.id||0;this.id=y+1,u.id=y;var g=this;return new Promise(function(v,k){g.resolvers[y]=function(M,$){M?(g.convertGwtStyleError(M),k(M)):v($)},g.worker.postMessage(u)})}},{key:"receive",value:function(u,y){var g=y.data,v=u.resolvers[g.id];v&&(delete u.resolvers[g.id],g.error?v(g.error):v(null,g.data))}},{key:"terminate",value:function(){this.worker&&this.worker.terminate()}},{key:"convertGwtStyleError",value:function(u){if(u){var y=u.__java$exception;y&&(y.cause&&y.cause.backingJsObject&&(u.cause=y.cause.backingJsObject,this.convertGwtStyleError(u.cause)),delete u.__java$exception)}}}])})()},{}],2:[function(e,n,s){"use strict";var o=e("./elk-api.js").default;Object.defineProperty(n.exports,"__esModule",{value:!0}),n.exports=o,o.default=o},{"./elk-api.js":1}]},{},[2])(2)})});var ct=jt(at(),1),Tt="http://www.w3.org/2000/svg",T=26,X="13px Inter, system-ui, sans-serif",et="12px Inter, system-ui, sans-serif",lt=null;function ht(){return lt??=new ct.default({workerUrl:new URL("elk-worker.js",import.meta.url).href}),lt}var tt=null;function _(r,t=X){return tt??=document.createElement("canvas").getContext("2d"),tt.font=t,Math.ceil(tt.measureText(r).width)}var dt=" \u25C7 ",q=r=>r.uses?`${r.name}${dt}${r.uses}`:r.name;function E(r,t={},i=null){let e=document.createElementNS(Tt,r);for(let[n,s]of Object.entries(t))e.setAttribute(n,String(s));return i&&i.append(e),e}var ut=`
2
+ :host { position: relative; height: 100%; min-height: 320px; font: ${X}; color: var(--tb-text, #1c1c1c); }
3
+ /* the page's styles set the element's display: the column is a box of its own */
4
+ .frame { display: flex; flex-direction: column; height: 100%; min-height: 320px; }
5
+ .bar { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; padding: 4px; margin-bottom: 6px;
6
+ border: 1px solid var(--tb-border, #e7e5e4); border-radius: var(--tb-radius, 8px); background: var(--tb-surface, #fff); }
7
+ .bar button { display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 10px; border: 1px solid transparent;
8
+ border-radius: calc(var(--tb-radius, 8px) - 2px); background: none; color: inherit; font: inherit; font-size: 13px; cursor: pointer; }
9
+ .bar button:hover:not(:disabled) { background: color-mix(in oklch, var(--tb-text, #1c1c1c) 7%, transparent); }
10
+ .bar button:focus-visible { outline: 2px solid var(--tb-focus, var(--tb-primary, #2563eb)); outline-offset: 1px; }
11
+ .bar button:disabled { opacity: .4; cursor: default; }
12
+ .bar button.tool { cursor: grab; border-color: var(--tb-border, #e7e5e4); }
13
+ .bar button.go { margin-left: auto; border-color: var(--tb-border, #e7e5e4); font-weight: 600; }
14
+ .bar .sep { width: 1px; height: 20px; background: var(--tb-border, #e7e5e4); margin: 0 4px; }
15
+ .bar svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
16
+ .stage { position: relative; flex: 1; overflow: auto; min-height: 160px; outline: none; }
17
+ .stage > svg { display: block; user-select: none; touch-action: none; overflow: visible; }
18
+ .node.lifted { opacity: .85; }
19
+ .node.lifted > .box { stroke-dasharray: 4 3; }
20
+ .hint { position: absolute; left: 0; right: 0; bottom: 12px; margin: 0 auto; max-width: 420px; text-align: center; color: var(--tb-muted, #6b7280); font-size: 13px; pointer-events: none; }
21
+ .empty { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 40px 16px; color: var(--tb-muted, #6b7280); text-align: center; }
22
+ .empty button { height: 34px; padding: 0 14px; border: 1px solid var(--tb-border-strong, #a8a29e); border-radius: var(--tb-radius, 8px);
23
+ background: var(--tb-surface, #fff); color: var(--tb-text, #1c1c1c); font: inherit; cursor: pointer; }
24
+ .box { fill: var(--tb-surface, #fff); stroke: var(--tb-border-strong, #a8a29e); stroke-width: 1.25; }
25
+ .group > .box { fill: color-mix(in oklch, var(--tb-text, #1c1c1c) 3%, var(--tb-surface, #fff)); }
26
+ .parallel > .box { stroke-dasharray: 5 4; fill: none; }
27
+ .machine > .box { fill: none; stroke: none; }
28
+ .inner { fill: none; stroke: var(--tb-border-strong, #a8a29e); stroke-width: 1.25; }
29
+ .name { fill: var(--tb-text, #1c1c1c); dominant-baseline: central; }
30
+ .group > .name, .parallel > .name { font-weight: 600; font-size: 12px; fill: var(--tb-muted, #6b7280); }
31
+ .node { cursor: pointer; outline: none; }
32
+ .node:focus-visible > .box { stroke: var(--tb-focus, var(--tb-primary, #2563eb)); stroke-width: 2.5; }
33
+ .node.active > .box { fill: color-mix(in oklch, var(--tb-success, #16a34a) 18%, var(--tb-surface, #fff)); stroke: var(--tb-success, #16a34a); }
34
+ :is(.group, .parallel).active > .box { fill: color-mix(in oklch, var(--tb-success, #16a34a) 6%, var(--tb-surface, #fff)); }
35
+ .node.selected > .box, .node.selected > .inner { stroke: var(--tb-primary, #2563eb); stroke-width: 2.25; }
36
+ .node.selected > .box { fill: var(--tb-primary-soft, #eef3ff); }
37
+ .node.target > .box { stroke: var(--tb-primary, #2563eb); stroke-width: 2.5; stroke-dasharray: none;
38
+ fill: color-mix(in oklch, var(--tb-primary, #2563eb) 10%, var(--tb-surface, #fff)); }
39
+ .machine.target > .box { stroke: var(--tb-primary, #2563eb); stroke-dasharray: 6 4; }
40
+ .uses > .box { stroke: var(--tb-primary, #2563eb); stroke-opacity: .55; }
41
+ .mark { fill: var(--tb-primary, #2563eb); font-weight: 600; }
42
+ /* what a state shows of the fragment it uses: seen, not changed here */
43
+ .fixed > .box { fill: color-mix(in oklch, var(--tb-text, #1c1c1c) 4%, var(--tb-surface, #fff)); stroke-dasharray: 3 3; stroke-opacity: .7; }
44
+ .fixed > .name { fill: var(--tb-muted, #6b7280); }
45
+ .fixed.group > .box { fill: none; }
46
+ .way.fixed path.line { stroke-dasharray: 4 3; stroke-opacity: .7; }
47
+ .way.fixed { cursor: default; }
48
+ .dot { fill: var(--tb-text, #1c1c1c); }
49
+ .way { cursor: pointer; }
50
+ .way path.line { fill: none; stroke: var(--tb-muted, #78716c); stroke-width: 1.5; }
51
+ .way path.hit { fill: none; stroke: transparent; stroke-width: 12; }
52
+ .way .label { fill: var(--tb-muted, #57534e); font-size: 12px; dominant-baseline: central; }
53
+ .way .label-bg { fill: var(--tb-bg, #fff); opacity: .85; }
54
+ .way.selected path.line { stroke: var(--tb-primary, #2563eb); stroke-width: 2.25; }
55
+ .way.selected .label { fill: var(--tb-primary, #2563eb); font-weight: 600; }
56
+ .way:hover path.line { stroke: var(--tb-text, #1c1c1c); }
57
+ .head { fill: var(--tb-muted, #78716c); }
58
+ .way.selected .head { fill: var(--tb-primary, #2563eb); }
59
+ .start path.line { stroke: var(--tb-text, #1c1c1c); }
60
+ .start .head { fill: var(--tb-text, #1c1c1c); }
61
+ .handle { fill: var(--tb-surface, #fff); stroke: var(--tb-primary, #2563eb); stroke-width: 2; cursor: crosshair; }
62
+ .handle:hover { fill: var(--tb-primary, #2563eb); }
63
+ .rubber { stroke: var(--tb-primary, #2563eb); stroke-width: 2; stroke-dasharray: 6 4; fill: none; pointer-events: none; }
64
+ .ghost { position: fixed; z-index: 10; pointer-events: none; padding: 6px 14px; border: 1.5px dashed var(--tb-primary, #2563eb);
65
+ border-radius: 9px; background: var(--tb-surface, #fff); color: var(--tb-text, #1c1c1c); font: ${X}; opacity: .9; }
66
+ .naming > .name, .naming > .label, .naming:not(.group, .parallel) > :is(.box, .inner) { visibility: hidden; }
67
+ .inline { position: absolute; z-index: 5; box-sizing: border-box; height: 28px; padding: 0 6px;
68
+ border: 2px solid var(--tb-primary, #2563eb); border-radius: 6px; background: var(--tb-surface, #fff);
69
+ color: var(--tb-text, #1c1c1c); font: ${X}; text-align: center; outline: none; }
70
+ .menu { position: fixed; z-index: 20; min-width: 180px; padding: 4px; border: 1px solid var(--tb-border, #e7e5e4);
71
+ border-radius: var(--tb-radius, 8px); background: var(--tb-surface, #fff); box-shadow: 0 8px 24px rgb(0 0 0 / .14); }
72
+ .menu button { display: block; width: 100%; padding: 7px 10px; border: none; border-radius: 5px; background: none;
73
+ color: var(--tb-text, #1c1c1c); font: inherit; text-align: left; cursor: pointer; }
74
+ .menu button:hover, .menu button:focus-visible { background: color-mix(in oklch, var(--tb-primary, #2563eb) 12%, transparent); outline: none; }
75
+ .menu button.danger { color: var(--tb-danger, #dc2626); }
76
+ `;function ft(r,t="RIGHT"){let i=new Map;for(let l of r.nodes){let h=l.parent??"";i.has(h)||i.set(h,[]),i.get(h).push(l)}let e=[],n=l=>{let h=i.get(l.id)??[],p=l.kind==="machine"?"":q(l),m={id:l.id,layoutOptions:{}};if(h.length){m.children=h.map(n),m.layoutOptions["elk.padding"]=`[top=${p?T+8:12},left=14,bottom=14,right=14]`,p&&(m.layoutOptions["elk.nodeSize.minimum"]=`(${_(p)+28},40)`),m.layoutOptions["elk.nodeSize.constraints"]="MINIMUM_SIZE";let x=h.find(d=>d.initial);x&&l.kind!=="parallel"&&(m.children.unshift({id:`start:${l.id}`,width:12,height:12}),e.push({id:`start:${l.id}>`,sources:[`start:${l.id}`],targets:[x.id]}))}else m.width=Math.max(64,_(p)+32),m.height=36;return m},s=r.nodes.find(l=>l.parent==null),o=s?n(s):{id:"none",children:[]};s&&(i.get(s.id)??[]).length<3&&(o.layoutOptions["elk.nodeSize.minimum"]="(320,180)");let a=new Set(r.nodes.map(l=>l.id));for(let l of r.ways)a.has(l.from)&&l.to.forEach((h,p)=>{if(!a.has(h))return;let m={id:`${l.id}#${p}`,sources:[l.from],targets:[h]};l.label&&(m.labels=[{text:l.label,width:_(l.label,et)+8,height:18}]),e.push(m)});return{id:"graph",layoutOptions:{"elk.algorithm":"layered","elk.direction":t,"elk.hierarchyHandling":"INCLUDE_CHILDREN","elk.json.edgeCoords":"ROOT","elk.json.shapeCoords":"ROOT","elk.edgeRouting":"SPLINES","elk.layered.spacing.nodeNodeBetweenLayers":"48","elk.spacing.nodeNode":"28","elk.spacing.edgeLabel":"4","elk.edgeLabels.inline":"true","elk.padding":"[top=12,left=12,bottom=12,right=12]"},children:[o],edges:e}}function Dt(r,t,i){let e=Math.atan2(i.y-t.y,i.x-t.x),n=9,s=4.5,o=i.x-n*Math.cos(e),a=i.y-n*Math.sin(e),l=[[i.x,i.y],[o+s*Math.sin(e),a-s*Math.cos(e)],[o-s*Math.sin(e),a+s*Math.cos(e)]];E("polygon",{class:"head",points:l.map(h=>h.join(",")).join(" ")},r)}function pt(r,t,i,e,n=t.frame){let s=new Map(r.nodes.map(d=>[d.id,d])),o=Math.ceil(n.w),a=Math.ceil(n.h),l=E("svg",{viewBox:`${n.x} ${n.y} ${o} ${a}`,width:Math.ceil(o*e),height:Math.ceil(a*e),role:"group","aria-label":`Machine ${r.name??""}`.trim()}),h=E("g",{},l),p=[...t.boxes].sort((d,c)=>d.depth-c.depth);for(let d of p){let c=s.get(d.id);if(!c)continue;let f=d.kind,u=E("g",{class:`node ${f}${c.fixed?" fixed":""}${c.uses?" uses":""}`,"data-id":c.id},h);c.kind!=="machine"&&!c.fixed&&(u.setAttribute("tabindex","0"),u.setAttribute("role","button"),u.setAttribute("aria-label",c.uses?`${c.kind} ${c.name}, uses the fragment ${c.uses}`:`${c.kind} ${c.name}`));let y=f==="group"||f==="parallel"?10:9;if(E("rect",{class:"box",x:d.x,y:d.y,width:d.w,height:d.h,rx:y},u),c.kind==="final"&&E("rect",{class:"inner",x:d.x+3.5,y:d.y+3.5,width:d.w-7,height:d.h-7,rx:y-3},u),c.kind!=="machine"){let g=E("text",{class:"name"},u);f==="group"||f==="parallel"?(g.setAttribute("x",d.x+12),g.setAttribute("y",d.y+T/2+2)):(g.setAttribute("x",d.x+d.w/2),g.setAttribute("y",d.y+d.h/2),g.setAttribute("text-anchor","middle")),g.textContent=c.name,c.uses&&(E("tspan",{class:"mark"},g).textContent=`${dt}${c.uses}`)}}for(let d of t.dots)E("circle",{class:"dot",cx:d.x,cy:d.y,r:6},h);let m=E("g",{},l);for(let d of i){let c=E("g",{class:d.start?"start":d.fixed?"way fixed":"way","data-id":d.id},m);if(d.start||E("path",{class:"hit",d:d.d},c),E("path",{class:"line",d:d.d},c),Dt(c,d.tip[0],d.tip[1]),d.label){let f=d.label.w;E("rect",{class:"label-bg",x:d.label.x-f/2,y:d.label.y-9,width:f,height:18,rx:4},c),E("text",{class:"label",x:d.label.x,y:d.label.y,"text-anchor":"middle"},c).textContent=d.label.text}}E("g",{class:"overlay"},l);let x=t.boxes.map(d=>({id:d.id,kind:d.kind,x:d.x,y:d.y,width:d.w,height:d.h,depth:d.depth}));return{svg:l,rects:x}}function yt(r,t,i,e){let n=Object.assign(document.createElement("div"),{className:"menu"});n.setAttribute("role","menu");let s=null,o=()=>{n.remove(),s&&document.removeEventListener("pointerdown",s,!0),s=null};for(let[a,l,h]of i){let p=Object.assign(document.createElement("button"),{type:"button",textContent:a});p.setAttribute("role","menuitem"),h&&(p.className=h),p.addEventListener("click",()=>{o(),l()}),n.append(p)}return n.addEventListener("keydown",a=>{let l=[...n.querySelectorAll("button")],h=l.indexOf(r.activeElement);a.key==="Escape"?(a.preventDefault(),o(),e()):a.key==="ArrowDown"?(a.preventDefault(),l[(h+1)%l.length].focus()):a.key==="ArrowUp"&&(a.preventDefault(),l[(h-1+l.length)%l.length].focus())}),n.style.left=`${Math.min(t.clientX,innerWidth-200)}px`,n.style.top=`${Math.min(t.clientY,innerHeight-40*i.length-16)}px`,r.append(n),n.querySelector("button").focus(),s=a=>{a.composedPath().includes(n)||o()},setTimeout(()=>{s&&document.addEventListener("pointerdown",s,!0)}),o}var Nt={l:14,t:T+8,r:14,b:14},_t={l:24,t:24,r:24,b:24},xt=48,O=26,G=r=>({w:Math.max(64,_(r)+32),h:36});function It(r){let t=new Map;for(let i of r.nodes)i.parent!=null&&(t.has(i.parent)||t.set(i.parent,[]),t.get(i.parent).push(i));return t}var C=(r,t,i)=>{for(let e of r)e.x+=t,e.y+=i,"tx"in e&&(e.tx+=t,e.ty+=i)},gt=(r,t)=>r.x<t.x+t.w&&t.x<r.x+r.w&&r.y<t.y+t.h&&t.y<r.y+r.h;function Pt(r,t){let i=r.x+r.w/2,e=r.y+r.h/2,n=[{x:r.x-O+6,y:e,tx:r.x,ty:e,room:{x:r.x-O-4,y:e-10,w:O+4,h:20}},{x:i,y:r.y-O+6,tx:i,ty:r.y,room:{x:i-10,y:r.y-O-4,w:20,h:O+4}},{x:r.x+r.w+O-6,y:e,tx:r.x+r.w,ty:e,room:{x:r.x+r.w,y:e-10,w:O+4,h:20}},{x:i,y:r.y+r.h+O-6,tx:i,ty:r.y+r.h,room:{x:i-10,y:r.y+r.h,w:20,h:O+4}}],s=n.find(o=>!t.some(a=>gt(o.room,a)))??n[0];return{x:s.x,y:s.y,tx:s.tx,ty:s.ty}}var Rt=(r,t)=>(r.get(t.id)??[]).some(i=>i.fixed&&i.owner===t.id);function qt(r,t){let i=r.filter(e=>Rt(t,e.c)).map(e=>({s:e,was:{x:e.b.x,y:e.b.y,...G(q(e.c))}}));for(let e=0;i.length&&e<1e3;e++){let{s:n,was:s}=i.shift();for(let o of r){if(o===n||!gt(n.b,o.b))continue;let a=0,l=0;if(o.b.x>=s.x+s.w?a=n.b.x+n.b.w-(s.x+s.w):o.b.y>=s.y+s.h&&(l=n.b.y+n.b.h-(s.y+s.h)),a<=0&&l<=0)continue;let h={...o.b};C([o.b],a,l),C(o.m.items,a,l),C(o.m.dots,a,l);let p=o.m.items.find(m=>m.id===o.c.id);p.push={x:(p.push?.x??0)+a,y:(p.push?.y??0)+l},i.push({s:o,was:h})}}}function nt(r,t,i,e,n,s){let o=(t.get(i.id)??[]).filter(b=>b.id!==n),a=t.get(i.id)??[],l=i.parent==null;if(!a.length&&!l){let{w:b,h:w}=G(q(i));return{box:{x:0,y:0,w:b,h:w},items:[{id:i.id,kind:i.kind,x:0,y:0,w:b,h:w,depth:s}],dots:[]}}let h=l?_t:Nt,p=[],m=[],x=[],d=[];for(let b of o){let w=nt(r,t,b,e,n,s+1);(e.has(b.id)?x:d).push([b,w])}let c=[],f=x.map(([b,w])=>{let A=e.get(b.id);return C(w.items,A.x-w.box.x,A.y-w.box.y),C(w.dots,A.x-w.box.x,A.y-w.box.y),{c:b,m:w,b:{x:A.x,y:A.y,w:w.box.w,h:w.box.h}}});qt(f,t);for(let{c:b,m:w,b:A}of f)p.push(...w.items),m.push(...w.dots),c.push([b,A]);let u=c.length?Math.max(...c.map(([,b])=>b.x+b.w))+xt:h.l+(i.kind!=="parallel"?O:0),y=c.length?Math.min(...c.map(([,b])=>b.y)):h.t;for(let[b,w]of d)C(w.items,u-w.box.x,y-w.box.y),C(w.dots,u-w.box.x,y-w.box.y),p.push(...w.items),m.push(...w.dots),c.push([b,{x:u,y,w:w.box.w,h:w.box.h}]),u+=w.box.w+xt;let g=1/0,v=1/0,k=-1/0,M=-1/0;for(let[,b]of c)g=Math.min(g,b.x),v=Math.min(v,b.y),k=Math.max(k,b.x+b.w),M=Math.max(M,b.y+b.h);let $=c.find(([b])=>b.initial);if($&&i.kind!=="parallel"){let b=Pt($[1],c.filter(w=>w!==$).map(([,w])=>w));b.to=$[0].id,m.push(b),g=Math.min(g,b.x-6),v=Math.min(v,b.y-6),k=Math.max(k,b.x+6),M=Math.max(M,b.y+6)}let S=l?"":q(i),j=l?a.length<3?320:0:Math.max(64,_(S)+28),D=l?a.length<3?180:0:T+14,L=c.length?{x:Math.min(0,g-h.l),y:Math.min(0,v-h.t)}:{x:0,y:0};L.w=Math.max(j,(c.length?k+h.r:0)-L.x),L.h=Math.max(D,(c.length?M+h.b:0)-L.y);let U=l?"machine":i.kind==="parallel"?"parallel":"group";return p.push({id:i.id,kind:U,...L,depth:s}),{box:L,items:p,dots:m}}function W(r,t,i=null){let e=It(r),n=r.nodes.find(a=>a.parent==null),s=nt(r,e,n,t,i?.id??null,0);if(i){let a=r.nodes.find(h=>h.id===i.id),l=nt(r,e,a,t,null,100);C(l.items,i.x-l.box.x,i.y-l.box.y),C(l.dots,i.x-l.box.x,i.y-l.box.y),s.items.push(...l.items),s.dots.push(...l.dots)}s.items.sort((a,l)=>l.depth-a.depth);let o=s.items.find(a=>a.id===n.id);return{boxes:s.items,dots:s.dots,frame:{x:o.x,y:o.y,w:o.w,h:o.h}}}function z(r,t,i){let e=r.nodes.find(n=>n.id===i);return e?e.parent==null?{x:0,y:0}:t.boxes.find(n=>n.id===i)??null:null}function H(r,t,i=null,e=null){let n=new Map(t.boxes.map(o=>[o.id,o])),s=[];for(let o of r.nodes){if(o.parent==null||o.fixed)continue;let a=n.get(o.id),l=z(r,t,o.id===i?e:o.parent),h=a?.push?.x??0,p=a?.push?.y??0;a&&l&&s.push(o.id,Math.round(a.x-h-l.x),Math.round(a.y-p-l.y))}return s}function wt(r,t){return[...new Set(r.nodes.filter(e=>e.fixed&&!t.has(e.id)).map(e=>e.owner))].map(e=>({name:e,nodes:[{...r.nodes.find(n=>n.id===e),parent:null},...r.nodes.filter(n=>n.fixed&&n.owner===e)],ways:r.ways.filter(n=>n.fixed&&n.owner===e)}))}function vt(r){let t=new Map,i=(e,n,s,o)=>{!o&&!e.id.startsWith("start:")&&t.set(e.id,{x:Math.round(e.x-n),y:Math.round(e.y-s)});for(let a of e.children??[])i(a,e.x,e.y,!1)};for(let e of r.children??[])i(e,e.x,e.y,!0);return t}var F=r=>({x:r.x+r.w/2,y:r.y+r.h/2}),Y=(r,t)=>t.x>=r.x&&t.y>=r.y&&t.x+t.w<=r.x+r.w&&t.y+t.h<=r.y+r.h;function mt(r,t){let i=F(r),e=t.x-i.x,n=t.y-i.y;if(!e&&!n)return i;let s=Math.min(e?r.w/2/Math.abs(e):1/0,n?r.h/2/Math.abs(n):1/0);return{x:i.x+e*s,y:i.y+n*s}}function bt(r,t){let i=F(t),e=[[t.x-r.x,{x:r.x,y:i.y},{x:t.x,y:i.y}],[r.x+r.w-t.x-t.w,{x:r.x+r.w,y:i.y},{x:t.x+t.w,y:i.y}],[r.y+r.h-t.y-t.h,{x:i.x,y:r.y+r.h},{x:i.x,y:t.y+t.h}]];return e.sort((n,s)=>n[0]-s[0]),[e[0][1],e[0][2]]}function Wt(r,t,i,e,n){let s=1-n;return{x:s*s*s*r.x+3*s*s*n*t.x+3*s*n*n*i.x+n*n*n*e.x,y:s*s*s*r.y+3*s*s*n*t.y+3*s*n*n*i.y+n*n*n*e.y}}function kt(r,t){let i=new Map(t.boxes.map(a=>[a.id,a])),e=[],n=new Map,s=[];for(let a of r.ways)a.to.forEach((l,h)=>{if(!i.has(a.from)||!i.has(l))return;let p=[a.from,l].sort().join("|");n.set(p,(n.get(p)??0)+1),s.push({w:a,t:l,i:h,key:p})});let o=new Map;for(let{w:a,t:l,i:h,key:p}of s){let m=i.get(a.from),x=i.get(l),d=o.get(p)??0;o.set(p,d+1);let c=h===0&&a.label?a.label:"",f=c?_(c,et)+8:0;if(m===x){let A=14+d*10,P={x:m.x+m.w-18-d*8,y:m.y},N={x:m.x+m.w,y:m.y+14+d*4},K={x:P.x,y:P.y-30-A},B={x:N.x+30+A,y:N.y},J=-1/0,Q=1/0;for(let V=0;V<=20;V++){let st=Wt(P,K,B,N,V/20);J=Math.max(J,st.x),Q=Math.min(Q,st.y)}e.push({id:a.id,d:`M${P.x},${P.y} C${K.x},${K.y} ${B.x},${B.y} ${N.x},${N.y}`,tip:[B,N],label:c?{x:J+4+f/2,y:(Q+N.y)/2,w:f,text:c}:null});continue}let u,y;Y(m,x)?[u,y]=bt(m,x):Y(x,m)?[y,u]=bt(x,m):(u=F(m),y=F(x));let g=n.get(p),v=a.from<l,k=(d-(g-1)/2)*26*(v?1:-1);g===2&&!k&&(k=13);let M=(u.x+y.x)/2,$=(u.y+y.y)/2,S=Math.hypot(y.x-u.x,y.y-u.y)||1,j=-(y.y-u.y)/S,D=(y.x-u.x)/S,L={x:M+j*k*2,y:$+D*k*2};!Y(m,x)&&!Y(x,m)&&(u=mt(m,L),y=mt(x,L));let U={x:.25*u.x+.5*L.x+.25*y.x,y:.25*u.y+.5*L.y+.25*y.y},b=k<0?-1:1,w=Math.abs(j)*f/2+Math.abs(D)*9+4;e.push({id:a.id,d:`M${u.x},${u.y} Q${L.x},${L.y} ${y.x},${y.y}`,tip:[L,y],label:c?{x:U.x+j*w*b,y:U.y+D*w*b,w:f,text:c}:null})}for(let a of t.dots){if(!i.has(a.to))continue;let l=Math.hypot(a.tx-a.x,a.ty-a.y)||1,h={x:a.x+(a.tx-a.x)/l*6,y:a.y+(a.ty-a.y)/l*6},p={x:a.tx,y:a.ty};e.push({id:"",start:!0,d:`M${h.x},${h.y} L${p.x},${p.y}`,tip:[h,p],label:null})}return e}var zt=.8,Ht=14,Mt=8,Ut={state:'<rect x="3" y="6" width="18" height="12" rx="4"/>',final:'<rect x="2.5" y="5.5" width="19" height="13" rx="4"/><rect x="5.5" y="8.5" width="13" height="7" rx="2"/>',parallel:'<rect x="3" y="4" width="18" height="16" rx="3" stroke-dasharray="3 2.5"/><path d="M12 7v10"/>',undo:'<path d="M9 14 4 9l5-5"/><path d="M4 9h10a6 6 0 0 1 0 12h-3"/>',redo:'<path d="m15 14 5-5-5-5"/><path d="M20 9H10a6 6 0 0 0 0 12h3"/>',try:'<path d="M7 4v16l13-8z"/>',restart:'<path d="M3 12a9 9 0 1 0 3-6.7L3 8"/><path d="M3 3v5h5"/>',stop:'<path d="M6 6l12 12M18 6 6 18"/>',tidy:'<rect x="3" y="4" width="7" height="6" rx="1.5"/><rect x="14" y="4" width="7" height="6" rx="1.5"/><rect x="8.5" y="14" width="7" height="6" rx="1.5"/><path d="M6.5 10v2h11v-2M12 12v2"/>'};function I(r,t,i,e){let n=document.createElement("button");return n.type="button",r&&(n.className=r),n.innerHTML=`<svg viewBox="0 0 24 24" aria-hidden="true">${Ut[t]}</svg>`,n.append(i),e&&(n.title=e),n}var it=class extends HTMLElement{#g=this.attachShadow({mode:"open"});#w=document.createElement("div");#n=document.createElement("div");#e=null;#U="";#v=null;#j=[];#T=null;#u=null;#L=!1;#D=!1;#N=!1;#i=null;#f=[];#_=0;#p=null;#I=!1;#r=null;#P=1;#y=null;#B=null;#$="";#a=null;#x=!1;#k=null;#l=null;#X=new ResizeObserver(()=>this.#q());constructor(){super();let t=new CSSStyleSheet;t.replaceSync(ut),this.#g.adoptedStyleSheets=[t],this.#w.className="bar",this.#w.setAttribute("role","toolbar"),this.#w.setAttribute("aria-label","Machine"),this.#n.className="stage",this.#n.tabIndex=0,this.#n.setAttribute("aria-label","Drawing of the machine");let i=Object.assign(document.createElement("div"),{className:"frame"});i.append(this.#w,this.#n),this.#g.append(i),this.#it(),this.#M()}connectedCallback(){this.#X.observe(this.#n)}disconnectedCallback(){this.#X.disconnect(),this.#C()}get graph(){return this.#e}set graph(t){let i=JSON.stringify(t??null);i!==this.#U&&(this.#U=i,this.#e=t&&Array.isArray(t.nodes)?t:null,this.#M(),this.#tt())}get selected(){return this.#v}set selected(t){this.#v=typeof t=="string"?t:null,this.#H()}get active(){return this.#j}set active(t){this.#j=Array.isArray(t)?t:[],this.#H()}get editing(){return this.#T}set editing(t){let i=typeof t=="string"?t:null;i&&i!==this.#T&&(this.#u=i),this.#T=i,this.#K()}get trying(){return this.#L}set trying(t){this.#L!==!!t&&(this.#L=!!t,this.#M())}get canUndo(){return this.#D}set canUndo(t){this.#D=!!t,this.#M()}get canRedo(){return this.#N}set canRedo(t){this.#N=!!t,this.#M()}#t(t,i=null){this.dispatchEvent(new CustomEvent(t,{detail:i}))}#o(){return this.#e?.nodes.find(t=>t.parent==null)?.id??null}#s(t){return this.#e?.nodes.find(i=>i.id===t)??null}#A(t){return this.#e?.ways.find(i=>i.id===t)??null}#Q(t,i){for(let e=this.#s(t);e;e=this.#s(e.parent))if(e.id===i)return!0;return!1}#M(){let t=this.#w;t.replaceChildren();let i=[["state","State","Drag it onto the drawing, or click it"],["final","Final","A state the machine ends in"],["parallel","Parallel","A state whose regions run at once"]];for(let[o,a,l]of i){let h=I("tool",o,a,l);h.disabled=!this.#e,h.addEventListener("pointerdown",p=>this.#V(p,o,a)),h.addEventListener("click",()=>{if(this.#x){this.#x=!1;return}this.#t("add",[o,this.#o(),null,this.#c()])}),t.append(h)}t.append(Object.assign(document.createElement("span"),{className:"sep"}));let e=I("","undo","Undo");e.disabled=!this.#D,e.addEventListener("click",()=>this.#t("undo"));let n=I("","redo","Redo");n.disabled=!this.#N,n.addEventListener("click",()=>this.#t("redo"));let s=I("","tidy","Tidy","Lay the machine out again");if(s.disabled=!this.#e||this.#e.nodes.length<2,s.addEventListener("click",()=>this.#et()),t.append(e,n,s),!(!this.#e||this.#e.kind==="fragment"))if(this.#L){let o=I("go","restart","Restart");o.addEventListener("click",()=>this.#t("restart"));let a=I("","stop","Stop");a.addEventListener("click",()=>this.#t("stop")),t.append(o,a)}else{let o=I("go","try","Try","Run the machine here and send it events");o.addEventListener("click",()=>this.#t("try")),t.append(o)}}#V(t,i,e){if(t.button!==0||!this.#e)return;let n=t.currentTarget;n.setPointerCapture(t.pointerId);let s=Object.assign(document.createElement("div"),{className:"ghost",textContent:e}),o=!1,a=null,l=null,h=[t.clientX,t.clientY],p=x=>{!o&&Math.hypot(x.clientX-h[0],x.clientY-h[1])<5||(o||(o=!0,this.#g.append(s)),s.style.left=`${x.clientX+10}px`,s.style.top=`${x.clientY+6}px`,a=this.#i&&this.#Z(x)?this.#G(this.#h(x),null):null,l=a?this.#z(this.#h(x),a,i):null,this.#E(a))},m=x=>{n.removeEventListener("pointermove",p),n.removeEventListener("pointerup",m),n.removeEventListener("pointercancel",m),s.remove(),this.#E(null),o&&(this.#x=!0,x.type==="pointerup"&&a&&this.#t("add",[i,a,l,this.#c(!0)]))};n.addEventListener("pointermove",p),n.addEventListener("pointerup",m),n.addEventListener("pointercancel",m)}#Z(t){let i=this.#n.getBoundingClientRect();return t.clientX>=i.left&&t.clientX<=i.right&&t.clientY>=i.top&&t.clientY<=i.bottom}async#tt(){let t=++this.#_,i=this.#e;if(!i||!i.nodes.length){this.#p=null,this.#r=null,this.#i=null;let n=Object.assign(document.createElement("div"),{className:"empty"});n.append("This file makes no machines yet.");let s=Object.assign(document.createElement("button"),{type:"button",textContent:"New machine"});s.addEventListener("click",()=>this.#t("add",["machine",null])),n.append(s),this.#n.replaceChildren(n);return}let e=new Map(i.nodes.filter(n=>n.parent!=null&&typeof n.x=="number"&&typeof n.y=="number").map(n=>[n.id,{x:n.x,y:n.y}]));if(this.#I=i.nodes.some(n=>!n.fixed&&e.has(n.id)),!this.#I&&i.nodes.length>1)try{e=await this.#R(i)}catch(n){if(t!==this.#_)return;this.#n.replaceChildren(Object.assign(document.createElement("div"),{className:"empty",textContent:`The drawing failed: ${n?.message??n}`}));return}for(let n of wt(i,e))try{for(let[s,o]of await this.#R(n))!e.has(s)&&s!==n.name&&e.set(s,o)}catch{}t===this.#_&&(this.#p=e,this.#$="",this.#q(),this.dispatchEvent(new Event("drawn")))}async#R(t){let[i,e]=await Promise.all(["RIGHT","DOWN"].map(a=>ht().layout(ft(t,a)))),n=Math.max(1,this.#n.clientWidth-8),s=Math.max(1,this.#n.clientHeight-8),o=a=>Math.min(n/a.width,s/a.height);return vt(o(e)>o(i)*1.05?e:i)}async#et(){let t=this.#e;if(!t||t.nodes.length<2)return;let i=await this.#R(t);t===this.#e&&(this.#y=null,this.#t("place",H(t,W(t,i))))}#c(t=!1){return(t||this.#I)&&this.#r?H(this.#e,this.#r):null}#q(){if(!this.#p||!this.#e)return;let t=W(this.#e,this.#p),i=Math.max(1,this.#n.clientWidth-8),e=Math.max(1,this.#n.clientHeight-8),n=this.#y&&this.#B===this.#e.name,s=Math.min(i/t.frame.w,e/t.frame.h),o=n?this.#P:Math.round(Math.min(1,Math.max(zt,s))*20)/20,a=n?Math.min(this.#y.x,t.frame.x):t.frame.x,l=n?Math.min(this.#y.y,t.frame.y):t.frame.y,h={x:a,y:l,w:Math.max(t.frame.x+t.frame.w-a,i/o),h:Math.max(t.frame.y+t.frame.h-l,e/o)},p=`${o} ${h.x} ${h.y} ${Math.round(h.w)} ${Math.round(h.h)}`;p!==this.#$&&(this.#$=p,this.#P=o,this.#r=t,this.#y=h,this.#B=this.#e.name,this.#Y(t,h),this.#K(),this.#u&&!this.#d(this.#u)&&(this.#u=null))}#Y(t,i,e=null){let n=this.#g.activeElement?.closest?.(".node")?.getAttribute("data-id"),{svg:s,rects:o}=pt(this.#e,t,kt(this.#e,t),this.#P,i);this.#i=s,this.#f=o,this.#k=null;let a=[s],l=this.#e.nodes.length-1;!e&&(l<3||this.#e.ways.length===0)&&a.push(Object.assign(document.createElement("div"),{className:"hint",textContent:"Drag a state from the bar, or double-click an empty spot. Drag a state to place it, into another to put it inside. Drag from the dots around a state to join it to another."})),this.#n.replaceChildren(...a),e&&this.#d(e)?.classList.add("lifted"),n&&this.#d(n)?.focus({preventScroll:!0}),this.#l&&(this.#n.append(this.#l.input),this.#l.place()?this.#l.input.focus():this.#l.input.remove()),this.#H()}#nt(){this.#$="",this.#q()}#W(t){return this.#r?.boxes.find(i=>i.id===t)??null}#z(t,i,e="state"){let n=this.#r&&z(this.#e,this.#r,i),{w:s,h:o}=G(e==="final"?"done":e==="parallel"?"together":"state");return n?[Math.round(t.x-n.x-s/2),Math.round(t.y-n.y-o/2)]:null}#H(){let t=this.#i;if(!t)return;let i=new Set(this.#j);for(let e of t.querySelectorAll("[data-id]")){let n=e.getAttribute("data-id");e.classList.toggle("selected",n!==""&&n===this.#v),e.classList.toggle("active",i.has(n))}}#d(t){return t?this.#i?.querySelector(`[data-id="${CSS.escape(t)}"]`):null}#h(t){let i=this.#i.createSVGPoint();return i.x=t.clientX,i.y=t.clientY,i.matrixTransform(this.#i.getScreenCTM().inverse())}#O(t,i,e=0){return i.x>=t.x-e&&i.x<=t.x+t.width+e&&i.y>=t.y-e&&i.y<=t.y+t.height+e}#m(t){let i=this.#f.find(n=>n.kind!=="machine"&&this.#O(n,t))??null,e=i&&this.#s(i.id)?.owner;return e?this.#f.find(n=>n.id===e)??null:i}#F(t){let i=t.getAttribute("data-id");return this.#A(i)?.owner??i}#G(t,i){for(let e of this.#f)if(!(e.kind==="final"||this.#s(e.id)?.fixed||!this.#O(e,t))&&!(i&&this.#Q(e.id,i)))return e.id;return this.#o()}#E(t){for(let i of this.#i?.querySelectorAll(".node.target")??[])i.classList.remove("target");this.#d(t)?.classList.add("target")}#S(t){let i=this.#i?.querySelector(".overlay");if(!i||this.#k===t?.id||(this.#k=t?.id??null,i.replaceChildren(),!t||this.#a))return;let e=[[t.x+t.width/2,t.y],[t.x+t.width,t.y+t.height/2],[t.x+t.width/2,t.y+t.height],[t.x,t.y+t.height/2]];for(let[n,s]of e){let o=E("circle",{class:"handle",cx:n,cy:s,r:5.5,"data-for":t.id},i);E("title",{},o).textContent="Drag onto another state to make a way"}}#it(){let t=this.#n;t.addEventListener("pointermove",e=>{if(!this.#i)return;let n=this.#h(e),s=this.#a;if(!s){let o=this.#m(n);if(o)this.#S(o);else{let a=this.#f.find(l=>l.id===this.#k);(!a||!this.#O(a,n,Ht))&&this.#S(null)}return}if(s.mode==="link"){s.line.setAttribute("d",`M${s.start.x},${s.start.y} L${n.x},${n.y}`),s.over=this.#m(n)?.id??null,this.#E(s.over);return}if(s.mode==="press"&&Math.hypot(e.clientX-s.client[0],e.clientY-s.client[1])>5){let o=this.#W(s.id);if(!o)return;s.mode="move",s.grab={x:s.at.x-o.x,y:s.at.y-o.y},s.frame=this.#y,this.#S(null)}s.mode==="move"&&(s.x=n.x-s.grab.x,s.y=n.y-s.grab.y,s.point=n,s.frame_asked||(s.frame_asked=!0,requestAnimationFrame(()=>{s.frame_asked=!1,this.#a===s&&(s.arr=W(this.#e,this.#p,{id:s.id,x:s.x,y:s.y}),this.#Y(s.arr,s.frame,s.id),s.into=this.#G(s.point,s.id),this.#E(s.into))})))}),t.addEventListener("pointerdown",e=>{if(this.#C(),e.button!==0||!this.#i)return;let n=this.#h(e),s=e.target.closest?.(".handle");if(s){e.preventDefault(),t.setPointerCapture(e.pointerId);let a=s.getAttribute("data-for"),l={x:+s.getAttribute("cx"),y:+s.getAttribute("cy")},h=E("path",{class:"rubber",d:`M${l.x},${l.y}`},this.#i);this.#a={mode:"link",from:a,start:l,line:h,over:null},this.#i.querySelector(".overlay")?.replaceChildren(),this.#k=null;return}if(e.target.closest?.(".way"))return;let o=this.#m(n);o&&(t.setPointerCapture(e.pointerId),this.#a={mode:"press",id:o.id,client:[e.clientX,e.clientY],at:n})});let i=e=>{let n=this.#a;if(n&&(this.#a=null,this.#E(null),n.line?.remove(),n.mode==="move"&&this.#nt(),e.type==="pointerup")){if(this.#x=!0,n.mode==="press")n.id!==this.#v&&this.#t("pick",n.id);else if(n.mode==="link")if(n.over)this.#t("connect",[n.from,n.over]);else{let s=this.#s(n.from)?.parent;this.#t("grow",[n.from,this.#z(this.#h(e),s),this.#c(!0)])}else if(n.mode==="move"&&n.arr){let s=this.#s(n.id),o=n.into??s?.parent,a=z(this.#e,n.arr,o);if(!s||!a)return;let l=[Math.round(n.x-a.x),Math.round(n.y-a.y)],h=H(this.#e,n.arr,n.id,o);o===s.parent?this.#t("place",h):this.#t("move",[n.id,o,l,h])}}};t.addEventListener("pointerup",i),t.addEventListener("pointercancel",i),t.addEventListener("pointerleave",()=>{this.#a||this.#S(null)}),t.addEventListener("click",e=>{if(this.#x){this.#x=!1;return}if(!this.#i)return;let n=e.target.closest?.(".way");if(n)return this.#t("pick",this.#F(n));this.#m(this.#h(e))||this.#t("pick",this.#o())}),t.addEventListener("dblclick",e=>{if(!this.#i)return;e.preventDefault();let n=e.target.closest?.(".way");if(n){let h=this.#A(n.getAttribute("data-id"));return h?.fixed?this.#t("fragment",this.#s(h.owner)?.uses):this.#b(h?.id)}let s=this.#h(e),o=this.#m(s),a=h=>this.#t("add",["state",h,this.#z(s,h),this.#c(!0)]);if(!o)return a(this.#o());let l=this.#f.find(h=>h.kind!=="machine"&&this.#O(h,s));if(l&&this.#s(l.id)?.fixed)return this.#t("fragment",this.#s(o.id)?.uses);if((o.kind==="group"||o.kind==="parallel")&&s.y>o.y+T)return a(o.id);this.#b(o.id)}),t.addEventListener("contextmenu",e=>{if(!this.#i)return;e.preventDefault();let n=e.target.closest?.(".way"),s=n?null:this.#m(this.#h(e));this.#J(e,n?this.#F(n):s?.id??this.#o())}),t.addEventListener("keydown",e=>{let n=e.target.closest?.(".node"),s=n?.getAttribute("data-id")??this.#v;if(e.key==="Enter"&&n)e.preventDefault(),this.#t("pick",s);else if((e.key==="Delete"||e.key==="Backspace")&&s&&s!==this.#o())e.preventDefault(),this.#t("remove",s);else if(e.key==="F2"&&s&&s!==this.#o())e.preventDefault(),this.#b(s);else if(e.key.startsWith("Arrow")&&s&&this.#s(s)&&s!==this.#o())e.preventDefault(),this.#st(s,e.key,e.shiftKey?Mt*4:Mt);else if(e.key==="ContextMenu"&&n){e.preventDefault();let o=n.getBoundingClientRect();this.#J({clientX:o.left+8,clientY:o.bottom},s)}})}#st(t,i,e){let n=this.#W(t);if(!n)return;let[s,o]={ArrowLeft:[-e,0],ArrowRight:[e,0],ArrowUp:[0,-e],ArrowDown:[0,e]}[i]??[0,0],a=W(this.#e,this.#p,{id:t,x:n.x+s,y:n.y+o});this.#t("place",H(this.#e,a,t,this.#s(t).parent))}#K(){let t=this.#u;!t||!this.#d(t)||(this.#u=null,this.#b(t))}#b(t){if(!this.#d(t))return;let e=this.#A(t),n=e?null:this.#s(t);if(!e&&(!n||n.kind==="machine"))return;let s=e?e.events??"":n.name,o=Object.assign(document.createElement("input"),{className:"inline",value:s,spellcheck:!1});o.setAttribute("aria-label",e?"The events the way is taken on":"The name of the state"),e&&(o.placeholder="event");let a=()=>{let x=this.#d(t);if(!x)return!1;for(let g of this.#i?.querySelectorAll(".naming")??[])g.classList.remove("naming");x.classList.add("naming");let d=this.#n.getBoundingClientRect(),c,f,u;if(e)c=(x.querySelector(".label-bg")??x.querySelector(".line")).getBoundingClientRect(),f=28,u=6;else{let g=x.querySelector(".box"),v=g.getBoundingClientRect(),k=v.width/(+g.getAttribute("width")||v.width||1),M=x.classList.contains("group")||x.classList.contains("parallel");c=M?{left:v.left+4*k,top:v.top+3*k,width:v.width-8*k,height:(T-2)*k}:v,f=c.height,u=(M?7:9)*k}o.style.height=`${f}px`,o.style.borderRadius=`${u}px`,o.style.width=`${c.width}px`;let y=Math.max(c.width,o.scrollWidth>o.clientWidth?o.scrollWidth+16:0,e?90:0);return o.style.width=`${y}px`,o.style.left=`${Math.max(this.#n.scrollLeft+2,c.left-d.left+this.#n.scrollLeft+c.width/2-y/2)}px`,o.style.top=`${c.top-d.top+this.#n.scrollTop+c.height/2-f/2}px`,!0};o.addEventListener("input",()=>a());let l=!1,h=!1,p=x=>{x.composedPath().includes(o)||(h=!0)};document.addEventListener("pointerdown",p,!0);let m=x=>{if(l)return;l=!0,document.removeEventListener("pointerdown",p,!0),this.#l=null,o.remove();for(let c of this.#i?.querySelectorAll(".naming")??[])c.classList.remove("naming");let d=o.value.trim();x&&d&&d!==s&&this.#t("rename",[t,d]),this.#n.focus({preventScroll:!0})};o.addEventListener("keydown",x=>{x.stopPropagation(),x.key==="Tab"&&(h=!0),x.key==="Enter"?(x.preventDefault(),m(!0)):x.key==="Escape"&&(x.preventDefault(),m(!1))}),o.addEventListener("blur",()=>{if(h)return m(!0);setTimeout(()=>{!l&&o.isConnected&&o.focus()})}),o.addEventListener("pointerdown",x=>x.stopPropagation()),o.addEventListener("dblclick",x=>x.stopPropagation()),this.#l?.finish(!0),this.#n.append(o),a(),this.#l={id:t,input:o,place:a,finish:m},o.focus(),o.select()}#J(t,i){if(this.#C(),!i)return;let e=[],n=this.#A(i),s=n?null:this.#s(i);if(n)e.push(["Rename its events",()=>this.#b(i)],["Delete the way",()=>this.#t("remove",i),"danger"]);else if(s?.kind==="machine")e.push(["Add a state",()=>this.#t("add",["state",i,null,this.#c()])],["Add a final state",()=>this.#t("add",["final",i,null,this.#c()])]);else if(s){let o=this.#s(s.parent);if(!s.initial&&o?.kind!=="parallel"&&e.push(["Make it the initial state",()=>this.#t("initial",i)]),s.kind!=="final"&&e.push(["Add a state inside",()=>this.#t("add",["state",i,null,this.#c()])]),s.uses){let a=this.#e.nodes.some(l=>l.owner===i);e.push([a?`Close ${s.uses}`:`Open ${s.uses}: see its states here`,()=>this.#t("open",i)],[`Edit the fragment ${s.uses}`,()=>this.#t("fragment",s.uses)])}e.push(["Rename",()=>this.#b(i)]),s.parent!==this.#o()&&e.push(["Take it out of "+(o?.name??"its state"),()=>{let a=this.#W(i),l=z(this.#e,this.#r,o?.parent),h=a&&l?[Math.round(a.x-l.x),Math.round(a.y-l.y)]:null;this.#t("move",[i,o?.parent,h,this.#c(!0)])}]),e.push(["Delete the state",()=>this.#t("remove",i),"danger"])}e.length&&(this.#t("pick",i),this.#C=yt(this.#g,t,e,()=>this.#n.focus()))}#C=()=>{}};customElements.define("tb-machine-graph",it);
@@ -0,0 +1,121 @@
1
+ Trebol [
2
+ title: "The edits of a machine made by dragging on its drawing: a state put into another, a state and a way at once"
3
+ exports: [move-state grow-state machine-index rename-way-events]
4
+ ]
5
+
6
+ import %model.trb
7
+ import %machines.trb
8
+ import %machine-edits.trb
9
+ import %machine-places.trb
10
+
11
+ ; Each edit gives the file's new text, or none when it cannot be made, as those of machine-edits.trb do. They
12
+ ; are made in steps, the design read again from the text between them, and give the text at the end: one
13
+ ; change for Undo. States are found again after a step by their names, which the designer keeps apart.
14
+
15
+ machine-index: function ["The place of a machine among those of a design" d [object!] m [object!]] [
16
+ k: 0
17
+ foreach x machines-of d [k: k + 1 if same? x/block m/block [return k]]
18
+ none
19
+ ]
20
+
21
+ node-named: function [m [object!] name] [
22
+ found: none
23
+ foreach n next m/nodes [
24
+ if n/name = name [
25
+ if found [return none] ; two of that name: none is the one meant
26
+ found: n/id
27
+ ]
28
+ ]
29
+ found
30
+ ]
31
+
32
+ ; The ways' targets written as the engine finds them from where each way is, once states have moved.
33
+ fix-targets: function [text [string!] k [integer!]] [
34
+ d: design-of text
35
+ m: pick machines-of d k
36
+ changes: copy []
37
+ foreach w m/ways [
38
+ foreach [b i] w/targets [
39
+ v: pick b i
40
+ name: either path? :v [last v] [:v]
41
+ if all [word? :name id: node-named m name] [
42
+ want: target-name m w/level id
43
+ if all [want want <> :v] [append changes value-change d b i mold want]
44
+ ]
45
+ ]
46
+ ]
47
+ either empty? changes [text] [apply-changes d/text changes]
48
+ ]
49
+
50
+ ; The code written from an index on, as lines with the first one's indent taken off the rest.
51
+ code-at: function [d [object!] s [pair!]] [
52
+ code: copy/part at d/text s/x s/y
53
+ indent: indent-at d/text s/x
54
+ either empty? indent [code] [replace/all code rejoin [newline indent] newline]
55
+ ]
56
+
57
+ move-state: function [
58
+ "The text with a state put inside another state, or in the machine: its ways out go with it, and every way into it is written anew"
59
+ d [object!] m [object!] id [integer!] into [integer!]
60
+ ] [
61
+ n: machine-node m id
62
+ to-node: machine-node m into
63
+ if any [id = 1 into = n/parent inside-state? m into id to-node/kind = 'final] [return none]
64
+ k: machine-index d m
65
+ p: machine-node m n/parent
66
+ ; out: the state, and the ways out of it written beside it
67
+ code: code-at d range-span d n/holder n/from n/to
68
+ changes: cut-change d n/holder n/from n/to
69
+ foreach w m/ways [
70
+ if all [w/level = n/parent w/source = n/name] [
71
+ append code rejoin [newline code-at d range-span d w/block w/from w/to]
72
+ append changes cut-change d w/block w/from w/to
73
+ ]
74
+ ]
75
+ if all [p/initial = n/name p/initial-at] [
76
+ others: exclude copy p/children reduce [id]
77
+ append changes either empty? others [
78
+ cut-change d p/block p/initial-at p/initial-at + 1
79
+ ] [
80
+ value-change d p/block p/initial-at + 1 form select machine-node m first others 'name
81
+ ]
82
+ ]
83
+ t: apply-changes d/text changes
84
+ ; in: after the last state of its new place, the first one of a place with none being its initial
85
+ d2: attempt [design-of t]
86
+ unless all [d2 m2: pick machines-of d2 k] [return none]
87
+ place: either into = 1 [1] [node-named m2 to-node/name]
88
+ unless place [return none]
89
+ q: machine-node m2 place
90
+ if all [empty? q/children q/kind <> 'parallel] [code: rejoin ["initial " n/name newline code]]
91
+ k2: either empty? q/children [if q/initial-at [q/initial-at + 1]] [select machine-node m2 last q/children 'to]
92
+ t: into-node d2 q k2 code
93
+ unless t: attempt [fix-targets t k] [return none]
94
+ ; its places, and those of the states inside it, go where it went
95
+ new: append either into = 1 [copy []] [machine-path m into] n/name
96
+ rename-places t machine-key d m to path! machine-path m id to path! new
97
+ ]
98
+
99
+ grow-state: function [
100
+ "The text with a new state beside one, and a way from that one to it on an event"
101
+ d [object!] m [object!] from [integer!] name [word!] event [word!]
102
+ ] [
103
+ s: machine-node m from
104
+ level: machine-node m s/parent
105
+ ; the regions of a parallel state have no ways between them
106
+ if any [from = 1 level/kind = 'parallel] [return none]
107
+ k: machine-index d m
108
+ unless t: add-state d m level/id name 'state [return none]
109
+ d2: design-of t
110
+ m2: pick machines-of d2 k
111
+ add-way d2 m2 node-named m2 s/name node-named m2 name event
112
+ ]
113
+
114
+ rename-way-events: function [
115
+ "The text with the events of a way as typed: start, start resume or [start resume]"
116
+ d [object!] m [object!] w [object!] typed [string!]
117
+ ] [
118
+ v: attempt [load/all typed]
119
+ if all [block? v 1 = length? v block? first v] [v: first v]
120
+ either all [block? v not empty? v parse v [some word!]] [set-way-events d m w v] [none]
121
+ ]