Skip to content

Commit 53df906

Browse files
committed
Adjust width of the podcast block in the editor
1 parent 8ae18f6 commit 53df906

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

assets/js/edit.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const ALLOWED_MEDIA_TYPES = ['audio'];
2323
const { select } = wp.data;
2424

2525
import { Button } from '@wordpress/components';
26-
import { useState, useEffect } from '@wordpress/element';
26+
import { useState, useEffect, useCallback, useRef } from '@wordpress/element';
2727
import { dispatch, useSelect, useDispatch } from '@wordpress/data';
2828
import { createBlock } from '@wordpress/blocks';
2929

@@ -201,6 +201,9 @@ function Edit( props ) {
201201
}
202202
}
203203

204+
const adminMenuElement = document.getElementById('adminmenuwrap');
205+
const blockRef = useRef(document.querySelector('.wp-block-podcasting-podcast-outer'));
206+
204207
// If the user changes one of the toggles, the checkDisplaySettings function will be called to check if any of the display settings are enabled.
205208
// If at least one of the display settings are enabled, then we want to show the More/Less button.
206209
useEffect(() => {
@@ -217,6 +220,28 @@ function Edit( props ) {
217220
setShowPodcastMeta(false)
218221
}, [isDocked])
219222

223+
/**
224+
* Update the position and width of the block depending on the isDocked value.
225+
*
226+
* @param {string} isDocked - The value of the isDocked attribute.
227+
* @param {number} containerWidth - The width of the container.
228+
*/
229+
const updateBlockPosition = useCallback((isDocked, containerWidth) => {
230+
if (isDocked === 'none') {
231+
blockRef.current.style.left = '0px';
232+
blockRef.current.style.width = `auto`;
233+
234+
return;
235+
}
236+
237+
const blockElementStyle = window.getComputedStyle(blockRef.current);
238+
const paddingLeft = parseInt(blockElementStyle.paddingLeft, 10);
239+
const paddingRight = parseInt(blockElementStyle.paddingRight, 10);
240+
const left = adminMenuElement ? adminMenuElement.offsetWidth : 0;
241+
blockRef.current.style.left = `${left}px`;
242+
blockRef.current.style.width = `${containerWidth - paddingLeft - paddingRight - 2}px`;
243+
}, [adminMenuElement]);
244+
220245
useEffect(() => {
221246
// Remove any existing classes
222247
document.body.classList.remove('has-docked-top', 'has-docked-bottom', 'docked-in-editor');
@@ -227,7 +252,18 @@ function Edit( props ) {
227252
} else if (isDocked === 'bottom') {
228253
document.body.classList.add('has-docked-bottom', 'docked-in-editor');
229254
}
230-
}, [isDocked]); // Run this effect when isDocked changes
255+
256+
// Update the position and width of the block depending on the isDocked value.
257+
const editorArea = document.querySelector('.editor-visual-editor');
258+
if (editorArea) {
259+
const resizeObserver = new ResizeObserver(entries => {
260+
for (let entry of entries) {
261+
updateBlockPosition(isDocked, entry.contentRect.width);
262+
}
263+
});
264+
resizeObserver.observe(editorArea);
265+
}
266+
}, [isDocked, updateBlockPosition]); // Run this effect when isDocked changes
231267

232268
return (
233269
<Fragment>
@@ -490,7 +526,7 @@ function Edit( props ) {
490526
</PanelRow>
491527
</PanelBody>
492528
</InspectorControls>
493-
<div className={`wp-block-podcasting-podcast-outer ${dockedClass}`}>
529+
<div ref={blockRef} className={`wp-block-podcasting-podcast-outer ${dockedClass}`}>
494530
{src ? (
495531
<>
496532
<div className="wp-block-podcasting-podcast__container">

0 commit comments

Comments
 (0)