@plaudit/gutenberg-api-extensions 2.5.1 → 2.7.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,4 +1,5 @@
1
1
  import type { BlockIcon } from "@wordpress/blocks";
2
+ import type { RadioControlProps } from "@wordpress/components/build-types/radio-control/types";
2
3
  import type { RangeControlProps } from "@wordpress/components/build-types/range-control/types";
3
4
  import type { TextControlProps } from "@wordpress/components/build-types/text-control/types";
4
5
  import type { TextareaControlProps } from "@wordpress/components/build-types/textarea-control/types";
@@ -6,13 +7,20 @@ import type { ToggleControlProps } from "@wordpress/components/build-types/toggl
6
7
  import type { ToggleGroupControlProps } from "@wordpress/components/build-types/toggle-group-control/types";
7
8
  import type { PickableOptions } from "../controls/types";
8
9
  import type { SimpleNativeProperty } from "./simple-native-property";
10
+ export type ActualBlockEditProps = {
11
+ attributes: Record<string, any>;
12
+ setAttributes: (attributes: Record<string, any>) => void;
13
+ name: string;
14
+ };
9
15
  type CommonPropertyConfig<T, V> = {
10
16
  name: string;
11
17
  label: string;
12
18
  default: T;
13
19
  enum?: T[];
14
- component?: V;
20
+ component?: Partial<V>;
15
21
  help?: string;
22
+ alwaysStore?: boolean;
23
+ condition?(blockEditProps: ActualBlockEditProps): boolean;
16
24
  };
