import { __ } from "@wordpress/i18n";
import { createHigherOrderComponent } from "@wordpress/compose";
import { addFilter } from "@wordpress/hooks";
import { BlockControls } from "@wordpress/block-editor";
import { ToolbarGroup, ToolbarButton } from "@wordpress/components";
import { dispatch, select } from "@wordpress/data";
import { serialize } from "@wordpress/blocks";
import { Fragment } from "@wordpress/element";
import { STORE_KEY } from "./store";
const SUPPORTED_BLOCKS = [
"core/paragraph",
"core/heading",
"core/list",
"core/list-item",
"core/quote",
"core/pullquote",
"core/preformatted",
"core/verse",
"core/code",
"core/details",
"core/group",
"core/columns",
"core/column",
];
const aiIcon = (
);
const isSupported = (name) => SUPPORTED_BLOCKS.indexOf(name) !== -1;
const withAIBlockControl = createHigherOrderComponent((BlockEdit) => {
return (props) => {
const { name, isSelected, clientId } = props;
if (!isSelected || !isSupported(name)) {
return ;
}
const onClick = () => {
const block = select("core/block-editor").getBlock(clientId);
if (!block) {
return;
}
const html = serialize([block]);
const tmp = document.createElement("div");
tmp.innerHTML = html;
const plain = (tmp.textContent || tmp.innerText || "")
.replace(/\r\n?/g, "\n")
.replace(/[ \t]+/g, " ")
.replace(/[ \t]*\n[ \t]*/g, "\n")
.replace(/\n{2,}/g, "\n\n")
.trim();
dispatch(STORE_KEY).openModal({
selectionType: "block",
selectionText: plain,
selectionHtml: html,
blockClientId: clientId,
inlineContext: null,
});
};
return (
);
};
}, "withAIBlockControl");
addFilter("editor.BlockEdit", "betterdocs/ai-edit-block-control", withAIBlockControl);