Skip to content

Commit

Permalink
Konnorrogers/fix direct upload success events (#231)
Browse files Browse the repository at this point in the history
* fix upload success events

* remove console.log

* prettier

* prettier

* add changelog entry
  • Loading branch information
KonnorRogers authored Nov 22, 2024
1 parent 5160d52 commit 01dbfbf
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-forks-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rhino-editor": patch
---

Fix the hacky workaround for slow / unstable connections
5 changes: 5 additions & 0 deletions .changeset/sweet-crews-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rhino-editor": patch
---

remove unnecessary console.log
19 changes: 19 additions & 0 deletions src/exports/attachment-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { uuidv4 } from "../internal/uuidv4.js";
import type { EditorView } from "@tiptap/pm/view";
import { LOADING_STATES } from "./elements/attachment-editor.js";
import { toDefaultCaption } from "../internal/to-default-caption.js";
import {
AttachmentUpload,
AttachmentUploadCompleteEvent,
AttachmentUploadSucceedEvent,
} from "./attachment-upload.js";

export interface AttachmentManagerAttributes {
src: string;
Expand Down Expand Up @@ -30,6 +35,7 @@ export interface AttachmentManagerAttributes {
export class AttachmentManager implements AttachmentManagerAttributes {
attributes: AttachmentManagerAttributes;
editorView: EditorView;
directUpload?: AttachmentUpload;

static get previewableRegex() {
return /^image(\/(gif|png|jpe?g)|$)/;
Expand Down Expand Up @@ -77,6 +83,8 @@ export class AttachmentManager implements AttachmentManagerAttributes {
previewable: this.isPreviewable,
});

this.handleSuccess();

return;
}

Expand Down Expand Up @@ -107,6 +115,7 @@ export class AttachmentManager implements AttachmentManagerAttributes {
previewable: this.isPreviewable,
});
image.remove();
this.handleSuccess();
};
return;
}
Expand All @@ -120,6 +129,16 @@ export class AttachmentManager implements AttachmentManagerAttributes {
contentType: this.contentType,
previewable: this.isPreviewable,
});
this.handleSuccess();
}

handleSuccess() {
const upload = this.directUpload;
if (upload) {
this.setUploadProgress(100);
upload.element.dispatchEvent(new AttachmentUploadSucceedEvent(upload));
upload.element.dispatchEvent(new AttachmentUploadCompleteEvent(upload));
}
}

/**
Expand Down
36 changes: 8 additions & 28 deletions src/exports/attachment-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,34 +152,14 @@ export class AttachmentUpload implements DirectUploadDelegate {
return;
}

const blobUrl = this.createBlobUrl(blob.signed_id, blob.filename);
this.attachment.setAttributes({
sgid: blob.attachable_sgid ?? "",
url: blobUrl,
});

// TODO: This may create problems for non-images, could use something like an `<object src="<url>">` instead.
const template = document.createElement("template");
const obj = document.createElement("object");
obj.toggleAttribute("hidden", true);
template.append(obj);

obj.onload = () => {
template.remove();
this.progress = 100;
this.setUploadProgress();
this.element.dispatchEvent(new AttachmentUploadSucceedEvent(this));
this.element.dispatchEvent(new AttachmentUploadCompleteEvent(this));
};

obj.onerror = () => {
template.remove();
this.handleError();
};

obj.data = blobUrl;
// Needs to append to for onerror / onload to fire.
document.body.append(template);
if (blob.attachable_sgid) {
const blobUrl = this.createBlobUrl(blob.signed_id, blob.filename);
this.attachment.directUpload = this;
this.attachment.setAttributes({
sgid: blob.attachable_sgid,
url: blobUrl,
});
}
}

setUploadProgress() {
Expand Down
1 change: 0 additions & 1 deletion src/exports/elements/tip-tap-editor-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,6 @@ export class TipTapEditorBase extends BaseElement {
return attachment === e.attachmentUpload;
});

console.log("complete");
if (index > -1) {
this.pendingAttachments.splice(index, 1);
}
Expand Down
1 change: 0 additions & 1 deletion src/exports/elements/tip-tap-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1559,7 +1559,6 @@ export class TipTapEditor extends TipTapEditorBase {
if (e.defaultPrevented) {
return;
}
console.log("show");
const anchoredRegion = e.currentTarget as RoleAnchoredRegion;
anchoredRegion.anchor = { getBoundingClientRect: e.clientRect };
Expand Down

0 comments on commit 01dbfbf

Please sign in to comment.