Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -937,12 +937,12 @@ Applies a series of CSS rule transforms to wrap selectors inside a given class a
_Parameters_

- _styles_ `EditorStyle[]`: CSS rules.
- _wrapperSelector_ `string`: Wrapper selector.
- _wrapperSelector_ Wrapper selector.
Comment thread
manzoorwanijk marked this conversation as resolved.
Resolved
- _transformOptions_ `TransformOptions`: Additional options for style transformation.

_Returns_

- `Array`: converted rules.
- converted rules.

### Typewriter

Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
"@testing-library/dom": "^10.4.1",
"@testing-library/react": "^16.3.2",
"@testing-library/user-event": "^14.6.1",
"@types/postcss-prefix-selector": "^1.16.3",
"deep-freeze": "^0.0.1"
},
"peerDependencies": {
Expand Down
46 changes: 0 additions & 46 deletions packages/block-editor/src/utils/block-bindings.js

This file was deleted.

51 changes: 51 additions & 0 deletions packages/block-editor/src/utils/block-bindings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
type Binding = { source?: string };
type Bindings = Record< string, Binding >;

const DEFAULT_ATTRIBUTE = '__default';
const PATTERN_OVERRIDES_SOURCE = 'core/pattern-overrides';

/**
* Checks if the block has the `__default` binding for pattern overrides.
*
* @param bindings A block's bindings from the metadata attribute.
*
* @return Whether the block has the `__default` binding for pattern overrides.
*/
export function hasPatternOverridesDefaultBinding(
bindings: Bindings | undefined | null
): bindings is Bindings {
return bindings?.[ DEFAULT_ATTRIBUTE ]?.source === PATTERN_OVERRIDES_SOURCE;
}

/**
* Returns the bindings with the `__default` binding for pattern overrides
* replaced with the full-set of supported attributes. e.g.:
*
* - bindings passed in: `{ __default: { source: 'core/pattern-overrides' } }`
* - bindings returned: `{ content: { source: 'core/pattern-overrides' } }`
*
* @param bindings A block's bindings from the metadata attribute.
* @param supportedAttributes The block's attributes which are supported by block bindings.
*
* @return The bindings with default replaced for pattern overrides.
*/
export function replacePatternOverridesDefaultBinding(
bindings: Bindings | undefined | null,
supportedAttributes: string[]
) {
// The `__default` binding currently only works for pattern overrides.
if ( ! hasPatternOverridesDefaultBinding( bindings ) ) {
return bindings;
}

const bindingsWithDefaults: Bindings = {};
for ( const attributeName of supportedAttributes ) {
// If the block has mixed binding sources, retain any non pattern override bindings.
const bindingSource = bindings[ attributeName ]
? bindings[ attributeName ]
: { source: PATTERN_OVERRIDES_SOURCE };
bindingsWithDefaults[ attributeName ] = bindingSource;
}

return bindingsWithDefaults;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
* Returns `undefined` for plain values, non-strings, or any other
* unrecognised format.
*
* @param {*} rawValue Raw style value stored in the style object.
* @param {'color'|'gradient'} type Preset type, e.g. `'color'` or `'gradient'`.
* @return {string|undefined} The palette slug, or undefined.
* @param rawValue Raw style value stored in the style object.
* @param type Preset type, e.g. `'color'` or `'gradient'`.
* @return The palette slug, or undefined.
*/
export function extractPresetSlug( rawValue, type ) {
export function extractPresetSlug(
rawValue: unknown,
type: 'color' | 'gradient'
) {
if ( typeof rawValue !== 'string' ) {
return undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ const BLOCK_APPENDER_CLASS = '.block-editor-button-block-appender';
/**
* Returns true if two elements are contained within the same block.
*
* @param {Element} a First element.
* @param {Element} b Second element.
* @param a First element.
* @param b Second element.
*
* @return {boolean} Whether elements are in the same block.
* @return Whether elements are in the same block.
*/
export function isInSameBlock( a, b ) {
export function isInSameBlock( a: Element, b: Element ) {
return a.closest( BLOCK_SELECTOR ) === b.closest( BLOCK_SELECTOR );
}

/**
* Returns true if an element is considered part of the block and not its inner
* blocks or appender.
*
* @param {Element} blockElement Block container element.
* @param {Element} element Element.
* @param blockElement Block container element.
* @param element Element.
*
* @return {boolean} Whether an element is considered part of the block and not
* its inner blocks or appender.
* @return Whether an element is considered part of the block and not
* its inner blocks or appender.
*/
export function isInsideRootBlock( blockElement, element ) {
export function isInsideRootBlock( blockElement: Element, element: Element ) {
const parentBlock = element.closest(
[ BLOCK_SELECTOR, APPENDER_SELECTOR, BLOCK_APPENDER_CLASS ].join( ',' )
);
Expand All @@ -34,12 +34,12 @@ export function isInsideRootBlock( blockElement, element ) {
/**
* Finds the block client ID given any DOM node inside the block.
*
* @param {Node?} node DOM node.
* @param node DOM node.
*
* @return {string|undefined} Client ID or undefined if the node is not part of
* a block.
* @return Client ID or undefined if the node is not part of
* a block.
*/
export function getBlockClientId( node ) {
export function getBlockClientId( node: Node | null ) {
while ( node && node.nodeType !== node.ELEMENT_NODE ) {
node = node.parentNode;
}
Expand All @@ -48,7 +48,7 @@ export function getBlockClientId( node ) {
return;
}

const elementNode = /** @type {Element} */ ( node );
const elementNode = node as Element;
const blockNode = elementNode.closest( BLOCK_SELECTOR );

if ( ! blockNode ) {
Expand All @@ -61,11 +61,11 @@ export function getBlockClientId( node ) {
/**
* Calculates the union of two rectangles.
*
* @param {DOMRect} rect1 First rectangle.
* @param {DOMRect} rect2 Second rectangle.
* @return {DOMRect} Union of the two rectangles.
* @param rect1 First rectangle.
* @param rect2 Second rectangle.
* @return Union of the two rectangles.
*/
export function rectUnion( rect1, rect2 ) {
export function rectUnion( rect1: DOMRect, rect2: DOMRect ) {
const left = Math.min( rect1.left, rect2.left );
const right = Math.max( rect1.right, rect2.right );
const bottom = Math.max( rect1.bottom, rect2.bottom );
Expand All @@ -77,10 +77,10 @@ export function rectUnion( rect1, rect2 ) {
/**
* Returns whether an element is visible.
*
* @param {Element} element Element.
* @return {boolean} Whether the element is visible.
* @param element Element.
* @return Whether the element is visible.
*/
export function isElementVisible( element ) {
export function isElementVisible( element: Element ) {
const viewport = element.ownerDocument.defaultView;
if ( ! viewport ) {
return false;
Expand Down Expand Up @@ -121,10 +121,10 @@ export function isElementVisible( element ) {
/**
* Checks if the element is scrollable.
*
* @param {Element} element Element.
* @return {boolean} True if the element is scrollable.
* @param element Element.
* @return True if the element is scrollable.
*/
function isScrollable( element ) {
function isScrollable( element: Element ) {
const style = window.getComputedStyle( element );
return (
style.overflowX === 'auto' ||
Expand All @@ -145,10 +145,10 @@ export const WITH_OVERFLOW_ELEMENT_BLOCKS = [ 'core/navigation' ];
* The returned rect represents the full extent of the element and its visible
* children, which may extend beyond the viewport.
*
* @param {Element} element Element.
* @return {DOMRect} Bounding client rect of the element and its visible children.
* @param element Element.
* @return Bounding client rect of the element and its visible children.
*/
export function getElementBounds( element ) {
export function getElementBounds( element: Element ) {
const viewport = element.ownerDocument.defaultView;

if ( ! viewport ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ import { store, getElement, getContext } from '@wordpress/interactivity';
*/
import { optimizeFitText } from './fit-text-utils';

interface FitTextContext {
fontSize?: number;
}

// Initialize via Interactivity API for client-side navigation
store( 'core/fit-text', {
callbacks: {
init() {
const context = getContext();
const { ref } = getElement();
const context = getContext< FitTextContext >();
const { ref } = getElement() as { ref: HTMLElement | null };

const applyFontSize = ( fontSize ) => {
const applyFontSize = ( fontSize: number ) => {
if ( ! ref ) {
return;
}
if ( fontSize === 0 ) {
ref.style.fontSize = '';
} else {
Expand All @@ -33,7 +40,7 @@ store( 'core/fit-text', {
context.fontSize = optimizeFitText( ref, applyFontSize );

// Starts ResizeObserver to handle dynamic resizing.
if ( window.ResizeObserver && ref.parentElement ) {
if ( window.ResizeObserver && ref?.parentElement ) {
const resizeObserver = new window.ResizeObserver( () => {
context.fontSize = optimizeFitText( ref, applyFontSize );
} );
Expand All @@ -47,6 +54,7 @@ store( 'core/fit-text', {
}
};
}
return undefined;
},
},
} );
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
* Uses callback-based approach for maximum code reuse between editor and frontend.
*/

type ApplyFontSize = ( size: number ) => void;

/**
* Find optimal font size using simple binary search between 0-2400px.
*
* @param {HTMLElement} textElement The text element
* @param {Function} applyFontSize Function that receives font size in pixels
* @return {number} Optimal font size
* @param textElement The text element
* @param applyFontSize Function that receives font size in pixels
* @return Optimal font size
*/
function findOptimalFontSize( textElement, applyFontSize ) {
function findOptimalFontSize(
textElement: HTMLElement,
applyFontSize: ApplyFontSize
) {
const alreadyHasScrollableHeight =
textElement.scrollHeight > textElement.clientHeight;
let minSize = 0;
Expand Down Expand Up @@ -85,10 +90,13 @@ function findOptimalFontSize( textElement, applyFontSize ) {
* Complete fit text optimization for a single text element.
* Handles the full flow using callbacks for font size application.
*
* @param {HTMLElement} textElement The text element (paragraph, heading, etc.)
* @param {Function} applyFontSize Function that receives font size in pixels (0 to clear, >0 to apply)
* @param textElement The text element (paragraph, heading, etc.)
* @param applyFontSize Function that receives font size in pixels (0 to clear, >0 to apply)
*/
export function optimizeFitText( textElement, applyFontSize ) {
export function optimizeFitText(
textElement: HTMLElement | null,
applyFontSize: ApplyFontSize
) {
if ( ! textElement ) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
*/
import { _x } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import type { FormattedFont } from './types';

/**
* Formats font styles to human readable names.
*
* @param {string} fontStyle font style string
* @return {Object} new object with formatted font style
* @param fontStyle font style string
* @return new object with formatted font style
*/
export function formatFontStyle( fontStyle ) {
export function formatFontStyle(
fontStyle: FormattedFont | string | undefined
): FormattedFont {
if ( ! fontStyle ) {
return {};
}
Expand Down
Loading
Loading