import { useEntity, useReplicache } from "src/replicache"; import { useEntitySetContext } from "./EntitySetProvider"; import { v7 } from "uuid"; import { BaseBlock } from "./Blocks/Block"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { useDrag } from "src/hooks/useDrag"; import { useLongPress } from "src/hooks/useLongPress"; import { focusBlock } from "src/utils/focusBlock"; import { elementId } from "src/utils/elementId"; import { useUIState } from "src/useUIState"; import useMeasure from "react-use-measure"; import { useIsMobile } from "src/hooks/isMobile"; import { Media } from "./Media"; import { TooltipButton } from "./Buttons"; import { useBlockKeyboardHandlers } from "./Blocks/useBlockKeyboardHandlers"; import { AddSmall } from "./Icons/AddSmall"; export function Canvas(props: { entityID: string; preview?: boolean }) { let entity_set = useEntitySetContext(); let ref = useRef(null); useEffect(() => { let abort = new AbortController(); let isTouch = false; let startX: number, startY: number, scrollLeft: number, scrollTop: number; let el = ref.current; ref.current?.addEventListener( "wheel", (e) => { if (!el) return; if ( (e.deltaX > 0 && el.scrollLeft >= el.scrollWidth - el.clientWidth) || (e.deltaX < 0 && el.scrollLeft <= 0) || (e.deltaY > 0 && el.scrollTop >= el.scrollHeight - el.clientHeight) || (e.deltaY < 0 && el.scrollTop <= 0) ) { return; } e.preventDefault(); el.scrollLeft += e.deltaX; el.scrollTop += e.deltaY; }, { passive: false, signal: abort.signal }, ); return () => abort.abort(); }); let narrowWidth = useEntity(props.entityID, "canvas/narrow-width")?.data .value; return (
); } export function CanvasContent(props: { entityID: string; preview?: boolean }) { let blocks = useEntity(props.entityID, "canvas/block"); let { rep } = useReplicache(); let entity_set = useEntitySetContext(); let height = Math.max(...blocks.map((f) => f.data.position.y), 0); return (
{ if (e.currentTarget !== e.target) return; useUIState.setState(() => ({ selectedBlocks: [], focusedEntity: { entityType: "page", entityID: props.entityID }, })); useUIState.setState({ focusedEntity: { entityType: "page", entityID: props.entityID }, }); document .getElementById(elementId.page(props.entityID).container) ?.scrollIntoView({ behavior: "smooth", inline: "nearest", }); if (e.detail === 2 || e.ctrlKey || e.metaKey) { let parentRect = e.currentTarget.getBoundingClientRect(); let newEntityID = v7(); await rep?.mutate.addCanvasBlock({ newEntityID, parent: props.entityID, position: { x: Math.max(e.clientX - parentRect.left, 0), y: Math.max(e.clientY - parentRect.top - 12, 0), }, factID: v7(), type: "text", permission_set: entity_set.set, }); focusBlock( { type: "text", parent: props.entityID, value: newEntityID }, { type: "start" }, ); } }} style={{ minHeight: height + 512, contain: "size layout paint", }} className="relative h-full w-[1272px]" > {blocks .sort((a, b) => { if (a.data.position.y === b.data.position.y) { return a.data.position.x - b.data.position.x; } return a.data.position.y - b.data.position.y; }) .map((b) => { return ( ); })}
); } function CanvasWidthHandle(props: { entityID: string }) { let canvasFocused = useUIState((s) => s.focusedEntity?.entityType === "page"); let { rep } = useReplicache(); let narrowWidth = useEntity(props.entityID, "canvas/narrow-width")?.data .value; return (