17
25
  export declare function rangeProperty(config: CommonPropertyConfig<number, RangeControlProps> & {
18
26
  min: number;
@@ -20,6 +28,12 @@ export declare function rangeProperty(config: CommonPropertyConfig<number, Range
20
28
  step?: number;
21
29
  enum?: number[];
22
30
  }): SimpleNativeProperty;
31
+ export declare function radioProperty(config: CommonPropertyConfig<string, RadioControlProps> & {
32
+ options: PickableOptions<string>;
33
+ }): SimpleNativeProperty;
34
+ export declare function selectProperty(config: CommonPropertyConfig<string, never> & {
35
+ options: PickableOptions<string>;
36
+ }): SimpleNativeProperty;
23
37
  export declare function toggleProperty(config: CommonPropertyConfig<boolean, ToggleControlProps>): SimpleNativeProperty;
24
38
  type ToggleGroupPropertyConfigBase<T extends string | number> = CommonPropertyConfig<T, ToggleGroupControlProps> & {
25
39
  options: PickableOptions<T, {
@@ -1,5 +1,5 @@
1
1
  import { MediaUpload, MediaUploadCheck } from "@wordpress/block-editor";
2
- import { Button, Card, CardBody, CardHeader, FocalPointPicker, RangeControl, ResponsiveWrapper, TextareaControl, TextControl, ToggleControl, __experimentalToggleGroupControl as ToggleGroupControl, __experimentalHeading as Heading, __experimentalToggleGroupControlOption as ToggleGroupControlOption, __experimentalToggleGroupControlOptionIcon as ToggleGroupControlOptionIcon } from "@wordpress/components";
2
+ import { Button, Card, CardBody, CardHeader, FocalPointPicker, RadioControl, RangeControl, SelectControl, ResponsiveWrapper, TextareaControl, TextControl, ToggleControl, __experimentalToggleGroupControl as ToggleGroupControl, __experimentalHeading as Heading, __experimentalToggleGroupControlOption as ToggleGroupControlOption, __experimentalToggleGroupControlOptionIcon as ToggleGroupControlOptionIcon } from "@wordpress/components";
3
3
  import { useSelect } from "@wordpress/data";
4
4
  import { __ } from "@wordpress/i18n";
5
5
  import React from "react";
@@ -9,16 +9,46 @@ export function rangeProperty(config) {
9
9
  type: 'number',
10
10
  enum: config.enum,
11
11
  default: config.default,
12
+ condition: config.condition,
13
+ alwaysStore: config.alwaysStore,
12
14
  renderer(value, onChange) {
13
15
  return React.createElement(RangeControl, Object.assign({ value: value, onChange: onChange, min: config.min, max: config.max, label: config.label, step: config.step, help: config.help }, config.component));
14
16
  }
15
17
  };
16
18
  }
19
+ export function radioProperty(config) {
20
+ return {
21
+ name: config.name,
22
+ type: 'string',
23
+ enum: config.enum,
24
+ default: config.default,
25
+ condition: config.condition,
26
+ alwaysStore: config.alwaysStore,
27
+ renderer(value, onChange) {
28
+ return React.createElement(RadioControl, Object.assign({ selected: value, onChange: onChange, options: asOptions(config.options), label: config.label, help: config.help }, config.component));
29
+ }
30
+ };
31
+ }
32
+ export function selectProperty(config) {
33
+ return {
34
+ name: config.name,
35
+ type: 'string',
36
+ enum: config.enum,
37
+ default: config.default,
38
+ condition: config.condition,
39
+ alwaysStore: config.alwaysStore,
40
+ renderer(value, onChange) {
41
+ return React.createElement(SelectControl, { value: value !== null && value !== void 0 ? value : '', onChange: onChange, options: asOptions(config.options, ''), label: config.label, help: config.help });
42
+ }
43
+ };
44
+ }
17
45
  export function toggleProperty(config) {
18
46
  return {
19
47
  name: config.name,
20
48
  type: 'boolean',
21
49
  default: config.default,
50
+ condition: config.condition,
51
+ alwaysStore: config.alwaysStore,
22
52
  renderer(value, onChange) {
23
53
  return React.createElement(ToggleControl, Object.assign({ checked: value, onChange: onChange, label: config.label, help: config.help }, config.component));
24
54
  }
@@ -33,6 +63,8 @@ export function toggleGroupProperty(config) {
33
63
  name: config.name,
34
64
  type: 'string',
35
65
  default: config.default,
66
+ condition: config.condition,
67
+ alwaysStore: config.alwaysStore,
36
68
  renderer(value, onChange) {
37
69
  return React.createElement(ToggleGroupControl, Object.assign({ value: value, onChange: v => onChange(v === null || v === void 0 ? void 0 : v.toString()), label: config.label, help: config.help, isBlock: true }, config.component, { children: children }));
38
70
  }
@@ -43,6 +75,8 @@ export function toggleGroupProperty(config) {
43
75
  name: config.name,
44
76
  type: 'number',
45
77
  default: config.default,
78
+ condition: config.condition,
79
+ alwaysStore: config.alwaysStore,
46
80
  renderer(value, onChange) {
47
81
  return React.createElement(ToggleGroupControl, Object.assign({ value: value, label: config.label, help: config.help, isBlock: true }, config.component, { onChange: v => onChange(v === undefined || typeof v === 'number' ? v : (v.includes('.') ? parseFloat(v) : parseInt(v))), children: children }));
48
82
  }
@@ -54,6 +88,8 @@ export function textProperty(config) {
54
88
  name: config.name,
55
89
  type: 'string',
56
90
  default: config.default,
91
+ condition: config.condition,
92
+ alwaysStore: config.alwaysStore,
57
93
  renderer(value, onChange) {
58
94
  return React.createElement(TextControl, Object.assign({ value: value, onChange: onChange, label: config.label, help: config.help }, config.component));
59
95
  }
@@ -64,6 +100,8 @@ export function textareaProperty(config) {
64
100
  name: config.name,
65
101
  type: 'string',
66
102
  default: config.default,
103
+ condition: config.condition,
104
+ alwaysStore: config.alwaysStore,
67
105
  renderer(value, onChange) {
68
106
  return React.createElement(TextareaControl, Object.assign({ value: value, onChange: onChange, label: config.label, help: config.help }, config.component));
69
107
  }
@@ -74,6 +112,8 @@ export function imageProperty(config) {
74
112
  return {
75
113
  name: config.name,
76
114
  type: "object",
115
+ condition: config.condition,
116
+ alwaysStore: config.alwaysStore,
77
117
  default: { media: { id: (_c = (_b = (_a = config.default) === null || _a === void 0 ? void 0 : _a.media) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : 0, url: (_f = (_e = (_d = config.default) === null || _d === void 0 ? void 0 : _d.media) === null || _e === void 0 ? void 0 : _e.url) !== null && _f !== void 0 ? _f : '' }, pos: { x: (_j = (_h = (_g = config.default) === null || _g === void 0 ? void 0 : _g.pos) === null || _h === void 0 ? void 0 : _h.x) !== null && _j !== void 0 ? _j : 50, y: (_m = (_l = (_k = config.default) === null || _k === void 0 ? void 0 : _k.pos) === null || _l === void 0 ? void 0 : _l.y) !== null && _m !== void 0 ? _m : 50 } },
78
118
  renderer(value, onChange) {
79
119
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
@@ -115,3 +155,10 @@ export function imageProperty(config) {
115
155
  }
116
156
  };
117
157
  }
158
+ function asOptions(options, noSelectionValue) {
159
+ const res = options.map(opt => ({ value: opt[0], label: typeof opt[1] === 'string' ? opt[1] : opt[1].text }));
160
+ if (noSelectionValue !== undefined && !res.some(opt => !opt.value)) {
161
+ res.splice(0, 0, { value: noSelectionValue, label: "-- No Selection --" });
162
+ }
163
+ return res;
164
+ }
@@ -1,3 +1,4 @@
1
+ import type { ActualBlockEditProps } from "./common-native-property-implementations";
1
2
  import React from "react";
2
3
  export type InspectorPanelGroup = 'default' | 'advanced' | 'background' | 'border' | 'color' | 'dimensions' | 'effects' | 'filter' | 'list' | 'position' | 'settings' | 'styles' | 'typography';
3
4
  type GenericSimpleNativeProperty<T, V extends 'string' | 'number' | 'boolean' | 'array' | 'object'> = {
@@ -5,7 +6,9 @@ type GenericSimpleNativeProperty<T, V extends 'string' | 'number' | 'boolean' |
5
6
  type: V;
6
7
  enum?: T[];
7
8
  default: T;
9
+ alwaysStore?: boolean;
8
10
  renderer(value: T, onChange: (v: T | undefined) => void): React.JSX.Element;
11
+ condition?(blockEditProps: ActualBlockEditProps): boolean;
9
12
  };
10
13
  export type SimpleNativeProperty = GenericSimpleNativeProperty<string, 'string'> & {
11
14
  enum?: string[];
@@ -17,6 +20,7 @@ export type SimpleNativePanel = {
17
20
  group?: InspectorPanelGroup;
18
21
  initialOpen?: boolean;
19
22
  properties: SimpleNativeProperty[];
23
+ condition?(blockEditProps: ActualBlockEditProps): boolean;
20
24
  };
21
25
  export declare function registerSimpleNativeProperties(config: {
22
26
  block: `${string}/${string}` | Array<`${string}/${string}`>;
@@ -39,7 +39,7 @@ export function installSimpleNativePropertiesSupport() {
39
39
  for (const property of blockSimpleNativePanel.properties) {
40
40
  const attrPath = property.name.split('.');
41
41
  if (attrPath.length === 1) {
42
- injectableProperties[property.name] = { type: property.type, default: property.default };
42
+ injectableProperties[property.name] = { type: property.type, default: property.alwaysStore ? undefined : property.default };
43
43
  if ('enum' in property) {
44
44
  injectableProperties[property.name].enum = property.enum;
45
45
  }
@@ -90,10 +90,11 @@ export function installSimpleNativePropertiesSupport() {
90
90
  if (!blockSimpleNativePanels) {
91
91
  return React.createElement(BlockEdit, Object.assign({}, blockEditProps));
92
92
  }
93
+ let keyIndex = 0;
93
94
  return React.createElement(React.Fragment, null,
94
95
  React.createElement(BlockEdit, Object.assign({}, blockEditProps)),
95
- ...blockSimpleNativePanels.map(p => React.createElement(React.Fragment, null,
96
- React.createElement(InspectorPanel, Object.assign({}, p), ...p.properties.map(prop => {
96
+ ...blockSimpleNativePanels.map(p => {
97
+ const items = p.properties.map(prop => {
97
98
  const propPath = prop.name.split('.');
98
99
  let existingValue;
99
100
  if (propPath.length === 1) {
@@ -123,34 +124,40 @@ export function installSimpleNativePropertiesSupport() {
123
124
  }
124
125
  existingValue = graphExistingValue !== null && graphExistingValue !== void 0 ? graphExistingValue : prop.default;
125
126
  }
126
- switch (prop.type) {
127
- case "array":
128
- // If the value is not an array or is a sparse array, then it will cause unrecoverable errors upon conversion to PHP
129
- if (!Array.isArray(existingValue) || existingValue.length > existingValue.filter(() => true).length) {
130
- throw new Error(`Invalid value passed to an array-type property: ${existingValue}`);
131
- }
132
- return prop.renderer(existingValue, value => setDottedAttribute(blockEditProps, prop.name, value));
133
- case "object":
134
- if (Array.isArray(existingValue) || typeof existingValue !== 'object') {
135
- throw new Error(`Invalid value passed to an object-type property: ${existingValue}`);
136
- }
137
- return prop.renderer(existingValue, value => setDottedAttribute(blockEditProps, prop.name, value));
138
- case "boolean":
139
- if (typeof existingValue !== 'boolean' && existingValue !== undefined) {
140
- existingValue = !!existingValue;
141
- }
142
- return prop.renderer(existingValue, value => setDottedAttribute(blockEditProps, prop.name, value));
143
- case 'string':
144
- if (typeof existingValue !== 'string' && existingValue !== undefined) {
145
- existingValue = existingValue.toString();
146
- }
147
- return prop.renderer(existingValue, value => setDottedAttribute(blockEditProps, prop.name, value));
148
- case "number":
149
- if (typeof existingValue !== 'number' && existingValue !== undefined) {
150
- existingValue = parseFloat(existingValue);
151
- }
152
- return prop.renderer(existingValue, value => setDottedAttribute(blockEditProps, prop.name, value));
127
+ let ele;
128
+ if (prop.type === "array") { // If the value is not an array or is a sparse array, then it will cause unrecoverable errors upon conversion to PHP
129
+ if (!Array.isArray(existingValue) || existingValue.length > existingValue.filter(() => true).length) {
130
+ throw new Error(`Invalid value passed to an array-type property: ${existingValue}`);
131
+ }
132
+ ele = prop.renderer(existingValue, value => setDottedAttribute(blockEditProps, prop.name, value));
133
+ }
134
+ else if (prop.type === "object") {
135
+ if (Array.isArray(existingValue) || typeof existingValue !== 'object') {
136
+ throw new Error(`Invalid value passed to an object-type property: ${existingValue}`);
137
+ }
138
+ ele = prop.renderer(existingValue, value => setDottedAttribute(blockEditProps, prop.name, value));
139
+ }
140
+ else if (prop.type === "boolean") {
141
+ if (typeof existingValue !== 'boolean' && existingValue !== undefined) {
142
+ existingValue = !!existingValue;
143
+ }
144
+ ele = prop.renderer(existingValue, value => setDottedAttribute(blockEditProps, prop.name, value));
145
+ }
146
+ else if (prop.type === "string") {
147
+ if (typeof existingValue !== 'string' && existingValue !== undefined) {
148
+ existingValue = existingValue.toString();
149
+ }
150
+ ele = prop.renderer(existingValue, value => setDottedAttribute(blockEditProps, prop.name, value));
151
+ }
152
+ else {
153
+ if (typeof existingValue !== 'number' && existingValue !== undefined) {
154
+ existingValue = parseFloat(existingValue);
155
+ }
156
+ ele = prop.renderer(existingValue, value => setDottedAttribute(blockEditProps, prop.name, value));
153
157
  }
154
- })))));
158
+ return prop.condition === undefined || prop.condition(blockEditProps) ? ele : undefined;
159
+ }).filter((ele) => ele !== undefined);
160
+ return React.createElement(InspectorPanel, Object.assign({}, p, { condition: () => items.length > 0 && (p.condition === undefined || p.condition(blockEditProps)), key: `plaudit-simple-native-property-${keyIndex++}` }), ...items);
161
+ }));
155
162
  }, 'plauditGutenbergApiExtensionsSimpleNativeProperties'));
156
163
  }
@@ -2,4 +2,5 @@ import type { PanelBodyProps } from "@wordpress/components/build-types/panel/typ
2
2
  import React from "react";
3
3
  export declare function InspectorPanel(props: PanelBodyProps & {
4
4
  group?: string;
5
+ condition?(): boolean;
5
6
  }): React.JSX.Element;
@@ -4,5 +4,5 @@ import React from "react";
4
4
  export function InspectorPanel(props) {
5
5
  const FullyTypedInspectorControls = InspectorControls;
6
6
  return React.createElement(FullyTypedInspectorControls, { group: props.group },
7
- React.createElement(PanelBody, Object.assign({}, props, { children: props.children })));
7
+ React.createElement(PanelBody, Object.assign({}, props, { children: props.condition === undefined || props.condition() ? props.children : [] })));
8
8
  }
@@ -0,0 +1,2 @@
1
+ import React from "react";
2
+ export declare const placementCenter: React.JSX.Element;
@@ -0,0 +1,3 @@
1
+ import React from "react";
2
+ export const placementCenter = React.createElement("svg", { version: "1.1", x: "0px", y: "0px", focusable: false, viewBox: "0 0 24 24", enableBackground: "new 0 0 24 24", xmlSpace: "preserve" },
3
+ React.createElement("path", { d: "M20,11h-5V4H9v7H4v1.5h5V20h6v-7.5h5V11z" }));
@@ -0,0 +1,2 @@
1
+ import React from "react";
2
+ export declare const placementEnd: React.JSX.Element;
@@ -0,0 +1,3 @@
1
+ import React from "react";
2
+ export const placementEnd = React.createElement("svg", { version: "1.1", x: "0px", y: "0px", focusable: false, viewBox: "0 0 24 24", enableBackground: "new 0 0 24 24", xmlSpace: "preserve" },
3
+ React.createElement("path", { d: "M15,4H9v11h6V4z M4,18.5V20h16v-1.5H4z" }));
@@ -0,0 +1,2 @@
1
+ import React from "react";
2
+ export declare const placementStart: React.JSX.Element;
@@ -0,0 +1,3 @@
1
+ import React from "react";
2
+ export const placementStart = React.createElement("svg", { version: "1.1", x: "0px", y: "0px", focusable: false, viewBox: "0 0 24 24", enableBackground: "new 0 0 24 24", xmlSpace: "preserve" },
3
+ React.createElement("path", { d: "M9,20h6V9H9V20z M4,4v1.5h16V4H4z" }));
@@ -0,0 +1,2 @@
1
+ import React from "react";
2
+ export declare const placementStretch: React.JSX.Element;
@@ -0,0 +1,3 @@
1
+ import React from "react";
2
+ export const placementStretch = React.createElement("svg", { version: "1.1", x: "0px", y: "0px", focusable: false, viewBox: "0 0 24 24", enableBackground: "new 0 0 24 24", xmlSpace: "preserve" },
3
+ React.createElement("path", { d: "M8.8,6.5h6v11h-6V6.5z M4,18.5h16V20H4V18.5z M4,4h16v1.5H4V4z" }));
@@ -1,5 +1,9 @@
1
- export * from './plaudit-icons/plaudit-icon';
2
1
  export * from './plaudit-icons/column-1';
3
2
  export * from './plaudit-icons/column-2';
4
3
  export * from './plaudit-icons/column-3';
4
+ export * from './plaudit-icons/placement-start';
5
+ export * from './plaudit-icons/placement-center';
6
+ export * from './plaudit-icons/placement-end';
7
+ export * from './plaudit-icons/placement-stretch';
8
+ export * from './plaudit-icons/plaudit-icon';
5
9
  export * from './plaudit-icons/reusable-block-marker';
@@ -1,8 +1,12 @@
1
1
  // =============================================
2
2
  // Allows sharing of assets in block configurations
3
3
  // =============================================
4
- export * from './plaudit-icons/plaudit-icon';
5
4
  export * from './plaudit-icons/column-1';
6
5
  export * from './plaudit-icons/column-2';
7
6
  export * from './plaudit-icons/column-3';
7
+ export * from './plaudit-icons/placement-start';
8
+ export * from './plaudit-icons/placement-center';
9
+ export * from './plaudit-icons/placement-end';
10
+ export * from './plaudit-icons/placement-stretch';
11
+ export * from './plaudit-icons/plaudit-icon';
8
12
  export * from './plaudit-icons/reusable-block-marker';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/gutenberg-api-extensions",
3
- "version": "2.5.1",
3
+ "version": "2.7.0",
4
4
  "scripts": {
5
5
  "prepublishOnly": "rm -rf build && mkdir build && tsc",
6
6
  "build": "tsc",
@@ -34,7 +34,7 @@
34
34
  "react-dom": "^18.2.0"
35
35
  },
36
36
  "devDependencies": {
37
- "@types/react": "^18.2.55",
37
+ "@types/react": "^18.2.57",
38
38
  "@types/wordpress__block-editor": "^11.5.10",
39
39
  "@types/wordpress__blocks": "^12.5.13",
40
40
  "typescript": "^5.3.3"