a tool for shared writing and social publishing

handle canvases better

+24 -10
+24 -10
app/[leaflet_id]/actions/PublishButton.tsx
··· 28 28 import { useIsMobile } from "src/hooks/isMobile"; 29 29 import { useReplicache, useEntity } from "src/replicache"; 30 30 import { Json } from "supabase/database.types"; 31 - import { useBlocks } from "src/hooks/queries/useBlocks"; 31 + import { 32 + useBlocks, 33 + useCanvasBlocksWithType, 34 + } from "src/hooks/queries/useBlocks"; 32 35 import * as Y from "yjs"; 33 36 import * as base64 from "base64-js"; 34 37 import { YJSFragmentToString } from "components/Blocks/TextBlock/RenderYJSFragment"; ··· 370 373 371 374 let useTitle = (entityID: string) => { 372 375 let rootPage = useEntity(entityID, "root/page")[0].data.value; 373 - let blocks = useBlocks(rootPage); 374 - let firstBlock = blocks[0]; 376 + let canvasBlocks = useCanvasBlocksWithType(rootPage).filter( 377 + (b) => b.type === "text" || b.type === "heading", 378 + ); 379 + let blocks = useBlocks(rootPage).filter( 380 + (b) => b.type === "text" || b.type === "heading", 381 + ); 382 + let firstBlock = canvasBlocks[0] || blocks[0]; 375 383 376 384 let firstBlockText = useEntity(firstBlock?.value, "block/text")?.data.value; 377 385 ··· 384 392 return YJSFragmentToString(nodes[0]) || "Untitled"; 385 393 }, [firstBlockText]); 386 394 387 - let secondBlock = blocks[1]; 388 - let secondBlockTextValue = useEntity(secondBlock?.value, "block/text")?.data 389 - .value; 395 + // Only handle second block logic for linear documents, not canvas 396 + let isCanvas = canvasBlocks.length > 0; 397 + let secondBlock = !isCanvas ? blocks[1] : undefined; 398 + let secondBlockTextValue = useEntity(secondBlock?.value || null, "block/text") 399 + ?.data.value; 390 400 const secondBlockText = useMemo(() => { 391 401 if (!secondBlockTextValue) return ""; 392 402 let doc = new Y.Doc(); ··· 394 404 Y.applyUpdate(doc, update); 395 405 let nodes = doc.getXmlElement("prosemirror").toArray(); 396 406 return YJSFragmentToString(nodes[0]) || ""; 397 - }, [firstBlockText]); 407 + }, [secondBlockTextValue]); 398 408 399 409 let entitiesToDelete = useMemo(() => { 400 410 let etod: string[] = []; ··· 402 412 if (firstBlock?.type === "heading") { 403 413 etod.push(firstBlock.value); 404 414 } 405 - // Delete second block if it's empty text 406 - if (secondBlockText.trim() === "" && secondBlock?.type === "text") { 415 + // Delete second block if it's empty text (only for linear documents) 416 + if ( 417 + !isCanvas && 418 + secondBlockText.trim() === "" && 419 + secondBlock?.type === "text" 420 + ) { 407 421 etod.push(secondBlock.value); 408 422 } 409 423 return etod; 410 - }, [firstBlock, secondBlockText, secondBlock]); 424 + }, [firstBlock, secondBlockText, secondBlock, isCanvas]); 411 425 412 426 return { title: leafletTitle, entitiesToDelete }; 413 427 };