feat: Add option to replace existing file attachment in editor
This commit is contained in:
@@ -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";
|
||||
|
||||
Reference in New Issue
Block a user