mirror of
https://github.com/outline/outline.git
synced 2026-11-28 00:09:58 +03:00
Include mermaid SVGs in Lightbox (#10146)
* fix: include mermaid svgs in lightbox * Fixes: 1. Focus isn't restored back to mermaid code block when Lightbox is closed 2. Read-only mode requires extra click on to both open and close Lightbox for mermaid SVGs * fix: `zoom-in` cursor for SVGs * fix: make SVGs downloadable * fix: tsc * fix: graphite * fix: zoom-in should span the wrapper * fix: graphite * fix: name * fix: no need to re-render mermaid svg within lightbox fix: rely on `code-block` as the `svg` is updated upon doc change * fix: graphite * fix: lightbox crash when mermaid block is deleted * fix: render mermaid at pos `0` * fix: graphite * fix: refactor to simplify Lightbox * fix: graphite
This commit is contained in:
1 parent
0f9146066c
commit
d8ef7b2892
10 files changed
+285
-84
No files matched your search
+93
-67
@@ -1,12 +1,9 @@
|
||||
import { useEditor } from "~/editor/components/EditorContext";
|
||||
import { observer } from "mobx-react";
|
||||
import * as Dialog from "@radix-ui/react-dialog";
|
||||
import { findChildren } from "@shared/editor/queries/findChildren";
|
||||
import findIndex from "lodash/findIndex";
|
||||
import styled, { css, Keyframes, keyframes } from "styled-components";
|
||||
import { forwardRef, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { sanitizeUrl } from "@shared/utils/urls";
|
||||
import { Error } from "@shared/editor/components/Image";
|
||||
import { forwardRef, useCallback, useEffect, useRef, useState } from "react";
|
||||
import { isInternalUrl } from "@shared/utils/urls";
|
||||
import { Error as ImageError } from "@shared/editor/components/Image";
|
||||
import {
|
||||
BackIcon,
|
||||
CloseIcon,
|
||||
@@ -19,7 +16,6 @@ import { depths, extraArea, s } from "@shared/styles";
|
||||
import NudeButton from "./NudeButton";
|
||||
import useIdle from "~/hooks/useIdle";
|
||||
import { Second } from "@shared/utils/time";
|
||||
import { downloadImageNode } from "@shared/editor/nodes/Image";
|
||||
import * as VisuallyHidden from "@radix-ui/react-visually-hidden";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import Tooltip from "~/components/Tooltip";
|
||||
@@ -29,6 +25,9 @@ import Button from "./Button";
|
||||
import CopyToClipboard from "./CopyToClipboard";
|
||||
import { Separator } from "./Actions";
|
||||
import useSwipe from "~/hooks/useSwipe";
|
||||
import { toast } from "sonner";
|
||||
import { findIndex } from "lodash";
|
||||
import { LightboxImage } from "@shared/editor/lib/Lightbox";
|
||||
|
||||
export enum LightboxStatus {
|
||||
READY_TO_OPEN,
|
||||
@@ -60,22 +59,22 @@ type Animation = {
|
||||
const ANIMATION_DURATION = 0.3 * Second.ms;
|
||||
|
||||
type Props = {
|
||||
/** Callback triggered when the active image position is updated */
|
||||
onUpdate: (pos: number | null) => void;
|
||||
/** List of allowed images */
|
||||
images: LightboxImage[];
|
||||
/** The position of the currently active image in the document */
|
||||
activePos: number | null;
|
||||
activeImage: LightboxImage;
|
||||
/** Callback triggered when the active image is updated */
|
||||
onUpdate: (activeImage: LightboxImage | null) => void;
|
||||
/** Callback triggered when Lightbox closes */
|
||||
onClose: () => void;
|
||||
};
|
||||
|
||||
function Lightbox({ onUpdate, activePos }: Props) {
|
||||
const { view } = useEditor();
|
||||
function Lightbox({ images, activeImage, onUpdate, onClose }: Props) {
|
||||
const isIdle = useIdle(3 * Second.ms);
|
||||
const { t } = useTranslation();
|
||||
const imgRef = useRef<HTMLImageElement | null>(null);
|
||||
const overlayRef = useRef<HTMLDivElement | null>(null);
|
||||
const [status, setStatus] = useState<Status>({ lightbox: null, image: null });
|
||||
const [imageElements] = useState(
|
||||
view?.dom.querySelectorAll(".component-image img")
|
||||
);
|
||||
const animation = useRef<Animation | null>(null);
|
||||
const finalImage = useRef<{
|
||||
center: { x: number; y: number };
|
||||
@@ -83,23 +82,10 @@ function Lightbox({ onUpdate, activePos }: Props) {
|
||||
height: number;
|
||||
} | null>(null);
|
||||
|
||||
const imageNodes = useMemo(
|
||||
() =>
|
||||
view
|
||||
? findChildren(
|
||||
view.state.doc,
|
||||
(child) => child.type === view.state.schema.nodes.image,
|
||||
true
|
||||
)
|
||||
: [],
|
||||
[view]
|
||||
);
|
||||
const currentImageIndex = findIndex(
|
||||
imageNodes,
|
||||
(node) => node.pos === activePos
|
||||
images,
|
||||
(img) => img.getPos() === activeImage.getPos()
|
||||
);
|
||||
const currentImageNode =
|
||||
currentImageIndex >= 0 ? imageNodes[currentImageIndex].node : undefined;
|
||||
|
||||
// Debugging status changes
|
||||
// useEffect(() => {
|
||||
@@ -108,15 +94,21 @@ function Lightbox({ onUpdate, activePos }: Props) {
|
||||
// );
|
||||
// }, [status]);
|
||||
|
||||
useEffect(() => () => view.focus(), []);
|
||||
useEffect(
|
||||
() => () => {
|
||||
if (status.lightbox === LightboxStatus.CLOSED) {
|
||||
onClose();
|
||||
}
|
||||
},
|
||||
[status.lightbox]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
!!activePos &&
|
||||
setStatus({
|
||||
lightbox: LightboxStatus.READY_TO_OPEN,
|
||||
image: status.image,
|
||||
});
|
||||
}, [!!activePos]);
|
||||
setStatus({
|
||||
lightbox: LightboxStatus.READY_TO_OPEN,
|
||||
image: status.image,
|
||||
});
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (status.image === ImageStatus.LOADED) {
|
||||
@@ -179,11 +171,10 @@ function Lightbox({ onUpdate, activePos }: Props) {
|
||||
const setupZoomIn = () => {
|
||||
if (imgRef.current) {
|
||||
// in editor
|
||||
const editorImageEl = imageElements[currentImageIndex];
|
||||
const editorImageEl = activeImage.getElement();
|
||||
if (!editorImageEl) {
|
||||
return;
|
||||
}
|
||||
|
||||
const editorImgDOMRect = editorImageEl.getBoundingClientRect();
|
||||
const {
|
||||
top: editorImgTop,
|
||||
@@ -289,7 +280,7 @@ function Lightbox({ onUpdate, activePos }: Props) {
|
||||
};
|
||||
|
||||
// in editor
|
||||
const editorImageEl = imageElements[currentImageIndex];
|
||||
const editorImageEl = activeImage.getElement();
|
||||
let to;
|
||||
if (editorImageEl?.isConnected) {
|
||||
const editorImgDOMRect = editorImageEl.getBoundingClientRect();
|
||||
@@ -364,33 +355,23 @@ function Lightbox({ onUpdate, activePos }: Props) {
|
||||
}
|
||||
};
|
||||
|
||||
if (!activePos) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const prev = () => {
|
||||
if (status.lightbox === LightboxStatus.OPENED) {
|
||||
if (!activePos) {
|
||||
return;
|
||||
}
|
||||
const prevIndex = currentImageIndex - 1;
|
||||
if (prevIndex < 0) {
|
||||
return;
|
||||
}
|
||||
onUpdate(imageNodes[prevIndex].pos);
|
||||
onUpdate(images[prevIndex]);
|
||||
}
|
||||
};
|
||||
|
||||
const next = () => {
|
||||
if (status.lightbox === LightboxStatus.OPENED) {
|
||||
if (!activePos) {
|
||||
return;
|
||||
}
|
||||
const nextIndex = currentImageIndex + 1;
|
||||
if (nextIndex >= imageNodes.length) {
|
||||
if (nextIndex >= images.length) {
|
||||
return;
|
||||
}
|
||||
onUpdate(imageNodes[nextIndex].pos);
|
||||
onUpdate(images[nextIndex]);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -406,12 +387,63 @@ function Lightbox({ onUpdate, activePos }: Props) {
|
||||
}
|
||||
};
|
||||
|
||||
const download = () => {
|
||||
if (currentImageNode && status.lightbox === LightboxStatus.OPENED) {
|
||||
void downloadImageNode(currentImageNode);
|
||||
const svgDataURLToBlob = (dataURL: string) => {
|
||||
// Match the SVG data URL format
|
||||
const match = dataURL.match(/^data:image\/svg\+xml,(.*)$/i);
|
||||
if (!match) {
|
||||
return;
|
||||
}
|
||||
|
||||
const encodedSVGData = match[1];
|
||||
const decodedSVGData = decodeURIComponent(encodedSVGData);
|
||||
|
||||
// Convert string to Uint8Array
|
||||
const uint8 = new Uint8Array(decodedSVGData.length);
|
||||
for (let i = 0; i < decodedSVGData.length; ++i) {
|
||||
uint8[i] = decodedSVGData.charCodeAt(i);
|
||||
}
|
||||
|
||||
// Create and return the Blob
|
||||
return new Blob([uint8], { type: "image/svg+xml" });
|
||||
};
|
||||
|
||||
const downloadImage = async (src: string, saveAs: string) => {
|
||||
let imageBlob;
|
||||
if (isInternalUrl(src)) {
|
||||
const image = await fetch(src);
|
||||
imageBlob = await image.blob();
|
||||
} else {
|
||||
// Assuming it's a mermaid svg
|
||||
imageBlob = svgDataURLToBlob(src);
|
||||
}
|
||||
|
||||
if (!imageBlob) {
|
||||
toast.error(t("Unable to download image"));
|
||||
return;
|
||||
}
|
||||
|
||||
const imageURL = URL.createObjectURL(imageBlob);
|
||||
const name = saveAs || "image";
|
||||
const extension = imageBlob.type.split(/\/|\+/g)[1];
|
||||
|
||||
// create a temporary link node and click it with our image data
|
||||
const link = document.createElement("a");
|
||||
link.href = imageURL;
|
||||
link.download = `${name}.${extension}`;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
|
||||
// cleanup
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(imageURL);
|
||||
};
|
||||
|
||||
const download = useCallback(() => {
|
||||
if (activeImage && status.lightbox === LightboxStatus.OPENED) {
|
||||
void downloadImage(activeImage.getSrc(), activeImage.getAlt());
|
||||
}
|
||||
}, [activeImage, status.lightbox]);
|
||||
|
||||
const handleKeyDown = (ev: React.KeyboardEvent<HTMLDivElement>) => {
|
||||
ev.preventDefault();
|
||||
switch (ev.key) {
|
||||
@@ -459,14 +491,8 @@ function Lightbox({ onUpdate, activePos }: Props) {
|
||||
}
|
||||
};
|
||||
|
||||
if (!currentImageNode) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const src = sanitizeUrl(currentImageNode.attrs.src) ?? "";
|
||||
|
||||
return (
|
||||
<Dialog.Root open={!!activePos}>
|
||||
<Dialog.Root open={true}>
|
||||
<Dialog.Portal>
|
||||
<StyledOverlay
|
||||
ref={overlayRef}
|
||||
@@ -529,8 +555,8 @@ function Lightbox({ onUpdate, activePos }: Props) {
|
||||
)}
|
||||
<Image
|
||||
ref={imgRef}
|
||||
src={src}
|
||||
alt={currentImageNode.attrs.alt ?? ""}
|
||||
src={activeImage.getSrc()}
|
||||
alt={activeImage.getAlt()}
|
||||
onLoading={() =>
|
||||
setStatus({
|
||||
lightbox: status.lightbox,
|
||||
@@ -556,7 +582,7 @@ function Lightbox({ onUpdate, activePos }: Props) {
|
||||
status={status}
|
||||
animation={animation.current}
|
||||
/>
|
||||
{currentImageIndex < imageNodes.length - 1 && (
|
||||
{currentImageIndex < images.length - 1 && (
|
||||
<Nav dir="right" $hidden={isIdle} animation={animation.current}>
|
||||
<NavButton onClick={next} size={32} aria-label={t("Next")}>
|
||||
<NextIcon size={32} />
|
||||
@@ -787,7 +813,7 @@ const Nav = styled.div<{
|
||||
: ""}
|
||||
`;
|
||||
|
||||
const StyledError = styled(Error)<{
|
||||
const StyledError = styled(ImageError)<{
|
||||
animation: Animation | null;
|
||||
}>`
|
||||
${(props) =>
|
||||
|
||||
+26
-8
@@ -54,6 +54,12 @@ import EditorContext from "./components/EditorContext";
|
||||
import { NodeViewRenderer } from "./components/NodeViewRenderer";
|
||||
import SelectionToolbar from "./components/SelectionToolbar";
|
||||
import WithTheme from "./components/WithTheme";
|
||||
import isNull from "lodash/isNull";
|
||||
import { map } from "lodash";
|
||||
import {
|
||||
LightboxImage,
|
||||
LightboxImageFactory,
|
||||
} from "@shared/editor/lib/Lightbox";
|
||||
import Lightbox from "~/components/Lightbox";
|
||||
|
||||
export type Props = {
|
||||
@@ -146,8 +152,8 @@ type State = {
|
||||
isEditorFocused: boolean;
|
||||
/** If the toolbar for a text selection is visible */
|
||||
selectionToolbarOpen: boolean;
|
||||
/** Position of image in doc that's being currently viewed in Lightbox */
|
||||
activeLightboxImgPos: number | null;
|
||||
/** Image that's being currently viewed in Lightbox */
|
||||
activeLightboxImage: LightboxImage | null;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -177,7 +183,7 @@ export class Editor extends React.PureComponent<
|
||||
isRTL: false,
|
||||
isEditorFocused: false,
|
||||
selectionToolbarOpen: false,
|
||||
activeLightboxImgPos: null,
|
||||
activeLightboxImage: null,
|
||||
};
|
||||
|
||||
isInitialized = false;
|
||||
@@ -640,6 +646,16 @@ export class Editor extends React.PureComponent<
|
||||
*/
|
||||
public getImages = () => ProsemirrorHelper.getImages(this.view.state.doc);
|
||||
|
||||
public getLightboxImages = (): LightboxImage[] => {
|
||||
const lightboxNodes = ProsemirrorHelper.getLightboxNodes(
|
||||
this.view.state.doc
|
||||
);
|
||||
|
||||
return map(lightboxNodes, (node) =>
|
||||
LightboxImageFactory.createLightboxImage(this.view, node.pos)
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Return the tasks/checkmarks in the current editor.
|
||||
*
|
||||
@@ -717,10 +733,10 @@ export class Editor extends React.PureComponent<
|
||||
dispatch(tr);
|
||||
};
|
||||
|
||||
public updateActiveLightbox = (pos: number | null) => {
|
||||
public updateActiveLightboxImage = (activeImage: LightboxImage | null) => {
|
||||
this.setState((state) => ({
|
||||
...state,
|
||||
activeLightboxImgPos: pos,
|
||||
activeLightboxImage: activeImage,
|
||||
}));
|
||||
};
|
||||
|
||||
@@ -843,10 +859,12 @@ export class Editor extends React.PureComponent<
|
||||
)}
|
||||
</Observer>
|
||||
</Flex>
|
||||
{this.state.activeLightboxImgPos && (
|
||||
{!isNull(this.state.activeLightboxImage) && (
|
||||
<Lightbox
|
||||
onUpdate={this.updateActiveLightbox}
|
||||
activePos={this.state.activeLightboxImgPos}
|
||||
images={this.getLightboxImages()}
|
||||
activeImage={this.state.activeLightboxImage}
|
||||
onUpdate={this.updateActiveLightboxImage}
|
||||
onClose={() => this.view.focus()}
|
||||
/>
|
||||
)}
|
||||
</EditorContext.Provider>
|
||||
|
||||
@@ -1387,6 +1387,10 @@ mark {
|
||||
`
|
||||
}
|
||||
|
||||
&:is(.code-active) + .mermaid-diagram-wrapper {
|
||||
cursor: zoom-in;
|
||||
}
|
||||
|
||||
// Hide code without display none so toolbar can still be positioned against it
|
||||
&:not(.code-active) {
|
||||
height: ${props.staticHTML || props.readOnly ? "auto" : "0"};
|
||||
@@ -1402,6 +1406,9 @@ mark {
|
||||
height: 0;
|
||||
overflow: hidden;
|
||||
margin: -0.5em 0 0 0;
|
||||
& + .mermaid-diagram-wrapper {
|
||||
cursor: zoom-in;
|
||||
}
|
||||
}
|
||||
|
||||
.code-block.with-line-numbers {
|
||||
|
||||
@@ -15,6 +15,8 @@ import { isCode } from "../lib/isCode";
|
||||
import { isRemoteTransaction } from "../lib/multiplayer";
|
||||
import { findBlockNodes } from "../queries/findChildren";
|
||||
import { NodeWithPos } from "../types";
|
||||
import type { Editor } from "../../../app/editor";
|
||||
import { LightboxImageFactory } from "../lib/Lightbox";
|
||||
|
||||
type MermaidState = {
|
||||
decorationSet: DecorationSet;
|
||||
@@ -49,14 +51,16 @@ class MermaidRenderer {
|
||||
readonly diagramId: string;
|
||||
readonly element: HTMLElement;
|
||||
readonly elementId: string;
|
||||
readonly editor: Editor;
|
||||
|
||||
constructor() {
|
||||
constructor(editor: Editor) {
|
||||
this.diagramId = uuidv4();
|
||||
this.elementId = `mermaid-diagram-wrapper-${this.diagramId}`;
|
||||
this.element =
|
||||
document.getElementById(this.elementId) || document.createElement("div");
|
||||
this.element.id = this.elementId;
|
||||
this.element.classList.add("mermaid-diagram-wrapper");
|
||||
this.editor = editor;
|
||||
}
|
||||
|
||||
renderImmediately = async (
|
||||
@@ -90,7 +94,7 @@ class MermaidRenderer {
|
||||
// the future if Mermaid is able to handle this automatically.
|
||||
gantt: { useWidth: 700 },
|
||||
pie: { useWidth: 700 },
|
||||
fontFamily: "inherit",
|
||||
fontFamily: getComputedStyle(this.element).fontFamily || "inherit",
|
||||
theme: isDark ? "dark" : "default",
|
||||
darkMode: isDark,
|
||||
});
|
||||
@@ -175,10 +179,12 @@ function getNewState({
|
||||
doc,
|
||||
name,
|
||||
pluginState,
|
||||
editor,
|
||||
}: {
|
||||
doc: Node;
|
||||
name: string;
|
||||
pluginState: MermaidState;
|
||||
editor: Editor;
|
||||
}): MermaidState {
|
||||
const decorations: Decoration[] = [];
|
||||
|
||||
@@ -201,7 +207,7 @@ function getNewState({
|
||||
);
|
||||
|
||||
const renderer: MermaidRenderer =
|
||||
bestDecoration?.spec?.renderer ?? new MermaidRenderer();
|
||||
bestDecoration?.spec?.renderer ?? new MermaidRenderer(editor);
|
||||
|
||||
const diagramDecoration = Decoration.widget(
|
||||
block.pos + block.node.nodeSize,
|
||||
@@ -239,9 +245,11 @@ function getNewState({
|
||||
export default function Mermaid({
|
||||
name,
|
||||
isDark,
|
||||
editor,
|
||||
}: {
|
||||
name: string;
|
||||
isDark: boolean;
|
||||
editor: Editor;
|
||||
}) {
|
||||
return new Plugin({
|
||||
key: new PluginKey("mermaid"),
|
||||
@@ -255,6 +263,7 @@ export default function Mermaid({
|
||||
doc,
|
||||
name,
|
||||
pluginState,
|
||||
editor,
|
||||
});
|
||||
},
|
||||
apply: (
|
||||
@@ -285,6 +294,7 @@ export default function Mermaid({
|
||||
doc: transaction.doc,
|
||||
name,
|
||||
pluginState,
|
||||
editor,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -306,7 +316,7 @@ export default function Mermaid({
|
||||
return this.getState(state)?.decorationSet;
|
||||
},
|
||||
handleDOMEvents: {
|
||||
mousedown(view, event) {
|
||||
mouseup(view, event) {
|
||||
const target = event.target as HTMLElement;
|
||||
const diagram = target?.closest(".mermaid-diagram-wrapper");
|
||||
const codeBlock = diagram?.previousElementSibling;
|
||||
@@ -320,8 +330,20 @@ export default function Mermaid({
|
||||
return false;
|
||||
}
|
||||
|
||||
// select node
|
||||
if (diagram && event.detail === 1) {
|
||||
const { selection: textSelection } = view.state;
|
||||
const $pos = view.state.doc.resolve(pos);
|
||||
const selected =
|
||||
textSelection.from >= $pos.start() &&
|
||||
textSelection.to <= $pos.end();
|
||||
if (selected || editor.props.readOnly) {
|
||||
editor.updateActiveLightboxImage(
|
||||
LightboxImageFactory.createLightboxImage(view, $pos.before())
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
// select node
|
||||
view.dispatch(
|
||||
view.state.tr
|
||||
.setSelection(TextSelection.near(view.state.doc.resolve(pos)))
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
import { Node } from "prosemirror-model";
|
||||
import { isCode } from "./isCode";
|
||||
import { EditorView } from "prosemirror-view";
|
||||
import { sanitizeUrl } from "@shared/utils/urls";
|
||||
|
||||
export abstract class LightboxImage {
|
||||
protected pos: number;
|
||||
protected element: Element;
|
||||
protected src: string;
|
||||
protected alt: string;
|
||||
|
||||
constructor() {}
|
||||
|
||||
public abstract getElement(): Element | null | undefined;
|
||||
public abstract getSrc(): string;
|
||||
public abstract getAlt(): string;
|
||||
public abstract getPos(): number;
|
||||
}
|
||||
|
||||
class LightboxRegularImage extends LightboxImage {
|
||||
constructor(view: EditorView, pos: number) {
|
||||
super();
|
||||
this.pos = pos;
|
||||
const node = view.state.doc.nodeAt(pos);
|
||||
this.src = sanitizeUrl(node?.attrs.src) ?? "";
|
||||
this.alt = node?.attrs.alt ?? "";
|
||||
this.element = view.nodeDOM(pos) as HTMLSpanElement;
|
||||
}
|
||||
|
||||
getElement() {
|
||||
return this.element.querySelector("img");
|
||||
}
|
||||
|
||||
getSrc() {
|
||||
return this.src;
|
||||
}
|
||||
|
||||
getAlt() {
|
||||
return this.alt;
|
||||
}
|
||||
|
||||
getPos() {
|
||||
return this.pos;
|
||||
}
|
||||
}
|
||||
|
||||
class LightboxMermaidImage extends LightboxImage {
|
||||
constructor(view: EditorView, pos: number) {
|
||||
super();
|
||||
this.element = view.nodeDOM(pos) as HTMLDivElement;
|
||||
this.pos = pos;
|
||||
this.src = this.svgToSrc(this.extractSvg());
|
||||
this.alt = "";
|
||||
}
|
||||
|
||||
private svgToSrc(svg: string): string {
|
||||
return `data:image/svg+xml,${encodeURIComponent(svg)}`;
|
||||
}
|
||||
|
||||
private extractSvg(): string {
|
||||
const mermaidWrapper = this.element.nextElementSibling;
|
||||
if (!mermaidWrapper) {
|
||||
return "";
|
||||
}
|
||||
const svg = mermaidWrapper.firstElementChild;
|
||||
if (!svg || !(svg instanceof SVGElement)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return svg.outerHTML;
|
||||
}
|
||||
|
||||
getElement() {
|
||||
return this.element.nextElementSibling?.firstElementChild;
|
||||
}
|
||||
|
||||
getSrc() {
|
||||
return this.src;
|
||||
}
|
||||
|
||||
getAlt() {
|
||||
return this.alt;
|
||||
}
|
||||
|
||||
getPos() {
|
||||
return this.pos;
|
||||
}
|
||||
}
|
||||
|
||||
export class LightboxImageFactory {
|
||||
static createLightboxImage(view: EditorView, pos: number): LightboxImage {
|
||||
const node = view.state.doc.nodeAt(pos)!;
|
||||
if (isImage(node)) {
|
||||
return new LightboxRegularImage(view, pos);
|
||||
}
|
||||
|
||||
if (isMermaid(node)) {
|
||||
return new LightboxMermaidImage(view, pos);
|
||||
}
|
||||
|
||||
throw new Error("Unsupported node type for LightboxImage");
|
||||
}
|
||||
}
|
||||
|
||||
const isImage = (node: Node) => node.type.name === "image";
|
||||
const isMermaid = (node: Node) =>
|
||||
isCode(node) && node.attrs.language === "mermaidjs";
|
||||
|
||||
export const isLightboxNode = (node: Node) => isImage(node) || isMermaid(node);
|
||||
@@ -193,6 +193,7 @@ export default class CodeFence extends Node {
|
||||
Mermaid({
|
||||
name: this.name,
|
||||
isDark: this.editor.props.theme.isDark,
|
||||
editor: this.editor,
|
||||
}),
|
||||
new Plugin({
|
||||
key: new PluginKey("code-fence-split"),
|
||||
|
||||
@@ -15,6 +15,7 @@ import { MarkdownSerializerState } from "../lib/markdown/serializer";
|
||||
import { EditorStyleHelper } from "../styles/EditorStyleHelper";
|
||||
import { ComponentProps } from "../types";
|
||||
import SimpleImage from "./SimpleImage";
|
||||
import { LightboxImageFactory } from "../lib/Lightbox";
|
||||
|
||||
const imageSizeRegex = /\s=(\d+)?x(\d+)?$/;
|
||||
|
||||
@@ -322,9 +323,11 @@ export default class Image extends SimpleImage {
|
||||
};
|
||||
|
||||
handleClick =
|
||||
({ getPos }: ComponentProps) =>
|
||||
({ getPos, view }: ComponentProps) =>
|
||||
() => {
|
||||
this.editor.updateActiveLightbox(getPos());
|
||||
this.editor.updateActiveLightboxImage(
|
||||
LightboxImageFactory.createLightboxImage(view, getPos())
|
||||
);
|
||||
};
|
||||
|
||||
component = (props: ComponentProps) => {
|
||||
|
||||
@@ -14,6 +14,7 @@ import uploadPlaceholderPlugin from "../lib/uploadPlaceholder";
|
||||
import { UploadPlugin } from "../plugins/UploadPlugin";
|
||||
import { ComponentProps } from "../types";
|
||||
import Node from "./Node";
|
||||
import { LightboxImageFactory } from "../lib/Lightbox";
|
||||
|
||||
export default class SimpleImage extends Node {
|
||||
options: Options;
|
||||
@@ -77,9 +78,11 @@ export default class SimpleImage extends Node {
|
||||
}
|
||||
|
||||
handleClick =
|
||||
({ getPos }: ComponentProps) =>
|
||||
({ view, getPos }: ComponentProps) =>
|
||||
() => {
|
||||
this.editor.updateActiveLightbox(getPos());
|
||||
this.editor.updateActiveLightboxImage(
|
||||
LightboxImageFactory.createLightboxImage(view, getPos())
|
||||
);
|
||||
};
|
||||
|
||||
component = (props: ComponentProps) => (
|
||||
|
||||
@@ -319,6 +319,7 @@
|
||||
"Permission": "Permission",
|
||||
"Change Language": "Change Language",
|
||||
"Dismiss": "Dismiss",
|
||||
"Unable to download image": "Unable to download image",
|
||||
"Lightbox": "Lightbox",
|
||||
"View, navigate, or download images in the document": "View, navigate, or download images in the document",
|
||||
"Close": "Close",
|
||||
|
||||
@@ -4,6 +4,8 @@ import textBetween from "../editor/lib/textBetween";
|
||||
import { ProsemirrorData } from "../types";
|
||||
import { TextHelper } from "./TextHelper";
|
||||
import env from "../env";
|
||||
import { findChildren } from "@shared/editor/queries/findChildren";
|
||||
import { isLightboxNode } from "@shared/editor/lib/Lightbox";
|
||||
|
||||
export type Heading = {
|
||||
/* The heading in plain text */
|
||||
@@ -229,6 +231,15 @@ export class ProsemirrorHelper {
|
||||
return images;
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates through the document to find all valid Lightbox nodes.
|
||||
*
|
||||
* @param doc Prosemirror document node
|
||||
* @returns Array<NodeWithPos> of nodes allowed in Lightbox
|
||||
*/
|
||||
static getLightboxNodes = (doc: Node) =>
|
||||
findChildren(doc, isLightboxNode, true);
|
||||
|
||||
/**
|
||||
* Iterates through the document to find all of the videos.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user