import { useEntity, useReplicache } from "src/replicache";
import { ToolbarButton } from ".";
import { Separator, ShortcutKey } from "components/Layout";
import { metaKey } from "src/utils/metaKey";
import { getBlocksWithType } from "src/hooks/queries/useBlocks";
import { useUIState } from "src/useUIState";
import { LockBlockButton } from "./LockBlockButton";
import { TextAlignmentButton } from "./TextAlignmentToolbar";
import { ImageFullBleedButton, ImageAltTextButton } from "./ImageToolbar";
import { DeleteSmall } from "components/Icons/DeleteSmall";
export const BlockToolbar = (props: {
setToolbarState: (
state: "areYouSure" | "block" | "text-alignment" | "img-alt-text",
) => void;
}) => {
let focusedEntity = useUIState((s) => s.focusedEntity);
let focusedEntityType = useEntity(
focusedEntity?.entityType === "page"
? focusedEntity.entityID
: focusedEntity?.parent || null,
"page/type",
);
let blockType = useEntity(
focusedEntity?.entityType === "block" ? focusedEntity?.entityID : null,
"block/type",
)?.data.value;
return (
{
props.setToolbarState("areYouSure");
}}
tooltipContent="Delete Block"
>
{blockType === "image" && (
<>
{focusedEntityType?.data.value !== "canvas" && (
)}
>
)}
{(blockType === "button" || blockType === "datetime") && (
<>
{focusedEntityType?.data.value !== "canvas" && (
)}
>
)}
);
};
const MoveBlockButtons = () => {
let { rep } = useReplicache();
const getSortedSelection = async () => {
let selectedBlocks = useUIState.getState().selectedBlocks;
let siblings =
(await rep?.query((tx) =>
getBlocksWithType(tx, selectedBlocks[0].parent),
)) || [];
let sortedBlocks = siblings.filter((s) =>
selectedBlocks.find((sb) => sb.value === s.value),
);
return [sortedBlocks, siblings];
};
return (
<>
{
let [sortedBlocks, siblings] = await getSortedSelection();
if (sortedBlocks.length > 1) return;
let block = sortedBlocks[0];
let previousBlock =
siblings?.[siblings.findIndex((s) => s.value === block.value) - 1];
if (previousBlock.value === block.listData?.parent) {
previousBlock =
siblings?.[
siblings.findIndex((s) => s.value === block.value) - 2
];
}
if (
previousBlock?.listData &&
block.listData &&
block.listData.depth > 1 &&
!previousBlock.listData.path.find(
(f) => f.entity === block.listData?.parent,
)
) {
let depth = block.listData.depth;
let newParent = previousBlock.listData.path.find(
(f) => f.depth === depth - 1,
);
if (!newParent) return;
if (useUIState.getState().foldedBlocks.includes(newParent.entity))
useUIState.getState().toggleFold(newParent.entity);
rep?.mutate.moveBlock({
block: block.value,
oldParent: block.listData?.parent,
newParent: newParent.entity,
position: { type: "end" },
});
} else {
rep?.mutate.moveBlockUp({
entityID: block.value,
parent: block.listData?.parent || block.parent,
});
}
}}
tooltipContent={
Move Up
Shift +{" "}
{metaKey()} +{" "}
↑
}
>
{
let [sortedBlocks, siblings] = await getSortedSelection();
if (sortedBlocks.length > 1) return;
let block = sortedBlocks[0];
let nextBlock = siblings
.slice(siblings.findIndex((s) => s.value === block.value) + 1)
.find(
(f) =>
f.listData &&
block.listData &&
!f.listData.path.find((f) => f.entity === block.value),
);
if (
nextBlock?.listData &&
block.listData &&
nextBlock.listData.depth === block.listData.depth - 1
) {
if (useUIState.getState().foldedBlocks.includes(nextBlock.value))
useUIState.getState().toggleFold(nextBlock.value);
rep?.mutate.moveBlock({
block: block.value,
oldParent: block.listData?.parent,
newParent: nextBlock.value,
position: { type: "first" },
});
} else {
rep?.mutate.moveBlockDown({
entityID: block.value,
parent: block.listData?.parent || block.parent,
});
}
}}
tooltipContent={
Move Down
Shift +{" "}
{metaKey()} +{" "}
↓
}
>
>
);
};
const MoveBlockDown = () => {
return (
);
};
const MoveBlockUp = () => {
return (
);
};