mirror of
https://github.com/lWolvesl/antigravity-usage-monitor.git
synced 2026-11-26 17:09:28 +08:00
feat(panel): restore IDE mount compatibility, add composer dock mode, and refine chart tooltips
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* @fileoverview Bespoke CustomSelect dropdown component replacing native <select> / <option>.
|
||||
* Supports incremental option patching while open without losing focus or closing the menu.
|
||||
* Supports both single-select and multi-select (`multiple: true`) modes, with incremental
|
||||
* option patching while open without losing focus or closing the menu.
|
||||
*/
|
||||
|
||||
import { defineComponent } from '../../core/component.js';
|
||||
@@ -14,6 +15,40 @@ import {
|
||||
} from '../../core/domPatch.js';
|
||||
import { createSvgIcon, patchSvgIcon } from '../../icons/SvgIcon.js';
|
||||
|
||||
function normalizeMultiSelection(rawValue, options = [], allValue = '') {
|
||||
const arr = Array.isArray(rawValue)
|
||||
? rawValue
|
||||
: rawValue !== undefined && rawValue !== null && rawValue !== ''
|
||||
? [rawValue]
|
||||
: [];
|
||||
const clean = [];
|
||||
const seen = new Set();
|
||||
for (const item of arr) {
|
||||
const str = String(item ?? '');
|
||||
if (!str || seen.has(str)) continue;
|
||||
seen.add(str);
|
||||
clean.push(str);
|
||||
}
|
||||
if (allValue && (clean.length === 0 || clean.includes(allValue))) {
|
||||
return [allValue];
|
||||
}
|
||||
if (options.length > 0) {
|
||||
const validSet = new Set(options.map((o) => String(o.value)));
|
||||
const filtered = clean.filter((v) => validSet.has(v));
|
||||
if (filtered.length > 0) return filtered;
|
||||
return allValue ? [allValue] : [];
|
||||
}
|
||||
return clean;
|
||||
}
|
||||
|
||||
function sameStringArray(a, b) {
|
||||
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) return false;
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
if (a[i] !== b[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export const CustomSelect = defineComponent({
|
||||
name: 'CustomSelect',
|
||||
setup({ getProps, setProps, emit, onCleanup }) {
|
||||
@@ -49,9 +84,23 @@ export const CustomSelect = defineComponent({
|
||||
hidden: true,
|
||||
});
|
||||
|
||||
const initialClass = ['custom-select', getProps().className].filter(Boolean).join(' ');
|
||||
const initialClass = [
|
||||
'custom-select',
|
||||
getProps().multiple ? 'is-multi' : '',
|
||||
getProps().className,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(' ');
|
||||
const rootEl = h('div', { className: initialClass }, [triggerBtn, menuEl]);
|
||||
|
||||
function getSelectedSet() {
|
||||
const { multiple, modelValue, options = [], allValue = '' } = getProps();
|
||||
if (!multiple) {
|
||||
return new Set(modelValue !== undefined && modelValue !== null ? [modelValue] : []);
|
||||
}
|
||||
return new Set(normalizeMultiSelection(modelValue, options, allValue));
|
||||
}
|
||||
|
||||
function toggleOpen(nextOpen) {
|
||||
isOpen = Boolean(nextOpen);
|
||||
patchClasses(rootEl, { 'is-open': isOpen });
|
||||
@@ -60,10 +109,10 @@ export const CustomSelect = defineComponent({
|
||||
patchSvgIcon(chevronIcon, isOpen ? 'chevron-up' : 'chevron-down');
|
||||
if (isOpen) {
|
||||
const opts = getProps().options || [];
|
||||
const currentVal = getProps().modelValue;
|
||||
const selSet = getSelectedSet();
|
||||
focusedIndex = Math.max(
|
||||
0,
|
||||
opts.findIndex((o) => o.value === currentVal)
|
||||
opts.findIndex((o) => selSet.has(o.value))
|
||||
);
|
||||
renderMenuOptions();
|
||||
}
|
||||
@@ -71,11 +120,47 @@ export const CustomSelect = defineComponent({
|
||||
|
||||
function chooseOption(option) {
|
||||
if (!option) return;
|
||||
const prevVal = getProps().modelValue;
|
||||
toggleOpen(false);
|
||||
if (option.value !== prevVal) {
|
||||
setProps({ modelValue: option.value });
|
||||
emit('update:modelValue', option.value);
|
||||
const { multiple, options = [], allValue = '', collapseAll = false } = getProps();
|
||||
if (!multiple) {
|
||||
const prevVal = getProps().modelValue;
|
||||
toggleOpen(false);
|
||||
if (option.value !== prevVal) {
|
||||
setProps({ modelValue: option.value });
|
||||
emit('update:modelValue', option.value);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const prevList = normalizeMultiSelection(getProps().modelValue, options, allValue);
|
||||
const val = String(option.value);
|
||||
let nextList;
|
||||
if (allValue && val === allValue) {
|
||||
nextList = [allValue];
|
||||
} else {
|
||||
const base = prevList.filter((v) => v !== allValue);
|
||||
if (base.includes(val)) {
|
||||
const rest = base.filter((v) => v !== val);
|
||||
nextList = rest.length > 0 ? rest : allValue ? [allValue] : [val];
|
||||
} else {
|
||||
const addedSet = new Set([...base, val]);
|
||||
const ordered = options
|
||||
.filter((o) => String(o.value) !== allValue && addedSet.has(String(o.value)))
|
||||
.map((o) => String(o.value));
|
||||
nextList = ordered.length > 0 ? ordered : [...base, val];
|
||||
if (collapseAll && allValue) {
|
||||
const nonAllTotal = options.filter((o) => String(o.value) !== allValue).length;
|
||||
if (nonAllTotal > 0 && nextList.length >= nonAllTotal) {
|
||||
nextList = [allValue];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!sameStringArray(prevList, nextList)) {
|
||||
setProps({ modelValue: nextList });
|
||||
emit('update:modelValue', nextList);
|
||||
} else {
|
||||
renderMenuOptions();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,7 +186,7 @@ export const CustomSelect = defineComponent({
|
||||
renderMenuOptions();
|
||||
menuEl.children[focusedIndex]?.scrollIntoView?.({ block: 'nearest' });
|
||||
}
|
||||
} else if (e.key === 'Enter' && isOpen) {
|
||||
} else if ((e.key === 'Enter' || (e.key === ' ' && isOpen && getProps().multiple)) && isOpen) {
|
||||
e.preventDefault();
|
||||
const target = opts[focusedIndex];
|
||||
if (target) chooseOption(target);
|
||||
@@ -112,14 +197,20 @@ export const CustomSelect = defineComponent({
|
||||
}
|
||||
|
||||
function renderMenuOptions() {
|
||||
const { options = [], modelValue } = getProps();
|
||||
const { options = [], multiple = false } = getProps();
|
||||
const selSet = getSelectedSet();
|
||||
patchAttr(menuEl, 'aria-multiselectable', multiple ? 'true' : null);
|
||||
patchKeyedList(menuEl, options, {
|
||||
keyFn: (opt) => opt.value,
|
||||
sigFn: (opt, idx) =>
|
||||
`${opt.value}::${opt.label}::${opt.meta || ''}::${opt.value === modelValue}::${idx === focusedIndex}`,
|
||||
`${multiple}::${opt.value}::${opt.label}::${opt.meta || ''}::${selSet.has(opt.value)}::${idx === focusedIndex}`,
|
||||
createRow: (opt) => {
|
||||
const checkboxIcon = createSvgIcon('check', { size: 10 });
|
||||
const checkboxBox = h('span', { className: 'select-checkbox', hidden: true }, [
|
||||
checkboxIcon,
|
||||
]);
|
||||
const textSpan = h('span', { className: 'select-option-text' });
|
||||
const metaSpan = h('span', { className: 'text-muted' });
|
||||
const metaSpan = h('span', { className: 'text-muted select-option-meta' });
|
||||
const checkIcon = createSvgIcon('check', { size: 12 });
|
||||
const btn = h(
|
||||
'button',
|
||||
@@ -129,21 +220,26 @@ export const CustomSelect = defineComponent({
|
||||
className: 'select-option',
|
||||
onClick: (e) => {
|
||||
e.stopPropagation();
|
||||
const latestOpt = (getProps().options || []).find((o) => o.value === opt.value) || opt;
|
||||
const latestOpt =
|
||||
(getProps().options || []).find((o) => o.value === opt.value) || opt;
|
||||
chooseOption(latestOpt);
|
||||
},
|
||||
},
|
||||
[textSpan, metaSpan, checkIcon]
|
||||
[checkboxBox, textSpan, metaSpan, checkIcon]
|
||||
);
|
||||
return { el: btn, refs: { textSpan, metaSpan, checkIcon } };
|
||||
return { el: btn, refs: { checkboxBox, checkboxIcon, textSpan, metaSpan, checkIcon } };
|
||||
},
|
||||
updateRow: (refs, opt, idx, el) => {
|
||||
const selected = opt.value === modelValue;
|
||||
const isMulti = Boolean(getProps().multiple);
|
||||
const selected = selSet.has(opt.value);
|
||||
const focused = idx === focusedIndex;
|
||||
patchAttr(refs.checkboxBox, 'hidden', !isMulti);
|
||||
patchClasses(refs.checkboxBox, { 'is-checked': selected });
|
||||
patchAttr(refs.checkboxIcon, 'hidden', !selected);
|
||||
patchText(refs.textSpan, opt.label);
|
||||
patchText(refs.metaSpan, opt.meta || '');
|
||||
patchAttr(refs.metaSpan, 'hidden', !opt.meta);
|
||||
patchAttr(refs.checkIcon, 'hidden', !selected);
|
||||
patchAttr(refs.checkIcon, 'hidden', isMulti || !selected);
|
||||
patchClasses(el, {
|
||||
'is-selected': selected,
|
||||
'is-focused': focused,
|
||||
@@ -155,17 +251,51 @@ export const CustomSelect = defineComponent({
|
||||
}
|
||||
|
||||
function syncTriggerAndMenu() {
|
||||
const { options = [], modelValue, placeholder = '请选择', icon } = getProps();
|
||||
const {
|
||||
options = [],
|
||||
modelValue,
|
||||
placeholder = '请选择',
|
||||
icon,
|
||||
multiple = false,
|
||||
allValue = '',
|
||||
multiUnit = '项',
|
||||
} = getProps();
|
||||
patchClasses(rootEl, { 'is-multi': Boolean(multiple) });
|
||||
patchAttr(leadIcon, 'hidden', !icon);
|
||||
if (icon) {
|
||||
patchSvgIcon(leadIcon, icon);
|
||||
}
|
||||
const matchedOpt = options.find((o) => o.value === modelValue);
|
||||
const selectedOpt = matchedOpt || (!modelValue ? options[0] : undefined);
|
||||
const fallbackText = modelValue ? `${placeholder} (${String(modelValue).slice(0, 8)})` : placeholder;
|
||||
const labelText = selectedOpt ? selectedOpt.label : fallbackText;
|
||||
|
||||
let labelText = placeholder;
|
||||
let titleText = placeholder;
|
||||
|
||||
if (multiple) {
|
||||
const vals = normalizeMultiSelection(modelValue, options, allValue);
|
||||
if ((allValue && vals.includes(allValue)) || vals.length === 0) {
|
||||
const allOpt = options.find((o) => String(o.value) === allValue);
|
||||
labelText = allOpt ? allOpt.label : placeholder;
|
||||
titleText = labelText;
|
||||
} else if (vals.length === 1) {
|
||||
const singleOpt = options.find((o) => String(o.value) === vals[0]);
|
||||
labelText = singleOpt ? singleOpt.label : vals[0];
|
||||
titleText = labelText;
|
||||
} else {
|
||||
const names = vals.map((v) => options.find((o) => String(o.value) === v)?.label || v);
|
||||
labelText = `已选 ${vals.length} ${multiUnit}`;
|
||||
titleText = `已选 (${vals.length}):${names.join('、')}`;
|
||||
}
|
||||
} else {
|
||||
const matchedOpt = options.find((o) => o.value === modelValue);
|
||||
const selectedOpt = matchedOpt || (!modelValue ? options[0] : undefined);
|
||||
const fallbackText = modelValue
|
||||
? `${placeholder} (${String(modelValue).slice(0, 8)})`
|
||||
: placeholder;
|
||||
labelText = selectedOpt ? selectedOpt.label : fallbackText;
|
||||
titleText = labelText;
|
||||
}
|
||||
|
||||
patchText(triggerLabel, labelText);
|
||||
patchAttr(triggerBtn, 'title', labelText);
|
||||
patchAttr(triggerBtn, 'title', titleText);
|
||||
if (isOpen) {
|
||||
renderMenuOptions();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user