Skip to content

Commit

Permalink
fix pending uploads not getting cleared (#237)
Browse files Browse the repository at this point in the history
* fix pending uploads not getting cleared

* fix pending uploads not getting cleared

* prettier
  • Loading branch information
KonnorRogers authored Dec 5, 2024
1 parent 67e4c9c commit 1c395c5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-tigers-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rhino-editor": patch
---

Fix pendingAttachments not clearing attachments that get cancelled
35 changes: 30 additions & 5 deletions src/exports/elements/tip-tap-editor-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -393,12 +394,12 @@ 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,
Expand All @@ -407,19 +408,43 @@ 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) {
Expand Down

0 comments on commit 1c395c5

Please sign in to comment.