PluginProbe
Tableberg – Simple Gutenberg Table Block / 1.2.0
Tableberg – Simple Gutenberg Table Block v1.2.0
1.2.0 1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 trunk 0.0.2 0.2.1 0.3.2 0.3.3 0.4.1 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 All 43 releases
tableberg / src / blocks / element-edit-common.tsx

element-edit-common.tsx in Tableberg – Simple Gutenberg Table Block 1.2.0, at src/blocks/element-edit-common.tsx

149 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { CSSProperties } from "react";
2
3 import { CellElement, CellKey } from "../attributes";
4 import {
5 ElementAlignment,
6 elementAlignmentToJustifyContent,
7 } from "../alignment";
8
9 /** Shared bits for the element blocks' edit components. */
10
11 export function alignmentWrapperStyle(align: ElementAlignment): CSSProperties {
12 return {
13 display: "flex",
14 justifyContent: elementAlignmentToJustifyContent(align),
15 textAlign: align,
16 };
17 }
18
19 // Placeholder coords for reused controls whose store path is inactive in
20 // the native editor (the passed updateAttrs/updateStyles override always
21 // wins).
22 export const NO_COORDS = "0,0" as CellKey;
23
24 import { BlockControls } from "@wordpress/block-editor";
25 import {
26 MenuGroup,
27 MenuItem,
28 ToolbarDropdownMenu,
29 } from "@wordpress/components";
30 import { styles as stylesIcon } from "@wordpress/icons";
31 import { ToolbarWithDropdown } from "@tableberg/components";
32 import { __ } from "@wordpress/i18n";
33
34 import {
35 applyElementStyleClipboardPayload,
36 readElementStylePayload,
37 rememberElementStylePayload,
38 serializeElementStyleClipboardPayload,
39 writeClipboardText,
40 } from "../hooks/block-editor-compat/elementClipboard";
41
42 export function ElementAlignmentToolbar({
43 align,
44 onChange,
45 }: {
46 align: ElementAlignment;
47 onChange: (align: ElementAlignment) => void;
48 }) {
49 return (
50 <BlockControls>
51 <ToolbarWithDropdown
52 title={__("Align element", "tableberg")}
53 value={align}
54 onChange={(newAlign?: string) => {
55 if (
56 newAlign === "left" ||
57 newAlign === "center" ||
58 newAlign === "right"
59 ) {
60 onChange(newAlign);
61 }
62 }}
63 controlset="alignment"
64 />
65 </BlockControls>
66 );
67 }
68
69 /**
70 * Copy styles / paste styles for an element block.
71 *
72 * The element options menu that carries these in the preview path works off
73 * the table store, which the element blocks are not in, so they get their own
74 * toolbar entry here. The clipboard payload is the shared one, so styles can
75 * be pasted onto any element that has the same style keys.
76 */
77 export function ElementStyleOptions({
78 elementName,
79 attributes,
80 setAttributes,
81 }: {
82 /** The element's name without the `tableberg/` prefix. */
83 elementName: string;
84 /** Merged with the defaults, so the copy carries what is rendered. */
85 attributes: Record<string, unknown>;
86 setAttributes: (attrs: Record<string, unknown>) => void;
87 }) {
88 const element = {
89 name: elementName,
90 attributes,
91 } as unknown as CellElement;
92
93 return (
94 <BlockControls group="other">
95 <ToolbarDropdownMenu
96 icon={stylesIcon}
97 label={__("Element styles", "tableberg")}
98 toggleProps={{
99 title: __("Element styles", "tableberg"),
100 showTooltip: true,
101 }}
102 popoverProps={{ placement: "bottom-start" }}
103 >
104 {({ onClose }) => (
105 <MenuGroup>
106 <MenuItem
107 onClick={() => {
108 rememberElementStylePayload(element);
109 // Also on the system clipboard, so the styles
110 // can be pasted in another tab.
111 void writeClipboardText(
112 serializeElementStyleClipboardPayload(
113 element
114 )
115 );
116 onClose();
117 }}
118 >
119 {__("Copy styles", "tableberg")}
120 </MenuItem>
121 <MenuItem
122 onClick={() => {
123 void (async () => {
124 const payload =
125 await readElementStylePayload();
126
127 if (!payload) {
128 return;
129 }
130
131 setAttributes(
132 applyElementStyleClipboardPayload(
133 element,
134 payload
135 ).attributes as Record<string, unknown>
136 );
137 })();
138 onClose();
139 }}
140 >
141 {__("Paste styles", "tableberg")}
142 </MenuItem>
143 </MenuGroup>
144 )}
145 </ToolbarDropdownMenu>
146 </BlockControls>
147 );
148 }
149