mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-21 03:51:45 +08:00
chore(editor): reorg packages (#10702)
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const currentViewListSchema = z.array(
|
||||
z.object({
|
||||
blockId: z.string(),
|
||||
viewId: z.string(),
|
||||
})
|
||||
);
|
||||
const maxLength = 20;
|
||||
const currentViewListKey = 'blocksuite:databaseBlock:view:currentViewList';
|
||||
const storage = globalThis.sessionStorage;
|
||||
const createCurrentViewStorage = () => {
|
||||
const getList = () => {
|
||||
const string = storage?.getItem(currentViewListKey);
|
||||
if (!string) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const result = currentViewListSchema.safeParse(JSON.parse(string));
|
||||
if (result.success) {
|
||||
return result.data;
|
||||
}
|
||||
} catch {
|
||||
// do nothing
|
||||
}
|
||||
return;
|
||||
};
|
||||
const saveList = () => {
|
||||
storage.setItem(currentViewListKey, JSON.stringify(list));
|
||||
};
|
||||
|
||||
const list = getList() ?? [];
|
||||
|
||||
return {
|
||||
getCurrentView: (blockId: string) => {
|
||||
return list.find(item => item.blockId === blockId)?.viewId;
|
||||
},
|
||||
setCurrentView: (blockId: string, viewId: string) => {
|
||||
const configIndex = list.findIndex(item => item.blockId === blockId);
|
||||
if (configIndex >= 0) {
|
||||
list.splice(configIndex, 1);
|
||||
}
|
||||
if (list.length >= maxLength) {
|
||||
list.pop();
|
||||
}
|
||||
list.unshift({ blockId, viewId });
|
||||
saveList();
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const currentViewStorage = createCurrentViewStorage();
|
||||
Reference in New Issue
Block a user