@huaqiu/dsh-sidebar-entry 0.5.21 → 0.5.22

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/index.d.mts CHANGED
@@ -49,6 +49,15 @@ interface SidebarEntryOptions {
49
49
  refresh?: {
50
50
  subscribe(listener: () => void): () => void;
51
51
  };
52
+ /**
53
+ * Optional numeric badge (e.g. actionable plugin-update count). When set,
54
+ * the row renders a small count badge after the label; the badge is hidden
55
+ * at count 0. `subscribe` re-renders the badge whenever the count changes.
56
+ */
57
+ badge?: {
58
+ subscribe(listener: () => void): () => void;
59
+ count(): number;
60
+ };
52
61
  /** Click action (open/toggle the owning panel). */
53
62
  onToggle(): void;
54
63
  /** Family-block position: 'before' inserts ahead of sibling plugin rows, 'after' behind them. */
package/lib/index.mjs CHANGED
@@ -27,16 +27,35 @@ function createEntry(options) {
27
27
  iconSpan.className = options.css["entryIcon"] ?? "";
28
28
  iconSpan.innerHTML = options.icon;
29
29
  entry.append(iconSpan, labelSpan);
30
+ let badgeSpan = null;
31
+ const applyBadge = () => {
32
+ if (options.badge === void 0) return;
33
+ const count = options.badge.count();
34
+ if (count <= 0) {
35
+ badgeSpan?.remove();
36
+ badgeSpan = null;
37
+ return;
38
+ }
39
+ if (badgeSpan === null) {
40
+ badgeSpan = document.createElement("span");
41
+ badgeSpan.className = options.css["entryBadge"] ?? "";
42
+ entry.append(badgeSpan);
43
+ }
44
+ badgeSpan.textContent = String(count);
45
+ entry.setAttribute("data-has-badge", "");
46
+ };
30
47
  const applyLabel = () => {
31
48
  entry.setAttribute("aria-label", options.label());
32
49
  if (options.tooltip !== void 0) entry.setAttribute("title", options.tooltip());
33
50
  labelSpan.textContent = options.label();
34
51
  };
35
52
  applyLabel();
53
+ applyBadge();
36
54
  entry.addEventListener("click", options.onToggle);
37
55
  return {
38
56
  entry,
39
- applyLabel
57
+ applyLabel,
58
+ applyBadge
40
59
  };
41
60
  }
42
61
  /** Re-insert the entry after the New Session row (before the browser region). */
@@ -60,13 +79,17 @@ function placeEntry(root, entry, options) {
60
79
  */
61
80
  function mountSidebarEntry(options) {
62
81
  if (typeof document !== "undefined" && document.querySelector(options.rowSelector) !== null) return () => {};
63
- const { entry, applyLabel } = createEntry(options);
82
+ const { entry, applyLabel, applyBadge } = createEntry(options);
64
83
  let root;
65
84
  let placed = false;
66
85
  let unsubscribeRefresh;
67
86
  if (options.refresh !== void 0) try {
68
87
  unsubscribeRefresh = options.refresh.subscribe(applyLabel);
69
88
  } catch {}
89
+ let unsubscribeBadge;
90
+ if (options.badge !== void 0) try {
91
+ unsubscribeBadge = options.badge.subscribe(applyBadge);
92
+ } catch {}
70
93
  const tryPlace = () => {
71
94
  if (root !== void 0 && !root.isConnected) {
72
95
  rootObserver.disconnect();
@@ -102,7 +125,7 @@ function mountSidebarEntry(options) {
102
125
  }
103
126
  if (!root.contains(entry)) placed = placeEntry(root, entry, options);
104
127
  });
105
- const unsubscribeActive = options.active === void 0 ? void 0 : (() => {
128
+ options.active === void 0 || (() => {
106
129
  const syncActive = () => {
107
130
  if (options.active.isOpen()) entry.dataset.active = "true";
108
131
  else delete entry.dataset.active;
@@ -116,7 +139,7 @@ function mountSidebarEntry(options) {
116
139
  waitObserver.disconnect();
117
140
  rootObserver.disconnect();
118
141
  unsubscribeRefresh?.();
119
- unsubscribeActive?.();
142
+ unsubscribeBadge?.();
120
143
  entry.remove();
121
144
  };
122
145
  }
@@ -131,7 +154,8 @@ function mountSidebarEntry(options) {
131
154
  const HUAQIU_SIDEBAR_ENTRY_FAMILY = [
132
155
  "[data-hqcg-footprint-entry]",
133
156
  "[data-hqcg-symbol-entry]",
134
- "[data-hqcg-schematic-entry]"
157
+ "[data-hqcg-schematic-entry]",
158
+ "[data-hq-plugin-update-entry]"
135
159
  ];
136
160
  /**
137
161
  * Read the host UI language from `<html lang>`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@huaqiu/dsh-sidebar-entry",
3
- "version": "0.5.21",
3
+ "version": "0.5.22",
4
4
  "type": "module",
5
5
  "main": "./lib/index.mjs",
6
6
  "types": "./lib/index.d.mts",
package/src/index.ts CHANGED
@@ -47,6 +47,15 @@ export interface SidebarEntryOptions {
47
47
  * tooltip whenever the active locale changes.
48
48
  */
49
49
  refresh?: { subscribe(listener: () => void): () => void }
50
+ /**
51
+ * Optional numeric badge (e.g. actionable plugin-update count). When set,
52
+ * the row renders a small count badge after the label; the badge is hidden
53
+ * at count 0. `subscribe` re-renders the badge whenever the count changes.
54
+ */
55
+ badge?: {
56
+ subscribe(listener: () => void): () => void
57
+ count(): number
58
+ }
50
59
  /** Click action (open/toggle the owning panel). */
51
60
  onToggle(): void
52
61
  /** Family-block position: 'before' inserts ahead of sibling plugin rows, 'after' behind them. */
@@ -86,7 +95,11 @@ function newSessionButton(root: HTMLElement): HTMLButtonElement | undefined {
86
95
  }
87
96
 
88
97
  /** Build the entry row (a detached button; insert once the shell is up). */
89
- function createEntry(options: SidebarEntryOptions): { entry: HTMLButtonElement; applyLabel: () => void } {
98
+ function createEntry(options: SidebarEntryOptions): {
99
+ entry: HTMLButtonElement
100
+ applyLabel: () => void
101
+ applyBadge: () => void
102
+ } {
90
103
  const entry = document.createElement('button')
91
104
  entry.type = 'button'
92
105
  entry.setAttribute(options.rowAttribute, '')
@@ -101,14 +114,33 @@ function createEntry(options: SidebarEntryOptions): { entry: HTMLButtonElement;
101
114
  iconSpan.className = options.css['entryIcon'] ?? ''
102
115
  iconSpan.innerHTML = options.icon
103
116
  entry.append(iconSpan, labelSpan)
117
+ // Optional numeric badge (visible only when count > 0).
118
+ let badgeSpan: HTMLSpanElement | null = null
119
+ const applyBadge = (): void => {
120
+ if (options.badge === undefined) return
121
+ const count = options.badge.count()
122
+ if (count <= 0) {
123
+ badgeSpan?.remove()
124
+ badgeSpan = null
125
+ return
126
+ }
127
+ if (badgeSpan === null) {
128
+ badgeSpan = document.createElement('span')
129
+ badgeSpan.className = options.css['entryBadge'] ?? ''
130
+ entry.append(badgeSpan)
131
+ }
132
+ badgeSpan.textContent = String(count)
133
+ entry.setAttribute('data-has-badge', '')
134
+ }
104
135
  const applyLabel = (): void => {
105
136
  entry.setAttribute('aria-label', options.label())
106
137
  if (options.tooltip !== undefined) entry.setAttribute('title', options.tooltip())
107
138
  labelSpan.textContent = options.label()
108
139
  }
109
140
  applyLabel()
141
+ applyBadge()
110
142
  entry.addEventListener('click', options.onToggle)
111
- return { entry, applyLabel }
143
+ return { entry, applyLabel, applyBadge }
112
144
  }
113
145
 
114
146
  /** Re-insert the entry after the New Session row (before the browser region). */
@@ -150,7 +182,7 @@ export function mountSidebarEntry(options: SidebarEntryOptions): () => void {
150
182
  if (typeof document !== 'undefined' && document.querySelector(options.rowSelector) !== null) {
151
183
  return () => {}
152
184
  }
153
- const { entry, applyLabel } = createEntry(options)
185
+ const { entry, applyLabel, applyBadge } = createEntry(options)
154
186
  let root: HTMLElement | undefined
155
187
  let placed = false
156
188
  let unsubscribeRefresh: (() => void) | undefined
@@ -162,6 +194,15 @@ export function mountSidebarEntry(options: SidebarEntryOptions): () => void {
162
194
  // its initial value and the next reload resolves it again.
163
195
  }
164
196
  }
197
+ let unsubscribeBadge: (() => void) | undefined
198
+ if (options.badge !== undefined) {
199
+ try {
200
+ unsubscribeBadge = options.badge.subscribe(applyBadge)
201
+ } catch {
202
+ // Same defensive rule as refresh: a throwing subscription must not
203
+ // break the mount; the badge keeps its initial value.
204
+ }
205
+ }
165
206
 
166
207
  const tryPlace = (): void => {
167
208
  if (root !== undefined && !root.isConnected) {
@@ -230,7 +271,7 @@ export function mountSidebarEntry(options: SidebarEntryOptions): () => void {
230
271
  waitObserver.disconnect()
231
272
  rootObserver.disconnect()
232
273
  unsubscribeRefresh?.()
233
- unsubscribeActive?.()
274
+ unsubscribeBadge?.()
234
275
  entry.remove()
235
276
  }
236
277
  }
@@ -247,6 +288,7 @@ export const HUAQIU_SIDEBAR_ENTRY_FAMILY: readonly string[] = [
247
288
  '[data-hqcg-footprint-entry]',
248
289
  '[data-hqcg-symbol-entry]',
249
290
  '[data-hqcg-schematic-entry]',
291
+ '[data-hq-plugin-update-entry]',
250
292
  ]
251
293
 
252
294
  // ---- host locale detection (shared by every family sidebar row) ----