@kemu-io/hs-react 0.2.36

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 (39) hide show
  1. package/cjs/WidgetWrapper.d.ts +563 -0
  2. package/cjs/WidgetWrapper.js +1 -0
  3. package/cjs/components/ResizableContainer.js +14 -0
  4. package/cjs/components/WidgetContainer.js +12 -0
  5. package/cjs/components/index.js +1 -0
  6. package/cjs/constants.js +1 -0
  7. package/cjs/hooks/index.js +1 -0
  8. package/cjs/hooks/useOnBroadcastEvent.js +1 -0
  9. package/cjs/hooks/useOnParentEvent.js +1 -0
  10. package/cjs/hooks/useOnSetOutputsEvent.js +1 -0
  11. package/cjs/hooks/useReactiveWidgetState.js +1 -0
  12. package/cjs/hooks/useResizableWidgetDimensions.js +1 -0
  13. package/cjs/hooks/useWidgetState.js +1 -0
  14. package/cjs/lib/InstanceContext.js +1 -0
  15. package/cjs/lib/cache.js +1 -0
  16. package/cjs/lib/globalContext.js +1 -0
  17. package/cjs/types/context_t.js +1 -0
  18. package/cjs/types/hooks_t.js +1 -0
  19. package/cjs/types/widgetUI_t.js +1 -0
  20. package/mjs/WidgetWrapper.d.ts +563 -0
  21. package/mjs/WidgetWrapper.js +1 -0
  22. package/mjs/components/ResizableContainer.js +14 -0
  23. package/mjs/components/WidgetContainer.js +12 -0
  24. package/mjs/components/index.js +1 -0
  25. package/mjs/constants.js +1 -0
  26. package/mjs/hooks/index.js +1 -0
  27. package/mjs/hooks/useOnBroadcastEvent.js +1 -0
  28. package/mjs/hooks/useOnParentEvent.js +1 -0
  29. package/mjs/hooks/useOnSetOutputsEvent.js +1 -0
  30. package/mjs/hooks/useReactiveWidgetState.js +1 -0
  31. package/mjs/hooks/useResizableWidgetDimensions.js +1 -0
  32. package/mjs/hooks/useWidgetState.js +1 -0
  33. package/mjs/lib/InstanceContext.js +1 -0
  34. package/mjs/lib/cache.js +1 -0
  35. package/mjs/lib/globalContext.js +1 -0
  36. package/mjs/types/context_t.js +1 -0
  37. package/mjs/types/hooks_t.js +1 -0
  38. package/mjs/types/widgetUI_t.js +1 -0
  39. package/package.json +45 -0
