Skip to content

Commit

Permalink
Merge pull request #64 from prezly/feature/care-2063-image-resized-in…
Browse files Browse the repository at this point in the history
…-published-story-different-to-editor-option

[CARE-2063] Image resized in published story different to editor option
  • Loading branch information
fgyimah authored Jul 24, 2023
2 parents d97dcd3 + 35d6847 commit 3109ddf
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 8 deletions.
Binary file modified .loki/reference/chrome_iphone7_Elements_Image_Width_100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_laptop_Elements_Image_Width_100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_narrow_Elements_Image_Width_100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions src/elements/Image/Image.slate.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,39 @@ export const Width100: Story = () => (
/>
);

/**
* @see https://linear.app/prezly/issue/CARE-2063/image-resized-in-published-story-different-to-editor-option#comment-ee5bbe02
*/
export const Width100SmallImage: Story = () => (
<Renderer
nodes={[
{
type: 'image-block',
href: '',
new_tab: true,
align: 'left',
layout: 'contained',
width: '100%',
file: {
version: 2,
uuid: 'd0bdf122-a96a-425b-93e8-e3f1a052d413',
filename: 'dog-300x300.jpeg',
mime_type: 'image/jpeg',
size: 30264,
original_width: 150,
original_height: 150,
effects: [],
},
children: [
{
text: 'Contained 100% width',
},
],
},
]}
/>
);

export const Width45: Story = () => (
<Renderer
nodes={[
Expand Down
24 changes: 16 additions & 8 deletions src/elements/Image/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ function getContainerStyle(node: ImageNode): CSSProperties {
return {};
}

const width = `${parseFloat(node.width).toFixed(2)}%`;
const maxWidth = `${node.file.original_width}px`;
const unit = node.width.slice(-1) === '%' ? '%' : 'px';

if (width === `${(100).toFixed(2)}%`) {
return {};
if (unit === '%' && parseFloat(node.width).toFixed(2) === Number(100).toFixed(2)) {
return { maxWidth };
}

return { width };
return { width: node.width, maxWidth };
}

const NEW_TAB_ATTRIBUTES: Partial<AnchorHTMLAttributes<HTMLAnchorElement>> = {
Expand All @@ -36,14 +37,21 @@ const NEW_TAB_ATTRIBUTES: Partial<AnchorHTMLAttributes<HTMLAnchorElement>> = {

export function Image({ children, className, node, onDownload, onPreviewOpen, ...props }: Props) {
const { file, href, align, layout } = node;

const isNewTab = node.new_tab ?? true;
const title = stringifyNode(node).trim();
const containerStyle = getContainerStyle(node);

const [isPreviewOpen, setIsPreviewOpen] = useState<boolean>(false);
// eslint-disable-next-line react-hooks/exhaustive-deps
const image = useMemo(() => UploadcareImage.createFromPrezlyStoragePayload(file), [file.uuid]);
const containerStyle = getContainerStyle(node);
const handleRolloverClick = () => setIsPreviewOpen(true);
const handleImagePreviewClose = () => setIsPreviewOpen(false);
const title = stringifyNode(node).trim();

function handleRolloverClick() {
setIsPreviewOpen(true);
}
function handleImagePreviewClose() {
setIsPreviewOpen(false);
}

return (
<figure
Expand Down

0 comments on commit 3109ddf

Please sign in to comment.