enhance(frontend): deckをpreferences管理に

This commit is contained in:
syuilo 2025-03-11 11:14:55 +09:00
parent 1f2801af02
commit 1f345eb839
7 changed files with 231 additions and 215 deletions

View file

@ -3,13 +3,23 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { throttle } from 'throttle-debounce';
import { notificationTypes } from 'misskey-js';
import { ref } from 'vue';
import { v4 as uuid } from 'uuid';
import { i18n } from './i18n.js';
import type { BasicTimelineType } from '@/timelines.js';
import type { SoundStore } from '@/preferences/def.js';
import { misskeyApi } from '@/utility/misskey-api.js';
import type { MenuItem } from '@/types/menu.js';
import { deepClone } from '@/utility/clone.js';
import { store } from '@/store.js';
import { prefer } from '@/preferences.js';
import * as os from '@/os.js';
export type DeckProfile = {
name: string;
id: string;
columns: Column[];
layout: Column['id'][][];
};
type ColumnWidget = {
name: string;
@ -53,127 +63,132 @@ export type Column = {
soundSetting?: SoundStore;
};
export const loadDeck = async () => {
let deck;
const _currentProfile = prefer.s['deck.profiles'].find(p => p.name === prefer.s['deck.profile']);
const __currentProfile = _currentProfile ? deepClone(_currentProfile) : null;
export const columns = ref(__currentProfile ? __currentProfile.columns : []);
export const layout = ref(__currentProfile ? __currentProfile.layout : []);
try {
deck = await misskeyApi('i/registry/get', {
scope: ['client', 'deck', 'profiles'],
key: store.s['deck.profile'],
});
} catch (err) {
if (typeof err === 'object' && err != null && 'code' in err && err.code === 'NO_SUCH_KEY') {
// 後方互換性のため
if (store.s['deck.profile'] === 'default') {
saveDeck();
return;
}
if (prefer.s['deck.profile'] == null) {
addProfile('Main');
}
store.set('deck.columns', []);
store.set('deck.layout', []);
return;
}
throw err;
}
export function forceSaveCurrentDeckProfile() {
const currentProfile = prefer.s['deck.profiles'].find(p => p.name === prefer.s['deck.profile']);
if (currentProfile == null) return;
store.set('deck.columns', deck.columns);
store.set('deck.layout', deck.layout);
const newProfile = deepClone(currentProfile);
newProfile.columns = columns.value;
newProfile.layout = layout.value;
const newProfiles = prefer.s['deck.profiles'].filter(p => p.name !== prefer.s['deck.profile']);
newProfiles.push(newProfile);
prefer.commit('deck.profiles', newProfiles);
}
export const saveCurrentDeckProfile = () => {
forceSaveCurrentDeckProfile();
};
export async function forceSaveDeck() {
await misskeyApi('i/registry/set', {
scope: ['client', 'deck', 'profiles'],
key: store.s['deck.profile'],
value: {
columns: store.r['deck.columns'].value,
layout: store.r['deck.layout'].value,
},
});
function switchProfile(profile: DeckProfile) {
prefer.commit('deck.profile', profile.name);
const currentProfile = deepClone(profile);
columns.value = currentProfile.columns;
layout.value = currentProfile.layout;
forceSaveCurrentDeckProfile();
}
// TODO: deckがloadされていない状態でsaveすると意図せず上書きが発生するので対策する
export const saveDeck = throttle(1000, () => {
forceSaveDeck();
});
function addProfile(name: string) {
if (name.trim() === '') return;
if (prefer.s['deck.profiles'].find(p => p.name === name)) return;
export async function getProfiles(): Promise<string[]> {
return await misskeyApi('i/registry/keys', {
scope: ['client', 'deck', 'profiles'],
});
const newProfile: DeckProfile = {
id: uuid(),
name,
columns: [],
layout: [],
};
prefer.commit('deck.profiles', [...prefer.s['deck.profiles'], newProfile]);
switchProfile(newProfile);
}
export async function deleteProfile(key: string): Promise<void> {
return await misskeyApi('i/registry/remove', {
scope: ['client', 'deck', 'profiles'],
key: key,
});
function createFirstProfile() {
addProfile('Main');
}
export function deleteProfile(name: string): void {
const newProfiles = prefer.s['deck.profiles'].filter(p => p.name !== name);
prefer.commit('deck.profiles', newProfiles);
if (prefer.s['deck.profiles'].length === 0) {
createFirstProfile();
} else {
switchProfile(prefer.s['deck.profiles'][0]);
}
}
export function addColumn(column: Column) {
if (column.name === undefined) column.name = null;
store.push('deck.columns', column);
store.push('deck.layout', [column.id]);
saveDeck();
columns.value.push(column);
layout.value.push([column.id]);
saveCurrentDeckProfile();
}
export function removeColumn(id: Column['id']) {
store.set('deck.columns', store.s['deck.columns'].filter(c => c.id !== id));
store.set('deck.layout', store.s['deck.layout']
.map(ids => ids.filter(_id => _id !== id))
.filter(ids => ids.length > 0));
saveDeck();
columns.value = columns.value.filter(c => c.id !== id);
layout.value = layout.value.map(ids => ids.filter(_id => _id !== id)).filter(ids => ids.length > 0);
saveCurrentDeckProfile();
}
export function swapColumn(a: Column['id'], b: Column['id']) {
const aX = store.s['deck.layout'].findIndex(ids => ids.indexOf(a) !== -1);
const aY = store.s['deck.layout'][aX].findIndex(id => id === a);
const bX = store.s['deck.layout'].findIndex(ids => ids.indexOf(b) !== -1);
const bY = store.s['deck.layout'][bX].findIndex(id => id === b);
const layout = deepClone(store.s['deck.layout']);
layout[aX][aY] = b;
layout[bX][bY] = a;
store.set('deck.layout', layout);
saveDeck();
const aX = layout.value.findIndex(ids => ids.indexOf(a) !== -1);
const aY = layout.value[aX].findIndex(id => id === a);
const bX = layout.value.findIndex(ids => ids.indexOf(b) !== -1);
const bY = layout.value[bX].findIndex(id => id === b);
const newLayout = deepClone(layout.value);
newLayout[aX][aY] = b;
newLayout[bX][bY] = a;
layout.value = newLayout;
saveCurrentDeckProfile();
}
export function swapLeftColumn(id: Column['id']) {
const layout = deepClone(store.s['deck.layout']);
store.s['deck.layout'].some((ids, i) => {
const newLayout = deepClone(layout.value);
layout.value.some((ids, i) => {
if (ids.includes(id)) {
const left = store.s['deck.layout'][i - 1];
const left = layout.value[i - 1];
if (left) {
layout[i - 1] = store.s['deck.layout'][i];
layout[i] = left;
store.set('deck.layout', layout);
newLayout[i - 1] = layout.value[i];
newLayout[i] = left;
layout.value = newLayout;
}
return true;
}
return false;
});
saveDeck();
saveCurrentDeckProfile();
}
export function swapRightColumn(id: Column['id']) {
const layout = deepClone(store.s['deck.layout']);
store.s['deck.layout'].some((ids, i) => {
const newLayout = deepClone(layout.value);
layout.value.some((ids, i) => {
if (ids.includes(id)) {
const right = store.s['deck.layout'][i + 1];
const right = layout.value[i + 1];
if (right) {
layout[i + 1] = store.s['deck.layout'][i];
layout[i] = right;
store.set('deck.layout', layout);
newLayout[i + 1] = layout.value[i];
newLayout[i] = right;
layout.value = newLayout;
}
return true;
}
return false;
});
saveDeck();
saveCurrentDeckProfile();
}
export function swapUpColumn(id: Column['id']) {
const layout = deepClone(store.s['deck.layout']);
const idsIndex = store.s['deck.layout'].findIndex(ids => ids.includes(id));
const ids = deepClone(store.s['deck.layout'][idsIndex]);
const newLayout = deepClone(layout.value);
const idsIndex = layout.value.findIndex(ids => ids.includes(id));
const ids = deepClone(layout.value[idsIndex]);
ids.some((x, i) => {
if (x === id) {
const up = ids[i - 1];
@ -181,20 +196,20 @@ export function swapUpColumn(id: Column['id']) {
ids[i - 1] = id;
ids[i] = up;
layout[idsIndex] = ids;
store.set('deck.layout', layout);
newLayout[idsIndex] = ids;
layout.value = newLayout;
}
return true;
}
return false;
});
saveDeck();
saveCurrentDeckProfile();
}
export function swapDownColumn(id: Column['id']) {
const layout = deepClone(store.s['deck.layout']);
const idsIndex = store.s['deck.layout'].findIndex(ids => ids.includes(id));
const ids = deepClone(store.s['deck.layout'][idsIndex]);
const newLayout = deepClone(layout.value);
const idsIndex = layout.value.findIndex(ids => ids.includes(id));
const ids = deepClone(layout.value[idsIndex]);
ids.some((x, i) => {
if (x === id) {
const down = ids[i + 1];
@ -202,105 +217,137 @@ export function swapDownColumn(id: Column['id']) {
ids[i + 1] = id;
ids[i] = down;
layout[idsIndex] = ids;
store.set('deck.layout', layout);
newLayout[idsIndex] = ids;
layout.value = newLayout;
}
return true;
}
return false;
});
saveDeck();
saveCurrentDeckProfile();
}
export function stackLeftColumn(id: Column['id']) {
let layout = deepClone(store.s['deck.layout']);
const i = store.s['deck.layout'].findIndex(ids => ids.includes(id));
layout = layout.map(ids => ids.filter(_id => _id !== id));
layout[i - 1].push(id);
layout = layout.filter(ids => ids.length > 0);
store.set('deck.layout', layout);
saveDeck();
let newLayout = deepClone(layout.value);
const i = layout.value.findIndex(ids => ids.includes(id));
newLayout = newLayout.map(ids => ids.filter(_id => _id !== id));
newLayout[i - 1].push(id);
newLayout = newLayout.filter(ids => ids.length > 0);
layout.value = newLayout;
saveCurrentDeckProfile();
}
export function popRightColumn(id: Column['id']) {
let layout = deepClone(store.s['deck.layout']);
const i = store.s['deck.layout'].findIndex(ids => ids.includes(id));
const affected = layout[i];
layout = layout.map(ids => ids.filter(_id => _id !== id));
layout.splice(i + 1, 0, [id]);
layout = layout.filter(ids => ids.length > 0);
store.set('deck.layout', layout);
let newLayout = deepClone(layout.value);
const i = layout.value.findIndex(ids => ids.includes(id));
const affected = newLayout[i];
newLayout = newLayout.map(ids => ids.filter(_id => _id !== id));
newLayout.splice(i + 1, 0, [id]);
newLayout = newLayout.filter(ids => ids.length > 0);
layout.value = newLayout;
const columns = deepClone(store.s['deck.columns']);
for (const column of columns) {
const newColumns = deepClone(columns.value);
for (const column of newColumns) {
if (affected.includes(column.id)) {
column.active = true;
}
}
store.set('deck.columns', columns);
columns.value = newColumns;
saveDeck();
saveCurrentDeckProfile();
}
export function addColumnWidget(id: Column['id'], widget: ColumnWidget) {
const columns = deepClone(store.s['deck.columns']);
const columnIndex = store.s['deck.columns'].findIndex(c => c.id === id);
const column = deepClone(store.s['deck.columns'][columnIndex]);
const newColumns = deepClone(columns.value);
const columnIndex = columns.value.findIndex(c => c.id === id);
const column = deepClone(columns.value[columnIndex]);
if (column == null) return;
if (column.widgets == null) column.widgets = [];
column.widgets.unshift(widget);
columns[columnIndex] = column;
store.set('deck.columns', columns);
saveDeck();
newColumns[columnIndex] = column;
columns.value = newColumns;
saveCurrentDeckProfile();
}
export function removeColumnWidget(id: Column['id'], widget: ColumnWidget) {
const columns = deepClone(store.s['deck.columns']);
const columnIndex = store.s['deck.columns'].findIndex(c => c.id === id);
const column = deepClone(store.s['deck.columns'][columnIndex]);
const newColumns = deepClone(columns.value);
const columnIndex = columns.value.findIndex(c => c.id === id);
const column = deepClone(columns.value[columnIndex]);
if (column == null) return;
if (column.widgets == null) column.widgets = [];
column.widgets = column.widgets.filter(w => w.id !== widget.id);
columns[columnIndex] = column;
store.set('deck.columns', columns);
saveDeck();
newColumns[columnIndex] = column;
columns.value = newColumns;
saveCurrentDeckProfile();
}
export function setColumnWidgets(id: Column['id'], widgets: ColumnWidget[]) {
const columns = deepClone(store.s['deck.columns']);
const columnIndex = store.s['deck.columns'].findIndex(c => c.id === id);
const column = deepClone(store.s['deck.columns'][columnIndex]);
const newColumns = deepClone(columns.value);
const columnIndex = columns.value.findIndex(c => c.id === id);
const column = deepClone(columns.value[columnIndex]);
if (column == null) return;
column.widgets = widgets;
columns[columnIndex] = column;
store.set('deck.columns', columns);
saveDeck();
newColumns[columnIndex] = column;
columns.value = newColumns;
saveCurrentDeckProfile();
}
export function updateColumnWidget(id: Column['id'], widgetId: string, widgetData: any) {
const columns = deepClone(store.s['deck.columns']);
const columnIndex = store.s['deck.columns'].findIndex(c => c.id === id);
const column = deepClone(store.s['deck.columns'][columnIndex]);
const newColumns = deepClone(columns.value);
const columnIndex = columns.value.findIndex(c => c.id === id);
const column = deepClone(columns.value[columnIndex]);
if (column == null) return;
if (column.widgets == null) column.widgets = [];
column.widgets = column.widgets.map(w => w.id === widgetId ? {
...w,
data: widgetData,
} : w);
columns[columnIndex] = column;
store.set('deck.columns', columns);
saveDeck();
newColumns[columnIndex] = column;
columns.value = newColumns;
saveCurrentDeckProfile();
}
export function updateColumn(id: Column['id'], column: Partial<Column>) {
const columns = deepClone(store.s['deck.columns']);
const columnIndex = store.s['deck.columns'].findIndex(c => c.id === id);
const currentColumn = deepClone(store.s['deck.columns'][columnIndex]);
const newColumns = deepClone(columns.value);
const columnIndex = columns.value.findIndex(c => c.id === id);
const currentColumn = deepClone(columns.value[columnIndex]);
if (currentColumn == null) return;
for (const [k, v] of Object.entries(column)) {
currentColumn[k] = v;
}
columns[columnIndex] = currentColumn;
store.set('deck.columns', columns);
saveDeck();
newColumns[columnIndex] = currentColumn;
columns.value = newColumns;
saveCurrentDeckProfile();
}
export function switchProfileMenu(ev: MouseEvent) {
const items: MenuItem[] = prefer.s['deck.profile'] ? [{
text: prefer.s['deck.profile'],
active: true,
action: () => {},
}] : [];
const profiles = prefer.s['deck.profiles'];
items.push(...(profiles.filter(p => p.name !== prefer.s['deck.profile']).map(p => ({
text: p.name,
action: () => {
switchProfile(p);
},
}))), { type: 'divider' as const }, {
text: i18n.ts._deck.newProfile,
icon: 'ti ti-plus',
action: async () => {
const { canceled, result: name } = await os.inputText({
title: i18n.ts._deck.profile,
minLength: 1,
});
if (canceled || name == null || name.trim() === '') return;
addProfile(name);
},
});
os.popupMenu(items, ev.currentTarget ?? ev.target);
}