| 1 |
/** |
| 2 |
* The AI Actions half of the block, as the editor sees it. |
| 3 |
* |
| 4 |
* Two sources, and the split matters: |
| 5 |
* |
| 6 |
* - `AI_ACTION_TOGGLES` is the fixed map from block attribute to action id. It |
| 7 |
* has to be spelled out because the attributes are declared statically in |
| 8 |
* `attributes.js` and mirrored in `Blocks\ReadingTime::AI_ACTIONS`; all three |
| 9 |
* lists have to agree. |
| 10 |
* - `catalog()` is the live registry, handed over by |
| 11 |
* `Editors\BlockEditor::ai_actions_catalog()`. Labels, descriptions, icons and |
| 12 |
* order come from there, so the preview matches the frontend exactly and an |
| 13 |
* entry a third party added through `betterdocs_ai_actions` still shows up. |
| 14 |
*/ |
| 15 |
|
| 16 |
import { __ } from "@wordpress/i18n"; |
| 17 |
import { settingFlag } from "./settingDefaults"; |
| 18 |
|
| 19 |
/** |
| 20 |
* Block attribute => registry id, in registry priority order. |
| 21 |
* |
| 22 |
* The labels are the inspector's, not the registry's: a toggle reads better in |
| 23 |
* title case beside the other controls, while the menu keeps the registry's own |
| 24 |
* sentence-case wording. |
| 25 |
*/ |
| 26 |
export const AI_ACTION_TOGGLES = [ |
| 27 |
{ attribute: "aiCopyPage", id: "copy-page", label: __("Copy Page", "betterdocs") }, |
| 28 |
{ attribute: "aiViewMarkdown", id: "view-markdown", label: __("View as Markdown", "betterdocs") }, |
| 29 |
{ attribute: "aiChatGPT", id: "open-chatgpt", label: __("Open in ChatGPT", "betterdocs") }, |
| 30 |
{ attribute: "aiClaude", id: "open-claude", label: __("Open in Claude", "betterdocs") }, |
| 31 |
{ attribute: "aiGemini", id: "open-aistudio", label: __("Open in Google AI Studio", "betterdocs") }, |
| 32 |
{ attribute: "aiPerplexity", id: "open-perplexity", label: __("Open in Perplexity", "betterdocs") }, |
| 33 |
{ attribute: "aiGrok", id: "open-grok", label: __("Open in Grok", "betterdocs") }, |
| 34 |
]; |
| 35 |
|
| 36 |
const payload = |
| 37 |
(typeof window !== "undefined" && |
| 38 |
window.betterDocsBlocksHelper && |
| 39 |
window.betterDocsBlocksHelper.aiActions) || |
| 40 |
{}; |
| 41 |
|
| 42 |
/** |
| 43 |
* The disclosure arrow, as rendered by Core\AIActions::icon('caret'). |
| 44 |
* |
| 45 |
* @return {string} SVG markup, or "" when the payload is missing. |
| 46 |
*/ |
| 47 |
export const caretMarkup = () => payload.caret || ""; |
| 48 |
|
| 49 |
/** |
| 50 |
* The registry entries this instance should show, in order. |
| 51 |
* |
| 52 |
* An entry with a block attribute follows that attribute — which itself defaults |
| 53 |
* to the Settings panel, so an untouched control still tracks the site-wide |
| 54 |
* value. An entry without one (a third-party addition) follows its `setting_key`, |
| 55 |
* which is what Core\AIActions::action_enabled() does server-side. |
| 56 |
* |
| 57 |
* @param {Object} attributes |
| 58 |
* @return {Object[]} |
| 59 |
*/ |
| 60 |
export const enabledActions = (attributes) => |
| 61 |
(payload.items || []).filter((action) => { |
| 62 |
const toggle = AI_ACTION_TOGGLES.find(({ id }) => id === action.id); |
| 63 |
|
| 64 |
if (toggle) { |
| 65 |
return !!attributes[toggle.attribute]; |
| 66 |
} |
| 67 |
|
| 68 |
// No setting key at all means "always on", same as the server. |
| 69 |
return action.setting_key ? settingFlag(action.setting_key) : true; |
| 70 |
}); |
| 71 |
|