@devicechain/widget-sdk 0.0.0-stage → 0.20.0-rc.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +83 -2
- package/dist/chart/chart-options.d.ts +14 -0
- package/dist/chart/chart-theme.d.ts +8 -0
- package/dist/chart/echart.d.ts +8 -0
- package/dist/chunks/chunk-CC2V7LVI.js +67 -0
- package/dist/chunks/chunk-CC2V7LVI.js.map +7 -0
- package/dist/context.d.ts +29 -0
- package/dist/define-widget.d.ts +22 -0
- package/dist/echarts.d.ts +3 -0
- package/dist/echarts.js +193 -0
- package/dist/echarts.js.map +7 -0
- package/dist/flash.d.ts +13 -0
- package/dist/format.d.ts +3 -0
- package/dist/frame.d.ts +8 -0
- package/dist/hooks.d.ts +31 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +546 -0
- package/dist/index.js.map +7 -0
- package/dist/registry.d.ts +12 -0
- package/dist/theme.d.ts +2 -0
- package/dist/widget-options.d.ts +8 -0
- package/dist/widget.d.ts +31 -0
- package/package.json +74 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright The DeviceChain Authors
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,84 @@
|
|
|
1
|
-
|
|
1
|
+
<!--
|
|
2
|
+
Copyright The DeviceChain Authors
|
|
3
|
+
SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
-->
|
|
2
5
|
|
|
3
|
-
|
|
6
|
+
# @devicechain/widget-sdk
|
|
7
|
+
|
|
8
|
+
The typed contract for writing a DeviceChain dashboard widget as a React component, and the
|
|
9
|
+
sealed registry that makes it available to a host application. The built-in widgets are
|
|
10
|
+
declared through exactly this interface.
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install @devicechain/widget-sdk @devicechain/dashboards react
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
`@devicechain/dashboards` and `react` are **peer dependencies**: the SDK defines the React
|
|
17
|
+
contexts a widget reads (selection, candidate lookup, time zone, theme root), and a context is
|
|
18
|
+
identified by the object that created it, so every widget package and the host must resolve
|
|
19
|
+
the same single copy of this package. Declare it as a peer in your own widget package too,
|
|
20
|
+
never as a dependency.
|
|
21
|
+
|
|
22
|
+
## What is in the box
|
|
23
|
+
|
|
24
|
+
The root entry is chart-free and map-free: the card frame (`WidgetFrame`), theme-variable
|
|
25
|
+
references (`css`), formatters, the change-flash cue, option readers (`optString`,
|
|
26
|
+
`optNumber`, ...) and the React hooks that bind a hub channel to widget state. A widget
|
|
27
|
+
that only draws DOM imports from here and pulls in neither Apache ECharts nor MapLibre nor
|
|
28
|
+
the built-in widgets.
|
|
29
|
+
|
|
30
|
+
Chart helpers live on a separate entry, `@devicechain/widget-sdk/echarts`: the `EChart`
|
|
31
|
+
wrapper, `buildLineOption` / `buildGaugeOption` and the chart colour theme. `echarts` is an
|
|
32
|
+
**optional peer dependency**, needed only by a widget that imports that entry.
|
|
33
|
+
|
|
34
|
+
## Declare a widget
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
import { defineWidget } from '@devicechain/widget-sdk';
|
|
38
|
+
|
|
39
|
+
export const tankLevelWidget = defineWidget({
|
|
40
|
+
descriptor: {
|
|
41
|
+
id: 'acme/tank-level',
|
|
42
|
+
version: '1.0.0',
|
|
43
|
+
sdkApiVersion: 1,
|
|
44
|
+
optionsVersion: 1,
|
|
45
|
+
display: { title: 'Tank level', category: 'Telemetry' },
|
|
46
|
+
channel: 'measurement',
|
|
47
|
+
datasource: { mode: 'required', selectors: ['device', 'slot'] },
|
|
48
|
+
binding: { window: { option: 'window', min: 1 } },
|
|
49
|
+
options: {
|
|
50
|
+
unit: { kind: 'string', doc: 'Unit shown beside the level.' },
|
|
51
|
+
window: { kind: 'number', integer: true, min: 1, doc: 'Samples retained.' },
|
|
52
|
+
},
|
|
53
|
+
defaults: { unit: '%' },
|
|
54
|
+
interactions: {},
|
|
55
|
+
},
|
|
56
|
+
implementation: {
|
|
57
|
+
kind: 'trusted-react',
|
|
58
|
+
// `data` is typed from the channel: a measurement widget receives measurement data.
|
|
59
|
+
component: ({ data }) => <span>{Object.values(data.latest)[0]?.value}</span>,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
The descriptor is plain JSON-serializable data: no component, callback or URL. A descriptor
|
|
65
|
+
that disagrees with its channel, or whose defaults or bindings disagree with its option
|
|
66
|
+
schema, fails type checking, and `createWidgetRegistry` checks the same rules again at
|
|
67
|
+
runtime.
|
|
68
|
+
|
|
69
|
+
## Register it
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { createWidgetRegistry } from '@devicechain/widget-sdk';
|
|
73
|
+
|
|
74
|
+
const registry = createWidgetRegistry([tankLevelWidget]);
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
A registry is validated and sealed when it is created: duplicate ids, malformed descriptors,
|
|
78
|
+
an unsupported `sdkApiVersion` and invalid bindings or defaults are all reported together in
|
|
79
|
+
one `WidgetRegistryError`. There is no global registration; two registries on one page are
|
|
80
|
+
independent. Extension ids are namespaced (`scope/name`); the un-namespaced ids are reserved
|
|
81
|
+
for the built-in widgets.
|
|
82
|
+
|
|
83
|
+
Trusted widget code runs with the host page's privileges. The declarations in a descriptor
|
|
84
|
+
shape the API; they are not a security boundary.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { MeasurementSample } from '@devicechain/dashboards';
|
|
2
|
+
import type { ElementSize } from '../hooks';
|
|
3
|
+
import type { ChartTheme } from './chart-theme';
|
|
4
|
+
import type { EChartOption } from './echart';
|
|
5
|
+
export interface LineChartOptions {
|
|
6
|
+
measurements?: string[];
|
|
7
|
+
}
|
|
8
|
+
export declare function buildLineOption(samples: MeasurementSample[], theme: ChartTheme, options?: LineChartOptions, size?: ElementSize): EChartOption;
|
|
9
|
+
export interface GaugeChartOptions {
|
|
10
|
+
min?: number;
|
|
11
|
+
max?: number;
|
|
12
|
+
unit?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function buildGaugeOption(value: number | null, theme: ChartTheme, options?: GaugeChartOptions, size?: ElementSize): EChartOption;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as echarts from 'echarts/core';
|
|
2
|
+
export type EChartOption = echarts.EChartsCoreOption;
|
|
3
|
+
export interface EChartProps {
|
|
4
|
+
option: EChartOption;
|
|
5
|
+
className?: string;
|
|
6
|
+
style?: React.CSSProperties;
|
|
7
|
+
}
|
|
8
|
+
export declare function EChart({ option, className, style }: EChartProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// src/context.tsx
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
var WidgetSubjectContext = createContext(void 0);
|
|
5
|
+
function useWidgetSubject() {
|
|
6
|
+
return useContext(WidgetSubjectContext);
|
|
7
|
+
}
|
|
8
|
+
function WidgetSubjectProvider({
|
|
9
|
+
label,
|
|
10
|
+
children
|
|
11
|
+
}) {
|
|
12
|
+
return /* @__PURE__ */ jsx(WidgetSubjectContext.Provider, { value: label, children });
|
|
13
|
+
}
|
|
14
|
+
var WidgetTimeZoneContext = createContext(void 0);
|
|
15
|
+
function WidgetTimeZoneProvider({
|
|
16
|
+
timeZone,
|
|
17
|
+
children
|
|
18
|
+
}) {
|
|
19
|
+
return /* @__PURE__ */ jsx(WidgetTimeZoneContext.Provider, { value: timeZone, children });
|
|
20
|
+
}
|
|
21
|
+
function useWidgetTimeZone() {
|
|
22
|
+
return useContext(WidgetTimeZoneContext);
|
|
23
|
+
}
|
|
24
|
+
var WidgetThemeRootContext = createContext(void 0);
|
|
25
|
+
function WidgetThemeRootProvider({
|
|
26
|
+
root,
|
|
27
|
+
children
|
|
28
|
+
}) {
|
|
29
|
+
return /* @__PURE__ */ jsx(WidgetThemeRootContext.Provider, { value: root, children });
|
|
30
|
+
}
|
|
31
|
+
function useWidgetThemeRoot() {
|
|
32
|
+
return useContext(WidgetThemeRootContext);
|
|
33
|
+
}
|
|
34
|
+
var WidgetSelectContext = createContext(void 0);
|
|
35
|
+
function WidgetSelectProvider({
|
|
36
|
+
select,
|
|
37
|
+
children
|
|
38
|
+
}) {
|
|
39
|
+
return /* @__PURE__ */ jsx(WidgetSelectContext.Provider, { value: select, children });
|
|
40
|
+
}
|
|
41
|
+
function useWidgetSelect() {
|
|
42
|
+
return useContext(WidgetSelectContext);
|
|
43
|
+
}
|
|
44
|
+
var WidgetCandidatesContext = createContext(void 0);
|
|
45
|
+
function WidgetCandidatesProvider({
|
|
46
|
+
candidates,
|
|
47
|
+
children
|
|
48
|
+
}) {
|
|
49
|
+
return /* @__PURE__ */ jsx(WidgetCandidatesContext.Provider, { value: candidates, children });
|
|
50
|
+
}
|
|
51
|
+
function useWidgetCandidates() {
|
|
52
|
+
return useContext(WidgetCandidatesContext);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export {
|
|
56
|
+
useWidgetSubject,
|
|
57
|
+
WidgetSubjectProvider,
|
|
58
|
+
WidgetTimeZoneProvider,
|
|
59
|
+
useWidgetTimeZone,
|
|
60
|
+
WidgetThemeRootProvider,
|
|
61
|
+
useWidgetThemeRoot,
|
|
62
|
+
WidgetSelectProvider,
|
|
63
|
+
useWidgetSelect,
|
|
64
|
+
WidgetCandidatesProvider,
|
|
65
|
+
useWidgetCandidates
|
|
66
|
+
};
|
|
67
|
+
//# sourceMappingURL=chunk-CC2V7LVI.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/context.tsx"],
|
|
4
|
+
"sourcesContent": ["// Copyright The DeviceChain Authors\n// SPDX-License-Identifier: Apache-2.0\n\n// The ambient host context a widget reads: the resolved subject, the display time zone, the\n// theme root, the selection callback and the candidate provider.\n//\n// \uD83D\uDD34 THESE CONTEXTS ARE DEFINED HERE AND NOWHERE ELSE. A React context is identified by the\n// OBJECT createContext returned, so two copies of this module \u2014 one bundled into a widget\n// package, one in the host \u2014 are two unrelated contexts: the host's provider writes to one\n// and the widget reads the other, and the selector silently does nothing. Exactly one\n// definition, in this package, which `@devicechain/widgets` and every third-party widget\n// package depend on as a PEER (never a dependency) so the host's install supplies the\n// single copy.\n\nimport { createContext, useContext, type ReactNode } from 'react';\n\nimport type { SelectionCandidate, SelectionTarget } from '@devicechain/dashboards';\n\n// WidgetSubjectContext carries the resolved entity a widget shows (its datasource's\n// bound device/anchor), computed once by the renderer and provided per widget. The\n// frame reads it as a subtitle so every board answers \"which asset is this?\" \u2014 and,\n// because it reads the RESOLVED binding, it updates for free once selection re-points a\n// slot (ADR-039 selection-overlay amendment). Kept as ambient context, not a widget\n// prop, so the pure widget contract (widget, data) stays untouched and no widget needs\n// to know how a subject is resolved.\nconst WidgetSubjectContext = createContext<string | undefined>(undefined);\n\n// useWidgetSubject reads the resolved subject line (the frame's fallback subtitle).\nexport function useWidgetSubject(): string | undefined {\n return useContext(WidgetSubjectContext);\n}\n\nexport function WidgetSubjectProvider({\n label,\n children,\n}: {\n label: string | undefined;\n children: ReactNode;\n}) {\n return <WidgetSubjectContext.Provider value={label}>{children}</WidgetSubjectContext.Provider>;\n}\n\n// WidgetTimeZone is the zone widgets print instants in. Undefined (the default) means the\n// viewer's own zone, which is what every live host wants; a host replaying a recording\n// pins one so the board's times agree with the recording's own clock.\nconst WidgetTimeZoneContext = createContext<string | undefined>(undefined);\n\nexport function WidgetTimeZoneProvider({\n timeZone,\n children,\n}: {\n timeZone: string | undefined;\n children: ReactNode;\n}) {\n return <WidgetTimeZoneContext.Provider value={timeZone}>{children}</WidgetTimeZoneContext.Provider>;\n}\n\nexport function useWidgetTimeZone(): string | undefined {\n return useContext(WidgetTimeZoneContext);\n}\n\n// WidgetThemeRoot is the element chart colors are read from. Undefined (the default) means\n// the document root, which is where a console or dashboard app sets its tokens. A host that\n// scopes its tokens to a container (an embed on a page whose own stylesheet owns the root)\n// provides that container, and charts resolve their colors there instead.\nconst WidgetThemeRootContext = createContext<Element | undefined>(undefined);\n\nexport function WidgetThemeRootProvider({\n root,\n children,\n}: {\n root: Element | undefined;\n children: ReactNode;\n}) {\n return <WidgetThemeRootContext.Provider value={root}>{children}</WidgetThemeRootContext.Provider>;\n}\n\nexport function useWidgetThemeRoot(): Element | undefined {\n return useContext(WidgetThemeRootContext);\n}\n\n// WidgetSelect is a view-interaction callback (ADR-039 selection amendment): a widget\n// drives a slot selection (an alarm-table originator drill, a context-selector pick) by\n// naming its target slot + the entity to bind. Kept DISTINCT from WidgetActions (the\n// write seam) \u2014 selection changes only the view's binding overlay, never the backend.\n// Provided by the renderer from the host; absent (undefined) in edit mode and any host\n// that wires no selection, so a widget feature-detects it before offering a drill.\nexport type WidgetSelect = (target: SelectionTarget) => void;\n\nconst WidgetSelectContext = createContext<WidgetSelect | undefined>(undefined);\n\nexport function WidgetSelectProvider({\n select,\n children,\n}: {\n select: WidgetSelect | undefined;\n children: ReactNode;\n}) {\n return <WidgetSelectContext.Provider value={select}>{children}</WidgetSelectContext.Provider>;\n}\n\n// useWidgetSelect returns the ambient selection callback, or undefined where selection is\n// not wired (edit mode, a read-only embedder). A widget renders its drill affordance only\n// when this is defined.\nexport function useWidgetSelect(): WidgetSelect | undefined {\n return useContext(WidgetSelectContext);\n}\n\n// WidgetCandidates is the ambient companion to WidgetSelect (ADR-039 selection amendment): a\n// context/entity-selector widget asks it for the options a target slot may bind to. The host\n// computes candidates from the definition's scope + resolved bindings (useSlotCandidates), so\n// the widget stays presentational and the pure widget contract stays untouched. Distinct from\n// the hub's data channels \u2014 candidates are a fetch-on-open list, not a live stream. Undefined\n// where not wired (edit/preview, a host that supplies no selection) \u2192 the selector is inert.\nexport type WidgetCandidates = (slot: string) => Promise<SelectionCandidate[]>;\n\nconst WidgetCandidatesContext = createContext<WidgetCandidates | undefined>(undefined);\n\nexport function WidgetCandidatesProvider({\n candidates,\n children,\n}: {\n candidates: WidgetCandidates | undefined;\n children: ReactNode;\n}) {\n return <WidgetCandidatesContext.Provider value={candidates}>{children}</WidgetCandidatesContext.Provider>;\n}\n\n// useWidgetCandidates returns the ambient candidate provider, or undefined where selection is\n// not wired. A selector widget renders its picker only when both this and useWidgetSelect are\n// defined.\nexport function useWidgetCandidates(): WidgetCandidates | undefined {\n return useContext(WidgetCandidatesContext);\n}\n"],
|
|
5
|
+
"mappings": ";AAcA,SAAS,eAAe,kBAAkC;AAyBjD;AAdT,IAAM,uBAAuB,cAAkC,MAAS;AAGjE,SAAS,mBAAuC;AACrD,SAAO,WAAW,oBAAoB;AACxC;AAEO,SAAS,sBAAsB;AAAA,EACpC;AAAA,EACA;AACF,GAGG;AACD,SAAO,oBAAC,qBAAqB,UAArB,EAA8B,OAAO,OAAQ,UAAS;AAChE;AAKA,IAAM,wBAAwB,cAAkC,MAAS;AAElE,SAAS,uBAAuB;AAAA,EACrC;AAAA,EACA;AACF,GAGG;AACD,SAAO,oBAAC,sBAAsB,UAAtB,EAA+B,OAAO,UAAW,UAAS;AACpE;AAEO,SAAS,oBAAwC;AACtD,SAAO,WAAW,qBAAqB;AACzC;AAMA,IAAM,yBAAyB,cAAmC,MAAS;AAEpE,SAAS,wBAAwB;AAAA,EACtC;AAAA,EACA;AACF,GAGG;AACD,SAAO,oBAAC,uBAAuB,UAAvB,EAAgC,OAAO,MAAO,UAAS;AACjE;AAEO,SAAS,qBAA0C;AACxD,SAAO,WAAW,sBAAsB;AAC1C;AAUA,IAAM,sBAAsB,cAAwC,MAAS;AAEtE,SAAS,qBAAqB;AAAA,EACnC;AAAA,EACA;AACF,GAGG;AACD,SAAO,oBAAC,oBAAoB,UAApB,EAA6B,OAAO,QAAS,UAAS;AAChE;AAKO,SAAS,kBAA4C;AAC1D,SAAO,WAAW,mBAAmB;AACvC;AAUA,IAAM,0BAA0B,cAA4C,MAAS;AAE9E,SAAS,yBAAyB;AAAA,EACvC;AAAA,EACA;AACF,GAGG;AACD,SAAO,oBAAC,wBAAwB,UAAxB,EAAiC,OAAO,YAAa,UAAS;AACxE;AAKO,SAAS,sBAAoD;AAClE,SAAO,WAAW,uBAAuB;AAC3C;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import type { SelectionCandidate, SelectionTarget } from '@devicechain/dashboards';
|
|
3
|
+
export declare function useWidgetSubject(): string | undefined;
|
|
4
|
+
export declare function WidgetSubjectProvider({ label, children, }: {
|
|
5
|
+
label: string | undefined;
|
|
6
|
+
children: ReactNode;
|
|
7
|
+
}): import("react").JSX.Element;
|
|
8
|
+
export declare function WidgetTimeZoneProvider({ timeZone, children, }: {
|
|
9
|
+
timeZone: string | undefined;
|
|
10
|
+
children: ReactNode;
|
|
11
|
+
}): import("react").JSX.Element;
|
|
12
|
+
export declare function useWidgetTimeZone(): string | undefined;
|
|
13
|
+
export declare function WidgetThemeRootProvider({ root, children, }: {
|
|
14
|
+
root: Element | undefined;
|
|
15
|
+
children: ReactNode;
|
|
16
|
+
}): import("react").JSX.Element;
|
|
17
|
+
export declare function useWidgetThemeRoot(): Element | undefined;
|
|
18
|
+
export type WidgetSelect = (target: SelectionTarget) => void;
|
|
19
|
+
export declare function WidgetSelectProvider({ select, children, }: {
|
|
20
|
+
select: WidgetSelect | undefined;
|
|
21
|
+
children: ReactNode;
|
|
22
|
+
}): import("react").JSX.Element;
|
|
23
|
+
export declare function useWidgetSelect(): WidgetSelect | undefined;
|
|
24
|
+
export type WidgetCandidates = (slot: string) => Promise<SelectionCandidate[]>;
|
|
25
|
+
export declare function WidgetCandidatesProvider({ candidates, children, }: {
|
|
26
|
+
candidates: WidgetCandidates | undefined;
|
|
27
|
+
children: ReactNode;
|
|
28
|
+
}): import("react").JSX.Element;
|
|
29
|
+
export declare function useWidgetCandidates(): WidgetCandidates | undefined;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ChannelSnapshot, OptionsOf, WidgetChannel, WidgetDescriptor, WidgetOptionSpecs } from '@devicechain/dashboards';
|
|
2
|
+
import type { ComponentType } from 'react';
|
|
3
|
+
import type { WidgetEditorProps, WidgetProps } from './widget';
|
|
4
|
+
export interface WidgetImplementation<C extends WidgetChannel, S extends WidgetOptionSpecs> {
|
|
5
|
+
kind: 'trusted-react';
|
|
6
|
+
component: ComponentType<WidgetProps<ChannelSnapshot<C>>>;
|
|
7
|
+
editor?: ComponentType<WidgetEditorProps<OptionsOf<S>>>;
|
|
8
|
+
}
|
|
9
|
+
export interface WidgetDefinition<C extends WidgetChannel = WidgetChannel, S extends WidgetOptionSpecs = WidgetOptionSpecs> {
|
|
10
|
+
descriptor: WidgetDescriptor<C, S>;
|
|
11
|
+
implementation: WidgetImplementation<C, S>;
|
|
12
|
+
}
|
|
13
|
+
export type ErasedComponent = ComponentType<any>;
|
|
14
|
+
export interface ErasedWidgetDefinition {
|
|
15
|
+
descriptor: WidgetDescriptor;
|
|
16
|
+
implementation: {
|
|
17
|
+
kind: 'trusted-react';
|
|
18
|
+
component: ErasedComponent;
|
|
19
|
+
editor?: ErasedComponent;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export declare function defineWidget<const C extends WidgetChannel, const S extends WidgetOptionSpecs>(definition: WidgetDefinition<C, S>): WidgetDefinition<C, S>;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { EChart, type EChartOption, type EChartProps } from './chart/echart';
|
|
2
|
+
export { buildGaugeOption, buildLineOption, type GaugeChartOptions, type LineChartOptions, } from './chart/chart-options';
|
|
3
|
+
export { resolveChartTheme, useChartTheme, type ChartTheme } from './chart/chart-theme';
|
package/dist/echarts.js
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useWidgetThemeRoot
|
|
3
|
+
} from "./chunks/chunk-CC2V7LVI.js";
|
|
4
|
+
|
|
5
|
+
// src/chart/echart.tsx
|
|
6
|
+
import { GaugeChart, LineChart } from "echarts/charts";
|
|
7
|
+
import { GridComponent, TooltipComponent } from "echarts/components";
|
|
8
|
+
import * as echarts from "echarts/core";
|
|
9
|
+
import { CanvasRenderer } from "echarts/renderers";
|
|
10
|
+
import { useEffect, useRef } from "react";
|
|
11
|
+
import { jsx } from "react/jsx-runtime";
|
|
12
|
+
echarts.use([LineChart, GaugeChart, GridComponent, TooltipComponent, CanvasRenderer]);
|
|
13
|
+
function EChart({ option, className, style }) {
|
|
14
|
+
const containerRef = useRef(null);
|
|
15
|
+
const chartRef = useRef(null);
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
const el = containerRef.current;
|
|
18
|
+
if (!el) return;
|
|
19
|
+
const chart = echarts.init(el);
|
|
20
|
+
chartRef.current = chart;
|
|
21
|
+
const observer = new ResizeObserver(() => chart.resize());
|
|
22
|
+
observer.observe(el);
|
|
23
|
+
return () => {
|
|
24
|
+
observer.disconnect();
|
|
25
|
+
chart.dispose();
|
|
26
|
+
chartRef.current = null;
|
|
27
|
+
};
|
|
28
|
+
}, []);
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
chartRef.current?.setOption(option, { notMerge: true });
|
|
31
|
+
}, [option]);
|
|
32
|
+
return /* @__PURE__ */ jsx("div", { ref: containerRef, className, style: { width: "100%", height: "100%", ...style } });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/chart/chart-options.ts
|
|
36
|
+
function distinctNames(samples) {
|
|
37
|
+
const seen = [];
|
|
38
|
+
for (const s of samples) {
|
|
39
|
+
if (!seen.includes(s.name)) seen.push(s.name);
|
|
40
|
+
}
|
|
41
|
+
return seen;
|
|
42
|
+
}
|
|
43
|
+
var clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, v));
|
|
44
|
+
function shortSide(size) {
|
|
45
|
+
const s = Math.min(size?.width ?? 0, size?.height ?? 0);
|
|
46
|
+
return s > 0 ? s : 200;
|
|
47
|
+
}
|
|
48
|
+
function buildLineOption(samples, theme, options = {}, size) {
|
|
49
|
+
const names = options.measurements?.length ? options.measurements : distinctNames(samples);
|
|
50
|
+
const axisFont = clamp(Math.round(shortSide(size) / 22), 8, 12);
|
|
51
|
+
const series = names.map((name, i) => {
|
|
52
|
+
const color = theme.series[i % theme.series.length];
|
|
53
|
+
return {
|
|
54
|
+
name,
|
|
55
|
+
type: "line",
|
|
56
|
+
showSymbol: false,
|
|
57
|
+
lineStyle: { color },
|
|
58
|
+
itemStyle: { color },
|
|
59
|
+
data: samples.filter((s) => s.name === name && s.value != null && s.occurredTime != null).map((s) => [s.occurredTime, s.value])
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
return {
|
|
63
|
+
animation: false,
|
|
64
|
+
textStyle: { color: theme.foreground, fontSize: axisFont },
|
|
65
|
+
grid: { left: 8, right: 12, top: names.length > 1 ? 28 : 12, bottom: 8, containLabel: true },
|
|
66
|
+
tooltip: { trigger: "axis" },
|
|
67
|
+
legend: names.length > 1 ? { textStyle: { color: theme.mutedForeground, fontSize: axisFont }, top: 0 } : void 0,
|
|
68
|
+
xAxis: {
|
|
69
|
+
type: "time",
|
|
70
|
+
axisLine: { lineStyle: { color: theme.border } },
|
|
71
|
+
axisLabel: { color: theme.mutedForeground, fontSize: axisFont, hideOverlap: true }
|
|
72
|
+
},
|
|
73
|
+
yAxis: {
|
|
74
|
+
type: "value",
|
|
75
|
+
splitLine: { lineStyle: { color: theme.border } },
|
|
76
|
+
axisLabel: { color: theme.mutedForeground, fontSize: axisFont }
|
|
77
|
+
},
|
|
78
|
+
series
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function buildGaugeOption(value, theme, options = {}, size) {
|
|
82
|
+
const min = options.min ?? 0;
|
|
83
|
+
const max = options.max ?? 100;
|
|
84
|
+
const accent = theme.series[0];
|
|
85
|
+
const unitSuffix = options.unit ? ` ${options.unit}` : "";
|
|
86
|
+
const s = shortSide(size);
|
|
87
|
+
const arcWidth = clamp(Math.round(s / 26), 4, 12);
|
|
88
|
+
const labelFont = clamp(Math.round(s / 20), 8, 13);
|
|
89
|
+
const valueFont = clamp(Math.round(s / 7), 14, 40);
|
|
90
|
+
return {
|
|
91
|
+
animation: false,
|
|
92
|
+
series: [
|
|
93
|
+
{
|
|
94
|
+
type: "gauge",
|
|
95
|
+
min,
|
|
96
|
+
max,
|
|
97
|
+
splitNumber: s < 160 ? 4 : 5,
|
|
98
|
+
// fewer ticks than the default 10 → no cram
|
|
99
|
+
radius: "95%",
|
|
100
|
+
center: ["50%", "58%"],
|
|
101
|
+
progress: { show: true, width: arcWidth, itemStyle: { color: accent } },
|
|
102
|
+
pointer: { itemStyle: { color: accent }, length: "55%", width: clamp(Math.round(s / 50), 2, 6) },
|
|
103
|
+
anchor: { show: false },
|
|
104
|
+
axisLine: { lineStyle: { width: arcWidth, color: [[1, theme.border]] } },
|
|
105
|
+
axisTick: { show: false },
|
|
106
|
+
splitLine: { length: arcWidth, lineStyle: { color: theme.border, width: 1 } },
|
|
107
|
+
axisLabel: { color: theme.mutedForeground, fontSize: labelFont, distance: arcWidth + 2 },
|
|
108
|
+
detail: {
|
|
109
|
+
valueAnimation: false,
|
|
110
|
+
color: theme.foreground,
|
|
111
|
+
fontSize: valueFont,
|
|
112
|
+
offsetCenter: [0, "30%"],
|
|
113
|
+
formatter: (v) => value == null ? "\u2014" : `${v}${unitSuffix}`
|
|
114
|
+
},
|
|
115
|
+
data: [{ value: value ?? min }]
|
|
116
|
+
}
|
|
117
|
+
]
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// src/chart/chart-theme.ts
|
|
122
|
+
import { useMemo, useSyncExternalStore } from "react";
|
|
123
|
+
var FALLBACK = {
|
|
124
|
+
foreground: "hsl(240 10% 3.9%)",
|
|
125
|
+
mutedForeground: "hsl(240 3.8% 46.1%)",
|
|
126
|
+
border: "hsl(240 5.9% 90%)",
|
|
127
|
+
series: [
|
|
128
|
+
"hsl(220 70% 50%)",
|
|
129
|
+
"hsl(160 60% 45%)",
|
|
130
|
+
"hsl(30 80% 55%)",
|
|
131
|
+
"hsl(280 65% 60%)",
|
|
132
|
+
"hsl(340 75% 55%)"
|
|
133
|
+
]
|
|
134
|
+
};
|
|
135
|
+
function readVar(style, name, fallback) {
|
|
136
|
+
const raw = style.getPropertyValue(`--${name}`).trim();
|
|
137
|
+
return raw ? `hsl(${raw})` : fallback;
|
|
138
|
+
}
|
|
139
|
+
function resolveChartTheme(el) {
|
|
140
|
+
const style = getComputedStyle(el);
|
|
141
|
+
return {
|
|
142
|
+
foreground: readVar(style, "foreground", FALLBACK.foreground),
|
|
143
|
+
mutedForeground: readVar(style, "muted-foreground", FALLBACK.mutedForeground),
|
|
144
|
+
border: readVar(style, "border", FALLBACK.border),
|
|
145
|
+
series: FALLBACK.series.map((fallback, i) => readVar(style, `chart-${i + 1}`, fallback))
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
var sharedTheme = null;
|
|
149
|
+
var themeListeners = /* @__PURE__ */ new Set();
|
|
150
|
+
var themeObserver = null;
|
|
151
|
+
function recomputeTheme() {
|
|
152
|
+
sharedTheme = resolveChartTheme(document.documentElement);
|
|
153
|
+
}
|
|
154
|
+
function subscribeTheme(listener) {
|
|
155
|
+
if (!themeObserver) {
|
|
156
|
+
recomputeTheme();
|
|
157
|
+
themeObserver = new MutationObserver(() => {
|
|
158
|
+
recomputeTheme();
|
|
159
|
+
for (const l of themeListeners) l();
|
|
160
|
+
});
|
|
161
|
+
themeObserver.observe(document.documentElement, {
|
|
162
|
+
attributes: true,
|
|
163
|
+
attributeFilter: ["class", "style"]
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
themeListeners.add(listener);
|
|
167
|
+
return () => {
|
|
168
|
+
themeListeners.delete(listener);
|
|
169
|
+
if (themeListeners.size === 0 && themeObserver) {
|
|
170
|
+
themeObserver.disconnect();
|
|
171
|
+
themeObserver = null;
|
|
172
|
+
sharedTheme = null;
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
function getThemeSnapshot() {
|
|
177
|
+
if (!sharedTheme) recomputeTheme();
|
|
178
|
+
return sharedTheme;
|
|
179
|
+
}
|
|
180
|
+
function useChartTheme() {
|
|
181
|
+
const shared = useSyncExternalStore(subscribeTheme, getThemeSnapshot, getThemeSnapshot);
|
|
182
|
+
const root = useWidgetThemeRoot();
|
|
183
|
+
const scoped = useMemo(() => root ? resolveChartTheme(root) : null, [root]);
|
|
184
|
+
return scoped ?? shared;
|
|
185
|
+
}
|
|
186
|
+
export {
|
|
187
|
+
EChart,
|
|
188
|
+
buildGaugeOption,
|
|
189
|
+
buildLineOption,
|
|
190
|
+
resolveChartTheme,
|
|
191
|
+
useChartTheme
|
|
192
|
+
};
|
|
193
|
+
//# sourceMappingURL=echarts.js.map
|