| import * as Y from "yjs"; |
|
|
| |
| |
| |
| |
| |
| |
| |
| export interface EmbedDataFileMeta { |
| name: string; |
| |
| ext: string; |
| |
| size: number; |
| |
| uploader?: string; |
| |
| addedAt: number; |
| |
| rowCount?: number; |
| columns?: string[]; |
| } |
|
|
| export interface EmbedDataFile { |
| meta: EmbedDataFileMeta; |
| |
| content: string; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function createEmbedDataStore(ydoc: Y.Doc) { |
| const ymap = ydoc.getMap<EmbedDataFile>("embed-data"); |
|
|
| function get(name: string): EmbedDataFile | undefined { |
| return ymap.get(name); |
| } |
|
|
| function getContent(name: string): string { |
| return ymap.get(name)?.content ?? ""; |
| } |
|
|
| function getMeta(name: string): EmbedDataFileMeta | undefined { |
| return ymap.get(name)?.meta; |
| } |
|
|
| function has(name: string): boolean { |
| return ymap.has(name); |
| } |
|
|
| function set(file: EmbedDataFile) { |
| ymap.set(file.meta.name, file); |
| } |
|
|
| function remove(name: string) { |
| ymap.delete(name); |
| } |
|
|
| function rename(oldName: string, newName: string) { |
| const file = ymap.get(oldName); |
| if (!file) return; |
| ydoc.transact(() => { |
| ymap.set(newName, { ...file, meta: { ...file.meta, name: newName } }); |
| ymap.delete(oldName); |
| }); |
| } |
|
|
| function keys(): string[] { |
| return Array.from(ymap.keys()); |
| } |
|
|
| function list(): EmbedDataFileMeta[] { |
| const items: EmbedDataFileMeta[] = []; |
| ymap.forEach((file) => { |
| items.push(file.meta); |
| }); |
| return items.sort((a, b) => a.name.localeCompare(b.name)); |
| } |
|
|
| function observe(callback: () => void) { |
| ymap.observe(callback); |
| return () => ymap.unobserve(callback); |
| } |
|
|
| return { |
| get, |
| getContent, |
| getMeta, |
| has, |
| set, |
| remove, |
| rename, |
| keys, |
| list, |
| observe, |
| }; |
| } |
|
|
| export type EmbedDataStore = ReturnType<typeof createEmbedDataStore>; |
|
|