import { useBlockProps, useInnerBlocksProps, store, InnerBlocks, InspectorControls, } from "@wordpress/block-editor"; import { BlockEditProps, BlockInstance, registerBlockType, } from "@wordpress/blocks"; import metadata from "./block.json"; import "./style.scss"; import "./editor.scss"; import { useSelect, useDispatch } from "@wordpress/data"; import { createBlock } from "@wordpress/blocks"; import { Button, __experimentalConfirmDialog as ConfirmDialog, TextControl, } from "@wordpress/components"; import { positionLeft, positionRight, positionCenter, stretchFullWidth, reset, plus, } from "@wordpress/icons"; import { useState } from "react"; import { SpacingControlSingle } from "@tableberg/components"; import { __experimentalToggleGroupControl as ToggleGroupControl, __experimentalToggleGroupControlOptionIcon as ToggleGroupControlOptionIcon, __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem, __experimentalNumberControl as NumberControl, } from "@wordpress/components"; import { Icon } from "@wordpress/icons"; import { __ } from "@wordpress/i18n"; import { ColorControl } from "@tableberg/components"; import { getSpacingCssSingle } from "@tableberg/shared/utils/styling-helpers"; import blockIcon from "@tableberg/shared/icons/tableberg"; export interface ToggleBlockTypes { tabs: Array; defaultActiveTabIndex: number; alignment: string; gap: string; tabType: string; activeTabTextColor: string; activeTabBackgroundColor: string; inactiveTabTextColor: string; inactiveTabBackgroundColor: string; tabBorderRadius: string; } interface AlignmentControls { icon: JSX.Element; title: string; value: string; } const alignmentOptions: Array = [ { icon: positionLeft, title: "Align left", value: "left", }, { icon: positionCenter, title: "Align center", value: "center", }, { icon: positionRight, title: "Align right", value: "right", }, { icon: stretchFullWidth, title: "Stretch full width", value: "full", }, ]; function edit({ clientId, attributes, setAttributes, isSelected, }: BlockEditProps) { const blockProps = useBlockProps(); const { children, ...innerBlocksProps } = useInnerBlocksProps(blockProps, { allowedBlocks: ["tableberg/table"], template: [ ["tableberg/table"], ["tableberg/table"], ["tableberg/table"], ], // @ts-ignore renderAppender: false, }); const { tabs, alignment, defaultActiveTabIndex, gap, activeTabTextColor, inactiveTabTextColor, activeTabBackgroundColor, inactiveTabBackgroundColor, tabBorderRadius, } = attributes; const [activeTab, setActiveTab] = useState(defaultActiveTabIndex); const { innerBlocks, innerBlocksLength } = useSelect( select => { const innerBlocks = ( select(store) as BlockEditorStoreSelectors ).getBlock(clientId)?.innerBlocks; return { innerBlocks, innerBlocksLength: innerBlocks?.length, }; }, [clientId] ); const [deleteConfirmDialogIsOpen, setDeleteConfirmDialogIsOpen] = useState(false); const [deleteIndex, setDeleteIndex] = useState(0); const { insertBlock, removeBlock } = useDispatch( store ) as unknown as BlockEditorStoreActions; function tabAdditionHandler() { insertBlock( createBlock("tableberg/table"), innerBlocksLength, clientId, false ); setActiveTab(innerBlocksLength!); setAttributes({ tabs: [...tabs, "Untitled Tab"], }); } function removeTabHandler(i: number) { if (!innerBlocks || !innerBlocksLength) { return; } removeBlock(innerBlocks[i].clientId, false); const newTabs = [...tabs.slice(0, i), ...tabs.slice(i + 1)]; setActiveTab(0); setAttributes({ tabs: newTabs, }); } function cssTemplate({ clientId }: BlockInstance, i: number) { return ` #block-${clientId} { display: ${activeTab === i ? "block" : "none"}; }`; } const SidebarControls = ( <> {}} > true} > { const newTabs = [...tabs]; newTabs[activeTab] = val; setAttributes({ tabs: newTabs, }); }} /> setAttributes({ defaultActiveTabIndex: Math.max( 0, Number(newVal || 1) - 1 ), }) } min={1} max={tabs.length} /> true} > setAttributes({ alignment: newAlignment, }) } > {alignmentOptions.map(({ icon, title, value }) => ( ))} setAttributes({ activeTabTextColor: newColor, }) } onDeselect={() => setAttributes({ activeTabTextColor: "", }) } /> setAttributes({ inactiveTabTextColor: newColor, }) } onDeselect={() => setAttributes({ inactiveTabTextColor: "", }) } /> setAttributes({ activeTabBackgroundColor: newColor, }) } onDeselect={() => setAttributes({ activeTabBackgroundColor: "", }) } /> setAttributes({ inactiveTabBackgroundColor: newColor, }) } onDeselect={() => setAttributes({ inactiveTabBackgroundColor: "", }) } /> true} > setAttributes({ gap: newGap, }) } /> true} > setAttributes({ tabBorderRadius: newValue, }) } /> ); return (
{SidebarControls}
{children}
); } function ToggleTab({ title, active = false, styles, onClick, onDelete, }: { title: string; active: boolean; onClick: () => void; onDelete: () => void; styles: { activeTabBackgroundColor: string; inactiveTabBackgroundColor: string; activeTabTextColor: string; inactiveTabTextColor: string; tabBorderRadius: string; }; }) { const activeStyles = { backgroundColor: styles.activeTabBackgroundColor, borderRadius: getSpacingCssSingle(styles.tabBorderRadius), color: styles.activeTabTextColor, }; const inactiveStyles = { backgroundColor: styles.inactiveTabBackgroundColor, borderRadius: getSpacingCssSingle(styles.tabBorderRadius), color: styles.inactiveTabTextColor, }; return (

{title}

); } function save() { const blockProps = useBlockProps.save(); return (
); } registerBlockType(metadata as any, { attributes: metadata.attributes as any, icon: blockIcon, edit, save, });