@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
198 lines (197 loc) • 6.91 kB
JavaScript
// packages/editor/src/components/post-revisions-preview/revisions-canvas.js
import clsx from "clsx";
import { Spinner } from "@wordpress/components";
import { privateApis as blockEditorPrivateApis } from "@wordpress/block-editor";
import { useSelect } from "@wordpress/data";
import { useContext, useEffect } from "@wordpress/element";
import { addFilter } from "@wordpress/hooks";
import { store as blocksStore } from "@wordpress/blocks";
import { sprintf, __ } from "@wordpress/i18n";
import { VisuallyHidden } from "@wordpress/ui";
import { unlock } from "../../lock-unlock.mjs";
import { store as editorStore } from "../../store/index.mjs";
import VisualEditor from "../visual-editor/index.mjs";
import {
registerDiffFormatTypes,
unregisterDiffFormatTypes,
DIFF_DESCRIPTION_IDS
} from "./diff-format-types.mjs";
import { useDiffMarkers } from "./diff-markers.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var { usePrivateStyleOverride, PrivateBlockContext } = unlock(
blockEditorPrivateApis
);
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 sprintf(__("Added block: %s"), blockTitle);
case "removed":
return sprintf(__("Removed block: %s"), blockTitle);
case "modified":
return sprintf(__("Modified block: %s"), blockTitle);
}
}
function BlockDiffLabelProvider({ status, name, attributes, children }) {
const context = useContext(PrivateBlockContext);
const blockTitle = useSelect(
(select) => {
const { getActiveBlockVariation, getBlockType } = select(blocksStore);
return getActiveBlockVariation(name, attributes)?.title ?? getBlockType(name)?.title;
},
[name, attributes]
);
return /* @__PURE__ */ 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 = clsx(className, {
"is-revision-added": diffStatus === "added",
"is-revision-removed": diffStatus === "removed",
"is-revision-modified": diffStatus === "modified"
});
if (!diffStatus) {
return /* @__PURE__ */ jsx(BlockListBlock, { ...props, className: enhancedClassName });
}
return /* @__PURE__ */ jsx(
BlockDiffLabelProvider,
{
status: diffStatus,
name,
attributes,
children: /* @__PURE__ */ jsx(BlockListBlock, { ...props, className: enhancedClassName })
}
);
};
}
var FILTER_NAME = "editor/revisions-canvas/withRevisionDiffClasses";
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__ */ jsxs(VisuallyHidden, { children: [
/* @__PURE__ */ jsx("span", { id: DIFF_DESCRIPTION_IDS.removed, children: __("Removed") }),
/* @__PURE__ */ jsx("span", { id: DIFF_DESCRIPTION_IDS.added, children: __("Added") }),
/* @__PURE__ */ jsx("span", { id: DIFF_DESCRIPTION_IDS.formatAdded, children: __("Format added") }),
/* @__PURE__ */ jsx("span", { id: DIFF_DESCRIPTION_IDS.formatRemoved, children: __("Format removed") }),
/* @__PURE__ */ jsx("span", { id: DIFF_DESCRIPTION_IDS.formatChanged, children: __("Format changed") })
] });
}
function CanvasContent({ showDiff }) {
const [contentRef, diffMarkers] = useDiffMarkers();
return /* @__PURE__ */ jsxs(Fragment, { children: [
showDiff && /* @__PURE__ */ jsx(DiffDescriptions, {}),
/* @__PURE__ */ jsx(VisualEditor, { contentRef }),
showDiff && diffMarkers
] });
}
function RevisionsCanvas() {
useEffect(() => {
registerDiffFormatTypes();
return () => {
unregisterDiffFormatTypes();
};
}, []);
const { revision, showDiff } = useSelect((select) => {
const { getCurrentRevision, isShowingRevisionDiff } = unlock(
select(editorStore)
);
return {
revision: getCurrentRevision(),
showDiff: isShowingRevisionDiff()
};
}, []);
return revision ? /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsx(DiffStyleOverrides, { showDiff }),
/* @__PURE__ */ jsx("div", { className: "editor-revisions-canvas__content", children: /* @__PURE__ */ jsx(CanvasContent, { showDiff }) })
] }) : /* @__PURE__ */ jsx("div", { className: "editor-revisions-canvas__loading", children: /* @__PURE__ */ jsx(Spinner, {}) });
}
export {
RevisionsCanvas as default
};
//# sourceMappingURL=revisions-canvas.mjs.map