dsh-asuta-model-group-label 0.1.13 → 0.1.14

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.
Files changed (2) hide show
  1. package/client.js +44 -4
  2. package/package.json +1 -1
package/client.js CHANGED
@@ -69,6 +69,47 @@ window.__ModuleLoader__.load({
69
69
  try { localStorage.setItem(storageKey, JSON.stringify([...favorites])) } catch {}
70
70
  reconcile()
71
71
  }
72
+ function findModelSearch(menu) {
73
+ return [...menu.parentElement.querySelectorAll('[role="searchbox"][aria-controls]')]
74
+ .find(input => input.getAttribute('aria-controls') === menu.id)
75
+ }
76
+ function syncVisibleModelHighlight(menu, search) {
77
+ const top = menu.getBoundingClientRect().top + menu.clientTop
78
+ const bottom = top + menu.clientHeight
79
+ const rows = [...menu.querySelectorAll(rowSelector)].filter(row => !row.disabled)
80
+ const highlighted = document.getElementById(search.getAttribute('aria-activedescendant'))
81
+ const inView = (row, fully) => {
82
+ const bounds = row.getBoundingClientRect()
83
+ const heading = row.parentElement.querySelector(':scope > [data-menu-group-heading]')
84
+ const visibleTop = Math.max(top, heading?.getBoundingClientRect().bottom ?? top)
85
+ return fully ? bounds.top >= visibleTop && bounds.bottom <= bottom : bounds.bottom > visibleTop && bounds.top < bottom
86
+ }
87
+ if (rows.includes(highlighted) && inView(highlighted, true)) return
88
+ const visible = rows.find(row => inView(row, true)) ?? rows.find(row => inView(row, false))
89
+ if (!visible || visible === highlighted) return
90
+ // The native row's focus handler updates the model that Enter and Tab select.
91
+ visible.focus({ preventScroll: true })
92
+ search.focus({ preventScroll: true })
93
+ }
94
+ function removeFavorite(menu, item) {
95
+ const scrollTop = menu.scrollTop
96
+ const search = findModelSearch(menu)
97
+ if (item.line.contains(document.activeElement)) {
98
+ const choices = [...item.line.parentElement.querySelectorAll('.cdx-model-favorite-choice')]
99
+ const index = choices.indexOf(item.choice)
100
+ const next = choices.slice(index + 1).find(choice => !choice.disabled)
101
+ const previous = choices.slice(0, index).findLast(choice => !choice.disabled)
102
+ const focus = next ?? previous ?? search ?? item.data.row.nextElementSibling
103
+ // Transfer focus before removing the row so the host keeps the menu open.
104
+ focus?.focus({ preventScroll: true })
105
+ }
106
+ openingLists.get(menu)?.finish()
107
+ toggle(item.data.key)
108
+ menu.scrollTop = scrollTop
109
+ if (search === document.activeElement && !menu.querySelector('.cdx-model-favorite-choice:not(:disabled)')) {
110
+ syncVisibleModelHighlight(menu, search)
111
+ }
112
+ }
72
113
  function paint(star, key, label) {
73
114
  const selected = favorites.has(key), text = selected ? '★' : '☆'
74
115
  if (star.textContent !== text) star.textContent = text
@@ -213,7 +254,7 @@ window.__ModuleLoader__.load({
213
254
  paint(star, key, name)
214
255
  paintColor(row, color, key, name)
215
256
  const list = byMenu.get(menu) || []
216
- if (favorites.has(key)) list.push({ key, name, group: label.dataset.cdxModelGroup, row, star })
257
+ if (favorites.has(key)) list.push({ key, name, group: label.dataset.cdxModelGroup, row })
217
258
  byMenu.set(menu, list)
218
259
  }
219
260
  for (const [row, actions] of controls) if (!seen.has(row)) removeControls(row, actions)
@@ -240,7 +281,7 @@ window.__ModuleLoader__.load({
240
281
  line.className = 'cdx-model-favorite-row'; choice.type = 'button'; choice.className = 'cdx-model-favorite-choice'
241
282
  choice.setAttribute('role', 'menuitem')
242
283
  choice.addEventListener('click', () => item.data.row.click())
243
- const star = starButton(() => { item.data.star.focus(); toggle(item.data.key) })
284
+ const star = starButton(() => removeFavorite(menu, item))
244
285
  const color = colorButton(() => item.data.key)
245
286
  line.append(choice, star, color)
246
287
  item = { line, choice, star, color }; entry.rows.set(data.key, item)
@@ -300,8 +341,7 @@ window.__ModuleLoader__.load({
300
341
  if (!menu || menu.hidden) return
301
342
  const nativeRows = [...menu.querySelectorAll(rowSelector)].filter(row => controls.has(row) && !row.disabled)
302
343
  if (!nativeRows.length) return
303
- const search = [...menu.parentElement.querySelectorAll('[role="searchbox"][aria-controls]')]
304
- .find(input => input.getAttribute('aria-controls') === menu.id)
344
+ const search = findModelSearch(menu)
305
345
  const favorites = [...menu.querySelectorAll('.cdx-model-favorite-choice:not(:disabled)')]
306
346
  const fromControl = target.matches('.cdx-model-star,.cdx-model-color')
307
347
  const choice = fromControl ? (target.matches('.cdx-model-color') ? target.previousElementSibling?.previousElementSibling : target.previousElementSibling)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-asuta-model-group-label",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "type": "module",
5
5
  "description": "Show the active model group and model name in the composer. · 在输入栏的模型按钮显示分组名称/模型名称。",
6
6
  "exports": {