@plaudit/gutenberg-api-extensions 1.4.2 → 1.5.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: Required<TAttributes>;
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
- export function registerSimpleBlockType(metadata, config) {
5
+ function applyDefaults(config, props) {
6
6
  var _a;
7
- function applyDefaults(props) {
8
- const mutableProps = Object.assign(Object.assign({}, props), { attributes: Object.assign({}, props.attributes) });
9
- for (const [k, v] of Object.entries(config.defaults)) {
10
- if (mutableProps.attributes[k] === undefined) {
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
- const rendered = config.renderer(populatedProps, useBlockProps, () => React.createElement(InnerBlocks, Object.assign({}, config.innerBlocksProps)), "edit");
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
- rendered);
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
- return config.renderer(applyDefaults(props), useBlockProps.save, () => React.createElement(InnerBlocks.Content, null), "save");
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/gutenberg-api-extensions",
3
- "version": "1.4.2",
3
+ "version": "1.5.0",
4
4
  "scripts": {
5
5
  "prepublishOnly": "rm -rf build && mkdir build && tsc",
6
6
  "build": "tsc",