@wordpress/block-editor 15.6.1 → 15.6.2
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/build/hooks/allowed-blocks.js +50 -1
- package/build/hooks/allowed-blocks.js.map +2 -2
- package/build/hooks/fit-text.js +2 -2
- package/build/hooks/fit-text.js.map +2 -2
- package/build-module/hooks/allowed-blocks.js +49 -1
- package/build-module/hooks/allowed-blocks.js.map +2 -2
- package/build-module/hooks/fit-text.js +2 -2
- package/build-module/hooks/fit-text.js.map +2 -2
- package/package.json +2 -2
- package/src/hooks/allowed-blocks.js +89 -1
- package/src/hooks/fit-text.js +2 -2
- package/src/hooks/test/allowed-blocks.js +278 -0
|
@@ -29,6 +29,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
29
29
|
var allowed_blocks_exports = {};
|
|
30
30
|
__export(allowed_blocks_exports, {
|
|
31
31
|
addAttribute: () => addAttribute,
|
|
32
|
+
addTransforms: () => addTransforms,
|
|
32
33
|
default: () => allowed_blocks_default
|
|
33
34
|
});
|
|
34
35
|
module.exports = __toCommonJS(allowed_blocks_exports);
|
|
@@ -77,8 +78,56 @@ function addAttribute(settings) {
|
|
|
77
78
|
"core/allowedBlocks/attribute",
|
|
78
79
|
addAttribute
|
|
79
80
|
);
|
|
81
|
+
function addTransforms(result, source, index, results) {
|
|
82
|
+
if (!(0, import_blocks.hasBlockSupport)(result.name, "allowedBlocks")) {
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
if (source.length !== 1 && results.length === 1 && result.innerBlocks.length === source.length) {
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
if (results.length === 1 && source.length > 1 || results.length > 1 && source.length === 1) {
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
if (results.length > 1 && source.length > 1 && results.length !== source.length) {
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
if (result.attributes.allowedBlocks) {
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
const sourceAllowedBlocks = source[index]?.attributes?.allowedBlocks;
|
|
98
|
+
if (!sourceAllowedBlocks) {
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
const blockType = (0, import_blocks.getBlockType)(result.name);
|
|
102
|
+
const destinationAllowedBlocks = blockType?.allowedBlocks || [];
|
|
103
|
+
if (!destinationAllowedBlocks.length) {
|
|
104
|
+
return {
|
|
105
|
+
...result,
|
|
106
|
+
attributes: {
|
|
107
|
+
...result.attributes,
|
|
108
|
+
allowedBlocks: sourceAllowedBlocks
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
const filteredSourceAllowedBlocks = sourceAllowedBlocks.filter(
|
|
113
|
+
(block) => destinationAllowedBlocks.includes(block)
|
|
114
|
+
);
|
|
115
|
+
return {
|
|
116
|
+
...result,
|
|
117
|
+
attributes: {
|
|
118
|
+
...result.attributes,
|
|
119
|
+
allowedBlocks: filteredSourceAllowedBlocks
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
(0, import_hooks.addFilter)(
|
|
124
|
+
"blocks.switchToBlockType.transformedBlock",
|
|
125
|
+
"core/allowedBlocks/addTransforms",
|
|
126
|
+
addTransforms
|
|
127
|
+
);
|
|
80
128
|
// Annotate the CommonJS export names for ESM import in node:
|
|
81
129
|
0 && (module.exports = {
|
|
82
|
-
addAttribute
|
|
130
|
+
addAttribute,
|
|
131
|
+
addTransforms
|
|
83
132
|
});
|
|
84
133
|
//# sourceMappingURL=allowed-blocks.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hooks/allowed-blocks.js"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { addFilter } from '@wordpress/hooks';\nimport { hasBlockSupport } from '@wordpress/blocks';\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { store as blockEditorStore } from '../store';\nimport { PrivateInspectorControlsAllowedBlocks } from '../components/inspector-controls/groups';\nimport BlockAllowedBlocksControl from '../components/block-allowed-blocks/allowed-blocks-control';\n\nfunction BlockEditAllowedBlocksControlPure( { clientId } ) {\n\tconst isContentOnly = useSelect(\n\t\t( select ) => {\n\t\t\treturn (\n\t\t\t\tselect( blockEditorStore ).getBlockEditingMode( clientId ) ===\n\t\t\t\t'contentOnly'\n\t\t\t);\n\t\t},\n\t\t[ clientId ]\n\t);\n\n\tif ( isContentOnly ) {\n\t\treturn null;\n\t}\n\n\treturn (\n\t\t<PrivateInspectorControlsAllowedBlocks.Fill>\n\t\t\t<BlockAllowedBlocksControl clientId={ clientId } />\n\t\t</PrivateInspectorControlsAllowedBlocks.Fill>\n\t);\n}\n\nexport default {\n\tedit: BlockEditAllowedBlocksControlPure,\n\tattributeKeys: [ 'allowedBlocks' ],\n\thasSupport( name ) {\n\t\treturn hasBlockSupport( name, 'allowedBlocks' );\n\t},\n};\n\n/**\n * Filters registered block settings, extending attributes with allowedBlocks.\n *\n * @param {Object} settings Original block settings.\n *\n * @return {Object} Filtered block settings.\n */\nexport function addAttribute( settings ) {\n\t// Allow blocks to specify their own attribute definition with default values if needed.\n\tif ( settings?.attributes?.allowedBlocks?.type ) {\n\t\treturn settings;\n\t}\n\tif ( hasBlockSupport( settings, 'allowedBlocks' ) ) {\n\t\t// Gracefully handle if settings.attributes is undefined.\n\t\tsettings.attributes = {\n\t\t\t...settings.attributes,\n\t\t\tallowedBlocks: {\n\t\t\t\ttype: 'array',\n\t\t\t},\n\t\t};\n\t}\n\n\treturn settings;\n}\n\naddFilter(\n\t'blocks.registerBlockType',\n\t'core/allowedBlocks/attribute',\n\taddAttribute\n);\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA+BG;AA5BH,mBAA0B;AAC1B,
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { addFilter } from '@wordpress/hooks';\nimport { hasBlockSupport, getBlockType } from '@wordpress/blocks';\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { store as blockEditorStore } from '../store';\nimport { PrivateInspectorControlsAllowedBlocks } from '../components/inspector-controls/groups';\nimport BlockAllowedBlocksControl from '../components/block-allowed-blocks/allowed-blocks-control';\n\nfunction BlockEditAllowedBlocksControlPure( { clientId } ) {\n\tconst isContentOnly = useSelect(\n\t\t( select ) => {\n\t\t\treturn (\n\t\t\t\tselect( blockEditorStore ).getBlockEditingMode( clientId ) ===\n\t\t\t\t'contentOnly'\n\t\t\t);\n\t\t},\n\t\t[ clientId ]\n\t);\n\n\tif ( isContentOnly ) {\n\t\treturn null;\n\t}\n\n\treturn (\n\t\t<PrivateInspectorControlsAllowedBlocks.Fill>\n\t\t\t<BlockAllowedBlocksControl clientId={ clientId } />\n\t\t</PrivateInspectorControlsAllowedBlocks.Fill>\n\t);\n}\n\nexport default {\n\tedit: BlockEditAllowedBlocksControlPure,\n\tattributeKeys: [ 'allowedBlocks' ],\n\thasSupport( name ) {\n\t\treturn hasBlockSupport( name, 'allowedBlocks' );\n\t},\n};\n\n/**\n * Filters registered block settings, extending attributes with allowedBlocks.\n *\n * @param {Object} settings Original block settings.\n *\n * @return {Object} Filtered block settings.\n */\nexport function addAttribute( settings ) {\n\t// Allow blocks to specify their own attribute definition with default values if needed.\n\tif ( settings?.attributes?.allowedBlocks?.type ) {\n\t\treturn settings;\n\t}\n\tif ( hasBlockSupport( settings, 'allowedBlocks' ) ) {\n\t\t// Gracefully handle if settings.attributes is undefined.\n\t\tsettings.attributes = {\n\t\t\t...settings.attributes,\n\t\t\tallowedBlocks: {\n\t\t\t\ttype: 'array',\n\t\t\t},\n\t\t};\n\t}\n\n\treturn settings;\n}\n\naddFilter(\n\t'blocks.registerBlockType',\n\t'core/allowedBlocks/attribute',\n\taddAttribute\n);\n\n/**\n * Add transforms to preserve allowedBlocks on block transformations.\n *\n * @param {Object} result The transformed block.\n * @param {Array} source Original blocks transformed.\n * @param {number} index Index of the transformed block.\n * @param {Array} results All blocks that resulted from the transformation.\n * @return {Object} Modified transformed block.\n */\nexport function addTransforms( result, source, index, results ) {\n\tif ( ! hasBlockSupport( result.name, 'allowedBlocks' ) ) {\n\t\treturn result;\n\t}\n\n\t// If the condition verifies we are probably in the presence of a wrapping transform\n\t// e.g: nesting paragraphs in a group or columns and in that case the attribute should not be kept.\n\tif (\n\t\tsource.length !== 1 &&\n\t\tresults.length === 1 &&\n\t\tresult.innerBlocks.length === source.length\n\t) {\n\t\treturn result;\n\t}\n\n\t// If we are transforming one block to multiple blocks or multiple blocks to one block,\n\t// we ignore the attribute during the transform.\n\tif (\n\t\t( results.length === 1 && source.length > 1 ) ||\n\t\t( results.length > 1 && source.length === 1 )\n\t) {\n\t\treturn result;\n\t}\n\n\t// If we are transforming multiple blocks to multiple blocks with different counts,\n\t// we ignore the attribute during the transform.\n\tif (\n\t\tresults.length > 1 &&\n\t\tsource.length > 1 &&\n\t\tresults.length !== source.length\n\t) {\n\t\treturn result;\n\t}\n\n\t// If the target block already has allowedBlocks, we don't need to preserve\n\t// the source allowedBlocks.\n\tif ( result.attributes.allowedBlocks ) {\n\t\treturn result;\n\t}\n\n\tconst sourceAllowedBlocks = source[ index ]?.attributes?.allowedBlocks;\n\n\tif ( ! sourceAllowedBlocks ) {\n\t\treturn result;\n\t}\n\n\tconst blockType = getBlockType( result.name );\n\tconst destinationAllowedBlocks = blockType?.allowedBlocks || [];\n\n\tif ( ! destinationAllowedBlocks.length ) {\n\t\treturn {\n\t\t\t...result,\n\t\t\tattributes: {\n\t\t\t\t...result.attributes,\n\t\t\t\tallowedBlocks: sourceAllowedBlocks,\n\t\t\t},\n\t\t};\n\t}\n\n\t// Filter out any source allowed blocks that are not defined in the destination allowed blocks.\n\tconst filteredSourceAllowedBlocks = sourceAllowedBlocks.filter( ( block ) =>\n\t\tdestinationAllowedBlocks.includes( block )\n\t);\n\n\treturn {\n\t\t...result,\n\t\tattributes: {\n\t\t\t...result.attributes,\n\t\t\tallowedBlocks: filteredSourceAllowedBlocks,\n\t\t},\n\t};\n}\n\naddFilter(\n\t'blocks.switchToBlockType.transformedBlock',\n\t'core/allowedBlocks/addTransforms',\n\taddTransforms\n);\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA+BG;AA5BH,mBAA0B;AAC1B,oBAA8C;AAC9C,kBAA0B;AAK1B,mBAA0C;AAC1C,oBAAsD;AACtD,oCAAsC;AAEtC,SAAS,kCAAmC,EAAE,SAAS,GAAI;AAC1D,QAAM,oBAAgB;AAAA,IACrB,CAAE,WAAY;AACb,aACC,OAAQ,aAAAA,KAAiB,EAAE,oBAAqB,QAAS,MACzD;AAAA,IAEF;AAAA,IACA,CAAE,QAAS;AAAA,EACZ;AAEA,MAAK,eAAgB;AACpB,WAAO;AAAA,EACR;AAEA,SACC,4CAAC,oDAAsC,MAAtC,EACA,sDAAC,8BAAAC,SAAA,EAA0B,UAAsB,GAClD;AAEF;AAEA,IAAO,yBAAQ;AAAA,EACd,MAAM;AAAA,EACN,eAAe,CAAE,eAAgB;AAAA,EACjC,WAAY,MAAO;AAClB,eAAO,+BAAiB,MAAM,eAAgB;AAAA,EAC/C;AACD;AASO,SAAS,aAAc,UAAW;AAExC,MAAK,UAAU,YAAY,eAAe,MAAO;AAChD,WAAO;AAAA,EACR;AACA,UAAK,+BAAiB,UAAU,eAAgB,GAAI;AAEnD,aAAS,aAAa;AAAA,MACrB,GAAG,SAAS;AAAA,MACZ,eAAe;AAAA,QACd,MAAM;AAAA,MACP;AAAA,IACD;AAAA,EACD;AAEA,SAAO;AACR;AAAA,IAEA;AAAA,EACC;AAAA,EACA;AAAA,EACA;AACD;AAWO,SAAS,cAAe,QAAQ,QAAQ,OAAO,SAAU;AAC/D,MAAK,KAAE,+BAAiB,OAAO,MAAM,eAAgB,GAAI;AACxD,WAAO;AAAA,EACR;AAIA,MACC,OAAO,WAAW,KAClB,QAAQ,WAAW,KACnB,OAAO,YAAY,WAAW,OAAO,QACpC;AACD,WAAO;AAAA,EACR;AAIA,MACG,QAAQ,WAAW,KAAK,OAAO,SAAS,KACxC,QAAQ,SAAS,KAAK,OAAO,WAAW,GACzC;AACD,WAAO;AAAA,EACR;AAIA,MACC,QAAQ,SAAS,KACjB,OAAO,SAAS,KAChB,QAAQ,WAAW,OAAO,QACzB;AACD,WAAO;AAAA,EACR;AAIA,MAAK,OAAO,WAAW,eAAgB;AACtC,WAAO;AAAA,EACR;AAEA,QAAM,sBAAsB,OAAQ,KAAM,GAAG,YAAY;AAEzD,MAAK,CAAE,qBAAsB;AAC5B,WAAO;AAAA,EACR;AAEA,QAAM,gBAAY,4BAAc,OAAO,IAAK;AAC5C,QAAM,2BAA2B,WAAW,iBAAiB,CAAC;AAE9D,MAAK,CAAE,yBAAyB,QAAS;AACxC,WAAO;AAAA,MACN,GAAG;AAAA,MACH,YAAY;AAAA,QACX,GAAG,OAAO;AAAA,QACV,eAAe;AAAA,MAChB;AAAA,IACD;AAAA,EACD;AAGA,QAAM,8BAA8B,oBAAoB;AAAA,IAAQ,CAAE,UACjE,yBAAyB,SAAU,KAAM;AAAA,EAC1C;AAEA,SAAO;AAAA,IACN,GAAG;AAAA,IACH,YAAY;AAAA,MACX,GAAG,OAAO;AAAA,MACV,eAAe;AAAA,IAChB;AAAA,EACD;AACD;AAAA,IAEA;AAAA,EACC;AAAA,EACA;AAAA,EACA;AACD;",
|
|
6
6
|
"names": ["blockEditorStore", "BlockAllowedBlocksControl"]
|
|
7
7
|
}
|
package/build/hooks/fit-text.js
CHANGED
|
@@ -67,12 +67,12 @@ function useFitText({ fitText, name, clientId }) {
|
|
|
67
67
|
const blockElement = (0, import_use_block_refs.useBlockElement)(clientId);
|
|
68
68
|
const blockAttributes = (0, import_data.useSelect)(
|
|
69
69
|
(select) => {
|
|
70
|
-
if (!clientId) {
|
|
70
|
+
if (!clientId || !hasFitTextSupport2 || !fitText) {
|
|
71
71
|
return;
|
|
72
72
|
}
|
|
73
73
|
return select(import_store.store).getBlockAttributes(clientId);
|
|
74
74
|
},
|
|
75
|
-
[clientId]
|
|
75
|
+
[clientId, hasFitTextSupport2, fitText]
|
|
76
76
|
);
|
|
77
77
|
const applyFitText = (0, import_element.useCallback)(() => {
|
|
78
78
|
if (!blockElement || !hasFitTextSupport2 || !fitText) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hooks/fit-text.js"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { addFilter } from '@wordpress/hooks';\nimport { hasBlockSupport } from '@wordpress/blocks';\nimport { useEffect, useCallback } from '@wordpress/element';\nimport { useSelect } from '@wordpress/data';\nimport { __ } from '@wordpress/i18n';\nimport {\n\tToggleControl,\n\t__experimentalToolsPanelItem as ToolsPanelItem,\n} from '@wordpress/components';\n\n/**\n * Internal dependencies\n */\nimport { optimizeFitText } from '../utils/fit-text-utils';\nimport { store as blockEditorStore } from '../store';\nimport { useBlockElement } from '../components/block-list/use-block-props/use-block-refs';\nimport InspectorControls from '../components/inspector-controls';\n\nexport const FIT_TEXT_SUPPORT_KEY = 'typography.fitText';\n\n/**\n * Filters registered block settings, extending attributes to include\n * the `fitText` attribute.\n *\n * @param {Object} settings Original block settings.\n * @return {Object} Filtered block settings.\n */\nfunction addAttributes( settings ) {\n\tif ( ! hasBlockSupport( settings, FIT_TEXT_SUPPORT_KEY ) ) {\n\t\treturn settings;\n\t}\n\n\t// Allow blocks to specify their own attribute definition.\n\tif ( settings.attributes?.fitText ) {\n\t\treturn settings;\n\t}\n\n\t// Add fitText attribute.\n\treturn {\n\t\t...settings,\n\t\tattributes: {\n\t\t\t...settings.attributes,\n\t\t\tfitText: {\n\t\t\t\ttype: 'boolean',\n\t\t\t},\n\t\t},\n\t};\n}\n\n/**\n * Custom hook to handle fit text functionality in the editor.\n *\n * @param {Object} props Component props.\n * @param {?boolean} props.fitText Fit text attribute.\n * @param {string} props.name Block name.\n * @param {string} props.clientId Block client ID.\n */\nfunction useFitText( { fitText, name, clientId } ) {\n\tconst hasFitTextSupport = hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY );\n\tconst blockElement = useBlockElement( clientId );\n\n\t// Monitor block attribute changes\n\t// Any attribute may change the available space.\n\tconst blockAttributes = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! clientId ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\treturn select( blockEditorStore ).getBlockAttributes( clientId );\n\t\t},\n\t\t[ clientId ]\n\t);\n\n\tconst applyFitText = useCallback( () => {\n\t\tif ( ! blockElement || ! hasFitTextSupport || ! fitText ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Get or create style element with unique ID\n\t\tconst styleId = `fit-text-${ clientId }`;\n\t\tlet styleElement = blockElement.ownerDocument.getElementById( styleId );\n\t\tif ( ! styleElement ) {\n\t\t\tstyleElement = blockElement.ownerDocument.createElement( 'style' );\n\t\t\tstyleElement.id = styleId;\n\t\t\tblockElement.ownerDocument.head.appendChild( styleElement );\n\t\t}\n\n\t\tconst blockSelector = `#block-${ clientId }`;\n\n\t\tconst applyStylesFn = ( css ) => {\n\t\t\tstyleElement.textContent = css;\n\t\t};\n\n\t\toptimizeFitText( blockElement, blockSelector, applyStylesFn );\n\t}, [ blockElement, clientId, hasFitTextSupport, fitText ] );\n\n\tuseEffect( () => {\n\t\tif (\n\t\t\t! fitText ||\n\t\t\t! blockElement ||\n\t\t\t! clientId ||\n\t\t\t! hasFitTextSupport\n\t\t) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Apply initially\n\t\tapplyFitText();\n\n\t\t// Store current element value for cleanup\n\t\tconst currentElement = blockElement;\n\n\t\t// Watch for size changes\n\t\tlet resizeObserver;\n\t\tif ( window.ResizeObserver && currentElement.parentElement ) {\n\t\t\tresizeObserver = new window.ResizeObserver( applyFitText );\n\t\t\tresizeObserver.observe( currentElement.parentElement );\n\t\t}\n\n\t\t// Cleanup function\n\t\treturn () => {\n\t\t\tif ( resizeObserver ) {\n\t\t\t\tresizeObserver.disconnect();\n\t\t\t}\n\n\t\t\tconst styleId = `fit-text-${ clientId }`;\n\t\t\tconst styleElement =\n\t\t\t\tcurrentElement.ownerDocument.getElementById( styleId );\n\t\t\tif ( styleElement ) {\n\t\t\t\tstyleElement.remove();\n\t\t\t}\n\t\t};\n\t}, [ fitText, clientId, applyFitText, blockElement, hasFitTextSupport ] );\n\n\t// Trigger fit text recalculation when content changes\n\tuseEffect( () => {\n\t\tif ( fitText && blockElement && hasFitTextSupport ) {\n\t\t\t// Wait for next frame to ensure DOM has updated after content changes\n\t\t\tconst frameId = window.requestAnimationFrame( () => {\n\t\t\t\tif ( blockElement ) {\n\t\t\t\t\tapplyFitText();\n\t\t\t\t}\n\t\t\t} );\n\n\t\t\treturn () => window.cancelAnimationFrame( frameId );\n\t\t}\n\t}, [\n\t\tblockAttributes,\n\t\tfitText,\n\t\tapplyFitText,\n\t\tblockElement,\n\t\thasFitTextSupport,\n\t] );\n}\n\n/**\n * Fit text control component for the typography panel.\n *\n * @param {Object} props Component props.\n * @param {string} props.clientId Block client ID.\n * @param {Function} props.setAttributes Function to set block attributes.\n * @param {string} props.name Block name.\n * @param {boolean} props.fitText Whether fit text is enabled.\n * @param {string} props.fontSize Font size slug.\n * @param {Object} props.style Block style object.\n */\nexport function FitTextControl( {\n\tclientId,\n\tfitText = false,\n\tsetAttributes,\n\tname,\n\tfontSize,\n\tstyle,\n} ) {\n\tif ( ! hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ) ) {\n\t\treturn null;\n\t}\n\treturn (\n\t\t<InspectorControls group=\"typography\">\n\t\t\t<ToolsPanelItem\n\t\t\t\thasValue={ () => fitText }\n\t\t\t\tlabel={ __( 'Fit text' ) }\n\t\t\t\tonDeselect={ () => setAttributes( { fitText: undefined } ) }\n\t\t\t\tresetAllFilter={ () => ( { fitText: undefined } ) }\n\t\t\t\tpanelId={ clientId }\n\t\t\t>\n\t\t\t\t<ToggleControl\n\t\t\t\t\t__nextHasNoMarginBottom\n\t\t\t\t\tlabel={ __( 'Fit text' ) }\n\t\t\t\t\tchecked={ fitText }\n\t\t\t\t\tonChange={ () => {\n\t\t\t\t\t\tconst newFitText = ! fitText || undefined;\n\t\t\t\t\t\tconst updates = { fitText: newFitText };\n\n\t\t\t\t\t\t// When enabling fit text, clear font size if it has a value\n\t\t\t\t\t\tif ( newFitText ) {\n\t\t\t\t\t\t\tif ( fontSize ) {\n\t\t\t\t\t\t\t\tupdates.fontSize = undefined;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif ( style?.typography?.fontSize ) {\n\t\t\t\t\t\t\t\tupdates.style = {\n\t\t\t\t\t\t\t\t\t...style,\n\t\t\t\t\t\t\t\t\ttypography: {\n\t\t\t\t\t\t\t\t\t\t...style?.typography,\n\t\t\t\t\t\t\t\t\t\tfontSize: undefined,\n\t\t\t\t\t\t\t\t\t},\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\n\t\t\t\t\t\tsetAttributes( updates );\n\t\t\t\t\t} }\n\t\t\t\t\thelp={\n\t\t\t\t\t\tfitText\n\t\t\t\t\t\t\t? __( 'Text will resize to fit its container.' )\n\t\t\t\t\t\t\t: __(\n\t\t\t\t\t\t\t\t\t'The text will resize to fit its container, resetting other font size settings.'\n\t\t\t\t\t\t\t )\n\t\t\t\t\t}\n\t\t\t\t/>\n\t\t\t</ToolsPanelItem>\n\t\t</InspectorControls>\n\t);\n}\n\n/**\n * Override props applied to the block element on save.\n *\n * @param {Object} props Additional props applied to the block element.\n * @param {Object} blockType Block type.\n * @param {Object} attributes Block attributes.\n * @return {Object} Filtered props applied to the block element.\n */\nfunction addSaveProps( props, blockType, attributes ) {\n\tif ( ! hasBlockSupport( blockType, FIT_TEXT_SUPPORT_KEY ) ) {\n\t\treturn props;\n\t}\n\n\tconst { fitText } = attributes;\n\n\tif ( ! fitText ) {\n\t\treturn props;\n\t}\n\n\t// Add CSS class for frontend detection and styling\n\tconst className = props.className\n\t\t? `${ props.className } has-fit-text`\n\t\t: 'has-fit-text';\n\n\treturn {\n\t\t...props,\n\t\tclassName,\n\t};\n}\n/**\n * Override props applied to the block element in the editor.\n *\n * @param {Object} props Component props including block attributes.\n * @param {string} props.name Block name.\n * @param {boolean} props.fitText Whether fit text is enabled.\n * @param {string} props.clientId Block client ID.\n * @return {Object} Filtered props applied to the block element.\n */\nfunction useBlockProps( { name, fitText, clientId } ) {\n\tuseFitText( { fitText, name, clientId } );\n\tif ( ! fitText || ! hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ) ) {\n\t\treturn {};\n\t}\n\treturn {\n\t\tclassName: 'has-fit-text',\n\t};\n}\n\naddFilter(\n\t'blocks.registerBlockType',\n\t'core/fit-text/addAttribute',\n\taddAttributes\n);\n\nconst hasFitTextSupport = ( blockNameOrType ) => {\n\treturn hasBlockSupport( blockNameOrType, FIT_TEXT_SUPPORT_KEY );\n};\n\nexport default {\n\tuseBlockProps,\n\taddSaveProps,\n\tattributeKeys: [ 'fitText', 'fontSize', 'style' ],\n\thasSupport: hasFitTextSupport,\n\tedit: FitTextControl,\n};\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA6LI;AA1LJ,mBAA0B;AAC1B,oBAAgC;AAChC,qBAAuC;AACvC,kBAA0B;AAC1B,kBAAmB;AACnB,wBAGO;AAKP,4BAAgC;AAChC,mBAA0C;AAC1C,4BAAgC;AAChC,gCAA8B;AAEvB,MAAM,uBAAuB;AASpC,SAAS,cAAe,UAAW;AAClC,MAAK,KAAE,+BAAiB,UAAU,oBAAqB,GAAI;AAC1D,WAAO;AAAA,EACR;AAGA,MAAK,SAAS,YAAY,SAAU;AACnC,WAAO;AAAA,EACR;AAGA,SAAO;AAAA,IACN,GAAG;AAAA,IACH,YAAY;AAAA,MACX,GAAG,SAAS;AAAA,MACZ,SAAS;AAAA,QACR,MAAM;AAAA,MACP;AAAA,IACD;AAAA,EACD;AACD;AAUA,SAAS,WAAY,EAAE,SAAS,MAAM,SAAS,GAAI;AAClD,QAAMA,yBAAoB,+BAAiB,MAAM,oBAAqB;AACtE,QAAM,mBAAe,uCAAiB,QAAS;AAI/C,QAAM,sBAAkB;AAAA,IACvB,CAAE,WAAY;AACb,UAAK,CAAE,
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { addFilter } from '@wordpress/hooks';\nimport { hasBlockSupport } from '@wordpress/blocks';\nimport { useEffect, useCallback } from '@wordpress/element';\nimport { useSelect } from '@wordpress/data';\nimport { __ } from '@wordpress/i18n';\nimport {\n\tToggleControl,\n\t__experimentalToolsPanelItem as ToolsPanelItem,\n} from '@wordpress/components';\n\n/**\n * Internal dependencies\n */\nimport { optimizeFitText } from '../utils/fit-text-utils';\nimport { store as blockEditorStore } from '../store';\nimport { useBlockElement } from '../components/block-list/use-block-props/use-block-refs';\nimport InspectorControls from '../components/inspector-controls';\n\nexport const FIT_TEXT_SUPPORT_KEY = 'typography.fitText';\n\n/**\n * Filters registered block settings, extending attributes to include\n * the `fitText` attribute.\n *\n * @param {Object} settings Original block settings.\n * @return {Object} Filtered block settings.\n */\nfunction addAttributes( settings ) {\n\tif ( ! hasBlockSupport( settings, FIT_TEXT_SUPPORT_KEY ) ) {\n\t\treturn settings;\n\t}\n\n\t// Allow blocks to specify their own attribute definition.\n\tif ( settings.attributes?.fitText ) {\n\t\treturn settings;\n\t}\n\n\t// Add fitText attribute.\n\treturn {\n\t\t...settings,\n\t\tattributes: {\n\t\t\t...settings.attributes,\n\t\t\tfitText: {\n\t\t\t\ttype: 'boolean',\n\t\t\t},\n\t\t},\n\t};\n}\n\n/**\n * Custom hook to handle fit text functionality in the editor.\n *\n * @param {Object} props Component props.\n * @param {?boolean} props.fitText Fit text attribute.\n * @param {string} props.name Block name.\n * @param {string} props.clientId Block client ID.\n */\nfunction useFitText( { fitText, name, clientId } ) {\n\tconst hasFitTextSupport = hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY );\n\tconst blockElement = useBlockElement( clientId );\n\n\t// Monitor block attribute changes\n\t// Any attribute may change the available space.\n\tconst blockAttributes = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! clientId || ! hasFitTextSupport || ! fitText ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\treturn select( blockEditorStore ).getBlockAttributes( clientId );\n\t\t},\n\t\t[ clientId, hasFitTextSupport, fitText ]\n\t);\n\n\tconst applyFitText = useCallback( () => {\n\t\tif ( ! blockElement || ! hasFitTextSupport || ! fitText ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Get or create style element with unique ID\n\t\tconst styleId = `fit-text-${ clientId }`;\n\t\tlet styleElement = blockElement.ownerDocument.getElementById( styleId );\n\t\tif ( ! styleElement ) {\n\t\t\tstyleElement = blockElement.ownerDocument.createElement( 'style' );\n\t\t\tstyleElement.id = styleId;\n\t\t\tblockElement.ownerDocument.head.appendChild( styleElement );\n\t\t}\n\n\t\tconst blockSelector = `#block-${ clientId }`;\n\n\t\tconst applyStylesFn = ( css ) => {\n\t\t\tstyleElement.textContent = css;\n\t\t};\n\n\t\toptimizeFitText( blockElement, blockSelector, applyStylesFn );\n\t}, [ blockElement, clientId, hasFitTextSupport, fitText ] );\n\n\tuseEffect( () => {\n\t\tif (\n\t\t\t! fitText ||\n\t\t\t! blockElement ||\n\t\t\t! clientId ||\n\t\t\t! hasFitTextSupport\n\t\t) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Apply initially\n\t\tapplyFitText();\n\n\t\t// Store current element value for cleanup\n\t\tconst currentElement = blockElement;\n\n\t\t// Watch for size changes\n\t\tlet resizeObserver;\n\t\tif ( window.ResizeObserver && currentElement.parentElement ) {\n\t\t\tresizeObserver = new window.ResizeObserver( applyFitText );\n\t\t\tresizeObserver.observe( currentElement.parentElement );\n\t\t}\n\n\t\t// Cleanup function\n\t\treturn () => {\n\t\t\tif ( resizeObserver ) {\n\t\t\t\tresizeObserver.disconnect();\n\t\t\t}\n\n\t\t\tconst styleId = `fit-text-${ clientId }`;\n\t\t\tconst styleElement =\n\t\t\t\tcurrentElement.ownerDocument.getElementById( styleId );\n\t\t\tif ( styleElement ) {\n\t\t\t\tstyleElement.remove();\n\t\t\t}\n\t\t};\n\t}, [ fitText, clientId, applyFitText, blockElement, hasFitTextSupport ] );\n\n\t// Trigger fit text recalculation when content changes\n\tuseEffect( () => {\n\t\tif ( fitText && blockElement && hasFitTextSupport ) {\n\t\t\t// Wait for next frame to ensure DOM has updated after content changes\n\t\t\tconst frameId = window.requestAnimationFrame( () => {\n\t\t\t\tif ( blockElement ) {\n\t\t\t\t\tapplyFitText();\n\t\t\t\t}\n\t\t\t} );\n\n\t\t\treturn () => window.cancelAnimationFrame( frameId );\n\t\t}\n\t}, [\n\t\tblockAttributes,\n\t\tfitText,\n\t\tapplyFitText,\n\t\tblockElement,\n\t\thasFitTextSupport,\n\t] );\n}\n\n/**\n * Fit text control component for the typography panel.\n *\n * @param {Object} props Component props.\n * @param {string} props.clientId Block client ID.\n * @param {Function} props.setAttributes Function to set block attributes.\n * @param {string} props.name Block name.\n * @param {boolean} props.fitText Whether fit text is enabled.\n * @param {string} props.fontSize Font size slug.\n * @param {Object} props.style Block style object.\n */\nexport function FitTextControl( {\n\tclientId,\n\tfitText = false,\n\tsetAttributes,\n\tname,\n\tfontSize,\n\tstyle,\n} ) {\n\tif ( ! hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ) ) {\n\t\treturn null;\n\t}\n\treturn (\n\t\t<InspectorControls group=\"typography\">\n\t\t\t<ToolsPanelItem\n\t\t\t\thasValue={ () => fitText }\n\t\t\t\tlabel={ __( 'Fit text' ) }\n\t\t\t\tonDeselect={ () => setAttributes( { fitText: undefined } ) }\n\t\t\t\tresetAllFilter={ () => ( { fitText: undefined } ) }\n\t\t\t\tpanelId={ clientId }\n\t\t\t>\n\t\t\t\t<ToggleControl\n\t\t\t\t\t__nextHasNoMarginBottom\n\t\t\t\t\tlabel={ __( 'Fit text' ) }\n\t\t\t\t\tchecked={ fitText }\n\t\t\t\t\tonChange={ () => {\n\t\t\t\t\t\tconst newFitText = ! fitText || undefined;\n\t\t\t\t\t\tconst updates = { fitText: newFitText };\n\n\t\t\t\t\t\t// When enabling fit text, clear font size if it has a value\n\t\t\t\t\t\tif ( newFitText ) {\n\t\t\t\t\t\t\tif ( fontSize ) {\n\t\t\t\t\t\t\t\tupdates.fontSize = undefined;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif ( style?.typography?.fontSize ) {\n\t\t\t\t\t\t\t\tupdates.style = {\n\t\t\t\t\t\t\t\t\t...style,\n\t\t\t\t\t\t\t\t\ttypography: {\n\t\t\t\t\t\t\t\t\t\t...style?.typography,\n\t\t\t\t\t\t\t\t\t\tfontSize: undefined,\n\t\t\t\t\t\t\t\t\t},\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\n\t\t\t\t\t\tsetAttributes( updates );\n\t\t\t\t\t} }\n\t\t\t\t\thelp={\n\t\t\t\t\t\tfitText\n\t\t\t\t\t\t\t? __( 'Text will resize to fit its container.' )\n\t\t\t\t\t\t\t: __(\n\t\t\t\t\t\t\t\t\t'The text will resize to fit its container, resetting other font size settings.'\n\t\t\t\t\t\t\t )\n\t\t\t\t\t}\n\t\t\t\t/>\n\t\t\t</ToolsPanelItem>\n\t\t</InspectorControls>\n\t);\n}\n\n/**\n * Override props applied to the block element on save.\n *\n * @param {Object} props Additional props applied to the block element.\n * @param {Object} blockType Block type.\n * @param {Object} attributes Block attributes.\n * @return {Object} Filtered props applied to the block element.\n */\nfunction addSaveProps( props, blockType, attributes ) {\n\tif ( ! hasBlockSupport( blockType, FIT_TEXT_SUPPORT_KEY ) ) {\n\t\treturn props;\n\t}\n\n\tconst { fitText } = attributes;\n\n\tif ( ! fitText ) {\n\t\treturn props;\n\t}\n\n\t// Add CSS class for frontend detection and styling\n\tconst className = props.className\n\t\t? `${ props.className } has-fit-text`\n\t\t: 'has-fit-text';\n\n\treturn {\n\t\t...props,\n\t\tclassName,\n\t};\n}\n/**\n * Override props applied to the block element in the editor.\n *\n * @param {Object} props Component props including block attributes.\n * @param {string} props.name Block name.\n * @param {boolean} props.fitText Whether fit text is enabled.\n * @param {string} props.clientId Block client ID.\n * @return {Object} Filtered props applied to the block element.\n */\nfunction useBlockProps( { name, fitText, clientId } ) {\n\tuseFitText( { fitText, name, clientId } );\n\tif ( ! fitText || ! hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ) ) {\n\t\treturn {};\n\t}\n\treturn {\n\t\tclassName: 'has-fit-text',\n\t};\n}\n\naddFilter(\n\t'blocks.registerBlockType',\n\t'core/fit-text/addAttribute',\n\taddAttributes\n);\n\nconst hasFitTextSupport = ( blockNameOrType ) => {\n\treturn hasBlockSupport( blockNameOrType, FIT_TEXT_SUPPORT_KEY );\n};\n\nexport default {\n\tuseBlockProps,\n\taddSaveProps,\n\tattributeKeys: [ 'fitText', 'fontSize', 'style' ],\n\thasSupport: hasFitTextSupport,\n\tedit: FitTextControl,\n};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA6LI;AA1LJ,mBAA0B;AAC1B,oBAAgC;AAChC,qBAAuC;AACvC,kBAA0B;AAC1B,kBAAmB;AACnB,wBAGO;AAKP,4BAAgC;AAChC,mBAA0C;AAC1C,4BAAgC;AAChC,gCAA8B;AAEvB,MAAM,uBAAuB;AASpC,SAAS,cAAe,UAAW;AAClC,MAAK,KAAE,+BAAiB,UAAU,oBAAqB,GAAI;AAC1D,WAAO;AAAA,EAC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TextSupport", "blockEditorStore", "InspectorControls", "ToolsPanelItem"]
|
|
7
7
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx } from "react/jsx-runtime";
|
|
2
2
|
import { addFilter } from "@wordpress/hooks";
|
|
3
|
-
import { hasBlockSupport } from "@wordpress/blocks";
|
|
3
|
+
import { hasBlockSupport, getBlockType } from "@wordpress/blocks";
|
|
4
4
|
import { useSelect } from "@wordpress/data";
|
|
5
5
|
import { store as blockEditorStore } from "../store";
|
|
6
6
|
import { PrivateInspectorControlsAllowedBlocks } from "../components/inspector-controls/groups";
|
|
@@ -43,8 +43,56 @@ addFilter(
|
|
|
43
43
|
"core/allowedBlocks/attribute",
|
|
44
44
|
addAttribute
|
|
45
45
|
);
|
|
46
|
+
function addTransforms(result, source, index, results) {
|
|
47
|
+
if (!hasBlockSupport(result.name, "allowedBlocks")) {
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
if (source.length !== 1 && results.length === 1 && result.innerBlocks.length === source.length) {
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
if (results.length === 1 && source.length > 1 || results.length > 1 && source.length === 1) {
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
if (results.length > 1 && source.length > 1 && results.length !== source.length) {
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
if (result.attributes.allowedBlocks) {
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
const sourceAllowedBlocks = source[index]?.attributes?.allowedBlocks;
|
|
63
|
+
if (!sourceAllowedBlocks) {
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
const blockType = getBlockType(result.name);
|
|
67
|
+
const destinationAllowedBlocks = blockType?.allowedBlocks || [];
|
|
68
|
+
if (!destinationAllowedBlocks.length) {
|
|
69
|
+
return {
|
|
70
|
+
...result,
|
|
71
|
+
attributes: {
|
|
72
|
+
...result.attributes,
|
|
73
|
+
allowedBlocks: sourceAllowedBlocks
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
const filteredSourceAllowedBlocks = sourceAllowedBlocks.filter(
|
|
78
|
+
(block) => destinationAllowedBlocks.includes(block)
|
|
79
|
+
);
|
|
80
|
+
return {
|
|
81
|
+
...result,
|
|
82
|
+
attributes: {
|
|
83
|
+
...result.attributes,
|
|
84
|
+
allowedBlocks: filteredSourceAllowedBlocks
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
addFilter(
|
|
89
|
+
"blocks.switchToBlockType.transformedBlock",
|
|
90
|
+
"core/allowedBlocks/addTransforms",
|
|
91
|
+
addTransforms
|
|
92
|
+
);
|
|
46
93
|
export {
|
|
47
94
|
addAttribute,
|
|
95
|
+
addTransforms,
|
|
48
96
|
allowed_blocks_default as default
|
|
49
97
|
};
|
|
50
98
|
//# sourceMappingURL=allowed-blocks.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hooks/allowed-blocks.js"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { addFilter } from '@wordpress/hooks';\nimport { hasBlockSupport } from '@wordpress/blocks';\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { store as blockEditorStore } from '../store';\nimport { PrivateInspectorControlsAllowedBlocks } from '../components/inspector-controls/groups';\nimport BlockAllowedBlocksControl from '../components/block-allowed-blocks/allowed-blocks-control';\n\nfunction BlockEditAllowedBlocksControlPure( { clientId } ) {\n\tconst isContentOnly = useSelect(\n\t\t( select ) => {\n\t\t\treturn (\n\t\t\t\tselect( blockEditorStore ).getBlockEditingMode( clientId ) ===\n\t\t\t\t'contentOnly'\n\t\t\t);\n\t\t},\n\t\t[ clientId ]\n\t);\n\n\tif ( isContentOnly ) {\n\t\treturn null;\n\t}\n\n\treturn (\n\t\t<PrivateInspectorControlsAllowedBlocks.Fill>\n\t\t\t<BlockAllowedBlocksControl clientId={ clientId } />\n\t\t</PrivateInspectorControlsAllowedBlocks.Fill>\n\t);\n}\n\nexport default {\n\tedit: BlockEditAllowedBlocksControlPure,\n\tattributeKeys: [ 'allowedBlocks' ],\n\thasSupport( name ) {\n\t\treturn hasBlockSupport( name, 'allowedBlocks' );\n\t},\n};\n\n/**\n * Filters registered block settings, extending attributes with allowedBlocks.\n *\n * @param {Object} settings Original block settings.\n *\n * @return {Object} Filtered block settings.\n */\nexport function addAttribute( settings ) {\n\t// Allow blocks to specify their own attribute definition with default values if needed.\n\tif ( settings?.attributes?.allowedBlocks?.type ) {\n\t\treturn settings;\n\t}\n\tif ( hasBlockSupport( settings, 'allowedBlocks' ) ) {\n\t\t// Gracefully handle if settings.attributes is undefined.\n\t\tsettings.attributes = {\n\t\t\t...settings.attributes,\n\t\t\tallowedBlocks: {\n\t\t\t\ttype: 'array',\n\t\t\t},\n\t\t};\n\t}\n\n\treturn settings;\n}\n\naddFilter(\n\t'blocks.registerBlockType',\n\t'core/allowedBlocks/attribute',\n\taddAttribute\n);\n"],
|
|
5
|
-
"mappings": "AA+BG;AA5BH,SAAS,iBAAiB;AAC1B,SAAS,
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { addFilter } from '@wordpress/hooks';\nimport { hasBlockSupport, getBlockType } from '@wordpress/blocks';\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { store as blockEditorStore } from '../store';\nimport { PrivateInspectorControlsAllowedBlocks } from '../components/inspector-controls/groups';\nimport BlockAllowedBlocksControl from '../components/block-allowed-blocks/allowed-blocks-control';\n\nfunction BlockEditAllowedBlocksControlPure( { clientId } ) {\n\tconst isContentOnly = useSelect(\n\t\t( select ) => {\n\t\t\treturn (\n\t\t\t\tselect( blockEditorStore ).getBlockEditingMode( clientId ) ===\n\t\t\t\t'contentOnly'\n\t\t\t);\n\t\t},\n\t\t[ clientId ]\n\t);\n\n\tif ( isContentOnly ) {\n\t\treturn null;\n\t}\n\n\treturn (\n\t\t<PrivateInspectorControlsAllowedBlocks.Fill>\n\t\t\t<BlockAllowedBlocksControl clientId={ clientId } />\n\t\t</PrivateInspectorControlsAllowedBlocks.Fill>\n\t);\n}\n\nexport default {\n\tedit: BlockEditAllowedBlocksControlPure,\n\tattributeKeys: [ 'allowedBlocks' ],\n\thasSupport( name ) {\n\t\treturn hasBlockSupport( name, 'allowedBlocks' );\n\t},\n};\n\n/**\n * Filters registered block settings, extending attributes with allowedBlocks.\n *\n * @param {Object} settings Original block settings.\n *\n * @return {Object} Filtered block settings.\n */\nexport function addAttribute( settings ) {\n\t// Allow blocks to specify their own attribute definition with default values if needed.\n\tif ( settings?.attributes?.allowedBlocks?.type ) {\n\t\treturn settings;\n\t}\n\tif ( hasBlockSupport( settings, 'allowedBlocks' ) ) {\n\t\t// Gracefully handle if settings.attributes is undefined.\n\t\tsettings.attributes = {\n\t\t\t...settings.attributes,\n\t\t\tallowedBlocks: {\n\t\t\t\ttype: 'array',\n\t\t\t},\n\t\t};\n\t}\n\n\treturn settings;\n}\n\naddFilter(\n\t'blocks.registerBlockType',\n\t'core/allowedBlocks/attribute',\n\taddAttribute\n);\n\n/**\n * Add transforms to preserve allowedBlocks on block transformations.\n *\n * @param {Object} result The transformed block.\n * @param {Array} source Original blocks transformed.\n * @param {number} index Index of the transformed block.\n * @param {Array} results All blocks that resulted from the transformation.\n * @return {Object} Modified transformed block.\n */\nexport function addTransforms( result, source, index, results ) {\n\tif ( ! hasBlockSupport( result.name, 'allowedBlocks' ) ) {\n\t\treturn result;\n\t}\n\n\t// If the condition verifies we are probably in the presence of a wrapping transform\n\t// e.g: nesting paragraphs in a group or columns and in that case the attribute should not be kept.\n\tif (\n\t\tsource.length !== 1 &&\n\t\tresults.length === 1 &&\n\t\tresult.innerBlocks.length === source.length\n\t) {\n\t\treturn result;\n\t}\n\n\t// If we are transforming one block to multiple blocks or multiple blocks to one block,\n\t// we ignore the attribute during the transform.\n\tif (\n\t\t( results.length === 1 && source.length > 1 ) ||\n\t\t( results.length > 1 && source.length === 1 )\n\t) {\n\t\treturn result;\n\t}\n\n\t// If we are transforming multiple blocks to multiple blocks with different counts,\n\t// we ignore the attribute during the transform.\n\tif (\n\t\tresults.length > 1 &&\n\t\tsource.length > 1 &&\n\t\tresults.length !== source.length\n\t) {\n\t\treturn result;\n\t}\n\n\t// If the target block already has allowedBlocks, we don't need to preserve\n\t// the source allowedBlocks.\n\tif ( result.attributes.allowedBlocks ) {\n\t\treturn result;\n\t}\n\n\tconst sourceAllowedBlocks = source[ index ]?.attributes?.allowedBlocks;\n\n\tif ( ! sourceAllowedBlocks ) {\n\t\treturn result;\n\t}\n\n\tconst blockType = getBlockType( result.name );\n\tconst destinationAllowedBlocks = blockType?.allowedBlocks || [];\n\n\tif ( ! destinationAllowedBlocks.length ) {\n\t\treturn {\n\t\t\t...result,\n\t\t\tattributes: {\n\t\t\t\t...result.attributes,\n\t\t\t\tallowedBlocks: sourceAllowedBlocks,\n\t\t\t},\n\t\t};\n\t}\n\n\t// Filter out any source allowed blocks that are not defined in the destination allowed blocks.\n\tconst filteredSourceAllowedBlocks = sourceAllowedBlocks.filter( ( block ) =>\n\t\tdestinationAllowedBlocks.includes( block )\n\t);\n\n\treturn {\n\t\t...result,\n\t\tattributes: {\n\t\t\t...result.attributes,\n\t\t\tallowedBlocks: filteredSourceAllowedBlocks,\n\t\t},\n\t};\n}\n\naddFilter(\n\t'blocks.switchToBlockType.transformedBlock',\n\t'core/allowedBlocks/addTransforms',\n\taddTransforms\n);\n"],
|
|
5
|
+
"mappings": "AA+BG;AA5BH,SAAS,iBAAiB;AAC1B,SAAS,iBAAiB,oBAAoB;AAC9C,SAAS,iBAAiB;AAK1B,SAAS,SAAS,wBAAwB;AAC1C,SAAS,6CAA6C;AACtD,OAAO,+BAA+B;AAEtC,SAAS,kCAAmC,EAAE,SAAS,GAAI;AAC1D,QAAM,gBAAgB;AAAA,IACrB,CAAE,WAAY;AACb,aAC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,aAAc,OAAO,IAAK;AAC5C,QAAM,2BAA2B,WAAW,iBAAiB,CAAC;AAE9D,MAAK,CAAE,yBAAyB,QAAS;AACxC,WAAO;AAAA,MACN,GAAG;AAAA,MACH,YAAY;AAAA,QACX,GAAG,OAAO;AAAA,QACV,eAAe;AAAA,MAChB;AAAA,IACD;AAAA,EACD;AAGA,QAAM,8BAA8B,oBAAoB;AAAA,IAAQ,CAAE,UACjE,yBAAyB,SAAU,KAAM;AAAA,EAC1C;AAEA,SAAO;AAAA,IACN,GAAG;AAAA,IACH,YAAY;AAAA,MACX,GAAG,OAAO;AAAA,MACV,eAAe;AAAA,IAChB;AAAA,EACD;AACD;AAEA;AAAA,EACC;AAAA,EACA;AAAA,EACA;AACD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -35,12 +35,12 @@ function useFitText({ fitText, name, clientId }) {
|
|
|
35
35
|
const blockElement = useBlockElement(clientId);
|
|
36
36
|
const blockAttributes = useSelect(
|
|
37
37
|
(select) => {
|
|
38
|
-
if (!clientId) {
|
|
38
|
+
if (!clientId || !hasFitTextSupport2 || !fitText) {
|
|
39
39
|
return;
|
|
40
40
|
}
|
|
41
41
|
return select(blockEditorStore).getBlockAttributes(clientId);
|
|
42
42
|
},
|
|
43
|
-
[clientId]
|
|
43
|
+
[clientId, hasFitTextSupport2, fitText]
|
|
44
44
|
);
|
|
45
45
|
const applyFitText = useCallback(() => {
|
|
46
46
|
if (!blockElement || !hasFitTextSupport2 || !fitText) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hooks/fit-text.js"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { addFilter } from '@wordpress/hooks';\nimport { hasBlockSupport } from '@wordpress/blocks';\nimport { useEffect, useCallback } from '@wordpress/element';\nimport { useSelect } from '@wordpress/data';\nimport { __ } from '@wordpress/i18n';\nimport {\n\tToggleControl,\n\t__experimentalToolsPanelItem as ToolsPanelItem,\n} from '@wordpress/components';\n\n/**\n * Internal dependencies\n */\nimport { optimizeFitText } from '../utils/fit-text-utils';\nimport { store as blockEditorStore } from '../store';\nimport { useBlockElement } from '../components/block-list/use-block-props/use-block-refs';\nimport InspectorControls from '../components/inspector-controls';\n\nexport const FIT_TEXT_SUPPORT_KEY = 'typography.fitText';\n\n/**\n * Filters registered block settings, extending attributes to include\n * the `fitText` attribute.\n *\n * @param {Object} settings Original block settings.\n * @return {Object} Filtered block settings.\n */\nfunction addAttributes( settings ) {\n\tif ( ! hasBlockSupport( settings, FIT_TEXT_SUPPORT_KEY ) ) {\n\t\treturn settings;\n\t}\n\n\t// Allow blocks to specify their own attribute definition.\n\tif ( settings.attributes?.fitText ) {\n\t\treturn settings;\n\t}\n\n\t// Add fitText attribute.\n\treturn {\n\t\t...settings,\n\t\tattributes: {\n\t\t\t...settings.attributes,\n\t\t\tfitText: {\n\t\t\t\ttype: 'boolean',\n\t\t\t},\n\t\t},\n\t};\n}\n\n/**\n * Custom hook to handle fit text functionality in the editor.\n *\n * @param {Object} props Component props.\n * @param {?boolean} props.fitText Fit text attribute.\n * @param {string} props.name Block name.\n * @param {string} props.clientId Block client ID.\n */\nfunction useFitText( { fitText, name, clientId } ) {\n\tconst hasFitTextSupport = hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY );\n\tconst blockElement = useBlockElement( clientId );\n\n\t// Monitor block attribute changes\n\t// Any attribute may change the available space.\n\tconst blockAttributes = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! clientId ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\treturn select( blockEditorStore ).getBlockAttributes( clientId );\n\t\t},\n\t\t[ clientId ]\n\t);\n\n\tconst applyFitText = useCallback( () => {\n\t\tif ( ! blockElement || ! hasFitTextSupport || ! fitText ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Get or create style element with unique ID\n\t\tconst styleId = `fit-text-${ clientId }`;\n\t\tlet styleElement = blockElement.ownerDocument.getElementById( styleId );\n\t\tif ( ! styleElement ) {\n\t\t\tstyleElement = blockElement.ownerDocument.createElement( 'style' );\n\t\t\tstyleElement.id = styleId;\n\t\t\tblockElement.ownerDocument.head.appendChild( styleElement );\n\t\t}\n\n\t\tconst blockSelector = `#block-${ clientId }`;\n\n\t\tconst applyStylesFn = ( css ) => {\n\t\t\tstyleElement.textContent = css;\n\t\t};\n\n\t\toptimizeFitText( blockElement, blockSelector, applyStylesFn );\n\t}, [ blockElement, clientId, hasFitTextSupport, fitText ] );\n\n\tuseEffect( () => {\n\t\tif (\n\t\t\t! fitText ||\n\t\t\t! blockElement ||\n\t\t\t! clientId ||\n\t\t\t! hasFitTextSupport\n\t\t) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Apply initially\n\t\tapplyFitText();\n\n\t\t// Store current element value for cleanup\n\t\tconst currentElement = blockElement;\n\n\t\t// Watch for size changes\n\t\tlet resizeObserver;\n\t\tif ( window.ResizeObserver && currentElement.parentElement ) {\n\t\t\tresizeObserver = new window.ResizeObserver( applyFitText );\n\t\t\tresizeObserver.observe( currentElement.parentElement );\n\t\t}\n\n\t\t// Cleanup function\n\t\treturn () => {\n\t\t\tif ( resizeObserver ) {\n\t\t\t\tresizeObserver.disconnect();\n\t\t\t}\n\n\t\t\tconst styleId = `fit-text-${ clientId }`;\n\t\t\tconst styleElement =\n\t\t\t\tcurrentElement.ownerDocument.getElementById( styleId );\n\t\t\tif ( styleElement ) {\n\t\t\t\tstyleElement.remove();\n\t\t\t}\n\t\t};\n\t}, [ fitText, clientId, applyFitText, blockElement, hasFitTextSupport ] );\n\n\t// Trigger fit text recalculation when content changes\n\tuseEffect( () => {\n\t\tif ( fitText && blockElement && hasFitTextSupport ) {\n\t\t\t// Wait for next frame to ensure DOM has updated after content changes\n\t\t\tconst frameId = window.requestAnimationFrame( () => {\n\t\t\t\tif ( blockElement ) {\n\t\t\t\t\tapplyFitText();\n\t\t\t\t}\n\t\t\t} );\n\n\t\t\treturn () => window.cancelAnimationFrame( frameId );\n\t\t}\n\t}, [\n\t\tblockAttributes,\n\t\tfitText,\n\t\tapplyFitText,\n\t\tblockElement,\n\t\thasFitTextSupport,\n\t] );\n}\n\n/**\n * Fit text control component for the typography panel.\n *\n * @param {Object} props Component props.\n * @param {string} props.clientId Block client ID.\n * @param {Function} props.setAttributes Function to set block attributes.\n * @param {string} props.name Block name.\n * @param {boolean} props.fitText Whether fit text is enabled.\n * @param {string} props.fontSize Font size slug.\n * @param {Object} props.style Block style object.\n */\nexport function FitTextControl( {\n\tclientId,\n\tfitText = false,\n\tsetAttributes,\n\tname,\n\tfontSize,\n\tstyle,\n} ) {\n\tif ( ! hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ) ) {\n\t\treturn null;\n\t}\n\treturn (\n\t\t<InspectorControls group=\"typography\">\n\t\t\t<ToolsPanelItem\n\t\t\t\thasValue={ () => fitText }\n\t\t\t\tlabel={ __( 'Fit text' ) }\n\t\t\t\tonDeselect={ () => setAttributes( { fitText: undefined } ) }\n\t\t\t\tresetAllFilter={ () => ( { fitText: undefined } ) }\n\t\t\t\tpanelId={ clientId }\n\t\t\t>\n\t\t\t\t<ToggleControl\n\t\t\t\t\t__nextHasNoMarginBottom\n\t\t\t\t\tlabel={ __( 'Fit text' ) }\n\t\t\t\t\tchecked={ fitText }\n\t\t\t\t\tonChange={ () => {\n\t\t\t\t\t\tconst newFitText = ! fitText || undefined;\n\t\t\t\t\t\tconst updates = { fitText: newFitText };\n\n\t\t\t\t\t\t// When enabling fit text, clear font size if it has a value\n\t\t\t\t\t\tif ( newFitText ) {\n\t\t\t\t\t\t\tif ( fontSize ) {\n\t\t\t\t\t\t\t\tupdates.fontSize = undefined;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif ( style?.typography?.fontSize ) {\n\t\t\t\t\t\t\t\tupdates.style = {\n\t\t\t\t\t\t\t\t\t...style,\n\t\t\t\t\t\t\t\t\ttypography: {\n\t\t\t\t\t\t\t\t\t\t...style?.typography,\n\t\t\t\t\t\t\t\t\t\tfontSize: undefined,\n\t\t\t\t\t\t\t\t\t},\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\n\t\t\t\t\t\tsetAttributes( updates );\n\t\t\t\t\t} }\n\t\t\t\t\thelp={\n\t\t\t\t\t\tfitText\n\t\t\t\t\t\t\t? __( 'Text will resize to fit its container.' )\n\t\t\t\t\t\t\t: __(\n\t\t\t\t\t\t\t\t\t'The text will resize to fit its container, resetting other font size settings.'\n\t\t\t\t\t\t\t )\n\t\t\t\t\t}\n\t\t\t\t/>\n\t\t\t</ToolsPanelItem>\n\t\t</InspectorControls>\n\t);\n}\n\n/**\n * Override props applied to the block element on save.\n *\n * @param {Object} props Additional props applied to the block element.\n * @param {Object} blockType Block type.\n * @param {Object} attributes Block attributes.\n * @return {Object} Filtered props applied to the block element.\n */\nfunction addSaveProps( props, blockType, attributes ) {\n\tif ( ! hasBlockSupport( blockType, FIT_TEXT_SUPPORT_KEY ) ) {\n\t\treturn props;\n\t}\n\n\tconst { fitText } = attributes;\n\n\tif ( ! fitText ) {\n\t\treturn props;\n\t}\n\n\t// Add CSS class for frontend detection and styling\n\tconst className = props.className\n\t\t? `${ props.className } has-fit-text`\n\t\t: 'has-fit-text';\n\n\treturn {\n\t\t...props,\n\t\tclassName,\n\t};\n}\n/**\n * Override props applied to the block element in the editor.\n *\n * @param {Object} props Component props including block attributes.\n * @param {string} props.name Block name.\n * @param {boolean} props.fitText Whether fit text is enabled.\n * @param {string} props.clientId Block client ID.\n * @return {Object} Filtered props applied to the block element.\n */\nfunction useBlockProps( { name, fitText, clientId } ) {\n\tuseFitText( { fitText, name, clientId } );\n\tif ( ! fitText || ! hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ) ) {\n\t\treturn {};\n\t}\n\treturn {\n\t\tclassName: 'has-fit-text',\n\t};\n}\n\naddFilter(\n\t'blocks.registerBlockType',\n\t'core/fit-text/addAttribute',\n\taddAttributes\n);\n\nconst hasFitTextSupport = ( blockNameOrType ) => {\n\treturn hasBlockSupport( blockNameOrType, FIT_TEXT_SUPPORT_KEY );\n};\n\nexport default {\n\tuseBlockProps,\n\taddSaveProps,\n\tattributeKeys: [ 'fitText', 'fontSize', 'style' ],\n\thasSupport: hasFitTextSupport,\n\tedit: FitTextControl,\n};\n"],
|
|
5
|
-
"mappings": "AA6LI;AA1LJ,SAAS,iBAAiB;AAC1B,SAAS,uBAAuB;AAChC,SAAS,WAAW,mBAAmB;AACvC,SAAS,iBAAiB;AAC1B,SAAS,UAAU;AACnB;AAAA,EACC;AAAA,EACA,gCAAgC;AAAA,OAC1B;AAKP,SAAS,uBAAuB;AAChC,SAAS,SAAS,wBAAwB;AAC1C,SAAS,uBAAuB;AAChC,OAAO,uBAAuB;AAEvB,MAAM,uBAAuB;AASpC,SAAS,cAAe,UAAW;AAClC,MAAK,CAAE,gBAAiB,UAAU,oBAAqB,GAAI;AAC1D,WAAO;AAAA,EACR;AAGA,MAAK,SAAS,YAAY,SAAU;AACnC,WAAO;AAAA,EACR;AAGA,SAAO;AAAA,IACN,GAAG;AAAA,IACH,YAAY;AAAA,MACX,GAAG,SAAS;AAAA,MACZ,SAAS;AAAA,QACR,MAAM;AAAA,MACP;AAAA,IACD;AAAA,EACD;AACD;AAUA,SAAS,WAAY,EAAE,SAAS,MAAM,SAAS,GAAI;AAClD,QAAMA,qBAAoB,gBAAiB,MAAM,oBAAqB;AACtE,QAAM,eAAe,gBAAiB,QAAS;AAI/C,QAAM,kBAAkB;AAAA,IACvB,CAAE,WAAY;AACb,UAAK,CAAE,
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { addFilter } from '@wordpress/hooks';\nimport { hasBlockSupport } from '@wordpress/blocks';\nimport { useEffect, useCallback } from '@wordpress/element';\nimport { useSelect } from '@wordpress/data';\nimport { __ } from '@wordpress/i18n';\nimport {\n\tToggleControl,\n\t__experimentalToolsPanelItem as ToolsPanelItem,\n} from '@wordpress/components';\n\n/**\n * Internal dependencies\n */\nimport { optimizeFitText } from '../utils/fit-text-utils';\nimport { store as blockEditorStore } from '../store';\nimport { useBlockElement } from '../components/block-list/use-block-props/use-block-refs';\nimport InspectorControls from '../components/inspector-controls';\n\nexport const FIT_TEXT_SUPPORT_KEY = 'typography.fitText';\n\n/**\n * Filters registered block settings, extending attributes to include\n * the `fitText` attribute.\n *\n * @param {Object} settings Original block settings.\n * @return {Object} Filtered block settings.\n */\nfunction addAttributes( settings ) {\n\tif ( ! hasBlockSupport( settings, FIT_TEXT_SUPPORT_KEY ) ) {\n\t\treturn settings;\n\t}\n\n\t// Allow blocks to specify their own attribute definition.\n\tif ( settings.attributes?.fitText ) {\n\t\treturn settings;\n\t}\n\n\t// Add fitText attribute.\n\treturn {\n\t\t...settings,\n\t\tattributes: {\n\t\t\t...settings.attributes,\n\t\t\tfitText: {\n\t\t\t\ttype: 'boolean',\n\t\t\t},\n\t\t},\n\t};\n}\n\n/**\n * Custom hook to handle fit text functionality in the editor.\n *\n * @param {Object} props Component props.\n * @param {?boolean} props.fitText Fit text attribute.\n * @param {string} props.name Block name.\n * @param {string} props.clientId Block client ID.\n */\nfunction useFitText( { fitText, name, clientId } ) {\n\tconst hasFitTextSupport = hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY );\n\tconst blockElement = useBlockElement( clientId );\n\n\t// Monitor block attribute changes\n\t// Any attribute may change the available space.\n\tconst blockAttributes = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! clientId || ! hasFitTextSupport || ! fitText ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\treturn select( blockEditorStore ).getBlockAttributes( clientId );\n\t\t},\n\t\t[ clientId, hasFitTextSupport, fitText ]\n\t);\n\n\tconst applyFitText = useCallback( () => {\n\t\tif ( ! blockElement || ! hasFitTextSupport || ! fitText ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Get or create style element with unique ID\n\t\tconst styleId = `fit-text-${ clientId }`;\n\t\tlet styleElement = blockElement.ownerDocument.getElementById( styleId );\n\t\tif ( ! styleElement ) {\n\t\t\tstyleElement = blockElement.ownerDocument.createElement( 'style' );\n\t\t\tstyleElement.id = styleId;\n\t\t\tblockElement.ownerDocument.head.appendChild( styleElement );\n\t\t}\n\n\t\tconst blockSelector = `#block-${ clientId }`;\n\n\t\tconst applyStylesFn = ( css ) => {\n\t\t\tstyleElement.textContent = css;\n\t\t};\n\n\t\toptimizeFitText( blockElement, blockSelector, applyStylesFn );\n\t}, [ blockElement, clientId, hasFitTextSupport, fitText ] );\n\n\tuseEffect( () => {\n\t\tif (\n\t\t\t! fitText ||\n\t\t\t! blockElement ||\n\t\t\t! clientId ||\n\t\t\t! hasFitTextSupport\n\t\t) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Apply initially\n\t\tapplyFitText();\n\n\t\t// Store current element value for cleanup\n\t\tconst currentElement = blockElement;\n\n\t\t// Watch for size changes\n\t\tlet resizeObserver;\n\t\tif ( window.ResizeObserver && currentElement.parentElement ) {\n\t\t\tresizeObserver = new window.ResizeObserver( applyFitText );\n\t\t\tresizeObserver.observe( currentElement.parentElement );\n\t\t}\n\n\t\t// Cleanup function\n\t\treturn () => {\n\t\t\tif ( resizeObserver ) {\n\t\t\t\tresizeObserver.disconnect();\n\t\t\t}\n\n\t\t\tconst styleId = `fit-text-${ clientId }`;\n\t\t\tconst styleElement =\n\t\t\t\tcurrentElement.ownerDocument.getElementById( styleId );\n\t\t\tif ( styleElement ) {\n\t\t\t\tstyleElement.remove();\n\t\t\t}\n\t\t};\n\t}, [ fitText, clientId, applyFitText, blockElement, hasFitTextSupport ] );\n\n\t// Trigger fit text recalculation when content changes\n\tuseEffect( () => {\n\t\tif ( fitText && blockElement && hasFitTextSupport ) {\n\t\t\t// Wait for next frame to ensure DOM has updated after content changes\n\t\t\tconst frameId = window.requestAnimationFrame( () => {\n\t\t\t\tif ( blockElement ) {\n\t\t\t\t\tapplyFitText();\n\t\t\t\t}\n\t\t\t} );\n\n\t\t\treturn () => window.cancelAnimationFrame( frameId );\n\t\t}\n\t}, [\n\t\tblockAttributes,\n\t\tfitText,\n\t\tapplyFitText,\n\t\tblockElement,\n\t\thasFitTextSupport,\n\t] );\n}\n\n/**\n * Fit text control component for the typography panel.\n *\n * @param {Object} props Component props.\n * @param {string} props.clientId Block client ID.\n * @param {Function} props.setAttributes Function to set block attributes.\n * @param {string} props.name Block name.\n * @param {boolean} props.fitText Whether fit text is enabled.\n * @param {string} props.fontSize Font size slug.\n * @param {Object} props.style Block style object.\n */\nexport function FitTextControl( {\n\tclientId,\n\tfitText = false,\n\tsetAttributes,\n\tname,\n\tfontSize,\n\tstyle,\n} ) {\n\tif ( ! hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ) ) {\n\t\treturn null;\n\t}\n\treturn (\n\t\t<InspectorControls group=\"typography\">\n\t\t\t<ToolsPanelItem\n\t\t\t\thasValue={ () => fitText }\n\t\t\t\tlabel={ __( 'Fit text' ) }\n\t\t\t\tonDeselect={ () => setAttributes( { fitText: undefined } ) }\n\t\t\t\tresetAllFilter={ () => ( { fitText: undefined } ) }\n\t\t\t\tpanelId={ clientId }\n\t\t\t>\n\t\t\t\t<ToggleControl\n\t\t\t\t\t__nextHasNoMarginBottom\n\t\t\t\t\tlabel={ __( 'Fit text' ) }\n\t\t\t\t\tchecked={ fitText }\n\t\t\t\t\tonChange={ () => {\n\t\t\t\t\t\tconst newFitText = ! fitText || undefined;\n\t\t\t\t\t\tconst updates = { fitText: newFitText };\n\n\t\t\t\t\t\t// When enabling fit text, clear font size if it has a value\n\t\t\t\t\t\tif ( newFitText ) {\n\t\t\t\t\t\t\tif ( fontSize ) {\n\t\t\t\t\t\t\t\tupdates.fontSize = undefined;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif ( style?.typography?.fontSize ) {\n\t\t\t\t\t\t\t\tupdates.style = {\n\t\t\t\t\t\t\t\t\t...style,\n\t\t\t\t\t\t\t\t\ttypography: {\n\t\t\t\t\t\t\t\t\t\t...style?.typography,\n\t\t\t\t\t\t\t\t\t\tfontSize: undefined,\n\t\t\t\t\t\t\t\t\t},\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\n\t\t\t\t\t\tsetAttributes( updates );\n\t\t\t\t\t} }\n\t\t\t\t\thelp={\n\t\t\t\t\t\tfitText\n\t\t\t\t\t\t\t? __( 'Text will resize to fit its container.' )\n\t\t\t\t\t\t\t: __(\n\t\t\t\t\t\t\t\t\t'The text will resize to fit its container, resetting other font size settings.'\n\t\t\t\t\t\t\t )\n\t\t\t\t\t}\n\t\t\t\t/>\n\t\t\t</ToolsPanelItem>\n\t\t</InspectorControls>\n\t);\n}\n\n/**\n * Override props applied to the block element on save.\n *\n * @param {Object} props Additional props applied to the block element.\n * @param {Object} blockType Block type.\n * @param {Object} attributes Block attributes.\n * @return {Object} Filtered props applied to the block element.\n */\nfunction addSaveProps( props, blockType, attributes ) {\n\tif ( ! hasBlockSupport( blockType, FIT_TEXT_SUPPORT_KEY ) ) {\n\t\treturn props;\n\t}\n\n\tconst { fitText } = attributes;\n\n\tif ( ! fitText ) {\n\t\treturn props;\n\t}\n\n\t// Add CSS class for frontend detection and styling\n\tconst className = props.className\n\t\t? `${ props.className } has-fit-text`\n\t\t: 'has-fit-text';\n\n\treturn {\n\t\t...props,\n\t\tclassName,\n\t};\n}\n/**\n * Override props applied to the block element in the editor.\n *\n * @param {Object} props Component props including block attributes.\n * @param {string} props.name Block name.\n * @param {boolean} props.fitText Whether fit text is enabled.\n * @param {string} props.clientId Block client ID.\n * @return {Object} Filtered props applied to the block element.\n */\nfunction useBlockProps( { name, fitText, clientId } ) {\n\tuseFitText( { fitText, name, clientId } );\n\tif ( ! fitText || ! hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ) ) {\n\t\treturn {};\n\t}\n\treturn {\n\t\tclassName: 'has-fit-text',\n\t};\n}\n\naddFilter(\n\t'blocks.registerBlockType',\n\t'core/fit-text/addAttribute',\n\taddAttributes\n);\n\nconst hasFitTextSupport = ( blockNameOrType ) => {\n\treturn hasBlockSupport( blockNameOrType, FIT_TEXT_SUPPORT_KEY );\n};\n\nexport default {\n\tuseBlockProps,\n\taddSaveProps,\n\tattributeKeys: [ 'fitText', 'fontSize', 'style' ],\n\thasSupport: hasFitTextSupport,\n\tedit: FitTextControl,\n};\n"],
|
|
5
|
+
"mappings": "AA6LI;AA1LJ,SAAS,iBAAiB;AAC1B,SAAS,uBAAuB;AAChC,SAAS,WAAW,mBAAmB;AACvC,SAAS,iBAAiB;AAC1B,SAAS,UAAU;AACnB;AAAA,EACC;AAAA,EACA,gCAAgC;AAAA,OAC1B;AAKP,SAAS,uBAAuB;AAChC,SAAS,SAAS,wBAAwB;AAC1C,SAAS,uBAAuB;AAChC,OAAO,uBAAuB;AAEvB,MAAM,uBAAuB;AASpC,SAAS,cAAe,UAAW;AAClC,MAAK,CAAE,gBAAiB,UAAU,oBAAqB,GAAI;AAC1D,WAAO;AAAA,EACR;AAGA,MAAK,SAAS,YAAY,SAAU;AACnC,WAAO;AAAA,EACR;AAGA,SAAO;AAAA,IACN,GAAG;AAAA,IACH,YAAY;AAAA,MACX,GAAG,SAAS;AAAA,MACZ,SAAS;AAAA,QACR,MAAM;AAAA,MACP;AAAA,IACD;AAAA,EACD;AACD;AAUA,SAAS,WAAY,EAAE,SAAS,MAAM,SAAS,GAAI;AAClD,QAAMA,qBAAoB,gBAAiB,MAAM,oBAAqB;AACtE,QAAM,eAAe,gBAAiB,QAAS;AAI/C,QAAM,kBAAkB;AAAA,IACvB,CAAE,WAAY;AACb,UAAK,CAAE,YAAY,CAAEA,sBAAqB,CAAE,SAAU;AACrD;AAAA,MACD;AACA,aAAO,OAAQ,gBAAiB,EAAE,mBAAoB,QAAS;AAAA,IAChE;AAAA,IACA,CAAE,UAAUA,oBAAmB,OAAQ;AAAA,EACxC;AAEA,QAAM,eAAe,YAAa,MAAM;AACvC,QAAK,CAAE,gBAA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M,YACrB,GAAI,MAAM,SAAU,kBACpB;AAEH,SAAO;AAAA,IACN,GAAG;AAAA,IACH;AAAA,EACD;AACD;AAUA,SAAS,cAAe,EAAE,MAAM,SAAS,SAAS,GAAI;AACrD,aAAY,EAAE,SAAS,MAAM,SAAS,CAAE;AACxC,MAAK,CAAE,WAAW,CAAE,gBAAiB,MAAM,oBAAqB,GAAI;AACnE,WAAO,CAAC;AAAA,EACT;AACA,SAAO;AAAA,IACN,WAAW;AAAA,EACZ;AACD;AAEA;AAAA,EACC;AAAA,EACA;AAAA,EACA;AACD;AAEA,MAAM,oBAAoB,CAAE,oBAAqB;AAChD,SAAO,gBAAiB,iBAAiB,oBAAqB;AAC/D;AAEA,IAAO,mBAAQ;AAAA,EACd;AAAA,EACA;AAAA,EACA,eAAe,CAAE,WAAW,YAAY,OAAQ;AAAA,EAChD,YAAY;AAAA,EACZ,MAAM;AACP;",
|
|
6
6
|
"names": ["hasFitTextSupport"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/block-editor",
|
|
3
|
-
"version": "15.6.
|
|
3
|
+
"version": "15.6.2",
|
|
4
4
|
"description": "Generic block editor.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -111,5 +111,5 @@
|
|
|
111
111
|
"publishConfig": {
|
|
112
112
|
"access": "public"
|
|
113
113
|
},
|
|
114
|
-
"gitHead": "
|
|
114
|
+
"gitHead": "45005cc254bab59182927e35a68cd22f6320634d"
|
|
115
115
|
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
4
|
import { addFilter } from '@wordpress/hooks';
|
|
5
|
-
import { hasBlockSupport } from '@wordpress/blocks';
|
|
5
|
+
import { hasBlockSupport, getBlockType } from '@wordpress/blocks';
|
|
6
6
|
import { useSelect } from '@wordpress/data';
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -72,3 +72,91 @@ addFilter(
|
|
|
72
72
|
'core/allowedBlocks/attribute',
|
|
73
73
|
addAttribute
|
|
74
74
|
);
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Add transforms to preserve allowedBlocks on block transformations.
|
|
78
|
+
*
|
|
79
|
+
* @param {Object} result The transformed block.
|
|
80
|
+
* @param {Array} source Original blocks transformed.
|
|
81
|
+
* @param {number} index Index of the transformed block.
|
|
82
|
+
* @param {Array} results All blocks that resulted from the transformation.
|
|
83
|
+
* @return {Object} Modified transformed block.
|
|
84
|
+
*/
|
|
85
|
+
export function addTransforms( result, source, index, results ) {
|
|
86
|
+
if ( ! hasBlockSupport( result.name, 'allowedBlocks' ) ) {
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// If the condition verifies we are probably in the presence of a wrapping transform
|
|
91
|
+
// e.g: nesting paragraphs in a group or columns and in that case the attribute should not be kept.
|
|
92
|
+
if (
|
|
93
|
+
source.length !== 1 &&
|
|
94
|
+
results.length === 1 &&
|
|
95
|
+
result.innerBlocks.length === source.length
|
|
96
|
+
) {
|
|
97
|
+
return result;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// If we are transforming one block to multiple blocks or multiple blocks to one block,
|
|
101
|
+
// we ignore the attribute during the transform.
|
|
102
|
+
if (
|
|
103
|
+
( results.length === 1 && source.length > 1 ) ||
|
|
104
|
+
( results.length > 1 && source.length === 1 )
|
|
105
|
+
) {
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// If we are transforming multiple blocks to multiple blocks with different counts,
|
|
110
|
+
// we ignore the attribute during the transform.
|
|
111
|
+
if (
|
|
112
|
+
results.length > 1 &&
|
|
113
|
+
source.length > 1 &&
|
|
114
|
+
results.length !== source.length
|
|
115
|
+
) {
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// If the target block already has allowedBlocks, we don't need to preserve
|
|
120
|
+
// the source allowedBlocks.
|
|
121
|
+
if ( result.attributes.allowedBlocks ) {
|
|
122
|
+
return result;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const sourceAllowedBlocks = source[ index ]?.attributes?.allowedBlocks;
|
|
126
|
+
|
|
127
|
+
if ( ! sourceAllowedBlocks ) {
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const blockType = getBlockType( result.name );
|
|
132
|
+
const destinationAllowedBlocks = blockType?.allowedBlocks || [];
|
|
133
|
+
|
|
134
|
+
if ( ! destinationAllowedBlocks.length ) {
|
|
135
|
+
return {
|
|
136
|
+
...result,
|
|
137
|
+
attributes: {
|
|
138
|
+
...result.attributes,
|
|
139
|
+
allowedBlocks: sourceAllowedBlocks,
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Filter out any source allowed blocks that are not defined in the destination allowed blocks.
|
|
145
|
+
const filteredSourceAllowedBlocks = sourceAllowedBlocks.filter( ( block ) =>
|
|
146
|
+
destinationAllowedBlocks.includes( block )
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
...result,
|
|
151
|
+
attributes: {
|
|
152
|
+
...result.attributes,
|
|
153
|
+
allowedBlocks: filteredSourceAllowedBlocks,
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
addFilter(
|
|
159
|
+
'blocks.switchToBlockType.transformedBlock',
|
|
160
|
+
'core/allowedBlocks/addTransforms',
|
|
161
|
+
addTransforms
|
|
162
|
+
);
|
package/src/hooks/fit-text.js
CHANGED
|
@@ -66,12 +66,12 @@ function useFitText( { fitText, name, clientId } ) {
|
|
|
66
66
|
// Any attribute may change the available space.
|
|
67
67
|
const blockAttributes = useSelect(
|
|
68
68
|
( select ) => {
|
|
69
|
-
if ( ! clientId ) {
|
|
69
|
+
if ( ! clientId || ! hasFitTextSupport || ! fitText ) {
|
|
70
70
|
return;
|
|
71
71
|
}
|
|
72
72
|
return select( blockEditorStore ).getBlockAttributes( clientId );
|
|
73
73
|
},
|
|
74
|
-
[ clientId ]
|
|
74
|
+
[ clientId, hasFitTextSupport, fitText ]
|
|
75
75
|
);
|
|
76
76
|
|
|
77
77
|
const applyFitText = useCallback( () => {
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import {
|
|
5
|
+
getBlockTypes,
|
|
6
|
+
registerBlockType,
|
|
7
|
+
unregisterBlockType,
|
|
8
|
+
} from '@wordpress/blocks';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Internal dependencies
|
|
12
|
+
*/
|
|
13
|
+
import { addTransforms } from '../allowed-blocks';
|
|
14
|
+
|
|
15
|
+
describe( 'allowedBlocks', () => {
|
|
16
|
+
afterEach( () => {
|
|
17
|
+
getBlockTypes().forEach( ( block ) => {
|
|
18
|
+
unregisterBlockType( block.name );
|
|
19
|
+
} );
|
|
20
|
+
} );
|
|
21
|
+
|
|
22
|
+
describe( 'addTransforms()', () => {
|
|
23
|
+
it( 'should not preserve allowedBlocks in wrapping transforms', () => {
|
|
24
|
+
registerBlockType( 'core/bar', {
|
|
25
|
+
title: 'Bar',
|
|
26
|
+
supports: { allowedBlocks: true },
|
|
27
|
+
} );
|
|
28
|
+
|
|
29
|
+
const source = [
|
|
30
|
+
{
|
|
31
|
+
name: 'core/foo',
|
|
32
|
+
attributes: {
|
|
33
|
+
allowedBlocks: [ 'core/paragraph', 'core/heading' ],
|
|
34
|
+
},
|
|
35
|
+
innerBlocks: [],
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
name: 'core/foo',
|
|
39
|
+
attributes: { allowedBlocks: [ 'core/paragraph' ] },
|
|
40
|
+
innerBlocks: [],
|
|
41
|
+
},
|
|
42
|
+
];
|
|
43
|
+
const result = {
|
|
44
|
+
name: 'core/bar',
|
|
45
|
+
attributes: {},
|
|
46
|
+
innerBlocks: source,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const transformed = addTransforms( result, source, 0, [ result ] );
|
|
50
|
+
|
|
51
|
+
expect( transformed.attributes.allowedBlocks ).toBeUndefined();
|
|
52
|
+
} );
|
|
53
|
+
|
|
54
|
+
it( 'should not preserve allowedBlocks in one-to-many transforms', () => {
|
|
55
|
+
registerBlockType( 'core/bar', {
|
|
56
|
+
title: 'Bar',
|
|
57
|
+
supports: { allowedBlocks: true },
|
|
58
|
+
} );
|
|
59
|
+
|
|
60
|
+
const source = [
|
|
61
|
+
{
|
|
62
|
+
name: 'core/foo',
|
|
63
|
+
attributes: {
|
|
64
|
+
allowedBlocks: [ 'core/paragraph', 'core/heading' ],
|
|
65
|
+
},
|
|
66
|
+
innerBlocks: [],
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
const results = [
|
|
70
|
+
{ name: 'core/bar', attributes: {}, innerBlocks: [] },
|
|
71
|
+
{ name: 'core/bar', attributes: {}, innerBlocks: [] },
|
|
72
|
+
];
|
|
73
|
+
|
|
74
|
+
const transformed = addTransforms(
|
|
75
|
+
results[ 0 ],
|
|
76
|
+
source,
|
|
77
|
+
0,
|
|
78
|
+
results
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
expect( transformed.attributes.allowedBlocks ).toBeUndefined();
|
|
82
|
+
} );
|
|
83
|
+
|
|
84
|
+
it( 'should not preserve allowedBlocks in many-to-one transforms', () => {
|
|
85
|
+
registerBlockType( 'core/bar', {
|
|
86
|
+
title: 'Bar',
|
|
87
|
+
supports: { allowedBlocks: true },
|
|
88
|
+
} );
|
|
89
|
+
|
|
90
|
+
const source = [
|
|
91
|
+
{
|
|
92
|
+
name: 'core/foo',
|
|
93
|
+
attributes: {
|
|
94
|
+
allowedBlocks: [ 'core/paragraph', 'core/heading' ],
|
|
95
|
+
},
|
|
96
|
+
innerBlocks: [],
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
name: 'core/foo',
|
|
100
|
+
attributes: { allowedBlocks: [ 'core/paragraph' ] },
|
|
101
|
+
innerBlocks: [],
|
|
102
|
+
},
|
|
103
|
+
];
|
|
104
|
+
const result = {
|
|
105
|
+
name: 'core/bar',
|
|
106
|
+
attributes: {},
|
|
107
|
+
innerBlocks: [],
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const transformed = addTransforms( result, source, 0, [ result ] );
|
|
111
|
+
|
|
112
|
+
expect( transformed.attributes.allowedBlocks ).toBeUndefined();
|
|
113
|
+
} );
|
|
114
|
+
|
|
115
|
+
it( 'should not preserve allowedBlocks in many-to-many transforms with different counts', () => {
|
|
116
|
+
registerBlockType( 'core/bar', {
|
|
117
|
+
title: 'Bar',
|
|
118
|
+
supports: { allowedBlocks: true },
|
|
119
|
+
} );
|
|
120
|
+
|
|
121
|
+
const source = [
|
|
122
|
+
{
|
|
123
|
+
name: 'core/foo',
|
|
124
|
+
attributes: {
|
|
125
|
+
allowedBlocks: [ 'core/paragraph', 'core/heading' ],
|
|
126
|
+
},
|
|
127
|
+
innerBlocks: [],
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
name: 'core/foo',
|
|
131
|
+
attributes: { allowedBlocks: [ 'core/paragraph' ] },
|
|
132
|
+
innerBlocks: [],
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
name: 'core/foo',
|
|
136
|
+
attributes: { allowedBlocks: [ 'core/heading' ] },
|
|
137
|
+
innerBlocks: [],
|
|
138
|
+
},
|
|
139
|
+
];
|
|
140
|
+
const results = [
|
|
141
|
+
{ name: 'core/bar', attributes: {}, innerBlocks: [] },
|
|
142
|
+
{ name: 'core/bar', attributes: {}, innerBlocks: [] },
|
|
143
|
+
];
|
|
144
|
+
|
|
145
|
+
const [ firstTransformed, secondTransformed ] = results.map(
|
|
146
|
+
( result, index ) =>
|
|
147
|
+
addTransforms( result, source, index, results )
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
expect( firstTransformed.attributes.allowedBlocks ).toBeUndefined();
|
|
151
|
+
expect(
|
|
152
|
+
secondTransformed.attributes.allowedBlocks
|
|
153
|
+
).toBeUndefined();
|
|
154
|
+
} );
|
|
155
|
+
|
|
156
|
+
it( 'should preserve allowedBlocks in many-to-many transforms with same counts', () => {
|
|
157
|
+
registerBlockType( 'core/bar', {
|
|
158
|
+
title: 'Bar',
|
|
159
|
+
supports: { allowedBlocks: true },
|
|
160
|
+
} );
|
|
161
|
+
|
|
162
|
+
const source = [
|
|
163
|
+
{
|
|
164
|
+
name: 'core/foo',
|
|
165
|
+
attributes: {
|
|
166
|
+
allowedBlocks: [ 'core/image', 'core/heading' ],
|
|
167
|
+
},
|
|
168
|
+
innerBlocks: [],
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
name: 'core/foo',
|
|
172
|
+
attributes: { allowedBlocks: [ 'core/paragraph' ] },
|
|
173
|
+
innerBlocks: [],
|
|
174
|
+
},
|
|
175
|
+
];
|
|
176
|
+
const results = [
|
|
177
|
+
{ name: 'core/bar', attributes: {}, innerBlocks: [] },
|
|
178
|
+
{ name: 'core/bar', attributes: {}, innerBlocks: [] },
|
|
179
|
+
];
|
|
180
|
+
|
|
181
|
+
const [ firstTransformed, secondTransformed ] = results.map(
|
|
182
|
+
( result, index ) =>
|
|
183
|
+
addTransforms( result, source, index, results )
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
expect( firstTransformed.attributes.allowedBlocks ).toEqual( [
|
|
187
|
+
'core/image',
|
|
188
|
+
'core/heading',
|
|
189
|
+
] );
|
|
190
|
+
expect( secondTransformed.attributes.allowedBlocks ).toEqual( [
|
|
191
|
+
'core/paragraph',
|
|
192
|
+
] );
|
|
193
|
+
} );
|
|
194
|
+
|
|
195
|
+
it( "should filter allowedBlocks based on destination block's allowedBlocks", () => {
|
|
196
|
+
registerBlockType( 'core/bar', {
|
|
197
|
+
title: 'Bar',
|
|
198
|
+
supports: { allowedBlocks: true },
|
|
199
|
+
allowedBlocks: [ 'core/paragraph' ],
|
|
200
|
+
} );
|
|
201
|
+
|
|
202
|
+
const source = [
|
|
203
|
+
{
|
|
204
|
+
name: 'core/foo',
|
|
205
|
+
attributes: {
|
|
206
|
+
allowedBlocks: [
|
|
207
|
+
'core/paragraph',
|
|
208
|
+
'core/heading',
|
|
209
|
+
'core/image',
|
|
210
|
+
],
|
|
211
|
+
},
|
|
212
|
+
innerBlocks: [],
|
|
213
|
+
},
|
|
214
|
+
];
|
|
215
|
+
const result = {
|
|
216
|
+
name: 'core/bar',
|
|
217
|
+
attributes: {},
|
|
218
|
+
innerBlocks: [],
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const transformed = addTransforms( result, source, 0, [ result ] );
|
|
222
|
+
|
|
223
|
+
expect( transformed.attributes.allowedBlocks ).toEqual( [
|
|
224
|
+
'core/paragraph',
|
|
225
|
+
] );
|
|
226
|
+
} );
|
|
227
|
+
|
|
228
|
+
it( 'should not override existing allowedBlocks in target block', () => {
|
|
229
|
+
registerBlockType( 'core/bar', {
|
|
230
|
+
title: 'Bar',
|
|
231
|
+
supports: { allowedBlocks: true },
|
|
232
|
+
} );
|
|
233
|
+
|
|
234
|
+
const source = [
|
|
235
|
+
{
|
|
236
|
+
name: 'core/foo',
|
|
237
|
+
attributes: { allowedBlocks: [ 'core/paragraph' ] },
|
|
238
|
+
innerBlocks: [],
|
|
239
|
+
},
|
|
240
|
+
];
|
|
241
|
+
const result = {
|
|
242
|
+
name: 'core/bar',
|
|
243
|
+
attributes: { allowedBlocks: [ 'core/heading' ] },
|
|
244
|
+
innerBlocks: [],
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
const transformed = addTransforms( result, source, 0, [ result ] );
|
|
248
|
+
|
|
249
|
+
expect( transformed.attributes.allowedBlocks ).toEqual( [
|
|
250
|
+
'core/heading',
|
|
251
|
+
] );
|
|
252
|
+
} );
|
|
253
|
+
|
|
254
|
+
it( 'should not preserve allowedBlocks when target block does not support allowedBlocks', () => {
|
|
255
|
+
registerBlockType( 'core/bar', {
|
|
256
|
+
title: 'Bar',
|
|
257
|
+
} );
|
|
258
|
+
|
|
259
|
+
const source = [
|
|
260
|
+
{
|
|
261
|
+
name: 'core/foo',
|
|
262
|
+
attributes: {
|
|
263
|
+
allowedBlocks: [ 'core/paragraph', 'core/heading' ],
|
|
264
|
+
},
|
|
265
|
+
innerBlocks: [],
|
|
266
|
+
},
|
|
267
|
+
];
|
|
268
|
+
const result = {
|
|
269
|
+
name: 'core/bar',
|
|
270
|
+
attributes: {},
|
|
271
|
+
innerBlocks: [],
|
|
272
|
+
};
|
|
273
|
+
const transformed = addTransforms( result, source, 0, [ result ] );
|
|
274
|
+
|
|
275
|
+
expect( transformed.attributes.allowedBlocks ).toBeUndefined();
|
|
276
|
+
} );
|
|
277
|
+
} );
|
|
278
|
+
} );
|