mirror of
https://github.com/outline/outline.git
synced 2026-11-21 13:34:02 +03:00
* feat: POC * fix: cleanup * fix: cleanup * fix: can use existing `UiStore` in favor of `LightboxStore` * fix: style dialog overlay and content * fix: style and position action buttons * fix: keyboard nav * fix: display caption * fix: cleanup * fix: force `Lightbox` to unmount if `DocumentScene` re-renders * fix: cleanup * fix: images going out of bounds–trying `object-fit` for first pass * fix: making `Figure` a flexbox and setting its size to img dimensions, gets rid of the `object-fit` "letterboxing" effect * feat: image transition * fix: match animation to that of `ImageZoom` * fix: `fade-in` overlay * fix: match overlay background to main background * fix: fade out nav if idle for 3s * fix: width & height transition in firefox * fix: move nav buttons sideways * fix: button sizes * fix: don't let nav buttons become invisible due to image in backdrop * feat: download btn * feat: handle swipe left, right & down on mobile, also double tap to open lightbox * fix: off-by-one * fix: for img to be visible, if not animating * fix: cleanup * just a showcase commit, it's broken and will be reverted * Revert "just a showcase commit, it's broken and will be reverted" This reverts commit 63d7cbda9895f92a530cfb3a8402eaacb2414947. * feat: `animateOnClose` * fix: prevent body from shifting horizontally * Revert "fix: prevent body from shifting horizontally" This reverts commit fcc2d0d1daaab458325ece880d13903b42560dfd. * fix: lint * fix: make lightbox resilient to doc changes * fix: `VisuallyHidden` `Dialog.Title` * fix: fade in action and nav buttons too * fix: `fadeOut` overlay, action & nav buttons upon close * fix: `dom.querySelector` is not a function * fix: let `objectFit` compute the img dimensions * fix: flashing caption * fix: hide nav keys based on whether it's a first or last img * fix: tooltip for action buttons * Fixes: 1. `Space` with the image selected should trigger the lightbox 2. There should be a "Zoom in" cursor on the image when selected so you know you can click to zoom * fix: fade out on close if there's img loading err * fix: pull animation duration into a const * fix: upscaled editor image animating unreliably * fix: lint * fixes: 1. Model as state machine 2. Replace `transition` with `animation` * fix: cleanup * fix: DRY * fix: cleanup * fix: img loading indicator * fix: swipe * fix: types * fix: Incorrect transitions when image nodes are duplicated * Tweaks, translations, aria * fix: Incorrect transition if image is added while lightbox is open * fix: review * Remove ImageZoom, react-medium-image-zoom * Tweak styling * fix: Quick fix for multiple open editors * refactor: Move active lightbox state into editor * Apply graphite suggestions --------- Co-authored-by: Tom Moor <tom@getoutline.com>
236 lines
6.3 KiB
TypeScript
236 lines
6.3 KiB
TypeScript
import { Token } from "markdown-it";
|
|
import { InputRule } from "prosemirror-inputrules";
|
|
import { Node as ProsemirrorNode, NodeSpec, NodeType } from "prosemirror-model";
|
|
import { TextSelection, NodeSelection, Command } from "prosemirror-state";
|
|
import * as React from "react";
|
|
import { Primitive } from "utility-types";
|
|
import { getEventFiles } from "../../utils/files";
|
|
import { sanitizeUrl } from "../../utils/urls";
|
|
import { AttachmentValidation } from "../../validations";
|
|
import insertFiles, { Options } from "../commands/insertFiles";
|
|
import { default as ImageComponent } from "../components/Image";
|
|
import { MarkdownSerializerState } from "../lib/markdown/serializer";
|
|
import uploadPlaceholderPlugin from "../lib/uploadPlaceholder";
|
|
import { UploadPlugin } from "../plugins/UploadPlugin";
|
|
import { ComponentProps } from "../types";
|
|
import Node from "./Node";
|
|
|
|
export default class SimpleImage extends Node {
|
|
options: Options;
|
|
|
|
get name() {
|
|
return "image";
|
|
}
|
|
|
|
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: sanitizeUrl(node.attrs.src),
|
|
contentEditable: "false",
|
|
},
|
|
],
|
|
],
|
|
};
|
|
}
|
|
|
|
handleClick =
|
|
({ getPos }: ComponentProps) =>
|
|
() => {
|
|
this.editor.updateActiveLightbox(getPos());
|
|
};
|
|
|
|
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 } =
|
|
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,
|
|
dictionary: this.options.dictionary,
|
|
replaceExisting: true,
|
|
attrs: {
|
|
width: node.attrs.width,
|
|
},
|
|
});
|
|
};
|
|
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)];
|
|
}
|
|
}
|