mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
* feat: Render uploading images and videos with their node component * fix: Insert uploaded block nodes next to the paragraph instead of inside it * fix: Insert dropped block nodes on the nearest side of the paragraph * feat: Fade in uploaded images and videos from their placeholder * feat: Preview the new image while it replaces an existing one * fix: Keep the placeholder size and preload pasted images after upload * fix: Move the selection off an image while an upload replaces it * feat: Play uploaded videos from the local file in the uploading editor * feat: Display uploaded images from the local file in the uploading editor * feat: Soften image and video corners, round them fully when selected * feat: Render uploading attachments with the attachment component * feat: Show the file size of an attachment while it uploads * feat: Show upload progress on the placeholder instead of a toast * fix: Fade the content of an uploading file but not its progress bar * fix: Keep the local file of an upload when its placeholder is mapped away * chore: Simplify upload placeholder state and remove unused progress callback * Remove test * fix: Address review feedback on upload placeholders Expose upload progress to assistive technology outside the inert placeholder, free local files when the editor state is replaced, reuse the fadeIn animation and move the video wrapper class to EditorStyleHelper. * chore: Move image and PDF wrapper class names to EditorStyleHelper * feat: Scale in an upload placeholder when it appears * chore: Make the upload placeholder scale-in more subtle
312 lines
10 KiB
TypeScript
312 lines
10 KiB
TypeScript
import type { Node } from "prosemirror-model";
|
||
import { Fragment, Slice } from "prosemirror-model";
|
||
import { NodeSelection, Plugin } from "prosemirror-state";
|
||
import { v4 as uuidv4 } from "uuid";
|
||
import {
|
||
getDataTransferFiles,
|
||
getDataTransferImage,
|
||
dataUrlToFile,
|
||
} from "../../utils/files";
|
||
import { fileNameFromUrl, isInternalUrl } from "../../utils/urls";
|
||
import type { Options } from "../commands/insertFiles";
|
||
import insertFiles from "../commands/insertFiles";
|
||
import FileHelper from "../lib/FileHelper";
|
||
import uploadPlaceholder, { findPlaceholder } from "../lib/uploadPlaceholder";
|
||
|
||
export class UploadPlugin extends Plugin {
|
||
constructor(options: Options) {
|
||
super({
|
||
props: {
|
||
handleDOMEvents: {
|
||
paste(view, event: ClipboardEvent): boolean {
|
||
if (!view.editable || !options.uploadFile) {
|
||
return false;
|
||
}
|
||
|
||
if (!event.clipboardData) {
|
||
return false;
|
||
}
|
||
|
||
// check if we actually pasted any files
|
||
const files = getDataTransferFiles(event);
|
||
if (files.length === 0) {
|
||
return false;
|
||
}
|
||
|
||
// When copying from Microsoft Office product the clipboard contains
|
||
// an image version of the content, check if there is also text and
|
||
// use that instead in this scenario.
|
||
const html = event.clipboardData.getData("text/html");
|
||
|
||
// Fallback to default paste behavior if the clipboard contains HTML
|
||
// Even if there is an image, it's likely to be a screenshot from eg
|
||
// Microsoft Suite / Apple Numbers – and not the original content.
|
||
if (html.length && !getDataTransferImage(event)) {
|
||
return false;
|
||
}
|
||
|
||
const { selection } = view.state;
|
||
|
||
// With an image selected, pasting a single image replaces it in
|
||
// place rather than inserting a second image alongside.
|
||
if (
|
||
selection instanceof NodeSelection &&
|
||
selection.node.type.name === "image" &&
|
||
files.length === 1 &&
|
||
FileHelper.isImage(files[0].type) &&
|
||
!options.isAttachment
|
||
) {
|
||
const { attrs } = selection.node;
|
||
|
||
void insertFiles(view, event, selection.from, files, {
|
||
...options,
|
||
replaceExisting: true,
|
||
attrs: {
|
||
width: attrs.width,
|
||
height: attrs.height,
|
||
alt: attrs.alt,
|
||
layoutClass: attrs.layoutClass,
|
||
},
|
||
});
|
||
return true;
|
||
}
|
||
|
||
if (!selection.empty) {
|
||
view.dispatch(view.state.tr.deleteSelection());
|
||
}
|
||
|
||
void insertFiles(
|
||
view,
|
||
event,
|
||
view.state.selection.from,
|
||
files,
|
||
options
|
||
);
|
||
return true;
|
||
},
|
||
drop(view, event: DragEvent): boolean {
|
||
if (!view.editable || !options.uploadFile) {
|
||
return false;
|
||
}
|
||
|
||
// grab the position in the document for the cursor
|
||
const result = view.posAtCoords({
|
||
left: event.clientX,
|
||
top: event.clientY,
|
||
});
|
||
if (!result) {
|
||
return false;
|
||
}
|
||
|
||
const files = getDataTransferFiles(event);
|
||
if (files.length) {
|
||
void insertFiles(view, event, result.pos, files, options);
|
||
return true;
|
||
}
|
||
|
||
const imageSrc = getDataTransferImage(event);
|
||
if (imageSrc && !isInternalUrl(imageSrc)) {
|
||
event.stopPropagation();
|
||
event.preventDefault();
|
||
|
||
void fetch(imageSrc)
|
||
.then((response) => response.blob())
|
||
.then((blob) => {
|
||
const fileName = fileNameFromUrl(imageSrc) ?? "pasted-image";
|
||
|
||
void insertFiles(
|
||
view,
|
||
event,
|
||
result.pos,
|
||
[
|
||
new File([blob], fileName, {
|
||
type: blob.type,
|
||
}),
|
||
],
|
||
options
|
||
);
|
||
});
|
||
}
|
||
|
||
return false;
|
||
},
|
||
},
|
||
transformPasted: (slice, view) => {
|
||
const uploads: {
|
||
originalSrc: string;
|
||
searchSrc: string;
|
||
id: string;
|
||
}[] = [];
|
||
|
||
const mapNode = (node: Node): Node => {
|
||
if (
|
||
node.type.name === "image" &&
|
||
node.attrs.src &&
|
||
!isInternalUrl(node.attrs.src)
|
||
) {
|
||
const id = uuidv4();
|
||
const redirectUrl = `/api/attachments.redirect?id=${id}`;
|
||
uploads.push({
|
||
originalSrc: node.attrs.src,
|
||
searchSrc: redirectUrl,
|
||
id,
|
||
});
|
||
return node.type.create({
|
||
...node.attrs,
|
||
src: redirectUrl,
|
||
});
|
||
}
|
||
|
||
if (node.content.size > 0) {
|
||
const nodes: Node[] = [];
|
||
node.content.forEach((child) => {
|
||
nodes.push(mapNode(child));
|
||
});
|
||
return node.copy(Fragment.from(nodes));
|
||
}
|
||
|
||
return node;
|
||
};
|
||
|
||
const nodes: Node[] = [];
|
||
slice.content.forEach((node) => {
|
||
nodes.push(mapNode(node));
|
||
});
|
||
const newContent = Fragment.from(nodes);
|
||
|
||
// We need to wait for the pasted content to be inserted before we can
|
||
// find the nodes and replace them with placeholders.
|
||
setTimeout(() => {
|
||
void Promise.all(
|
||
uploads.map(async (upload) => {
|
||
if (view.isDestroyed) {
|
||
return;
|
||
}
|
||
|
||
const { state } = view;
|
||
let pos = -1;
|
||
let nodeSize = 0;
|
||
let attrs = {};
|
||
let dimensions: { width?: number; height?: number } | undefined;
|
||
let file: File | undefined;
|
||
|
||
state.doc.nodesBetween(
|
||
0,
|
||
state.doc.nodeSize - 2,
|
||
(node, nodePos) => {
|
||
if (
|
||
node.type.name === "image" &&
|
||
node.attrs.src === upload.searchSrc
|
||
) {
|
||
pos = nodePos;
|
||
nodeSize = node.nodeSize;
|
||
attrs = node.attrs;
|
||
if (node.attrs.width || node.attrs.height) {
|
||
dimensions = {
|
||
width: node.attrs.width,
|
||
height: node.attrs.height,
|
||
};
|
||
}
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
);
|
||
|
||
if (pos !== -1) {
|
||
const isBase64 = upload.originalSrc.startsWith("data:");
|
||
file = isBase64
|
||
? dataUrlToFile(upload.originalSrc, "pasted-image")
|
||
: undefined;
|
||
dimensions =
|
||
(isBase64 && file
|
||
? await FileHelper.getImageDimensions(file)
|
||
: undefined) ?? dimensions;
|
||
|
||
if (view.isDestroyed) {
|
||
return;
|
||
}
|
||
|
||
// The position may have changed while we were awaiting dimensions
|
||
let currentPos = -1;
|
||
view.state.doc.nodesBetween(
|
||
0,
|
||
view.state.doc.nodeSize - 2,
|
||
(node, nodePos) => {
|
||
if (
|
||
node.type.name === "image" &&
|
||
node.attrs.src === upload.searchSrc
|
||
) {
|
||
currentPos = nodePos;
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
);
|
||
|
||
if (currentPos !== -1) {
|
||
view.dispatch(
|
||
view.state.tr
|
||
.setMeta(uploadPlaceholder, {
|
||
add: {
|
||
id: upload.id,
|
||
file,
|
||
src: isBase64 ? undefined : upload.originalSrc,
|
||
pos: currentPos,
|
||
type: "image",
|
||
attrs: { ...attrs, ...dimensions },
|
||
},
|
||
})
|
||
.delete(currentPos, currentPos + nodeSize)
|
||
);
|
||
}
|
||
}
|
||
|
||
const url = await options.uploadFile?.(upload.originalSrc, {
|
||
id: upload.id,
|
||
});
|
||
|
||
if (view.isDestroyed) {
|
||
return;
|
||
}
|
||
|
||
if (url) {
|
||
// The uploaded image is only measured if the placeholder had
|
||
// no dimensions, so that the size does not change.
|
||
dimensions ??= await FileHelper.getImageDimensions(
|
||
await FileHelper.getFileForUrl(url)
|
||
);
|
||
// Without a local file the image displays from storage, so
|
||
// it is loaded before it replaces the placeholder.
|
||
if (!file) {
|
||
await FileHelper.loadImage(url);
|
||
}
|
||
|
||
const result = findPlaceholder(view.state, upload.id);
|
||
if (result && !view.isDestroyed) {
|
||
const [from, to] = result;
|
||
const node = view.state.schema.nodes.image.create({
|
||
...attrs,
|
||
...dimensions,
|
||
src: url,
|
||
});
|
||
view.dispatch(
|
||
view.state.tr
|
||
.replaceWith(from, to || from, node)
|
||
.setMeta(uploadPlaceholder, {
|
||
remove: { id: upload.id, node },
|
||
})
|
||
);
|
||
}
|
||
}
|
||
})
|
||
);
|
||
}, 0);
|
||
|
||
return new Slice(newContent, slice.openStart, slice.openEnd);
|
||
},
|
||
},
|
||
});
|
||
}
|
||
}
|