@ohmygame/plugin-ui 0.0.0-stage → 2.3.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.
- package/LICENSE +201 -0
- package/README.md +32 -2
- package/index.d.ts +170 -0
- package/index.js +127 -0
- package/package.json +28 -4
- package/plugin-ui.css +158 -0
- package/ui/1/base.js +107 -0
- package/ui/1/controls.js +231 -0
- package/ui/1/custom-elements.json +2472 -0
- package/ui/1/feedback.js +340 -0
- package/ui/1/forms.js +464 -0
- package/ui/1/index.d.ts +518 -0
- package/ui/1/index.js +9 -0
- package/ui/1/layout.js +419 -0
- package/ui/1/media.js +370 -0
- package/ui/1/navigation.js +161 -0
package/plugin-ui.css
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/* OhMyGame plugin page styles, built on the app's design tokens
|
|
2
|
+
(src/renderer/ui/tokens.css). When the page connects, and whenever the
|
|
3
|
+
theme changes, the app sets each token in PAGE_TOKENS
|
|
4
|
+
(src/renderer/plugin-frame.tsx) on :root. Until then these defaults, the
|
|
5
|
+
app's own, keep the page readable. The page's root is transparent, so the
|
|
6
|
+
panel's surface shows through it. */
|
|
7
|
+
:root {
|
|
8
|
+
--gray-0: #141414;
|
|
9
|
+
--gray-1: #181818;
|
|
10
|
+
--gray-2: #1c1c1c;
|
|
11
|
+
--gray-3: #242424;
|
|
12
|
+
--gray-4: #2a2a2a;
|
|
13
|
+
--gray-5: #303030;
|
|
14
|
+
--gray-6: #3b3b3b;
|
|
15
|
+
--gray-8: #737373;
|
|
16
|
+
--gray-9: #a3a3a3;
|
|
17
|
+
--gray-12: #ececec;
|
|
18
|
+
--color-accent: #2563eb;
|
|
19
|
+
--color-on-accent: #ffffff;
|
|
20
|
+
--color-grid-dot: #333333;
|
|
21
|
+
--hue-audio: #7fb79a;
|
|
22
|
+
--hue-code: #7fb0c2;
|
|
23
|
+
--hue-data: #c8ac6a;
|
|
24
|
+
--hue-doc: #8fa3d6;
|
|
25
|
+
--hue-image: #d6a07a;
|
|
26
|
+
--hue-model: #a897d4;
|
|
27
|
+
--hue-video: #cf8aa5;
|
|
28
|
+
--hue-danger: #e57b72;
|
|
29
|
+
--hue-success: #6fb68f;
|
|
30
|
+
--hue-warning: #d6ad66;
|
|
31
|
+
--syntax-comment: var(--color-text-tertiary);
|
|
32
|
+
--syntax-keyword: var(--hue-model);
|
|
33
|
+
--syntax-number: var(--hue-data);
|
|
34
|
+
--syntax-string: var(--hue-audio);
|
|
35
|
+
--color-app: var(--gray-0);
|
|
36
|
+
--color-stage: var(--gray-1);
|
|
37
|
+
--color-surface: var(--gray-2);
|
|
38
|
+
--color-surface-raised: var(--gray-3);
|
|
39
|
+
--color-hover: var(--gray-4);
|
|
40
|
+
--color-selected: var(--gray-5);
|
|
41
|
+
--color-border: var(--gray-5);
|
|
42
|
+
--color-border-strong: var(--gray-6);
|
|
43
|
+
--color-text: var(--gray-12);
|
|
44
|
+
--color-text-secondary: var(--gray-9);
|
|
45
|
+
--color-text-tertiary: var(--gray-8);
|
|
46
|
+
--color-ink: var(--gray-12);
|
|
47
|
+
--color-on-ink: var(--gray-0);
|
|
48
|
+
--color-focus: color-mix(in srgb, var(--gray-12) 70%, transparent);
|
|
49
|
+
--color-control-thumb: var(--gray-12);
|
|
50
|
+
--color-success: var(--hue-success);
|
|
51
|
+
--color-danger: var(--hue-danger);
|
|
52
|
+
--color-warning: var(--hue-warning);
|
|
53
|
+
--shadow-hairline: 0 0 0 1px var(--color-border);
|
|
54
|
+
--shadow-float: 0 12px 32px rgb(0 0 0 / 40%), 0 0 0 1px rgb(255 255 255 / 6%);
|
|
55
|
+
--font-sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
|
|
56
|
+
--font-mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
|
|
57
|
+
--text-xs: 11px;
|
|
58
|
+
--text-sm: 12px;
|
|
59
|
+
--text-md: 13px;
|
|
60
|
+
--text-lg: 15px;
|
|
61
|
+
--text-xl: 20px;
|
|
62
|
+
--weight-regular: 400;
|
|
63
|
+
--weight-medium: 500;
|
|
64
|
+
--weight-semibold: 600;
|
|
65
|
+
--radius-sm: 6px;
|
|
66
|
+
--radius-md: 8px;
|
|
67
|
+
--radius-lg: 12px;
|
|
68
|
+
--radius-pill: 999px;
|
|
69
|
+
--space-1: 4px;
|
|
70
|
+
--space-2: 8px;
|
|
71
|
+
--space-3: 12px;
|
|
72
|
+
--space-4: 16px;
|
|
73
|
+
--space-6: 24px;
|
|
74
|
+
--ease-out: cubic-bezier(0.2, 0.8, 0.2, 1);
|
|
75
|
+
--duration-fast: 120ms;
|
|
76
|
+
--duration-base: 200ms;
|
|
77
|
+
color-scheme: dark;
|
|
78
|
+
font-family: var(--font-sans);
|
|
79
|
+
font-size: var(--text-md);
|
|
80
|
+
line-height: 1.45;
|
|
81
|
+
color: var(--color-text);
|
|
82
|
+
background: transparent;
|
|
83
|
+
}
|
|
84
|
+
:root[data-appearance="light"] {
|
|
85
|
+
--gray-0: #f3f3f3;
|
|
86
|
+
--gray-1: #f8f8f8;
|
|
87
|
+
--gray-2: #ffffff;
|
|
88
|
+
--gray-3: #f6f6f6;
|
|
89
|
+
--gray-4: #efefef;
|
|
90
|
+
--gray-5: #e5e5e5;
|
|
91
|
+
--gray-6: #d4d4d4;
|
|
92
|
+
--gray-8: #8c8c8c;
|
|
93
|
+
--gray-9: #5c5c5c;
|
|
94
|
+
--gray-12: #1a1a1a;
|
|
95
|
+
--color-grid-dot: #dedede;
|
|
96
|
+
--hue-audio: #3f8e68;
|
|
97
|
+
--hue-code: #3f879f;
|
|
98
|
+
--hue-data: #9c7c2c;
|
|
99
|
+
--hue-doc: #5b74b8;
|
|
100
|
+
--hue-image: #c0784a;
|
|
101
|
+
--hue-model: #7a62b8;
|
|
102
|
+
--hue-video: #b8577f;
|
|
103
|
+
--hue-danger: #c4473f;
|
|
104
|
+
--hue-success: #3a8f63;
|
|
105
|
+
--hue-warning: #a8771f;
|
|
106
|
+
--color-focus: color-mix(in srgb, var(--gray-12) 70%, transparent);
|
|
107
|
+
--color-control-thumb: #ffffff;
|
|
108
|
+
--shadow-float: 0 8px 28px rgb(0 0 0 / 9%), 0 0 0 1px rgb(0 0 0 / 7%);
|
|
109
|
+
color-scheme: light;
|
|
110
|
+
}
|
|
111
|
+
* { box-sizing: border-box; }
|
|
112
|
+
body { margin: 0; padding: var(--space-4); }
|
|
113
|
+
:focus-visible { outline: 2px solid var(--color-focus); outline-offset: 2px; }
|
|
114
|
+
|
|
115
|
+
.omg-row { display: flex; align-items: center; gap: var(--space-2); min-height: 32px; }
|
|
116
|
+
.omg-row + .omg-row { border-top: 1px solid var(--color-border); }
|
|
117
|
+
.omg-muted { color: var(--color-text-secondary); }
|
|
118
|
+
|
|
119
|
+
/* Filled, no outline, as the app's buttons: secondary is a step up from the
|
|
120
|
+
surface, primary is ink. */
|
|
121
|
+
a { color: var(--omg-accent, var(--color-accent)); }
|
|
122
|
+
|
|
123
|
+
.omg-button {
|
|
124
|
+
display: inline-flex; align-items: center; justify-content: center; gap: var(--space-1);
|
|
125
|
+
height: 30px; padding: 0 var(--space-3); border: 0; border-radius: var(--radius-md);
|
|
126
|
+
background: var(--color-hover); color: var(--color-text);
|
|
127
|
+
font: inherit; font-weight: var(--weight-medium); white-space: nowrap; cursor: pointer;
|
|
128
|
+
transition: background var(--duration-fast), opacity var(--duration-fast);
|
|
129
|
+
}
|
|
130
|
+
.omg-button:not(:disabled):hover { background: var(--color-selected); }
|
|
131
|
+
.omg-button[data-variant="primary"] { background: var(--color-ink); color: var(--color-on-ink); }
|
|
132
|
+
.omg-button[data-variant="primary"]:not(:disabled):hover { background: var(--color-ink); opacity: 0.88; }
|
|
133
|
+
.omg-button[data-variant="danger"] { color: var(--color-danger); }
|
|
134
|
+
.omg-button:disabled { opacity: 0.4; cursor: default; }
|
|
135
|
+
|
|
136
|
+
/* Fields sit below the surface, outlined by a hairline that firms up on
|
|
137
|
+
hover and while typing. */
|
|
138
|
+
.omg-input, .omg-select {
|
|
139
|
+
height: 32px; padding: 0 var(--space-2); border: 0; border-radius: var(--radius-md); min-width: 0;
|
|
140
|
+
background: var(--color-stage); color: var(--color-text); font: inherit;
|
|
141
|
+
box-shadow: inset 0 0 0 1px var(--color-border);
|
|
142
|
+
transition: box-shadow var(--duration-fast);
|
|
143
|
+
}
|
|
144
|
+
.omg-input:hover, .omg-select:hover { box-shadow: inset 0 0 0 1px var(--color-border-strong); }
|
|
145
|
+
/* The caret shows where typing goes; a select keeps its keyboard ring. */
|
|
146
|
+
.omg-input:focus { outline: none; }
|
|
147
|
+
|
|
148
|
+
.omg-menu {
|
|
149
|
+
list-style: none; margin: 0; padding: var(--space-1); border-radius: var(--radius-lg);
|
|
150
|
+
background: var(--color-surface-raised); box-shadow: var(--shadow-float);
|
|
151
|
+
}
|
|
152
|
+
.omg-menu > li { display: flex; align-items: center; min-height: 30px; padding: 0 var(--space-2); border-radius: var(--radius-sm); cursor: pointer; }
|
|
153
|
+
.omg-menu > li:hover { background: var(--color-hover); }
|
|
154
|
+
.omg-menu > li[aria-selected="true"] { background: var(--color-selected); }
|
|
155
|
+
|
|
156
|
+
@media (prefers-reduced-motion: reduce) {
|
|
157
|
+
* { transition-duration: 1ms !important; animation-duration: 1ms !important; }
|
|
158
|
+
}
|
package/ui/1/base.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// What every OhMyGame page component shares. Components draw in their own
|
|
2
|
+
// Shadow DOM from the app's tokens, which the bridge sets on the page's
|
|
3
|
+
// :root and which reach every shadow tree. A plugin customizes them only
|
|
4
|
+
// through their attributes, CSS custom properties and `::part()`.
|
|
5
|
+
//
|
|
6
|
+
// `--omg-accent` is the plugin's brand colour, the app's accent by default,
|
|
7
|
+
// and `--omg-on-accent` the text that goes on it.
|
|
8
|
+
// Components use it only for the busy spinner, controls that are on, and
|
|
9
|
+
// links; buttons stay ink.
|
|
10
|
+
|
|
11
|
+
/** A stylesheet shared by every instance of a component. */
|
|
12
|
+
export function css(text) {
|
|
13
|
+
const sheet = new CSSStyleSheet();
|
|
14
|
+
sheet.replaceSync(text);
|
|
15
|
+
return sheet;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const BASE = css(`
|
|
19
|
+
:host { box-sizing: border-box; }
|
|
20
|
+
:host([hidden]) { display: none !important; }
|
|
21
|
+
*, *::before, *::after { box-sizing: inherit; }
|
|
22
|
+
[hidden] { display: none !important; }
|
|
23
|
+
/* A paragraph placed in a component is spaced by the component, not by its margins. */
|
|
24
|
+
::slotted(p) { margin: 0; }
|
|
25
|
+
:focus-visible { outline: 2px solid var(--color-focus); outline-offset: 2px; }
|
|
26
|
+
@keyframes omg-spin { to { transform: rotate(360deg); } }
|
|
27
|
+
.spinner {
|
|
28
|
+
width: 12px; height: 12px; flex: none; border-radius: var(--radius-pill);
|
|
29
|
+
border: 2px solid currentColor; border-right-color: transparent;
|
|
30
|
+
animation: omg-spin 0.8s linear infinite;
|
|
31
|
+
}
|
|
32
|
+
@media (prefers-reduced-motion: reduce) {
|
|
33
|
+
*, *::before, *::after { transition-duration: 1ms !important; animation-duration: 1ms !important; }
|
|
34
|
+
}
|
|
35
|
+
`);
|
|
36
|
+
|
|
37
|
+
const warned = new Set();
|
|
38
|
+
|
|
39
|
+
/** Tells a plugin's author once, in the page's console. */
|
|
40
|
+
function warnOnce(message) {
|
|
41
|
+
if (warned.has(message)) return;
|
|
42
|
+
warned.add(message);
|
|
43
|
+
console.warn(`[OhMyGame] ${message}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A component: its template is static HTML, and attributes fill it as text. */
|
|
47
|
+
export class OmgElement extends HTMLElement {
|
|
48
|
+
/** @type {CSSStyleSheet[]} */
|
|
49
|
+
static styles = [];
|
|
50
|
+
static template = "";
|
|
51
|
+
static delegatesFocus = false;
|
|
52
|
+
/** What to use instead, when the component is deprecated; matches its `@deprecated` tag. */
|
|
53
|
+
static deprecated = undefined;
|
|
54
|
+
/** What to use instead of each deprecated attribute; matches its `@deprecated attr` tags. Each must be observed, to warn when a script sets it later. */
|
|
55
|
+
static deprecatedAttributes = {};
|
|
56
|
+
|
|
57
|
+
constructor() {
|
|
58
|
+
super();
|
|
59
|
+
const type = /** @type {typeof OmgElement} */ (this.constructor);
|
|
60
|
+
const root = this.attachShadow({ mode: "open", delegatesFocus: type.delegatesFocus });
|
|
61
|
+
root.adoptedStyleSheets = [BASE, ...type.styles];
|
|
62
|
+
root.innerHTML = type.template;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The element of this component's shadow tree with the given part. */
|
|
66
|
+
part(name) {
|
|
67
|
+
return this.shadowRoot.querySelector(`[part~="${name}"]`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
connectedCallback() {
|
|
71
|
+
const type = /** @type {typeof OmgElement} */ (this.constructor);
|
|
72
|
+
if (type.deprecated) warnOnce(`<${this.localName}> is deprecated: ${type.deprecated}`);
|
|
73
|
+
for (const name of Object.keys(type.deprecatedAttributes)) if (this.hasAttribute(name)) this.#warnAttribute(name);
|
|
74
|
+
this.update();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
attributeChangedCallback(name, _previous, value) {
|
|
78
|
+
if (value !== null && Object.hasOwn(/** @type {typeof OmgElement} */ (this.constructor).deprecatedAttributes, name)) this.#warnAttribute(name);
|
|
79
|
+
if (this.isConnected) this.update();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
#warnAttribute(name) {
|
|
83
|
+
const reason = /** @type {typeof OmgElement} */ (this.constructor).deprecatedAttributes[name];
|
|
84
|
+
warnOnce(`<${this.localName}>'s ${name} is deprecated: ${reason}`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Draws the component from its attributes. */
|
|
88
|
+
update() {}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Sets an element's text, hiding it when there is none. */
|
|
92
|
+
export function text(element, value) {
|
|
93
|
+
element.textContent = value ?? "";
|
|
94
|
+
element.hidden = !value;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Whether a slot has elements or non-blank text assigned. */
|
|
98
|
+
export function filled(slot) {
|
|
99
|
+
return slot
|
|
100
|
+
.assignedNodes({ flatten: true })
|
|
101
|
+
.some((node) => node.nodeType === Node.ELEMENT_NODE || node.textContent.trim());
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Defines a component once, so a page may load the components twice. */
|
|
105
|
+
export function define(name, component) {
|
|
106
|
+
if (!customElements.get(name)) customElements.define(name, component);
|
|
107
|
+
}
|
package/ui/1/controls.js
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
// Controls: buttons, a button bound to one of the plugin's commands, and a
|
|
2
|
+
// select.
|
|
3
|
+
import { connect } from "../../plugin-ui.js";
|
|
4
|
+
import { OmgElement, css, define, text } from "./base.js";
|
|
5
|
+
|
|
6
|
+
// Listens for the app's port from the start: the app sends it once, when the
|
|
7
|
+
// page loads, and a page may use these components without connecting itself.
|
|
8
|
+
const connection = connect();
|
|
9
|
+
|
|
10
|
+
const BUTTON = css(`
|
|
11
|
+
:host { display: inline-flex; vertical-align: middle; max-width: 100%; }
|
|
12
|
+
button {
|
|
13
|
+
display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2); width: 100%;
|
|
14
|
+
height: 30px; padding: 0 var(--space-3); border: 0; border-radius: var(--radius-md);
|
|
15
|
+
background: var(--_button-background, var(--color-hover)); color: var(--color-text);
|
|
16
|
+
font: inherit; font-weight: var(--weight-medium); white-space: nowrap; cursor: pointer;
|
|
17
|
+
transition: background var(--duration-fast), opacity var(--duration-fast);
|
|
18
|
+
}
|
|
19
|
+
button:not(:disabled):hover { background: var(--color-selected); }
|
|
20
|
+
:host([variant="primary"]) button { background: var(--color-ink); color: var(--color-on-ink); }
|
|
21
|
+
:host([variant="primary"]) button:not(:disabled):hover { background: var(--color-ink); opacity: 0.88; }
|
|
22
|
+
:host([variant="ghost"]) button { background: transparent; }
|
|
23
|
+
:host([variant="ghost"]) button:not(:disabled):hover { background: var(--color-hover); }
|
|
24
|
+
:host([variant="danger"]) button { color: var(--color-danger); }
|
|
25
|
+
button:disabled { opacity: 0.4; cursor: default; }
|
|
26
|
+
button[aria-busy="true"] { opacity: 0.7; cursor: progress; }
|
|
27
|
+
`);
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* `<omg-button variant="secondary|primary|ghost|danger" disabled busy>`: the
|
|
31
|
+
* app's button. Primary is ink; there is one per view. A busy button shows a
|
|
32
|
+
* spinner and takes no clicks.
|
|
33
|
+
*
|
|
34
|
+
* @attr {"secondary" | "primary" | "ghost" | "danger"} variant - Secondary by default.
|
|
35
|
+
* @attr {boolean} disabled - Takes no clicks.
|
|
36
|
+
* @attr {boolean} busy - Shows a spinner and takes no clicks.
|
|
37
|
+
* @prop {boolean} disabled - The `disabled` attribute.
|
|
38
|
+
* @prop {boolean} busy - The `busy` attribute.
|
|
39
|
+
* @slot - The label.
|
|
40
|
+
* @part button - The button.
|
|
41
|
+
* @part spinner - The spinner shown while busy.
|
|
42
|
+
*/
|
|
43
|
+
class OmgButton extends OmgElement {
|
|
44
|
+
static observedAttributes = ["variant", "disabled", "busy"];
|
|
45
|
+
static delegatesFocus = true;
|
|
46
|
+
static styles = [BUTTON];
|
|
47
|
+
static template = `<button part="button" type="button"><span class="spinner" part="spinner" hidden></span><slot></slot></button>`;
|
|
48
|
+
|
|
49
|
+
constructor() {
|
|
50
|
+
super();
|
|
51
|
+
// Neither a disabled nor a busy button reports a click.
|
|
52
|
+
this.addEventListener(
|
|
53
|
+
"click",
|
|
54
|
+
(event) => {
|
|
55
|
+
if (this.#inactive()) {
|
|
56
|
+
event.preventDefault();
|
|
57
|
+
event.stopImmediatePropagation();
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
{ capture: true },
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
get disabled() {
|
|
65
|
+
return this.hasAttribute("disabled");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
set disabled(value) {
|
|
69
|
+
this.toggleAttribute("disabled", Boolean(value));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get busy() {
|
|
73
|
+
return this.hasAttribute("busy");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
set busy(value) {
|
|
77
|
+
this.toggleAttribute("busy", Boolean(value));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Whether the button takes no clicks now; a command button adds its own reasons. */
|
|
81
|
+
blocked() {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
#inactive() {
|
|
86
|
+
return this.disabled || this.busy || this.blocked();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
update() {
|
|
90
|
+
const button = this.part("button");
|
|
91
|
+
button.disabled = this.disabled || (!this.busy && this.blocked());
|
|
92
|
+
button.setAttribute("aria-busy", String(this.busy));
|
|
93
|
+
// A busy button keeps its focus, so it stays enabled but inert.
|
|
94
|
+
button.setAttribute("aria-disabled", String(this.#inactive()));
|
|
95
|
+
this.part("spinner").hidden = !this.busy;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Commands running now from command buttons that wait for one another. */
|
|
100
|
+
const running = new Set();
|
|
101
|
+
const changes = new EventTarget();
|
|
102
|
+
const changed = () => changes.dispatchEvent(new Event("change"));
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* `<omg-command-button command input variant concurrent quiet>`: runs one of
|
|
106
|
+
* the plugin's commands, by its manifest id, when clicked. `input` is JSON,
|
|
107
|
+
* or set the `input` property. While it runs, the button is busy and shows
|
|
108
|
+
* the command's progress beside it, and other command buttons wait unless
|
|
109
|
+
* they are `concurrent`. A failure shows beside it unless it is `quiet`,
|
|
110
|
+
* and is its `error` attribute until the next run, so a page that draws the
|
|
111
|
+
* button again can pass it on.
|
|
112
|
+
*
|
|
113
|
+
* Events: `omg-command-start` (cancelable, before it runs),
|
|
114
|
+
* `omg-command-result` with `detail.result`, and `omg-command-error` with
|
|
115
|
+
* `detail.error`, cancelable to show the failure yourself.
|
|
116
|
+
*
|
|
117
|
+
* @attr {string} command - The command's id in the manifest.
|
|
118
|
+
* @attr {string} input - The command's input, as JSON.
|
|
119
|
+
* @attr {boolean} concurrent - Runs while other command buttons run, and doesn't make them wait.
|
|
120
|
+
* @attr {boolean} quiet - Doesn't show a failure beside it.
|
|
121
|
+
* @attr {string} error - The last run's failure, until the next run.
|
|
122
|
+
* @prop {unknown} input - The command's input; it takes the place of the attribute.
|
|
123
|
+
* @method {Promise<unknown>} run - Runs the command, as a click does; resolves to its result, or undefined when it didn't run or failed.
|
|
124
|
+
* @fires {{ command: string; input: unknown }} omg-command-start - Before it runs; cancel it to not run.
|
|
125
|
+
* @fires {{ command: string; result: unknown }} omg-command-result - The command's result.
|
|
126
|
+
* @fires {{ command: string; error: string }} omg-command-error - The command failed; cancel it to show the failure yourself.
|
|
127
|
+
* @part progress - The command's progress while it runs.
|
|
128
|
+
* @part error - The failure.
|
|
129
|
+
*/
|
|
130
|
+
class OmgCommandButton extends OmgButton {
|
|
131
|
+
static observedAttributes = [...OmgButton.observedAttributes, "command", "input", "concurrent", "quiet", "error"];
|
|
132
|
+
static styles = [
|
|
133
|
+
BUTTON,
|
|
134
|
+
css(`
|
|
135
|
+
:host { flex-wrap: wrap; align-items: center; gap: var(--space-1) var(--space-2); }
|
|
136
|
+
button { width: auto; }
|
|
137
|
+
.progress { color: var(--color-text-secondary); }
|
|
138
|
+
.error { color: var(--color-danger); white-space: pre-wrap; overflow-wrap: anywhere; }
|
|
139
|
+
`),
|
|
140
|
+
];
|
|
141
|
+
static template = `${OmgButton.template}<span class="progress" part="progress" role="status" hidden></span><span class="error" part="error" role="alert" hidden></span>`;
|
|
142
|
+
|
|
143
|
+
#input;
|
|
144
|
+
#running = false;
|
|
145
|
+
#stopProgress;
|
|
146
|
+
#onChange = () => this.update();
|
|
147
|
+
|
|
148
|
+
constructor() {
|
|
149
|
+
super();
|
|
150
|
+
// Only the button runs the command, not its progress or error beside it.
|
|
151
|
+
this.part("button").addEventListener("click", () => void this.run());
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
get input() {
|
|
155
|
+
if (this.#input !== undefined) return this.#input;
|
|
156
|
+
const value = this.getAttribute("input");
|
|
157
|
+
return value === null ? undefined : JSON.parse(value);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
set input(value) {
|
|
161
|
+
this.#input = value;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
connectedCallback() {
|
|
165
|
+
changes.addEventListener("change", this.#onChange);
|
|
166
|
+
super.connectedCallback();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
disconnectedCallback() {
|
|
170
|
+
changes.removeEventListener("change", this.#onChange);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
blocked() {
|
|
174
|
+
return !this.hasAttribute("concurrent") && !this.#running && [...running].some((button) => button !== this);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Runs the command, as a click does; resolves to its result, or undefined when it didn't run or failed. */
|
|
178
|
+
async run() {
|
|
179
|
+
const command = this.getAttribute("command");
|
|
180
|
+
if (!command || this.#running || this.disabled || this.busy || this.blocked()) return undefined;
|
|
181
|
+
let input;
|
|
182
|
+
try {
|
|
183
|
+
input = this.input;
|
|
184
|
+
} catch {
|
|
185
|
+
this.#fail(command, new Error(`The input of command button "${command}" isn't JSON`));
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
188
|
+
const start = new CustomEvent("omg-command-start", { bubbles: true, composed: true, cancelable: true, detail: { command, input } });
|
|
189
|
+
if (!this.dispatchEvent(start)) return undefined;
|
|
190
|
+
// Taken before anything waits, so a second click can't start another run.
|
|
191
|
+
this.#running = true;
|
|
192
|
+
if (!this.hasAttribute("concurrent")) running.add(this);
|
|
193
|
+
this.busy = true;
|
|
194
|
+
this.removeAttribute("error");
|
|
195
|
+
changed();
|
|
196
|
+
try {
|
|
197
|
+
const host = await connection;
|
|
198
|
+
this.#stopProgress = host.on("progress", (value) => {
|
|
199
|
+
if (value.command === command) text(this.part("progress"), value.message);
|
|
200
|
+
});
|
|
201
|
+
const result = await host.request("command.run", input === undefined ? { command } : { command, input });
|
|
202
|
+
this.dispatchEvent(new CustomEvent("omg-command-result", { bubbles: true, composed: true, detail: { command, result } }));
|
|
203
|
+
return result;
|
|
204
|
+
} catch (error) {
|
|
205
|
+
this.#fail(command, error instanceof Error ? error : new Error(String(error)));
|
|
206
|
+
return undefined;
|
|
207
|
+
} finally {
|
|
208
|
+
this.#stopProgress?.();
|
|
209
|
+
this.#stopProgress = undefined;
|
|
210
|
+
this.#running = false;
|
|
211
|
+
running.delete(this);
|
|
212
|
+
this.busy = false;
|
|
213
|
+
text(this.part("progress"), "");
|
|
214
|
+
changed();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
update() {
|
|
219
|
+
super.update();
|
|
220
|
+
text(this.part("error"), this.getAttribute("error"));
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Reports a failure of the run of `command`, whatever the attribute says now. */
|
|
224
|
+
#fail(command, error) {
|
|
225
|
+
const event = new CustomEvent("omg-command-error", { bubbles: true, composed: true, cancelable: true, detail: { command, error: error.message } });
|
|
226
|
+
if (this.dispatchEvent(event) && !this.hasAttribute("quiet")) this.setAttribute("error", error.message);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
define("omg-button", OmgButton);
|
|
231
|
+
define("omg-command-button", OmgCommandButton);
|