mirror of
https://github.com/lWolvesl/antigravity-usage-monitor.git
synced 2026-11-26 17:09:28 +08:00
feat(panel): upgrade modular dashboard UI, 6-segment context meter, and consumption analytics
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* @fileoverview 6-channel segmented context window progress bar updated via
|
||||
* CSS custom variables against the current model's automatic compaction baseline.
|
||||
*/
|
||||
|
||||
import { defineComponent } from '../../core/component.js';
|
||||
import { h, patchAttr, patchStyleVar } from '../../core/domPatch.js';
|
||||
|
||||
export const ContextSegmentMeter = defineComponent({
|
||||
name: 'ContextSegmentMeter',
|
||||
setup({ getProps }) {
|
||||
const segMessages = h('div', { className: 'progress-seg seg-messages' });
|
||||
const segSystemTools = h('div', { className: 'progress-seg seg-system-tools' });
|
||||
const segMcpTools = h('div', { className: 'progress-seg seg-mcp-tools' });
|
||||
const segSkills = h('div', { className: 'progress-seg seg-skills' });
|
||||
const segSystemPrompt = h('div', { className: 'progress-seg seg-system-prompt' });
|
||||
const segUserRules = h('div', { className: 'progress-seg seg-user-rules' });
|
||||
|
||||
const trackEl = h(
|
||||
'div',
|
||||
{
|
||||
className: 'progress-track',
|
||||
role: 'progressbar',
|
||||
'aria-valuemin': '0',
|
||||
'aria-valuemax': '100',
|
||||
},
|
||||
[segMessages, segSystemTools, segMcpTools, segSkills, segSystemPrompt, segUserRules]
|
||||
);
|
||||
|
||||
function syncMeter() {
|
||||
const ctx = getProps().context || {};
|
||||
const limit = Math.max(1, Number(ctx.compactionLimitTokens || ctx.limitTokens || 256000));
|
||||
const pctMessages = Math.min(100, ((ctx.messagesTokens || 0) / limit) * 100);
|
||||
const pctSystemTools = Math.min(100, ((ctx.systemToolsTokens || 0) / limit) * 100);
|
||||
const pctMcpTools = Math.min(100, ((ctx.mcpToolsTokens || 0) / limit) * 100);
|
||||
const pctSkills = Math.min(100, ((ctx.skillsTokens || 0) / limit) * 100);
|
||||
const pctSystemPrompt = Math.min(100, ((ctx.systemPromptTokens || 0) / limit) * 100);
|
||||
const pctUserRules = Math.min(100, ((ctx.userRulesTokens || 0) / limit) * 100);
|
||||
|
||||
patchStyleVar(segMessages, '--seg-w', `${pctMessages.toFixed(2)}%`);
|
||||
patchStyleVar(segSystemTools, '--seg-w', `${pctSystemTools.toFixed(2)}%`);
|
||||
patchStyleVar(segMcpTools, '--seg-w', `${pctMcpTools.toFixed(2)}%`);
|
||||
patchStyleVar(segSkills, '--seg-w', `${pctSkills.toFixed(2)}%`);
|
||||
patchStyleVar(segSystemPrompt, '--seg-w', `${pctSystemPrompt.toFixed(2)}%`);
|
||||
patchStyleVar(segUserRules, '--seg-w', `${pctUserRules.toFixed(2)}%`);
|
||||
patchAttr(trackEl, 'aria-valuenow', Math.round(ctx.compactionUsedPct ?? ctx.usedPct ?? 0));
|
||||
}
|
||||
|
||||
syncMeter();
|
||||
|
||||
return {
|
||||
el: trackEl,
|
||||
refs: {
|
||||
segMessages,
|
||||
segSystemTools,
|
||||
segMcpTools,
|
||||
segSkills,
|
||||
segSystemPrompt,
|
||||
segUserRules,
|
||||
},
|
||||
onPropsChange: syncMeter,
|
||||
};
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,282 @@
|
||||
/**
|
||||
* @fileoverview Bespoke CustomDateTimePicker calendar matrix and hour/minute stepper.
|
||||
* Replaces native <input type="datetime-local"> and emits Unix epoch ms.
|
||||
*/
|
||||
|
||||
import { defineComponent } from '../../core/component.js';
|
||||
import { h, patchAttr, patchClasses, patchKeyedList, patchText } from '../../core/domPatch.js';
|
||||
import { formatCompactDateTime } from '../../core/formatters.js';
|
||||
import { createSvgIcon } from '../../icons/SvgIcon.js';
|
||||
|
||||
const WEEKDAYS = ['一', '二', '三', '四', '五', '六', '日'];
|
||||
|
||||
function buildCalendarDays(viewYear, viewMonth, selectedDate) {
|
||||
const firstDay = new Date(viewYear, viewMonth, 1);
|
||||
// Convert Sunday=0..Saturday=6 into Monday=0..Sunday=6
|
||||
const startOffset = (firstDay.getDay() + 6) % 7;
|
||||
const cells = [];
|
||||
|
||||
for (let i = 0; i < 42; i++) {
|
||||
const cellDate = new Date(viewYear, viewMonth, 1 - startOffset + i);
|
||||
const y = cellDate.getFullYear();
|
||||
const m = cellDate.getMonth();
|
||||
const d = cellDate.getDate();
|
||||
const isCurrentMonth = m === viewMonth;
|
||||
const isSelected =
|
||||
selectedDate &&
|
||||
selectedDate.getFullYear() === y &&
|
||||
selectedDate.getMonth() === m &&
|
||||
selectedDate.getDate() === d;
|
||||
|
||||
cells.push({
|
||||
key: `${y}-${m}-${d}`,
|
||||
year: y,
|
||||
month: m,
|
||||
day: d,
|
||||
isCurrentMonth,
|
||||
isSelected,
|
||||
});
|
||||
}
|
||||
return cells;
|
||||
}
|
||||
|
||||
export const CustomDateTimePicker = defineComponent({
|
||||
name: 'CustomDateTimePicker',
|
||||
setup({ getProps, emit, onCleanup }) {
|
||||
let isOpen = false;
|
||||
const initialDate = new Date(getProps().modelValue || Date.now());
|
||||
let viewYear = initialDate.getFullYear();
|
||||
let viewMonth = initialDate.getMonth();
|
||||
|
||||
const triggerText = h('span', { className: 'text-mono' });
|
||||
const triggerBtn = h(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
className: 'dt-trigger',
|
||||
'aria-expanded': 'false',
|
||||
onClick: (e) => {
|
||||
e.stopPropagation();
|
||||
togglePopover(!isOpen);
|
||||
},
|
||||
},
|
||||
[createSvgIcon('calendar', { size: 13 }), triggerText]
|
||||
);
|
||||
|
||||
const monthTitle = h('span', { className: 'text-mono' });
|
||||
const prevMonthBtn = h(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
className: 'icon-btn',
|
||||
'aria-label': '上个月',
|
||||
onClick: (e) => {
|
||||
e.stopPropagation();
|
||||
viewMonth -= 1;
|
||||
if (viewMonth < 0) {
|
||||
viewMonth = 11;
|
||||
viewYear -= 1;
|
||||
}
|
||||
renderCalendar();
|
||||
},
|
||||
},
|
||||
[createSvgIcon('chevron-left', { size: 12 })]
|
||||
);
|
||||
|
||||
const nextMonthBtn = h(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
className: 'icon-btn',
|
||||
'aria-label': '下个月',
|
||||
onClick: (e) => {
|
||||
e.stopPropagation();
|
||||
viewMonth += 1;
|
||||
if (viewMonth > 11) {
|
||||
viewMonth = 0;
|
||||
viewYear += 1;
|
||||
}
|
||||
renderCalendar();
|
||||
},
|
||||
},
|
||||
[createSvgIcon('chevron-right', { size: 12 })]
|
||||
);
|
||||
|
||||
const navBar = h('div', { className: 'dt-nav' }, [prevMonthBtn, monthTitle, nextMonthBtn]);
|
||||
|
||||
const weekdayRow = h(
|
||||
'div',
|
||||
{ className: 'dt-weekdays' },
|
||||
WEEKDAYS.map((w) => h('span', { className: 'dt-weekday', text: w }))
|
||||
);
|
||||
|
||||
const daysGrid = h('div', { className: 'dt-days' });
|
||||
|
||||
const hourValSpan = h('strong', { className: 'text-mono', text: '00' });
|
||||
const minValSpan = h('strong', { className: 'text-mono', text: '00' });
|
||||
|
||||
function adjustTime(part, delta) {
|
||||
const cur = new Date(getProps().modelValue || Date.now());
|
||||
if (part === 'hour') {
|
||||
cur.setHours((cur.getHours() + delta + 24) % 24);
|
||||
} else {
|
||||
cur.setMinutes((cur.getMinutes() + delta + 60) % 60);
|
||||
}
|
||||
const nextMs = cur.getTime();
|
||||
viewYear = cur.getFullYear();
|
||||
viewMonth = cur.getMonth();
|
||||
emit('update:modelValue', nextMs);
|
||||
}
|
||||
|
||||
const timeRow = h('div', { className: 'dt-time-row' }, [
|
||||
h('span', { className: 'text-muted', text: '时 / 分' }),
|
||||
h('div', { className: 'dt-time-controls' }, [
|
||||
h('div', { className: 'dt-mini-step' }, [
|
||||
h('button', {
|
||||
type: 'button',
|
||||
className: 'dt-mini-btn',
|
||||
text: '-',
|
||||
onClick: (e) => {
|
||||
e.stopPropagation();
|
||||
adjustTime('hour', -1);
|
||||
},
|
||||
}),
|
||||
hourValSpan,
|
||||
h('button', {
|
||||
type: 'button',
|
||||
className: 'dt-mini-btn',
|
||||
text: '+',
|
||||
onClick: (e) => {
|
||||
e.stopPropagation();
|
||||
adjustTime('hour', 1);
|
||||
},
|
||||
}),
|
||||
]),
|
||||
h('span', { text: ':' }),
|
||||
h('div', { className: 'dt-mini-step' }, [
|
||||
h('button', {
|
||||
type: 'button',
|
||||
className: 'dt-mini-btn',
|
||||
text: '-',
|
||||
onClick: (e) => {
|
||||
e.stopPropagation();
|
||||
adjustTime('minute', -5);
|
||||
},
|
||||
}),
|
||||
minValSpan,
|
||||
h('button', {
|
||||
type: 'button',
|
||||
className: 'dt-mini-btn',
|
||||
text: '+',
|
||||
onClick: (e) => {
|
||||
e.stopPropagation();
|
||||
adjustTime('minute', 5);
|
||||
},
|
||||
}),
|
||||
]),
|
||||
]),
|
||||
]);
|
||||
|
||||
const popoverEl = h(
|
||||
'div',
|
||||
{
|
||||
className: 'dt-popover',
|
||||
hidden: true,
|
||||
},
|
||||
[navBar, weekdayRow, daysGrid, timeRow]
|
||||
);
|
||||
|
||||
const rootEl = h('div', { className: 'datetime-picker' }, [triggerBtn, popoverEl]);
|
||||
|
||||
function togglePopover(nextOpen) {
|
||||
isOpen = Boolean(nextOpen);
|
||||
patchAttr(popoverEl, 'hidden', !isOpen);
|
||||
patchAttr(triggerBtn, 'aria-expanded', isOpen ? 'true' : 'false');
|
||||
if (isOpen) {
|
||||
const cur = new Date(getProps().modelValue || Date.now());
|
||||
viewYear = cur.getFullYear();
|
||||
viewMonth = cur.getMonth();
|
||||
renderCalendar();
|
||||
}
|
||||
}
|
||||
|
||||
function selectDay(cell) {
|
||||
const cur = new Date(getProps().modelValue || Date.now());
|
||||
cur.setFullYear(cell.year, cell.month, cell.day);
|
||||
const nextMs = cur.getTime();
|
||||
viewYear = cell.year;
|
||||
viewMonth = cell.month;
|
||||
emit('update:modelValue', nextMs);
|
||||
}
|
||||
|
||||
function renderCalendar() {
|
||||
const cur = new Date(getProps().modelValue || Date.now());
|
||||
const pad = (n) => String(n).padStart(2, '0');
|
||||
patchText(monthTitle, `${viewYear}-${pad(viewMonth + 1)}`);
|
||||
patchText(hourValSpan, pad(cur.getHours()));
|
||||
patchText(minValSpan, pad(cur.getMinutes()));
|
||||
|
||||
const cells = buildCalendarDays(viewYear, viewMonth, cur);
|
||||
patchKeyedList(daysGrid, cells, {
|
||||
keyFn: (c) => c.key,
|
||||
sigFn: (c) => `${c.key}:${c.isCurrentMonth}:${c.isSelected}`,
|
||||
createRow: (cell) => {
|
||||
const btn = h('button', {
|
||||
type: 'button',
|
||||
className: 'dt-day',
|
||||
onClick: (e) => {
|
||||
e.stopPropagation();
|
||||
selectDay(cell);
|
||||
},
|
||||
});
|
||||
return { el: btn, refs: {} };
|
||||
},
|
||||
updateRow: (_refs, cell, _idx, el) => {
|
||||
patchText(el, cell.day);
|
||||
patchClasses(el, {
|
||||
'is-selected': cell.isSelected,
|
||||
'is-outside': !cell.isCurrentMonth,
|
||||
});
|
||||
patchAttr(el, 'data-date-key', cell.key);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function syncDisplay() {
|
||||
const ms = Number(getProps().modelValue || Date.now());
|
||||
patchText(triggerText, formatCompactDateTime(ms));
|
||||
if (isOpen) {
|
||||
renderCalendar();
|
||||
}
|
||||
}
|
||||
|
||||
const handleDocClick = (e) => {
|
||||
if (isOpen && !rootEl.contains(e.target)) {
|
||||
togglePopover(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (typeof document !== 'undefined' && document.addEventListener) {
|
||||
document.addEventListener('click', handleDocClick);
|
||||
onCleanup(() => document.removeEventListener('click', handleDocClick));
|
||||
}
|
||||
|
||||
syncDisplay();
|
||||
|
||||
return {
|
||||
el: rootEl,
|
||||
refs: { triggerBtn, triggerText, popoverEl, daysGrid, hourValSpan, minValSpan },
|
||||
onPropsChange: syncDisplay,
|
||||
getValue: () => Number(getProps().modelValue || 0),
|
||||
methods: {
|
||||
open: () => togglePopover(true),
|
||||
close: () => togglePopover(false),
|
||||
selectDayByOffset: (daysDelta) => {
|
||||
const cur = new Date(getProps().modelValue || Date.now());
|
||||
cur.setDate(cur.getDate() + daysDelta);
|
||||
emit('update:modelValue', cur.getTime());
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* @fileoverview Bespoke CustomNumberInput spinbutton & precision stepper.
|
||||
* Replaces native <input type="number"> with custom SVG step controls,
|
||||
* keyboard ArrowUp/ArrowDown (+Shift 10x), and min/max clamping.
|
||||
*/
|
||||
|
||||
import { defineComponent } from '../../core/component.js';
|
||||
import { h, patchAttr } from '../../core/domPatch.js';
|
||||
import { createSvgIcon } from '../../icons/SvgIcon.js';
|
||||
|
||||
export const CustomNumberInput = defineComponent({
|
||||
name: 'CustomNumberInput',
|
||||
setup({ getProps, emit }) {
|
||||
function clampValue(raw) {
|
||||
const { min = -Infinity, max = Infinity, precision = 0 } = getProps();
|
||||
const num = Number(raw);
|
||||
if (!Number.isFinite(num)) {
|
||||
return Number(getProps().modelValue ?? (Number.isFinite(min) ? min : 0));
|
||||
}
|
||||
const clamped = Math.min(max, Math.max(min, num));
|
||||
return precision > 0 ? Number(clamped.toFixed(precision)) : Math.round(clamped);
|
||||
}
|
||||
|
||||
function applyStep(direction, multiplier = 1) {
|
||||
const { step = 1, modelValue = 0 } = getProps();
|
||||
const current = Number(modelValue) || 0;
|
||||
const next = clampValue(current + direction * step * multiplier);
|
||||
syncFieldDisplay(next);
|
||||
if (next !== current) {
|
||||
emit('update:modelValue', next);
|
||||
}
|
||||
}
|
||||
|
||||
const decBtn = h(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
className: 'num-step-btn',
|
||||
'aria-label': '减少数值',
|
||||
onClick: (e) => {
|
||||
e.preventDefault();
|
||||
applyStep(-1, e.shiftKey ? 10 : 1);
|
||||
},
|
||||
},
|
||||
[createSvgIcon('minus', { size: 12 })]
|
||||
);
|
||||
|
||||
const incBtn = h(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
className: 'num-step-btn',
|
||||
'aria-label': '增加数值',
|
||||
onClick: (e) => {
|
||||
e.preventDefault();
|
||||
applyStep(1, e.shiftKey ? 10 : 1);
|
||||
},
|
||||
},
|
||||
[createSvgIcon('plus', { size: 12 })]
|
||||
);
|
||||
|
||||
const textField = h('input', {
|
||||
type: 'text',
|
||||
inputmode: 'decimal',
|
||||
role: 'spinbutton',
|
||||
className: 'num-field',
|
||||
onKeydown: (e) => {
|
||||
if (e.key === 'ArrowUp') {
|
||||
e.preventDefault();
|
||||
applyStep(1, e.shiftKey ? 10 : 1);
|
||||
} else if (e.key === 'ArrowDown') {
|
||||
e.preventDefault();
|
||||
applyStep(-1, e.shiftKey ? 10 : 1);
|
||||
} else if (e.key === 'Enter') {
|
||||
e.preventDefault();
|
||||
commitManualInput();
|
||||
}
|
||||
},
|
||||
onBlur: () => commitManualInput(),
|
||||
});
|
||||
|
||||
function commitManualInput() {
|
||||
const rawText = String(textField.value || '').replace(/[^0-9.-]/g, '');
|
||||
const next = clampValue(rawText);
|
||||
const prev = Number(getProps().modelValue);
|
||||
syncFieldDisplay(next);
|
||||
if (next !== prev) {
|
||||
emit('update:modelValue', next);
|
||||
}
|
||||
}
|
||||
|
||||
function syncFieldDisplay(val = getProps().modelValue) {
|
||||
const { min, max } = getProps();
|
||||
const normalized = clampValue(val);
|
||||
if (textField.value !== String(normalized)) {
|
||||
textField.value = String(normalized);
|
||||
}
|
||||
patchAttr(textField, 'aria-valuenow', normalized);
|
||||
if (Number.isFinite(min)) patchAttr(textField, 'aria-valuemin', min);
|
||||
if (Number.isFinite(max)) patchAttr(textField, 'aria-valuemax', max);
|
||||
}
|
||||
|
||||
const rootEl = h('div', { className: 'number-input' }, [decBtn, textField, incBtn]);
|
||||
syncFieldDisplay();
|
||||
|
||||
return {
|
||||
el: rootEl,
|
||||
refs: { decBtn, incBtn, textField },
|
||||
onPropsChange: () => syncFieldDisplay(),
|
||||
getValue: () => clampValue(textField.value),
|
||||
methods: {
|
||||
stepUp: (mult = 1) => applyStep(1, mult),
|
||||
stepDown: (mult = 1) => applyStep(-1, mult),
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,164 @@
|
||||
/**
|
||||
* @fileoverview Bespoke custom scrollbar component (零原生滚动条).
|
||||
* Hides native OS scrollbars completely and renders a precision 4px floating
|
||||
* rail + GPU-translated thumb with drag, track-jump, and modelValue ratio emit.
|
||||
*/
|
||||
|
||||
import { defineComponent } from '../../core/component.js';
|
||||
import { h, patchClasses, patchStyleVar } from '../../core/domPatch.js';
|
||||
|
||||
export const CustomScrollbar = defineComponent({
|
||||
name: 'CustomScrollbar',
|
||||
setup({ getProps, emit, onCleanup }) {
|
||||
const contentEl = getProps().contentEl || h('div');
|
||||
contentEl.classList.add('custom-scroll-content');
|
||||
|
||||
const thumbEl = h('div', { className: 'scrollbar-thumb' });
|
||||
const railEl = h(
|
||||
'div',
|
||||
{
|
||||
className: 'scrollbar-rail is-inactive',
|
||||
'aria-hidden': 'true',
|
||||
},
|
||||
[thumbEl]
|
||||
);
|
||||
|
||||
const hostEl = h(
|
||||
'div',
|
||||
{
|
||||
className: `custom-scroll-host ${getProps().className || ''}`.trim(),
|
||||
},
|
||||
[contentEl, railEl]
|
||||
);
|
||||
|
||||
let isDragging = false;
|
||||
let dragStartY = 0;
|
||||
let dragStartScrollTop = 0;
|
||||
|
||||
function syncThumb() {
|
||||
const scrollHeight = Number(contentEl.scrollHeight || 0);
|
||||
const clientHeight = Number(contentEl.clientHeight || 0);
|
||||
const scrollTop = Number(contentEl.scrollTop || 0);
|
||||
const overflow = scrollHeight - clientHeight;
|
||||
|
||||
if (overflow <= 1 || clientHeight <= 0) {
|
||||
patchClasses(railEl, { 'is-inactive': true });
|
||||
patchStyleVar(thumbEl, '--thumb-h', '0px');
|
||||
patchStyleVar(thumbEl, '--thumb-y', '0px');
|
||||
return;
|
||||
}
|
||||
|
||||
patchClasses(railEl, { 'is-inactive': false });
|
||||
const railHeight = Math.max(24, clientHeight - 4);
|
||||
const thumbHeight = Math.max(24, Math.round((clientHeight / scrollHeight) * railHeight));
|
||||
const maxThumbTop = Math.max(0, railHeight - thumbHeight);
|
||||
const ratio = Math.max(0, Math.min(1, scrollTop / overflow));
|
||||
const thumbTop = Math.round(ratio * maxThumbTop);
|
||||
|
||||
patchStyleVar(thumbEl, '--thumb-h', `${thumbHeight}px`);
|
||||
patchStyleVar(thumbEl, '--thumb-y', `${thumbTop}px`);
|
||||
}
|
||||
|
||||
function onContentScroll() {
|
||||
syncThumb();
|
||||
const overflow = (contentEl.scrollHeight || 0) - (contentEl.clientHeight || 0);
|
||||
if (overflow > 1) {
|
||||
const ratio = +Math.max(0, Math.min(1, (contentEl.scrollTop || 0) / overflow)).toFixed(3);
|
||||
emit('update:modelValue', ratio);
|
||||
}
|
||||
}
|
||||
|
||||
function onDocMouseMove(e) {
|
||||
if (!isDragging) return;
|
||||
const clientHeight = Number(contentEl.clientHeight || 0);
|
||||
const scrollHeight = Number(contentEl.scrollHeight || 0);
|
||||
const overflow = scrollHeight - clientHeight;
|
||||
if (overflow <= 1) return;
|
||||
|
||||
const railHeight = Math.max(24, clientHeight - 4);
|
||||
const thumbHeight = Math.max(24, Math.round((clientHeight / scrollHeight) * railHeight));
|
||||
const maxThumbTop = Math.max(1, railHeight - thumbHeight);
|
||||
|
||||
const deltaY = (e.clientY || 0) - dragStartY;
|
||||
const scrollDelta = (deltaY / maxThumbTop) * overflow;
|
||||
contentEl.scrollTop = Math.max(0, Math.min(overflow, dragStartScrollTop + scrollDelta));
|
||||
syncThumb();
|
||||
}
|
||||
|
||||
function onDocMouseUp() {
|
||||
if (!isDragging) return;
|
||||
isDragging = false;
|
||||
patchClasses(railEl, { 'is-dragging': false });
|
||||
if (typeof document !== 'undefined') {
|
||||
document.removeEventListener('mousemove', onDocMouseMove);
|
||||
document.removeEventListener('mouseup', onDocMouseUp);
|
||||
}
|
||||
}
|
||||
|
||||
thumbEl.addEventListener('mousedown', (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
isDragging = true;
|
||||
dragStartY = e.clientY || 0;
|
||||
dragStartScrollTop = Number(contentEl.scrollTop || 0);
|
||||
patchClasses(railEl, { 'is-dragging': true });
|
||||
if (typeof document !== 'undefined') {
|
||||
document.addEventListener('mousemove', onDocMouseMove);
|
||||
document.addEventListener('mouseup', onDocMouseUp);
|
||||
}
|
||||
});
|
||||
|
||||
railEl.addEventListener('mousedown', (e) => {
|
||||
if (e.target === thumbEl) return;
|
||||
e.preventDefault();
|
||||
const rect = railEl.getBoundingClientRect ? railEl.getBoundingClientRect() : null;
|
||||
if (!rect || rect.height <= 0) return;
|
||||
const clickOffset = (e.clientY || 0) - rect.top;
|
||||
const ratio = Math.max(0, Math.min(1, clickOffset / rect.height));
|
||||
const overflow = Math.max(0, (contentEl.scrollHeight || 0) - (contentEl.clientHeight || 0));
|
||||
contentEl.scrollTop = Math.round(ratio * overflow);
|
||||
syncThumb();
|
||||
});
|
||||
|
||||
contentEl.addEventListener('scroll', onContentScroll);
|
||||
hostEl.addEventListener('mouseenter', syncThumb);
|
||||
|
||||
let resizeObserver = null;
|
||||
if (typeof ResizeObserver !== 'undefined') {
|
||||
resizeObserver = new ResizeObserver(() => syncThumb());
|
||||
resizeObserver.observe(hostEl);
|
||||
resizeObserver.observe(contentEl);
|
||||
}
|
||||
|
||||
syncThumb();
|
||||
|
||||
onCleanup(() => {
|
||||
onDocMouseUp();
|
||||
contentEl.removeEventListener('scroll', onContentScroll);
|
||||
hostEl.removeEventListener('mouseenter', syncThumb);
|
||||
if (resizeObserver) {
|
||||
resizeObserver.disconnect();
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
el: hostEl,
|
||||
refs: { contentEl, railEl, thumbEl },
|
||||
onPropsChange: syncThumb,
|
||||
methods: {
|
||||
refresh() {
|
||||
syncThumb();
|
||||
if (typeof queueMicrotask === 'function') {
|
||||
queueMicrotask(syncThumb);
|
||||
}
|
||||
},
|
||||
scrollToRatio(ratio) {
|
||||
const r = Math.max(0, Math.min(1, Number(ratio || 0)));
|
||||
const overflow = Math.max(0, (contentEl.scrollHeight || 0) - (contentEl.clientHeight || 0));
|
||||
contentEl.scrollTop = Math.round(r * overflow);
|
||||
syncThumb();
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,182 @@
|
||||
/**
|
||||
* @fileoverview Bespoke CustomSelect dropdown component replacing native <select> / <option>.
|
||||
* Supports incremental option patching while open without losing focus or closing the menu.
|
||||
*/
|
||||
|
||||
import { defineComponent } from '../../core/component.js';
|
||||
import { h, patchAttr, patchClasses, patchKeyedList, patchText } from '../../core/domPatch.js';
|
||||
import { createSvgIcon, patchSvgIcon } from '../../icons/SvgIcon.js';
|
||||
|
||||
export const CustomSelect = defineComponent({
|
||||
name: 'CustomSelect',
|
||||
setup({ getProps, emit, onCleanup }) {
|
||||
let isOpen = false;
|
||||
let focusedIndex = -1;
|
||||
|
||||
const leadIcon = createSvgIcon(getProps().icon || 'bolt', { size: 13 });
|
||||
const triggerLabel = h('span', { className: 'select-label' });
|
||||
const chevronIcon = createSvgIcon('chevron-down', { size: 13 });
|
||||
|
||||
const triggerBtn = h(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
className: 'select-trigger',
|
||||
role: 'combobox',
|
||||
'aria-expanded': 'false',
|
||||
'aria-haspopup': 'listbox',
|
||||
onClick: (e) => {
|
||||
e.stopPropagation();
|
||||
toggleOpen(!isOpen);
|
||||
},
|
||||
onKeydown: handleKeydown,
|
||||
},
|
||||
[leadIcon, triggerLabel, chevronIcon]
|
||||
);
|
||||
|
||||
const menuEl = h('div', {
|
||||
className: 'select-menu',
|
||||
role: 'listbox',
|
||||
hidden: true,
|
||||
});
|
||||
|
||||
const rootEl = h('div', { className: 'custom-select' }, [triggerBtn, menuEl]);
|
||||
|
||||
function toggleOpen(nextOpen) {
|
||||
isOpen = Boolean(nextOpen);
|
||||
patchClasses(rootEl, { 'is-open': isOpen });
|
||||
patchAttr(triggerBtn, 'aria-expanded', isOpen ? 'true' : 'false');
|
||||
patchAttr(menuEl, 'hidden', !isOpen);
|
||||
patchSvgIcon(chevronIcon, isOpen ? 'chevron-up' : 'chevron-down');
|
||||
if (isOpen) {
|
||||
const opts = getProps().options || [];
|
||||
const currentVal = getProps().modelValue;
|
||||
focusedIndex = Math.max(
|
||||
0,
|
||||
opts.findIndex((o) => o.value === currentVal)
|
||||
);
|
||||
renderMenuOptions();
|
||||
}
|
||||
}
|
||||
|
||||
function chooseOption(option) {
|
||||
if (!option) return;
|
||||
const prevVal = getProps().modelValue;
|
||||
toggleOpen(false);
|
||||
if (option.value !== prevVal) {
|
||||
emit('update:modelValue', option.value);
|
||||
}
|
||||
}
|
||||
|
||||
function handleKeydown(e) {
|
||||
const opts = getProps().options || [];
|
||||
if (!opts.length) return;
|
||||
|
||||
if (e.key === 'ArrowDown') {
|
||||
e.preventDefault();
|
||||
if (!isOpen) {
|
||||
toggleOpen(true);
|
||||
} else {
|
||||
focusedIndex = (focusedIndex + 1) % opts.length;
|
||||
renderMenuOptions();
|
||||
}
|
||||
} else if (e.key === 'ArrowUp') {
|
||||
e.preventDefault();
|
||||
if (!isOpen) {
|
||||
toggleOpen(true);
|
||||
} else {
|
||||
focusedIndex = (focusedIndex - 1 + opts.length) % opts.length;
|
||||
renderMenuOptions();
|
||||
}
|
||||
} else if (e.key === 'Enter' && isOpen) {
|
||||
e.preventDefault();
|
||||
const target = opts[focusedIndex];
|
||||
if (target) chooseOption(target);
|
||||
} else if (e.key === 'Escape' && isOpen) {
|
||||
e.preventDefault();
|
||||
toggleOpen(false);
|
||||
}
|
||||
}
|
||||
|
||||
function renderMenuOptions() {
|
||||
const { options = [], modelValue } = getProps();
|
||||
patchKeyedList(menuEl, options, {
|
||||
keyFn: (opt) => opt.value,
|
||||
sigFn: (opt, idx) =>
|
||||
`${opt.value}::${opt.label}::${opt.meta || ''}::${opt.value === modelValue}::${idx === focusedIndex}`,
|
||||
createRow: (opt) => {
|
||||
const textSpan = h('span', { className: 'select-option-text' });
|
||||
const metaSpan = h('span', { className: 'text-muted' });
|
||||
const checkIcon = createSvgIcon('check', { size: 12 });
|
||||
const btn = h(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
role: 'option',
|
||||
className: 'select-option',
|
||||
onClick: (e) => {
|
||||
e.stopPropagation();
|
||||
const latestOpt = (getProps().options || []).find((o) => o.value === opt.value) || opt;
|
||||
chooseOption(latestOpt);
|
||||
},
|
||||
},
|
||||
[textSpan, metaSpan, checkIcon]
|
||||
);
|
||||
return { el: btn, refs: { textSpan, metaSpan, checkIcon } };
|
||||
},
|
||||
updateRow: (refs, opt, idx, el) => {
|
||||
const selected = opt.value === modelValue;
|
||||
const focused = idx === focusedIndex;
|
||||
patchText(refs.textSpan, opt.label);
|
||||
patchText(refs.metaSpan, opt.meta || '');
|
||||
patchAttr(refs.metaSpan, 'hidden', !opt.meta);
|
||||
patchAttr(refs.checkIcon, 'hidden', !selected);
|
||||
patchClasses(el, {
|
||||
'is-selected': selected,
|
||||
'is-focused': focused,
|
||||
});
|
||||
patchAttr(el, 'aria-selected', selected ? 'true' : 'false');
|
||||
patchAttr(el, 'data-value', opt.value);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function syncTriggerAndMenu() {
|
||||
const { options = [], modelValue, placeholder = '请选择', icon = 'bolt' } = getProps();
|
||||
patchSvgIcon(leadIcon, icon);
|
||||
const selectedOpt = options.find((o) => o.value === modelValue) || options[0];
|
||||
patchText(triggerLabel, selectedOpt ? selectedOpt.label : placeholder);
|
||||
if (isOpen) {
|
||||
renderMenuOptions();
|
||||
}
|
||||
}
|
||||
|
||||
const handleDocClick = (e) => {
|
||||
if (isOpen && !rootEl.contains(e.target)) {
|
||||
toggleOpen(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (typeof document !== 'undefined' && document.addEventListener) {
|
||||
document.addEventListener('click', handleDocClick);
|
||||
onCleanup(() => document.removeEventListener('click', handleDocClick));
|
||||
}
|
||||
|
||||
syncTriggerAndMenu();
|
||||
|
||||
return {
|
||||
el: rootEl,
|
||||
refs: { triggerBtn, triggerLabel, menuEl },
|
||||
onPropsChange: syncTriggerAndMenu,
|
||||
getValue: () => getProps().modelValue,
|
||||
methods: {
|
||||
open: () => toggleOpen(true),
|
||||
close: () => toggleOpen(false),
|
||||
selectByValue: (val) => {
|
||||
const found = (getProps().options || []).find((o) => o.value === val);
|
||||
if (found) chooseOption(found);
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* @fileoverview Micro-bezel (2px radius) SegmentedControl switcher
|
||||
* for time-range filtering, context limit selection, and sub-table switching.
|
||||
*/
|
||||
|
||||
import { defineComponent } from '../../core/component.js';
|
||||
import { h, patchAttr, patchClasses, patchKeyedList, patchText } from '../../core/domPatch.js';
|
||||
|
||||
export const SegmentedControl = defineComponent({
|
||||
name: 'SegmentedControl',
|
||||
setup({ getProps, emit }) {
|
||||
const rootEl = h('div', {
|
||||
className: 'segmented',
|
||||
role: 'group',
|
||||
'aria-label': getProps().ariaLabel || '分段切换器',
|
||||
});
|
||||
|
||||
function syncSegments() {
|
||||
const { options = [], modelValue } = getProps();
|
||||
patchKeyedList(rootEl, options, {
|
||||
keyFn: (opt) => opt.value,
|
||||
sigFn: (opt) => `${opt.value}::${opt.label}::${opt.value === modelValue}`,
|
||||
createRow: (opt) => {
|
||||
const btn = h('button', {
|
||||
type: 'button',
|
||||
className: 'seg-btn',
|
||||
onClick: (e) => {
|
||||
e.stopPropagation();
|
||||
if (getProps().modelValue !== opt.value) {
|
||||
emit('update:modelValue', opt.value);
|
||||
}
|
||||
},
|
||||
});
|
||||
return { el: btn, refs: {} };
|
||||
},
|
||||
updateRow: (_refs, opt, _idx, el) => {
|
||||
const active = opt.value === modelValue;
|
||||
patchText(el, opt.label);
|
||||
patchClasses(el, { 'is-active': active });
|
||||
patchAttr(el, 'aria-pressed', active ? 'true' : 'false');
|
||||
patchAttr(el, 'data-seg-value', opt.value);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
syncSegments();
|
||||
|
||||
return {
|
||||
el: rootEl,
|
||||
refs: {},
|
||||
onPropsChange: syncSegments,
|
||||
getValue: () => getProps().modelValue,
|
||||
};
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* @fileoverview Top Long-Pill Navigation Bar (顶部唯一长药丸功能分割栏).
|
||||
* Splits the application into focused single-viewport views and animates
|
||||
* the active glider using GPU-accelerated translate3d.
|
||||
*/
|
||||
|
||||
import { defineComponent } from '../../core/component.js';
|
||||
import { h, patchAttr, patchClasses, patchStyleVar, patchText } from '../../core/domPatch.js';
|
||||
import { createSvgIcon } from '../../icons/SvgIcon.js';
|
||||
|
||||
export const TopLongPillNav = defineComponent({
|
||||
name: 'TopLongPillNav',
|
||||
setup({ getProps, emit }) {
|
||||
const glider = h('div', { className: 'pill-glider', 'aria-hidden': 'true' });
|
||||
const navEl = h(
|
||||
'nav',
|
||||
{
|
||||
className: 'long-pill-nav',
|
||||
role: 'tablist',
|
||||
'aria-label': '主功能视图分割导航',
|
||||
},
|
||||
[glider]
|
||||
);
|
||||
|
||||
const tabButtons = new Map();
|
||||
|
||||
function selectValue(nextVal) {
|
||||
const current = getProps().modelValue;
|
||||
if (nextVal !== current) {
|
||||
emit('update:modelValue', nextVal);
|
||||
}
|
||||
}
|
||||
|
||||
function syncTabs() {
|
||||
const { items = [], modelValue } = getProps();
|
||||
patchStyleVar(navEl, '--pill-count', Math.max(1, items.length));
|
||||
|
||||
let activeIdx = 0;
|
||||
items.forEach((item, index) => {
|
||||
if (item.value === modelValue) {
|
||||
activeIdx = index;
|
||||
}
|
||||
let entry = tabButtons.get(item.value);
|
||||
if (!entry) {
|
||||
const iconEl = item.icon ? createSvgIcon(item.icon, { size: 14 }) : null;
|
||||
const labelEl = h('span', { className: 'pill-tab-label', text: item.label });
|
||||
const badgeEl = h('span', { className: 'pill-tab-badge', hidden: true });
|
||||
const btn = h(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
role: 'tab',
|
||||
className: 'pill-tab',
|
||||
dataset: { pillValue: item.value },
|
||||
onClick: () => selectValue(item.value),
|
||||
onKeydown: (e) => {
|
||||
if (e.key === 'ArrowRight' || e.key === 'ArrowLeft') {
|
||||
e.preventDefault();
|
||||
const curItems = getProps().items || [];
|
||||
const delta = e.key === 'ArrowRight' ? 1 : -1;
|
||||
const nextIndex = (index + delta + curItems.length) % curItems.length;
|
||||
const target = curItems[nextIndex];
|
||||
if (target) {
|
||||
selectValue(target.value);
|
||||
tabButtons.get(target.value)?.btn?.focus();
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
[iconEl, labelEl, badgeEl]
|
||||
);
|
||||
entry = { btn, labelEl, badgeEl };
|
||||
tabButtons.set(item.value, entry);
|
||||
navEl.appendChild(btn);
|
||||
}
|
||||
|
||||
patchText(entry.labelEl, item.label);
|
||||
if (item.badge !== undefined && item.badge !== null && item.badge !== '') {
|
||||
patchAttr(entry.badgeEl, 'hidden', false);
|
||||
patchText(entry.badgeEl, `(${item.badge})`);
|
||||
} else {
|
||||
patchAttr(entry.badgeEl, 'hidden', true);
|
||||
}
|
||||
|
||||
const isActive = item.value === modelValue;
|
||||
patchClasses(entry.btn, { 'is-active': isActive });
|
||||
patchAttr(entry.btn, 'aria-selected', isActive ? 'true' : 'false');
|
||||
patchAttr(entry.btn, 'tabindex', isActive ? '0' : '-1');
|
||||
});
|
||||
|
||||
patchStyleVar(navEl, '--active-index', activeIdx);
|
||||
}
|
||||
|
||||
syncTabs();
|
||||
|
||||
return {
|
||||
el: navEl,
|
||||
refs: { glider, tabButtons },
|
||||
onPropsChange: syncTabs,
|
||||
getValue: () => getProps().modelValue,
|
||||
};
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user