(null);
let preview = (value: number) => {
uncommitted.current = value;
setInterim(value);
if (!Number.isNaN(value))
props.onPreviewWidth?.(
Math.max(MAX_WIDTH_MIN, Math.min(MAX_WIDTH_MAX, value)),
);
};
let commit = (value: number) => {
uncommitted.current = null;
let clamped = Math.max(
MAX_WIDTH_MIN,
Math.min(MAX_WIDTH_MAX, Number.isNaN(value) ? defaultWidth : value),
);
setInterim(clamped);
rep?.mutate.assertFact({
entity: props.entityID,
attribute: "image/max-width",
data: { type: "number", value: clamped },
});
// Hand rendering back to the committed fact.
props.onPreviewWidth?.(null);
};
let commitRef = useRef(commit);
commitRef.current = commit;
useEffect(
() => () => {
if (uncommitted.current !== null) commitRef.current(uncommitted.current);
},
[],
);
return (
preview(value[0])}
onValueCommit={(value) => commit(value[0])}
>
);
}
function CoverImageControl(props: { entityID: string }) {
let { rep, rootEntity } = useReplicache();
let entity_set = useEntitySetContext();
let { data: pubData } = useLeafletPublicationData();
let publicationPage = useLeafletPublicationPage();
let image = useEntity(props.entityID, "block/image");
let alt = useEntity(props.entityID, "image/alt")?.data.value;
let coverEntity = useEntity(rootEntity, "root/cover-image")?.data.value;
let coverImage = useEntity(coverEntity ?? null, "block/image");
let coverOriginal = useEntity(coverEntity ?? null, "cover-image/original");
// Only show in a publication, with write permissions, and an image to copy.
// Publication pages (e.g. an About page) have no cover image.
if (
!pubData?.publications ||
!entity_set.permissions.write ||
publicationPage ||
!image
)
return null;
let coverSrc = (coverOriginal ?? coverImage)?.data.src;
let isCoverImage = !!coverSrc && coverSrc === image.data.src;
return (
<>
{
if (!rep) return;
if (isCoverImage) {
if (coverEntity)
await rep.mutate.deleteEntity({ entity: coverEntity });
} else {
await setCoverImageFromEntity(rep, {
rootEntity,
permission_set: entity_set.set,
image: image.data,
alt,
});
}
}}
/>
>
);
}