@huaqiu/dsh-sidebar-entry 0.5.22 → 0.6.1

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.mjs CHANGED
@@ -58,15 +58,40 @@ function createEntry(options) {
58
58
  applyBadge
59
59
  };
60
60
  }
61
- /** Re-insert the entry after the New Session row (before the browser region). */
61
+ /**
62
+ * Re-insert the entry after the New Session row, in the family's intended
63
+ * order.
64
+ *
65
+ * The `familySelectors` array order is the single source of truth for visual
66
+ * order, so the rendered order is stable regardless of which plugin mounts
67
+ * first or of observer callback order — a re-render can never swap two rows.
68
+ * (Earlier builds used the `position: 'before' | 'after'` convention, which
69
+ * clustered rows by mount order and let a late-mounting `before` row steal the
70
+ * lead. The array order supersedes it; `position` remains only as a fallback
71
+ * for a row whose selector is absent from the family list.)
72
+ */
62
73
  function placeEntry(root, entry, options) {
63
74
  const button = newSessionButton(root);
64
75
  if (button === void 0) return false;
65
76
  if (entry.parentElement !== root) {
66
77
  const row = button.closest("[class*=\"logoRow\"]");
67
78
  const base = row !== null && row.parentElement === root ? row : button;
68
- const family = Array.from(root.children).filter((el) => el instanceof HTMLElement && el.matches(options.familySelectors.join(", ")));
69
- const anchor = options.position === "before" ? family.length > 0 ? family[0] : base.nextElementSibling : family.length > 0 ? family[family.length - 1].nextElementSibling : base.nextElementSibling;
79
+ const order = options.familySelectors;
80
+ const present = Array.from(root.children).filter((el) => el instanceof HTMLElement && el.matches(order.join(", "))).map((el) => ({
81
+ el,
82
+ idx: order.findIndex((sel) => el.matches(sel))
83
+ }));
84
+ const selfIndex = order.indexOf(options.rowSelector);
85
+ let anchor;
86
+ if (selfIndex === -1) anchor = options.position === "before" ? present.length > 0 ? present[0].el : base.nextElementSibling : present.length > 0 ? present[present.length - 1].el.nextElementSibling : base.nextElementSibling;
87
+ else {
88
+ const later = present.find((member) => member.idx > selfIndex);
89
+ if (later !== void 0) anchor = later.el;
90
+ else {
91
+ const earlier = [...present].reverse().find((member) => member.idx < selfIndex);
92
+ anchor = earlier !== void 0 ? earlier.el.nextElementSibling : base.nextElementSibling;
93
+ }
94
+ }
70
95
  root.insertBefore(entry, anchor ?? null);
71
96
  }
72
97
  return true;
@@ -152,10 +177,11 @@ function mountSidebarEntry(options) {
152
177
  * family row means adding its selector here (and to no plugin-local list).
153
178
  */
154
179
  const HUAQIU_SIDEBAR_ENTRY_FAMILY = [
180
+ "[data-hq-plugin-update-entry]",
155
181
  "[data-hqcg-footprint-entry]",
156
182
  "[data-hqcg-symbol-entry]",
157
183
  "[data-hqcg-schematic-entry]",
158
- "[data-hq-plugin-update-entry]"
184
+ "[data-hq-dummy-entry]"
159
185
  ];
160
186
  /**
161
187
  * 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.22",
3
+ "version": "0.6.1",
4
4
  "type": "module",
5
5
  "main": "./lib/index.mjs",
6
6
  "types": "./lib/index.d.mts",
package/src/index.ts CHANGED
@@ -143,26 +143,51 @@ function createEntry(options: SidebarEntryOptions): {
143
143
  return { entry, applyLabel, applyBadge }
144
144
  }
145
145
 
146
- /** Re-insert the entry after the New Session row (before the browser region). */
146
+ /**
147
+ * Re-insert the entry after the New Session row, in the family's intended
148
+ * order.
149
+ *
150
+ * The `familySelectors` array order is the single source of truth for visual
151
+ * order, so the rendered order is stable regardless of which plugin mounts
152
+ * first or of observer callback order — a re-render can never swap two rows.
153
+ * (Earlier builds used the `position: 'before' | 'after'` convention, which
154
+ * clustered rows by mount order and let a late-mounting `before` row steal the
155
+ * lead. The array order supersedes it; `position` remains only as a fallback
156
+ * for a row whose selector is absent from the family list.)
157
+ */
147
158
  function placeEntry(root: HTMLElement, entry: HTMLButtonElement, options: SidebarEntryOptions): boolean {
148
159
  const button = newSessionButton(root)
149
160
  if (button === undefined) return false
150
161
  if (entry.parentElement !== root) {
151
- // Position relative to the family block (entries injected by sibling
152
- // plugins), never relative to transient logoRow geometry: every family
153
- // plugin that self-heals during a re-render then lands in the same
154
- // relative order, so the entries cannot swap positions regardless of
155
- // observer callback order or of shell wrapper changes. There is no
156
- // append-to-end fallback: appending at the end would randomly reorder
157
- // the block after a shell re-render.
158
162
  const row = button.closest('[class*="logoRow"]')
159
163
  const base = (row !== null && row.parentElement === root) ? row : button
160
- const family = Array.from(root.children).filter(
161
- (el): el is HTMLElement => el instanceof HTMLElement && el.matches(options.familySelectors.join(', ')),
162
- )
163
- const anchor = options.position === 'before'
164
- ? (family.length > 0 ? family[0] : base.nextElementSibling)
165
- : (family.length > 0 ? family[family.length - 1]!.nextElementSibling : base.nextElementSibling)
164
+
165
+ // Present family members, each tagged with its index in the intended
166
+ // order. The entry being placed is detached here, so it is never counted.
167
+ const order = options.familySelectors
168
+ const present = Array.from(root.children)
169
+ .filter((el): el is HTMLElement => el instanceof HTMLElement && el.matches(order.join(', ')))
170
+ .map((el) => ({ el, idx: order.findIndex((sel) => el.matches(sel)) }))
171
+
172
+ const selfIndex = order.indexOf(options.rowSelector)
173
+ let anchor: Node | null
174
+ if (selfIndex === -1) {
175
+ // Not part of the ordered family: keep the legacy position rule.
176
+ anchor = options.position === 'before'
177
+ ? (present.length > 0 ? present[0]!.el : base.nextElementSibling)
178
+ : (present.length > 0 ? present[present.length - 1]!.el.nextElementSibling : base.nextElementSibling)
179
+ } else {
180
+ // Insert before the first present member that should come AFTER this
181
+ // entry; otherwise insert after the last present member that should come
182
+ // BEFORE it; otherwise sit right after the New Session button.
183
+ const later = present.find((member) => member.idx > selfIndex)
184
+ if (later !== undefined) {
185
+ anchor = later.el
186
+ } else {
187
+ const earlier = [...present].reverse().find((member) => member.idx < selfIndex)
188
+ anchor = earlier !== undefined ? earlier.el.nextElementSibling : base.nextElementSibling
189
+ }
190
+ }
166
191
  root.insertBefore(entry, anchor ?? null)
167
192
  }
168
193
  return true
@@ -285,10 +310,13 @@ export function mountSidebarEntry(options: SidebarEntryOptions): () => void {
285
310
  * family row means adding its selector here (and to no plugin-local list).
286
311
  */
287
312
  export const HUAQIU_SIDEBAR_ENTRY_FAMILY: readonly string[] = [
313
+ // Plugin Updates leads the family block: it is the most frequently used
314
+ // toolbutton and carries the actionable-update badge, so it sits first.
315
+ '[data-hq-plugin-update-entry]',
288
316
  '[data-hqcg-footprint-entry]',
289
317
  '[data-hqcg-symbol-entry]',
290
318
  '[data-hqcg-schematic-entry]',
291
- '[data-hq-plugin-update-entry]',
319
+ '[data-hq-dummy-entry]',
292
320
  ]
293
321
 
294
322
  // ---- host locale detection (shared by every family sidebar row) ----