Files
outline/app/editor/components/MediaLinkEditor.tsx
T
Tom Moor 178e923cb7 feat: Add selection toolbar for video nodes (#14118)
* feat: Add selection toolbar for video nodes

* fix: Keep video caption on replace, download video as a file

* fix: Drop removed upload progress prop from video replace
2026-10-17 12:21:46 -04:00

148 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { OpenIcon, TrashIcon } from "outline-icons";
import type { Node } from "prosemirror-model";
import { Selection, TextSelection } from "prosemirror-state";
import type { EditorView } from "prosemirror-view";
import { useCallback, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import styled from "styled-components";
import Flex from "~/components/Flex";
import Tooltip from "~/components/Tooltip";
import Input from "~/editor/components/Input";
import ToolbarButton from "./ToolbarButton";
import useOnClickOutside from "~/hooks/useOnClickOutside";
type Props = {
node?: Node;
view: EditorView;
autoFocus?: boolean;
onLinkUpdate: () => void;
onLinkRemove: () => void;
onEscape: () => void;
onClickOutside: (ev: MouseEvent | TouchEvent) => void;
};
export function MediaLinkEditor({
node,
view,
onLinkUpdate,
onLinkRemove,
onEscape,
onClickOutside,
}: Props) {
const { t } = useTranslation();
const url = (node?.attrs.href ?? node?.attrs.src) as string;
const [localUrl, setLocalUrl] = useState(url);
const wrapperRef = useRef<HTMLDivElement>(null);
// Image and video nodes store their URL in `src` and autofocus the input.
// Embeds use `href` and do not autofocus, as it made the editor scroll to
// top unexpectedly–leaving that out for now
const isSrcNode = node?.type.name === "image" || node?.type.name === "video";
const moveSelectionToEnd = useCallback(() => {
const { state, dispatch } = view;
const nextSelection = Selection.findFrom(
state.tr.doc.resolve(state.selection.from),
1,
true
);
const selection = nextSelection ?? TextSelection.create(state.tr.doc, 0);
dispatch(state.tr.setSelection(selection));
view.focus();
}, [view]);
const openLink = useCallback(() => {
window.open(url, "_blank");
}, [url]);
const remove = useCallback(() => {
const { state, dispatch } = view;
dispatch(state.tr.deleteSelection());
onLinkRemove();
}, [view, onLinkRemove]);
const update = useCallback(() => {
const { state } = view;
const hrefType = isSrcNode ? "src" : "href";
const tr = state.tr.setNodeMarkup(state.selection.from, undefined, {
...node?.attrs,
[hrefType]: localUrl,
});
view.dispatch(tr);
moveSelectionToEnd();
onLinkUpdate();
}, [localUrl, node, isSrcNode, view, moveSelectionToEnd, onLinkUpdate]);
useOnClickOutside(wrapperRef, onClickOutside);
const handleKeyDown = useCallback(
(event: React.KeyboardEvent<HTMLInputElement>) => {
if (event.nativeEvent.isComposing) {
return;
}
switch (event.key) {
case "Enter": {
event.preventDefault();
update();
return;
}
case "Escape": {
event.preventDefault();
moveSelectionToEnd();
onEscape();
return;
}
}
},
[update, moveSelectionToEnd, onEscape]
);
if (!node) {
return null;
}
return (
<Wrapper ref={wrapperRef}>
<Input
autoFocus={isSrcNode}
value={localUrl}
placeholder={`${t("Paste a link")}…`}
onChange={(e) => setLocalUrl(e.target.value)}
onKeyDown={handleKeyDown}
readOnly={!view.editable}
/>
<Tooltip content={t("Open link")}>
<ToolbarButton onClick={openLink} disabled={!localUrl}>
<OpenIcon />
</ToolbarButton>
</Tooltip>
{view.editable && (
<Tooltip
content={
node.type.name === "embed"
? t("Delete embed")
: node.type.name === "video"
? t("Delete video")
: t("Delete image")
}
>
<ToolbarButton onClick={remove}>
<TrashIcon />
</ToolbarButton>
</Tooltip>
)}
</Wrapper>
);
}
const Wrapper = styled(Flex)`
pointer-events: all;
gap: 6px;
padding: 6px;
min-width: 350px;
`;