mirror of
https://github.com/outline/outline.git
synced 2026-12-03 01:59:03 +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
262 lines
7.1 KiB
TypeScript
262 lines
7.1 KiB
TypeScript
import type Token from "markdown-it/lib/token.mjs";
|
|
import { InputRule } from "prosemirror-inputrules";
|
|
import type {
|
|
Node as ProsemirrorNode,
|
|
NodeSpec,
|
|
NodeType,
|
|
} from "prosemirror-model";
|
|
import type { Command } from "prosemirror-state";
|
|
import { TextSelection, NodeSelection } from "prosemirror-state";
|
|
import * as React from "react";
|
|
import type { Primitive } from "utility-types";
|
|
import { getEventFiles } from "../../utils/files";
|
|
import { sanitizeImageSrc } from "../../utils/urls";
|
|
import { AttachmentValidation } from "../../validations";
|
|
import type { Options } from "../commands/insertFiles";
|
|
import insertFiles from "../commands/insertFiles";
|
|
import { default as ImageComponent } from "../components/Image";
|
|
import type { MarkdownSerializerState } from "../lib/markdown/serializer";
|
|
import uploadPlaceholderPlugin from "../lib/uploadPlaceholder";
|
|
import { UploadPlugin } from "../plugins/UploadPlugin";
|
|
import type { ComponentProps } from "../types";
|
|
import Node from "./Node";
|
|
import { LightboxImageFactory } from "../lib/Lightbox";
|
|
|
|
export default class SimpleImage extends Node {
|
|
options: Options & {
|
|
userId?: string;
|
|
};
|
|
|
|
get name() {
|
|
return "image";
|
|
}
|
|
|
|
/** The component relies on load events to reveal the image, and `toDOM` already emits a plain img. */
|
|
get allowComponentInStaticHTML() {
|
|
return false;
|
|
}
|
|
|
|
get schema(): NodeSpec {
|
|
return {
|
|
inline: true,
|
|
attrs: {
|
|
src: {
|
|
default: "",
|
|
},
|
|
alt: {
|
|
default: null,
|
|
},
|
|
},
|
|
content: "text*",
|
|
marks: "",
|
|
group: "inline",
|
|
selectable: true,
|
|
draggable: true,
|
|
parseDOM: [
|
|
{
|
|
tag: "div[class~=image]",
|
|
getAttrs: (dom: HTMLDivElement) => {
|
|
const img = dom.getElementsByTagName("img")[0];
|
|
return {
|
|
src: img?.getAttribute("src"),
|
|
alt: img?.getAttribute("alt"),
|
|
title: img?.getAttribute("title"),
|
|
};
|
|
},
|
|
},
|
|
{
|
|
tag: "img",
|
|
getAttrs: (dom: HTMLImageElement) => ({
|
|
src: dom.getAttribute("src"),
|
|
alt: dom.getAttribute("alt"),
|
|
title: dom.getAttribute("title"),
|
|
}),
|
|
},
|
|
],
|
|
toDOM: (node) => [
|
|
"div",
|
|
{
|
|
class: "image",
|
|
},
|
|
[
|
|
"img",
|
|
{
|
|
...node.attrs,
|
|
src: sanitizeImageSrc(node.attrs.src),
|
|
contentEditable: "false",
|
|
},
|
|
],
|
|
],
|
|
leafText: (node) =>
|
|
node.attrs.alt ? `(image: ${node.attrs.alt})` : "(image)",
|
|
};
|
|
}
|
|
|
|
handleClick =
|
|
({ view, getPos }: ComponentProps) =>
|
|
() => {
|
|
const pos = getPos();
|
|
if (pos === undefined) {
|
|
return;
|
|
}
|
|
|
|
this.editor.updateActiveLightboxImage(
|
|
LightboxImageFactory.createLightboxImage(view, pos)
|
|
);
|
|
};
|
|
|
|
component = (props: ComponentProps) => (
|
|
<ImageComponent {...props} onClick={this.handleClick(props)} />
|
|
);
|
|
|
|
keys(): Record<string, Command> {
|
|
return {
|
|
Enter: (state, dispatch) => {
|
|
const { selection } = state;
|
|
if (
|
|
selection instanceof NodeSelection &&
|
|
selection.node?.type.name === this.name
|
|
) {
|
|
const tr = state.tr;
|
|
if (dispatch) {
|
|
dispatch(
|
|
tr
|
|
.insert(selection.to, state.schema.nodes.paragraph.create({}))
|
|
.setSelection(
|
|
TextSelection.near(tr.doc.resolve(selection.to + 2), 1)
|
|
)
|
|
.scrollIntoView()
|
|
);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
},
|
|
};
|
|
}
|
|
|
|
toMarkdown(state: MarkdownSerializerState, node: ProsemirrorNode) {
|
|
state.write(
|
|
"  +
|
|
")"
|
|
);
|
|
}
|
|
|
|
parseMarkdown() {
|
|
return {
|
|
node: "image",
|
|
getAttrs: (token: Token) => ({
|
|
src: token.attrGet("src"),
|
|
alt: token.content || null,
|
|
}),
|
|
};
|
|
}
|
|
|
|
commands({ type }: { type: NodeType }) {
|
|
return {
|
|
deleteImage: (): Command => (state, dispatch) => {
|
|
dispatch?.(state.tr.deleteSelection());
|
|
return true;
|
|
},
|
|
replaceImage: (): Command => (state) => {
|
|
if (!(state.selection instanceof NodeSelection)) {
|
|
return false;
|
|
}
|
|
const { view } = this.editor;
|
|
const { node } = state.selection;
|
|
const { uploadFile, onFileUploadStart, onFileUploadStop, onNotice } =
|
|
this.editor.props;
|
|
|
|
if (!uploadFile) {
|
|
throw new Error("uploadFile prop is required to replace images");
|
|
}
|
|
|
|
if (node.type.name !== "image") {
|
|
return false;
|
|
}
|
|
|
|
// create an input element and click to trigger picker
|
|
const inputElement = document.createElement("input");
|
|
inputElement.type = "file";
|
|
inputElement.accept = AttachmentValidation.imageContentTypes.join(", ");
|
|
inputElement.onchange = (event) => {
|
|
const files = getEventFiles(event);
|
|
void insertFiles(view, event, state.selection.from, files, {
|
|
uploadFile,
|
|
onFileUploadStart,
|
|
onFileUploadStop,
|
|
onNotice,
|
|
replaceExisting: true,
|
|
attrs: {
|
|
width: node.attrs.width,
|
|
height: node.attrs.height,
|
|
alt: node.attrs.alt,
|
|
layoutClass: node.attrs.layoutClass,
|
|
},
|
|
});
|
|
};
|
|
inputElement.click();
|
|
return true;
|
|
},
|
|
createImage:
|
|
(attrs: Record<string, Primitive>): Command =>
|
|
(state, dispatch) => {
|
|
const { selection } = state;
|
|
const position =
|
|
selection instanceof TextSelection
|
|
? selection.$cursor?.pos
|
|
: selection.$to.pos;
|
|
if (position === undefined) {
|
|
return false;
|
|
}
|
|
|
|
const node = type.create(attrs);
|
|
const transaction = state.tr.insert(position, node);
|
|
dispatch?.(transaction);
|
|
return true;
|
|
},
|
|
};
|
|
}
|
|
|
|
inputRules({ type }: { type: NodeType }) {
|
|
/**
|
|
* Matches following attributes in Markdown-typed image: [, alt, src, class]
|
|
*
|
|
* Example:
|
|
*  -> [, "Lorem", "image.jpg"]
|
|
*  -> [, "", "image.jpg", "small"]
|
|
*  -> [, "Lorem", "image.jpg", "small"]
|
|
*/
|
|
const IMAGE_INPUT_REGEX =
|
|
/!\[(?<alt>[^\][]*?)]\((?<filename>[^\][]*?)(?=“|\))“?(?<layoutclass>[^\][”]+)?”?\)$/;
|
|
|
|
return [
|
|
new InputRule(IMAGE_INPUT_REGEX, (state, match, start, end) => {
|
|
const [okay, alt, src] = match;
|
|
const { tr } = state;
|
|
|
|
if (okay) {
|
|
tr.replaceWith(
|
|
start - 1,
|
|
end,
|
|
type.create({
|
|
src,
|
|
alt,
|
|
})
|
|
);
|
|
}
|
|
|
|
return tr;
|
|
}),
|
|
];
|
|
}
|
|
|
|
get plugins() {
|
|
return [uploadPlaceholderPlugin, new UploadPlugin(this.options)];
|
|
}
|
|
}
|