@wordpress/block-editor 14.7.1-next.082ed6819.0 → 14.8.0
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/CHANGELOG.md +2 -0
- package/build/components/block-toolbar/index.js +11 -4
- package/build/components/block-toolbar/index.js.map +1 -1
- package/build/components/iframe/index.js +12 -216
- package/build/components/iframe/index.js.map +1 -1
- package/build/components/iframe/use-scale-canvas.js +377 -0
- package/build/components/iframe/use-scale-canvas.js.map +1 -0
- package/build/components/image-editor/use-save-image.js +22 -3
- package/build/components/image-editor/use-save-image.js.map +1 -1
- package/build-module/components/block-toolbar/index.js +11 -4
- package/build-module/components/block-toolbar/index.js.map +1 -1
- package/build-module/components/iframe/index.js +14 -218
- package/build-module/components/iframe/index.js.map +1 -1
- package/build-module/components/iframe/use-scale-canvas.js +371 -0
- package/build-module/components/iframe/use-scale-canvas.js.map +1 -0
- package/build-module/components/image-editor/use-save-image.js +22 -3
- package/build-module/components/image-editor/use-save-image.js.map +1 -1
- package/build-style/content-rtl.css +7 -22
- package/build-style/content.css +7 -22
- package/build-style/style-rtl.css +30 -0
- package/build-style/style.css +30 -0
- package/package.json +31 -31
- package/src/components/block-canvas/style.scss +2 -1
- package/src/components/block-toolbar/index.js +8 -0
- package/src/components/block-tools/style.scss +39 -0
- package/src/components/color-palette/test/__snapshots__/control.js.snap +2 -2
- package/src/components/iframe/content.scss +34 -41
- package/src/components/iframe/index.js +13 -313
- package/src/components/iframe/use-scale-canvas.js +468 -0
- package/src/components/image-editor/use-save-image.js +27 -2
|
@@ -12,16 +12,9 @@ import {
|
|
|
12
12
|
forwardRef,
|
|
13
13
|
useMemo,
|
|
14
14
|
useEffect,
|
|
15
|
-
useRef,
|
|
16
15
|
} from '@wordpress/element';
|
|
17
16
|
import { __ } from '@wordpress/i18n';
|
|
18
|
-
import {
|
|
19
|
-
useResizeObserver,
|
|
20
|
-
useMergeRefs,
|
|
21
|
-
useRefEffect,
|
|
22
|
-
useDisabled,
|
|
23
|
-
useReducedMotion,
|
|
24
|
-
} from '@wordpress/compose';
|
|
17
|
+
import { useMergeRefs, useRefEffect, useDisabled } from '@wordpress/compose';
|
|
25
18
|
import { __experimentalStyleProvider as StyleProvider } from '@wordpress/components';
|
|
26
19
|
import { useSelect } from '@wordpress/data';
|
|
27
20
|
|
|
@@ -31,6 +24,7 @@ import { useSelect } from '@wordpress/data';
|
|
|
31
24
|
import { useBlockSelectionClearer } from '../block-selection-clearer';
|
|
32
25
|
import { useWritingFlow } from '../writing-flow';
|
|
33
26
|
import { getCompatibilityStyles } from './get-compatibility-styles';
|
|
27
|
+
import { useScaleCanvas } from './use-scale-canvas';
|
|
34
28
|
import { store as blockEditorStore } from '../../store';
|
|
35
29
|
|
|
36
30
|
function bubbleEvent( event, Constructor, frame ) {
|
|
@@ -124,15 +118,9 @@ function Iframe( {
|
|
|
124
118
|
const { styles = '', scripts = '' } = resolvedAssets;
|
|
125
119
|
/** @type {[Document, import('react').Dispatch<Document>]} */
|
|
126
120
|
const [ iframeDocument, setIframeDocument ] = useState();
|
|
127
|
-
const initialContainerWidthRef = useRef( 0 );
|
|
128
121
|
const [ bodyClasses, setBodyClasses ] = useState( [] );
|
|
129
122
|
const clearerRef = useBlockSelectionClearer();
|
|
130
123
|
const [ before, writingFlowRef, after ] = useWritingFlow();
|
|
131
|
-
const [ contentResizeListener, { height: contentHeight } ] =
|
|
132
|
-
useResizeObserver();
|
|
133
|
-
const [ containerResizeListener, { width: containerWidth } ] =
|
|
134
|
-
useResizeObserver();
|
|
135
|
-
const prefersReducedMotion = useReducedMotion();
|
|
136
124
|
|
|
137
125
|
const setRef = useRefEffect( ( node ) => {
|
|
138
126
|
node._load = () => {
|
|
@@ -228,61 +216,16 @@ function Iframe( {
|
|
|
228
216
|
};
|
|
229
217
|
}, [] );
|
|
230
218
|
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
return () => {
|
|
242
|
-
nodeWindow.removeEventListener( 'resize', onResize );
|
|
243
|
-
};
|
|
244
|
-
}, [] );
|
|
245
|
-
|
|
246
|
-
const [ windowInnerWidth, setWindowInnerWidth ] = useState();
|
|
247
|
-
|
|
248
|
-
const windowResizeRef = useRefEffect( ( node ) => {
|
|
249
|
-
const nodeWindow = node.ownerDocument.defaultView;
|
|
250
|
-
|
|
251
|
-
setWindowInnerWidth( nodeWindow.innerWidth );
|
|
252
|
-
const onResize = () => {
|
|
253
|
-
setWindowInnerWidth( nodeWindow.innerWidth );
|
|
254
|
-
};
|
|
255
|
-
nodeWindow.addEventListener( 'resize', onResize );
|
|
256
|
-
return () => {
|
|
257
|
-
nodeWindow.removeEventListener( 'resize', onResize );
|
|
258
|
-
};
|
|
259
|
-
}, [] );
|
|
260
|
-
|
|
261
|
-
const isZoomedOut = scale !== 1;
|
|
262
|
-
|
|
263
|
-
useEffect( () => {
|
|
264
|
-
if ( ! isZoomedOut ) {
|
|
265
|
-
initialContainerWidthRef.current = containerWidth;
|
|
266
|
-
}
|
|
267
|
-
}, [ containerWidth, isZoomedOut ] );
|
|
268
|
-
|
|
269
|
-
const scaleContainerWidth = Math.max(
|
|
270
|
-
initialContainerWidthRef.current,
|
|
271
|
-
containerWidth
|
|
272
|
-
);
|
|
273
|
-
|
|
274
|
-
const frameSizeValue = parseInt( frameSize );
|
|
275
|
-
|
|
276
|
-
const maxWidth = 750;
|
|
277
|
-
const scaleValue =
|
|
278
|
-
scale === 'auto-scaled'
|
|
279
|
-
? ( Math.min( containerWidth, maxWidth ) - frameSizeValue * 2 ) /
|
|
280
|
-
scaleContainerWidth
|
|
281
|
-
: scale;
|
|
282
|
-
|
|
283
|
-
const prevScaleRef = useRef( scaleValue );
|
|
284
|
-
const prevFrameSizeRef = useRef( frameSizeValue );
|
|
285
|
-
const prevClientHeightRef = useRef( /* Initialized in the useEffect. */ );
|
|
219
|
+
const {
|
|
220
|
+
contentResizeListener,
|
|
221
|
+
containerResizeListener,
|
|
222
|
+
isZoomedOut,
|
|
223
|
+
scaleContainerWidth,
|
|
224
|
+
} = useScaleCanvas( {
|
|
225
|
+
scale,
|
|
226
|
+
frameSize: parseInt( frameSize ),
|
|
227
|
+
iframeDocument,
|
|
228
|
+
} );
|
|
286
229
|
|
|
287
230
|
const disabledRef = useDisabled( { isDisabled: ! readonly } );
|
|
288
231
|
const bodyRef = useMergeRefs( [
|
|
@@ -291,10 +234,6 @@ function Iframe( {
|
|
|
291
234
|
clearerRef,
|
|
292
235
|
writingFlowRef,
|
|
293
236
|
disabledRef,
|
|
294
|
-
// Avoid resize listeners when not needed, these will trigger
|
|
295
|
-
// unnecessary re-renders when animating the iframe width, or when
|
|
296
|
-
// expanding preview iframes.
|
|
297
|
-
isZoomedOut ? iframeResizeRef : null,
|
|
298
237
|
] );
|
|
299
238
|
|
|
300
239
|
// Correct doctype is required to enable rendering in standards
|
|
@@ -336,245 +275,6 @@ function Iframe( {
|
|
|
336
275
|
|
|
337
276
|
useEffect( () => cleanup, [ cleanup ] );
|
|
338
277
|
|
|
339
|
-
useEffect( () => {
|
|
340
|
-
if (
|
|
341
|
-
! iframeDocument ||
|
|
342
|
-
// HACK: Checking if isZoomedOut differs from prevIsZoomedOut here
|
|
343
|
-
// instead of the dependency array to appease the linter.
|
|
344
|
-
( scaleValue === 1 ) === ( prevScaleRef.current === 1 )
|
|
345
|
-
) {
|
|
346
|
-
return;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
// Unscaled height of the current iframe container.
|
|
350
|
-
const clientHeight = iframeDocument.documentElement.clientHeight;
|
|
351
|
-
|
|
352
|
-
// Scaled height of the current iframe content.
|
|
353
|
-
const scrollHeight = iframeDocument.documentElement.scrollHeight;
|
|
354
|
-
|
|
355
|
-
// Previous scale value.
|
|
356
|
-
const prevScale = prevScaleRef.current;
|
|
357
|
-
|
|
358
|
-
// Unscaled size of the previous padding around the iframe content.
|
|
359
|
-
const prevFrameSize = prevFrameSizeRef.current;
|
|
360
|
-
|
|
361
|
-
// Unscaled height of the previous iframe container.
|
|
362
|
-
const prevClientHeight = prevClientHeightRef.current ?? clientHeight;
|
|
363
|
-
|
|
364
|
-
// We can't trust the set value from contentHeight, as it was measured
|
|
365
|
-
// before the zoom out mode was changed. After zoom out mode is changed,
|
|
366
|
-
// appenders may appear or disappear, so we need to get the height from
|
|
367
|
-
// the iframe at this point when we're about to animate the zoom out.
|
|
368
|
-
// The iframe scrollTop, scrollHeight, and clientHeight will all be
|
|
369
|
-
// accurate. The client height also does change when the zoom out mode
|
|
370
|
-
// is toggled, as the bottom bar about selecting the template is
|
|
371
|
-
// added/removed when toggling zoom out mode.
|
|
372
|
-
const scrollTop = iframeDocument.documentElement.scrollTop;
|
|
373
|
-
|
|
374
|
-
// Step 0: Start with the current scrollTop.
|
|
375
|
-
let scrollTopNext = scrollTop;
|
|
376
|
-
|
|
377
|
-
// Step 1: Undo the effects of the previous scale and frame around the
|
|
378
|
-
// midpoint of the visible area.
|
|
379
|
-
scrollTopNext =
|
|
380
|
-
( scrollTopNext + prevClientHeight / 2 - prevFrameSize ) /
|
|
381
|
-
prevScale -
|
|
382
|
-
prevClientHeight / 2;
|
|
383
|
-
|
|
384
|
-
// Step 2: Apply the new scale and frame around the midpoint of the
|
|
385
|
-
// visible area.
|
|
386
|
-
scrollTopNext =
|
|
387
|
-
( scrollTopNext + clientHeight / 2 ) * scaleValue +
|
|
388
|
-
frameSizeValue -
|
|
389
|
-
clientHeight / 2;
|
|
390
|
-
|
|
391
|
-
// Step 3: Handle an edge case so that you scroll to the top of the
|
|
392
|
-
// iframe if the top of the iframe content is visible in the container.
|
|
393
|
-
// The same edge case for the bottom is skipped because changing content
|
|
394
|
-
// makes calculating it impossible.
|
|
395
|
-
scrollTopNext = scrollTop <= prevFrameSize ? 0 : scrollTopNext;
|
|
396
|
-
|
|
397
|
-
// This is the scrollTop value if you are scrolled to the bottom of the
|
|
398
|
-
// iframe. We can't just let the browser handle it because we need to
|
|
399
|
-
// animate the scaling.
|
|
400
|
-
const maxScrollTop =
|
|
401
|
-
scrollHeight * ( scaleValue / prevScale ) +
|
|
402
|
-
frameSizeValue * 2 -
|
|
403
|
-
clientHeight;
|
|
404
|
-
|
|
405
|
-
// Step 4: Clamp the scrollTopNext between the minimum and maximum
|
|
406
|
-
// possible scrollTop positions. Round the value to avoid subpixel
|
|
407
|
-
// truncation by the browser which sometimes causes a 1px error.
|
|
408
|
-
scrollTopNext = Math.round(
|
|
409
|
-
Math.min(
|
|
410
|
-
Math.max( 0, scrollTopNext ),
|
|
411
|
-
Math.max( 0, maxScrollTop )
|
|
412
|
-
)
|
|
413
|
-
);
|
|
414
|
-
|
|
415
|
-
iframeDocument.documentElement.style.setProperty(
|
|
416
|
-
'--wp-block-editor-iframe-zoom-out-scroll-top',
|
|
417
|
-
`${ scrollTop }px`
|
|
418
|
-
);
|
|
419
|
-
|
|
420
|
-
iframeDocument.documentElement.style.setProperty(
|
|
421
|
-
'--wp-block-editor-iframe-zoom-out-scroll-top-next',
|
|
422
|
-
`${ scrollTopNext }px`
|
|
423
|
-
);
|
|
424
|
-
|
|
425
|
-
iframeDocument.documentElement.classList.add( 'zoom-out-animation' );
|
|
426
|
-
|
|
427
|
-
function onZoomOutTransitionEnd() {
|
|
428
|
-
// Remove the position fixed for the animation.
|
|
429
|
-
iframeDocument.documentElement.classList.remove(
|
|
430
|
-
'zoom-out-animation'
|
|
431
|
-
);
|
|
432
|
-
|
|
433
|
-
// Update previous values.
|
|
434
|
-
prevClientHeightRef.current = clientHeight;
|
|
435
|
-
prevFrameSizeRef.current = frameSizeValue;
|
|
436
|
-
prevScaleRef.current = scaleValue;
|
|
437
|
-
|
|
438
|
-
// Set the final scroll position that was just animated to.
|
|
439
|
-
// Disable reason: Eslint isn't smart enough to know that this is a
|
|
440
|
-
// DOM element. https://github.com/facebook/react/issues/31483
|
|
441
|
-
// eslint-disable-next-line react-compiler/react-compiler
|
|
442
|
-
iframeDocument.documentElement.scrollTop = scrollTopNext;
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
let raf;
|
|
446
|
-
if ( prefersReducedMotion ) {
|
|
447
|
-
// Hack: Wait for the window values to recalculate.
|
|
448
|
-
raf = iframeDocument.defaultView.requestAnimationFrame(
|
|
449
|
-
onZoomOutTransitionEnd
|
|
450
|
-
);
|
|
451
|
-
} else {
|
|
452
|
-
iframeDocument.documentElement.addEventListener(
|
|
453
|
-
'transitionend',
|
|
454
|
-
onZoomOutTransitionEnd,
|
|
455
|
-
{ once: true }
|
|
456
|
-
);
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
return () => {
|
|
460
|
-
iframeDocument.documentElement.style.removeProperty(
|
|
461
|
-
'--wp-block-editor-iframe-zoom-out-scroll-top'
|
|
462
|
-
);
|
|
463
|
-
iframeDocument.documentElement.style.removeProperty(
|
|
464
|
-
'--wp-block-editor-iframe-zoom-out-scroll-top-next'
|
|
465
|
-
);
|
|
466
|
-
iframeDocument.documentElement.classList.remove(
|
|
467
|
-
'zoom-out-animation'
|
|
468
|
-
);
|
|
469
|
-
if ( prefersReducedMotion ) {
|
|
470
|
-
iframeDocument.defaultView.cancelAnimationFrame( raf );
|
|
471
|
-
} else {
|
|
472
|
-
iframeDocument.documentElement.removeEventListener(
|
|
473
|
-
'transitionend',
|
|
474
|
-
onZoomOutTransitionEnd
|
|
475
|
-
);
|
|
476
|
-
}
|
|
477
|
-
};
|
|
478
|
-
}, [ iframeDocument, scaleValue, frameSizeValue, prefersReducedMotion ] );
|
|
479
|
-
|
|
480
|
-
// Toggle zoom out CSS Classes only when zoom out mode changes. We could add these into the useEffect
|
|
481
|
-
// that controls settings the CSS variables, but then we would need to do more work to ensure we're
|
|
482
|
-
// only toggling these when the zoom out mode changes, as that useEffect is also triggered by a large
|
|
483
|
-
// number of dependencies.
|
|
484
|
-
useEffect( () => {
|
|
485
|
-
if ( ! iframeDocument ) {
|
|
486
|
-
return;
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
if ( isZoomedOut ) {
|
|
490
|
-
iframeDocument.documentElement.classList.add( 'is-zoomed-out' );
|
|
491
|
-
} else {
|
|
492
|
-
// HACK: Since we can't remove this in the cleanup, we need to do it here.
|
|
493
|
-
iframeDocument.documentElement.classList.remove( 'is-zoomed-out' );
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
return () => {
|
|
497
|
-
// HACK: Skipping cleanup because it causes issues with the zoom out
|
|
498
|
-
// animation. More refactoring is needed to fix this properly.
|
|
499
|
-
// iframeDocument.documentElement.classList.remove( 'is-zoomed-out' );
|
|
500
|
-
};
|
|
501
|
-
}, [ iframeDocument, isZoomedOut ] );
|
|
502
|
-
|
|
503
|
-
// Calculate the scaling and CSS variables for the zoom out canvas
|
|
504
|
-
useEffect( () => {
|
|
505
|
-
if ( ! iframeDocument ) {
|
|
506
|
-
return;
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
// Note: When we initialize the zoom out when the canvas is smaller (sidebars open),
|
|
510
|
-
// initialContainerWidthRef will be smaller than the full page, and reflow will happen
|
|
511
|
-
// when the canvas area becomes larger due to sidebars closing. This is a known but
|
|
512
|
-
// minor divergence for now.
|
|
513
|
-
|
|
514
|
-
// This scaling calculation has to happen within the JS because CSS calc() can
|
|
515
|
-
// only divide and multiply by a unitless value. I.e. calc( 100px / 2 ) is valid
|
|
516
|
-
// but calc( 100px / 2px ) is not.
|
|
517
|
-
iframeDocument.documentElement.style.setProperty(
|
|
518
|
-
'--wp-block-editor-iframe-zoom-out-scale',
|
|
519
|
-
scaleValue
|
|
520
|
-
);
|
|
521
|
-
|
|
522
|
-
// frameSize has to be a px value for the scaling and frame size to be computed correctly.
|
|
523
|
-
iframeDocument.documentElement.style.setProperty(
|
|
524
|
-
'--wp-block-editor-iframe-zoom-out-frame-size',
|
|
525
|
-
typeof frameSize === 'number' ? `${ frameSize }px` : frameSize
|
|
526
|
-
);
|
|
527
|
-
iframeDocument.documentElement.style.setProperty(
|
|
528
|
-
'--wp-block-editor-iframe-zoom-out-content-height',
|
|
529
|
-
`${ contentHeight }px`
|
|
530
|
-
);
|
|
531
|
-
iframeDocument.documentElement.style.setProperty(
|
|
532
|
-
'--wp-block-editor-iframe-zoom-out-inner-height',
|
|
533
|
-
`${ iframeWindowInnerHeight }px`
|
|
534
|
-
);
|
|
535
|
-
iframeDocument.documentElement.style.setProperty(
|
|
536
|
-
'--wp-block-editor-iframe-zoom-out-container-width',
|
|
537
|
-
`${ containerWidth }px`
|
|
538
|
-
);
|
|
539
|
-
iframeDocument.documentElement.style.setProperty(
|
|
540
|
-
'--wp-block-editor-iframe-zoom-out-scale-container-width',
|
|
541
|
-
`${ scaleContainerWidth }px`
|
|
542
|
-
);
|
|
543
|
-
|
|
544
|
-
return () => {
|
|
545
|
-
// HACK: Skipping cleanup because it causes issues with the zoom out
|
|
546
|
-
// animation. More refactoring is needed to fix this properly.
|
|
547
|
-
// iframeDocument.documentElement.style.removeProperty(
|
|
548
|
-
// '--wp-block-editor-iframe-zoom-out-scale'
|
|
549
|
-
// );
|
|
550
|
-
// iframeDocument.documentElement.style.removeProperty(
|
|
551
|
-
// '--wp-block-editor-iframe-zoom-out-frame-size'
|
|
552
|
-
// );
|
|
553
|
-
// iframeDocument.documentElement.style.removeProperty(
|
|
554
|
-
// '--wp-block-editor-iframe-zoom-out-content-height'
|
|
555
|
-
// );
|
|
556
|
-
// iframeDocument.documentElement.style.removeProperty(
|
|
557
|
-
// '--wp-block-editor-iframe-zoom-out-inner-height'
|
|
558
|
-
// );
|
|
559
|
-
// iframeDocument.documentElement.style.removeProperty(
|
|
560
|
-
// '--wp-block-editor-iframe-zoom-out-container-width'
|
|
561
|
-
// );
|
|
562
|
-
// iframeDocument.documentElement.style.removeProperty(
|
|
563
|
-
// '--wp-block-editor-iframe-zoom-out-scale-container-width'
|
|
564
|
-
// );
|
|
565
|
-
};
|
|
566
|
-
}, [
|
|
567
|
-
scaleValue,
|
|
568
|
-
frameSize,
|
|
569
|
-
iframeDocument,
|
|
570
|
-
iframeWindowInnerHeight,
|
|
571
|
-
contentHeight,
|
|
572
|
-
containerWidth,
|
|
573
|
-
windowInnerWidth,
|
|
574
|
-
isZoomedOut,
|
|
575
|
-
scaleContainerWidth,
|
|
576
|
-
] );
|
|
577
|
-
|
|
578
278
|
// Make sure to not render the before and after focusable div elements in view
|
|
579
279
|
// mode. They're only needed to capture focus in edit mode.
|
|
580
280
|
const shouldRenderFocusCaptureElements = tabIndex >= 0 && ! isPreviewMode;
|
|
@@ -654,7 +354,7 @@ function Iframe( {
|
|
|
654
354
|
);
|
|
655
355
|
|
|
656
356
|
return (
|
|
657
|
-
<div className="block-editor-iframe__container"
|
|
357
|
+
<div className="block-editor-iframe__container">
|
|
658
358
|
{ containerResizeListener }
|
|
659
359
|
<div
|
|
660
360
|
className={ clsx(
|