mirror of
https://github.com/docmost/docmost.git
synced 2026-08-30 02:25:01 +08:00
fix: Transaction handling on asset upload
This commit is contained in:
@@ -2,6 +2,7 @@ import { Node } from "@tiptap/pm/model";
|
|||||||
import { MediaUploadOptions, UploadFn } from "../media-utils";
|
import { MediaUploadOptions, UploadFn } from "../media-utils";
|
||||||
import { IAttachment } from "../types";
|
import { IAttachment } from "../types";
|
||||||
import { generateNodeId } from "../utils";
|
import { generateNodeId } from "../utils";
|
||||||
|
import { Transaction } from "@tiptap/pm/state";
|
||||||
|
|
||||||
const findAttachmentNodeByPlaceholderId = (
|
const findAttachmentNodeByPlaceholderId = (
|
||||||
doc: Node,
|
doc: Node,
|
||||||
@@ -37,8 +38,8 @@ const handleAttachmentUpload =
|
|||||||
size: file.size,
|
size: file.size,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let tr: Transaction | null = view.state.tr;
|
||||||
let placeholderShown = false;
|
let placeholderShown = false;
|
||||||
let tr = view.state.tr;
|
|
||||||
|
|
||||||
if (!initialPlaceholderNode) return;
|
if (!initialPlaceholderNode) return;
|
||||||
|
|
||||||
@@ -55,11 +56,14 @@ const handleAttachmentUpload =
|
|||||||
const displayPlaceholderTimeout = setTimeout(() => {
|
const displayPlaceholderTimeout = setTimeout(() => {
|
||||||
view.dispatch(tr);
|
view.dispatch(tr);
|
||||||
placeholderShown = true;
|
placeholderShown = true;
|
||||||
tr = view.state.tr;
|
tr = null;
|
||||||
}, 250);
|
}, 250);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const attachment: IAttachment = await onUpload(file, pageId);
|
const attachment: IAttachment = await onUpload(file, pageId);
|
||||||
|
|
||||||
|
tr = tr ?? view.state.tr;
|
||||||
|
|
||||||
const { pos: currentPos = null } =
|
const { pos: currentPos = null } =
|
||||||
findAttachmentNodeByPlaceholderId(tr.doc, placeholderId) || {};
|
findAttachmentNodeByPlaceholderId(tr.doc, placeholderId) || {};
|
||||||
|
|
||||||
@@ -75,6 +79,8 @@ const handleAttachmentUpload =
|
|||||||
attachmentId: attachment.id,
|
attachmentId: attachment.id,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
tr = tr ?? view.state.tr;
|
||||||
|
|
||||||
const { pos: currentPos = null } =
|
const { pos: currentPos = null } =
|
||||||
findAttachmentNodeByPlaceholderId(tr.doc, placeholderId) || {};
|
findAttachmentNodeByPlaceholderId(tr.doc, placeholderId) || {};
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { MediaUploadOptions, UploadFn } from "../media-utils";
|
|||||||
import { IAttachment } from "../types";
|
import { IAttachment } from "../types";
|
||||||
import { generateNodeId } from "../utils";
|
import { generateNodeId } from "../utils";
|
||||||
import { Node } from "@tiptap/pm/model";
|
import { Node } from "@tiptap/pm/model";
|
||||||
|
import { Transaction } from "@tiptap/pm/state";
|
||||||
|
|
||||||
const findImageNodeByPlaceholderId = (
|
const findImageNodeByPlaceholderId = (
|
||||||
doc: Node,
|
doc: Node,
|
||||||
@@ -42,8 +43,8 @@ const handleImageUpload =
|
|||||||
aspectRatio,
|
aspectRatio,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let tr: Transaction | null = view.state.tr;
|
||||||
let placeholderShown = false;
|
let placeholderShown = false;
|
||||||
let tr = view.state.tr;
|
|
||||||
|
|
||||||
if (!initialPlaceholderNode) return;
|
if (!initialPlaceholderNode) return;
|
||||||
|
|
||||||
@@ -61,11 +62,14 @@ const handleImageUpload =
|
|||||||
const displayPlaceholderTimeout = setTimeout(() => {
|
const displayPlaceholderTimeout = setTimeout(() => {
|
||||||
view.dispatch(tr);
|
view.dispatch(tr);
|
||||||
placeholderShown = true;
|
placeholderShown = true;
|
||||||
tr = view.state.tr;
|
tr = null;
|
||||||
}, 250);
|
}, 250);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const attachment: IAttachment = await onUpload(file, pageId);
|
const attachment: IAttachment = await onUpload(file, pageId);
|
||||||
|
|
||||||
|
tr = tr ?? view.state.tr;
|
||||||
|
|
||||||
const { pos: currentPos = null } =
|
const { pos: currentPos = null } =
|
||||||
findImageNodeByPlaceholderId(tr.doc, placeholderId) || {};
|
findImageNodeByPlaceholderId(tr.doc, placeholderId) || {};
|
||||||
|
|
||||||
@@ -81,6 +85,8 @@ const handleImageUpload =
|
|||||||
aspectRatio,
|
aspectRatio,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
tr = tr ?? view.state.tr;
|
||||||
|
|
||||||
const { pos: currentPos = null } =
|
const { pos: currentPos = null } =
|
||||||
findImageNodeByPlaceholderId(tr.doc, placeholderId) || {};
|
findImageNodeByPlaceholderId(tr.doc, placeholderId) || {};
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { Transaction } from "@tiptap/pm/state";
|
||||||
import { MediaUploadOptions, UploadFn } from "../media-utils";
|
import { MediaUploadOptions, UploadFn } from "../media-utils";
|
||||||
import { IAttachment } from "../types";
|
import { IAttachment } from "../types";
|
||||||
import { generateNodeId } from "../utils";
|
import { generateNodeId } from "../utils";
|
||||||
@@ -66,8 +67,8 @@ const handleVideoUpload =
|
|||||||
aspectRatio,
|
aspectRatio,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let tr: Transaction | null = view.state.tr;
|
||||||
let placeholderShown = false;
|
let placeholderShown = false;
|
||||||
let tr = view.state.tr;
|
|
||||||
|
|
||||||
if (!initialPlaceholderNode) {
|
if (!initialPlaceholderNode) {
|
||||||
URL.revokeObjectURL(objectUrl);
|
URL.revokeObjectURL(objectUrl);
|
||||||
@@ -88,11 +89,14 @@ const handleVideoUpload =
|
|||||||
const displayPlaceholderTimeout = setTimeout(() => {
|
const displayPlaceholderTimeout = setTimeout(() => {
|
||||||
view.dispatch(tr);
|
view.dispatch(tr);
|
||||||
placeholderShown = true;
|
placeholderShown = true;
|
||||||
tr = view.state.tr;
|
tr = null;
|
||||||
}, 250);
|
}, 250);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const attachment: IAttachment = await onUpload(file, pageId);
|
const attachment: IAttachment = await onUpload(file, pageId);
|
||||||
|
|
||||||
|
tr = tr ?? view.state.tr;
|
||||||
|
|
||||||
const { pos: currentPos = null } =
|
const { pos: currentPos = null } =
|
||||||
findVideoNodeByPlaceholderId(tr.doc, placeholderId) || {};
|
findVideoNodeByPlaceholderId(tr.doc, placeholderId) || {};
|
||||||
|
|
||||||
@@ -108,6 +112,8 @@ const handleVideoUpload =
|
|||||||
aspectRatio,
|
aspectRatio,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
tr = tr ?? view.state.tr;
|
||||||
|
|
||||||
const { pos: currentPos = null } =
|
const { pos: currentPos = null } =
|
||||||
findVideoNodeByPlaceholderId(tr.doc, placeholderId) || {};
|
findVideoNodeByPlaceholderId(tr.doc, placeholderId) || {};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user