{"version":3,"file":"actions.mjs","names":["coreStore","blockEditorStore"],"sources":["../../src/store/actions.ts"],"sourcesContent":["import { getBlockType, cloneBlock } from '@wordpress/blocks';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\nimport type { Block } from '@wordpress/blocks';\nimport { PATTERN_SYNC_TYPES } from '../constants';\nimport type { BlockMetadata } from '../types';\nimport type {\n\tPatternJSON,\n\tPatternOverrides,\n\tPatternsThunkArgs,\n\tSetEditingPatternAction,\n} from './types';\n\n/**\n * Returns a generator converting one or more static blocks into a pattern, or creating a new empty pattern.\n *\n * @param title      Pattern title.\n * @param syncType   They way block is synced, 'fully' or 'unsynced'.\n * @param content    Optional serialized content of blocks to convert to pattern.\n * @param categories Ids of any selected categories.\n */\nexport const createPattern =\n\t(\n\t\ttitle: string,\n\t\tsyncType: string | undefined,\n\t\tcontent?: string,\n\t\tcategories?: Array< number >\n\t) =>\n\tasync ( { registry }: PatternsThunkArgs ) => {\n\t\tconst meta =\n\t\t\tsyncType === PATTERN_SYNC_TYPES.unsynced\n\t\t\t\t? {\n\t\t\t\t\t\twp_pattern_sync_status: syncType,\n\t\t\t\t\t}\n\t\t\t\t: undefined;\n\n\t\tconst reusableBlock = {\n\t\t\ttitle,\n\t\t\tcontent,\n\t\t\tstatus: 'publish',\n\t\t\tmeta,\n\t\t\twp_pattern_category: categories,\n\t\t};\n\n\t\tconst updatedRecord = await registry\n\t\t\t.dispatch( coreStore )\n\t\t\t.saveEntityRecord( 'postType', 'wp_block', reusableBlock );\n\n\t\treturn updatedRecord;\n\t};\n\n/**\n * Create a pattern from a JSON file.\n * @param file       The JSON file instance of the pattern.\n * @param categories Ids of any selected categories.\n */\nexport const createPatternFromFile =\n\t( file: File, categories?: Array< number > ) =>\n\tasync ( { dispatch }: PatternsThunkArgs ) => {\n\t\tconst fileContent = await file.text();\n\t\tlet parsedContent: PatternJSON;\n\t\ttry {\n\t\t\tparsedContent = JSON.parse( fileContent );\n\t\t} catch {\n\t\t\tthrow new Error( 'Invalid JSON file' );\n\t\t}\n\t\tif (\n\t\t\tparsedContent.__file !== 'wp_block' ||\n\t\t\t! parsedContent.title ||\n\t\t\t! parsedContent.content ||\n\t\t\ttypeof parsedContent.title !== 'string' ||\n\t\t\ttypeof parsedContent.content !== 'string' ||\n\t\t\t( parsedContent.syncStatus &&\n\t\t\t\ttypeof parsedContent.syncStatus !== 'string' )\n\t\t) {\n\t\t\tthrow new Error( 'Invalid pattern JSON file' );\n\t\t}\n\n\t\tconst pattern = await dispatch.createPattern(\n\t\t\tparsedContent.title,\n\t\t\tparsedContent.syncStatus,\n\t\t\tparsedContent.content,\n\t\t\tcategories\n\t\t);\n\n\t\treturn pattern;\n\t};\n\n/**\n * Returns a generator converting a synced pattern block into a static block.\n *\n * @param clientId The client ID of the block to attach.\n */\nexport const convertSyncedPatternToStatic =\n\t( clientId: string ) =>\n\t( { registry }: PatternsThunkArgs ) => {\n\t\t// The action converts a pattern block that is in the editor.\n\t\tconst patternBlock = registry\n\t\t\t.select( blockEditorStore )\n\t\t\t.getBlock( clientId )!;\n\t\t// The pattern block keeps each named block's overridden attributes.\n\t\tconst existingOverrides = patternBlock.attributes?.content as\n\t\t\tPatternOverrides | undefined;\n\n\t\tfunction cloneBlocksAndRemoveBindings(\n\t\t\tblocks: Array< Block >\n\t\t): Array< Block > {\n\t\t\treturn blocks.map( ( block ) => {\n\t\t\t\t// Block attributes are untyped; every block may carry `metadata`.\n\t\t\t\tlet metadata = block.attributes.metadata as\n\t\t\t\t\tBlockMetadata | undefined;\n\t\t\t\tif ( metadata ) {\n\t\t\t\t\tmetadata = { ...metadata };\n\t\t\t\t\tdelete metadata.id;\n\t\t\t\t\tdelete metadata.bindings;\n\t\t\t\t\t// Use overridden values of the pattern block if they exist.\n\t\t\t\t\t// A block without a name reads the `undefined` key.\n\t\t\t\t\tif ( existingOverrides?.[ metadata.name as string ] ) {\n\t\t\t\t\t\t// Iterate over each overridden attribute.\n\t\t\t\t\t\tfor ( const [ attributeName, value ] of Object.entries(\n\t\t\t\t\t\t\t// Checked above, under the same possibly `undefined` key.\n\t\t\t\t\t\t\texistingOverrides[ metadata.name as string ]\n\t\t\t\t\t\t) ) {\n\t\t\t\t\t\t\t// Skip if the attribute does not exist in the block type.\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t! getBlockType( block.name )?.attributes[\n\t\t\t\t\t\t\t\t\tattributeName\n\t\t\t\t\t\t\t\t]\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t// Update the block attribute with the override value.\n\t\t\t\t\t\t\tblock.attributes[ attributeName ] = value;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn cloneBlock(\n\t\t\t\t\tblock,\n\t\t\t\t\t{\n\t\t\t\t\t\tmetadata:\n\t\t\t\t\t\t\tmetadata && Object.keys( metadata ).length > 0\n\t\t\t\t\t\t\t\t? metadata\n\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t},\n\t\t\t\t\tcloneBlocksAndRemoveBindings( block.innerBlocks )\n\t\t\t\t);\n\t\t\t} );\n\t\t}\n\n\t\tconst patternInnerBlocks = registry\n\t\t\t.select( blockEditorStore )\n\t\t\t.getBlocks( patternBlock.clientId );\n\n\t\tregistry\n\t\t\t.dispatch( blockEditorStore )\n\t\t\t.replaceBlocks(\n\t\t\t\tpatternBlock.clientId,\n\t\t\t\tcloneBlocksAndRemoveBindings( patternInnerBlocks )\n\t\t\t);\n\t};\n\n/**\n * Returns an action descriptor for SET_EDITING_PATTERN action.\n *\n * @param clientId  The clientID of the pattern to target.\n * @param isEditing Whether the block should be in editing state.\n * @return Action descriptor.\n */\nexport function setEditingPattern(\n\tclientId: string,\n\tisEditing: boolean\n): SetEditingPatternAction {\n\treturn {\n\t\ttype: 'SET_EDITING_PATTERN',\n\t\tclientId,\n\t\tisEditing,\n\t};\n}\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