import { BlockSettingsMenuControls, RichText, useBlockProps, } from "@wordpress/block-editor"; import { MenuItem } from "@wordpress/components"; import { BlockEditProps, createBlock, parse, registerBlockType, } from "@wordpress/blocks"; import { __ } from "@wordpress/i18n"; import { paragraph } from "@wordpress/icons"; import { mergeAttrsWithDefaultsAndApplyBindings } from "@tableberg/shared/utils/merge-attrs-with-defaults-and-apply-bindings"; import { TextElementAttributes, sidesToShorthand, textAttributeDefaults, } from "./element"; import metadata from "./block.json"; import { TextElementControls } from "./controls"; import { ElementBindings } from "../../dynamic-data/types"; import { CellElement } from "../../attributes"; import { applyElementStyleClipboardPayload, parseElementStyleClipboardPayload, readClipboardText, serializeElementStyleClipboardPayload, writeClipboardText, } from "../../hooks/block-editor-compat/elementClipboard"; import { ElementAlignmentToolbar, alignmentWrapperStyle, } from "../element-edit-common"; function TextEdit({ attributes, setAttributes, isSelected, onReplace, onRemove, insertBlocksAfter, clientId, }: BlockEditProps & { onReplace?: (blocks: unknown[]) => void; onRemove?: () => void; insertBlocksAfter?: (blocks: unknown[]) => void; }) { const merged = mergeAttrsWithDefaultsAndApplyBindings( attributes, textAttributeDefaults, undefined, {}, __("(No data)", "tableberg") ); const { content, align, styles } = merged; const textElement = { name: "text", attributes: merged, } as CellElement; const pasteStyles = async () => { const payload = parseElementStyleClipboardPayload( await readClipboardText() ); if (!payload) { return; } const styledElement = applyElementStyleClipboardPayload( textElement, payload ); const styledAttributes = styledElement.attributes as TextElementAttributes; setAttributes({ align: styledAttributes.align, styles: styledAttributes.styles, }); }; const blockProps = useBlockProps({ style: alignmentWrapperStyle(align), }); const paddingValue = sidesToShorthand(styles.padding); const marginValue = sidesToShorthand(styles.margin); const textStyle: Record = { "margin": marginValue ?? 0, "padding": paddingValue, "color": styles.textColor || undefined, "fontSize": styles.fontSize || undefined, "backgroundColor": styles.backgroundColor || undefined, "--tableberg-text-link-color": styles.linkColor || undefined, }; // Pasting a copied tableberg element while the caret is inside this text // block: RichText would inline the markup as plain text, so route it to // real block insertion instead (empty text gets replaced, otherwise the // blocks land right below). const onPasteCapture = (event: React.ClipboardEvent) => { const plain = event.clipboardData?.getData("text/plain") ?? ""; if (!plain.includes("