feat: Add option to replace existing file attachment in editor

This commit is contained in:
Tom Moor
2024-01-21 11:52:20 -05:00
parent cbb00c4871
commit 4ddb5c3eed
8 changed files with 157 additions and 39 deletions

View File

@@ -38,39 +38,39 @@ const uploadPlaceholder = new Plugin({
set = set.map(mapping, tr.doc);
if (action?.add) {
if (action.add.isImage) {
if (action.add.replaceExisting) {
const $pos = tr.doc.resolve(action.add.pos);
if ($pos.nodeAfter?.type.name === "image") {
const deco = Decoration.node(
$pos.pos,
$pos.pos + $pos.nodeAfter.nodeSize,
{
class: "image-replacement-uploading",
},
{
id: action.add.id,
}
);
set = set.add(tr.doc, [deco]);
}
} else {
const element = document.createElement("div");
element.className = "image placeholder";
const img = document.createElement("img");
img.src = URL.createObjectURL(action.add.file);
img.width = action.add.dimensions?.width;
img.height = action.add.dimensions?.height;
element.appendChild(img);
const deco = Decoration.widget(action.add.pos, element, {
id: action.add.id,
});
set = set.add(tr.doc, [deco]);
if (action.add.replaceExisting) {
const $pos = tr.doc.resolve(action.add.pos);
const nodeAfter = $pos.nodeAfter;
if (!nodeAfter) {
return;
}
const deco = Decoration.node(
$pos.pos,
$pos.pos + nodeAfter.nodeSize,
{
class: `${nodeAfter.type.name}-replacement-uploading`,
},
{
id: action.add.id,
}
);
set = set.add(tr.doc, [deco]);
} else if (action.add.isImage) {
const element = document.createElement("div");
element.className = "image placeholder";
const img = document.createElement("img");
img.src = URL.createObjectURL(action.add.file);
img.width = action.add.dimensions?.width;
img.height = action.add.dimensions?.height;
element.appendChild(img);
const deco = Decoration.widget(action.add.pos, element, {
id: action.add.id,
});
set = set.add(tr.doc, [deco]);
} else if (action.add.isVideo) {
const element = document.createElement("div");
element.className = "video placeholder";