Skip to content

Commit

Permalink
Fix mobile safari previews (#128)
Browse files Browse the repository at this point in the history
* add changeseT

* prettier

* remove console.log

* remove console.log

* remove console.log

* make TS happy

* prettier

* fix attachment previews on mobile safari

* remove nonsense

* prettier

* update to github changelog

* fix release deploys
  • Loading branch information
KonnorRogers committed Sep 21, 2023
1 parent 2987b63 commit fae7c47
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
@@ -1,6 +1,6 @@
{
"$schema": "https://unpkg.com/@changesets/config@2.3.0/schema.json",
"changelog": "@changesets/cli/changelog",
"changelog": ["@changesets/changelog-github", { "repo": "konnorrogers/rhino-editor" }],
"commit": false,
"fixed": [],
"linked": [],
Expand Down
5 changes: 5 additions & 0 deletions .changeset/purple-eggs-camp.md
@@ -0,0 +1,5 @@
---
"rhino-editor": patch
---

- Fix rendering of inline previews of images on Mobile Safari.
5 changes: 3 additions & 2 deletions .github/workflows/preview-deploy.yml
Expand Up @@ -5,8 +5,9 @@ env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
on:
release:
types: [published]
push:
tags:
- "*"
jobs:
Deploy-Preview:
runs-on: ubuntu-latest
Expand Down
19 changes: 18 additions & 1 deletion src/exports/attachment-manager.ts
Expand Up @@ -88,7 +88,7 @@ export class AttachmentManager implements AttachmentManagerAttributes {
/** This preloads the image so we don't show a big flash. */
const image = new Image();

image.src = obj.url;
image.setAttribute("src", obj.url);

image.onload = () => {
this.attributes.width = image.naturalWidth;
Expand Down Expand Up @@ -139,14 +139,27 @@ export class AttachmentManager implements AttachmentManagerAttributes {
});
}

/**
* This is an internal ID used for finding newly attached attachments in the TipTap editor.
* This is used primarily for direct upload purposes.
* This generally won't exist when a node is recreated from you database.
*/
get attachmentId() {
return this.attributes.attachmentId;
}

/**
* This is an internal ID used for finding newly attached images in the TipTap editor.
* This is used primarily for direct upload purposes.
* This generally won't exist when a node is recreated from you database.
*/
get imageId() {
return this.attributes.imageId;
}

/**
* `src` (when present) always maps to a URL.createObjectURL.
*/
get src() {
return this.attributes.src;
}
Expand Down Expand Up @@ -175,6 +188,10 @@ export class AttachmentManager implements AttachmentManagerAttributes {
return this.attributes.fileSize || this.file?.size;
}

/**
* This field is populated by the old Trix custom attachment API and denotes if we're using a custom
* attachment.
*/
get content() {
return this.attributes.content;
}
Expand Down
25 changes: 17 additions & 8 deletions src/exports/extensions/attachment.ts
Expand Up @@ -218,6 +218,7 @@ export const Attachment = Node.create<AttachmentOptions>({
nodeAttrs,
view,
);

view.dom.dispatchEvent(
new AttachmentRemoveEvent(attachmentManager),
);
Expand Down Expand Up @@ -483,13 +484,6 @@ export const Attachment = Node.create<AttachmentOptions>({
}
}

// Clean up any objects laying around
if (url) {
try {
URL.revokeObjectURL(src);
} catch (_e) {}
}

const isPreviewable = canPreview(previewable, contentType);

let imgSrc: string | undefined = undefined;
Expand Down Expand Up @@ -551,10 +545,25 @@ export const Attachment = Node.create<AttachmentOptions>({
const dom = scratch.firstElementChild;
const contentDOM = dom?.querySelector("figcaption");

let srcRevoked = false;

return {
dom,
contentDOM,
update() {
update(node) {
if (node.type.name !== "attachment") return false;

if (!srcRevoked && node.attrs.url) {
srcRevoked = true;

/** Do your part to save the environment. (Try to) prevent memory leaks. */
try {
URL.revokeObjectURL(node.attrs.src);
} catch (_e) {
/* We don't really care if this fails. We tried. */
}
}

return false;
},
};
Expand Down

0 comments on commit fae7c47

Please sign in to comment.