Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions TESTING_COVERAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Collection alias indexing: `sorted_parent_query_deduplicates_legacy_and_canonica

New-drive sync: WebSocket unit coverage verifies SUB and SYNC wait for a pending genesis acknowledgement, then resume on ResourceSaved. The Local DB-off rendering E2E exercises this ordering with real server persistence.

Document images: `document-image.spec.ts` uploads a freshly generated PNG through the editor's image picker in a browser-only drive, whose bytes never reach a server, and verifies the image renders from the local blob store before and after a reload instead of showing "Failed to load image". `subject.test.ts` covers mapping a content-addressed `/download/files/<hash>` URL back to its blob. An image in a server-synced drive on a device that has not got the bytes still loads from the server URL; that path is not separately covered.

Cover repositioning: `cover-reposition.spec.ts` uploads a real image and verifies multiple pointer movements update its framing before release (native image dragging previously interrupted the gesture).

Template visibility: `settings-templates.spec.ts` toggles Hide templates through Settings, verifies the loaded New page hides templates across reload, and restores them when unchecked.
Expand Down
7 changes: 7 additions & 0 deletions browser/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ This changelog covers all five packages, as they are (for now) updated as a whol

## UNRELEASED

- Images in documents show again instead of "Failed to load image". An uploaded
image's address points at the server, but a browser-only workspace never
sends its files there, so the server could not answer it. The editor now shows
the copy this browser already holds, as file previews already did, and falls
back to the server when there is none. New `useBlobObjectUrl` hook in
`@tomic/react` and `blobSubjectFromDownloadUrl` in `@tomic/lib`.

- Pasting an agent secret that opens a different agent than the signed-in
account no longer signs that account out on its own. The app now says which
account is signed in, shows both agents, and lets the user stay signed in or
Expand Down
41 changes: 31 additions & 10 deletions browser/data-browser/src/chunks/RTE/ImagePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import { Button } from '../../components/Button';
import { InputStyled, InputWrapper } from '../../components/forms/InputStyles';
import { Column, Row } from '../../components/Row';
import { FilePickerDialog } from '../../components/forms/FilePicker/FilePickerDialog';
import { useStore, type Server } from '@tomic/react';
import {
blobSubjectFromDownloadUrl,
useBlobObjectUrl,
useStore,
type Server,
} from '@tomic/react';
import {
ClearType,
FilePickerButton,
Expand Down Expand Up @@ -72,8 +77,18 @@ const MarkdownEditorImage = ({
const [altText, setAltText] = useState<string>();
const [caption, setCaption] = useState<string>();
const [float, setFloat] = useState<string>('none');
const [imageError, setImageError] = useState<boolean>(false);
const [urlValid, urlRef] = useHTMLFormFieldValidation();
const src: string | undefined = node.attrs.src || undefined;
// An uploaded image's `src` is its content-addressed download URL. Show the
// local copy of those bytes when there is one: in a browser-only drive it
// is the only copy, and no server can answer that URL.
const displaySrc = useBlobObjectUrl(
src ? blobSubjectFromDownloadUrl(src) : undefined,
src,
);
// Keyed by the src that failed, so a new src gets its own attempt.
const [failedSrc, setFailedSrc] = useState<string>();
const imageError = !!displaySrc && failedSrc === displaySrc;

const floatOptions: ButtonGroupOption[] = [
{
Expand Down Expand Up @@ -132,38 +147,44 @@ const MarkdownEditorImage = ({
);
}

if (src && !displaySrc) {
// Still looking for a local copy. Rendering the URL meanwhile would ask a
// server that may never have had the bytes, and flash the error state.
return <NodeViewWrapper />;
}

if (
!extension.options.markdownCompatible &&
node.attrs.src &&
displaySrc &&
node.attrs.caption
) {
return (
<NodeViewWrapper>
<StyledFigure>
<StyledImage
ref={ref as React.ForwardedRef<HTMLImageElement>}
src={node.attrs.src}
src={displaySrc}
alt={node.attrs.alt}
selected={selected}
float={node.attrs.float}
onError={() => setImageError(true)}
onError={() => setFailedSrc(displaySrc)}
/>
<figcaption>{node.attrs.caption}</figcaption>
</StyledFigure>
</NodeViewWrapper>
);
}

if (node.attrs.src) {
if (displaySrc) {
return (
<NodeViewWrapper>
<StyledImage
ref={ref as React.ForwardedRef<HTMLImageElement>}
src={node.attrs.src}
src={displaySrc}
alt={node.attrs.alt}
selected={selected}
float={node.attrs.float}
onError={() => setImageError(true)}
onError={() => setFailedSrc(displaySrc)}
/>
<p>{node.attrs.caption}</p>
</NodeViewWrapper>
Expand All @@ -179,7 +200,7 @@ const MarkdownEditorImage = ({
{!selectedSubject && (
<>
<Label>
Choose an image
<span>Choose an image</span>
<Column>
<StyledInputWrapper hasPrefix>
<FaLink />
Expand Down Expand Up @@ -222,7 +243,7 @@ const MarkdownEditorImage = ({
</Label>
)}
<Label>
Textual Description
<span>Textual Description</span>
<TextArea
placeholder='Alt text'
value={altText}
Expand Down
18 changes: 12 additions & 6 deletions browser/data-browser/src/locales/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -1414,9 +1414,8 @@ msgstr ""
msgid "Enter a URL..."
msgstr ""

#: src/chunks/RTE/ImagePicker.tsx
msgid "Choose an image <0/>"
msgstr ""
#~ msgid "Choose an image <0/>"
#~ msgstr ""

#: src/chunks/RTE/ImagePicker.tsx
msgid "Or"
Expand All @@ -1430,9 +1429,8 @@ msgstr ""
msgid "Alt text"
msgstr ""

#: src/chunks/RTE/ImagePicker.tsx
msgid "Textual Description <0/>"
msgstr ""
#~ msgid "Textual Description <0/>"
#~ msgstr ""

#: src/chunks/DashboardPage/BlockConfigDialog.tsx
#: src/chunks/RTE/ImagePicker.tsx
Expand Down Expand Up @@ -12504,3 +12502,11 @@ msgstr "Dein Arbeitsbereich konnte nicht in dein Konto übernommen werden. Versu
#: src/components/IdentityReconcileGate.tsx
msgid "Lets {0} edit the drives made on this browser, then switches to that identity. Everything stays editable and is backed up with your account."
msgstr "Erlaubt {0}, die in diesem Browser erstellten Drives zu bearbeiten, und wechselt dann zu dieser Identität. Alles bleibt bearbeitbar und wird mit deinem Konto gesichert."

#: src/chunks/RTE/ImagePicker.tsx
msgid "Choose an image"
msgstr ""

#: src/chunks/RTE/ImagePicker.tsx
msgid "Textual Description"
msgstr ""
18 changes: 12 additions & 6 deletions browser/data-browser/src/locales/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -1414,9 +1414,8 @@ msgstr "Failed to load image."
msgid "Enter a URL..."
msgstr "Enter a URL..."

#: src/chunks/RTE/ImagePicker.tsx
msgid "Choose an image <0/>"
msgstr "Choose an image <0/>"
#~ msgid "Choose an image <0/>"
#~ msgstr "Choose an image <0/>"

#: src/chunks/RTE/ImagePicker.tsx
msgid "Or"
Expand All @@ -1430,9 +1429,8 @@ msgstr "Add a caption..."
msgid "Alt text"
msgstr "Alt text"

#: src/chunks/RTE/ImagePicker.tsx
msgid "Textual Description <0/>"
msgstr "Textual Description <0/>"
#~ msgid "Textual Description <0/>"
#~ msgstr "Textual Description <0/>"

#: src/chunks/DashboardPage/BlockConfigDialog.tsx
#: src/chunks/RTE/ImagePicker.tsx
Expand Down Expand Up @@ -12550,3 +12548,11 @@ msgstr "Your workspace could not be brought into your account. Try again, or cho
#: src/components/IdentityReconcileGate.tsx
msgid "Lets {0} edit the drives made on this browser, then switches to that identity. Everything stays editable and is backed up with your account."
msgstr "Lets {0} edit the drives made on this browser, then switches to that identity. Everything stays editable and is backed up with your account."

#: src/chunks/RTE/ImagePicker.tsx
msgid "Choose an image"
msgstr "Choose an image"

#: src/chunks/RTE/ImagePicker.tsx
msgid "Textual Description"
msgstr "Textual Description"
18 changes: 12 additions & 6 deletions browser/data-browser/src/locales/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -1414,9 +1414,8 @@ msgstr "Error al cargar la imagen."
msgid "Enter a URL..."
msgstr "Introduce una URL..."

#: src/chunks/RTE/ImagePicker.tsx
msgid "Choose an image <0/>"
msgstr "Elegir una imagen <0/>"
#~ msgid "Choose an image <0/>"
#~ msgstr "Elegir una imagen <0/>"

#: src/chunks/RTE/ImagePicker.tsx
msgid "Or"
Expand All @@ -1430,9 +1429,8 @@ msgstr "Añade un pie de foto..."
msgid "Alt text"
msgstr "Texto alternativo"

#: src/chunks/RTE/ImagePicker.tsx
msgid "Textual Description <0/>"
msgstr "Descripción Textual <0/>"
#~ msgid "Textual Description <0/>"
#~ msgstr "Descripción Textual <0/>"

#: src/chunks/DashboardPage/BlockConfigDialog.tsx
#: src/chunks/RTE/ImagePicker.tsx
Expand Down Expand Up @@ -12504,3 +12502,11 @@ msgstr "No se pudo traer tu espacio de trabajo a tu cuenta. Inténtalo de nuevo
#: src/components/IdentityReconcileGate.tsx
msgid "Lets {0} edit the drives made on this browser, then switches to that identity. Everything stays editable and is backed up with your account."
msgstr "Permite que {0} edite las unidades creadas en este navegador y luego cambia a esa identidad. Todo sigue siendo editable y se respalda con tu cuenta."

#: src/chunks/RTE/ImagePicker.tsx
msgid "Choose an image"
msgstr ""

#: src/chunks/RTE/ImagePicker.tsx
msgid "Textual Description"
msgstr ""
18 changes: 12 additions & 6 deletions browser/data-browser/src/locales/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -1414,9 +1414,8 @@ msgstr "Échec du chargement de l'image."
msgid "Enter a URL..."
msgstr "Entrez une URL..."

#: src/chunks/RTE/ImagePicker.tsx
msgid "Choose an image <0/>"
msgstr "Choisir une image <0/>"
#~ msgid "Choose an image <0/>"
#~ msgstr "Choisir une image <0/>"

#: src/chunks/RTE/ImagePicker.tsx
msgid "Or"
Expand All @@ -1430,9 +1429,8 @@ msgstr "Ajouter une légende..."
msgid "Alt text"
msgstr "Texte alternatif"

#: src/chunks/RTE/ImagePicker.tsx
msgid "Textual Description <0/>"
msgstr "Description textuelle <0/>"
#~ msgid "Textual Description <0/>"
#~ msgstr "Description textuelle <0/>"

#: src/chunks/DashboardPage/BlockConfigDialog.tsx
#: src/chunks/RTE/ImagePicker.tsx
Expand Down Expand Up @@ -12504,3 +12502,11 @@ msgstr "Votre espace de travail n’a pas pu être ajouté à votre compte. Rée
#: src/components/IdentityReconcileGate.tsx
msgid "Lets {0} edit the drives made on this browser, then switches to that identity. Everything stays editable and is backed up with your account."
msgstr "Permet à {0} de modifier les drives créés dans ce navigateur, puis bascule vers cette identité. Tout reste modifiable et est sauvegardé avec votre compte."

#: src/chunks/RTE/ImagePicker.tsx
msgid "Choose an image"
msgstr ""

#: src/chunks/RTE/ImagePicker.tsx
msgid "Textual Description"
msgstr ""
118 changes: 118 additions & 0 deletions browser/e2e/tests/document-image.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { test, expect } from './fixtures';
import { before, openSubject, waitForClientDbFlush } from './test-utils';
import { core, dataBrowser } from '@tomic/lib';
import type { Page } from '@playwright/test';

/**
* A PNG no earlier run can have put on the server. Blobs are content
* addressed, so a fixed fixture file would be served by whichever test pushed
* it first and hide an image that only exists locally.
*/
async function uniquePng(page: Page): Promise<Buffer> {
const base64 = await page.evaluate(async () => {
const canvas = new OffscreenCanvas(24, 16);
const ctx = canvas.getContext('2d')!;
ctx.fillStyle = `hsl(${Math.floor(Math.random() * 360)} 70% 50%)`;
ctx.fillRect(0, 0, 24, 16);
ctx.fillText(crypto.randomUUID(), 0, 12);
const blob = await canvas.convertToBlob({ type: 'image/png' });
const bytes = new Uint8Array(await blob.arrayBuffer());
let binary = '';
for (const byte of bytes) binary += String.fromCharCode(byte);

return btoa(binary);
});

return Buffer.from(base64, 'base64');
}

test.describe('document images', () => {
test.beforeEach(before);

// A browser-only drive never sends its commits, or the bytes of its files,
// to a server, so the `downloadURL` an upload is given points at bytes no
// server has. The editor has to show the image from the local copy, the way
// the file preview already does.
test('an image uploaded in a browser-only drive renders in the document', async ({
page,
}) => {
const documentSubject = await page.evaluate(
async ({ documentClass, name }) => {
const store = window.store;
const drive = await store.createDrive('Browser-only workspace', {
personal: false,
localOnly: true,
});
const doc = await store.newResource({
parent: drive.subject,
isA: documentClass,
propVals: { [name]: 'Document with an image' },
});
await doc.save();
store.notifyResourceManuallyCreated(doc);

return doc.subject;
},
{
documentClass: dataBrowser.classes.document,
name: core.properties.name,
},
);

await openSubject(page, documentSubject);

const editor = page.getByLabel('Rich Text Editor');
await editor.click();
await page.keyboard.type('/image');
await expect(
page
.getByTestId('rte-command-list')
.getByRole('button', { name: 'Image' }),
).toBeVisible();
await page.keyboard.press('Enter');

await page.getByRole('button', { name: 'Select File' }).click();
await page.getByLabel('Upload', { exact: true }).setInputFiles({
name: 'local-only.png',
mimeType: 'image/png',
buffer: await uniquePng(page),
});
await page.getByRole('button', { name: 'Save', exact: true }).click();

const img = editor.locator('img');
await expect(img).toBeVisible();
await expect(page.getByText('Failed to load image.')).toHaveCount(0);
await expect
.poll(() => img.evaluate(el => (el as HTMLImageElement).naturalWidth))
.toBeGreaterThan(0);
// Rendered from the local bytes, not from a server that never got them.
await expect(img).toHaveAttribute('src', /^blob:/);

// And again after a reload, when the bytes come out of the local database.
// The editor saves on a debounce; let that save land on disk first.
await expect
.poll(() =>
page.evaluate(subject => {
const store = window.store;
const doc = store.resources.get(subject);

return (
!!doc &&
!doc.hasUnsavedChanges() &&
store.getSaveState(doc).scheduledCount === 0
);
}, documentSubject),
)
.toBe(true);
await waitForClientDbFlush(page, { required: true });
await page.reload();
const reloaded = page.getByLabel('Rich Text Editor').locator('img');
await expect(reloaded).toHaveAttribute('src', /^blob:/);
await expect
.poll(() =>
reloaded.evaluate(el => (el as HTMLImageElement).naturalWidth),
)
.toBeGreaterThan(0);
await expect(page.getByText('Failed to load image.')).toHaveCount(0);
});
});
Loading