2021-01-11 20:38:34 +09:00
|
|
|
import { throttle } from 'throttle-debounce';
|
2022-03-21 03:11:14 +09:00
|
|
|
import { markRaw } from 'vue';
|
|
|
|
import { notificationTypes } from 'misskey-js';
|
2022-06-12 19:50:47 +09:00
|
|
|
import { Storage } from '../../pizzax';
|
|
|
|
import { api } from '@/os';
|
2022-11-17 09:31:07 +09:00
|
|
|
import { deepClone } from '@/scripts/clone';
|
2020-12-19 10:55:52 +09:00
|
|
|
|
|
|
|
type ColumnWidget = {
|
|
|
|
name: string;
|
|
|
|
id: string;
|
|
|
|
data: Record<string, any>;
|
|
|
|
};
|
|
|
|
|
2022-03-21 03:11:14 +09:00
|
|
|
export type Column = {
|
2020-12-19 10:55:52 +09:00
|
|
|
id: string;
|
2023-02-09 10:35:28 +09:00
|
|
|
type: 'main' | 'widgets' | 'notifications' | 'tl' | 'antenna' | 'channel' | 'list' | 'mentions' | 'direct';
|
2020-12-19 10:55:52 +09:00
|
|
|
name: string | null;
|
|
|
|
width: number;
|
|
|
|
widgets?: ColumnWidget[];
|
2021-01-13 21:43:06 +09:00
|
|
|
active?: boolean;
|
2022-03-21 03:11:14 +09:00
|
|
|
flexible?: boolean;
|
|
|
|
antennaId?: string;
|
|
|
|
listId?: string;
|
2023-02-09 10:35:28 +09:00
|
|
|
channelId?: string;
|
2023-04-12 11:40:08 +09:00
|
|
|
roleId?: string;
|
2022-03-21 03:11:14 +09:00
|
|
|
includingTypes?: typeof notificationTypes[number][];
|
|
|
|
tl?: 'home' | 'local' | 'social' | 'global';
|
2020-12-19 10:55:52 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
export const deckStore = markRaw(new Storage('deck', {
|
2021-01-11 20:38:34 +09:00
|
|
|
profile: {
|
|
|
|
where: 'deviceAccount',
|
2022-06-12 19:50:47 +09:00
|
|
|
default: 'default',
|
2021-01-11 20:38:34 +09:00
|
|
|
},
|
2020-12-19 10:55:52 +09:00
|
|
|
columns: {
|
|
|
|
where: 'deviceAccount',
|
2022-06-12 19:50:47 +09:00
|
|
|
default: [] as Column[],
|
2020-12-19 10:55:52 +09:00
|
|
|
},
|
|
|
|
layout: {
|
|
|
|
where: 'deviceAccount',
|
2022-06-12 19:50:47 +09:00
|
|
|
default: [] as Column['id'][][],
|
2020-12-19 10:55:52 +09:00
|
|
|
},
|
|
|
|
columnAlign: {
|
|
|
|
where: 'deviceAccount',
|
2022-06-12 19:50:47 +09:00
|
|
|
default: 'left' as 'left' | 'right' | 'center',
|
2020-12-19 10:55:52 +09:00
|
|
|
},
|
|
|
|
alwaysShowMainColumn: {
|
|
|
|
where: 'deviceAccount',
|
2022-06-12 19:50:47 +09:00
|
|
|
default: true,
|
2020-12-19 10:55:52 +09:00
|
|
|
},
|
|
|
|
navWindow: {
|
|
|
|
where: 'deviceAccount',
|
2022-06-12 19:50:47 +09:00
|
|
|
default: true,
|
2020-12-19 10:55:52 +09:00
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
2021-01-11 20:38:34 +09:00
|
|
|
export const loadDeck = async () => {
|
|
|
|
let deck;
|
|
|
|
|
|
|
|
try {
|
|
|
|
deck = await api('i/registry/get', {
|
|
|
|
scope: ['client', 'deck', 'profiles'],
|
|
|
|
key: deckStore.state.profile,
|
|
|
|
});
|
2022-05-19 17:35:43 +09:00
|
|
|
} catch (err) {
|
|
|
|
if (err.code === 'NO_SUCH_KEY') {
|
2021-01-11 20:38:34 +09:00
|
|
|
// 後方互換性のため
|
|
|
|
if (deckStore.state.profile === 'default') {
|
|
|
|
saveDeck();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-16 15:19:44 +09:00
|
|
|
deckStore.set('columns', []);
|
|
|
|
deckStore.set('layout', []);
|
2021-01-11 20:38:34 +09:00
|
|
|
return;
|
|
|
|
}
|
2022-05-19 17:35:43 +09:00
|
|
|
throw err;
|
2021-01-11 20:38:34 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
deckStore.set('columns', deck.columns);
|
|
|
|
deckStore.set('layout', deck.layout);
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: deckがloadされていない状態でsaveすると意図せず上書きが発生するので対策する
|
|
|
|
export const saveDeck = throttle(1000, () => {
|
|
|
|
api('i/registry/set', {
|
|
|
|
scope: ['client', 'deck', 'profiles'],
|
|
|
|
key: deckStore.state.profile,
|
|
|
|
value: {
|
|
|
|
columns: deckStore.reactiveState.columns.value,
|
|
|
|
layout: deckStore.reactiveState.layout.value,
|
2022-06-12 19:50:47 +09:00
|
|
|
},
|
2021-01-11 20:38:34 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-07-16 15:19:44 +09:00
|
|
|
export async function getProfiles(): Promise<string[]> {
|
|
|
|
return await api('i/registry/keys', {
|
|
|
|
scope: ['client', 'deck', 'profiles'],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteProfile(key: string): Promise<void> {
|
|
|
|
return await api('i/registry/remove', {
|
|
|
|
scope: ['client', 'deck', 'profiles'],
|
|
|
|
key: key,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-19 10:55:52 +09:00
|
|
|
export function addColumn(column: Column) {
|
2022-05-19 17:35:43 +09:00
|
|
|
if (column.name === undefined) column.name = null;
|
2020-12-19 10:55:52 +09:00
|
|
|
deckStore.push('columns', column);
|
|
|
|
deckStore.push('layout', [column.id]);
|
2021-01-11 20:38:34 +09:00
|
|
|
saveDeck();
|
2020-12-19 10:55:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export function removeColumn(id: Column['id']) {
|
|
|
|
deckStore.set('columns', deckStore.state.columns.filter(c => c.id !== id));
|
|
|
|
deckStore.set('layout', deckStore.state.layout
|
|
|
|
.map(ids => ids.filter(_id => _id !== id))
|
|
|
|
.filter(ids => ids.length > 0));
|
2021-01-11 20:38:34 +09:00
|
|
|
saveDeck();
|
2020-12-19 10:55:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export function swapColumn(a: Column['id'], b: Column['id']) {
|
2022-05-19 17:35:43 +09:00
|
|
|
const aX = deckStore.state.layout.findIndex(ids => ids.indexOf(a) !== -1);
|
|
|
|
const aY = deckStore.state.layout[aX].findIndex(id => id === a);
|
|
|
|
const bX = deckStore.state.layout.findIndex(ids => ids.indexOf(b) !== -1);
|
|
|
|
const bY = deckStore.state.layout[bX].findIndex(id => id === b);
|
2022-11-17 09:31:07 +09:00
|
|
|
const layout = deepClone(deckStore.state.layout);
|
2020-12-19 10:55:52 +09:00
|
|
|
layout[aX][aY] = b;
|
|
|
|
layout[bX][bY] = a;
|
|
|
|
deckStore.set('layout', layout);
|
2021-01-11 20:38:34 +09:00
|
|
|
saveDeck();
|
2020-12-19 10:55:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export function swapLeftColumn(id: Column['id']) {
|
2022-11-17 09:31:07 +09:00
|
|
|
const layout = deepClone(deckStore.state.layout);
|
2020-12-19 10:55:52 +09:00
|
|
|
deckStore.state.layout.some((ids, i) => {
|
|
|
|
if (ids.includes(id)) {
|
|
|
|
const left = deckStore.state.layout[i - 1];
|
|
|
|
if (left) {
|
|
|
|
layout[i - 1] = deckStore.state.layout[i];
|
|
|
|
layout[i] = left;
|
|
|
|
deckStore.set('layout', layout);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2021-01-11 20:38:34 +09:00
|
|
|
saveDeck();
|
2020-12-19 10:55:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export function swapRightColumn(id: Column['id']) {
|
2022-11-17 09:31:07 +09:00
|
|
|
const layout = deepClone(deckStore.state.layout);
|
2020-12-19 10:55:52 +09:00
|
|
|
deckStore.state.layout.some((ids, i) => {
|
|
|
|
if (ids.includes(id)) {
|
|
|
|
const right = deckStore.state.layout[i + 1];
|
|
|
|
if (right) {
|
|
|
|
layout[i + 1] = deckStore.state.layout[i];
|
|
|
|
layout[i] = right;
|
|
|
|
deckStore.set('layout', layout);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2021-01-11 20:38:34 +09:00
|
|
|
saveDeck();
|
2020-12-19 10:55:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export function swapUpColumn(id: Column['id']) {
|
2022-11-17 09:31:07 +09:00
|
|
|
const layout = deepClone(deckStore.state.layout);
|
2020-12-19 10:55:52 +09:00
|
|
|
const idsIndex = deckStore.state.layout.findIndex(ids => ids.includes(id));
|
2022-11-17 09:31:07 +09:00
|
|
|
const ids = deepClone(deckStore.state.layout[idsIndex]);
|
2020-12-19 10:55:52 +09:00
|
|
|
ids.some((x, i) => {
|
|
|
|
if (x === id) {
|
|
|
|
const up = ids[i - 1];
|
|
|
|
if (up) {
|
|
|
|
ids[i - 1] = id;
|
|
|
|
ids[i] = up;
|
|
|
|
|
|
|
|
layout[idsIndex] = ids;
|
|
|
|
deckStore.set('layout', layout);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2021-01-11 20:38:34 +09:00
|
|
|
saveDeck();
|
2020-12-19 10:55:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export function swapDownColumn(id: Column['id']) {
|
2022-11-17 09:31:07 +09:00
|
|
|
const layout = deepClone(deckStore.state.layout);
|
2020-12-19 10:55:52 +09:00
|
|
|
const idsIndex = deckStore.state.layout.findIndex(ids => ids.includes(id));
|
2022-11-17 09:31:07 +09:00
|
|
|
const ids = deepClone(deckStore.state.layout[idsIndex]);
|
2020-12-19 10:55:52 +09:00
|
|
|
ids.some((x, i) => {
|
|
|
|
if (x === id) {
|
|
|
|
const down = ids[i + 1];
|
|
|
|
if (down) {
|
|
|
|
ids[i + 1] = id;
|
|
|
|
ids[i] = down;
|
|
|
|
|
|
|
|
layout[idsIndex] = ids;
|
|
|
|
deckStore.set('layout', layout);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2021-01-11 20:38:34 +09:00
|
|
|
saveDeck();
|
2020-12-19 10:55:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export function stackLeftColumn(id: Column['id']) {
|
2022-11-17 09:31:07 +09:00
|
|
|
let layout = deepClone(deckStore.state.layout);
|
2020-12-19 10:55:52 +09:00
|
|
|
const i = deckStore.state.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);
|
|
|
|
deckStore.set('layout', layout);
|
2021-01-11 20:38:34 +09:00
|
|
|
saveDeck();
|
2020-12-19 10:55:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export function popRightColumn(id: Column['id']) {
|
2022-11-17 09:31:07 +09:00
|
|
|
let layout = deepClone(deckStore.state.layout);
|
2020-12-19 10:55:52 +09:00
|
|
|
const i = deckStore.state.layout.findIndex(ids => ids.includes(id));
|
2021-10-15 02:42:10 +09:00
|
|
|
const affected = layout[i];
|
2020-12-19 10:55:52 +09:00
|
|
|
layout = layout.map(ids => ids.filter(_id => _id !== id));
|
|
|
|
layout.splice(i + 1, 0, [id]);
|
|
|
|
layout = layout.filter(ids => ids.length > 0);
|
|
|
|
deckStore.set('layout', layout);
|
2021-10-15 02:42:10 +09:00
|
|
|
|
2022-11-17 09:31:07 +09:00
|
|
|
const columns = deepClone(deckStore.state.columns);
|
2021-10-15 02:42:10 +09:00
|
|
|
for (const column of columns) {
|
|
|
|
if (affected.includes(column.id)) {
|
|
|
|
column.active = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deckStore.set('columns', columns);
|
|
|
|
|
2021-01-11 20:38:34 +09:00
|
|
|
saveDeck();
|
2020-12-19 10:55:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export function addColumnWidget(id: Column['id'], widget: ColumnWidget) {
|
2022-11-17 09:31:07 +09:00
|
|
|
const columns = deepClone(deckStore.state.columns);
|
2020-12-19 10:55:52 +09:00
|
|
|
const columnIndex = deckStore.state.columns.findIndex(c => c.id === id);
|
2022-11-17 09:31:07 +09:00
|
|
|
const column = deepClone(deckStore.state.columns[columnIndex]);
|
2020-12-19 10:55:52 +09:00
|
|
|
if (column == null) return;
|
|
|
|
if (column.widgets == null) column.widgets = [];
|
|
|
|
column.widgets.unshift(widget);
|
|
|
|
columns[columnIndex] = column;
|
|
|
|
deckStore.set('columns', columns);
|
2021-01-11 20:38:34 +09:00
|
|
|
saveDeck();
|
2020-12-19 10:55:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export function removeColumnWidget(id: Column['id'], widget: ColumnWidget) {
|
2022-11-17 09:31:07 +09:00
|
|
|
const columns = deepClone(deckStore.state.columns);
|
2020-12-19 10:55:52 +09:00
|
|
|
const columnIndex = deckStore.state.columns.findIndex(c => c.id === id);
|
2022-11-17 09:31:07 +09:00
|
|
|
const column = deepClone(deckStore.state.columns[columnIndex]);
|
2020-12-19 10:55:52 +09:00
|
|
|
if (column == null) return;
|
2022-05-19 17:35:43 +09:00
|
|
|
column.widgets = column.widgets.filter(w => w.id !== widget.id);
|
2020-12-19 10:55:52 +09:00
|
|
|
columns[columnIndex] = column;
|
|
|
|
deckStore.set('columns', columns);
|
2021-01-11 20:38:34 +09:00
|
|
|
saveDeck();
|
2020-12-19 10:55:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setColumnWidgets(id: Column['id'], widgets: ColumnWidget[]) {
|
2022-11-17 09:31:07 +09:00
|
|
|
const columns = deepClone(deckStore.state.columns);
|
2020-12-19 10:55:52 +09:00
|
|
|
const columnIndex = deckStore.state.columns.findIndex(c => c.id === id);
|
2022-11-17 09:31:07 +09:00
|
|
|
const column = deepClone(deckStore.state.columns[columnIndex]);
|
2020-12-19 10:55:52 +09:00
|
|
|
if (column == null) return;
|
|
|
|
column.widgets = widgets;
|
|
|
|
columns[columnIndex] = column;
|
|
|
|
deckStore.set('columns', columns);
|
2021-01-11 20:38:34 +09:00
|
|
|
saveDeck();
|
2020-12-19 10:55:52 +09:00
|
|
|
}
|
|
|
|
|
2022-06-12 19:50:43 +09:00
|
|
|
export function updateColumnWidget(id: Column['id'], widgetId: string, widgetData: any) {
|
2022-11-17 09:31:07 +09:00
|
|
|
const columns = deepClone(deckStore.state.columns);
|
2020-12-19 10:55:52 +09:00
|
|
|
const columnIndex = deckStore.state.columns.findIndex(c => c.id === id);
|
2022-11-17 09:31:07 +09:00
|
|
|
const column = deepClone(deckStore.state.columns[columnIndex]);
|
2020-12-19 10:55:52 +09:00
|
|
|
if (column == null) return;
|
|
|
|
column.widgets = column.widgets.map(w => w.id === widgetId ? {
|
|
|
|
...w,
|
2022-05-26 22:53:09 +09:00
|
|
|
data: widgetData,
|
2020-12-19 10:55:52 +09:00
|
|
|
} : w);
|
|
|
|
columns[columnIndex] = column;
|
|
|
|
deckStore.set('columns', columns);
|
2021-01-11 20:38:34 +09:00
|
|
|
saveDeck();
|
2020-12-19 10:55:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export function updateColumn(id: Column['id'], column: Partial<Column>) {
|
2022-11-17 09:31:07 +09:00
|
|
|
const columns = deepClone(deckStore.state.columns);
|
2020-12-19 10:55:52 +09:00
|
|
|
const columnIndex = deckStore.state.columns.findIndex(c => c.id === id);
|
2022-11-17 09:31:07 +09:00
|
|
|
const currentColumn = deepClone(deckStore.state.columns[columnIndex]);
|
2020-12-19 10:55:52 +09:00
|
|
|
if (currentColumn == null) return;
|
|
|
|
for (const [k, v] of Object.entries(column)) {
|
|
|
|
currentColumn[k] = v;
|
|
|
|
}
|
|
|
|
columns[columnIndex] = currentColumn;
|
|
|
|
deckStore.set('columns', columns);
|
2021-01-11 20:38:34 +09:00
|
|
|
saveDeck();
|
2020-12-19 10:55:52 +09:00
|
|
|
}
|