UNPKG

@wordpress/editor

Version:
225 lines (223 loc) 9.51 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); // packages/editor/src/components/post-revisions-preview/revisions-canvas.js var revisions_canvas_exports = {}; __export(revisions_canvas_exports, { default: () => RevisionsCanvas }); module.exports = __toCommonJS(revisions_canvas_exports); var import_clsx = __toESM(require("clsx")); var import_components = require("@wordpress/components"); var import_block_editor = require("@wordpress/block-editor"); var import_data = require("@wordpress/data"); var import_element = require("@wordpress/element"); var import_hooks = require("@wordpress/hooks"); var import_blocks = require("@wordpress/blocks"); var import_i18n = require("@wordpress/i18n"); var import_ui = require("@wordpress/ui"); var import_lock_unlock = require("../../lock-unlock.cjs"); var import_store = require("../../store/index.cjs"); var import_visual_editor = __toESM(require("../visual-editor/index.cjs")); var import_diff_format_types = require("./diff-format-types.cjs"); var import_diff_markers = require("./diff-markers.cjs"); var import_jsx_runtime = require("react/jsx-runtime"); var { usePrivateStyleOverride, PrivateBlockContext } = (0, import_lock_unlock.unlock)( import_block_editor.privateApis ); var REVISION_REMOVED_FILTER_SVG = ` <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 0 0" width="0" height="0" focusable="false" role="none" aria-hidden="true" style="visibility: hidden; position: absolute; left: -9999px; overflow: hidden;" > <defs> <filter id="revision-removed-filter" x="0" y="0" width="100%" height="100%"> <!-- Desaturate and add red tint --> <feColorMatrix type="matrix" values="0.5 0.3 0.2 0 0.15 0.2 0.2 0.1 0 0 0.2 0.2 0.1 0 0 0 0 0 0.8 0"/> </filter> </defs> </svg> `; var REVISION_DIFF_STYLES = ` .is-revision-added { box-shadow: inset 0 0 0 9999px color-mix(in srgb, currentColor 5%, #00a32a 15%), 0 0 0 4px color-mix(in srgb, currentColor 5%, #00a32a 15%); outline: 3px solid #00a32a; outline-offset: 2px; } .is-revision-removed, .revision-diff-removed { text-decoration: line-through; filter: url(#revision-removed-filter); } .is-revision-removed { outline: 3px dashed #d63638; outline-offset: 2px; } .is-revision-modified { outline: 3px dotted #9a7000 !important; outline-offset: 2px; } .revision-diff-added { background-color: color-mix(in srgb, currentColor 5%, #00a32a 15%); text-decoration: none; } /* Reset UA <mark> styles so format markers keep the same look as before. */ mark.revision-diff-format-added, mark.revision-diff-format-removed, mark.revision-diff-format-changed { background: transparent; color: inherit; padding: 0; } .revision-diff-format-added { text-decoration: underline wavy color-mix(in srgb, currentColor 30%, #00a32a 70%); text-decoration-thickness: 2px; } .revision-diff-format-removed { text-decoration: underline wavy color-mix(in srgb, currentColor 20%, #d63638 80%); text-decoration-thickness: 2px; } .revision-diff-format-changed { text-decoration: underline wavy color-mix(in srgb, currentColor 30%, #dba617 70%); text-decoration-thickness: 2px; } `; function getDiffStatusLabel(status, blockTitle) { switch (status) { case "added": return (0, import_i18n.sprintf)((0, import_i18n.__)("Added block: %s"), blockTitle); case "removed": return (0, import_i18n.sprintf)((0, import_i18n.__)("Removed block: %s"), blockTitle); case "modified": return (0, import_i18n.sprintf)((0, import_i18n.__)("Modified block: %s"), blockTitle); } } function BlockDiffLabelProvider({ status, name, attributes, children }) { const context = (0, import_element.useContext)(PrivateBlockContext); const blockTitle = (0, import_data.useSelect)( (select) => { const { getActiveBlockVariation, getBlockType } = select(import_blocks.store); return getActiveBlockVariation(name, attributes)?.title ?? getBlockType(name)?.title; }, [name, attributes] ); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( PrivateBlockContext.Provider, { value: { ...context, ariaLabel: blockTitle ? getDiffStatusLabel(status, blockTitle) : void 0 }, children } ); } function withRevisionDiffClasses(BlockListBlock) { return function WithRevisionDiffClasses(props) { const { block, className, name, attributes } = props; const diffStatus = block?.__revisionDiffStatus?.status; const enhancedClassName = (0, import_clsx.default)(className, { "is-revision-added": diffStatus === "added", "is-revision-removed": diffStatus === "removed", "is-revision-modified": diffStatus === "modified" }); if (!diffStatus) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(BlockListBlock, { ...props, className: enhancedClassName }); } return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( BlockDiffLabelProvider, { status: diffStatus, name, attributes, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(BlockListBlock, { ...props, className: enhancedClassName }) } ); }; } var FILTER_NAME = "editor/revisions-canvas/withRevisionDiffClasses"; (0, import_hooks.addFilter)("editor.BlockListBlock", FILTER_NAME, withRevisionDiffClasses); function DiffStyleOverrides({ showDiff }) { usePrivateStyleOverride({ css: showDiff ? REVISION_DIFF_STYLES : "" }); usePrivateStyleOverride({ assets: showDiff ? REVISION_REMOVED_FILTER_SVG : "", __unstableType: "svgs" }); return null; } function DiffDescriptions() { return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ui.VisuallyHidden, { children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { id: import_diff_format_types.DIFF_DESCRIPTION_IDS.removed, children: (0, import_i18n.__)("Removed") }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { id: import_diff_format_types.DIFF_DESCRIPTION_IDS.added, children: (0, import_i18n.__)("Added") }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { id: import_diff_format_types.DIFF_DESCRIPTION_IDS.formatAdded, children: (0, import_i18n.__)("Format added") }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { id: import_diff_format_types.DIFF_DESCRIPTION_IDS.formatRemoved, children: (0, import_i18n.__)("Format removed") }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { id: import_diff_format_types.DIFF_DESCRIPTION_IDS.formatChanged, children: (0, import_i18n.__)("Format changed") }) ] }); } function CanvasContent({ showDiff }) { const [contentRef, diffMarkers] = (0, import_diff_markers.useDiffMarkers)(); return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ showDiff && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(DiffDescriptions, {}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_visual_editor.default, { contentRef }), showDiff && diffMarkers ] }); } function RevisionsCanvas() { (0, import_element.useEffect)(() => { (0, import_diff_format_types.registerDiffFormatTypes)(); return () => { (0, import_diff_format_types.unregisterDiffFormatTypes)(); }; }, []); const { revision, showDiff } = (0, import_data.useSelect)((select) => { const { getCurrentRevision, isShowingRevisionDiff } = (0, import_lock_unlock.unlock)( select(import_store.store) ); return { revision: getCurrentRevision(), showDiff: isShowingRevisionDiff() }; }, []); return revision ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(DiffStyleOverrides, { showDiff }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "editor-revisions-canvas__content", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CanvasContent, { showDiff }) }) ] }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "editor-revisions-canvas__loading", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Spinner, {}) }); } //# sourceMappingURL=revisions-canvas.cjs.map