tangled
alpha
login
or
join now
leaflet.pub
/
leaflet
289
fork
atom
a tool for shared writing and social publishing
289
fork
atom
overview
issues
28
pulls
pipelines
handle canvases better
awarm.space
3 months ago
9c57409a
a2401dd1
+24
-10
1 changed file
expand all
collapse all
unified
split
app
[leaflet_id]
actions
PublishButton.tsx
+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
31
-
import { useBlocks } from "src/hooks/queries/useBlocks";
31
31
+
import {
32
32
+
useBlocks,
33
33
+
useCanvasBlocksWithType,
34
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
373
-
let blocks = useBlocks(rootPage);
374
374
-
let firstBlock = blocks[0];
376
376
+
let canvasBlocks = useCanvasBlocksWithType(rootPage).filter(
377
377
+
(b) => b.type === "text" || b.type === "heading",
378
378
+
);
379
379
+
let blocks = useBlocks(rootPage).filter(
380
380
+
(b) => b.type === "text" || b.type === "heading",
381
381
+
);
382
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
387
-
let secondBlock = blocks[1];
388
388
-
let secondBlockTextValue = useEntity(secondBlock?.value, "block/text")?.data
389
389
-
.value;
395
395
+
// Only handle second block logic for linear documents, not canvas
396
396
+
let isCanvas = canvasBlocks.length > 0;
397
397
+
let secondBlock = !isCanvas ? blocks[1] : undefined;
398
398
+
let secondBlockTextValue = useEntity(secondBlock?.value || null, "block/text")
399
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
397
-
}, [firstBlockText]);
407
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
405
-
// Delete second block if it's empty text
406
406
-
if (secondBlockText.trim() === "" && secondBlock?.type === "text") {
415
415
+
// Delete second block if it's empty text (only for linear documents)
416
416
+
if (
417
417
+
!isCanvas &&
418
418
+
secondBlockText.trim() === "" &&
419
419
+
secondBlock?.type === "text"
420
420
+
) {
407
421
etod.push(secondBlock.value);
408
422
}
409
423
return etod;
410
410
-
}, [firstBlock, secondBlockText, secondBlock]);
424
424
+
}, [firstBlock, secondBlockText, secondBlock, isCanvas]);
411
425
412
426
return { title: leafletTitle, entitiesToDelete };
413
427
};