mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-04-19 14:53:50 +02:00
custom col width persistent
This commit is contained in:
@@ -7,7 +7,42 @@ import {
|
|||||||
isFunction,
|
isFunction,
|
||||||
useVueTable
|
useVueTable
|
||||||
} from '@tanstack/vue-table';
|
} from '@tanstack/vue-table';
|
||||||
import { ref, unref } from 'vue';
|
import { ref, unref, watch } from 'vue';
|
||||||
|
|
||||||
|
function safeJsonParse(str) {
|
||||||
|
if (!str) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return JSON.parse(str);
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function debounce(fn, wait) {
|
||||||
|
let t = 0;
|
||||||
|
return (...args) => {
|
||||||
|
if (t) {
|
||||||
|
clearTimeout(t);
|
||||||
|
}
|
||||||
|
t = setTimeout(() => fn(...args), wait);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function filterSizingByColumns(sizing, columns) {
|
||||||
|
if (!sizing || typeof sizing !== 'object') {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
const ids = new Set((columns ?? []).map((c) => c?.id).filter(Boolean));
|
||||||
|
const out = {};
|
||||||
|
for (const [key, value] of Object.entries(sizing)) {
|
||||||
|
if (ids.has(key)) {
|
||||||
|
out[key] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
function getColumnId(col) {
|
function getColumnId(col) {
|
||||||
return col?.id ?? col?.accessorKey ?? null;
|
return col?.id ?? col?.accessorKey ?? null;
|
||||||
@@ -98,6 +133,10 @@ export function useVrcxVueTable(options) {
|
|||||||
fillRemainingSpace = true,
|
fillRemainingSpace = true,
|
||||||
spacerColumnId = '__spacer',
|
spacerColumnId = '__spacer',
|
||||||
|
|
||||||
|
persistKey,
|
||||||
|
persistColumnSizing = true,
|
||||||
|
persistDebounceMs = 200,
|
||||||
|
|
||||||
tableOptions = {}
|
tableOptions = {}
|
||||||
} = options ?? {};
|
} = options ?? {};
|
||||||
|
|
||||||
@@ -112,6 +151,29 @@ export function useVrcxVueTable(options) {
|
|||||||
const columnPinning = ref(initialColumnPinning ?? { left: [], right: [] });
|
const columnPinning = ref(initialColumnPinning ?? { left: [], right: [] });
|
||||||
const columnSizing = ref(initialColumnSizing ?? {});
|
const columnSizing = ref(initialColumnSizing ?? {});
|
||||||
|
|
||||||
|
const storageKey = persistKey ? `vrcx:table:${persistKey}` : null;
|
||||||
|
|
||||||
|
function readPersisted() {
|
||||||
|
if (!storageKey) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return safeJsonParse(localStorage.getItem(storageKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
function writePersisted(patch) {
|
||||||
|
if (!storageKey) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const cur = safeJsonParse(localStorage.getItem(storageKey)) ?? {};
|
||||||
|
const next = { ...cur, ...patch, updatedAt: Date.now() };
|
||||||
|
localStorage.setItem(storageKey, JSON.stringify(next));
|
||||||
|
}
|
||||||
|
|
||||||
|
const persisted = readPersisted();
|
||||||
|
if (persisted && persistColumnSizing && persisted.columnSizing) {
|
||||||
|
columnSizing.value = persisted.columnSizing;
|
||||||
|
}
|
||||||
|
|
||||||
const state = {};
|
const state = {};
|
||||||
const handlers = {};
|
const handlers = {};
|
||||||
const rowModels = {};
|
const rowModels = {};
|
||||||
@@ -199,6 +261,24 @@ export function useVrcxVueTable(options) {
|
|||||||
...tableOptions
|
...tableOptions
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const persistWrite = debounce(
|
||||||
|
(payload) => writePersisted(payload),
|
||||||
|
persistDebounceMs
|
||||||
|
);
|
||||||
|
|
||||||
|
if (storageKey && persistColumnSizing) {
|
||||||
|
watch(
|
||||||
|
columnSizing,
|
||||||
|
(val) => {
|
||||||
|
const cols = table.getAllLeafColumns?.() ?? [];
|
||||||
|
persistWrite({
|
||||||
|
columnSizing: filterSizingByColumns(val, cols)
|
||||||
|
});
|
||||||
|
},
|
||||||
|
{ deep: true }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
table,
|
table,
|
||||||
sorting,
|
sorting,
|
||||||
|
|||||||
@@ -76,6 +76,7 @@
|
|||||||
);
|
);
|
||||||
|
|
||||||
const { table, pagination } = useVrcxVueTable({
|
const { table, pagination } = useVrcxVueTable({
|
||||||
|
persistKey: 'feed',
|
||||||
data: feedDisplayData,
|
data: feedDisplayData,
|
||||||
columns: baseColumns,
|
columns: baseColumns,
|
||||||
getRowId: (row) => `${row.type}:${row.rowId}:${row.created_at ?? ''}`,
|
getRowId: (row) => `${row.type}:${row.rowId}:${row.created_at ?? ''}`,
|
||||||
|
|||||||
@@ -157,6 +157,7 @@
|
|||||||
);
|
);
|
||||||
|
|
||||||
const { table, pagination } = useVrcxVueTable({
|
const { table, pagination } = useVrcxVueTable({
|
||||||
|
persistKey: 'friendLog',
|
||||||
data: friendLogDisplayData,
|
data: friendLogDisplayData,
|
||||||
columns,
|
columns,
|
||||||
getRowId: (row) => `${row.type}:${row.rowId ?? row.userId ?? row.created_at ?? ''}`,
|
getRowId: (row) => `${row.type}:${row.rowId ?? row.userId ?? row.created_at ?? ''}`,
|
||||||
|
|||||||
@@ -156,6 +156,7 @@
|
|||||||
);
|
);
|
||||||
|
|
||||||
const { table, pagination } = useVrcxVueTable({
|
const { table, pagination } = useVrcxVueTable({
|
||||||
|
persistKey: 'gameLog',
|
||||||
data: gameLogDisplayData,
|
data: gameLogDisplayData,
|
||||||
columns,
|
columns,
|
||||||
getRowId: (row) => `${row.type}:${row.rowId ?? row.displayName + row.location + row.time}`,
|
getRowId: (row) => `${row.type}:${row.rowId ?? row.displayName + row.location + row.time}`,
|
||||||
|
|||||||
@@ -145,6 +145,7 @@
|
|||||||
);
|
);
|
||||||
|
|
||||||
const { table, pagination } = useVrcxVueTable({
|
const { table, pagination } = useVrcxVueTable({
|
||||||
|
persistKey: 'moderation',
|
||||||
data: moderationDisplayData,
|
data: moderationDisplayData,
|
||||||
columns,
|
columns,
|
||||||
getRowId: (row) => row.id ?? `${row.type}:${row.sourceUserId}:${row.targetUserId}:${row.created ?? ''}`,
|
getRowId: (row) => row.id ?? `${row.type}:${row.sourceUserId}:${row.targetUserId}:${row.created ?? ''}`,
|
||||||
|
|||||||
@@ -213,6 +213,7 @@
|
|||||||
);
|
);
|
||||||
|
|
||||||
const { table, pagination } = useVrcxVueTable({
|
const { table, pagination } = useVrcxVueTable({
|
||||||
|
persistKey: 'notifications',
|
||||||
data: notificationDisplayData,
|
data: notificationDisplayData,
|
||||||
columns,
|
columns,
|
||||||
getRowId: (row) => row.id ?? `${row.type}:${row.senderUserId ?? ''}:${row.created_at ?? ''}`,
|
getRowId: (row) => row.id ?? `${row.type}:${row.senderUserId ?? ''}:${row.created_at ?? ''}`,
|
||||||
|
|||||||
@@ -278,6 +278,7 @@
|
|||||||
const playerListDisplayData = computed(() => currentInstanceUsersData.value ?? []);
|
const playerListDisplayData = computed(() => currentInstanceUsersData.value ?? []);
|
||||||
|
|
||||||
const { table: playerListTable } = useVrcxVueTable({
|
const { table: playerListTable } = useVrcxVueTable({
|
||||||
|
persistKey: 'playerList',
|
||||||
data: playerListDisplayData,
|
data: playerListDisplayData,
|
||||||
columns: playerListColumns.value,
|
columns: playerListColumns.value,
|
||||||
getRowId: (row) => `${row?.ref?.id ?? ''}:${row?.displayName ?? ''}:${row?.photonId ?? ''}`,
|
getRowId: (row) => `${row?.ref?.id ?? ''}:${row?.displayName ?? ''}:${row?.photonId ?? ''}`,
|
||||||
|
|||||||
Reference in New Issue
Block a user