dsh-workbench-danmaku 0.3.3 → 0.4.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/lib/client.js CHANGED
@@ -1542,12 +1542,248 @@ var require_layer = __commonJS({
1542
1542
  }
1543
1543
  });
1544
1544
 
1545
+ // packages/addons/lib/config-client.cjs
1546
+ var require_config_client = __commonJS({
1547
+ "packages/addons/lib/config-client.cjs"(exports2, module2) {
1548
+ "use strict";
1549
+ var React = require("react");
1550
+ var BASE = "/workbench/api";
1551
+ var HOST_DOMAIN = "wb-shell";
1552
+ var DEFAULT_TTL = 3e4;
1553
+ var cache = /* @__PURE__ */ new Map();
1554
+ var inflight = /* @__PURE__ */ new Map();
1555
+ var listeners = /* @__PURE__ */ new Map();
1556
+ var events = null;
1557
+ var eventRefs = 0;
1558
+ function mergeValues(fallback, values) {
1559
+ var out = {};
1560
+ var base = fallback && typeof fallback === "object" ? fallback : {};
1561
+ var key;
1562
+ for (key in base) {
1563
+ if (Object.prototype.hasOwnProperty.call(base, key)) out[key] = base[key];
1564
+ }
1565
+ if (values && typeof values === "object") {
1566
+ for (key in values) {
1567
+ if (Object.prototype.hasOwnProperty.call(values, key)) out[key] = values[key];
1568
+ }
1569
+ }
1570
+ return out;
1571
+ }
1572
+ function unwrap(reply) {
1573
+ if (reply && typeof reply === "object" && Object.prototype.hasOwnProperty.call(reply, "result")) return reply.result;
1574
+ return reply;
1575
+ }
1576
+ function fetchHost(action, args) {
1577
+ return fetch(BASE + "/domains/" + HOST_DOMAIN + "/actions/" + encodeURIComponent(action), {
1578
+ method: "POST",
1579
+ headers: { "content-type": "application/json" },
1580
+ body: JSON.stringify(args || {})
1581
+ }).then(function(res) {
1582
+ return res.json().catch(function() {
1583
+ return { ok: false, error: "HTTP " + res.status };
1584
+ });
1585
+ });
1586
+ }
1587
+ function callHost(api, action, args) {
1588
+ if (api && typeof api.call === "function") {
1589
+ return Promise.resolve(api.call(action, args)).catch(function(error) {
1590
+ return { ok: false, error: String(error && error.message || error) };
1591
+ });
1592
+ }
1593
+ if (typeof fetch !== "function") return Promise.reject(new Error("\u5F53\u524D\u73AF\u5883\u6CA1\u6709 fetch\uFF0C\u8BFB\u4E0D\u5230\u5BBF\u4E3B\u914D\u7F6E\u4E2D\u5FC3"));
1594
+ return fetchHost(action, args);
1595
+ }
1596
+ function toSnapshot(body) {
1597
+ var reply = unwrap(body);
1598
+ if (!reply || typeof reply !== "object") throw new Error("\u5BBF\u4E3B\u914D\u7F6E\u4E2D\u5FC3\u6CA1\u6709\u8FD4\u56DE\u5FEB\u7167");
1599
+ if (reply.error) throw new Error(String(reply.error));
1600
+ var declared = reply.declared && typeof reply.declared === "object" ? reply.declared : {};
1601
+ return {
1602
+ values: reply.values && typeof reply.values === "object" ? reply.values : {},
1603
+ items: Array.isArray(declared.items) ? declared.items : [],
1604
+ groups: Array.isArray(declared.groups) ? declared.groups : [],
1605
+ overrides: reply.overrides && typeof reply.overrides === "object" ? reply.overrides : {},
1606
+ schemes: Array.isArray(reply.schemes) ? reply.schemes : [],
1607
+ activeSchemeId: reply.activeSchemeId || null
1608
+ };
1609
+ }
1610
+ function notify(domain, payload) {
1611
+ var set = listeners.get(domain);
1612
+ if (!set) return;
1613
+ set.forEach(function(fn) {
1614
+ try {
1615
+ fn(payload);
1616
+ } catch (error) {
1617
+ }
1618
+ });
1619
+ }
1620
+ function storeSnapshot(domain, snapshot) {
1621
+ cache.set(domain, { at: Date.now(), values: snapshot.values, snapshot });
1622
+ }
1623
+ function loadSnapshot(api, domain) {
1624
+ return callHost(api, "wb.configCenterGet", { domain }).then(toSnapshot);
1625
+ }
1626
+ function readConfig(api, domain, fallback, ttlMs) {
1627
+ var ttl = typeof ttlMs === "number" ? ttlMs : DEFAULT_TTL;
1628
+ var hit = cache.get(domain);
1629
+ if (hit && Date.now() - hit.at < ttl) return Promise.resolve(mergeValues(fallback, hit.values));
1630
+ if (inflight.has(domain)) {
1631
+ return inflight.get(domain).then(
1632
+ function(values) {
1633
+ return mergeValues(fallback, values);
1634
+ },
1635
+ function() {
1636
+ return mergeValues(fallback, hit ? hit.values : null);
1637
+ }
1638
+ );
1639
+ }
1640
+ var pending = loadSnapshot(api, domain).then(function(snapshot) {
1641
+ storeSnapshot(domain, snapshot);
1642
+ notify(domain, { reason: "read", values: snapshot.values });
1643
+ return snapshot.values;
1644
+ });
1645
+ inflight.set(domain, pending);
1646
+ var clear = function() {
1647
+ inflight.delete(domain);
1648
+ };
1649
+ pending.then(clear, clear);
1650
+ return pending.then(
1651
+ function(values) {
1652
+ return mergeValues(fallback, values);
1653
+ },
1654
+ function() {
1655
+ return mergeValues(fallback, hit ? hit.values : null);
1656
+ }
1657
+ );
1658
+ }
1659
+ function writeConfig(api, domain, values) {
1660
+ return callHost(api, "wb.configCenterSet", { domain, values: values || {} }).then(function(body) {
1661
+ var snapshot = toSnapshot(body);
1662
+ storeSnapshot(domain, snapshot);
1663
+ notify(domain, { reason: "set", values: snapshot.values });
1664
+ return snapshot.values;
1665
+ });
1666
+ }
1667
+ function invalidateConfig(domain) {
1668
+ cache.delete(domain);
1669
+ }
1670
+ function subscribeConfigChange(api, domain, cb) {
1671
+ if (typeof window === "undefined" || typeof window.EventSource !== "function") return function() {
1672
+ };
1673
+ var set = listeners.get(domain);
1674
+ if (!set) {
1675
+ set = /* @__PURE__ */ new Set();
1676
+ listeners.set(domain, set);
1677
+ }
1678
+ var handler = typeof cb === "function" ? cb : function() {
1679
+ };
1680
+ set.add(handler);
1681
+ if (!events) {
1682
+ events = new window.EventSource(BASE + "/events");
1683
+ events.addEventListener("domain-event", function(event) {
1684
+ var payload;
1685
+ try {
1686
+ payload = JSON.parse(event.data);
1687
+ } catch (error) {
1688
+ return;
1689
+ }
1690
+ if (!payload || payload.event !== "host.config-changed") return;
1691
+ var data = payload.data || {};
1692
+ var target = data.domain || payload.domainId;
1693
+ if (!target) return;
1694
+ invalidateConfig(target);
1695
+ notify(target, data);
1696
+ });
1697
+ }
1698
+ eventRefs += 1;
1699
+ var closed = false;
1700
+ return function() {
1701
+ if (closed) return;
1702
+ closed = true;
1703
+ set.delete(handler);
1704
+ if (!set.size) listeners.delete(domain);
1705
+ eventRefs -= 1;
1706
+ if (eventRefs <= 0 && events) {
1707
+ try {
1708
+ events.close();
1709
+ } catch (error) {
1710
+ }
1711
+ events = null;
1712
+ eventRefs = 0;
1713
+ }
1714
+ };
1715
+ }
1716
+ function useConfig(api, domain, fallback, ReactImpl) {
1717
+ var R = ReactImpl || React;
1718
+ var initial = {
1719
+ ready: false,
1720
+ values: mergeValues(fallback, null),
1721
+ items: [],
1722
+ groups: [],
1723
+ overrides: {},
1724
+ schemes: [],
1725
+ activeSchemeId: null,
1726
+ error: ""
1727
+ };
1728
+ var state = R.useState(initial);
1729
+ var current = state[0];
1730
+ var set = state[1];
1731
+ R.useEffect(function() {
1732
+ var alive = true;
1733
+ loadSnapshot(api, domain).then(
1734
+ function(snapshot) {
1735
+ if (!alive) return;
1736
+ storeSnapshot(domain, snapshot);
1737
+ set({
1738
+ ready: true,
1739
+ values: mergeValues(fallback, snapshot.values),
1740
+ items: snapshot.items,
1741
+ groups: snapshot.groups,
1742
+ overrides: snapshot.overrides,
1743
+ schemes: snapshot.schemes,
1744
+ activeSchemeId: snapshot.activeSchemeId,
1745
+ error: ""
1746
+ });
1747
+ },
1748
+ function(error) {
1749
+ if (!alive) return;
1750
+ set(function(prev) {
1751
+ return {
1752
+ ready: true,
1753
+ values: prev.values,
1754
+ items: prev.items,
1755
+ groups: prev.groups,
1756
+ overrides: prev.overrides,
1757
+ schemes: prev.schemes,
1758
+ activeSchemeId: prev.activeSchemeId,
1759
+ error: String(error && error.message || error)
1760
+ };
1761
+ });
1762
+ }
1763
+ );
1764
+ return function() {
1765
+ alive = false;
1766
+ };
1767
+ }, [api, domain]);
1768
+ return current;
1769
+ }
1770
+ module2.exports = {
1771
+ useConfig,
1772
+ readConfig,
1773
+ writeConfig,
1774
+ invalidateConfig,
1775
+ subscribeConfigChange
1776
+ };
1777
+ }
1778
+ });
1779
+
1545
1780
  // packages/addons/danmaku/client/overlay/mount.cjs
