@plaudit/gutenberg-api-extensions 1.4.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
import { type Block, type BlockConfiguration, type BlockSaveProps } from "@wordpress/blocks";
|
|
2
2
|
import { InnerBlocks } from "@wordpress/block-editor";
|
|
3
3
|
import type { UseBlockProps } from "@wordpress/block-editor/components/use-block-props";
|
|
4
|
-
import React, { type ReactNode } from "react";
|
|
4
|
+
import React, { type ComponentType, type ReactNode } from "react";
|
|
5
5
|
export type SimpleBlockTypeConfig<TAttributes extends Record<string, any>> = {
|
|
6
6
|
icon?: BlockConfiguration<TAttributes>['icon'] | undefined;
|
|
7
|
-
defaults
|
|
7
|
+
defaults?: Required<TAttributes>;
|
|
8
8
|
controls?: NonNullable<Block<TAttributes>['edit']>;
|
|
9
9
|
renderer: (attributes: BlockSaveProps<TAttributes>, wrapperProps: UseBlockProps | UseBlockProps['save'], InnerBlocks: () => React.ReactElement, mode: "edit" | "save") => ReactNode;
|
|
10
10
|
innerBlocksProps?: InnerBlocks.Props;
|
|
11
11
|
settings?: Partial<BlockConfiguration<TAttributes>>;
|
|
12
12
|
};
|
|
13
|
+
declare function applyDefaults<TAttributes extends Record<string, any>, K extends BlockSaveProps<TAttributes>>(config: Omit<SimpleBlockTypeConfig<TAttributes>, 'renderer'>, props: K): Omit<K, "attributes"> & {
|
|
14
|
+
attributes: { [P in keyof K["attributes"]]: K["attributes"][P]; };
|
|
15
|
+
};
|
|
13
16
|
export declare function registerSimpleBlockType<TAttributes extends Record<string, any> = {}>(metadata: BlockConfiguration<TAttributes>, config: SimpleBlockTypeConfig<TAttributes>): Block<TAttributes> | undefined;
|
|
17
|
+
type SimpleContainerTypeConfig<TAttributes extends Record<string, any>> = Omit<SimpleBlockTypeConfig<TAttributes>, 'renderer'> & {
|
|
18
|
+
tag?: ComponentType;
|
|
19
|
+
attributesFromProps?: (arg: ReturnType<typeof applyDefaults<TAttributes, BlockSaveProps<TAttributes>>>, mode: "edit" | "save") => Parameters<UseBlockProps | UseBlockProps['save']>[0];
|
|
20
|
+
};
|
|
21
|
+
export declare function registerSimpleContainerBlock<TAttributes extends Record<string, any> = {}>(metadata: BlockConfiguration<TAttributes>, config: SimpleContainerTypeConfig<TAttributes>): Block<TAttributes> | undefined;
|
|
22
|
+
export {};
|
|
@@ -1,26 +1,41 @@
|
|
|
1
1
|
import { registerBlockType } from "@wordpress/blocks";
|
|
2
|
-
import { InnerBlocks, useBlockProps } from "@wordpress/block-editor";
|
|
2
|
+
import { InnerBlocks, useBlockProps, useInnerBlocksProps } from "@wordpress/block-editor";
|
|
3
3
|
import { plauditIcon } from "../lib/plaudit-icons";
|
|
4
4
|
import React from "react";
|
|
5
|
-
|
|
5
|
+
function applyDefaults(config, props) {
|
|
6
6
|
var _a;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
mutableProps.attributes[k] = v;
|
|
12
|
-
}
|
|
7
|
+
const mutableProps = Object.assign(Object.assign({}, props), { attributes: Object.assign({}, props.attributes) });
|
|
8
|
+
for (const [k, v] of Object.entries((_a = config.defaults) !== null && _a !== void 0 ? _a : {})) {
|
|
9
|
+
if (mutableProps.attributes[k] === undefined) {
|
|
10
|
+
mutableProps.attributes[k] = v;
|
|
13
11
|
}
|
|
14
|
-
return mutableProps;
|
|
15
12
|
}
|
|
13
|
+
return mutableProps;
|
|
14
|
+
}
|
|
15
|
+
export function registerSimpleBlockType(metadata, config) {
|
|
16
|
+
var _a;
|
|
16
17
|
return registerBlockType(metadata, Object.assign(Object.assign({}, config.settings), { icon: (_a = config.icon) !== null && _a !== void 0 ? _a : plauditIcon, edit(props) {
|
|
17
|
-
const populatedProps = applyDefaults(props);
|
|
18
|
-
|
|
18
|
+
const populatedProps = applyDefaults(config, props);
|
|
19
|
+
return React.createElement(React.Fragment, null,
|
|
20
|
+
config.controls && React.createElement(config.controls, Object.assign({}, populatedProps)),
|
|
21
|
+
config.renderer(populatedProps, useBlockProps, () => React.createElement(InnerBlocks, Object.assign({}, config.innerBlocksProps)), "edit"));
|
|
22
|
+
},
|
|
23
|
+
save(props) {
|
|
24
|
+
return config.renderer(applyDefaults(config, props), useBlockProps.save, () => React.createElement(InnerBlocks.Content, null), "save");
|
|
25
|
+
} }));
|
|
26
|
+
}
|
|
27
|
+
export function registerSimpleContainerBlock(metadata, config) {
|
|
28
|
+
var _a, _b;
|
|
29
|
+
const Tag = (_a = config.tag) !== null && _a !== void 0 ? _a : ((props) => React.createElement("div", Object.assign({}, props)));
|
|
30
|
+
return registerBlockType(metadata, Object.assign(Object.assign({}, config.settings), { icon: (_b = config.icon) !== null && _b !== void 0 ? _b : plauditIcon, edit(props) {
|
|
31
|
+
var _a;
|
|
32
|
+
const populatedProps = applyDefaults(config, props);
|
|
19
33
|
return React.createElement(React.Fragment, null,
|
|
20
34
|
config.controls && React.createElement(config.controls, Object.assign({}, populatedProps)),
|
|
21
|
-
|
|
35
|
+
React.createElement(Tag, Object.assign({}, useInnerBlocksProps(useBlockProps(((_a = config.attributesFromProps) !== null && _a !== void 0 ? _a : (() => ({})))(populatedProps, "edit")), config.innerBlocksProps))));
|
|
22
36
|
},
|
|
23
37
|
save(props) {
|
|
24
|
-
|
|
38
|
+
var _a;
|
|
39
|
+
return React.createElement(Tag, Object.assign({}, useInnerBlocksProps.save(useBlockProps.save(((_a = config.attributesFromProps) !== null && _a !== void 0 ? _a : (() => ({})))(applyDefaults(config, props), "edit")))));
|
|
25
40
|
} }));
|
|
26
41
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import type { BlockIcon } from "@wordpress/blocks";
|
|
1
2
|
import type { PickableOptions, SimpleBlockControlProps } from "./types";
|
|
2
3
|
import React from "react";
|
|
3
|
-
import { BlockIcon } from "@wordpress/blocks";
|
|
4
4
|
export declare function PickOneFromToggleGroup<T extends string>(props: SimpleBlockControlProps<T, string> & {
|
|
5
5
|
options: PickableOptions<{
|
|
6
6
|
icon: BlockIcon;
|
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import { __experimentalToggleGroupControl as ToggleGroupControl, __experimentalToggleGroupControlOption as ToggleGroupControlOption, __experimentalToggleGroupControlOptionIcon as ToggleGroupControlOptionIcon, ColorPalette, RadioControl, SelectControl } from "@wordpress/components";
|
|
1
|
+
import { __experimentalToggleGroupControl as ToggleGroupControl, __experimentalToggleGroupControlOption as ToggleGroupControlOption, __experimentalToggleGroupControlOptionIcon as ToggleGroupControlOptionIcon, BaseControl, ColorPalette, RadioControl, SelectControl, useBaseControlProps } from "@wordpress/components";
|
|
2
2
|
import React from "react";
|
|
3
3
|
function makeSharedRadioAndSelectProps(props) {
|
|
4
4
|
return {
|
|
5
5
|
label: props.label,
|
|
6
|
-
onChange
|
|
7
|
-
|
|
6
|
+
onChange(value) {
|
|
7
|
+
props.setAttributes({ [props.attribute]: value });
|
|
8
|
+
},
|
|
9
|
+
options: props.options.map(([value, label]) => ({
|
|
8
10
|
value, label: typeof label === 'string' ? label : label.text
|
|
9
11
|
}))
|
|
10
12
|
};
|
|
11
13
|
}
|
|
12
14
|
export function PickOneFromToggleGroup(props) {
|
|
13
|
-
return React.createElement(ToggleGroupControl, { label: props.label, value: props.attributes[props.attribute], onChange: value => props.setAttributes({ [props.attribute]: value }), children:
|
|
15
|
+
return React.createElement(ToggleGroupControl, { label: props.label, value: props.attributes[props.attribute], onChange: value => props.setAttributes({ [props.attribute]: value }), children: props.options.map(([value, label]) => typeof label === 'string'
|
|
14
16
|
? React.createElement(ToggleGroupControlOption, { key: value, value: value, label: label })
|
|
15
17
|
: React.createElement(ToggleGroupControlOptionIcon, { key: value, value: value, label: label.text, icon: label.icon })) });
|
|
16
18
|
}
|
|
@@ -25,7 +27,7 @@ export function PickOneFromColors(props) {
|
|
|
25
27
|
const valueToColorMap = new Map();
|
|
26
28
|
const colorToValueMap = new Map();
|
|
27
29
|
const colors = [];
|
|
28
|
-
for (const [value, display] of
|
|
30
|
+
for (const [value, display] of props.options) {
|
|
29
31
|
if (typeof display === 'string') {
|
|
30
32
|
colors.push({ color: value, name: display });
|
|
31
33
|
}
|
|
@@ -39,8 +41,7 @@ export function PickOneFromColors(props) {
|
|
|
39
41
|
}
|
|
40
42
|
}
|
|
41
43
|
const currentColor = (_a = valueToColorMap.get(props.attributes[props.attribute])) !== null && _a !== void 0 ? _a : props.attributes[props.attribute];
|
|
42
|
-
const
|
|
43
|
-
return React.createElement(
|
|
44
|
-
React.createElement(
|
|
45
|
-
React.createElement(ColorPalette, { id: id, disableCustomColors: true, onChange: color => { var _a, _b; return props.setAttributes({ [props.attribute]: (_b = (_a = colorToValueMap.get(color !== null && color !== void 0 ? color : currentColor)) !== null && _a !== void 0 ? _a : color) !== null && _b !== void 0 ? _b : currentColor }); }, colors: colors, value: currentColor, clearable: false }));
|
|
44
|
+
const { baseControlProps, controlProps } = useBaseControlProps({ label: props.label });
|
|
45
|
+
return React.createElement(BaseControl, Object.assign({}, baseControlProps),
|
|
46
|
+
React.createElement(ColorPalette, Object.assign({}, controlProps, { disableCustomColors: true, onChange: color => { var _a, _b; return props.setAttributes({ [props.attribute]: (_b = (_a = colorToValueMap.get(color !== null && color !== void 0 ? color : currentColor)) !== null && _a !== void 0 ? _a : color) !== null && _b !== void 0 ? _b : currentColor }); }, colors: colors, value: currentColor, clearable: false })));
|
|
46
47
|
}
|
|
@@ -7,6 +7,6 @@ export type SpecificValueBlockProps<T extends string, V> = {
|
|
|
7
7
|
export type SimpleBlockControlProps<T extends string, V> = SpecificValueBlockProps<T, V> & {
|
|
8
8
|
label: string;
|
|
9
9
|
};
|
|
10
|
-
export type PickableOptions<T extends object = {}> =
|
|
10
|
+
export type PickableOptions<T extends object = {}> = Array<[string, string | ({
|
|
11
11
|
text: string;
|
|
12
|
-
} & T)>;
|
|
12
|
+
} & T)]>;
|