mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-24 13:58:50 +08:00
refactor(editor): extract database block (#9435)
Part of: [BS-2269](https://linear.app/affine-design/issue/BS-2269/%E8%BF%81%E7%A7%BB-database-block-%E5%88%B0-affine-%E6%96%87%E4%BB%B6%E5%A4%B9%E4%B8%8B%E5%B9%B6%E5%BC%80%E5%90%AF-nouncheckedindexedaccess)
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