@@ -0,0 +1,563 @@
1
+ // Generated by dts-bundle-generator v9.5.1
2
+
3
+ import React$1 from 'react';
4
+
5
+ export type Rect = {
6
+ width: number;
7
+ height: number;
8
+ top: number;
9
+ left: number;
10
+ };
11
+ export type Point = {
12
+ x: number;
13
+ y: number;
14
+ };
15
+ export type BinaryFile<D extends ArrayBuffer | Uint8Array | Uint8ClampedArray | Buffer = ArrayBuffer> = {
16
+ format: string;
17
+ data: D;
18
+ };
19
+ declare enum DataType {
20
+ Number = 0,
21
+ String = 1,
22
+ ArrayBuffer = 2,
23
+ Array = 3,
24
+ Boolean = 4,
25
+ /** a javascript object */
26
+ JsonObj = 5,
27
+ /**
28
+ * Does not care; mostly used by actions to indicate they accept anything since they probably won't use the
29
+ * value.
30
+ **/
31
+ Anything = 6,
32
+ /** Raw image data, produced in browser environments */
33
+ ImageData = 7,
34
+ /** Raw audio data fraction */
35
+ AudioBuffer = 8,
36
+ Rect = 9,
37
+ Point = 10,
38
+ ImageBitmap = 11,
39
+ /**
40
+ * Custom binary data. Use the `format` property to specify the type of data, i.e. 'image/png', 'audio/mp3', etc.
41
+ **/
42
+ BinaryFile = 12
43
+ }
44
+ export type JsonType = {
45
+ [key: string]: SupportedTypes;
46
+ };
47
+ export type KemuType = number | string | ArrayBuffer | JsonType | boolean | ImageData | AudioBuffer | Rect | Point | ImageBitmap | BinaryFile;
48
+ export type SupportedTypes = KemuType | KemuType[];
49
+ /** Type passed around between gates and blocks */
50
+ export type Data = {
51
+ /** the type of data represented */
52
+ type: DataType;
53
+ value: SupportedTypes;
54
+ /** original time when the event was first picked up by an input gate. */
55
+ timestamp: number;
56
+ };
57
+ /**
58
+ * Describe the shape of objects. For example:
59
+ *
60
+ * Given the following object:
61
+ * ```
62
+ * const obj = {
63
+ * property1: 12,
64
+ * property2: 'Hello'
65
+ * };
66
+ * ```
67
+ *
68
+ * The shape definition would be:
69
+ * ```
70
+ * {
71
+ * 'property1': DataType.Number,
72
+ * 'property2': DataType.String,
73
+ * }
74
+ * ```
75
+ *
76
+ * Use a JsonPrimitiveFormat (`$$primitive_{x}`) to specify the type of an array of primitive objects,
77
+ * where 'x' is the index in the array the value of the property belongs to. For example:
78
+ *
79
+ * An event with `[1, 'hi', {other: 'yes}]` would produce the following jsonShape:
80
+ *
81
+ * ```
82
+ * {
83
+ * '$$primitive_0': DataType.Number,
84
+ * '$$primitive_1': DataType.String,
85
+ * '$$primitive_2': DataType.JsonObj,
86
+ * }
87
+ * ```
88
+ *
89
+ * If no primitive index is specified, the system assumes all the items in the array
90
+ * will have the given primitive type. For example:
91
+ *
92
+ * An array of `[1, 2, 3, 4]` should be described as:
93
+ * ```
94
+ * {
95
+ * '$$primitive': DataType.Number
96
+ * }
97
+ * ```
98
+ */
99
+ export type JsonTypeShape = {
100
+ [key: string]: DataType;
101
+ };
102
+ /**
103
+ * Used by Widget (formerly known as Gates) modules when defining their
104
+ * input/output port list
105
+ */
106
+ export type WidgetPort = {
107
+ /** a unique identifier (in the context of the widget) for this port. */
108
+ name: string;
109
+ /** alternative label used to replace the name for presentation purposes only */
110
+ label?: string;
111
+ /** Type of data the port accepts or produces, an array indicates the gate can receive multiple types */
112
+ type: DataType | DataType[];
113
+ /**
114
+ * If true, processors won't be invoked until at least an event is received in this port.
115
+ */
116
+ required?: boolean;
117
+ /**
118
+ * Describes the shape of a JSON object when dataType == JsonObj. Or the shape of items
119
+ * when dataType == Array
120
+ */
121
+ jsonShape?: JsonTypeShape;
122
+ /** help description markdown */
123
+ description?: string;
124
+ };
125
+ export type WidgetState<T extends Record<string, any> = Record<string, unknown>> = T;
126
+ export type TargetOutput = {
127
+ /**
128
+ * the name of the port the `data` will be sent from.
129
+ * This name MUST match one of the ports
130
+ * returned by the `getOutputs` function or defined in the widget
131
+ * manifest. If the name does not match, the data will not be sent.
132
+ **/
133
+ name: string;
134
+ /**
135
+ * The data to send to the output port.
136
+ * If the value is `undefined` or `null`, no data will be sent.
137
+ * Also, the data type MUST match the type defined in the widget manifest.
138
+ */
139
+ value: SupportedTypes | undefined | null;
140
+ /**
141
+ * the data type of the value being sent.
142
+ * This value is automatically inferred from the widget manifest.
143
+ **/
144
+ type: DataType;
145
+ /** id of the variant that defined the output */
146
+ variantId?: string;
147
+ };
148
+ export type WidgetPortStr = Omit<WidgetPort, "type"> & {
149
+ type: string;
150
+ };
151
+ declare enum ProcessorType {
152
+ Javascript = "js",
153
+ Python = "py",
154
+ Executable = "exe"
155
+ }
156
+ export type ServiceSecret = {
157
+ /** a markdown description of the secret */
158
+ description: string;
159
+ /** if true, the secret is optional and the service will still start if the secret is not provided */
160
+ optional?: boolean;
161
+ };
162
+ export type ServiceWidgetVariant<P extends WidgetPort | WidgetPortStr> = {
163
+ /** unique identifier for the variant */
164
+ id: string;
165
+ name: string;
166
+ description: string;
167
+ /**
168
+ * custom color for the variant. If not provided, the main
169
+ * color defined in the manifest will be used.
170
+ **/
171
+ color?: string;
172
+ /** SVG icon data */
173
+ svgIcon?: string;
174
+ inputs?: P[];
175
+ outputs?: P[];
176
+ };
177
+ export type ServiceManifest<P extends WidgetPort | WidgetPortStr> = {
178
+ /** the unique name defined by the service */
179
+ name: string;
180
+ /** a unique identifier given during startup */
181
+ version: string;
182
+ /** a service title for UI purposes */
183
+ title?: string;
184
+ /** a shorten title to be used in the canvas */
185
+ shortTitle?: string;
186
+ description: string;
187
+ processor: ProcessorType;
188
+ /**
189
+ * if true, it indicates other services can subscribe
190
+ * to events emitted by this service. Only services
191
+ * with `eventEmitter` set to true can use `broadcast`
192
+ * to emit events.
193
+ */
194
+ eventEmitter?: boolean;
195
+ /**
196
+ * Specifies the default timeout in seconds when a parentEvent is received.
197
+ * This is used to discard events that take too long to process.
198
+ * Set to 0 to disable the timeout. Warning this may
199
+ * cause some parent widgets to hang indefinitely if the child
200
+ * widget does not respond to the event.
201
+ **/
202
+ processingTimeoutSec?: number;
203
+ /**
204
+ * if true, parent events sent to this widget will
205
+ * be resolved immediately, meaning the parent widget
206
+ * will not have to wait for this service to respond
207
+ * before continuing execution.
208
+ */
209
+ asyncParentEvents?: boolean;
210
+ /**
211
+ * if true, it indicates the service is only meant to be used in a
212
+ * web environment. The widget won't work when exported.
213
+ */
214
+ webOnly?: boolean;
215
+ /**
216
+ * if set the service will only receive env vars that start with this prefix.
217
+ * Use it to isolate the information services have access to.
218
+ * Eg:
219
+ * - `KEMU_{CUSTOM_PREFIX}_DB_URL`: Your service will receive this env var as `DB_URL`
220
+ *
221
+ * Remember that only variables that start with `KEMU_` are forwarded to services.
222
+ */
223
+ envVarPrefix?: string;
224
+ color?: string;
225
+ svgIcon?: string;
226
+ /**
227
+ * Flags this service as internal. Internal services are not
228
+ * meant to be used by the end-user. Attempts to use an internal service
229
+ * will likely result in an error.
230
+ */
231
+ internal?: boolean;
232
+ /**
233
+ * if true, the service is expected to have a `widgetUI.js` file in the root directory
234
+ * that will be used to mount a new UI onto the canvas.
235
+ **/
236
+ widgetUI?: boolean;
237
+ /**
238
+ * If true, the processor won't be forwarded any parent events.
239
+ * Use it when you have a widget with custom UI that performs all processing
240
+ * inside of its own component. Keep in mind that when exported, any processing
241
+ * logic that does not exist in the processor will not be available in the exported
242
+ * recipe.
243
+ */
244
+ ignoreParentEvents?: boolean;
245
+ /**
246
+ * A list of relative paths to `txt` files where custom widget clipboard data are stored. Eg:
247
+ * ```json
248
+ * {
249
+ * "customWidgets": [
250
+ * "customWidgets/widget1.txt",
251
+ * "customWidgets/widget2.txt",
252
+ * ]
253
+ * }
254
+ * ```
255
+ * This will cause the service to be rendered as a folder with the custom widgets as sub-widgets.
256
+ */
257
+ customWidgets?: string[];
258
+ /**
259
+ * A list of widget variants that can be selected by the user.
260
+ * All variants will invoke the same processor, but they can have different input/output ports
261
+ * and be visually distinct.
262
+ */
263
+ variants?: ServiceWidgetVariant<P>[];
264
+ /**
265
+ * A list of relative paths to folders containing extra services that are part of this service. Eg:
266
+ * ```json
267
+ * {
268
+ * "subServices": [
269
+ * "subServices/subService1",
270
+ * "subServices/subService2",
271
+ * ]
272
+ * }
273
+ * ```
274
+ *
275
+ * Each folder should contain all the required files that represent a service (manifest, processor, etc).
276
+ */
277
+ subServices?: string[];
278
+ /** the list of input ports. If not provided,
279
+ * `dynamicInputs` MUST be set to true, or loading the
280
+ * widget will fail.
281
+ */
282
+ inputs: P[];
283
+ /**
284
+ * the list of output ports. If not provided,
285
+ * `dynamicOutputs` MUST be set to true, or loading the
286
+ * widget will fail.
287
+ */
288
+ outputs: P[];
289
+ /**
290
+ * A map of secret names to their descriptions
291
+ */
292
+ requiredSecrets?: {
293
+ [name: string]: ServiceSecret;
294
+ };
295
+ };
296
+ export type ServiceWidgetInfo = {
297
+ name: string;
298
+ icon?: string;
299
+ color?: string;
300
+ description: string;
301
+ contents: string;
302
+ /**
303
+ * The id of the first root group in the widget.
304
+ * Keep in mind this id will be re-generated when the widget is added to the canvas.
305
+ */
306
+ rootGroupId: string;
307
+ protocolVersion: string;
308
+ };
309
+ /**
310
+ * This is the output of the service library after processing its own manifest file.
311
+ */
312
+ export type PartiallyProcessedManifest<B extends Buffer | ArrayBuffer = Buffer, SM extends WidgetPort | WidgetPortStr = WidgetPortStr> = ServiceManifest<SM> & {
313
+ widgetUIContents?: B;
314
+ uiContentChecksum?: string;
315
+ /** path to the root directory where the service resides */
316
+ path: string;
317
+ /** added by the service automatically when the service is running in dev mode */
318
+ readonly devMode?: boolean;
319
+ };
320
+ /**
321
+ * This is the output of the Hub after combining a PartiallyProcessedManifest and processing variants, sub-services and custom widgets.
322
+ */
323
+ export type ProcessedManifest<B extends Buffer | ArrayBuffer = Buffer> = Omit<PartiallyProcessedManifest<B, WidgetPort>, "customWidgets"> & {
324
+ customWidgets?: ServiceWidgetInfo[];
325
+ /** if present, it indicates this service is a sub-service of another service */
326
+ parentService?: {
327
+ name: string;
328
+ version: string;
329
+ };
330
+ };
331
+ export type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
332
+ [K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
333
+ }[Keys];
334
+ export type DefineDynamicPortsArgs = RequireAtLeastOne<{
335
+ /**
336
+ * Set to null to remove any previously defined ports or
337
+ * set to an empty array to remove ports from the widget.
338
+ */
339
+ outputs?: WidgetPort[] | null;
340
+ /**
341
+ * Set to null to remove any previously defined ports or
342
+ * set to an empty array to remove ports from the widget.
343
+ */
344
+ inputs?: WidgetPort[] | null;
345
+ }, "outputs" | "inputs"> & {
346
+ /** the id of the variant this definition applies to */
347
+ variantId?: string;
348
+ };
349
+ export type UseWidgetStateResponse<T extends WidgetState = WidgetState> = {
350
+ /**
351
+ * the current state of the widget at the time the hook was created.
352
+ * This state is not reactive and will not trigger a re-render when the state changes.
353
+ **/
354
+ state: Readonly<T>;
355
+ /**
356
+ * @returns the current state of the widget. This function is memoized and will always return the latest state.
357
+ */
358
+ getState: () => Readonly<T>;
359
+ /**
360
+ * Updates the state of the widget.
361
+ * @param newState the new state to set.
362
+ * @param triggerEvents if true, the widget will notify listeners that its state has changed. Default is true.
363
+ */
364
+ setState: (newState: T, triggerEvents?: boolean) => void;
365
+ };
366
+ /**
367
+ * @returns the current state and a memoized function to get the current state.
368
+ */
369
+ export type UseWidgetStateHook<T extends WidgetState = WidgetState> = () => UseWidgetStateResponse<T>;
370
+ export type DeepReadOnly<T> = T extends (infer R)[] ? ReadonlyArray<DeepReadOnly<R>> : T extends object ? {
371
+ readonly [K in keyof T]: DeepReadOnly<T[K]>;
372
+ } : T;
373
+ export type DeepReadOnlyArray<T> = ReadonlyArray<DeepReadOnly<T>>;
374
+ export type ServiceParentEvent = {
375
+ /** information about the widget producing */
376
+ sourceWidget: {
377
+ id: string;
378
+ port: string;
379
+ };
380
+ targetPort: string;
381
+ data: Data;
382
+ };
383
+ export type BroadcastEvent = {
384
+ outputs: TargetOutput[];
385
+ source: {
386
+ serviceName: string;
387
+ serviceVersion: string;
388
+ sessionId: number;
389
+ };
390
+ };
391
+ export type ServiceParentEventHandler = (event: ServiceParentEvent) => void | Promise<void>;
392
+ export type ServiceBroadcastEventHandler = (event: DeepReadOnly<BroadcastEvent>) => void | Promise<void>;
393
+ export type ServiceSetOutputsEventHandler = (outputs: DeepReadOnlyArray<TargetOutput>) => void | Promise<void>;
394
+ export type WidgetRenderArgs = {
395
+ /** indicates if the Hub service is currently online */
396
+ serviceOnline: boolean;
397
+ /** indicates if this Widget is currently disabled */
398
+ disabled: boolean;
399
+ };
400
+ export type WidgetUIInstance = {
401
+ /** unmounts the component */
402
+ destroy: () => void;
403
+ /**
404
+ * re-renders the component by triggering
405
+ * an internal state change.
406
+ **/
407
+ render: (a?: WidgetRenderArgs) => void;
408
+ handleParentEvent: ServiceParentEventHandler | null;
409
+ handleBroadcastEvent: ServiceBroadcastEventHandler | null;
410
+ handleSetOutputsEvent: ServiceSetOutputsEventHandler | null;
411
+ };
412
+ export type LimitedManifest = Omit<ProcessedManifest, "widgetUIContents" | "settingsUIContents" | "path">;
413
+ export type ReactWidgetUtils = {
414
+ /**
415
+ * Shows a native dialog to choose a file.
416
+ * @returns a promise that resolves to the paths of the chosen files.
417
+ **/
418
+ showChooseFileDialog: (args: {
419
+ title?: string;
420
+ fileTypes?: string[];
421
+ allowMultiple?: boolean;
422
+ }) => Promise<string[]>;
423
+ /**
424
+ * Shows a native dialog to choose a directory.
425
+ * @returns a promise that resolves to the path of the chosen directory.
426
+ */
427
+ showChooseDirectoryDialog: (args: {
428
+ title?: string;
429
+ }) => Promise<string>;
430
+ browser: {
431
+ /**
432
+ * @returns the Cache Storage path for the given file
433
+ **/
434
+ getCacheFilePath: (fileName: string) => string;
435
+ /**
436
+ * Adds the given data to the service cache
437
+ * @param filePath the full path of the file, including the file name and extension
438
+ * @param data the raw data to store
439
+ * @param headers optional response headers
440
+ * @returns
441
+ */
442
+ cacheFile: (filePath: string, data: string | ArrayBuffer | Uint8Array | Uint8ClampedArray | Blob, headers?: Headers) => Promise<void>;
443
+ /**
444
+ * Attempts to load the file from the cache storage
445
+ * @param filePath full path of the file, including the file name and extension
446
+ * @returns the cached file or undefined if the file is not found
447
+ */
448
+ getCachedFile: (filePath: string) => Promise<Response | undefined>;
449
+ /**
450
+ * Removes the file from the cache storage. If path does not exist, nothing happens.
451
+ * @param filePath full path of the file, including the file name and extension
452
+ */
453
+ removeCachedFile: (filePath: string) => Promise<void>;
454
+ /**
455
+ * Removes all entries linked to this service
456
+ */
457
+ clearServiceCache: () => Promise<void>;
458
+ };
459
+ };
460
+ export type CustomWidgetProps = {
461
+ /** the id of this widget in the recipe */
462
+ readonly widgetId: string;
463
+ /** the id of the recipe */
464
+ readonly recipeId: string;
465
+ /** the widget variant that was added to the workspace */
466
+ readonly variantId?: string;
467
+ /** the manifest file of the running service */
468
+ manifest: DeepReadOnly<LimitedManifest>;
469
+ serviceOnline: boolean;
470
+ disabled: boolean;
471
+ /**
472
+ * Redraws the ports and their connections.
473
+ * Useful when the size of your widget changes dynamically.
474
+ **/
475
+ repaintPorts: () => void;
476
+ /**
477
+ * Sets the value of the outputs of the widget.
478
+ * IMPORTANT: Keep in mind that port changes triggered from UI components won't work
479
+ * when the recipe is exported as a standalone service.
480
+ **/
481
+ setOutputs: GlobalContext["setOutputs"];
482
+ /**
483
+ * Invokes an `onUIEvent` in the processor file.
484
+ * @param name the name of the handler to be called.
485
+ * @returns a promise that resolves to the return value of the processor handler.
486
+ **/
487
+ callProcessorHandler: <R = any>(name: string, data?: any) => Promise<R>;
488
+ /**
489
+ * Allows the UI instance to change the default
490
+ * ports definition of a widget service based on custom logic. The interface
491
+ * will repaint the ports for the given widget accordingly.
492
+ *
493
+ * Keep in mind that calling this method while attached to other widgets will
494
+ * automatically remove the links with any parent or child widget.
495
+ **/
496
+ defineDynamicPorts: (config: DefineDynamicPortsArgs) => Promise<void>;
497
+ /**
498
+ * Utility functions
499
+ */
500
+ utils: ReactWidgetUtils;
501
+ };
502
+ export type WidgetCanvasDimensions = {
503
+ width: number;
504
+ height: number;
505
+ };
506
+ export type Output = Omit<TargetOutput, "value"> & {
507
+ value: SupportedTypes;
508
+ };
509
+ export type GlobalContext<T extends WidgetState = WidgetState> = {
510
+ useWidgetState: UseWidgetStateHook<T>;
511
+ setOutputs: (outputs: Output[]) => Promise<void>;
512
+ defineDynamicPorts: (config: DefineDynamicPortsArgs) => Promise<void>;
513
+ callProcessorHandler: <R = any>(name: string, data?: any) => Promise<R>;
514
+ setWidgetDimensions: (width: number, height: number) => void;
515
+ getWidgetDimensions: () => WidgetCanvasDimensions;
516
+ utils: ReactWidgetUtils;
517
+ widgetId: string;
518
+ recipeId: string;
519
+ variantId?: string;
520
+ manifest: LimitedManifest;
521
+ };
522
+ declare const PREVENT_DRAGGING_CLS = "no-dragging";
523
+ declare const ABORT_CHILD_DRAGGING_CLS = "no-child-drag";
524
+ declare const WIDGET_SELECTED_CLS = "widget-selected";
525
+ export type WidgetWrapper = {
526
+ repaintPorts: () => void;
527
+ onDestroy?: () => void;
528
+ globalContext: GlobalContext;
529
+ };
530
+ /**
531
+ * Wraps your custom widget component with the necessary context to interact with the Hub Service.
532
+ * @example
533
+ *
534
+ * ```tsx
535
+ * import { CustomWidgetProps, createWidgetUI } from '@kemu-io/hs-react';
536
+ * import packageJson from '../package.json';
537
+ *
538
+ * const WidgetUI = (props: CustomWidgetProps) => {
539
+ *
540
+ * return (
541
+ * <WidgetContainer>
542
+ * <button>Click Me</button>
543
+ * </WidgetContainer>
544
+ * );
545
+ * };
546
+ *
547
+ * export default createWidgetUI(WidgetUI, packageJson.name, packageJson.version);
548
+ *
549
+ * ```
550
+ */
551
+ export declare const createWidgetUI: (userWidget: (props: CustomWidgetProps) => React$1.ReactElement, serviceName: string, serviceVersion: string) => {
552
+ mountComponent: (containerEl: HTMLElement, props: WidgetWrapper) => Partial<WidgetUIInstance>;
553
+ };
554
+
555
+ declare namespace constants {
556
+ export { ABORT_CHILD_DRAGGING_CLS, PREVENT_DRAGGING_CLS, WIDGET_SELECTED_CLS };
557
+ }
558
+
559
+ export {
560
+ constants,
561
+ };
562
+
563
+ export {};
@@ -0,0 +1 @@
1
+ import{jsx as _jsx}from"react/jsx-runtime";import{useCallback,useEffect,useState,useMemo,useRef}from"react";import{createRoot}from"react-dom/client";import{CacheProvider}from"@emotion/react";import createCache from"./lib/cache.js";import{setContext}from"./lib/globalContext.js";import WidgetInstanceContextProvider from"./lib/InstanceContext.js";import*as constants from"./constants.js";export{constants};const KemuService=e=>{const{repaintPorts:t,onDestroy:n,globalContext:r,getParentContext:a}=e,[s,o]=useState(null),[c,i]=useState({disabled:!1,serviceOnline:!0}),l=useRef(null),d=useRef(null),u=useRef(null),m=useCallback((e=>{l.current=e}),[]),v=useCallback((e=>{d.current=e}),[]),p=useCallback((e=>{u.current=e}),[]);useEffect((()=>(e.container&&o((t=>t||createCache(e.serviceName,e.serviceVersion,e.container))),()=>{n&&n()})),[e.container]),useEffect((()=>{const e=a();e.handleSetOutputsEvent=async e=>{u.current&&await u.current(e)},e.handleBroadcastEvent=async e=>{if(d.current)return d.current(e)},e.handleParentEvent=async e=>!!l.current&&(await l.current(e),!0),e.handleRender=e=>{i((t=>({...t,...e})))}}),[]);const f=useMemo((()=>({setOutputs:r.setOutputs,callProcessorHandler:r.callProcessorHandler,repaintPorts:t,defineDynamicPorts:r.defineDynamicPorts,utils:r.utils,widgetId:r.widgetId,recipeId:r.recipeId,manifest:r.manifest,disabled:c.disabled,variantId:r.variantId,serviceOnline:c.serviceOnline})),[r,t,c.disabled,c.serviceOnline]),h=useMemo((()=>({useOnParentEvent:m,useOnBroadcastEvent:v,useOnSetOutputsEvent:p,setWidgetDimensions:r.setWidgetDimensions,getWidgetDimensions:r.getWidgetDimensions,getWidgetProps:()=>f})),[m,v,p,f]);if(useEffect((()=>{s&&(console.log("Cache updated, repainting ports"),t&&t())}),[s]),!s||!e.container)return null;const g=e.children;return _jsx(WidgetInstanceContextProvider,{value:h,children:_jsx(CacheProvider,{value:s,children:_jsx(g,{...f})})})};export const createWidgetUI=(e,t,n)=>({mountComponent:(r,a)=>{const s=createRoot(r);if(!a.globalContext)throw new Error("`globalContext` not provided");setContext(a.globalContext);const o={};s.render(_jsx(KemuService,{...a,serviceName:t.replace(/\./g,"-").replace(/[0-9]/g,"").toLowerCase(),serviceVersion:n,container:r,getParentContext:()=>o,children:e}));const c={render:e=>{o.handleRender&&o.handleRender(e)},destroy:()=>{s.unmount()},handleParentEvent:async e=>{let t=!1;o.handleParentEvent&&(t=await o.handleParentEvent(e)),!1===t&&(c.handleParentEvent=null)},handleBroadcastEvent:async e=>{o.handleBroadcastEvent&&await o.handleBroadcastEvent(e)},handleSetOutputsEvent:async e=>{o.handleSetOutputsEvent&&await o.handleSetOutputsEvent(e)}};return c}});
@@ -0,0 +1,14 @@
1
+ import{jsx as _jsx}from"react/jsx-runtime";import{useCallback,useContext}from"react";import{Resizable}from"re-resizable";import styled from"@emotion/styled";import{PREVENT_DRAGGING_CLS}from"../constants.js";import{WidgetInstanceContext}from"../lib/InstanceContext.js";import{WidgetContainer}from"./index.js";const ResizeHandle=styled.div`
2
+ position: absolute;
3
+ width: 20px;
4
+ height: 20px;
5
+ background-repeat: no-repeat;
6
+ background-origin: content-box;
7
+ box-sizing: border-box;
8
+ background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA2IDYiIHN0eWxlPSJiYWNrZ3JvdW5kLWNvbG9yOiNmZmZmZmYwMCIgeD0iMHB4IiB5PSIwcHgiIHdpZHRoPSI2cHgiIGhlaWdodD0iNnB4Ij48ZyBvcGFjaXR5PSIwLjMwMiI+PHBhdGggZD0iTSA2IDYgTCAwIDYgTCAwIDQuMiBMIDQgNC4yIEwgNC4yIDQuMiBMIDQuMiAwIEwgNiAwIEwgNiA2IEwgNiA2IFoiIGZpbGw9IiMwMDAwMDAiLz48L2c+PC9zdmc+);
9
+ background-position: bottom right;
10
+ padding: 0 3px 3px 0;
11
+ bottom: 0;
12
+ right: 0;
13
+ cursor: se-resize;
14
+ `,getDimensions=t=>({width:t.clientWidth,height:t.clientHeight}),ResizableContainer=t=>{const{children:e,lockAspectRatio:i,maxHeight:o,maxWidth:n,minHeight:s,minWidth:a,defaultWidth:g,defaultHeight:d,className:m,style:h,...r}=t,c=useContext(WidgetInstanceContext),l=c.getWidgetProps(),x=useCallback(((e,i,o)=>{if(l.repaintPorts(),t.onResize){const i=getDimensions(o);t.onResize(i.width,i.height,e)}}),[]),I=useCallback(((t,e,i)=>{const o=getDimensions(i);c.setWidgetDimensions(o.width,o.height)}),[]),b=c.getWidgetDimensions(),p=Math.max(a||0,64),C=Math.max(s||0,64),w=Math.max(b.width||g||0,p),u=Math.max(b.height||d||0,C);return _jsx(Resizable,{lockAspectRatio:i,minWidth:p,minHeight:C,maxWidth:n,maxHeight:o,defaultSize:{width:w,height:u},onResizeStop:I,onResize:x,enable:{bottomRight:!0,top:!1,right:!1,bottom:!1,bottomLeft:!1,left:!1,topLeft:!1,topRight:!1},handleComponent:{bottomRight:_jsx(ResizeHandle,{className:PREVENT_DRAGGING_CLS})},children:_jsx(WidgetContainer,{width:"100%",height:"100%",style:h,className:m,...r,children:e})})};export default ResizableContainer;
@@ -0,0 +1,12 @@
1
+ import{jsx as _jsx}from"react/jsx-runtime";import styled from"@emotion/styled";import{WIDGET_SELECTED_CLS}from"../constants.js";import{useContext}from"react";import{WidgetInstanceContext}from"../lib/InstanceContext.js";const WidgetContainer=styled.div`
2
+ width: ${({cWidth:e})=>"number"==typeof e?`${e}px`:e||"64px"};
3
+ height: ${({cHeight:e})=>"number"==typeof e?`${e}px`:e||"64px"};
4
+ background-color: ${({manifest:e,disabled:t,serviceOnline:i})=>t||!i?"#bbb":e.color};
5
+ ${({noBorderRadius:e})=>e?"":"border-radius: 8px;"}
6
+ ${({noPadding:e})=>e?"":"padding: 6px;"}
7
+
8
+ /* Add a shadow when the widget is selected */
9
+ .${WIDGET_SELECTED_CLS} & {
10
+ box-shadow: 0px 0px 7px 1px ${({manifest:e,disabled:t,serviceOnline:i})=>t||!i?"#bbb":e.color};
11
+ }
12
+ `,Wrap=e=>{const t=useContext(WidgetInstanceContext).getWidgetProps(),{className:i,width:o,height:n,...r}=e;return _jsx(WidgetContainer,{cWidth:o,cHeight:n,...r,...t,className:`k-hs-wrap ${i||""}`,children:e.children})};export default Wrap;
@@ -0,0 +1 @@
1
+ import WidgetContainer from"./WidgetContainer.js";import ResizableContainer from"./ResizableContainer.js";export{WidgetContainer,ResizableContainer};
@@ -0,0 +1 @@
1
+ export const PREVENT_DRAGGING_CLS="no-dragging";export const ABORT_CHILD_DRAGGING_CLS="no-child-drag";export const WIDGET_SELECTED_CLS="widget-selected";
@@ -0,0 +1 @@
1
+ import useOnBroadcastEvent from"./useOnBroadcastEvent.js";import useOnParentEvent from"./useOnParentEvent.js";import useOnSetOutputsEvent from"./useOnSetOutputsEvent.js";import useWidgetState from"./useWidgetState.js";import useReactiveWidgetState from"./useReactiveWidgetState.js";import useResizableWidgetDimensions from"./useResizableWidgetDimensions.js";export{useOnBroadcastEvent,useOnParentEvent,useOnSetOutputsEvent,useWidgetState,useReactiveWidgetState,useResizableWidgetDimensions};
@@ -0,0 +1 @@
1
+ import{useContext}from"react";import{WidgetInstanceContext}from"../lib/InstanceContext.js";const useOnBroadcastEvent=t=>{useContext(WidgetInstanceContext).useOnBroadcastEvent(t)};export default useOnBroadcastEvent;
@@ -0,0 +1 @@
1
+ import{useContext}from"react";import{WidgetInstanceContext}from"../lib/InstanceContext.js";const useOnParentEvent=t=>{useContext(WidgetInstanceContext).useOnParentEvent(t)};export default useOnParentEvent;
@@ -0,0 +1 @@
1
+ import{useContext}from"react";import{WidgetInstanceContext}from"../lib/InstanceContext.js";const useOnSetOutputsEvent=t=>{useContext(WidgetInstanceContext).useOnSetOutputsEvent(t)};export default useOnSetOutputsEvent;
@@ -0,0 +1 @@
1
+ import{useCallback}from"react";import{getContext}from"../lib/globalContext.js";const useReactiveWidgetState=(t,e)=>{const a=getContext(),{state:o,getState:i,setState:n}=a.useWidgetState();return[{...t,...o},useCallback((async(a,o=!0)=>{let r=a;const s={...t,...i()};if("function"==typeof a&&(r=await a(s)),e){if(!e(s,r))return}return n(r,o)}),[i,t])]};export default useReactiveWidgetState;
@@ -0,0 +1 @@
1
+ import{getContext}from"../lib/globalContext.js";const useResizableWidgetDimensions=e=>{const t=getContext().getWidgetDimensions();return{width:t.width||e?.defaultWidth||0,height:t.height||e?.defaultHeight||0}};export default useResizableWidgetDimensions;
@@ -0,0 +1 @@
1
+ import{getContext}from"../lib/globalContext.js";const useWidgetState=()=>getContext().useWidgetState();export default useWidgetState;
@@ -0,0 +1 @@
1
+ import{createContext}from"react";export const WidgetInstanceContext=createContext({useOnBroadcastEvent:()=>{},useOnParentEvent:()=>{},useOnSetOutputsEvent:()=>{},setWidgetDimensions:()=>{},getWidgetDimensions:()=>({width:0,height:0}),getWidgetProps:()=>({})});const WidgetInstanceContextProvider=WidgetInstanceContext.Provider;export default WidgetInstanceContextProvider;
@@ -0,0 +1 @@
1
+ import createCache from"@emotion/cache";const create=(e,t,r)=>{const a=`${e}-${t.split(".").map((e=>e.split("").map((e=>{const t=parseInt(e);return isNaN(t)?e:String.fromCharCode(t+97)})).join(""))).join("").toLocaleString()}`;return createCache({key:a,container:r})};export default create;
@@ -0,0 +1 @@
1
+ let context;const setContext=t=>{context=t},getContext=()=>{if(!context)throw new Error("Context not set");return context};export{setContext,getContext};
@@ -0,0 +1 @@
1
+ export{};
@@ -0,0 +1 @@
1
+ export{};
@@ -0,0 +1 @@
1
+ export{};