mirror of
https://github.com/outline/outline.git
synced 2026-11-29 16:40:27 +03:00
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
This commit is contained in:
1 parent
128c240405
commit
178e923cb7
8 files changed
+209
-11
No files matched your search
@@ -174,6 +174,11 @@ export function MediaDimension() {
|
||||
width: finalWidth,
|
||||
height: finalHeight,
|
||||
});
|
||||
} else if (nodeType === "video") {
|
||||
commands["resizeVideo"]({
|
||||
width: finalWidth,
|
||||
height: finalHeight,
|
||||
});
|
||||
}
|
||||
}, [
|
||||
commands,
|
||||
|
||||
@@ -34,10 +34,10 @@ export function MediaLinkEditor({
|
||||
const [localUrl, setLocalUrl] = useState(url);
|
||||
const wrapperRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
// If we're attempting to edit an image, autofocus the input
|
||||
// Not doing for embed type because it made the editor scroll to top
|
||||
// unexpectedly–leaving that out for now
|
||||
const isEditingImgUrl = node?.type.name === "image";
|
||||
// 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;
|
||||
@@ -64,7 +64,7 @@ export function MediaLinkEditor({
|
||||
|
||||
const update = useCallback(() => {
|
||||
const { state } = view;
|
||||
const hrefType = node?.type.name === "image" ? "src" : "href";
|
||||
const hrefType = isSrcNode ? "src" : "href";
|
||||
const tr = state.tr.setNodeMarkup(state.selection.from, undefined, {
|
||||
...node?.attrs,
|
||||
[hrefType]: localUrl,
|
||||
@@ -73,7 +73,7 @@ export function MediaLinkEditor({
|
||||
view.dispatch(tr);
|
||||
moveSelectionToEnd();
|
||||
onLinkUpdate();
|
||||
}, [localUrl, node, view, moveSelectionToEnd, onLinkUpdate]);
|
||||
}, [localUrl, node, isSrcNode, view, moveSelectionToEnd, onLinkUpdate]);
|
||||
|
||||
useOnClickOutside(wrapperRef, onClickOutside);
|
||||
|
||||
@@ -108,7 +108,7 @@ export function MediaLinkEditor({
|
||||
return (
|
||||
<Wrapper ref={wrapperRef}>
|
||||
<Input
|
||||
autoFocus={isEditingImgUrl}
|
||||
autoFocus={isSrcNode}
|
||||
value={localUrl}
|
||||
placeholder={`${t("Paste a link")}…`}
|
||||
onChange={(e) => setLocalUrl(e.target.value)}
|
||||
@@ -123,7 +123,11 @@ export function MediaLinkEditor({
|
||||
{view.editable && (
|
||||
<Tooltip
|
||||
content={
|
||||
node.type.name === "embed" ? t("Delete embed") : t("Delete image")
|
||||
node.type.name === "embed"
|
||||
? t("Delete embed")
|
||||
: node.type.name === "video"
|
||||
? t("Delete video")
|
||||
: t("Delete image")
|
||||
}
|
||||
>
|
||||
<ToolbarButton onClick={remove}>
|
||||
|
||||
@@ -256,7 +256,7 @@ export const SelectionToolbar = observer(function SelectionToolbar(
|
||||
items = items.map((item) => {
|
||||
if (item.children && Array.isArray(item.children)) {
|
||||
item.children = item.children.map((child) => {
|
||||
if (child.name === "editImageUrl") {
|
||||
if (child.name === "editImageUrl" || child.name === "editVideoUrl") {
|
||||
child.onClick = () => {
|
||||
setActiveToolbar(Toolbar.Media);
|
||||
};
|
||||
|
||||
@@ -22,6 +22,7 @@ import getReadOnlyMenuItems from "../menus/readOnly";
|
||||
import getTableMenuItems from "../menus/table";
|
||||
import getTableColMenuItems from "../menus/tableCol";
|
||||
import getTableRowMenuItems from "../menus/tableRow";
|
||||
import getVideoMenuItems from "../menus/video";
|
||||
|
||||
export default class SelectionToolbarExtension extends Extension {
|
||||
get name() {
|
||||
@@ -106,6 +107,11 @@ export default class SelectionToolbarExtension extends Extension {
|
||||
matches: (ctx) => ctx.selectedNodeType === "attachment",
|
||||
getItems: (ctx) => getAttachmentMenuItems(ctx),
|
||||
},
|
||||
{
|
||||
priority: 50,
|
||||
matches: (ctx) => ctx.selectedNodeType === "video",
|
||||
getItems: (ctx) => getVideoMenuItems(ctx),
|
||||
},
|
||||
{
|
||||
priority: 30,
|
||||
matches: (ctx) => ctx.readOnly,
|
||||
@@ -163,7 +169,9 @@ export default class SelectionToolbarExtension extends Extension {
|
||||
|
||||
if (
|
||||
selection instanceof NodeSelection &&
|
||||
["image", "attachment", "embed"].includes(selection.node.type.name)
|
||||
["image", "attachment", "embed", "video"].includes(
|
||||
selection.node.type.name
|
||||
)
|
||||
) {
|
||||
return selection;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import { t } from "i18next";
|
||||
import { TrashIcon, DownloadIcon, ReplaceIcon } from "outline-icons";
|
||||
import { NodeSelection } from "prosemirror-state";
|
||||
import type { MenuItem, SelectionContext } from "@shared/editor/types";
|
||||
|
||||
/**
|
||||
* Returns menu items for the video selection toolbar.
|
||||
*
|
||||
* @param ctx - the current selection context.
|
||||
* @returns an array of menu items.
|
||||
*/
|
||||
export default function videoMenuItems(ctx: SelectionContext): MenuItem[] {
|
||||
if (ctx.readOnly) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const { selection } = ctx.state;
|
||||
const hasWidth =
|
||||
selection instanceof NodeSelection && !!selection.node.attrs.width;
|
||||
|
||||
return [
|
||||
{
|
||||
name: "dimensions",
|
||||
tooltip: `${t("Width")} × ${t("Height")}`,
|
||||
visible: hasWidth,
|
||||
skipIcon: true,
|
||||
},
|
||||
{
|
||||
name: "separator",
|
||||
},
|
||||
{
|
||||
name: "downloadVideo",
|
||||
tooltip: t("Download video"),
|
||||
icon: <DownloadIcon />,
|
||||
},
|
||||
{
|
||||
tooltip: t("Replace video"),
|
||||
icon: <ReplaceIcon />,
|
||||
children: [
|
||||
{
|
||||
name: "replaceVideo",
|
||||
label: t("Upload a video"),
|
||||
},
|
||||
{
|
||||
name: "editVideoUrl",
|
||||
label: t("Edit video URL"),
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "deleteVideo",
|
||||
tooltip: t("Delete video"),
|
||||
icon: <TrashIcon />,
|
||||
},
|
||||
];
|
||||
}
|
||||
@@ -38,6 +38,8 @@ export type Options = {
|
||||
height?: number;
|
||||
/** Alt text / caption to use when inserting image */
|
||||
alt?: string | null;
|
||||
/** Caption to use when inserting video */
|
||||
title?: string | null;
|
||||
/** Layout class for alignment when inserting image */
|
||||
layoutClass?: string | null;
|
||||
/** Whether to show a PDF preview embed for attachment nodes */
|
||||
|
||||
@@ -5,10 +5,13 @@ import type {
|
||||
NodeType,
|
||||
Node as ProsemirrorNode,
|
||||
} from "prosemirror-model";
|
||||
import type { Command } from "prosemirror-state";
|
||||
import { NodeSelection, TextSelection } from "prosemirror-state";
|
||||
import * as React from "react";
|
||||
import type { Primitive } from "utility-types";
|
||||
import { getEventFiles } from "../../utils/files";
|
||||
import { sanitizeUrl } from "../../utils/urls";
|
||||
import insertFiles from "../commands/insertFiles";
|
||||
import toggleWrap from "../commands/toggleWrap";
|
||||
import Caption from "../components/Caption";
|
||||
import VideoComponent from "../components/Video";
|
||||
@@ -22,6 +25,42 @@ const parseDimension = (value: string | null): number | null => {
|
||||
return Number.isFinite(parsed) ? parsed : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Downloads a video through a blob URL, as videos are served inline and a
|
||||
* plain link would open them for playback.
|
||||
*
|
||||
* @param src the sanitized video URL.
|
||||
* @param title the video caption, used as the file name.
|
||||
*/
|
||||
const downloadVideo = async (src: string, title: string | null) => {
|
||||
try {
|
||||
const response = await fetch(src);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to download video: ${response.status}`);
|
||||
}
|
||||
const blob = await response.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const extension = blob.type.split("/")[1];
|
||||
const name = title || "video";
|
||||
|
||||
// create a temporary link node and click it with our video data
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download =
|
||||
extension && !name.endsWith(`.${extension}`)
|
||||
? `${name}.${extension}`
|
||||
: name;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
|
||||
// cleanup
|
||||
document.body.removeChild(link);
|
||||
setTimeout(() => URL.revokeObjectURL(url));
|
||||
} catch {
|
||||
window.open(src, "_blank", "noopener,noreferrer");
|
||||
}
|
||||
};
|
||||
|
||||
export default class Video extends Node {
|
||||
get name() {
|
||||
return "video";
|
||||
@@ -225,7 +264,86 @@ export default class Video extends Node {
|
||||
);
|
||||
|
||||
commands({ type }: { type: NodeType }) {
|
||||
return (attrs: Record<string, Primitive>) => toggleWrap(type, attrs);
|
||||
return {
|
||||
video: (attrs: Record<string, Primitive>) => toggleWrap(type, attrs),
|
||||
deleteVideo: (): Command => (state, dispatch) => {
|
||||
dispatch?.(state.tr.deleteSelection());
|
||||
return true;
|
||||
},
|
||||
replaceVideo: (): Command => (state) => {
|
||||
if (
|
||||
!(state.selection instanceof NodeSelection) ||
|
||||
state.selection.node.type !== type
|
||||
) {
|
||||
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 videos");
|
||||
}
|
||||
|
||||
// create an input element and click to trigger picker
|
||||
const inputElement = document.createElement("input");
|
||||
inputElement.type = "file";
|
||||
inputElement.accept = "video/*";
|
||||
inputElement.onchange = (event) => {
|
||||
const files = getEventFiles(event);
|
||||
void insertFiles(view, event, state.selection.from, files, {
|
||||
uploadFile,
|
||||
onFileUploadStart,
|
||||
onFileUploadStop,
|
||||
onNotice,
|
||||
replaceExisting: true,
|
||||
attrs: node.attrs.title ? { title: node.attrs.title } : undefined,
|
||||
});
|
||||
};
|
||||
inputElement.click();
|
||||
return true;
|
||||
},
|
||||
downloadVideo: (): Command => (state) => {
|
||||
if (
|
||||
!(state.selection instanceof NodeSelection) ||
|
||||
state.selection.node.type !== type
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
const { node } = state.selection;
|
||||
const src = sanitizeUrl(node.attrs.src);
|
||||
if (!src) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void downloadVideo(src, node.attrs.title);
|
||||
return true;
|
||||
},
|
||||
resizeVideo:
|
||||
({ width, height }: { width: number; height: number }): Command =>
|
||||
(state, dispatch) => {
|
||||
if (
|
||||
!(state.selection instanceof NodeSelection) ||
|
||||
state.selection.node.type !== type
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const { selection } = state;
|
||||
const tr = state.tr
|
||||
.setNodeMarkup(selection.from, undefined, {
|
||||
...selection.node.attrs,
|
||||
width,
|
||||
height,
|
||||
})
|
||||
.setMeta("addToHistory", true);
|
||||
|
||||
const $pos = tr.doc.resolve(selection.from);
|
||||
dispatch?.(tr.setSelection(new NodeSelection($pos)));
|
||||
return true;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
toMarkdown(state: MarkdownSerializerState, node: ProsemirrorNode) {
|
||||
|
||||
@@ -776,6 +776,7 @@
|
||||
"Height": "Height",
|
||||
"Paste a link": "Paste a link",
|
||||
"Delete embed": "Delete embed",
|
||||
"Delete video": "Delete video",
|
||||
"Delete image": "Delete image",
|
||||
"{{ userName }} won’t be notified, as they do not have access to this document": "{{ userName }} won’t be notified, as they do not have access to this document",
|
||||
"Members of “{{ groupName }}” that have access to this document will be notified": "Members of “{{ groupName }}” that have access to this document will be notified",
|
||||
@@ -846,6 +847,10 @@
|
||||
"Insert before": "Insert before",
|
||||
"Move left": "Move left",
|
||||
"Move right": "Move right",
|
||||
"Download video": "Download video",
|
||||
"Replace video": "Replace video",
|
||||
"Upload a video": "Upload a video",
|
||||
"Edit video URL": "Edit video URL",
|
||||
"Could not import file": "Could not import file",
|
||||
"Unsubscribed from document": "Unsubscribed from document",
|
||||
"Unsubscribed from collection": "Unsubscribed from collection",
|
||||
|
||||
Reference in new issue
Block a user