@wordpress/block-library
Version:
Block library for the WordPress editor.
240 lines (239 loc) • 9.79 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// \0rolldown/runtime.js
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 __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
let clsx = require("clsx");
clsx = __toESM(clsx);
let _wordpress_blob = require("@wordpress/blob");
let _wordpress_components = require("@wordpress/components");
let _wordpress_data = require("@wordpress/data");
let _wordpress_block_editor = require("@wordpress/block-editor");
let _wordpress_element = require("@wordpress/element");
let _wordpress_compose = require("@wordpress/compose");
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_icons = require("@wordpress/icons");
let _wordpress_core_data = require("@wordpress/core-data");
let _wordpress_notices = require("@wordpress/notices");
let _wordpress_url = require("@wordpress/url");
let __inspector_jsx = require("./inspector.cjs");
__inspector_jsx = __toESM(__inspector_jsx);
let __utils_index_js = require("./utils/index.cjs");
let ___utils_remove_anchor_tag_js = require("../utils/remove-anchor-tag.cjs");
___utils_remove_anchor_tag_js = __toESM(___utils_remove_anchor_tag_js);
let ___utils_hooks_js = require("../utils/hooks.cjs");
let react_jsx_runtime = require("react/jsx-runtime");
// packages/block-library/src/file/edit.jsx
const MIN_PREVIEW_HEIGHT = 200;
const MAX_PREVIEW_HEIGHT = 2e3;
function ClipboardToolbarButton({ text, disabled }) {
const { createNotice } = (0, _wordpress_data.useDispatch)(_wordpress_notices.store);
const ref = (0, _wordpress_compose.useCopyToClipboard)(text, () => {
createNotice("info", (0, _wordpress_i18n.__)("Copied URL to clipboard."), {
isDismissible: true,
type: "snackbar"
});
});
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToolbarButton, {
className: "components-clipboard-toolbar-button",
ref,
disabled,
children: (0, _wordpress_i18n.__)("Copy URL")
});
}
function FileEdit({ attributes, isSelected, setAttributes, clientId }) {
const { id, fileName, href, textLinkHref, textLinkTarget, showDownloadButton, downloadButtonText, displayPreview, previewHeight } = attributes;
const [temporaryURL, setTemporaryURL] = (0, _wordpress_element.useState)(attributes.blob);
const { media } = (0, _wordpress_data.useSelect)((select) => ({ media: !!id ? select(_wordpress_core_data.store).getEntityRecord("postType", "attachment", id, { context: "view" }) : void 0 }), [id]);
const { createErrorNotice } = (0, _wordpress_data.useDispatch)(_wordpress_notices.store);
const { toggleSelection } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store);
(0, ___utils_hooks_js.useUploadMediaFromBlobURL)({
url: temporaryURL,
onChange: onSelectFile,
onError: onUploadError
});
function onSelectFile(newMedia) {
if (!newMedia || !newMedia.url) {
setAttributes({
href: void 0,
fileName: void 0,
textLinkHref: void 0,
id: void 0,
fileId: void 0,
displayPreview: void 0,
previewHeight: void 0
});
setTemporaryURL();
return;
}
if ((0, _wordpress_blob.isBlobURL)(newMedia.url)) {
setTemporaryURL(newMedia.url);
return;
}
const isPdf = (newMedia.mime || newMedia.mime_type) === "application/pdf" || (0, _wordpress_url.getFilename)(newMedia.url).toLowerCase().endsWith(".pdf");
const pdfAttributes = {
displayPreview: isPdf ? attributes.displayPreview ?? true : void 0,
previewHeight: isPdf ? attributes.previewHeight ?? 600 : void 0
};
setAttributes({
href: newMedia.url,
fileName: newMedia.title,
textLinkHref: newMedia.url,
id: newMedia.id,
fileId: `wp-block-file--media-${clientId}`,
blob: void 0,
...pdfAttributes
});
setTemporaryURL();
}
function onUploadError(message) {
setAttributes({ href: void 0 });
createErrorNotice(message, { type: "snackbar" });
}
function changeLinkDestinationOption(newHref) {
setAttributes({ textLinkHref: newHref });
}
function changeOpenInNewWindow(newValue) {
setAttributes({ textLinkTarget: newValue ? "_blank" : false });
}
function changeShowDownloadButton(newValue) {
setAttributes({ showDownloadButton: newValue });
}
function changeDisplayPreview(newValue) {
setAttributes({ displayPreview: newValue });
}
function handleOnResizeStop(event, direction, elt, delta) {
toggleSelection(true);
setAttributes({ previewHeight: parseInt(previewHeight + delta.height, 10) });
}
function changePreviewHeight(newValue) {
setAttributes({ previewHeight: Math.max(parseInt(newValue, 10), 200) });
}
const attachmentPage = media && media.link;
const blockProps = (0, _wordpress_block_editor.useBlockProps)({ className: (0, clsx.default)(!!temporaryURL && (0, _wordpress_components.__unstableGetAnimateClassName)({ type: "loading" }), { "is-transient": !!temporaryURL }) });
const displayPreviewInEditor = (0, __utils_index_js.browserSupportsPdfs)() && displayPreview;
if (!href && !temporaryURL) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
...blockProps,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.MediaPlaceholder, {
icon: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockIcon, { icon: _wordpress_icons.file }),
labels: {
title: (0, _wordpress_i18n.__)("File"),
instructions: (0, _wordpress_i18n.__)("Drag and drop a file, upload, or choose from your library.")
},
onSelect: onSelectFile,
onError: onUploadError,
accept: "*"
})
});
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(__inspector_jsx.default, {
hrefs: {
href: href || temporaryURL,
textLinkHref,
attachmentPage
},
openInNewWindow: !!textLinkTarget,
showDownloadButton,
changeLinkDestinationOption,
changeOpenInNewWindow,
changeShowDownloadButton,
displayPreview,
changeDisplayPreview,
previewHeight,
changePreviewHeight
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_block_editor.BlockControls, {
group: "other",
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.MediaReplaceFlow, {
mediaId: id,
mediaURL: href,
accept: "*",
onSelect: onSelectFile,
onError: onUploadError,
onReset: () => onSelectFile(void 0)
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ClipboardToolbarButton, {
text: href,
disabled: (0, _wordpress_blob.isBlobURL)(href)
})]
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
...blockProps,
children: [displayPreviewInEditor && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.ResizableBox, {
size: {
height: previewHeight,
width: "100%"
},
minHeight: 200,
maxHeight: 2e3,
grid: [1, 10],
enable: {
top: false,
right: false,
bottom: true,
left: false,
topRight: false,
bottomRight: false,
bottomLeft: false,
topLeft: false
},
onResizeStart: () => toggleSelection(false),
onResizeStop: handleOnResizeStop,
showHandle: isSelected,
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("object", {
className: "wp-block-file__preview",
data: href,
type: "application/pdf",
"aria-label": (0, _wordpress_i18n.__)("Embed of the selected PDF file.")
}), !isSelected && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: "wp-block-file__preview-overlay" })]
}), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
className: "wp-block-file__content-wrapper",
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.RichText, {
identifier: "fileName",
tagName: "a",
value: fileName,
placeholder: (0, _wordpress_i18n.__)("Write file name…"),
withoutInteractiveFormatting: true,
onChange: (text) => setAttributes({ fileName: (0, ___utils_remove_anchor_tag_js.default)(text) }),
href: textLinkHref
}), showDownloadButton && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: "wp-block-file__button-richtext-wrapper",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.RichText, {
identifier: "downloadButtonText",
tagName: "div",
"aria-label": (0, _wordpress_i18n.__)("Download button text"),
className: (0, clsx.default)("wp-block-file__button", (0, _wordpress_block_editor.__experimentalGetElementClassName)("button")),
value: downloadButtonText,
withoutInteractiveFormatting: true,
placeholder: (0, _wordpress_i18n.__)("Add text…"),
onChange: (text) => setAttributes({ downloadButtonText: (0, ___utils_remove_anchor_tag_js.default)(text) })
})
})]
})]
})
] });
}
exports.MAX_PREVIEW_HEIGHT = MAX_PREVIEW_HEIGHT;
exports.MIN_PREVIEW_HEIGHT = MIN_PREVIEW_HEIGHT;
exports.default = FileEdit;
//# sourceMappingURL=edit.cjs.map