UNPKG

@wordpress/block-library

Version:
1 lines • 7.11 kB
{"version":3,"file":"transforms.cjs","names":["sprintf","__","createBlock","getTransformedAttributes","getBlockAttributes","getLevelFromHeadingNodeName"],"sources":["../../src/heading/transforms.js"],"sourcesContent":["import { createBlock, getBlockAttributes } from '@wordpress/blocks';\nimport { __, sprintf } from '@wordpress/i18n';\nimport { getLevelFromHeadingNodeName } from './shared';\nimport { getTransformedAttributes } from '../utils/get-transformed-attributes';\n\nconst transforms = {\n\tfrom: [\n\t\t{\n\t\t\ttype: 'block',\n\t\t\tisMultiBlock: true,\n\t\t\tblocks: [ 'core/paragraph' ],\n\t\t\t// The level shortcuts are declared here as well as on the\n\t\t\t// variations, so that they reach a paragraph and not only a\n\t\t\t// heading. Both declarations describe the same shortcut, and only\n\t\t\t// one of them can apply to any given selection.\n\t\t\tshortcuts: [ 1, 2, 3, 4, 5, 6 ].map( ( level ) => ( {\n\t\t\t\tname: `core/block-editor/transform-to-heading-${ level }`,\n\t\t\t\tdescription: sprintf(\n\t\t\t\t\t/* translators: %d: heading level e.g: \"1\", \"2\", \"3\" */\n\t\t\t\t\t__( 'Transform the selected block into a heading %d.' ),\n\t\t\t\t\tlevel\n\t\t\t\t),\n\t\t\t\tkeyCombination: {\n\t\t\t\t\tmodifier: 'access',\n\t\t\t\t\tcharacter: `${ level }`,\n\t\t\t\t},\n\t\t\t\tvariationName: `h${ level }`,\n\t\t\t} ) ),\n\t\t\ttransform: ( attributes ) =>\n\t\t\t\tattributes.map( ( _attributes ) => {\n\t\t\t\t\tconst { content, anchor, style } = _attributes;\n\t\t\t\t\tconst textAlign = style?.typography?.textAlign;\n\t\t\t\t\treturn createBlock( 'core/heading', {\n\t\t\t\t\t\t...getTransformedAttributes(\n\t\t\t\t\t\t\t_attributes,\n\t\t\t\t\t\t\t'core/heading',\n\t\t\t\t\t\t\t( { content: contentBinding } ) => ( {\n\t\t\t\t\t\t\t\tcontent: contentBinding,\n\t\t\t\t\t\t\t} )\n\t\t\t\t\t\t),\n\t\t\t\t\t\tcontent,\n\t\t\t\t\t\tanchor,\n\t\t\t\t\t\t...( textAlign && {\n\t\t\t\t\t\t\tstyle: {\n\t\t\t\t\t\t\t\ttypography: {\n\t\t\t\t\t\t\t\t\ttextAlign,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t} ),\n\t\t\t\t\t} );\n\t\t\t\t} ),\n\t\t},\n\t\t{\n\t\t\ttype: 'raw',\n\t\t\tselector: 'h1,h2,h3,h4,h5,h6',\n\t\t\tschema: ( { phrasingContentSchema, isPaste } ) => {\n\t\t\t\tconst schema = {\n\t\t\t\t\tchildren: phrasingContentSchema,\n\t\t\t\t\tattributes: isPaste ? [] : [ 'style', 'id' ],\n\t\t\t\t};\n\t\t\t\treturn {\n\t\t\t\t\th1: schema,\n\t\t\t\t\th2: schema,\n\t\t\t\t\th3: schema,\n\t\t\t\t\th4: schema,\n\t\t\t\t\th5: schema,\n\t\t\t\t\th6: schema,\n\t\t\t\t};\n\t\t\t},\n\t\t\ttransform( node ) {\n\t\t\t\tconst attributes = getBlockAttributes(\n\t\t\t\t\t'core/heading',\n\t\t\t\t\tnode.outerHTML\n\t\t\t\t);\n\t\t\t\tconst { textAlign } = node.style || {};\n\n\t\t\t\tattributes.level = getLevelFromHeadingNodeName( node.nodeName );\n\n\t\t\t\tif (\n\t\t\t\t\ttextAlign === 'left' ||\n\t\t\t\t\ttextAlign === 'center' ||\n\t\t\t\t\ttextAlign === 'right'\n\t\t\t\t) {\n\t\t\t\t\tattributes.style = {\n\t\t\t\t\t\t...attributes.style,\n\t\t\t\t\t\ttypography: {\n\t\t\t\t\t\t\t...attributes.style?.typography,\n\t\t\t\t\t\t\ttextAlign,\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\treturn createBlock( 'core/heading', attributes );\n\t\t\t},\n\t\t},\n\t\t...[ 1, 2, 3, 4, 5, 6 ].map( ( level ) => ( {\n\t\t\ttype: 'prefix',\n\t\t\tprefix: Array( level + 1 ).join( '#' ),\n\t\t\ttransform( content ) {\n\t\t\t\treturn createBlock( 'core/heading', {\n\t\t\t\t\tlevel,\n\t\t\t\t\tcontent,\n\t\t\t\t} );\n\t\t\t},\n\t\t} ) ),\n\t\t...[ 1, 2, 3, 4, 5, 6 ].map( ( level ) => ( {\n\t\t\ttype: 'enter',\n\t\t\tregExp: new RegExp( `^/(h|H)${ level }$` ),\n\t\t\ttransform: () => createBlock( 'core/heading', { level } ),\n\t\t} ) ),\n\t],\n\tto: [\n\t\t{\n\t\t\ttype: 'block',\n\t\t\tisMultiBlock: true,\n\t\t\tblocks: [ 'core/paragraph' ],\n\t\t\tshortcuts: [\n\t\t\t\t{\n\t\t\t\t\tname: 'core/block-editor/transform-heading-to-paragraph',\n\t\t\t\t\tdescription: __(\n\t\t\t\t\t\t'Transform the selected heading into a paragraph.'\n\t\t\t\t\t),\n\t\t\t\t\tkeyCombination: {\n\t\t\t\t\t\tmodifier: 'access',\n\t\t\t\t\t\tcharacter: '0',\n\t\t\t\t\t},\n\t\t\t\t\taliases: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tmodifier: 'access',\n\t\t\t\t\t\t\tcharacter: '7',\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t],\n\t\t\ttransform: ( attributes ) =>\n\t\t\t\tattributes.map( ( _attributes ) => {\n\t\t\t\t\tconst { content, style } = _attributes;\n\t\t\t\t\tconst textAlign = style?.typography?.textAlign;\n\t\t\t\t\treturn createBlock( 'core/paragraph', {\n\t\t\t\t\t\t...getTransformedAttributes(\n\t\t\t\t\t\t\t_attributes,\n\t\t\t\t\t\t\t'core/paragraph',\n\t\t\t\t\t\t\t( { content: contentBinding } ) => ( {\n\t\t\t\t\t\t\t\tcontent: contentBinding,\n\t\t\t\t\t\t\t} )\n\t\t\t\t\t\t),\n\t\t\t\t\t\tcontent,\n\t\t\t\t\t\t...( textAlign && {\n\t\t\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