Render png on mobile (#9087)
* Add mobile image-diff previews via shared data-URI builder - Extracts a `buildImageDataUri` helper (src/shared/image-data-uri.ts) shared by the desktop ImageViewer and mobile, so both trim whitespace-wrapped base64 and skip non-previewable mimes (e.g. application/pdf) the same way. - Adds mobile-diff-image-preview.ts to render binary git.diff results (add/modify/ delete) as images instead of falling back to "Binary preview unavailable". - Extracts resolveMobileFileTabDoc to consolidate the session file-tab loading logic (diff/image/html/text) out of the route file for testability. * Fix stale binary image fallback for empty modified diffs and relay reads - mobileDiffImageDataUri now distinguishes a true deletion (modified side absent) from a modify whose binary bytes arrived empty (relay/size-cap cases), returning null instead of the stale pre-change image - readWorkingDiffFile passes the file path to bufferToBlob so relay working-tree reads can detect previewable image extensions instead of always reporting empty binary content - add mobile-file-tab-doc.test.ts covering diff/image/binary/text resolution paths * Regenerate skill bundle manifest for 1.4.144-rc.2 Co-authored-by: Orca <help@stably.ai> * fix(review): trim comments to AGENTS.md's one/two-line why-only rule Comments in mobile-diff-image-preview.ts and mobile-file-tab-doc.ts ran 3-6 lines and narrated mechanism instead of stating only the non-obvious reason, per AGENTS.md's "Code Comments: Document the Why, Briefly" rule. Co-authored-by: Orca <help@stably.ai> * Distinguish read failures from true deletions in binary diff results - Working-tree stat/readFile errors and relay reads previously collapsed onto the same empty-content signal as a genuine deletion, letting previewers fall back to stale original bytes on a failed read. - Add modifiedDeleted/missing flags through status.ts, git-handler-ops, and git-working-file-read so only proven deletions trigger the original-bytes fallback; failed reads now return null. - Tighten buildImageDataUri to accept only image/* mimes instead of special-casing application/pdf. * fix(relay): expect missing:false on index blob maxBuffer overflow readBlobAtIndex now returns a missing flag so staged deletions are distinct from size-capped binary reads; update the overflow test. * Allow opening deleted files to show pre-delete text or image diffs Deleted files can now be opened to view their pre-delete content via git.diff (including images via modifiedDeleted). Only unresolved conflicts remain unopenable. Centralizes the canOpen rule in canOpenMobileGitStatusEntry() to keep opener guards consistent across the mobile source control UI. --------- Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { buildImageDataUri } from './image-data-uri'
|
||||
|
||||
describe('buildImageDataUri', () => {
|
||||
it('builds a data URI from base64 image bytes', () => {
|
||||
expect(buildImageDataUri('image/png', 'bmV3')).toBe('data:image/png;base64,bmV3')
|
||||
})
|
||||
|
||||
it('strips whitespace from line-wrapped base64 payloads', () => {
|
||||
expect(buildImageDataUri('image/png', 'bm\nV3\t bmV3\r\n')).toBe(
|
||||
'data:image/png;base64,bmV3bmV3'
|
||||
)
|
||||
})
|
||||
|
||||
it('returns null for an empty payload', () => {
|
||||
expect(buildImageDataUri('image/png', ' \n')).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null for a missing mime type', () => {
|
||||
expect(buildImageDataUri(undefined, 'bmV3')).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null for application/pdf (not an <img> source)', () => {
|
||||
expect(buildImageDataUri('application/pdf', 'JVBER')).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null for a non-image mime such as application/octet-stream', () => {
|
||||
expect(buildImageDataUri('application/octet-stream', 'AAAA')).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,20 @@
|
||||
// Builds an inline `data:` URI for base64 image bytes, shared by the desktop
|
||||
// editor ImageViewer and the mobile file preview so both decode images the same
|
||||
// way. Strips whitespace from the payload (base64 from git diffs and SSH streams
|
||||
// can arrive line-wrapped) and returns null when there is nothing an <img>/RN
|
||||
// <Image> can show — empty content or a non-image mime (PDF, octet-stream, …).
|
||||
export function buildImageDataUri(
|
||||
mimeType: string | undefined,
|
||||
base64Content: string
|
||||
): string | null {
|
||||
// Only image/* renders in an <img>/RN <Image>; reject every other mime
|
||||
// (application/pdf, application/octet-stream, …), not just PDF.
|
||||
if (!mimeType?.startsWith('image/')) {
|
||||
return null
|
||||
}
|
||||
const cleaned = base64Content.replace(/\s/g, '')
|
||||
if (!cleaned) {
|
||||
return null
|
||||
}
|
||||
return `data:${mimeType};base64,${cleaned}`
|
||||
}
|
||||
@@ -3742,6 +3742,13 @@ export type GitDiffBinaryResult = {
|
||||
isImage?: boolean
|
||||
/** MIME type for binary preview rendering, e.g. "image/png" or "application/pdf" */
|
||||
mimeType?: string
|
||||
/**
|
||||
* True only when the modified side is a proven deletion (working-tree file gone
|
||||
* or absent from the index) — distinct from an empty modified side caused by a
|
||||
* read failure or size cap. Lets previewers fall back to the original bytes for
|
||||
* a deletion without showing a stale image on a failed read.
|
||||
*/
|
||||
modifiedDeleted?: boolean
|
||||
} & (
|
||||
| { originalIsBinary: true; modifiedIsBinary: boolean }
|
||||
| { originalIsBinary: boolean; modifiedIsBinary: true }
|
||||
|
||||
Reference in New Issue
Block a user