1546
1781
  var require_mount = __commonJS({
1547
1782
  "packages/addons/danmaku/client/overlay/mount.cjs"(exports2, module2) {
1548
1783
  "use strict";
1549
1784
  var overlayStyle = require_style();
1550
1785
  var layerModule = require_layer();
1786
+ var configClient = require_config_client();
1551
1787
  var DOMAIN = "wb-addon-danmaku";
1552
1788
  var ACTION_PREFIX = "danmaku.";
1553
1789
  var STYLE_ID2 = "wbdm-dm-styles";
@@ -1663,19 +1899,11 @@ var require_mount = __commonJS({
1663
1899
  if (visible) place(config.layer);
1664
1900
  }
1665
1901
  function refresh() {
1666
- return call("configGet", {}).then(function(res) {
1667
- var result = res && res.result ? res.result : res;
1668
- if (res && res.ok && result && result.values) {
1902
+ return configClient.readConfig(null, DOMAIN, DEFAULTS, 0).then(function(values) {
1903
+ if (values && typeof values === "object") {
1669
1904
  retries = 0;
1670
1905
  configLoaded = true;
1671
- apply(result.values);
1672
- return;
1673
- }
1674
- throw new Error(res && res.error || "\u914D\u7F6E\u8BFB\u53D6\u5931\u8D25");
1675
- }).catch(function() {
1676
- if (retries < 5) {
1677
- retries += 1;
1678
- window.setTimeout(refresh, 1500 * retries);
1906
+ apply(values);
1679
1907
  }
1680
1908
  });
1681
1909
  }
@@ -2946,637 +3174,6 @@ var require_library = __commonJS({
2946
3174
  }
2947
3175
  });
2948
3176
 
2949
- // packages/addons/lib/config-scheme-bar.cjs
2950
- var require_config_scheme_bar = __commonJS({
2951
- "packages/addons/lib/config-scheme-bar.cjs"(exports2, module2) {
2952
- "use strict";
2953
- var React = require("react");
2954
- var h = React.createElement;
2955
- var COPY = {
2956
- zh: {
2957
- title: "\u914D\u7F6E\u65B9\u6848",
2958
- none: "\u4E0D\u4F7F\u7528\u65B9\u6848\uFF08\u63D2\u4EF6\u9ED8\u8BA4\u914D\u7F6E\uFF09",
2959
- create: "\u65B0\u5EFA\u65B9\u6848",
2960
- rename: "\u91CD\u547D\u540D",
2961
- remove: "\u5220\u9664",
2962
- namePlaceholder: "\u65B9\u6848\u540D\u79F0",
2963
- ok: "\u786E\u5B9A",
2964
- cancel: "\u53D6\u6D88",
2965
- current: "\u5F53\u524D\u65B9\u6848",
2966
- defaultHint: "\u672A\u9009\u62E9\u65B9\u6848\u65F6\u4F7F\u7528\u63D2\u4EF6\u9ED8\u8BA4\u914D\u7F6E\uFF1B\u4FEE\u6539\u4EFB\u610F\u8BBE\u7F6E\u4F1A\u81EA\u52A8\u65B0\u5EFA\u300C\u672C\u673A\u914D\u7F6E\u300D\u65B9\u6848\u3002",
2967
- syncedHint: "\u65B9\u6848\u4F1A\u968F\u4E91\u7AEF\u540C\u6B65\u4E0E\u5907\u4EFD\u4E00\u8D77\u8D70\uFF1B\u5F53\u524D\u9009\u4E2D\u54EA\u4E2A\u65B9\u6848\u53EA\u4FDD\u5B58\u5728\u672C\u673A\u3002",
2968
- confirmDelete: "\u5220\u9664\u5F53\u524D\u65B9\u6848\u540E\u4F1A\u56DE\u5230\u63D2\u4EF6\u9ED8\u8BA4\u914D\u7F6E\uFF0C\u786E\u5B9A\u5220\u9664\uFF1F",
2969
- createTitle: "\u65B0\u5EFA\u914D\u7F6E\u65B9\u6848",
2970
- renameTitle: "\u91CD\u547D\u540D\u914D\u7F6E\u65B9\u6848",
2971
- fromCurrent: "\u4ECE\u5F53\u524D\u65B9\u6848\u590D\u5236",
2972
- fromDefault: "\u4ECE\u63D2\u4EF6\u9ED8\u8BA4\u5F00\u59CB"
2973
- },
2974
- en: {
2975
- title: "Config scheme",
2976
- none: "No scheme (plugin defaults)",
2977
- create: "New scheme",
2978
- rename: "Rename",
2979
- remove: "Delete",
2980
- namePlaceholder: "Scheme name",
2981
- ok: "OK",
2982
- cancel: "Cancel",
2983
- current: "Active scheme",
2984
- defaultHint: 'With no scheme selected the plugin defaults apply. Editing any setting creates a local "My config" scheme automatically.',
2985
- syncedHint: "Schemes sync to the cloud and travel with backups; which scheme this device uses stays local.",
2986
- confirmDelete: "Deleting the active scheme returns to plugin defaults. Delete it?",
2987
- createTitle: "New config scheme",
2988
- renameTitle: "Rename config scheme",
2989
- fromCurrent: "Copy active scheme",
2990
- fromDefault: "Start from plugin defaults"
2991
- }
2992
- };
2993
- var styles2 = [
2994
- ".__P__-cfg-schemes{display:flex;flex-direction:column;gap:10px;padding:13px 16px;border:1px solid var(--cfg-line);border-radius:12px;background:var(--cfg-surface)}",
2995
- ".__P__-cfg-schemes-main{display:flex;align-items:center;gap:10px;flex-wrap:wrap}",
2996
- ".__P__-cfg-schemes-label{font-size:12.5px;font-weight:660;white-space:nowrap}",
2997
- ".__P__-cfg-schemes-select{border:1px solid var(--cfg-line);background:var(--dsw-alias-bg-base,transparent);color:inherit;border-radius:9px;padding:7px 11px;font:inherit;font-size:13px;line-height:1.4;outline:none;min-width:190px;max-width:100%}",
2998
- ".__P__-cfg-schemes-select:focus{border-color:var(--dsw-alias-brand-primary,currentColor);box-shadow:0 0 0 3px color-mix(in oklab,var(--dsw-alias-brand-primary,currentColor) 14%,transparent)}",
2999
- ".__P__-cfg-schemes-actions{display:flex;gap:7px;flex-wrap:wrap}",
3000
- ".__P__-cfg-sbtn{appearance:none;border:1px solid var(--cfg-line);background:transparent;color:inherit;border-radius:8px;padding:5px 11px;font:inherit;font-size:12px;line-height:1.5;cursor:pointer;transition:background .12s ease,border-color .12s ease}",
3001
- ".__P__-cfg-sbtn:hover:not(:disabled){background:var(--cfg-hover);border-color:color-mix(in oklab,currentColor 28%,var(--cfg-line))}",
3002
- ".__P__-cfg-sbtn:disabled{opacity:.5;cursor:default}",
3003
- ".__P__-cfg-sbtn.danger{color:var(--dsw-alias-state-error-primary,inherit)}",
3004
- ".__P__-cfg-schemes-form{display:flex;align-items:center;gap:7px;flex-wrap:wrap}",
3005
- ".__P__-cfg-schemes-input{flex:1 1 180px;min-width:120px;border:1px solid var(--cfg-line);background:var(--dsw-alias-bg-base,transparent);color:inherit;border-radius:8px;padding:6px 10px;font:inherit;font-size:13px;outline:none}",
3006
- ".__P__-cfg-schemes-input:focus{border-color:var(--dsw-alias-brand-primary,currentColor)}",
3007
- ".__P__-cfg-schemes-hint{font-size:11.5px;color:var(--cfg-muted);line-height:1.6}"
3008
- ];
3009
- function SchemeBar(props) {
3010
- var lang = props.lang === "en" ? "en" : "zh";
3011
- var c = props.copy ? Object.assign({}, COPY[lang], props.copy) : COPY[lang];
3012
- var prefix = props.prefix || "wb";
3013
- var cls = function(suffix) {
3014
- return "." + prefix + "-cfg-" + suffix;
3015
- };
3016
- var schemes = Array.isArray(props.schemes) ? props.schemes : [];
3017
- var activeId = props.activeId || "";
3018
- var busy = Boolean(props.busy);
3019
- var [editing, setEditing] = React.useState(null);
3020
- var [name, setName] = React.useState("");
3021
- var [from, setFrom] = React.useState("current");
3022
- function begin(mode) {
3023
- var current = schemes.filter(function(s) {
3024
- return s.id === activeId;
3025
- })[0];
3026
- setName(mode === "rename" && current ? current.name : "");
3027
- setFrom(current ? "current" : "default");
3028
- setEditing(mode);
3029
- }
3030
- function submit(event) {
3031
- if (event && event.preventDefault) event.preventDefault();
3032
- var value = String(name || "").trim();
3033
- if (!value || busy) return;
3034
- if (editing === "rename") props.onRename(activeId, value);
3035
- else props.onCreate(value, from);
3036
- setEditing(null);
3037
- setName("");
3038
- }
3039
- function remove() {
3040
- if (typeof window !== "undefined" && window.confirm && !window.confirm(c.confirmDelete)) return;
3041
- props.onDelete(activeId);
3042
- }
3043
- return h(
3044
- "div",
3045
- { className: prefix + "-cfg-schemes" },
3046
- h(
3047
- "div",
3048
- { className: prefix + "-cfg-schemes-main" },
3049
- h("span", { className: prefix + "-cfg-schemes-label" }, c.title),
3050
- h(
3051
- "select",
3052
- {
3053
- className: prefix + "-cfg-schemes-select",
3054
- value: activeId,
3055
- disabled: busy,
3056
- onChange: function(event) {
3057
- props.onActivate(event.target.value || null);
3058
- },
3059
- "aria-label": c.title
3060
- },
3061
- h("option", { value: "" }, c.none),
3062
- schemes.map(function(s) {
3063
- return h("option", { key: s.id, value: s.id }, s.name);
3064
- })
3065
- ),
3066
- h("span", { className: prefix + "-cfg-schemes-hint" }, activeId ? c.current : "")
3067
- ),
3068
- editing ? h(
3069
- "form",
3070
- { className: prefix + "-cfg-schemes-form", onSubmit: submit },
3071
- h("span", { className: prefix + "-cfg-schemes-label" }, editing === "rename" ? c.renameTitle : c.createTitle),
3072
- h("input", {
3073
- className: prefix + "-cfg-schemes-input",
3074
- value: name,
3075
- placeholder: c.namePlaceholder,
3076
- maxLength: 64,
3077
- autoFocus: true,
3078
- disabled: busy,
3079
- onChange: function(event) {
3080
- setName(event.target.value);
3081
- }
3082
- }),
3083
- editing === "create" && activeId ? h(
3084
- "select",
3085
- { className: prefix + "-cfg-schemes-select", value: from, disabled: busy, onChange: function(event) {
3086
- setFrom(event.target.value);
3087
- }, "aria-label": c.createTitle },
3088
- h("option", { value: "current" }, c.fromCurrent),
3089
- h("option", { value: "default" }, c.fromDefault)
3090
- ) : null,
3091
- h("button", { className: prefix + "-cfg-sbtn", type: "submit", disabled: busy || !String(name || "").trim() }, c.ok),
3092
- h("button", { className: prefix + "-cfg-sbtn", type: "button", disabled: busy, onClick: function() {
3093
- setEditing(null);
3094
- } }, c.cancel)
3095
- ) : h(
3096
- "div",
3097
- { className: prefix + "-cfg-schemes-actions" },
3098
- h("button", { className: prefix + "-cfg-sbtn", type: "button", disabled: busy, onClick: function() {
3099
- begin("create");
3100
- } }, c.create),
3101
- activeId ? h("button", { className: prefix + "-cfg-sbtn", type: "button", disabled: busy, onClick: function() {
3102
- begin("rename");
3103
- } }, c.rename) : null,
3104
- activeId ? h("button", { className: prefix + "-cfg-sbtn danger", type: "button", disabled: busy, onClick: remove }, c.remove) : null
3105
- ),
3106
- h("div", { className: prefix + "-cfg-schemes-hint" }, activeId ? c.syncedHint : c.defaultHint)
3107
- );
3108
- }
3109
- module2.exports = { SchemeBar, styles: styles2, COPY };
3110
- }
3111
- });
3112
-
3113
- // packages/addons/danmaku/client/views/config.cjs
3114
- var require_config = __commonJS({
3115
- "packages/addons/danmaku/client/views/config.cjs"(exports2, module2) {
3116
- "use strict";
3117
- var GLYPHS2 = require_icons2().ICONS;
3118
- var React = require("react");
3119
- var h = React.createElement;
3120
- var copy = require_copy();
3121
- var SW = require_switch();
3122
- var NF = require_number_field();
3123
- var SB = require_config_scheme_bar();
3124
- var DOMAIN = "danmaku";
3125
- var SCHEME_CSS = [
3126
- ".wbdm-wrap{--cfg-line:var(--wb-border,var(--dsw-alias-border-l2,color-mix(in oklab,currentColor 16%,transparent)));--cfg-surface:var(--wb-card,var(--wb-surface,var(--dsw-alias-bg-layer-1,transparent)));--cfg-muted:var(--wb-muted,var(--dsw-alias-label-secondary,color-mix(in oklab,currentColor 62%,transparent)));--cfg-hover:var(--dsw-alias-interactive-bg-hover,color-mix(in oklab,currentColor 5%,transparent))}"
3127
- ].concat(SB.styles).join("\n").split("__P__").join("wbdm");
3128
- var ICONS = {
3129
- check: GLYPHS2["check"],
3130
- power: GLYPHS2["power"],
3131
- refresh: GLYPHS2["refresh-cw"],
3132
- monitor: GLYPHS2["monitor"]
3133
- };
3134
- function glyph(Icon2, size) {
3135
- return h(Icon2, { size: size || 14, strokeWidth: 1.8, "aria-hidden": "true" });
3136
- }
3137
- function unwrap(r) {
3138
- return r && r.result !== void 0 ? r.result : r;
3139
- }
3140
- function useConfig(api, fallback) {
3141
- var stateS = React.useState({
3142
- ready: false,
3143
- values: Object.assign({}, fallback || {}),
3144
- items: [],
3145
- groups: [],
3146
- overrides: {},
3147
- warnings: [],
3148
- error: ""
3149
- });
3150
- var state = stateS[0];
3151
- var setState = stateS[1];
3152
- React.useEffect(function() {
3153
- var alive = true;
3154
- Promise.resolve(api.call(DOMAIN + ".configGet")).then(function(r) {
3155
- if (!alive) return;
3156
- if (r && r.ok === false) throw new Error(r && r.error || "load failed");
3157
- var body = unwrap(r);
3158
- if (!body || !body.values) throw new Error("empty config");
3159
- setState({
3160
- ready: true,
3161
- values: Object.assign({}, fallback || {}, body.values || {}),
3162
- items: body.items || [],
3163
- groups: body.groups || [],
3164
- overrides: body.overrides || {},
3165
- warnings: body.warnings || [],
3166
- error: "",
3167
- state: body
3168
- });
3169
- }).catch(function(e) {
3170
- if (!alive) return;
3171
- setState(function(prev) {
3172
- return Object.assign({}, prev, { ready: true, error: String(e && e.message || e) });
3173
- });
3174
- });
3175
- return function() {
3176
- alive = false;
3177
- };
3178
- }, [api]);
3179
- function setValues(next) {
3180
- if (!next || typeof next !== "object") return;
3181
- setState(function(prev) {
3182
- return Object.assign({}, prev, { values: Object.assign({}, prev.values, next) });
3183
- });
3184
- }
3185
- function setBody(body) {
3186
- if (!body) return;
3187
- setState({
3188
- ready: true,
3189
- values: Object.assign({}, fallback || {}, body.values || {}),
3190
- items: body.items || [],
3191
- groups: body.groups || [],
3192
- overrides: body.overrides || {},
3193
- warnings: body.warnings || [],
3194
- error: "",
3195
- state: body
3196
- });
3197
- }
3198
- return Object.assign({}, state, { setValues, setBody });
3199
- }
3200
- function labelOf(item, lang) {
3201
- return lang === "en" ? item.en || item.zh || item.id : item.zh || item.en || item.id;
3202
- }
3203
- function hintOf(item, lang) {
3204
- return lang === "en" ? item.hintEn || "" : item.hintZh || "";
3205
- }
3206
- function useDesktop(api, enabled) {
3207
- var stateS = React.useState({ data: null, error: "" });
3208
- var state = stateS[0];
3209
- var setState = stateS[1];
3210
- React.useEffect(function() {
3211
- if (!enabled) return void 0;
3212
- var alive = true;
3213
- function tick() {
3214
- Promise.resolve(api.call(DOMAIN + ".desktop", { op: "status" })).then(function(r) {
3215
- if (!alive) return;
3216
- if (r && r.ok === false) throw new Error(r && r.error || "status failed");
3217
- setState({ data: unwrap(r), error: "" });
3218
- }).catch(function(e) {
3219
- if (!alive) return;
3220
- setState({ data: null, error: String(e && e.message || e) });
3221
- });
3222
- }
3223
- tick();
3224
- var timer = window.setInterval(tick, 4e3);
3225
- return function() {
3226
- alive = false;
3227
- window.clearInterval(timer);
3228
- };
3229
- }, [api, enabled]);
3230
- return state;
3231
- }
3232
- function statusCell(key, value) {
3233
- return h(
3234
- "div",
3235
- { className: "wbdm-status-cell", key },
3236
- h("div", { className: "wbdm-status-k" }, key),
3237
- h("div", { className: "wbdm-status-v" }, value)
3238
- );
3239
- }
3240
- function DesktopCard(props) {
3241
- var api = props.api;
3242
- var lang = props.lang;
3243
- var C = copy.pick(lang);
3244
- var state = useDesktop(api, true);
3245
- var busyS = React.useState("");
3246
- var busy = busyS[0];
3247
- var setBusy = busyS[1];
3248
- var sayS = React.useState("");
3249
- var say = sayS[0];
3250
- var setSay = sayS[1];
3251
- var data = state.data || {};
3252
- function op(name) {
3253
- setBusy(name);
3254
- Promise.resolve(api.call(DOMAIN + ".desktop", { op: name })).then(function(r) {
3255
- if (r && r.ok === false) throw new Error(r && r.error || "failed");
3256
- setSay("");
3257
- return unwrap(r);
3258
- }).catch(function(e) {
3259
- setSay(String(e && e.message || e));
3260
- }).then(function() {
3261
- setBusy("");
3262
- });
3263
- }
3264
- var phase = data.phase || "stopped";
3265
- var phaseLabel = C.desktopPhase[phase] || phase;
3266
- return h(
3267
- "div",
3268
- { className: "wbdm-card" },
3269
- h(
3270
- "div",
3271
- { className: "wbdm-card-h" },
3272
- h(
3273
- "span",
3274
- { className: "wbdm-card-t" },
3275
- glyph(ICONS.monitor),
3276
- " ",
3277
- C.desktopTitle
3278
- ),
3279
- h("span", { className: "wbdm-card-s" }, C.desktopHint)
3280
- ),
3281
- h(
3282
- "div",
3283
- { className: "wbdm-status" },
3284
- statusCell(lang === "en" ? "State" : "\u72B6\u6001", phaseLabel + (data.suppressed ? " \xB7 " + C.desktopSuppressed : "")),
3285
- statusCell(lang === "en" ? "Protocol" : "\u534F\u8BAE", copy.fmt(C.desktopProtocol, { n: data.protocol || 1 })),
3286
- statusCell(lang === "en" ? "Displays" : "\u5C4F\u5E55", copy.fmt(C.desktopDisplays, { n: data.displays || 0 })),
3287
- statusCell(
3288
- lang === "en" ? "Theme" : "\u4E3B\u9898",
3289
- data.themeSynced ? C.desktopTheme : C.desktopThemeMissing
3290
- )
3291
- ),
3292
- state.error ? h("div", { className: "wbdm-notice error" }, state.error) : null,
3293
- say ? h("div", { className: "wbdm-notice error" }, say) : null,
3294
- h(
3295
- "div",
3296
- { className: "wbdm-status-actions" },
3297
- h(
3298
- "button",
3299
- {
3300
- type: "button",
3301
- className: "wbdm-btn",
3302
- disabled: Boolean(busy),
3303
- onClick: function() {
3304
- op("restart");
3305
- }
3306
- },
3307
- glyph(ICONS.refresh),
3308
- C.desktopRestart
3309
- ),
3310
- h(
3311
- "button",
3312
- {
3313
- type: "button",
3314
- className: "wbdm-btn",
3315
- disabled: Boolean(busy),
3316
- onClick: function() {
3317
- op("stop");
3318
- }
3319
- },
3320
- C.desktopStop
3321
- ),
3322
- h(
3323
- "button",
3324
- {
3325
- type: "button",
3326
- className: "wbdm-btn primary",
3327
- disabled: Boolean(busy),
3328
- onClick: function() {
3329
- op("resume");
3330
- }
3331
- },
3332
- glyph(ICONS.power),
3333
- C.desktopResume
3334
- )
3335
- )
3336
- );
3337
- }
3338
- function ConfigView(props) {
3339
- var api = props.api;
3340
- var lang = props.lang;
3341
- var t = props.t;
3342
- var C = copy.pick(lang);
3343
- var cfg = useConfig(api, {});
3344
- var localS = React.useState({});
3345
- var local = localS[0];
3346
- var setLocal = localS[1];
3347
- var toastS = React.useState("");
3348
- var toast = toastS[0];
3349
- var setToast = toastS[1];
3350
- var schemeBusyS = React.useState(false);
3351
- var schemeBusy = schemeBusyS[0];
3352
- var setSchemeBusy = schemeBusyS[1];
3353
- var schemeNote = lang === "en" ? "Schemes sync to the cloud and travel with backups; which scheme this device uses stays local" : "\u914D\u7F6E\u65B9\u6848\u4F1A\u968F\u4E91\u7AEF\u540C\u6B65\u4E0E\u5907\u4EFD\uFF1B\u5F53\u524D\u9009\u4E2D\u54EA\u4E2A\u65B9\u6848\u53EA\u4FDD\u5B58\u5728\u672C\u673A";
3354
- var seqRef = React.useRef(0);
3355
- var aliveRef = React.useRef(true);
3356
- React.useEffect(function() {
3357
- if (typeof document !== "undefined") {
3358
- var old = document.getElementById("wb-addon-danmaku-config-scheme-css");
3359
- if (old) {
3360
- old.textContent = SCHEME_CSS;
3361
- } else {
3362
- var el = document.createElement("style");
3363
- el.id = "wb-addon-danmaku-config-scheme-css";
3364
- el.textContent = SCHEME_CSS;
3365
- document.head.appendChild(el);
3366
- }
3367
- }
3368
- aliveRef.current = true;
3369
- return function() {
3370
- aliveRef.current = false;
3371
- };
3372
- }, []);
3373
- function values() {
3374
- return Object.assign({}, cfg.values, local);
3375
- }
3376
- function toastOnce(text) {
3377
- setToast(text);
3378
- window.setTimeout(function() {
3379
- if (aliveRef.current) setToast("");
3380
- }, 2200);
3381
- }
3382
- function save(id, value) {
3383
- var seq = ++seqRef.current;
3384
- var patch = {};
3385
- patch[id] = value;
3386
- setLocal(function(prev) {
3387
- return Object.assign({}, prev, patch);
3388
- });
3389
- Promise.resolve(api.call(DOMAIN + ".configSet", { values: patch })).then(function(r) {
3390
- if (r && r.ok === false) throw new Error(r && r.error || C.saveFail);
3391
- var body = unwrap(r) || {};
3392
- if (seq !== seqRef.current) return;
3393
- if (typeof cfg.setValues === "function") cfg.setValues(body.values);
3394
- setLocal({});
3395
- toastOnce(C.saved);
3396
- }).catch(function(e) {
3397
- setLocal(function(prev) {
3398
- var next = Object.assign({}, prev);
3399
- delete next[id];
3400
- return next;
3401
- });
3402
- toastOnce(String(e && e.message || e));
3403
- });
3404
- }
3405
- function schemeCall(action, args) {
3406
- setSchemeBusy(true);
3407
- return Promise.resolve(api.call(DOMAIN + "." + action, args || {})).then(function(r) {
3408
- if (r && r.ok === false) throw new Error(r && r.error || C.saveFail);
3409
- var body = unwrap(r) || {};
3410
- var next = body.state || body;
3411
- if (next && typeof cfg.setBody === "function") {
3412
- cfg.setBody(next);
3413
- seqRef.current += 1;
3414
- setLocal({});
3415
- }
3416
- toastOnce(C.saved);
3417
- }).catch(function(e) {
3418
- toastOnce(String(e && e.message || e));
3419
- }).then(function() {
3420
- setSchemeBusy(false);
3421
- });
3422
- }
3423
- function resetAll() {
3424
- Promise.resolve(api.call(DOMAIN + ".configReset", {})).then(function(r) {
3425
- if (r && r.ok === false) throw new Error(r && r.error || C.saveFail);
3426
- var body = unwrap(r) || {};
3427
- if (typeof cfg.setValues === "function") cfg.setValues(body.values);
3428
- seqRef.current += 1;
3429
- setLocal({});
3430
- toastOnce(C.resetDone);
3431
- }).catch(function(e) {
3432
- toastOnce(String(e && e.message || e));
3433
- });
3434
- }
3435
- function switchControl(item, value) {
3436
- return h(SW.Switch, {
3437
- h,
3438
- checked: value === true,
3439
- title: labelOf(item, lang),
3440
- ariaLabel: labelOf(item, lang),
3441
- onChange: function(next) {
3442
- save(item.id, next);
3443
- }
3444
- });
3445
- }
3446
- function control(item) {
3447
- var value = values()[item.id];
3448
- if (item.type === "bool") return switchControl(item, value);
3449
- if (item.type === "enum") {
3450
- return h(
3451
- "select",
3452
- {
3453
- className: "wbdm-select",
3454
- value: value === void 0 ? "" : String(value),
3455
- "aria-label": labelOf(item, lang),
3456
- onChange: function(e) {
3457
- var hit = (item.values || []).filter(function(option) {
3458
- return String(option.value) === e.target.value;
3459
- })[0];
3460
- save(item.id, hit ? hit.value : e.target.value);
3461
- }
3462
- },
3463
- (item.values || []).map(function(option) {
3464
- return h(
3465
- "option",
3466
- { key: String(option.value), value: String(option.value) },
3467
- lang === "en" ? option.en || option.zh || String(option.value) : option.zh || option.en || String(option.value)
3468
- );
3469
- })
3470
- );
3471
- }
3472
- if (item.type === "int" || item.type === "number") {
3473
- return h(NF.NumberField, {
3474
- h,
3475
- lang,
3476
- value: value === void 0 ? "" : value,
3477
- min: item.min,
3478
- max: item.max,
3479
- integer: item.type === "int",
3480
- ariaLabel: labelOf(item, lang),
3481
- onCommit: function(next) {
3482
- save(item.id, next);
3483
- }
3484
- });
3485
- }
3486
- return h("input", {
3487
- className: "wbdm-input",
3488
- type: "text",
3489
- value: value === void 0 ? "" : String(value),
3490
- "aria-label": labelOf(item, lang),
3491
- onChange: function(e) {
3492
- save(item.id, e.target.value);
3493
- }
3494
- });
3495
- }
3496
- var head = h(
3497
- "div",
3498
- { className: "wbdm-hero" },
3499
- h(
3500
- "div",
3501
- { className: "wbdm-hero-main" },
3502
- h("div", { className: "wbdm-title" }, C.configTitle),
3503
- h("div", { className: "wbdm-sub" }, schemeNote)
3504
- ),
3505
- h(
3506
- "button",
3507
- { type: "button", className: "wbdm-btn ghost", onClick: resetAll, disabled: !cfg.ready },
3508
- C.resetAll
3509
- )
3510
- );
3511
- if (!cfg.ready) return h("div", { className: "wbdm-wrap" }, h("div", { className: "wbdm-empty" }, C.loading));
3512
- if (cfg.error && !cfg.items.length) {
3513
- return h("div", { className: "wbdm-wrap" }, h("div", { className: "wbdm-notice error" }, cfg.error));
3514
- }
3515
- var groups = cfg.groups.length ? cfg.groups : [{ id: "all", zh: "\u5168\u90E8", en: "All" }];
3516
- var sections = groups.map(function(group) {
3517
- var items = cfg.items.filter(function(item) {
3518
- return (item.group || "all") === group.id;
3519
- });
3520
- if (!items.length) return null;
3521
- return h(
3522
- "div",
3523
- { className: "wbdm-card", key: group.id },
3524
- h(
3525
- "div",
3526
- { className: "wbdm-card-h" },
3527
- h("span", { className: "wbdm-card-t" }, lang === "en" ? group.en || group.zh : group.zh || group.en),
3528
- h("span", { className: "wbdm-card-s" }, String(items.length) + (lang === "en" ? " options" : " \u9879"))
3529
- ),
3530
- items.map(function(item) {
3531
- return h(
3532
- "div",
3533
- { className: "wbdm-crow", key: item.id },
3534
- h(
3535
- "div",
3536
- null,
3537
- h("div", { className: "wbdm-clabel" }, labelOf(item, lang)),
3538
- hintOf(item, lang) ? h("div", { className: "wbdm-chint" }, hintOf(item, lang)) : null
3539
- ),
3540
- h("div", { className: "wbdm-ctl" }, control(item))
3541
- );
3542
- })
3543
- );
3544
- });
3545
- return h(
3546
- "div",
3547
- { className: "wbdm-wrap" },
3548
- head,
3549
- h(SB.SchemeBar, {
3550
- key: "schemes",
3551
- lang,
3552
- prefix: "wbdm",
3553
- schemes: cfg.state && cfg.state.schemes || [],
3554
- activeId: cfg.state && cfg.state.scheme ? cfg.state.scheme.id : "",
3555
- busy: schemeBusy,
3556
- onActivate: function(id) {
3557
- schemeCall("configSchemeActivate", { id: id || "" });
3558
- },
3559
- onCreate: function(name, from) {
3560
- schemeCall("configSchemeCreate", { name, from: from || "current" });
3561
- },
3562
- onRename: function(id, name) {
3563
- schemeCall("configSchemeRename", { id, name });
3564
- },
3565
- onDelete: function(id) {
3566
- schemeCall("configSchemeDelete", { id });
3567
- }
3568
- }),
3569
- toast ? h("div", { className: "wbdm-notice ok" }, toast) : null,
3570
- cfg.warnings && cfg.warnings.length ? h("div", { className: "wbdm-notice warn" }, cfg.warnings.join("\uFF1B")) : null,
3571
- sections,
3572
- h(DesktopCard, { api, lang, t })
3573
- );
3574
- }
3575
- module2.exports = ConfigView;
3576
- module2.exports.useConfig = useConfig;
3577
- }
3578
- });
3579
-
3580
3177
  // packages/addons/danmaku/client/client.src.cjs
3581
3178
  var GLYPHS = require_icons2().ICONS;
3582
3179
  var styles = require_styles();
@@ -3624,7 +3221,7 @@ function definition(host) {
3624
3221
  var tab = makeTab(host.react);
3625
3222
  return {
3626
3223
  id: ADDON_ID,
3627
- version: "0.3.3",
3224
+ version: "0.4.0",
3628
3225
  icon: tab("megaphone"),
3629
3226
  views: [
3630
3227
  {
@@ -3632,12 +3229,6 @@ function definition(host) {
3632
3229
  icon: tab("list"),
3633
3230
  title: { zh: "\u5F39\u5E55\u5E93", en: "Library" },
3634
3231
  component: require_library()
3635
- },
3636
- {
3637
- id: "config",
3638
- icon: tab("settings"),
3639
- title: { zh: "\u914D\u7F6E", en: "Settings" },
3640
- component: require_config()
3641
3232
  }
3642
3233
  ]
3643
3234
  };