feat(nacos): improve configuration batch management
This commit is contained in:
16 files changed
+847
-95
No files matched your search
@@ -1,10 +1,20 @@
|
||||
import { computed, ref, type Ref } from "vue";
|
||||
import { safeLocalStorageGet, safeLocalStorageSet } from "@/lib/backend/safeStorage";
|
||||
import { safeLocalStorageGet, safeLocalStorageRemove, safeLocalStorageSet } from "@/lib/backend/safeStorage";
|
||||
|
||||
export const NACOS_CONFIG_LIST_COLUMN_WIDTHS_STORAGE_KEY = "dbx-nacos-config-list-column-widths";
|
||||
export const NACOS_CONFIG_LIST_HIDDEN_COLUMNS_STORAGE_KEY = "dbx-nacos-config-list-hidden-columns";
|
||||
export const DEFAULT_NACOS_CONFIG_LIST_COLUMN_WIDTHS = [280, 180, 180, 96] as const;
|
||||
export const NACOS_CONFIG_LIST_HORIZONTAL_PADDING = 24;
|
||||
const MIN_NACOS_CONFIG_LIST_COLUMN_WIDTHS = [140, 96, 96, 72] as const;
|
||||
const NACOS_CONFIG_LIST_COLUMNS = ["dataId", "group", "application", "format"] as const;
|
||||
const TOGGLEABLE_NACOS_CONFIG_LIST_COLUMNS = ["group", "application", "format"] as const;
|
||||
|
||||
export type NacosConfigListColumnKey = (typeof NACOS_CONFIG_LIST_COLUMNS)[number];
|
||||
export type ToggleableNacosConfigListColumnKey = (typeof TOGGLEABLE_NACOS_CONFIG_LIST_COLUMNS)[number];
|
||||
|
||||
function isToggleableColumn(key: unknown): key is ToggleableNacosConfigListColumnKey {
|
||||
return TOGGLEABLE_NACOS_CONFIG_LIST_COLUMNS.includes(key as ToggleableNacosConfigListColumnKey);
|
||||
}
|
||||
|
||||
function minWidthForColumn(index: number) {
|
||||
return MIN_NACOS_CONFIG_LIST_COLUMN_WIDTHS[index] ?? MIN_NACOS_CONFIG_LIST_COLUMN_WIDTHS[MIN_NACOS_CONFIG_LIST_COLUMN_WIDTHS.length - 1];
|
||||
@@ -34,20 +44,42 @@ function saveNacosConfigListColumnWidths(widths: readonly number[]) {
|
||||
safeLocalStorageSet(NACOS_CONFIG_LIST_COLUMN_WIDTHS_STORAGE_KEY, JSON.stringify(normalized));
|
||||
}
|
||||
|
||||
function loadNacosConfigListHiddenColumns(): ToggleableNacosConfigListColumnKey[] {
|
||||
const raw = safeLocalStorageGet(NACOS_CONFIG_LIST_HIDDEN_COLUMNS_STORAGE_KEY);
|
||||
if (!raw) return [];
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(raw);
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
return [...new Set(parsed.filter(isToggleableColumn))];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function saveNacosConfigListHiddenColumns(columns: readonly ToggleableNacosConfigListColumnKey[]) {
|
||||
const normalized = [...new Set(columns.filter(isToggleableColumn))];
|
||||
if (normalized.length === 0) {
|
||||
safeLocalStorageRemove(NACOS_CONFIG_LIST_HIDDEN_COLUMNS_STORAGE_KEY);
|
||||
return;
|
||||
}
|
||||
safeLocalStorageSet(NACOS_CONFIG_LIST_HIDDEN_COLUMNS_STORAGE_KEY, JSON.stringify(normalized));
|
||||
}
|
||||
|
||||
function gridTemplateColumnsForWidths(widths: readonly number[]) {
|
||||
return widths.map((width) => `${width}px`).join(" ");
|
||||
}
|
||||
|
||||
export function fitNacosConfigListColumnWidths(widths: readonly number[], availableWidth: number): number[] {
|
||||
function fitNacosConfigListColumnWidthsForIndexes(widths: readonly number[], availableWidth: number, columnIndexes: readonly number[]): number[] {
|
||||
const normalized = normalizeNacosConfigListColumnWidths([...widths]) ?? [...DEFAULT_NACOS_CONFIG_LIST_COLUMN_WIDTHS];
|
||||
const selectedWidths = columnIndexes.map((index) => normalized[index] ?? minWidthForColumn(index));
|
||||
const targetWidth = Math.floor(availableWidth) - NACOS_CONFIG_LIST_HORIZONTAL_PADDING;
|
||||
if (targetWidth <= 0) return normalized;
|
||||
if (targetWidth <= 0) return selectedWidths;
|
||||
|
||||
const minimums = normalized.map((_, index) => minWidthForColumn(index));
|
||||
const minimums = columnIndexes.map((index) => minWidthForColumn(index));
|
||||
const minimumTotal = minimums.reduce((sum, width) => sum + width, 0);
|
||||
if (targetWidth <= minimumTotal) return minimums;
|
||||
|
||||
const preferredExtras = normalized.map((width, index) => Math.max(0, width - minimums[index]!));
|
||||
const preferredExtras = selectedWidths.map((width, index) => Math.max(0, width - minimums[index]!));
|
||||
const preferredExtraTotal = preferredExtras.reduce((sum, width) => sum + width, 0);
|
||||
const availableExtra = targetWidth - minimumTotal;
|
||||
const fitted = minimums.map((minimum, index) => {
|
||||
@@ -58,42 +90,63 @@ export function fitNacosConfigListColumnWidths(widths: readonly number[], availa
|
||||
return fitted;
|
||||
}
|
||||
|
||||
export function fitNacosConfigListColumnWidths(widths: readonly number[], availableWidth: number): number[] {
|
||||
return fitNacosConfigListColumnWidthsForIndexes(
|
||||
widths,
|
||||
availableWidth,
|
||||
DEFAULT_NACOS_CONFIG_LIST_COLUMN_WIDTHS.map((_, index) => index),
|
||||
);
|
||||
}
|
||||
|
||||
export function useNacosConfigListColumnResize(availableWidth?: Readonly<Ref<number>>) {
|
||||
const preferredColumnWidths = ref(loadNacosConfigListColumnWidths());
|
||||
const hiddenColumnKeys = ref(loadNacosConfigListHiddenColumns());
|
||||
const resizingColumnIndex = ref<number | null>(null);
|
||||
const visibleColumns = computed(() => NACOS_CONFIG_LIST_COLUMNS.filter((key) => !hiddenColumnKeys.value.includes(key as ToggleableNacosConfigListColumnKey)));
|
||||
const visibleColumnIndexes = computed(() => visibleColumns.value.map((key) => NACOS_CONFIG_LIST_COLUMNS.indexOf(key)));
|
||||
const columnWidths = computed(() => {
|
||||
const width = availableWidth?.value ?? 0;
|
||||
return width > 0 ? fitNacosConfigListColumnWidths(preferredColumnWidths.value, width) : preferredColumnWidths.value;
|
||||
});
|
||||
const gridTemplateColumns = computed(() => gridTemplateColumnsForWidths(columnWidths.value));
|
||||
const totalWidth = computed(() => columnWidths.value.reduce((sum, width) => sum + width, 0));
|
||||
const visibleColumnWidths = computed(() => {
|
||||
const width = availableWidth?.value ?? 0;
|
||||
return width > 0 ? fitNacosConfigListColumnWidthsForIndexes(preferredColumnWidths.value, width, visibleColumnIndexes.value) : visibleColumnIndexes.value.map((index) => preferredColumnWidths.value[index]!);
|
||||
});
|
||||
const gridTemplateColumns = computed(() => gridTemplateColumnsForWidths(visibleColumnWidths.value));
|
||||
const totalWidth = computed(() => visibleColumnWidths.value.reduce((sum, width) => sum + width, 0));
|
||||
const minWidth = computed(() => `${totalWidth.value + NACOS_CONFIG_LIST_HORIZONTAL_PADDING}px`);
|
||||
|
||||
function onResizeStart(columnIndex: number, event: MouseEvent) {
|
||||
if (columnIndex < 0 || columnIndex >= columnWidths.value.length - 1) return;
|
||||
if (columnIndex < 0 || columnIndex >= visibleColumnWidths.value.length - 1) return;
|
||||
event.preventDefault();
|
||||
const startX = event.clientX;
|
||||
const startWidths = [...columnWidths.value];
|
||||
const startWidth = startWidths[columnIndex] ?? minWidthForColumn(columnIndex);
|
||||
const nextStartWidth = startWidths[columnIndex + 1] ?? minWidthForColumn(columnIndex + 1);
|
||||
const startWidths = [...visibleColumnWidths.value];
|
||||
const sourceIndex = visibleColumnIndexes.value[columnIndex]!;
|
||||
const nextIndex = visibleColumnIndexes.value[columnIndex + 1]!;
|
||||
const startWidth = startWidths[columnIndex] ?? minWidthForColumn(sourceIndex);
|
||||
const nextStartWidth = startWidths[columnIndex + 1] ?? minWidthForColumn(nextIndex);
|
||||
const pairWidth = startWidth + nextStartWidth;
|
||||
resizingColumnIndex.value = columnIndex;
|
||||
|
||||
const onMove = (moveEvent: MouseEvent) => {
|
||||
const delta = moveEvent.clientX - startX;
|
||||
const minimum = minWidthForColumn(columnIndex);
|
||||
const nextMinimum = minWidthForColumn(columnIndex + 1);
|
||||
const minimum = minWidthForColumn(sourceIndex);
|
||||
const nextMinimum = minWidthForColumn(nextIndex);
|
||||
const width = Math.min(pairWidth - nextMinimum, Math.max(minimum, startWidth + delta));
|
||||
const nextWidths = [...startWidths];
|
||||
nextWidths[columnIndex] = width;
|
||||
nextWidths[columnIndex + 1] = pairWidth - width;
|
||||
const nextWidths = [...preferredColumnWidths.value];
|
||||
nextWidths[sourceIndex] = width;
|
||||
nextWidths[nextIndex] = pairWidth - width;
|
||||
preferredColumnWidths.value = nextWidths;
|
||||
};
|
||||
|
||||
const onUp = (moveEvent: MouseEvent) => {
|
||||
onMove(moveEvent);
|
||||
resizingColumnIndex.value = null;
|
||||
preferredColumnWidths.value = [...columnWidths.value];
|
||||
const nextWidths = [...preferredColumnWidths.value];
|
||||
visibleColumnIndexes.value.forEach((index, visibleIndex) => {
|
||||
nextWidths[index] = visibleColumnWidths.value[visibleIndex]!;
|
||||
});
|
||||
preferredColumnWidths.value = nextWidths;
|
||||
saveNacosConfigListColumnWidths(preferredColumnWidths.value);
|
||||
document.removeEventListener("mousemove", onMove);
|
||||
document.removeEventListener("mouseup", onUp);
|
||||
@@ -103,12 +156,26 @@ export function useNacosConfigListColumnResize(availableWidth?: Readonly<Ref<num
|
||||
document.addEventListener("mouseup", onUp);
|
||||
}
|
||||
|
||||
function isColumnVisible(column: NacosConfigListColumnKey) {
|
||||
return !hiddenColumnKeys.value.includes(column as ToggleableNacosConfigListColumnKey);
|
||||
}
|
||||
|
||||
function setColumnVisible(column: ToggleableNacosConfigListColumnKey, visible: boolean) {
|
||||
const next = visible ? hiddenColumnKeys.value.filter((key) => key !== column) : [...new Set([...hiddenColumnKeys.value, column])];
|
||||
hiddenColumnKeys.value = next;
|
||||
saveNacosConfigListHiddenColumns(next);
|
||||
}
|
||||
|
||||
return {
|
||||
columnWidths,
|
||||
visibleColumns,
|
||||
toggleableColumns: TOGGLEABLE_NACOS_CONFIG_LIST_COLUMNS,
|
||||
gridTemplateColumns,
|
||||
totalWidth,
|
||||
minWidth,
|
||||
resizingColumnIndex,
|
||||
onResizeStart,
|
||||
isColumnVisible,
|
||||
setColumnVisible,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user