@wp-typia/project-tools 0.18.0 → 0.19.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.
- package/dist/runtime/alternate-render-targets.d.ts +5 -0
- package/dist/runtime/alternate-render-targets.js +29 -0
- package/dist/runtime/block-generator-service-core.d.ts +1 -1
- package/dist/runtime/block-generator-service-core.js +11 -7
- package/dist/runtime/block-generator-service-spec.d.ts +8 -1
- package/dist/runtime/block-generator-service-spec.js +248 -2
- package/dist/runtime/built-in-block-artifacts.js +3 -1
- package/dist/runtime/built-in-block-code-artifacts.js +3 -1
- package/dist/runtime/built-in-block-code-templates/compound-child.d.ts +1 -1
- package/dist/runtime/built-in-block-code-templates/compound-child.js +14 -9
- package/dist/runtime/built-in-block-code-templates/compound-parent.d.ts +2 -2
- package/dist/runtime/built-in-block-code-templates/compound-parent.js +100 -43
- package/dist/runtime/built-in-block-code-templates/compound-persistence.d.ts +1 -1
- package/dist/runtime/built-in-block-code-templates/compound-persistence.js +11 -8
- package/dist/runtime/built-in-block-non-ts-artifacts.js +505 -2
- package/dist/runtime/cli-add-block.d.ts +4 -1
- package/dist/runtime/cli-add-block.js +66 -31
- package/dist/runtime/cli-add-shared.d.ts +3 -1
- package/dist/runtime/cli-add-shared.js +12 -12
- package/dist/runtime/cli-core.d.ts +2 -0
- package/dist/runtime/cli-core.js +1 -0
- package/dist/runtime/cli-diagnostics.d.ts +26 -0
- package/dist/runtime/cli-diagnostics.js +107 -0
- package/dist/runtime/cli-doctor-workspace.js +4 -4
- package/dist/runtime/cli-help.js +4 -3
- package/dist/runtime/cli-scaffold.d.ts +3 -1
- package/dist/runtime/cli-scaffold.js +91 -15
- package/dist/runtime/cli-templates.js +26 -1
- package/dist/runtime/cli-validation.d.ts +66 -0
- package/dist/runtime/cli-validation.js +92 -0
- package/dist/runtime/compound-inner-blocks.d.ts +78 -0
- package/dist/runtime/compound-inner-blocks.js +88 -0
- package/dist/runtime/index.d.ts +5 -1
- package/dist/runtime/index.js +3 -1
- package/dist/runtime/migration-command-surface.js +2 -0
- package/dist/runtime/package-versions.d.ts +1 -0
- package/dist/runtime/package-versions.js +12 -0
- package/dist/runtime/scaffold-answer-resolution.js +10 -6
- package/dist/runtime/scaffold-bootstrap.js +5 -1
- package/dist/runtime/scaffold-documents.js +29 -6
- package/dist/runtime/scaffold-identifiers.d.ts +17 -0
- package/dist/runtime/scaffold-identifiers.js +22 -0
- package/dist/runtime/scaffold-onboarding.js +21 -13
- package/dist/runtime/scaffold-template-variable-groups.d.ts +154 -0
- package/dist/runtime/scaffold-template-variable-groups.js +13 -0
- package/dist/runtime/scaffold-template-variables.js +80 -1
- package/dist/runtime/scaffold.d.ts +21 -2
- package/dist/runtime/scaffold.js +12 -5
- package/dist/runtime/temp-roots.d.ts +44 -0
- package/dist/runtime/temp-roots.js +129 -0
- package/dist/runtime/template-builtins.js +4 -6
- package/dist/runtime/template-registry.d.ts +8 -0
- package/dist/runtime/template-registry.js +34 -1
- package/dist/runtime/template-source-external.d.ts +1 -0
- package/dist/runtime/template-source-external.js +4 -7
- package/dist/runtime/template-source-remote.js +44 -23
- package/dist/runtime/template-source-seeds.js +3 -9
- package/dist/runtime/template-source.d.ts +2 -3
- package/dist/runtime/template-source.js +13 -5
- package/dist/runtime/workspace-project.js +1 -1
- package/package.json +12 -2
- package/templates/_shared/compound/core/scripts/add-compound-child.ts.mustache +318 -18
|
@@ -9,8 +9,7 @@ import {
|
|
|
9
9
|
import { Notice, PanelBody, ToggleControl } from '@wordpress/components';
|
|
10
10
|
|
|
11
11
|
import {
|
|
12
|
-
|
|
13
|
-
DEFAULT_CHILD_TEMPLATE,
|
|
12
|
+
getRootInnerBlocksPropsOptions,
|
|
14
13
|
} from './children';
|
|
15
14
|
import { useTypiaValidation } from './hooks';
|
|
16
15
|
import type { {{pascalCase}}Attributes } from './types';
|
|
@@ -20,6 +19,14 @@ import {
|
|
|
20
19
|
} from './validators';
|
|
21
20
|
|
|
22
21
|
type EditProps = BlockEditProps< {{pascalCase}}Attributes >;
|
|
22
|
+
type CompoundInnerBlocksProps = Parameters< typeof InnerBlocks >[ 0 ] & {
|
|
23
|
+
defaultBlock?: [ string, Record< string, unknown > ];
|
|
24
|
+
directInsert?: boolean;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const TypedInnerBlocks = InnerBlocks as unknown as (
|
|
28
|
+
props: CompoundInnerBlocksProps
|
|
29
|
+
) => ReturnType< typeof InnerBlocks >;
|
|
23
30
|
|
|
24
31
|
export default function Edit( {
|
|
25
32
|
attributes,
|
|
@@ -33,6 +40,7 @@ export default function Edit( {
|
|
|
33
40
|
const blockProps = useBlockProps( {
|
|
34
41
|
className: '{{cssClassName}}',
|
|
35
42
|
} );
|
|
43
|
+
const rootInnerBlocksPropsOptions = getRootInnerBlocksPropsOptions();
|
|
36
44
|
|
|
37
45
|
return (
|
|
38
46
|
<>
|
|
@@ -80,12 +88,7 @@ export default function Edit( {
|
|
|
80
88
|
</Notice>
|
|
81
89
|
) }
|
|
82
90
|
<div className="{{cssClassName}}__items">
|
|
83
|
-
<
|
|
84
|
-
allowedBlocks={ ALLOWED_CHILD_BLOCKS }
|
|
85
|
-
renderAppender={ InnerBlocks.ButtonBlockAppender }
|
|
86
|
-
template={ DEFAULT_CHILD_TEMPLATE }
|
|
87
|
-
templateLock={ false }
|
|
88
|
-
/>
|
|
91
|
+
<TypedInnerBlocks { ...rootInnerBlocksPropsOptions } />
|
|
89
92
|
</div>
|
|
90
93
|
</div>
|
|
91
94
|
</>
|
|
@@ -200,6 +203,7 @@ export const sanitize{{pascalCase}}Attributes =
|
|
|
200
203
|
export const createAttributeUpdater = scaffoldValidators.createAttributeUpdater;
|
|
201
204
|
`;
|
|
202
205
|
export const COMPOUND_CHILDREN_TEMPLATE = `import type { BlockTemplate } from '@wp-typia/block-types/blocks/registration';
|
|
206
|
+
import { InnerBlocks } from '@wordpress/block-editor';
|
|
203
207
|
|
|
204
208
|
export const DEFAULT_CHILD_BLOCK_NAME = '{{namespace}}/{{slugKebabCase}}-item';
|
|
205
209
|
|
|
@@ -216,6 +220,31 @@ export interface CompoundChildSpec {
|
|
|
216
220
|
\ttitle: string;
|
|
217
221
|
}
|
|
218
222
|
|
|
223
|
+
export interface CompoundInnerBlocksConfig {
|
|
224
|
+
\tdefaultBlock?: [ string, Record< string, unknown > ];
|
|
225
|
+
\tdirectInsert: boolean;
|
|
226
|
+
\torientation?: 'horizontal' | 'vertical';
|
|
227
|
+
\ttemplate?: BlockTemplate;
|
|
228
|
+
\ttemplateLock: false | 'insert' | 'all';
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export type CompoundInnerBlocksPropsOptions = CompoundInnerBlocksConfig & {
|
|
232
|
+
\trenderAppender?: typeof InnerBlocks.ButtonBlockAppender;
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
export const ROOT_INNER_BLOCKS_PRESET_ID = '{{compoundInnerBlocksPreset}}';
|
|
236
|
+
export const ROOT_INNER_BLOCKS_PRESET_DESCRIPTION =
|
|
237
|
+
\t'{{compoundInnerBlocksPresetDescription}}';
|
|
238
|
+
|
|
239
|
+
const BASE_INNER_BLOCKS_CONFIG: Omit<
|
|
240
|
+
\tCompoundInnerBlocksConfig,
|
|
241
|
+
\t'defaultBlock' | 'template'
|
|
242
|
+
> = {
|
|
243
|
+
\tdirectInsert: {{compoundInnerBlocksDirectInsert}},
|
|
244
|
+
\torientation: {{compoundInnerBlocksOrientationExpression}},
|
|
245
|
+
\ttemplateLock: {{compoundInnerBlocksTemplateLockExpression}},
|
|
246
|
+
};
|
|
247
|
+
|
|
219
248
|
const ROOT_BLOCK_NAME = '{{namespace}}/{{slugKebabCase}}';
|
|
220
249
|
|
|
221
250
|
export const COMPOUND_CHILD_SPECS: CompoundChildSpec[] = [
|
|
@@ -243,10 +272,6 @@ export const COMPOUND_CHILD_SPECS: CompoundChildSpec[] = [
|
|
|
243
272
|
\t// add-child: insert new child specs here
|
|
244
273
|
];
|
|
245
274
|
|
|
246
|
-
function getChildSpecByKey( key: string ): CompoundChildSpec | undefined {
|
|
247
|
-
\treturn COMPOUND_CHILD_SPECS.find( ( spec ) => spec.key === key );
|
|
248
|
-
}
|
|
249
|
-
|
|
250
275
|
function buildTemplateEntriesForSpec( spec: CompoundChildSpec ): BlockTemplate {
|
|
251
276
|
\tconst nestedTemplate = buildNestedTemplateForKey( spec.key );
|
|
252
277
|
|
|
@@ -265,68 +290,100 @@ function buildNestedTemplateForKey( key: string ): BlockTemplate {
|
|
|
265
290
|
\t).flatMap( ( spec ) => buildTemplateEntriesForSpec( spec ) );
|
|
266
291
|
}
|
|
267
292
|
|
|
268
|
-
export const ALLOWED_CHILD_BLOCKS = COMPOUND_CHILD_SPECS.filter(
|
|
269
|
-
\t( spec ) => spec.placement === 'root'
|
|
270
|
-
).map( ( spec ) => spec.blockName );
|
|
271
|
-
|
|
272
293
|
export const DEFAULT_CHILD_TEMPLATE: BlockTemplate =
|
|
273
294
|
\tCOMPOUND_CHILD_SPECS.filter( ( spec ) => spec.placement === 'root' ).flatMap(
|
|
274
295
|
\t\t( spec ) => buildTemplateEntriesForSpec( spec )
|
|
275
296
|
\t);
|
|
276
297
|
|
|
298
|
+
function buildDefaultBlockEntry(
|
|
299
|
+
\ttemplate?: BlockTemplate
|
|
300
|
+
): [ string, Record< string, unknown > ] | undefined {
|
|
301
|
+
\tif (
|
|
302
|
+
\t\t! BASE_INNER_BLOCKS_CONFIG.directInsert ||
|
|
303
|
+
\t\t! Array.isArray( template ) ||
|
|
304
|
+
\t\ttemplate.length === 0 ||
|
|
305
|
+
\t\ttypeof template[ 0 ]?.[ 0 ] !== 'string'
|
|
306
|
+
\t) {
|
|
307
|
+
\t\treturn undefined;
|
|
308
|
+
\t}
|
|
309
|
+
|
|
310
|
+
\treturn [ template[ 0 ][ 0 ], {} ];
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function buildInnerBlocksPropsOptions(
|
|
314
|
+
\tconfig: CompoundInnerBlocksConfig
|
|
315
|
+
): CompoundInnerBlocksPropsOptions {
|
|
316
|
+
\treturn {
|
|
317
|
+
\t\t...config,
|
|
318
|
+
\t\trenderAppender:
|
|
319
|
+
\t\t\tconfig.templateLock === 'all'
|
|
320
|
+
\t\t\t\t? undefined
|
|
321
|
+
\t\t\t\t: InnerBlocks.ButtonBlockAppender,
|
|
322
|
+
\t};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export function getRootInnerBlocksConfig(): CompoundInnerBlocksConfig {
|
|
326
|
+
\treturn {
|
|
327
|
+
\t\t...BASE_INNER_BLOCKS_CONFIG,
|
|
328
|
+
\t\tdefaultBlock: buildDefaultBlockEntry( DEFAULT_CHILD_TEMPLATE ),
|
|
329
|
+
\t\ttemplate: DEFAULT_CHILD_TEMPLATE,
|
|
330
|
+
\t};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export function getRootInnerBlocksPropsOptions(): CompoundInnerBlocksPropsOptions {
|
|
334
|
+
\treturn buildInnerBlocksPropsOptions( getRootInnerBlocksConfig() );
|
|
335
|
+
}
|
|
336
|
+
|
|
277
337
|
export function getChildSpec( blockName: string ): CompoundChildSpec | undefined {
|
|
278
338
|
\treturn COMPOUND_CHILD_SPECS.find( ( spec ) => spec.blockName === blockName );
|
|
279
339
|
}
|
|
280
340
|
|
|
281
|
-
export function
|
|
341
|
+
export function getChildTemplate(
|
|
282
342
|
\tblockName: string
|
|
283
|
-
):
|
|
343
|
+
): BlockTemplate | undefined {
|
|
284
344
|
\tconst childSpec = getChildSpec( blockName );
|
|
285
345
|
\tif ( ! childSpec ) {
|
|
286
346
|
\t\treturn undefined;
|
|
287
347
|
\t}
|
|
288
348
|
|
|
289
|
-
\tconst
|
|
290
|
-
\
|
|
291
|
-
\t\
|
|
292
|
-
\t\t\tspec.ancestorKeys[ spec.ancestorKeys.length - 1 ] === childSpec.key
|
|
293
|
-
\t).map( ( spec ) => spec.blockName );
|
|
294
|
-
|
|
295
|
-
\tif ( directDescendants.length > 0 ) {
|
|
296
|
-
\t\treturn directDescendants;
|
|
349
|
+
\tconst nestedTemplate = buildNestedTemplateForKey( childSpec.key );
|
|
350
|
+
\tif ( nestedTemplate.length > 0 ) {
|
|
351
|
+
\t\treturn nestedTemplate;
|
|
297
352
|
\t}
|
|
298
353
|
|
|
299
354
|
\treturn childSpec.container ? [] : undefined;
|
|
300
355
|
}
|
|
301
356
|
|
|
302
|
-
export function
|
|
357
|
+
export function getChildInnerBlocksConfig(
|
|
303
358
|
\tblockName: string
|
|
304
|
-
):
|
|
359
|
+
): CompoundInnerBlocksConfig | undefined {
|
|
305
360
|
\tconst childSpec = getChildSpec( blockName );
|
|
306
|
-
\tif ( ! childSpec
|
|
361
|
+
\tif ( ! childSpec ) {
|
|
307
362
|
\t\treturn undefined;
|
|
308
363
|
\t}
|
|
309
364
|
|
|
310
|
-
\
|
|
311
|
-
\t\tconst ancestorSpec = getChildSpecByKey( key );
|
|
312
|
-
\t\treturn ancestorSpec ? [ ancestorSpec.blockName ] : [];
|
|
313
|
-
\t} );
|
|
314
|
-
}
|
|
365
|
+
\tconst template = getChildTemplate( blockName );
|
|
315
366
|
|
|
316
|
-
|
|
317
|
-
\tblockName: string
|
|
318
|
-
): BlockTemplate | undefined {
|
|
319
|
-
\tconst childSpec = getChildSpec( blockName );
|
|
320
|
-
\tif ( ! childSpec ) {
|
|
367
|
+
\tif ( ! childSpec.container && ! template ) {
|
|
321
368
|
\t\treturn undefined;
|
|
322
369
|
\t}
|
|
323
370
|
|
|
324
|
-
\
|
|
325
|
-
\
|
|
326
|
-
\t\
|
|
371
|
+
\treturn {
|
|
372
|
+
\t\t...BASE_INNER_BLOCKS_CONFIG,
|
|
373
|
+
\t\tdefaultBlock: buildDefaultBlockEntry( template ),
|
|
374
|
+
\t\ttemplate,
|
|
375
|
+
\t};
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
export function getChildInnerBlocksPropsOptions(
|
|
379
|
+
\tblockName: string
|
|
380
|
+
): CompoundInnerBlocksPropsOptions | undefined {
|
|
381
|
+
\tconst config = getChildInnerBlocksConfig( blockName );
|
|
382
|
+
\tif ( ! config ) {
|
|
383
|
+
\t\treturn undefined;
|
|
327
384
|
\t}
|
|
328
385
|
|
|
329
|
-
\treturn
|
|
386
|
+
\treturn buildInnerBlocksPropsOptions( config );
|
|
330
387
|
}
|
|
331
388
|
|
|
332
389
|
export function hasNestedChildBlocks( blockName: string ): boolean {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const COMPOUND_PERSISTENCE_PARENT_EDIT_TEMPLATE = "import type { BlockEditProps } from '@wp-typia/block-types/blocks/registration';\nimport { __ } from '@wordpress/i18n';\nimport {\n\tInspectorControls,\n\tInnerBlocks,\n\tRichText,\n\tuseBlockProps,\n} from '@wordpress/block-editor';\nimport {\n\tNotice,\n\tPanelBody,\n\tTextControl,\n\tToggleControl,\n} from '@wordpress/components';\n\nimport {\n\
|
|
1
|
+
export declare const COMPOUND_PERSISTENCE_PARENT_EDIT_TEMPLATE = "import type { BlockEditProps } from '@wp-typia/block-types/blocks/registration';\nimport { __ } from '@wordpress/i18n';\nimport {\n\tInspectorControls,\n\tInnerBlocks,\n\tRichText,\n\tuseBlockProps,\n} from '@wordpress/block-editor';\nimport {\n\tNotice,\n\tPanelBody,\n\tTextControl,\n\tToggleControl,\n} from '@wordpress/components';\n\nimport {\n\tgetRootInnerBlocksPropsOptions,\n} from './children';\nimport { useTypiaValidation } from './hooks';\nimport type { {{pascalCase}}Attributes } from './types';\nimport {\n\tcreateAttributeUpdater,\n\tvalidate{{pascalCase}}Attributes,\n} from './validators';\n\ntype EditProps = BlockEditProps< {{pascalCase}}Attributes >;\ntype CompoundInnerBlocksProps = Parameters< typeof InnerBlocks >[ 0 ] & {\n\tdefaultBlock?: [ string, Record< string, unknown > ];\n\tdirectInsert?: boolean;\n};\n\nconst TypedInnerBlocks = InnerBlocks as unknown as (\n\tprops: CompoundInnerBlocksProps\n) => ReturnType< typeof InnerBlocks >;\n\nexport default function Edit( {\n\tattributes,\n\tsetAttributes,\n}: EditProps ) {\n\tconst { errorMessages, isValid } = useTypiaValidation(\n\t\tattributes,\n\t\tvalidate{{pascalCase}}Attributes\n\t);\n\tconst updateAttribute = createAttributeUpdater( attributes, setAttributes );\n\tconst blockProps = useBlockProps( {\n\t\tclassName: '{{cssClassName}}',\n\t} );\n\tconst rootInnerBlocksPropsOptions = getRootInnerBlocksPropsOptions();\n\n\treturn (\n\t\t<>\n\t\t\t<InspectorControls>\n\t\t\t\t<PanelBody title={ __( 'Compound Settings', '{{textDomain}}' ) }>\n\t\t\t\t\t<ToggleControl\n\t\t\t\t\t\tlabel={ __( 'Show dividers between items', '{{textDomain}}' ) }\n\t\t\t\t\t\tchecked={ attributes.showDividers ?? true }\n\t\t\t\t\t\tonChange={ ( value ) => updateAttribute( 'showDividers', value ) }\n\t\t\t\t\t/>\n\t\t\t\t\t<ToggleControl\n\t\t\t\t\t\tlabel={ __( 'Show persisted count', '{{textDomain}}' ) }\n\t\t\t\t\t\tchecked={ attributes.showCount ?? true }\n\t\t\t\t\t\tonChange={ ( value ) => updateAttribute( 'showCount', value ) }\n\t\t\t\t\t/>\n\t\t\t\t\t<TextControl\n\t\t\t\t\t\tlabel={ __( 'Button label', '{{textDomain}}' ) }\n\t\t\t\t\t\tvalue={ attributes.buttonLabel ?? 'Persist Count' }\n\t\t\t\t\t\tonChange={ ( buttonLabel ) => updateAttribute( 'buttonLabel', buttonLabel ) }\n\t\t\t\t\t/>\n\t\t\t\t\t<TextControl\n\t\t\t\t\t\tlabel={ __( 'Resource key', '{{textDomain}}' ) }\n\t\t\t\t\t\tvalue={ attributes.resourceKey ?? '' }\n\t\t\t\t\t\tonChange={ ( resourceKey ) => updateAttribute( 'resourceKey', resourceKey ) }\n\t\t\t\t\t\thelp={ __( 'Stable key used by the persisted counter endpoint.', '{{textDomain}}' ) }\n\t\t\t\t\t/>\n\t\t\t\t\t<Notice status=\"info\" isDismissible={ false }>\n\t\t\t\t\t\t{ __( 'Storage mode: {{dataStorageMode}}', '{{textDomain}}' ) }\n\t\t\t\t\t</Notice>\n\t\t\t\t\t<Notice status=\"info\" isDismissible={ false }>\n\t\t\t\t\t\t{ __( 'Persistence policy: {{persistencePolicy}}', '{{textDomain}}' ) }\n\t\t\t\t\t</Notice>\n\t\t\t\t</PanelBody>\n\t\t\t\t{ ! isValid && (\n\t\t\t\t\t<PanelBody title={ __( 'Validation Errors', '{{textDomain}}' ) } initialOpen>\n\t\t\t\t\t\t{ errorMessages.map( ( error, index ) => (\n\t\t\t\t\t\t\t<Notice key={ index } status=\"error\" isDismissible={ false }>\n\t\t\t\t\t\t\t\t{ error }\n\t\t\t\t\t\t\t</Notice>\n\t\t\t\t\t\t) ) }\n\t\t\t\t\t</PanelBody>\n\t\t\t\t) }\n\t\t\t</InspectorControls>\n\t\t\t<div { ...blockProps }>\n\t\t\t\t<RichText\n\t\t\t\t\ttagName=\"h3\"\n\t\t\t\t\tclassName=\"{{cssClassName}}__heading\"\n\t\t\t\t\tvalue={ attributes.heading }\n\t\t\t\t\tonChange={ ( heading ) => updateAttribute( 'heading', heading ) }\n\t\t\t\t\tplaceholder={ __( {{titleJson}}, '{{textDomain}}' ) }\n\t\t\t\t/>\n\t\t\t\t<RichText\n\t\t\t\t\ttagName=\"p\"\n\t\t\t\t\tclassName=\"{{cssClassName}}__intro\"\n\t\t\t\t\tvalue={ attributes.intro ?? '' }\n\t\t\t\t\tonChange={ ( intro ) => updateAttribute( 'intro', intro ) }\n\t\t\t\t\tplaceholder={ __(\n\t\t\t\t\t\t'Add and reorder internal items inside this compound block.',\n\t\t\t\t\t\t'{{textDomain}}'\n\t\t\t\t\t) }\n\t\t\t\t/>\n\t\t\t\t{ ! isValid && (\n\t\t\t\t\t<Notice status=\"error\" isDismissible={ false }>\n\t\t\t\t\t\t<ul>\n\t\t\t\t\t\t\t{ errorMessages.map( ( error, index ) => <li key={ index }>{ error }</li> ) }\n\t\t\t\t\t\t</ul>\n\t\t\t\t\t</Notice>\n\t\t\t\t) }\n\t\t\t\t<p className=\"{{cssClassName}}__meta\">\n\t\t\t\t\t{ __( 'Resource key:', '{{textDomain}}' ) } { attributes.resourceKey || '\u2014' }\n\t\t\t\t</p>\n\t\t\t\t<div className=\"{{cssClassName}}__items\">\n\t\t\t\t\t<TypedInnerBlocks { ...rootInnerBlocksPropsOptions } />\n\t\t\t\t</div>\n\t\t\t</div>\n\t\t</>\n\t);\n}\n";
|
|
2
2
|
export declare const COMPOUND_PERSISTENCE_PARENT_SAVE_TEMPLATE = "export default function Save() {\n\treturn null;\n}\n";
|
|
3
3
|
export declare const COMPOUND_PERSISTENCE_PARENT_VALIDATORS_TEMPLATE = "import typia from 'typia';\nimport currentManifest from './manifest-defaults-document';\nimport type {\n\t{{pascalCase}}Attributes,\n\t{{pascalCase}}ValidationResult,\n} from './types';\nimport { generateResourceKey } from '@wp-typia/block-runtime/identifiers';\nimport { createTemplateValidatorToolkit } from '../../validator-toolkit';\n\nconst scaffoldValidators = createTemplateValidatorToolkit< {{pascalCase}}Attributes >( {\n\tassert: typia.createAssert< {{pascalCase}}Attributes >(),\n\tclone: typia.misc.createClone< {{pascalCase}}Attributes >() as (\n\t\tvalue: {{pascalCase}}Attributes,\n\t) => {{pascalCase}}Attributes,\n\tis: typia.createIs< {{pascalCase}}Attributes >(),\n\tmanifest: currentManifest,\n\tprune: typia.misc.createPrune< {{pascalCase}}Attributes >(),\n\trandom: typia.createRandom< {{pascalCase}}Attributes >() as (\n\t\t...args: unknown[]\n\t) => {{pascalCase}}Attributes,\n\tfinalize: ( normalized ) => ( {\n\t\t...normalized,\n\t\tresourceKey:\n\t\t\tnormalized.resourceKey && normalized.resourceKey.length > 0\n\t\t\t\t? normalized.resourceKey\n\t\t\t\t: generateResourceKey( '{{slugKebabCase}}' ),\n\t} ),\n\tvalidate: typia.createValidate< {{pascalCase}}Attributes >(),\n} );\n\nexport const validators = scaffoldValidators.validators;\n\nexport const validate{{pascalCase}}Attributes =\n\tscaffoldValidators.validateAttributes as (\n\t\tattributes: unknown\n\t) => {{pascalCase}}ValidationResult;\n\nexport const sanitize{{pascalCase}}Attributes =\n\tscaffoldValidators.sanitizeAttributes as (\n\t\tattributes: Partial< {{pascalCase}}Attributes >\n\t) => {{pascalCase}}Attributes;\n\nexport const createAttributeUpdater = scaffoldValidators.createAttributeUpdater;\n";
|
|
4
4
|
export declare const COMPOUND_PERSISTENCE_PARENT_INTERACTIVITY_TEMPLATE = "import { getContext, store } from '@wordpress/interactivity';\nimport { generatePublicWriteRequestId } from '@wp-typia/block-runtime/identifiers';\n\nimport { fetchBootstrap, fetchState, writeState } from './api';\nimport type {\n\t{{pascalCase}}ClientState,\n\t{{pascalCase}}Context,\n\t{{pascalCase}}State,\n} from './types';\n\nfunction hasExpiredPublicWriteToken(\n\texpiresAt?: number\n): boolean {\n\treturn (\n\t\ttypeof expiresAt === 'number' &&\n\t\texpiresAt > 0 &&\n\t\tDate.now() >= expiresAt * 1000\n\t);\n}\n\nfunction getWriteBlockedMessage(\n\tcontext: {{pascalCase}}Context\n): string {\n\treturn context.persistencePolicy === 'authenticated'\n\t\t? 'Sign in to persist this counter.'\n\t\t: 'Public writes are temporarily unavailable.';\n}\n\nconst BOOTSTRAP_MAX_ATTEMPTS = 3;\nconst BOOTSTRAP_RETRY_DELAYS_MS = [ 250, 500 ];\n\nasync function waitForBootstrapRetry( delayMs: number ): Promise< void > {\n\tawait new Promise( ( resolve ) => {\n\t\tsetTimeout( resolve, delayMs );\n\t} );\n}\n\nfunction getClientState(\n\tcontext: {{pascalCase}}Context\n): {{pascalCase}}ClientState {\n\tif ( context.client ) {\n\t\treturn context.client;\n\t}\n\n\tcontext.client = {\n\t\tbootstrapError: '',\n\t\twriteExpiry: 0,\n\t\twriteNonce: '',\n\t\twriteToken: '',\n\t};\n\n\treturn context.client;\n}\n\nfunction clearBootstrapError(\n\tcontext: {{pascalCase}}Context,\n\tclientState: {{pascalCase}}ClientState\n): void {\n\tif ( context.error === clientState.bootstrapError ) {\n\t\tcontext.error = '';\n\t}\n\tclientState.bootstrapError = '';\n}\n\nfunction setBootstrapError(\n\tcontext: {{pascalCase}}Context,\n\tclientState: {{pascalCase}}ClientState,\n\tmessage: string\n): void {\n\tclientState.bootstrapError = message;\n\tcontext.error = message;\n}\n\nconst { actions, state } = store( '{{slugKebabCase}}', {\n\tstate: {\n\t\tisHydrated: false,\n\t} as {{pascalCase}}State,\n\n\tactions: {\n\t\tasync loadState() {\n\t\t\tconst context = getContext< {{pascalCase}}Context >();\n\t\t\tif ( context.postId <= 0 || ! context.resourceKey ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tcontext.isLoading = true;\n\t\t\tcontext.error = '';\n\n\t\t\ttry {\n\t\t\t\tconst result = await fetchState( {\n\t\t\t\t\tpostId: context.postId,\n\t\t\t\t\tresourceKey: context.resourceKey,\n\t\t\t\t}, {\n\t\t\t\t\ttransportTarget: 'frontend',\n\t\t\t\t} );\n\t\t\t\tif ( ! result.isValid || ! result.data ) {\n\t\t\t\t\tcontext.error = result.errors[ 0 ]?.expected ?? 'Unable to load counter';\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tcontext.count = result.data.count;\n\t\t\t} catch ( error ) {\n\t\t\t\tcontext.error =\n\t\t\t\t\terror instanceof Error ? error.message : 'Unknown loading error';\n\t\t\t} finally {\n\t\t\t\tcontext.isLoading = false;\n\t\t\t}\n\t\t},\n\t\tasync loadBootstrap() {\n\t\t\tconst context = getContext< {{pascalCase}}Context >();\n\t\t\tconst clientState = getClientState( context );\n\t\t\tif ( context.postId <= 0 || ! context.resourceKey ) {\n\t\t\t\tcontext.bootstrapReady = true;\n\t\t\t\tcontext.canWrite = false;\n\t\t\t\tclientState.bootstrapError = '';\n\t\t\t\tclientState.writeExpiry = 0;\n\t\t\t\tclientState.writeNonce = '';\n\t\t\t\tclientState.writeToken = '';\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tcontext.isBootstrapping = true;\n\n\t\t\tlet bootstrapSucceeded = false;\n\t\t\tlet lastBootstrapError =\n\t\t\t\t'Unable to initialize write access';\n\t\t\tconst includePublicWriteCredentials = {{isPublicPersistencePolicy}};\n\t\t\tconst includeRestNonce = {{isAuthenticatedPersistencePolicy}};\n\n\t\t\tfor ( let attempt = 1; attempt <= BOOTSTRAP_MAX_ATTEMPTS; attempt += 1 ) {\n\t\t\t\ttry {\n\t\t\t\t\tconst result = await fetchBootstrap( {\n\t\t\t\t\t\tpostId: context.postId,\n\t\t\t\t\t\tresourceKey: context.resourceKey,\n\t\t\t\t\t}, {\n\t\t\t\t\t\ttransportTarget: 'frontend',\n\t\t\t\t\t} );\n\t\t\t\t\tif ( ! result.isValid || ! result.data ) {\n\t\t\t\t\t\tlastBootstrapError =\n\t\t\t\t\t\t\tresult.errors[ 0 ]?.expected ??\n\t\t\t\t\t\t\t'Unable to initialize write access';\n\t\t\t\t\t\tif ( attempt < BOOTSTRAP_MAX_ATTEMPTS ) {\n\t\t\t\t\t\t\tawait waitForBootstrapRetry(\n\t\t\t\t\t\t\t\tBOOTSTRAP_RETRY_DELAYS_MS[ attempt - 1 ] ?? 750\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t\tclientState.writeExpiry =\n\t\t\t\t\t\tincludePublicWriteCredentials &&\n\t\t\t\t\t\t'publicWriteExpiresAt' in result.data &&\n\t\t\t\t\t\ttypeof result.data.publicWriteExpiresAt === 'number' &&\n\t\t\t\t\t\tresult.data.publicWriteExpiresAt > 0\n\t\t\t\t\t\t\t? result.data.publicWriteExpiresAt\n\t\t\t\t\t\t\t: 0;\n\t\t\t\t\tclientState.writeToken =\n\t\t\t\t\t\tincludePublicWriteCredentials &&\n\t\t\t\t\t\t'publicWriteToken' in result.data &&\n\t\t\t\t\t\ttypeof result.data.publicWriteToken === 'string' &&\n\t\t\t\t\t\tresult.data.publicWriteToken.length > 0\n\t\t\t\t\t\t\t? result.data.publicWriteToken\n\t\t\t\t\t\t\t: '';\n\t\t\t\t\tclientState.writeNonce =\n\t\t\t\t\t\tincludeRestNonce &&\n\t\t\t\t\t\t'restNonce' in result.data &&\n\t\t\t\t\t\ttypeof result.data.restNonce === 'string' &&\n\t\t\t\t\t\tresult.data.restNonce.length > 0\n\t\t\t\t\t\t\t? result.data.restNonce\n\t\t\t\t\t\t\t: '';\n\t\t\t\t\tcontext.bootstrapReady = true;\n\t\t\t\t\tcontext.canWrite =\n\t\t\t\t\t\tresult.data.canWrite === true &&\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\tcontext.persistencePolicy === 'authenticated'\n\t\t\t\t\t\t\t\t? clientState.writeNonce.length > 0\n\t\t\t\t\t\t\t\t: clientState.writeToken.length > 0 &&\n\t\t\t\t\t\t\t\t\t! hasExpiredPublicWriteToken( clientState.writeExpiry )\n\t\t\t\t\t\t);\n\t\t\t\t\tclearBootstrapError( context, clientState );\n\t\t\t\t\tbootstrapSucceeded = true;\n\t\t\t\t\tbreak;\n\t\t\t\t} catch ( error ) {\n\t\t\t\t\tlastBootstrapError =\n\t\t\t\t\t\terror instanceof Error ? error.message : 'Unknown bootstrap error';\n\t\t\t\t\tif ( attempt < BOOTSTRAP_MAX_ATTEMPTS ) {\n\t\t\t\t\t\tawait waitForBootstrapRetry(\n\t\t\t\t\t\t\tBOOTSTRAP_RETRY_DELAYS_MS[ attempt - 1 ] ?? 750\n\t\t\t\t\t\t);\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif ( ! bootstrapSucceeded ) {\n\t\t\t\tcontext.bootstrapReady = false;\n\t\t\t\tcontext.canWrite = false;\n\t\t\t\tclientState.writeExpiry = 0;\n\t\t\t\tclientState.writeNonce = '';\n\t\t\t\tclientState.writeToken = '';\n\t\t\t\tsetBootstrapError( context, clientState, lastBootstrapError );\n\t\t\t}\n\t\t\tcontext.isBootstrapping = false;\n\t\t},\n\t\tasync increment() {\n\t\t\tconst context = getContext< {{pascalCase}}Context >();\n\t\t\tconst clientState = getClientState( context );\n\t\t\tif ( context.postId <= 0 || ! context.resourceKey ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( ! context.bootstrapReady ) {\n\t\t\t\tawait actions.loadBootstrap();\n\t\t\t}\n\t\t\tif ( ! context.bootstrapReady ) {\n\t\t\t\tcontext.error = 'Write access is still initializing.';\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (\n\t\t\t\tcontext.persistencePolicy === 'public' &&\n\t\t\t\thasExpiredPublicWriteToken( clientState.writeExpiry )\n\t\t\t) {\n\t\t\t\tawait actions.loadBootstrap();\n\t\t\t}\n\t\t\tif (\n\t\t\t\tcontext.persistencePolicy === 'public' &&\n\t\t\t\thasExpiredPublicWriteToken( clientState.writeExpiry )\n\t\t\t) {\n\t\t\t\tcontext.canWrite = false;\n\t\t\t\tcontext.error = getWriteBlockedMessage( context );\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( ! context.canWrite ) {\n\t\t\t\tcontext.error = getWriteBlockedMessage( context );\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tcontext.isSaving = true;\n\t\t\tcontext.error = '';\n\n\t\t\ttry {\n\t\t\t\tconst result = await writeState( {\n\t\t\t\t\tdelta: 1,\n\t\t\t\t\tpostId: context.postId,\n\t\t\t\t\tpublicWriteRequestId:\n\t\t\t\t\t\tcontext.persistencePolicy === 'public'\n\t\t\t\t\t\t\t? generatePublicWriteRequestId()\n\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\tpublicWriteToken:\n\t\t\t\t\t\tcontext.persistencePolicy === 'public' &&\n\t\t\t\t\t\tclientState.writeToken.length > 0\n\t\t\t\t\t\t\t? clientState.writeToken\n\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\tresourceKey: context.resourceKey,\n\t\t\t\t}, {\n\t\t\t\t\trestNonce:\n\t\t\t\t\t\tclientState.writeNonce.length > 0\n\t\t\t\t\t\t\t? clientState.writeNonce\n\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\ttransportTarget: 'frontend',\n\t\t\t\t} );\n\t\t\t\tif ( ! result.isValid || ! result.data ) {\n\t\t\t\t\tcontext.error = result.errors[ 0 ]?.expected ?? 'Unable to update counter';\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tcontext.count = result.data.count;\n\t\t\t\tcontext.storage = result.data.storage;\n\t\t\t} catch ( error ) {\n\t\t\t\tcontext.error =\n\t\t\t\t\terror instanceof Error ? error.message : 'Unknown update error';\n\t\t\t} finally {\n\t\t\t\tcontext.isSaving = false;\n\t\t\t}\n\t\t},\n\t},\n\n\tcallbacks: {\n\t\tinit() {\n\t\t\tconst context = getContext< {{pascalCase}}Context >();\n\t\t\tcontext.client = {\n\t\t\t\tbootstrapError: '',\n\t\t\t\twriteExpiry: 0,\n\t\t\t\twriteNonce: '',\n\t\t\t\twriteToken: '',\n\t\t\t};\n\t\t\tcontext.bootstrapReady = false;\n\t\t\tcontext.canWrite = false;\n\t\t\tcontext.count = 0;\n\t\t\tcontext.error = '';\n\t\t\tcontext.isBootstrapping = false;\n\t\t\tcontext.isLoading = false;\n\t\t\tcontext.isSaving = false;\n\t\t},\n\t\tmounted() {\n\t\t\tstate.isHydrated = true;\n\t\t\tif ( typeof document !== 'undefined' ) {\n\t\t\t\tdocument.documentElement.dataset[ '{{slugCamelCase}}Hydrated' ] = 'true';\n\t\t\t}\n\t\t\tvoid Promise.allSettled( [\n\t\t\t\tactions.loadState(),\n\t\t\t\tactions.loadBootstrap(),\n\t\t\t] );\n\t\t},\n\t},\n} );\n";
|
|
@@ -14,8 +14,7 @@ import {
|
|
|
14
14
|
} from '@wordpress/components';
|
|
15
15
|
|
|
16
16
|
import {
|
|
17
|
-
\
|
|
18
|
-
\tDEFAULT_CHILD_TEMPLATE,
|
|
17
|
+
\tgetRootInnerBlocksPropsOptions,
|
|
19
18
|
} from './children';
|
|
20
19
|
import { useTypiaValidation } from './hooks';
|
|
21
20
|
import type { {{pascalCase}}Attributes } from './types';
|
|
@@ -25,6 +24,14 @@ import {
|
|
|
25
24
|
} from './validators';
|
|
26
25
|
|
|
27
26
|
type EditProps = BlockEditProps< {{pascalCase}}Attributes >;
|
|
27
|
+
type CompoundInnerBlocksProps = Parameters< typeof InnerBlocks >[ 0 ] & {
|
|
28
|
+
\tdefaultBlock?: [ string, Record< string, unknown > ];
|
|
29
|
+
\tdirectInsert?: boolean;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const TypedInnerBlocks = InnerBlocks as unknown as (
|
|
33
|
+
\tprops: CompoundInnerBlocksProps
|
|
34
|
+
) => ReturnType< typeof InnerBlocks >;
|
|
28
35
|
|
|
29
36
|
export default function Edit( {
|
|
30
37
|
\tattributes,
|
|
@@ -38,6 +45,7 @@ export default function Edit( {
|
|
|
38
45
|
\tconst blockProps = useBlockProps( {
|
|
39
46
|
\t\tclassName: '{{cssClassName}}',
|
|
40
47
|
\t} );
|
|
48
|
+
\tconst rootInnerBlocksPropsOptions = getRootInnerBlocksPropsOptions();
|
|
41
49
|
|
|
42
50
|
\treturn (
|
|
43
51
|
\t\t<>
|
|
@@ -110,12 +118,7 @@ export default function Edit( {
|
|
|
110
118
|
\t\t\t\t\t{ __( 'Resource key:', '{{textDomain}}' ) } { attributes.resourceKey || '—' }
|
|
111
119
|
\t\t\t\t</p>
|
|
112
120
|
\t\t\t\t<div className="{{cssClassName}}__items">
|
|
113
|
-
\t\t\t\t\t<
|
|
114
|
-
\t\t\t\t\t\tallowedBlocks={ ALLOWED_CHILD_BLOCKS }
|
|
115
|
-
\t\t\t\t\t\trenderAppender={ InnerBlocks.ButtonBlockAppender }
|
|
116
|
-
\t\t\t\t\t\ttemplate={ DEFAULT_CHILD_TEMPLATE }
|
|
117
|
-
\t\t\t\t\t\ttemplateLock={ false }
|
|
118
|
-
\t\t\t\t\t/>
|
|
121
|
+
\t\t\t\t\t<TypedInnerBlocks { ...rootInnerBlocksPropsOptions } />
|
|
119
122
|
\t\t\t\t</div>
|
|
120
123
|
\t\t\t</div>
|
|
121
124
|
\t\t</>
|