Skip to content

Commit 01dbfbf

Browse files
authored
Konnorrogers/fix direct upload success events (#231)
* fix upload success events * remove console.log * prettier * prettier * add changelog entry
1 parent 5160d52 commit 01dbfbf

File tree

6 files changed

+37
-30
lines changed

6 files changed

+37
-30
lines changed

.changeset/silly-forks-switch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"rhino-editor": patch
3+
---
4+
5+
Fix the hacky workaround for slow / unstable connections

.changeset/sweet-crews-fly.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"rhino-editor": patch
3+
---
4+
5+
remove unnecessary console.log

src/exports/attachment-manager.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { uuidv4 } from "../internal/uuidv4.js";
33
import type { EditorView } from "@tiptap/pm/view";
44
import { LOADING_STATES } from "./elements/attachment-editor.js";
55
import { toDefaultCaption } from "../internal/to-default-caption.js";
6+
import {
7+
AttachmentUpload,
8+
AttachmentUploadCompleteEvent,
9+
AttachmentUploadSucceedEvent,
10+
} from "./attachment-upload.js";
611

712
export interface AttachmentManagerAttributes {
813
src: string;
@@ -30,6 +35,7 @@ export interface AttachmentManagerAttributes {
3035
export class AttachmentManager implements AttachmentManagerAttributes {
3136
attributes: AttachmentManagerAttributes;
3237
editorView: EditorView;
38+
directUpload?: AttachmentUpload;
3339

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

86+
this.handleSuccess();
87+
8088
return;
8189
}
8290

@@ -107,6 +115,7 @@ export class AttachmentManager implements AttachmentManagerAttributes {
107115
previewable: this.isPreviewable,
108116
});
109117
image.remove();
118+
this.handleSuccess();
110119
};
111120
return;
112121
}
@@ -120,6 +129,16 @@ export class AttachmentManager implements AttachmentManagerAttributes {
120129
contentType: this.contentType,
121130
previewable: this.isPreviewable,
122131
});
132+
this.handleSuccess();
133+
}
134+
135+
handleSuccess() {
136+
const upload = this.directUpload;
137+
if (upload) {
138+
this.setUploadProgress(100);
139+
upload.element.dispatchEvent(new AttachmentUploadSucceedEvent(upload));
140+
upload.element.dispatchEvent(new AttachmentUploadCompleteEvent(upload));
141+
}
123142
}
124143

125144
/**

src/exports/attachment-upload.ts

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -152,34 +152,14 @@ export class AttachmentUpload implements DirectUploadDelegate {
152152
return;
153153
}
154154

155-
const blobUrl = this.createBlobUrl(blob.signed_id, blob.filename);
156-
this.attachment.setAttributes({
157-
sgid: blob.attachable_sgid ?? "",
158-
url: blobUrl,
159-
});
160-
161-
// TODO: This may create problems for non-images, could use something like an `<object src="<url>">` instead.
162-
const template = document.createElement("template");
163-
const obj = document.createElement("object");
164-
obj.toggleAttribute("hidden", true);
165-
template.append(obj);
166-
167-
obj.onload = () => {
168-
template.remove();
169-
this.progress = 100;
170-
this.setUploadProgress();
171-
this.element.dispatchEvent(new AttachmentUploadSucceedEvent(this));
172-
this.element.dispatchEvent(new AttachmentUploadCompleteEvent(this));
173-
};
174-
175-
obj.onerror = () => {
176-
template.remove();
177-
this.handleError();
178-
};
179-
180-
obj.data = blobUrl;
181-
// Needs to append to for onerror / onload to fire.
182-
document.body.append(template);
155+
if (blob.attachable_sgid) {
156+
const blobUrl = this.createBlobUrl(blob.signed_id, blob.filename);
157+
this.attachment.directUpload = this;
158+
this.attachment.setAttributes({
159+
sgid: blob.attachable_sgid,
160+
url: blobUrl,
161+
});
162+
}
183163
}
184164

185165
setUploadProgress() {

src/exports/elements/tip-tap-editor-base.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,6 @@ export class TipTapEditorBase extends BaseElement {
422422
return attachment === e.attachmentUpload;
423423
});
424424

425-
console.log("complete");
426425
if (index > -1) {
427426
this.pendingAttachments.splice(index, 1);
428427
}

src/exports/elements/tip-tap-editor.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1559,7 +1559,6 @@ export class TipTapEditor extends TipTapEditorBase {
15591559
if (e.defaultPrevented) {
15601560
return;
15611561
}
1562-
console.log("show");
15631562
15641563
const anchoredRegion = e.currentTarget as RoleAnchoredRegion;
15651564
anchoredRegion.anchor = { getBoundingClientRect: e.clientRect };

0 commit comments

Comments
 (0)