| import * as Y from "yjs"; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function createEmbedStore(ydoc: Y.Doc) { |
| const ymap = ydoc.getMap<string>("embeds"); |
|
|
| function get(src: string): string { |
| return ymap.get(src) ?? ""; |
| } |
|
|
| function set(src: string, html: string) { |
| ymap.set(src, html); |
| } |
|
|
| function has(src: string): boolean { |
| return ymap.has(src); |
| } |
|
|
| function remove(src: string) { |
| ymap.delete(src); |
| } |
|
|
| function rename(oldSrc: string, newSrc: string) { |
| const html = ymap.get(oldSrc); |
| if (html !== undefined) { |
| ydoc.transact(() => { |
| ymap.set(newSrc, html); |
| ymap.delete(oldSrc); |
| }); |
| } |
| } |
|
|
| |
| function patch(src: string, search: string, replace: string): boolean { |
| const html = ymap.get(src); |
| if (!html || !html.includes(search)) return false; |
| ymap.set(src, html.replace(search, replace)); |
| return true; |
| } |
|
|
| function keys(): string[] { |
| return Array.from(ymap.keys()); |
| } |
|
|
| function getAll(): Record<string, string> { |
| const result: Record<string, string> = {}; |
| ymap.forEach((val, key) => { |
| result[key] = val; |
| }); |
| return result; |
| } |
|
|
| |
| |
| |
| |
| function observe(callback: () => void) { |
| ymap.observe(callback); |
| return () => ymap.unobserve(callback); |
| } |
|
|
| |
| |
| |
| |
| function observeKey(src: string, callback: (html: string) => void) { |
| const handler = (event: Y.YMapEvent<string>) => { |
| if (event.keysChanged.has(src)) { |
| callback(ymap.get(src) ?? ""); |
| } |
| }; |
| ymap.observe(handler); |
| return () => ymap.unobserve(handler); |
| } |
|
|
| return { |
| get, |
| set, |
| has, |
| remove, |
| rename, |
| patch, |
| keys, |
| getAll, |
| observe, |
| observeKey, |
| }; |
| } |
|
|
| export type EmbedStore = ReturnType<typeof createEmbedStore>; |
|
|