@stocksharp/diagram 0.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.
Files changed (87) hide show
  1. package/LICENSE +30 -0
  2. package/NOTICE +21 -0
  3. package/README.md +346 -0
  4. package/dist/esm/canvas-renderer.js +3226 -0
  5. package/dist/esm/canvas-renderer.js.map +1 -0
  6. package/dist/esm/core/action-registry.js +35 -0
  7. package/dist/esm/core/action-registry.js.map +1 -0
  8. package/dist/esm/core/document.js +350 -0
  9. package/dist/esm/core/document.js.map +1 -0
  10. package/dist/esm/core/history.js +136 -0
  11. package/dist/esm/core/history.js.map +1 -0
  12. package/dist/esm/core/model.js +2 -0
  13. package/dist/esm/core/model.js.map +1 -0
  14. package/dist/esm/core/state.js +73 -0
  15. package/dist/esm/core/state.js.map +1 -0
  16. package/dist/esm/core/view-state.js +70 -0
  17. package/dist/esm/core/view-state.js.map +1 -0
  18. package/dist/esm/diagram/api.js +2 -0
  19. package/dist/esm/diagram/api.js.map +1 -0
  20. package/dist/esm/diagram/catalog.js +45 -0
  21. package/dist/esm/diagram/catalog.js.map +1 -0
  22. package/dist/esm/diagram/event-emitter.js +42 -0
  23. package/dist/esm/diagram/event-emitter.js.map +1 -0
  24. package/dist/esm/diagram/palette.js +269 -0
  25. package/dist/esm/diagram/palette.js.map +1 -0
  26. package/dist/esm/diagram/stocksharp-diagram.js +784 -0
  27. package/dist/esm/diagram/stocksharp-diagram.js.map +1 -0
  28. package/dist/esm/diagram/types.js +105 -0
  29. package/dist/esm/diagram/types.js.map +1 -0
  30. package/dist/esm/embed.js +398 -0
  31. package/dist/esm/embed.js.map +1 -0
  32. package/dist/esm/i18n.js +12 -0
  33. package/dist/esm/i18n.js.map +1 -0
  34. package/dist/esm/index.js +12 -0
  35. package/dist/esm/index.js.map +1 -0
  36. package/dist/ssdiagram.js +5141 -0
  37. package/dist/ssdiagram.js.map +7 -0
  38. package/dist/types/canvas-renderer.d.ts +512 -0
  39. package/dist/types/canvas-renderer.d.ts.map +1 -0
  40. package/dist/types/core/action-registry.d.ts +18 -0
  41. package/dist/types/core/action-registry.d.ts.map +1 -0
  42. package/dist/types/core/document.d.ts +12 -0
  43. package/dist/types/core/document.d.ts.map +1 -0
  44. package/dist/types/core/history.d.ts +33 -0
  45. package/dist/types/core/history.d.ts.map +1 -0
  46. package/dist/types/core/model.d.ts +85 -0
  47. package/dist/types/core/model.d.ts.map +1 -0
  48. package/dist/types/core/state.d.ts +68 -0
  49. package/dist/types/core/state.d.ts.map +1 -0
  50. package/dist/types/core/view-state.d.ts +14 -0
  51. package/dist/types/core/view-state.d.ts.map +1 -0
  52. package/dist/types/diagram/api.d.ts +213 -0
  53. package/dist/types/diagram/api.d.ts.map +1 -0
  54. package/dist/types/diagram/catalog.d.ts +19 -0
  55. package/dist/types/diagram/catalog.d.ts.map +1 -0
  56. package/dist/types/diagram/event-emitter.d.ts +9 -0
  57. package/dist/types/diagram/event-emitter.d.ts.map +1 -0
  58. package/dist/types/diagram/palette.d.ts +63 -0
  59. package/dist/types/diagram/palette.d.ts.map +1 -0
  60. package/dist/types/diagram/stocksharp-diagram.d.ts +157 -0
  61. package/dist/types/diagram/stocksharp-diagram.d.ts.map +1 -0
  62. package/dist/types/diagram/types.d.ts +171 -0
  63. package/dist/types/diagram/types.d.ts.map +1 -0
  64. package/dist/types/embed.d.ts +35 -0
  65. package/dist/types/embed.d.ts.map +1 -0
  66. package/dist/types/i18n.d.ts +167 -0
  67. package/dist/types/i18n.d.ts.map +1 -0
  68. package/dist/types/index.d.ts +22 -0
  69. package/dist/types/index.d.ts.map +1 -0
  70. package/package.json +126 -0
  71. package/sample.png +0 -0
  72. package/src/canvas-renderer.ts +3249 -0
  73. package/src/core/action-registry.ts +46 -0
  74. package/src/core/document.ts +387 -0
  75. package/src/core/history.ts +154 -0
  76. package/src/core/model.ts +99 -0
  77. package/src/core/state.ts +148 -0
  78. package/src/core/view-state.ts +82 -0
  79. package/src/diagram/api.ts +257 -0
  80. package/src/diagram/catalog.ts +55 -0
  81. package/src/diagram/event-emitter.ts +44 -0
  82. package/src/diagram/palette.ts +312 -0
  83. package/src/diagram/stocksharp-diagram.ts +945 -0
  84. package/src/diagram/types.ts +332 -0
  85. package/src/embed.ts +508 -0
  86. package/src/i18n.ts +210 -0
  87. package/src/index.ts +171 -0
