Skip to content

Commit da63a47

Browse files
committed
fix(files): preserve native non-pixel image heights
1 parent b6feb86 commit da63a47

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/image-resize.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,27 @@ describe('ResizableImageView', () => {
161161
expect(image.style.height).toBe('100px')
162162
})
163163

164+
it.each(['50%', 'auto', '10em', 'calc(50% - 10px)', 'min-content', 'inherit'])(
165+
'preserves the native height-only CSS value %s before and after loading',
166+
(height) => {
167+
renderImage(vi.fn(), { height })
168+
const image = host.querySelector<HTMLImageElement>('img')!
169+
expect(image.style.width).toBe('')
170+
expect(image.style.height).toBe(height)
171+
expect(image.style.maxHeight).toBe('')
172+
173+
Object.defineProperties(image, {
174+
naturalWidth: { configurable: true, value: 400 },
175+
naturalHeight: { configurable: true, value: 200 },
176+
})
177+
act(() => image.dispatchEvent(new Event('load')))
178+
179+
expect(image.style.width).toBe('')
180+
expect(image.style.height).toBe(height)
181+
expect(image.style.maxHeight).toBe('')
182+
}
183+
)
184+
164185
it('commits one proportional width change and clears a stale explicit height', () => {
165186
const updateAttributes = vi.fn()
166187
const handle = renderImage(updateAttributes)

apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/image.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export function ResizableImageView({
135135
// stored value is stale (e.g. left over after the file's content was replaced) — so it wins once
136136
// available; stored metadata only reserves the box pre-load. Equal in the common case, so no shift.
137137
const intrinsicDimensions = measuredDimensions ?? storedDimensions
138+
const hasPixelHeight = committedHeight !== undefined && PIXEL_SIZE.test(committedHeight)
138139
const authoredDimensions =
139140
committedWidth &&
140141
committedHeight &&
@@ -150,7 +151,7 @@ export function ResizableImageView({
150151
dragWidth !== null
151152
? `${dragWidth}px`
152153
: (committedWidth ??
153-
(intrinsicDimensions
154+
(intrinsicDimensions && (!committedHeight || hasPixelHeight)
154155
? committedHeight
155156
? `calc(${committedHeight} * ${intrinsicDimensions.width / intrinsicDimensions.height})`
156157
: `${intrinsicDimensions.width}px`
@@ -161,8 +162,11 @@ export function ResizableImageView({
161162
const imageStyle: CSSProperties = {
162163
width: displayWidth,
163164
height:
164-
dragWidth === null && committedWidth && !authoredDimensions ? committedHeight : undefined,
165-
maxHeight: dragWidth === null && !committedWidth ? committedHeight : undefined,
165+
dragWidth === null && !authoredDimensions && (committedWidth || !hasPixelHeight)
166+
? committedHeight
167+
: undefined,
168+
maxHeight:
169+
dragWidth === null && !committedWidth && hasPixelHeight ? committedHeight : undefined,
166170
aspectRatio: displayDimensions
167171
? `${displayDimensions.width} / ${displayDimensions.height}`
168172
: undefined,

0 commit comments

Comments
 (0)