@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
178 lines (175 loc) • 6.59 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/editor/src/components/error-boundary/index.js
var error_boundary_exports = {};
__export(error_boundary_exports, {
default: () => error_boundary_default
});
module.exports = __toCommonJS(error_boundary_exports);
var import_element = require("@wordpress/element");
var import_i18n = require("@wordpress/i18n");
var import_ui = require("@wordpress/ui");
var import_data = require("@wordpress/data");
var import_compose = require("@wordpress/compose");
var import_hooks = require("@wordpress/hooks");
var import_store = require("../../store/index.cjs");
var import_jsx_runtime = require("react/jsx-runtime");
function getContent() {
try {
return (0, import_data.select)(import_store.store).getEditedPostContent();
} catch {
}
}
function getErrorName(error) {
return error instanceof Error && error.name || "Error";
}
function getErrorMessage(error) {
if (typeof error === "string" && error) {
return error;
}
if (typeof error?.message === "string" && error.message) {
return error.message;
}
return "An unknown error occurred.";
}
function getErrorSections(error, componentStack) {
const sections = [
{ label: getErrorName(error), content: getErrorMessage(error) }
];
if (error?.stack) {
sections.push({
label: "Stack",
content: error.stack.trim(),
preformatted: true
});
}
if (componentStack) {
sections.push({
label: "Component stack",
content: componentStack.trim(),
preformatted: true
});
}
sections.push({
label: "Environment",
content: `User agent: ${window.navigator.userAgent}`,
preformatted: true
});
return sections;
}
function getErrorReport(error, componentStack) {
const sections = getErrorSections(error, componentStack).map(
({ label, content, preformatted }) => `**${label}**
${preformatted ? `\`\`\`
${content}
\`\`\`` : content}`
);
return ["### Error report", ...sections].join("\n\n");
}
function CopyButton({ text, children, variant = "outline" }) {
const ref = (0, import_compose.useCopyToClipboard)(text);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.Notice.ActionButton, { variant, ref, children });
}
function ErrorReport({ error, componentStack }) {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_ui.Stack,
{
className: "editor-error-boundary__report",
direction: "column",
gap: "md",
children: getErrorSections(error, componentStack).map(
({ label, content }) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ui.Stack, { direction: "column", gap: "xs", children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.Text, { variant: "heading-md", children: label }),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("pre", { className: "editor-error-boundary__report-section", children: content })
] }, label)
)
}
);
}
function ErrorDetails({ error, componentStack }) {
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ui.CollapsibleCard.Root, { className: "editor-error-boundary__details", children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.CollapsibleCard.Header, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.Card.Title, { children: (0, import_i18n.__)("Error details") }) }),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.CollapsibleCard.Content, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
ErrorReport,
{
error,
componentStack
}
) })
] });
}
var ErrorBoundary = class extends import_element.Component {
constructor() {
super(...arguments);
this.state = {
error: null,
componentStack: null
};
}
componentDidCatch(error, errorInfo) {
this.setState({ componentStack: errorInfo?.componentStack });
(0, import_hooks.doAction)("editor.ErrorBoundary.errorLogged", error, errorInfo);
}
static getDerivedStateFromError(error) {
return { error };
}
render() {
const { error, componentStack } = this.state;
const { canCopyContent = false } = this.props;
if (!error) {
return this.props.children;
}
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
import_ui.Stack,
{
className: "editor-error-boundary",
direction: "column",
gap: "lg",
children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ui.Notice.Root, { intent: "error", children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.Notice.Title, { children: (0, import_i18n.__)("The editor has crashed") }),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.Notice.Description, { children: (0, import_i18n.__)(
"An unknown error occurred. Reload your browser to try again, or copy the error to report the problem or search."
) }),
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ui.Notice.Actions, { children: [
canCopyContent && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CopyButton, { text: getContent, children: (0, import_i18n.__)("Copy contents") }),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
CopyButton,
{
variant: "solid",
text: () => getErrorReport(error, componentStack),
children: (0, import_i18n.__)("Copy error")
}
)
] })
] }),
globalThis.SCRIPT_DEBUG ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
ErrorDetails,
{
error,
componentStack
}
) : null
]
}
);
}
};
var error_boundary_default = ErrorBoundary;
//# sourceMappingURL=index.cjs.map