package/LICENSE ADDED
@@ -0,0 +1,30 @@
1
+ StockSharp Custom License Notice
2
+
3
+ Copyright (c) 2010-present StockSharp Platform LLC and/or its affiliates.
4
+ All rights reserved.
5
+
6
+ All source code, binaries, documentation, examples, media assets,
7
+ configuration files, and other materials contained in this repository are
8
+ the proprietary property of StockSharp, unless a file or third-party notice
9
+ explicitly states otherwise.
10
+
11
+ This repository is not licensed under the Apache License, the MIT License,
12
+ or any other general-purpose open source license.
13
+ Viewing, downloading, copying, building, modifying, using, distributing, or
14
+ otherwise accessing any part of this repository is permitted only under the
15
+ StockSharp End User License Agreement and other applicable StockSharp terms
16
+ published on the official StockSharp website:
17
+
18
+ https://stocksharp.com/en/products/eula/
19
+
20
+ StockSharp may update its license terms on the official website. Users are
21
+ responsible for monitoring the official StockSharp website and complying
22
+ with the then-current terms. If this notice conflicts with the EULA or other
23
+ terms published on the official StockSharp website, the website terms control.
24
+
25
+ Nothing in this repository grants any rights to StockSharp trademarks,
26
+ service marks, product names, logos, or other brand assets except as
27
+ expressly permitted by StockSharp in writing.
28
+
29
+ Third-party components, if any, remain subject to their respective license
30
+ terms and notices.
package/NOTICE ADDED
@@ -0,0 +1,21 @@
1
+ StockSharp Notice
2
+
3
+ StockSharp Platform LLC
4
+ https://stocksharp.com/
5
+
6
+ Copyright (c) 2010-present StockSharp Platform LLC and/or its affiliates.
7
+ All rights reserved.
8
+
9
+ The materials in this repository are proprietary to StockSharp unless a file
10
+ or third-party notice explicitly states otherwise. Use of these materials is
11
+ governed by the StockSharp End User License Agreement and other applicable
12
+ terms published on the official StockSharp website:
13
+
14
+ https://stocksharp.com/en/products/eula/
15
+
16
+ StockSharp may update its license terms on the official website. Users are
17
+ responsible for monitoring the official StockSharp website and complying
18
+ with the then-current terms.
19
+
20
+ This repository may include third-party components. Those components remain
21
+ subject to their respective license terms and notices.
package/README.md ADDED
@@ -0,0 +1,346 @@
1
+ # @stocksharp/diagram
2
+
3
+ [![Build and test](https://github.com/StockSharp/Diagram/actions/workflows/ci.yml/badge.svg)](https://github.com/StockSharp/Diagram/actions/workflows/ci.yml)
4
+ [![npm version](https://img.shields.io/npm/v/%40stocksharp%2Fdiagram.svg)](https://www.npmjs.com/package/@stocksharp/diagram)
5
+ [![License](https://img.shields.io/badge/license-StockSharp%20EULA-c8202f.svg)](LICENSE)
6
+
7
+ **StockSharp JS Strategy Diagram** is the complete browser strategy-diagram
8
+ component: a typed `StockSharpDiagram` API, catalog and palette, canvas
9
+ renderer, read-only web embed, and versioned diagram document model.
10
+
11
+ ![StockSharp JS Strategy Diagram — visual strategy editor with typed connections and element palette](sample.png)
12
+
13
+ [Live demo](https://stocksharp.github.io/Diagram/demo/) ·
14
+ [GitHub repository](https://github.com/StockSharp/Diagram) ·
15
+ [Issue tracker](https://github.com/StockSharp/Diagram/issues)
16
+
17
+ The demo uses the same full stack exported to applications. It is not a
18
+ separate mock renderer.
19
+
20
+ ## Quick start
21
+
22
+ ```sh
23
+ npm install @stocksharp/diagram
24
+ ```
25
+
26
+ ```ts
27
+ import {
28
+ DiagramNode,
29
+ Link,
30
+ Node,
31
+ StockSharpCatalog,
32
+ StockSharpDiagram,
33
+ } from '@stocksharp/diagram';
34
+
35
+ const catalog = new StockSharpCatalog();
36
+ catalog.addNodeType(new Node({
37
+ id: 'source',
38
+ name: 'Market Data',
39
+ outPorts: [{ id: 'candles', name: 'Candles', type: 'Candle' }],
40
+ }));
41
+
42
+ const host = document.querySelector<HTMLElement>('#diagram')!;
43
+ const diagram = new StockSharpDiagram({ div: host, catalog });
44
+
45
+ diagram.load([
46
+ new DiagramNode({
47
+ id: 'market',
48
+ typeId: 'source',
49
+ name: 'BTC/USDT',
50
+ outPorts: [{ id: 'candles', name: 'Candles', type: 'Candle' }],
51
+ x: 20,
52
+ y: 80,
53
+ }),
54
+ ], []);
55
+
56
+ diagram.setTheme({
57
+ diagramBackground: '#131820',
58
+ gridColor: '#1e2633',
59
+ });
60
+ ```
61
+
62
+ The package also ships a ready-to-use browser bundle exposed as
63
+ `window.SSDiagram`:
64
+
65
+ ```html
66
+ <script src="https://cdn.jsdelivr.net/npm/@stocksharp/diagram@0.1.0/dist/ssdiagram.js"></script>
67
+ <script>
68
+ const { StockSharpCatalog, StockSharpDiagram } = window.SSDiagram;
69
+ </script>
70
+ ```
71
+
72
+ See the [complete example](https://github.com/StockSharp/Diagram/blob/main/examples/basic.ts)
73
+ for catalog construction, the draggable palette, typed links, history,
74
+ read-only mode, resize handling and theme switching.
75
+
76
+ The palette mirrors the Designer toolbox contract without owning host
77
+ behaviour. Subscribe to `nodeActivated` to insert/open an element and to
78
+ `contextMenuRequested` to show host-specific help. `setExcludedTypeIds()` and
79
+ `setNodeTypeExcluded()` hide elements dynamically; selection, filtering,
80
+ category expansion and catalog refreshes remain stable. Call `destroy()` when
81
+ the palette host is disposed so its catalog subscription is released.
82
+
83
+ Read-only mode remains inspectable: nodes, links and ports can still be
84
+ selected, copied and opened through host actions, while move/link/delete,
85
+ paste and history commands are disabled. Applications that need a different
86
+ policy can use `setInteractionPermissions()`.
87
+
88
+ `copySelectionToClipboard()` and `pasteSelectionFromClipboard()` use the
89
+ browser text clipboard when available and fall back to the last in-memory
90
+ copy. The versioned payload preserves node, port and link metadata and pastes
91
+ the complete selection as one undo transaction.
92
+
93
+ Node dragging snaps to the visible grid by default in `StockSharpDiagram`.
94
+ Configure it at construction time with `gridSnap` / `gridSize`, or at runtime
95
+ with `setGridSnap(enabled, size)`. Arrow keys move the current node selection
96
+ by one grid cell (Shift moves five); the whole gesture is one undo step.
97
+
98
+ Variadic input sockets use `isDynamic: true` with `dynamicMode: 'onConnect'`.
99
+ Connecting to that anchor creates a single-link sibling typed from the source;
100
+ disconnecting, relinking, or deleting the source prunes an orphan sibling. The
101
+ port and wire lifecycle is one undoable transaction and round-trips unchanged.
102
+
103
+ Port cardinality follows the Designer `LinkableMaximum` contract on both ends:
104
+ `maxLinks: 0` is unlimited, while a positive value limits that input or output.
105
+ An unlimited input accepts multiple sources and an unlimited output can fan out
106
+ to multiple targets; the same output/input pair is still never duplicated.
107
+ Use `updatePort(nodeId, direction, portId, patch)` to change `maxLinks`, type or
108
+ accepted types at runtime. Lowering a limit keeps existing wires and only
109
+ rejects new ones. `Any`, `Object`, `System.Object` and `*` are wildcard socket
110
+ types and therefore connect to every concrete type.
111
+
112
+ Drag a connected input socket with one wire to retarget that wire directly.
113
+ To move an output end, or choose one wire on a socket with several connections,
114
+ select the wire and drag its diamond endpoint handle. Dragging an output socket
115
+ itself always means "create another wire", so fan-out behaviour stays
116
+ unambiguous. Every successful relink is one undoable edit.
117
+
118
+ Wrap a host properties form in `transaction(label, action)` when it changes
119
+ several node or port fields. The edits are committed as one undo/redo operation;
120
+ if the action throws, every edit made inside it is rolled back.
121
+
122
+ The component shows a fullscreen request button in the diagram's top-right
123
+ corner. Clicking it only emits `fullscreenRequested`; it never changes page
124
+ layout or calls the browser Fullscreen API. The host decides whether to use a
125
+ CSS overlay, a modal, or `requestFullscreen()`, then acknowledges the applied
126
+ state with `setFullscreenState(value)`. That updates the icon and emits
127
+ `fullscreenChanged`. Set `showFullscreenButton: false` at construction time,
128
+ or call `setFullscreenButtonVisible(false)`, to hide the control. The button
129
+ accepts `--ssdiagram-control-background`,
130
+ `--ssdiagram-control-border` and `--ssdiagram-control-color` CSS overrides.
131
+
132
+ Viewport preferences are deliberately separate from the strategy document.
133
+ Persist `diagram.saveViewState()` in host settings and restore it with
134
+ `diagram.loadViewState(value)`. The versioned snapshot contains zoom, pan and
135
+ overview visibility; `viewChanged` fires for programmatic and interactive
136
+ viewport changes. A damaged settings value throws `DiagramViewStateError`
137
+ without modifying the current viewport.
138
+
139
+ `takeScreenshot()` returns a detached canvas exactly like the Charts API and
140
+ the WPF `SaveToImage` flow. With no options it copies the current viewport;
141
+ `takeScreenshot({ scope: 'content', pixelRatio: 2 })` renders the complete
142
+ scheme without moving or resizing the visible editor. Export options control
143
+ padding, background, grid, overview, selection and transient runtime state;
144
+ encode the returned canvas with `toBlob()` or `toDataURL()`.
145
+
146
+ The Docs/Portal helpers return a `DiagramEmbedHandle`. Re-rendering the same
147
+ host disposes its previous canvas, observers and timers; removing the host from
148
+ the DOM also disposes it automatically. Custom integrations can call
149
+ `handle.destroy()` or `destroyRenderedDiagram(host)` explicitly.
150
+ If a saved scheme references an element absent from the current palette, its
151
+ node is rendered as a transient red placeholder whose hover tooltip names the
152
+ missing type. Sites can localize that message through
153
+ `data-diagram-missing-element="Missing: {typeId}"` on the host.
154
+
155
+ ### Node actions and errors
156
+
157
+ Double-click handling is opt-in. Give only the node types controlled by the
158
+ host a non-empty `openAction`, then dispatch that value from `nodeOpen`:
159
+
160
+ ```ts
161
+ catalog.addNodeType(new Node({
162
+ id: 'indicator',
163
+ name: 'Indicator',
164
+ openAction: 'indicatorSettings',
165
+ }));
166
+
167
+ diagram.on('nodeOpen', ({ nodes }) => {
168
+ const node = nodes[0];
169
+ if (node?.openAction === 'indicatorSettings') openIndicatorDialog(node);
170
+ });
171
+ ```
172
+
173
+ Socket input is reported separately through `portClicked`, with
174
+ `leftClick`/`rightClick`, direction, and keyboard modifiers. A right-click
175
+ never starts a wire. `contextMenuRequested` also includes the exact port when
176
+ the menu was opened over a socket.
177
+
178
+ Runtime failures flash the node border before leaving it red. Errors found
179
+ while loading a scheme use a red background. Hovering either state shows the
180
+ full error text in a tooltip:
181
+
182
+ ```ts
183
+ diagram.setNodeError('orders', 'Order volume is not configured.');
184
+
185
+ diagram.load(nodes, links, {
186
+ nodeErrors: {
187
+ indicator_2: 'The saved Period value is invalid.',
188
+ },
189
+ });
190
+
191
+ diagram.clearNodeError('orders');
192
+ ```
193
+
194
+ Use `{ kind: 'load' }` with `setNodeError` to add a load-style error after the
195
+ initial load. Errors applied through this API are transient and are not written
196
+ by `save()`.
197
+
198
+ ### Debugger state
199
+
200
+ Execution state is deliberately separate from the saved scheme and undo
201
+ history. The host can mark the active element, publish socket values and
202
+ breakpoints, or cover an unusable scheme with a global status:
203
+
204
+ ```ts
205
+ diagram.setActiveNode('indicator_2');
206
+ diagram.setPortRuntimeState('indicator_2', 'out', 'value', {
207
+ breakpoint: true,
208
+ breakpointActive: true,
209
+ value: '102.45',
210
+ });
211
+ diagram.setGlobalError('This strategy is encrypted.', 'encrypted');
212
+ ```
213
+
214
+ Use `setRuntimeState()` for an atomic debugger snapshot and
215
+ `clearRuntimeState()` when execution stops. `runtimeStateChanged` always
216
+ returns a detached snapshot safe for host-side state stores.
217
+
218
+ If `loadDocument()` receives malformed JSON or an unsupported document, the
219
+ currently displayed scheme is left intact, a global `load` overlay shows the
220
+ failure, and `documentLoadFailed` is emitted. The original exception is still
221
+ thrown so callers can log or report it; loading a valid document clears the
222
+ overlay.
223
+
224
+ ## Architecture
225
+
226
+ The component has one document model and a separate rendering layer:
227
+
228
+ | Layer | Source | Purpose |
229
+ | --- | --- | --- |
230
+ | Document core | `src/core/*` | Versioned document serialization plus independent runtime, view and selection state |
231
+ | Public component API | `src/diagram/stocksharp-diagram.ts` | `StockSharpDiagram`: catalog-aware nodes, ports, validation, events, persistence, history and theming |
232
+ | Models and palette | `src/diagram/{types,catalog,palette}.ts` | Public data model and draggable HTML element palette |
233
+ | Web embed | `src/embed.ts` | Self-contained read-only rendering for web applications |
234
+ | Canvas renderer | `src/canvas-renderer.ts` | Internal drawing, routing, selection, editing, zoom, touch and overview |
235
+
236
+ Applications use `StockSharpDiagram` or the read-only embed. The renderer is
237
+ an implementation detail and is bundled into both entry points, so consumers
238
+ do not need a second runtime or copied source files.
239
+
240
+ ## Repository layout
241
+
242
+ ```text
243
+ src/
244
+ index.ts complete public entry point
245
+ core/
246
+ model.ts canonical versioned document
247
+ document.ts validation and serialization
248
+ state.ts runtime, view and selection state
249
+ history.ts commands, transactions, undo and redo
250
+ action-registry.ts executable context and host actions
251
+ diagram/
252
+ stocksharp-diagram.ts StockSharpDiagram API
253
+ api.ts typed public events and options
254
+ types.ts Node, DiagramNode, Port and Link models
255
+ catalog.ts node and socket-type catalog
256
+ palette.ts draggable HTML palette
257
+ event-emitter.ts
258
+ embed.ts self-contained read-only web renderer
259
+ canvas-renderer.ts internal canvas renderer
260
+ examples/basic.ts full-stack demo source
261
+ demo/ Charts-style GitHub Pages shell
262
+ tests/ core, renderer and public-API integration tests
263
+ ```
264
+
265
+ ## Source-first consumption
266
+
267
+ Applications can let their own esbuild/Vite build compile the TypeScript
268
+ published inside the package:
269
+
270
+ ```json
271
+ {
272
+ "dependencies": {
273
+ "@stocksharp/diagram": "^0.1.0"
274
+ }
275
+ }
276
+ ```
277
+
278
+ ```ts
279
+ import { StockSharpDiagram } from '@stocksharp/diagram/source';
280
+ import { renderAll } from '@stocksharp/diagram/source/embed';
281
+ ```
282
+
283
+ For sibling-repository development, replace the version with
284
+ `"file:../../Diagram"`; the import paths stay identical.
285
+
286
+ Dedicated entry points are available for consumers with narrower needs:
287
+
288
+ - `@stocksharp/diagram/document` - versioned document parser and serializer;
289
+ - `@stocksharp/diagram/state` - runtime/view/selection state helpers;
290
+ - `@stocksharp/diagram/history` - command and transaction history;
291
+ - `@stocksharp/diagram/actions` - typed action registry;
292
+ - `@stocksharp/diagram/embed` — compiled read-only web renderer;
293
+ - `@stocksharp/diagram/catalog`, `@stocksharp/diagram/palette`, `@stocksharp/diagram/types`.
294
+
295
+ ## Build output
296
+
297
+ `npm run build` produces:
298
+
299
+ | File | Purpose |
300
+ | --- | --- |
301
+ | `dist/esm/**` | complete ESM module tree |
302
+ | `dist/types/**` | TypeScript declarations |
303
+ | `dist/ssdiagram.js` | complete browser IIFE exposed as `window.SSDiagram` |
304
+ | `demo/dist/demo.js` | full-stack interactive example (excluded from the package) |
305
+
306
+ ## Commands
307
+
308
+ ```text
309
+ npm ci
310
+ npm test
311
+ npm run build
312
+ npm run serve
313
+ npm run pack:check
314
+ npm run release:check -- v0.1.0
315
+ npm run api:check
316
+ npm run api:update # only after reviewing an intentional public API change
317
+ npm run test:browser
318
+ ```
319
+
320
+ The local demo is served at http://localhost:8792/demo/index.html.
321
+
322
+ The browser suite covers Chromium smoke, interaction and lifecycle scenarios
323
+ at DPR 1 and 2.
324
+
325
+ CI verifies type checking, the reviewed declaration snapshot, unit/integration
326
+ tests, Chromium smoke/interaction/lifecycle checks, all bundles and tarball
327
+ contents. GitHub Pages publishes the demo from `main`.
328
+
329
+ Publishing is release-driven. Set `package.json` to the intended version and
330
+ publish a GitHub Release tagged `v<version>`. The `release.yml` workflow rejects
331
+ a mismatched tag, rebuilds and tests the repository, attaches the exact `.tgz`
332
+ artifact to the release, and publishes that tarball to npm with provenance.
333
+
334
+ The first publication needs a short-lived npm granular access token in the
335
+ repository Actions secret `NPM_TOKEN`. After `@stocksharp/diagram` exists on npm,
336
+ configure tokenless trusted publishing, then remove the secret and revoke the
337
+ bootstrap token:
338
+
339
+ ```sh
340
+ npm trust github @stocksharp/diagram --file release.yml --repo StockSharp/Diagram --allow-publish
341
+ ```
342
+
343
+ ## License
344
+
345
+ Copyright © 2010-present StockSharp Platform LLC and/or its affiliates. All
346
+ rights reserved. Use is governed by the StockSharp EULA and [LICENSE](LICENSE).