's bottom padding, which,
// unlike a margin, adds to the next block's top margin. min-h-9 keeps
// the 28px row on top of that padding.
props.listData && !props.nextBlock?.listData ? "pb-2 min-h-9!" : ""
}
${props.listData ? "isListItem" : ""}`}
>
{!props.preview && }
{dropIndicator && }
{props.preview || !entity_set.permissions.write ? (
baseBlock
) : (
{baseBlock}
)}
);
}, deepEqualsBlockProps);
// Registers the block as a draggable and a drop target, and hands the
// activator down to its handles: the ListMarker through ListDragHandleContext
// and, for blocks that aren't text editors, the body through
// BlockBodyDragContext (read by BlockLayout). Lives in its own leaf component
// because dnd-kit's hooks re-render their consumers on every `over` change
// during a drag: everything they touch here is memoized, so the block subtree
// (passed through as `children`) bails out and only this wiring re-renders.
// The measured node is an inset overlay div rather than the block wrapper
// itself for the same reason.
const BlockListDnd = (props: {
entityID: string;
isListItem: boolean;
bodyDraggable: boolean;
children: React.ReactNode;
}) => {
let draggable = useDraggable({
id: props.entityID,
disabled: !props.isListItem && !props.bodyDraggable,
});
let droppable = useDroppable({ id: props.entityID });
let setNodeRef = useCallback(
(el: HTMLElement | null) => {
draggable.setNodeRef(el);
droppable.setNodeRef(el);
},
[draggable.setNodeRef, droppable.setNodeRef],
);
let { attributes, listeners, setActivatorNodeRef } = draggable;
let dragHandle = useMemo(
() =>
props.isListItem ? { attributes, listeners, setActivatorNodeRef } : null,
[props.isListItem, attributes, listeners, setActivatorNodeRef],
);
return (
<>
{props.children}
>
);
};
// The line marking where a dragged list item will drop, drawn on the edge of
// the adjacent block and indented to the depth the item will land at.
const ListDropIndicator = (props: { indicator: string }) => {
let [edge, depth] = props.indicator.split(":");
return (
);
};
// Everything the Block render tree reads off its props. headingLevel /
// headingPath / listData.checked / listData.listStart are deliberately not
// compared: they're re-fetched via useEntity or unused by this subtree.
function deepEqualsBlockProps(
prevProps: BlockProps & { preview?: boolean },
nextProps: BlockProps & { preview?: boolean },
): boolean {
const scalarKeys = [
"pageType",
"entityID",
"parent",
"position",
"factID",
"type",
"nextPosition",
"headingFoldable",
"displayDepth",
"preview",
] as const;
if (scalarKeys.some((k) => prevProps[k] !== nextProps[k])) return false;
if (!listDataEquals(prevProps.listData, nextProps.listData)) return false;
return (
neighborEquals(prevProps.nextBlock, nextProps.nextBlock) &&
neighborEquals(prevProps.previousBlock, nextProps.previousBlock)
);
}
function listDataEquals(prev: Block["listData"], next: Block["listData"]) {
if (prev === next) return true;
if (!prev || !next) return false;
return (
prev.checklist === next.checklist &&
prev.parent === next.parent &&
prev.depth === next.depth &&
prev.displayNumber === next.displayNumber &&
prev.listStyle === next.listStyle &&
prev.path.length === next.path.length &&
prev.path.every(
(p, i) =>
p.depth === next.path[i].depth && p.entity === next.path[i].entity,
)
);
}
function neighborEquals(prev: Block | null, next: Block | null) {
if (prev === next) return true;
if (!prev || !next) return false;
return (
prev.factID === next.factID &&
prev.parent === next.parent &&
prev.position === next.position &&
prev.entityID === next.entityID &&
prev.type === next.type &&
deepEquals(prev.listData, next.listData)
);
}
export const BaseBlock = (
props: BlockProps & {
preview?: boolean;
areYouSure?: boolean;
setAreYouSure?: (value: boolean) => void;
},
) => {
// BaseBlock renders the actual block content, delete states, controls spacing between block and list markers
let BlockTypeComponent = BlockTypeComponents[props.type];
if (!BlockTypeComponent) return
unknown block
;
return (
<>
{props.headingFoldable && }
{props.listData && }
{props.areYouSure ? (
props.setAreYouSure && props.setAreYouSure(false)
}
type={props.type}
entityID={props.entityID}
/>
) : (
)}
>
);
};
const BlockTypeComponents: {
[K in Fact<"block/type">["data"]["value"]]: React.ComponentType<
BlockProps & { preview?: boolean }
>;
} = {
code: CodeBlock,
math: MathBlock,
card: PageLinkBlock,
"embedded-canvas": EmbeddedCanvasBlock,
text: TextBlock,
blockquote: TextBlock,
heading: TextBlock,
image: ImageBlock,
"image-gallery": ImageGalleryBlock,
link: ExternalLinkBlock,
embed: EmbedBlock,
html: HTMLBlock,
mailbox: MailboxBlock,
datetime: DateTimeBlock,
rsvp: RSVPBlock,
button: ButtonBlock,
poll: PollBlock,
"bluesky-post": BlueskyPostBlock,
"standard-site-post": StandardSitePostBlock,
"standard-site-publication": StandardSitePublicationBlock,
"horizontal-rule": HorizontalRule,
"members-only-delimiter": MembersOnlyDelimiterBlock,
"posts-list": PostsListBlock,
"recommended-pubs": RecommendedPubsBlock,
reply: ReplyBlock,
questions: QuestionsBlock,
signup: SubscribeBlock,
"post-header": PostHeaderBlock,
group: GroupBlock,
drawing: DrawingBlock,
};
// Only rendered on canvases. The Blocks list pads each block for a page; pull
// it back out so a block keeps its place on the canvas when it becomes the
// first block of a group.
function GroupBlock(props: React.ComponentProps) {
let focusedBlock = useUIState((s) =>
s.focusedEntity?.entityType === "block" &&
(s.focusedEntity.entityID === props.entityID ||
s.focusedEntity.parent === props.entityID)
? s.focusedEntity.entityID
: null,
);
let focusedType = useEntity(focusedBlock, "block/type")?.data.value;
// Any other child brings an options bar of its own.
let showOptions =
focusedBlock === props.entityID ||
(!!focusedType && isTextBlock[focusedType]);
return (
{focusedBlock && (
// Drawn around the children's text box, where a selected lone canvas
// block draws its border.
{showOptions && !props.preview && (
)}
)}
);
}
function spacingKind(block: Pick): SpacingKind {
if (block.type === "blockquote") return "blockquote";
if (block.listData) return "list";
if (block.type === "heading") return "heading";
if (block.type === "horizontal-rule") return "horizontal-rule";
return "other";
}
const BlockMultiselectIndicator = (props: BlockProps) => {
let first = props.previousBlock === null;
// One packed primitive selector instead of three array scans: null when not
// multiselected, otherwise which neighbors are also selected (for corner
// rounding).
let multiselectState = useUIState((s) => {
if (
s.selectedBlocks.length <= 1 ||
!s.selectedBlocks.some((b) => b.entityID === props.entityID)
)
return null;
let next = s.selectedBlocks.some(
(b) => b.entityID === props.nextBlock?.entityID,
);
let prev = s.selectedBlocks.some(
(b) => b.entityID === props.previousBlock?.entityID,
);
return `${next ? "n" : ""}${prev ? "p" : ""}`;
});
if (multiselectState !== null)
return (
);
};
export const BlockLayout = (props: {
isSelected: boolean;
children: React.ReactNode;
className?: string;
optionsClassName?: string;
hasBackground?: "accent" | "page";
borderOnHover?: boolean;
hasAlignment?: boolean;
areYouSure?: boolean;
setAreYouSure?: (value: boolean) => void;
extraOptions?: React.ReactNode;
}) => {
let bodyDrag = useBlockBodyDrag();
return (
{
if (
(e.target as Element).closest(
"button, a, input, textarea, select, [contenteditable], [data-draggable]",
)
)
return;
bodyDrag.onPointerDown?.(e);
}
: undefined
}
>
{/* A block's own list marker sits beside its body, so any ListMarker
rendered inside (a page link's preview of its page's title) must
not inherit the handle and pick this block up. */}
{props.children}
{props.isSelected && (
)}
);
};
let debounced: null | number = null;
const NonTextBlockOptions = (props: {
areYouSure?: boolean;
setAreYouSure?: (value: boolean) => void;
optionsClassName?: string;
extraOptions?: React.ReactNode;
// The block the bar acts on, when that isn't the focused one: a canvas
// group shows the bar while one of its children has focus.
block?: { entityID: string; parent: string };
}) => {
let { rep, undoManager } = useReplicache();
let entity_set = useEntitySetContext();
let focusedEntity = useUIState((s) => s.focusedEntity);
let block =
props.block ??
(focusedEntity?.entityType === "block" ? focusedEntity : undefined);
let pageType = useEntity(block?.parent || null, "page/type");
let isMultiselected = useUIState((s) => s.selectedBlocks.length > 1);
if (!block) return;
if (isMultiselected) return;
if (!entity_set.permissions.write) return null;
let { entityID, parent } = block;
return (