From d534bfa651e631ac32a5f6133d1678cc19e5ee7b Mon Sep 17 00:00:00 2001 From: konnorrogers Date: Thu, 5 Dec 2024 18:19:44 -0500 Subject: [PATCH 1/3] fix pending uploads not getting cleared --- src/exports/elements/tip-tap-editor-base.ts | 34 ++++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/exports/elements/tip-tap-editor-base.ts b/src/exports/elements/tip-tap-editor-base.ts index 9340d774..78fb6896 100644 --- a/src/exports/elements/tip-tap-editor-base.ts +++ b/src/exports/elements/tip-tap-editor-base.ts @@ -40,6 +40,7 @@ import { SelectionChangeEvent } from "../events/selection-change-event.js"; import { RhinoPasteEvent } from "../events/rhino-paste-event.js"; import { DOMSerializer, Slice } from "@tiptap/pm/model"; import type { EditorView } from "@tiptap/pm/view"; +import { AttachmentRemoveEvent } from "../events/attachment-remove-event.js"; export type Serializer = "" | "html" | "json"; @@ -393,12 +394,13 @@ export class TipTapEditorBase extends BaseElement { constructor() { super(); - this.registerDependencies(); - this.addEventListener(AddAttachmentEvent.eventName, this.handleAttachment); - this.__addPendingAttachment = this.__addPendingAttachment.bind(this); this.__removePendingAttachment = this.__removePendingAttachment.bind(this); + + this.registerDependencies(); + this.addEventListener(AddAttachmentEvent.eventName, this.handleAttachment); + this.addEventListener( AttachmentUploadStartEvent.eventName, this.__addPendingAttachment, @@ -407,19 +409,41 @@ export class TipTapEditorBase extends BaseElement { AttachmentUploadCompleteEvent.eventName, this.__removePendingAttachment, ); + this.addEventListener( + AttachmentRemoveEvent.eventName, + this.__removePendingAttachment, + ); this.addEventListener("drop", this.handleNativeDrop); this.addEventListener("rhino-paste", this.handlePaste); this.addEventListener("rhino-file-accept", this.handleFileAccept); } + /** + * @private + */ __addPendingAttachment(e: { attachmentUpload: AttachmentUpload }) { this.pendingAttachments.push(e.attachmentUpload); } - __removePendingAttachment(e: { attachmentUpload: AttachmentUpload }) { + + /** + * @private + */ + __removePendingAttachment(e: { attachment: AttachmentManager } | { attachmentUpload: AttachmentUpload }) { const index = this.pendingAttachments.findIndex((attachment) => { - return attachment === e.attachmentUpload; + + // This is what you get from an attachment upload finishing. + if ("attachmentUpload" in e) { + return attachment === e.attachmentUpload; + } + + // This is what you get from a generic "remove" event when an attachment is removed from the editor, this may not always be an upload. + if ("attachment" in e) { + return attachment.attachment.attachmentId === e.attachment.attachmentId; + } + + return false }); if (index > -1) { From 7cd7c3100929d56889a78e94e563f8fc772ff313 Mon Sep 17 00:00:00 2001 From: konnorrogers Date: Thu, 5 Dec 2024 18:20:41 -0500 Subject: [PATCH 2/3] fix pending uploads not getting cleared --- .changeset/strong-tigers-wash.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/strong-tigers-wash.md diff --git a/.changeset/strong-tigers-wash.md b/.changeset/strong-tigers-wash.md new file mode 100644 index 00000000..66fd6f75 --- /dev/null +++ b/.changeset/strong-tigers-wash.md @@ -0,0 +1,5 @@ +--- +"rhino-editor": patch +--- + +Fix pendingAttachments not clearing attachments that get cancelled From afefed0128fadae074a2b24f649e4c365699f60c Mon Sep 17 00:00:00 2001 From: konnorrogers Date: Thu, 5 Dec 2024 18:27:48 -0500 Subject: [PATCH 3/3] prettier --- src/exports/elements/tip-tap-editor-base.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/exports/elements/tip-tap-editor-base.ts b/src/exports/elements/tip-tap-editor-base.ts index 78fb6896..c183d4de 100644 --- a/src/exports/elements/tip-tap-editor-base.ts +++ b/src/exports/elements/tip-tap-editor-base.ts @@ -397,7 +397,6 @@ export class TipTapEditorBase extends BaseElement { this.__addPendingAttachment = this.__addPendingAttachment.bind(this); this.__removePendingAttachment = this.__removePendingAttachment.bind(this); - this.registerDependencies(); this.addEventListener(AddAttachmentEvent.eventName, this.handleAttachment); @@ -426,13 +425,15 @@ export class TipTapEditorBase extends BaseElement { this.pendingAttachments.push(e.attachmentUpload); } - /** * @private */ - __removePendingAttachment(e: { attachment: AttachmentManager } | { attachmentUpload: AttachmentUpload }) { + __removePendingAttachment( + e: + | { attachment: AttachmentManager } + | { attachmentUpload: AttachmentUpload }, + ) { const index = this.pendingAttachments.findIndex((attachment) => { - // This is what you get from an attachment upload finishing. if ("attachmentUpload" in e) { return attachment === e.attachmentUpload; @@ -443,7 +444,7 @@ export class TipTapEditorBase extends BaseElement { return attachment.attachment.attachmentId === e.attachment.attachmentId; } - return false + return false; }); if (index > -1) {