diff --git a/app/editor/components/MediaDimension.tsx b/app/editor/components/MediaDimension.tsx index d10bba1731..cc02eb2222 100644 --- a/app/editor/components/MediaDimension.tsx +++ b/app/editor/components/MediaDimension.tsx @@ -174,6 +174,11 @@ export function MediaDimension() { width: finalWidth, height: finalHeight, }); + } else if (nodeType === "video") { + commands["resizeVideo"]({ + width: finalWidth, + height: finalHeight, + }); } }, [ commands, diff --git a/app/editor/components/MediaLinkEditor.tsx b/app/editor/components/MediaLinkEditor.tsx index cb1af7fcab..76ae8c1965 100644 --- a/app/editor/components/MediaLinkEditor.tsx +++ b/app/editor/components/MediaLinkEditor.tsx @@ -34,10 +34,10 @@ export function MediaLinkEditor({ const [localUrl, setLocalUrl] = useState(url); const wrapperRef = useRef(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 ( setLocalUrl(e.target.value)} @@ -123,7 +123,11 @@ export function MediaLinkEditor({ {view.editable && ( diff --git a/app/editor/components/SelectionToolbar.tsx b/app/editor/components/SelectionToolbar.tsx index 9594bf1dce..d6cbfb9775 100644 --- a/app/editor/components/SelectionToolbar.tsx +++ b/app/editor/components/SelectionToolbar.tsx @@ -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); }; diff --git a/app/editor/extensions/SelectionToolbar.tsx b/app/editor/extensions/SelectionToolbar.tsx index 4ed57b3490..d11f7ca05e 100644 --- a/app/editor/extensions/SelectionToolbar.tsx +++ b/app/editor/extensions/SelectionToolbar.tsx @@ -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; } diff --git a/app/editor/menus/video.tsx b/app/editor/menus/video.tsx new file mode 100644 index 0000000000..892fac7ffe --- /dev/null +++ b/app/editor/menus/video.tsx @@ -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: , + }, + { + tooltip: t("Replace video"), + icon: , + children: [ + { + name: "replaceVideo", + label: t("Upload a video"), + }, + { + name: "editVideoUrl", + label: t("Edit video URL"), + }, + ], + }, + { + name: "deleteVideo", + tooltip: t("Delete video"), + icon: , + }, + ]; +} diff --git a/shared/editor/commands/insertFiles.ts b/shared/editor/commands/insertFiles.ts index 8c95d429b8..18e2672e2e 100644 --- a/shared/editor/commands/insertFiles.ts +++ b/shared/editor/commands/insertFiles.ts @@ -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 */ diff --git a/shared/editor/nodes/Video.tsx b/shared/editor/nodes/Video.tsx index 4bf016840f..1002198e51 100644 --- a/shared/editor/nodes/Video.tsx +++ b/shared/editor/nodes/Video.tsx @@ -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) => toggleWrap(type, attrs); + return { + video: (attrs: Record) => 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) { diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index 79933121cd..c152c38d27 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -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",