@wp-typia/block-types 0.3.0 → 0.3.1

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.
@@ -3,6 +3,7 @@ import type { BlockBorderSupportAttributes, BlockColorSupportAttributes, BlockDi
3
3
  import type { FlexWrap, LayoutType, Orientation } from "../block-editor/layout.js";
4
4
  import type { SpacingAxis, SpacingDimension } from "../block-editor/spacing.js";
5
5
  import { type WordPressBlockApiCompatibilityDiagnostic, type WordPressBlockApiCompatibilityFeature, type WordPressBlockApiCompatibilityManifest, type WordPressCompatibilitySettings, type WordPressVersion } from "./compatibility.js";
6
+ import { type DiagnosticLogger } from "./shared/diagnostics.js";
6
7
  /**
7
8
  * Derived from Gutenberg block support keys and commonly used block.json
8
9
  * support sections.
@@ -191,12 +192,14 @@ type HasNestedSupport<TSupports, TSection extends keyof BlockSupports, TKey exte
191
192
  type IfSupport<TCondition, TAttributes> = TCondition extends true ? TAttributes : {};
192
193
  export interface DefineSupportsInlineOptions {
193
194
  readonly allowUnknownFutureKeys?: boolean;
195
+ readonly logger?: DiagnosticLogger<WordPressBlockApiCompatibilityDiagnostic>;
194
196
  readonly minVersion?: WordPressVersion;
195
197
  readonly minWordPress?: WordPressVersion;
196
198
  readonly onDiagnostic?: (diagnostic: WordPressBlockApiCompatibilityDiagnostic) => void;
197
199
  readonly strict?: boolean;
198
200
  }
199
201
  export interface DefineSupportsOptions extends WordPressCompatibilitySettings {
202
+ readonly logger?: DiagnosticLogger<WordPressBlockApiCompatibilityDiagnostic>;
200
203
  readonly minWordPress?: WordPressVersion;
201
204
  readonly onDiagnostic?: (diagnostic: WordPressBlockApiCompatibilityDiagnostic) => void;
202
205
  }
@@ -1,4 +1,6 @@
1
1
  import { createWordPressBlockApiCompatibilityManifest, } from "./compatibility.js";
2
+ import { handleDiagnostics, } from "./shared/diagnostics.js";
3
+ import { isNonArrayObject, isObjectRecord, } from "./shared/object-utils.js";
2
4
  export const BLOCK_SUPPORT_FEATURES = [
3
5
  'align',
4
6
  'alignWide',
@@ -56,6 +58,7 @@ export const DEFINED_BLOCK_SUPPORTS_METADATA = Symbol.for("@wp-typia/block-types
56
58
  const KNOWN_BLOCK_SUPPORT_FEATURES = new Set(BLOCK_SUPPORT_FEATURES);
57
59
  const DEFINE_SUPPORTS_INLINE_OPTION_KEYS = new Set([
58
60
  "allowUnknownFutureKeys",
61
+ "logger",
59
62
  "minVersion",
60
63
  "minWordPress",
61
64
  "onDiagnostic",
@@ -85,21 +88,18 @@ const TOP_LEVEL_COMPATIBILITY_SUPPORT_KEYS = [
85
88
  "shadow",
86
89
  "visibility",
87
90
  ];
88
- function isSupportObject(value) {
89
- return typeof value === "object" && value !== null && !Array.isArray(value);
90
- }
91
91
  function isEnabledSupportValue(value) {
92
92
  return value !== false && value !== null && value !== undefined;
93
93
  }
94
94
  function isEnabledTopLevelSupportValue(value) {
95
- if (!isSupportObject(value)) {
96
- return isEnabledSupportValue(value);
95
+ if (!isObjectRecord(value)) {
96
+ return isNonArrayObject(value) ? false : isEnabledSupportValue(value);
97
97
  }
98
98
  return Object.entries(value).some(([key, nestedValue]) => !key.startsWith("__experimental") &&
99
99
  isEnabledSupportValue(nestedValue));
100
100
  }
101
101
  function hasEnabledNestedSupport(section, key) {
102
- return isSupportObject(section) && isEnabledSupportValue(section[key]);
102
+ return isObjectRecord(section) && isEnabledSupportValue(section[key]);
103
103
  }
104
104
  function addCompatibilityFeature(features, seen, feature) {
105
105
  const id = `blockSupports.${feature}`;
@@ -133,7 +133,7 @@ export function collectBlockSupportsCompatibilityFeatures(supports) {
133
133
  }
134
134
  }
135
135
  const color = supports.color;
136
- if (isSupportObject(color)) {
136
+ if (isObjectRecord(color)) {
137
137
  for (const key of COLOR_COMPATIBILITY_SUPPORT_KEYS) {
138
138
  if (isEnabledSupportValue(color[key])) {
139
139
  addCompatibilityFeature(features, seen, `color.${key}`);
@@ -190,24 +190,14 @@ export function createBlockSupportsCompatibilityManifest(supports, settings = {}
190
190
  const compatibilitySettings = resolveDefineSupportsSettings({}, settings);
191
191
  return createWordPressBlockApiCompatibilityManifest(collectBlockSupportsCompatibilityFeatures(supports), compatibilitySettings);
192
192
  }
193
- function handleDefineSupportsDiagnostics(diagnostics, onDiagnostic) {
194
- const errors = diagnostics.filter((diagnostic) => diagnostic.severity === "error");
195
- if (errors.length > 0) {
196
- throw new Error([
197
- "WordPress block supports compatibility check failed:",
198
- ...errors.map((diagnostic) => `- ${diagnostic.message}`),
199
- ].join("\n"));
200
- }
201
- for (const diagnostic of diagnostics) {
202
- if (onDiagnostic) {
203
- onDiagnostic(diagnostic);
204
- continue;
205
- }
206
- console.warn(`[wp-typia] ${diagnostic.message}`);
207
- }
193
+ function handleDefineSupportsDiagnostics(diagnostics, onDiagnostic, logger) {
194
+ handleDiagnostics(diagnostics, onDiagnostic, {
195
+ failureHeading: "WordPress block supports compatibility check failed:",
196
+ logger,
197
+ });
208
198
  }
209
199
  export function getDefinedSupportsCompatibilityManifest(supports) {
210
- return isSupportObject(supports)
200
+ return isObjectRecord(supports)
211
201
  ? supports[DEFINED_BLOCK_SUPPORTS_METADATA]?.manifest
212
202
  : undefined;
213
203
  }
@@ -215,7 +205,7 @@ export function defineSupports(supports, options = {}) {
215
205
  const { inlineOptions, supports: normalizedSupports } = splitDefineSupportsInput(supports);
216
206
  const settings = resolveDefineSupportsSettings(inlineOptions, options);
217
207
  const manifest = createBlockSupportsCompatibilityManifest(normalizedSupports, settings);
218
- handleDefineSupportsDiagnostics(manifest.diagnostics, options.onDiagnostic ?? inlineOptions.onDiagnostic);
208
+ handleDefineSupportsDiagnostics(manifest.diagnostics, options.onDiagnostic ?? inlineOptions.onDiagnostic, options.logger ?? inlineOptions.logger);
219
209
  Object.defineProperty(normalizedSupports, DEFINED_BLOCK_SUPPORTS_METADATA, {
220
210
  configurable: false,
221
211
  enumerable: false,
@@ -1,5 +1,6 @@
1
1
  import type { BlockAttributes, BlockVariation, BlockVariationScope } from "./registration.js";
2
2
  import { type WordPressBlockApiCompatibilityDiagnostic, type WordPressBlockApiCompatibilityFeature, type WordPressBlockApiCompatibilityManifest, type WordPressCompatibilitySettings, type WordPressVersion } from "./compatibility.js";
3
+ import { type DiagnosticLogger } from "./shared/diagnostics.js";
3
4
  export type BlockVariationAttributeMap<TAttributes extends BlockAttributes = BlockAttributes> = Partial<TAttributes> & BlockAttributes;
4
5
  export type BlockVariationInnerBlockTemplate = readonly [
5
6
  name: string,
@@ -20,6 +21,7 @@ export interface BlockVariationDefinition<TAttributes extends BlockAttributes =
20
21
  }
21
22
  export interface DefineVariationInlineOptions {
22
23
  readonly allowMissingIsActive?: boolean;
24
+ readonly logger?: DiagnosticLogger<BlockVariationDiagnostic>;
23
25
  readonly minVersion?: WordPressVersion;
24
26
  readonly minWordPress?: WordPressVersion;
25
27
  readonly onDiagnostic?: (diagnostic: BlockVariationDiagnostic) => void;
@@ -28,6 +30,7 @@ export interface DefineVariationInlineOptions {
28
30
  }
29
31
  export interface DefineVariationOptions extends WordPressCompatibilitySettings {
30
32
  readonly allowMissingIsActive?: boolean;
33
+ readonly logger?: DiagnosticLogger<BlockVariationDiagnostic>;
31
34
  readonly minWordPress?: WordPressVersion;
32
35
  readonly onDiagnostic?: (diagnostic: BlockVariationDiagnostic) => void;
33
36
  readonly requireIsActive?: boolean;
@@ -1,8 +1,12 @@
1
1
  import { createWordPressBlockApiCompatibilityManifest, } from "./compatibility.js";
2
+ import { getDiagnosticSeverity, handleDiagnostics, } from "./shared/diagnostics.js";
3
+ import { isObjectRecord } from "./shared/object-utils.js";
4
+ import { normalizeStaticRegistrationValue } from "./shared/static-registration.js";
2
5
  export const DEFINED_BLOCK_VARIATION_METADATA = Symbol.for("@wp-typia/block-types/defined-variation");
3
6
  export const DEFINED_BLOCK_VARIATIONS_METADATA = Symbol.for("@wp-typia/block-types/defined-variations");
4
7
  const DEFINE_VARIATION_INLINE_OPTION_KEYS = new Set([
5
8
  "allowMissingIsActive",
9
+ "logger",
6
10
  "minVersion",
7
11
  "minWordPress",
8
12
  "onDiagnostic",
@@ -14,9 +18,6 @@ const STABLE_VARIATION_MARKER_ATTRIBUTES = [
14
18
  "namespace",
15
19
  "wpTypiaVariation",
16
20
  ];
17
- function isObjectRecord(value) {
18
- return typeof value === "object" && value !== null && !Array.isArray(value);
19
- }
20
21
  function splitDefineVariationInput(variation) {
21
22
  const inlineOptions = {};
22
23
  const normalizedVariation = {};
@@ -54,12 +55,10 @@ function resolveDefineVariationSettings(inlineOptions, options) {
54
55
  requireIsActive: options.requireIsActive ?? inlineOptions.requireIsActive ?? true,
55
56
  strict,
56
57
  },
58
+ logger: options.logger ?? inlineOptions.logger,
57
59
  onDiagnostic: options.onDiagnostic ?? inlineOptions.onDiagnostic,
58
60
  };
59
61
  }
60
- function getDiagnosticSeverity(strict) {
61
- return strict ? "error" : "warning";
62
- }
63
62
  export function collectBlockVariationCompatibilityFeatures() {
64
63
  return [
65
64
  {
@@ -124,21 +123,11 @@ function createVariationDiagnostics(blockName, variation, options) {
124
123
  }
125
124
  return diagnostics;
126
125
  }
127
- function handleVariationDiagnostics(diagnostics, onDiagnostic) {
128
- const errors = diagnostics.filter((diagnostic) => diagnostic.severity === "error");
129
- if (errors.length > 0) {
130
- throw new Error([
131
- "WordPress block variation check failed:",
132
- ...errors.map((diagnostic) => `- ${diagnostic.message}`),
133
- ].join("\n"));
134
- }
135
- for (const diagnostic of diagnostics) {
136
- if (onDiagnostic) {
137
- onDiagnostic(diagnostic);
138
- continue;
139
- }
140
- console.warn(`[wp-typia] ${diagnostic.message}`);
141
- }
126
+ function handleVariationDiagnostics(diagnostics, onDiagnostic, logger) {
127
+ handleDiagnostics(diagnostics, onDiagnostic, {
128
+ failureHeading: "WordPress block variation check failed:",
129
+ logger,
130
+ });
142
131
  }
143
132
  export function getDefinedVariationMetadata(variation) {
144
133
  return isObjectRecord(variation)
@@ -164,7 +153,7 @@ export function defineVariation(blockName, variation, options = {}) {
164
153
  ...manifest.diagnostics,
165
154
  ...createVariationDiagnostics(blockName, normalizedVariation, resolved.diagnostics),
166
155
  ];
167
- handleVariationDiagnostics(diagnostics, resolved.onDiagnostic);
156
+ handleVariationDiagnostics(diagnostics, resolved.onDiagnostic, resolved.logger);
168
157
  Object.defineProperty(normalizedVariation, DEFINED_BLOCK_VARIATION_METADATA, {
169
158
  configurable: false,
170
159
  enumerable: false,
@@ -234,7 +223,7 @@ export function defineVariations(variations, options = {}) {
234
223
  ...variationDiagnostics,
235
224
  ...collectionDiagnostics,
236
225
  ];
237
- handleVariationDiagnostics(collectionDiagnostics, options.onDiagnostic);
226
+ handleVariationDiagnostics(collectionDiagnostics, options.onDiagnostic, options.logger);
238
227
  const normalizedVariations = [...variations];
239
228
  Object.defineProperty(normalizedVariations, DEFINED_BLOCK_VARIATIONS_METADATA, {
240
229
  configurable: false,
@@ -247,41 +236,12 @@ export function defineVariations(variations, options = {}) {
247
236
  });
248
237
  return normalizedVariations;
249
238
  }
250
- function normalizeStaticRegistrationValue(value, path) {
251
- if (value === undefined) {
252
- return undefined;
253
- }
254
- if (value === null ||
255
- typeof value === "boolean" ||
256
- typeof value === "number" ||
257
- typeof value === "string") {
258
- return value;
259
- }
260
- if (typeof value === "function") {
261
- throw new Error(`Cannot generate static variation registration code for function value at ${path}.`);
262
- }
263
- if (typeof value === "bigint" || typeof value === "symbol") {
264
- throw new Error(`Cannot generate static variation registration code for ${typeof value} value at ${path}.`);
265
- }
266
- if (Array.isArray(value)) {
267
- return value.map((entry, index) => normalizeStaticRegistrationValue(entry, `${path}[${index}]`));
268
- }
269
- if (isObjectRecord(value)) {
270
- const normalized = {};
271
- for (const [key, nestedValue] of Object.entries(value)) {
272
- const nextValue = normalizeStaticRegistrationValue(nestedValue, `${path}.${key}`);
273
- if (nextValue !== undefined) {
274
- normalized[key] = nextValue;
275
- }
276
- }
277
- return normalized;
278
- }
279
- throw new Error(`Cannot generate static variation registration code for unsupported value at ${path}.`);
280
- }
281
239
  export function createStaticBlockVariationRegistrationSource(variations, options = {}) {
282
240
  const importSource = options.importSource ?? "@wordpress/blocks";
283
241
  const functionName = options.functionName ?? "registerWpTypiaBlockVariations";
284
- const entries = createBlockVariationRegistrationPlan(variations).map((entry, index) => normalizeStaticRegistrationValue(entry, `variations[${index}]`));
242
+ const entries = createBlockVariationRegistrationPlan(variations).map((entry, index) => normalizeStaticRegistrationValue(entry, `variations[${index}]`, {
243
+ description: "variation",
244
+ }));
285
245
  const serializedEntries = JSON.stringify(entries, null, 2);
286
246
  return [
287
247
  `import { registerBlockVariation } from ${JSON.stringify(importSource)};`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wp-typia/block-types",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Shared WordPress block semantic types derived from Gutenberg and unofficial declarations",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",