UNPKG

tldraw

Version:

A tiny little drawing editor.

178 lines (177 loc) • 7.78 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var VideoShapeUtil_exports = {}; __export(VideoShapeUtil_exports, { VideoShapeUtil: () => VideoShapeUtil }); module.exports = __toCommonJS(VideoShapeUtil_exports); var import_jsx_runtime = require("react/jsx-runtime"); var import_editor = require("@tldraw/editor"); var import_classnames = __toESM(require("classnames"), 1); var import_react = require("react"); var import_BrokenAssetIcon = require("../shared/BrokenAssetIcon"); var import_HyperlinkButton = require("../shared/HyperlinkButton"); var import_useImageOrVideoAsset = require("../shared/useImageOrVideoAsset"); var import_usePrefersReducedMotion = require("../shared/usePrefersReducedMotion"); const videoSvgExportCache = new import_editor.WeakCache(); class VideoShapeUtil extends import_editor.BaseBoxShapeUtil { static type = "video"; static props = import_editor.videoShapeProps; static migrations = import_editor.videoShapeMigrations; options = { autoplay: true }; canEdit() { return true; } isAspectRatioLocked() { return true; } getDefaultProps() { return { w: 100, h: 100, assetId: null, autoplay: this.options.autoplay, url: "", altText: "", // Not used, but once upon a time were used to sync video state between users time: 0, playing: true }; } getAriaDescriptor(shape) { return shape.props.altText; } component(shape) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(VideoShape, { shape }); } indicator(shape) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("rect", { width: (0, import_editor.toDomPrecision)(shape.props.w), height: (0, import_editor.toDomPrecision)(shape.props.h) }); } useLegacyIndicator() { return false; } getIndicatorPath(shape) { const path = new Path2D(); path.rect(0, 0, shape.props.w, shape.props.h); return path; } async toSvg(shape, ctx) { const props = shape.props; if (!props.assetId) return null; const asset = this.editor.getAsset(props.assetId); if (!asset) return null; const src = await videoSvgExportCache.get(asset, async () => { const assetUrl = await ctx.resolveAssetUrl(asset.id, props.w); if (!assetUrl) return null; const video = await import_editor.MediaHelpers.loadVideo(assetUrl); return await import_editor.MediaHelpers.getVideoFrameAsDataUrl(video, 0); }); if (!src) return null; return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("image", { href: src, width: props.w, height: props.h, "aria-label": shape.props.altText }); } } const VideoShape = (0, import_react.memo)(function VideoShape2({ shape }) { const editor = (0, import_editor.useEditor)(); const showControls = editor.getShapeGeometry(shape).bounds.w * editor.getEfficientZoomLevel() >= 110; const isEditing = (0, import_editor.useIsEditing)(shape.id); const prefersReducedMotion = (0, import_usePrefersReducedMotion.usePrefersReducedMotion)(); const { Spinner } = (0, import_editor.useEditorComponents)(); const { asset, url } = (0, import_useImageOrVideoAsset.useImageOrVideoAsset)({ shapeId: shape.id, assetId: shape.props.assetId, width: shape.props.w }); const rVideo = (0, import_react.useRef)(null); const [isLoaded, setIsLoaded] = (0, import_react.useState)(false); const handleLoadedData = (0, import_react.useCallback)((e) => { const video = e.currentTarget; if (!video) return; setIsLoaded(true); }, []); const [isFullscreen, setIsFullscreen] = (0, import_react.useState)(false); (0, import_react.useEffect)(() => { const fullscreenChange = () => setIsFullscreen(document.fullscreenElement === rVideo.current); document.addEventListener("fullscreenchange", fullscreenChange); return () => document.removeEventListener("fullscreenchange", fullscreenChange); }); (0, import_react.useEffect)(() => { const video = rVideo.current; if (!video) return; if (isEditing) { if (document.activeElement !== video) { video.focus(); } } }, [isEditing, isLoaded]); return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_editor.HTMLContainer, { id: shape.id, style: { color: "var(--tl-color-text-3)", backgroundColor: asset ? "transparent" : "var(--tl-color-low)", border: asset ? "none" : "1px solid var(--tl-color-low-border)" }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "tl-counter-scaled", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "tl-video-container", children: !asset ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_BrokenAssetIcon.BrokenAssetIcon, {}) : Spinner && !asset.props.src ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Spinner, {}) : url ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( "video", { ref: rVideo, style: isEditing ? { pointerEvents: "all" } : !isLoaded ? { display: "none" } : void 0, className: (0, import_classnames.default)("tl-video", `tl-video-shape-${shape.id.split(":")[1]}`, { "tl-video-is-fullscreen": isFullscreen }), width: "100%", height: "100%", draggable: false, playsInline: true, autoPlay: shape.props.autoplay && !prefersReducedMotion, muted: true, loop: true, disableRemotePlayback: true, disablePictureInPicture: true, controls: isEditing && showControls, onLoadedData: handleLoadedData, hidden: !isLoaded, "aria-label": shape.props.altText, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("source", { src: url }) }, url ), !isLoaded && Spinner && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Spinner, {}) ] }) : null }) }) } ), "url" in shape.props && shape.props.url && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_HyperlinkButton.HyperlinkButton, { url: shape.props.url }) ] }); }); //# sourceMappingURL=VideoShapeUtil.js.map