@plaudit/gutenberg-api-extensions 1.0.2 → 1.1.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 +1,2 @@
1
1
  export * from "./simple-block";
2
+ export * from "./layered-styles";
@@ -1 +1,2 @@
1
1
  export * from "./simple-block";
2
+ export * from "./layered-styles";
@@ -0,0 +1,15 @@
1
+ export interface LayeredBlockStyleLayer {
2
+ name: string;
3
+ title: string;
4
+ default?: string | undefined;
5
+ options: Array<{
6
+ name: string;
7
+ label: string;
8
+ }>;
9
+ }
10
+ export interface LayeredBlockStylesConfig {
11
+ block: `${string}/${string}`;
12
+ layers: LayeredBlockStyleLayer[];
13
+ }
14
+ export declare function registerLayeredBlockStyles(config: LayeredBlockStylesConfig): void;
15
+ export declare function installLayeredBlockStylesSupport(): void;
@@ -0,0 +1,63 @@
1
+ import { createHigherOrderComponent } from "@wordpress/compose";
2
+ import { addFilter } from "@wordpress/hooks";
3
+ import React from "react";
4
+ import { InspectorPanel, PickOne } from "../controls";
5
+ export function registerLayeredBlockStyles(config) {
6
+ var _a;
7
+ const layeredBlockStyles = (_a = window.plauditLayeredBlockStyles) !== null && _a !== void 0 ? _a : (window.plauditLayeredBlockStyles = new Map());
8
+ layeredBlockStyles.set(config.block, config);
9
+ }
10
+ export function installLayeredBlockStylesSupport() {
11
+ var _a;
12
+ if (window.plauditLayeredBlockStylesInstalled) {
13
+ return;
14
+ }
15
+ window.plauditLayeredBlockStylesInstalled = true;
16
+ const layeredBlockStyles = (_a = window.plauditLayeredBlockStyles) !== null && _a !== void 0 ? _a : (window.plauditLayeredBlockStyles = new Map());
17
+ function convertLayersToRadios(layer, props) {
18
+ const options = {};
19
+ layer.options.forEach(({ name, label }) => options[name] = label);
20
+ return React.createElement(PickOne, Object.assign({}, props, { attribute: `layeredStyles__${layer.name}`, options: options, layout: 'toggle-group', label: layer.title }));
21
+ }
22
+ addFilter('blocks.registerBlockType', 'plaudit/gutenberg-api-extensions/layered-styles', (atts) => {
23
+ var _a;
24
+ if (layeredBlockStyles.has(atts.name)) {
25
+ (_a = layeredBlockStyles.get(atts.name)) === null || _a === void 0 ? void 0 : _a.layers.forEach(layer => {
26
+ const attName = `layeredStyles__${layer.name}`;
27
+ if (!atts.attributes[attName]) {
28
+ atts.attributes[attName] = { type: 'string', source: 'attribute', attribute: `data-layered-style-${layer.name}` };
29
+ }
30
+ });
31
+ }
32
+ return atts;
33
+ });
34
+ addFilter('blocks.getBlockAttributes', 'plaudit/gutenberg-api-extensions/layered-styles', (blockAttributes, blockType, innerHTML) => {
35
+ var _a;
36
+ if (layeredBlockStyles.has(blockType.name)) {
37
+ const blockRoot = new DOMParser().parseFromString(innerHTML, "application/xml").documentElement;
38
+ (_a = layeredBlockStyles.get(blockType.name)) === null || _a === void 0 ? void 0 : _a.layers.forEach(layer => {
39
+ var _a;
40
+ blockAttributes[`layeredStyles__${layer.name}`] = (_a = blockRoot.getAttribute(`data-layered-style-${layer.name}`)) !== null && _a !== void 0 ? _a : layer.default;
41
+ });
42
+ }
43
+ return blockAttributes;
44
+ });
45
+ addFilter('blocks.getSaveContent.extraProps', 'plaudit/gutenberg-api-extensions/layered-styles', (props, blockType, attributes) => {
46
+ if (!layeredBlockStyles.has(blockType.name)) {
47
+ return props;
48
+ }
49
+ const res = Object.assign({}, props);
50
+ layeredBlockStyles.get(blockType.name).layers.forEach(layer => {
51
+ res[`data-layered-style-${layer.name}`] = attributes[`layeredStyles__${layer.name}`];
52
+ });
53
+ return res;
54
+ });
55
+ addFilter('editor.BlockEdit', 'plaudit/gutenberg-api-extensions/layered-styles', createHigherOrderComponent((BlockEdit) => (props) => {
56
+ if (!layeredBlockStyles.has(props['name'])) {
57
+ return React.createElement(BlockEdit, Object.assign({}, props));
58
+ }
59
+ return React.createElement(React.Fragment, null,
60
+ React.createElement(BlockEdit, Object.assign({}, props)),
61
+ React.createElement(InspectorPanel, { title: "Layered Styles", initialOpen: true }, ...layeredBlockStyles.get(props['name']).layers.map(layer => convertLayersToRadios(layer, props))));
62
+ }, 'plauditGutenbergApiExtensionsLayeredStyles'));
63
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/gutenberg-api-extensions",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "scripts": {
5
5
  "prepublishOnly": "rm -rf build && mkdir build && tsc",
6
6
  "build": "tsc",
@@ -27,7 +27,7 @@
27
27
  "react-dom": "^18.2.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@types/react": "^18.2.11",
30
+ "@types/react": "^18.2.13",
31
31
  "@types/wordpress__block-editor": "^11.5.1",
32
32
  "@types/wordpress__blocks": "^12.5.0",
33
33
  "typescript": "^5.1.3"