|
| 1 | +'use node' |
| 2 | + |
| 3 | +import * as Y from 'yjs' |
| 4 | +import { v } from 'convex/values' |
| 5 | +import { BlockNoteEditor } from '@blocknote/core' |
| 6 | +import { blocksToYDoc, yDocToBlocks } from '@blocknote/core/yjs' |
| 7 | +import { internalAction } from '../_generated/server' |
| 8 | +import { internal } from '../_generated/api' |
| 9 | +import { customBlockValidator } from '../blocks/schema' |
| 10 | +import { editorSchema } from './editorSpecs' |
| 11 | +import type { CustomBlock } from './editorSpecs' |
| 12 | + |
| 13 | +function uint8ToArrayBuffer(uint8: Uint8Array): ArrayBuffer { |
| 14 | + if (uint8.byteOffset === 0 && uint8.byteLength === uint8.buffer.byteLength) { |
| 15 | + return uint8.buffer as ArrayBuffer |
| 16 | + } |
| 17 | + return uint8.buffer.slice( |
| 18 | + uint8.byteOffset, |
| 19 | + uint8.byteOffset + uint8.byteLength, |
| 20 | + ) as ArrayBuffer |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Converts blocks to a Yjs update and pushes it as the initial state |
| 25 | + * for a newly created note. Runs in Node.js to support @blocknote/core. |
| 26 | + */ |
| 27 | +export const initializeNoteContent = internalAction({ |
| 28 | + args: { |
| 29 | + noteId: v.id('notes'), |
| 30 | + content: v.array(customBlockValidator), |
| 31 | + }, |
| 32 | + handler: async (ctx, { noteId, content }) => { |
| 33 | + if (content.length === 0) return |
| 34 | + |
| 35 | + const editor = BlockNoteEditor.create({ |
| 36 | + schema: editorSchema, |
| 37 | + _headless: true, |
| 38 | + }) |
| 39 | + let doc: Y.Doc | undefined |
| 40 | + try { |
| 41 | + doc = blocksToYDoc(editor, content as Array<CustomBlock>) |
| 42 | + const update = uint8ToArrayBuffer(Y.encodeStateAsUpdate(doc)) |
| 43 | + await ctx.runMutation( |
| 44 | + internal.notes.internalMutations.pushYjsUpdateInternal, |
| 45 | + { |
| 46 | + documentId: noteId, |
| 47 | + update, |
| 48 | + }, |
| 49 | + ) |
| 50 | + } finally { |
| 51 | + doc?.destroy() |
| 52 | + editor._tiptapEditor.destroy() |
| 53 | + } |
| 54 | + }, |
| 55 | +}) |
| 56 | + |
| 57 | +/** |
| 58 | + * Reads the Yjs document for a note, converts it to blocks, and saves them. |
| 59 | + * Runs in Node.js to support @blocknote/core. |
| 60 | + */ |
| 61 | +export const persistNoteBlocksNode = internalAction({ |
| 62 | + args: { |
| 63 | + documentId: v.id('notes'), |
| 64 | + }, |
| 65 | + handler: async (ctx, { documentId }) => { |
| 66 | + const updates = await ctx.runQuery( |
| 67 | + internal.yjsSync.internalQueries.getUpdatesInternal, |
| 68 | + { documentId }, |
| 69 | + ) |
| 70 | + |
| 71 | + const doc = new Y.Doc() |
| 72 | + let editor: ReturnType<typeof BlockNoteEditor.create> | undefined |
| 73 | + try { |
| 74 | + for (const entry of updates) { |
| 75 | + Y.applyUpdate(doc, new Uint8Array(entry.update)) |
| 76 | + } |
| 77 | + |
| 78 | + editor = BlockNoteEditor.create({ |
| 79 | + schema: editorSchema, |
| 80 | + _headless: true, |
| 81 | + }) |
| 82 | + const blocks = yDocToBlocks(editor, doc) |
| 83 | + |
| 84 | + await ctx.runMutation( |
| 85 | + internal.notes.internalMutations.saveNoteBlocksInternal, |
| 86 | + { noteId: documentId, content: blocks }, |
| 87 | + ) |
| 88 | + } finally { |
| 89 | + doc.destroy() |
| 90 | + editor?._tiptapEditor.destroy() |
| 91 | + } |
| 92 | + }, |
| 93 | +}) |
0 commit comments