@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
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { useEffect, useRef, useCallback } from '@wordpress/element';
|
|
5
|
+
import { useReducedMotion, useResizeObserver } from '@wordpress/compose';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {Object} TransitionState
|
|
9
|
+
* @property {number} scaleValue Scale of the canvas.
|
|
10
|
+
* @property {number} frameSize Size of the frame/offset around the canvas.
|
|
11
|
+
* @property {number} clientHeight ClientHeight of the iframe.
|
|
12
|
+
* @property {number} scrollTop ScrollTop of the iframe.
|
|
13
|
+
* @property {number} scrollHeight ScrollHeight of the iframe.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Calculate the scale of the canvas.
|
|
18
|
+
*
|
|
19
|
+
* @param {Object} options Object of options
|
|
20
|
+
* @param {number} options.frameSize Size of the frame/offset around the canvas
|
|
21
|
+
* @param {number} options.containerWidth Actual width of the canvas container
|
|
22
|
+
* @param {number} options.maxContainerWidth Maximum width of the container to use for the scale calculation. This locks the canvas to a maximum width when zooming out.
|
|
23
|
+
* @param {number} options.scaleContainerWidth Width the of the container wrapping the canvas container
|
|
24
|
+
* @return {number} Scale value between 0 and/or equal to 1
|
|
25
|
+
*/
|
|
26
|
+
function calculateScale( {
|
|
27
|
+
frameSize,
|
|
28
|
+
containerWidth,
|
|
29
|
+
maxContainerWidth,
|
|
30
|
+
scaleContainerWidth,
|
|
31
|
+
} ) {
|
|
32
|
+
return (
|
|
33
|
+
( Math.min( containerWidth, maxContainerWidth ) - frameSize * 2 ) /
|
|
34
|
+
scaleContainerWidth
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Compute the next scrollTop position after scaling the iframe content.
|
|
40
|
+
*
|
|
41
|
+
* @param {TransitionState} transitionFrom Starting point of the transition
|
|
42
|
+
* @param {TransitionState} transitionTo Ending state of the transition
|
|
43
|
+
* @return {number} Next scrollTop position after scaling the iframe content.
|
|
44
|
+
*/
|
|
45
|
+
function computeScrollTopNext( transitionFrom, transitionTo ) {
|
|
46
|
+
const {
|
|
47
|
+
clientHeight: prevClientHeight,
|
|
48
|
+
frameSize: prevFrameSize,
|
|
49
|
+
scaleValue: prevScale,
|
|
50
|
+
scrollTop,
|
|
51
|
+
scrollHeight,
|
|
52
|
+
} = transitionFrom;
|
|
53
|
+
const { clientHeight, frameSize, scaleValue } = transitionTo;
|
|
54
|
+
// Step 0: Start with the current scrollTop.
|
|
55
|
+
let scrollTopNext = scrollTop;
|
|
56
|
+
// Step 1: Undo the effects of the previous scale and frame around the
|
|
57
|
+
// midpoint of the visible area.
|
|
58
|
+
scrollTopNext =
|
|
59
|
+
( scrollTopNext + prevClientHeight / 2 - prevFrameSize ) / prevScale -
|
|
60
|
+
prevClientHeight / 2;
|
|
61
|
+
|
|
62
|
+
// Step 2: Apply the new scale and frame around the midpoint of the
|
|
63
|
+
// visible area.
|
|
64
|
+
scrollTopNext =
|
|
65
|
+
( scrollTopNext + clientHeight / 2 ) * scaleValue +
|
|
66
|
+
frameSize -
|
|
67
|
+
clientHeight / 2;
|
|
68
|
+
|
|
69
|
+
// Step 3: Handle an edge case so that you scroll to the top of the
|
|
70
|
+
// iframe if the top of the iframe content is visible in the container.
|
|
71
|
+
// The same edge case for the bottom is skipped because changing content
|
|
72
|
+
// makes calculating it impossible.
|
|
73
|
+
scrollTopNext = scrollTop <= prevFrameSize ? 0 : scrollTopNext;
|
|
74
|
+
|
|
75
|
+
// This is the scrollTop value if you are scrolled to the bottom of the
|
|
76
|
+
// iframe. We can't just let the browser handle it because we need to
|
|
77
|
+
// animate the scaling.
|
|
78
|
+
const maxScrollTop =
|
|
79
|
+
scrollHeight * ( scaleValue / prevScale ) +
|
|
80
|
+
frameSize * 2 -
|
|
81
|
+
clientHeight;
|
|
82
|
+
|
|
83
|
+
// Step 4: Clamp the scrollTopNext between the minimum and maximum
|
|
84
|
+
// possible scrollTop positions. Round the value to avoid subpixel
|
|
85
|
+
// truncation by the browser which sometimes causes a 1px error.
|
|
86
|
+
return Math.round(
|
|
87
|
+
Math.min( Math.max( 0, scrollTopNext ), Math.max( 0, maxScrollTop ) )
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Generate the keyframes to use for the zoom out animation.
|
|
93
|
+
*
|
|
94
|
+
* @param {TransitionState} transitionFrom Starting transition state.
|
|
95
|
+
* @param {TransitionState} transitionTo Ending transition state.
|
|
96
|
+
* @return {Object[]} An array of keyframes to use for the animation.
|
|
97
|
+
*/
|
|
98
|
+
function getAnimationKeyframes( transitionFrom, transitionTo ) {
|
|
99
|
+
const {
|
|
100
|
+
scaleValue: prevScale,
|
|
101
|
+
frameSize: prevFrameSize,
|
|
102
|
+
scrollTop,
|
|
103
|
+
} = transitionFrom;
|
|
104
|
+
const { scaleValue, frameSize, scrollTop: scrollTopNext } = transitionTo;
|
|
105
|
+
|
|
106
|
+
return [
|
|
107
|
+
{
|
|
108
|
+
translate: `0 0`,
|
|
109
|
+
scale: prevScale,
|
|
110
|
+
paddingTop: `${ prevFrameSize / prevScale }px`,
|
|
111
|
+
paddingBottom: `${ prevFrameSize / prevScale }px`,
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
translate: `0 ${ scrollTop - scrollTopNext }px`,
|
|
115
|
+
scale: scaleValue,
|
|
116
|
+
paddingTop: `${ frameSize / scaleValue }px`,
|
|
117
|
+
paddingBottom: `${ frameSize / scaleValue }px`,
|
|
118
|
+
},
|
|
119
|
+
];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @typedef {Object} ScaleCanvasResult
|
|
124
|
+
* @property {boolean} isZoomedOut A boolean indicating if the canvas is zoomed out.
|
|
125
|
+
* @property {number} scaleContainerWidth The width of the container used to calculate the scale.
|
|
126
|
+
* @property {Object} contentResizeListener A resize observer for the content.
|
|
127
|
+
* @property {Object} containerResizeListener A resize observer for the container.
|
|
128
|
+
*/
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Handles scaling the canvas for the zoom out mode and animating between
|
|
132
|
+
* the states.
|
|
133
|
+
*
|
|
134
|
+
* @param {Object} options Object of options.
|
|
135
|
+
* @param {number} options.frameSize Size of the frame around the content.
|
|
136
|
+
* @param {Document} options.iframeDocument Document of the iframe.
|
|
137
|
+
* @param {number} options.maxContainerWidth Max width of the canvas to use as the starting scale point. Defaults to 750.
|
|
138
|
+
* @param {number|string} options.scale Scale of the canvas. Can be an decimal between 0 and 1, 1, or 'auto-scaled'.
|
|
139
|
+
* @return {ScaleCanvasResult} Properties of the result.
|
|
140
|
+
*/
|
|
141
|
+
export function useScaleCanvas( {
|
|
142
|
+
frameSize,
|
|
143
|
+
iframeDocument,
|
|
144
|
+
maxContainerWidth = 750,
|
|
145
|
+
scale,
|
|
146
|
+
} ) {
|
|
147
|
+
const [ contentResizeListener, { height: contentHeight } ] =
|
|
148
|
+
useResizeObserver();
|
|
149
|
+
const [ containerResizeListener, { width: containerWidth } ] =
|
|
150
|
+
useResizeObserver();
|
|
151
|
+
|
|
152
|
+
const initialContainerWidthRef = useRef( 0 );
|
|
153
|
+
const isZoomedOut = scale !== 1;
|
|
154
|
+
const prefersReducedMotion = useReducedMotion();
|
|
155
|
+
const isAutoScaled = scale === 'auto-scaled';
|
|
156
|
+
// Track if the animation should start when the useEffect runs.
|
|
157
|
+
const startAnimationRef = useRef( false );
|
|
158
|
+
// Track the animation so we know if we have an animation running,
|
|
159
|
+
// and can cancel it, reverse it, call a finish event, etc.
|
|
160
|
+
const animationRef = useRef( null );
|
|
161
|
+
|
|
162
|
+
useEffect( () => {
|
|
163
|
+
if ( ! isZoomedOut ) {
|
|
164
|
+
initialContainerWidthRef.current = containerWidth;
|
|
165
|
+
}
|
|
166
|
+
}, [ containerWidth, isZoomedOut ] );
|
|
167
|
+
|
|
168
|
+
const scaleContainerWidth = Math.max(
|
|
169
|
+
initialContainerWidthRef.current,
|
|
170
|
+
containerWidth
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
const scaleValue = isAutoScaled
|
|
174
|
+
? calculateScale( {
|
|
175
|
+
frameSize,
|
|
176
|
+
containerWidth,
|
|
177
|
+
maxContainerWidth,
|
|
178
|
+
scaleContainerWidth,
|
|
179
|
+
} )
|
|
180
|
+
: scale;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* The starting transition state for the zoom out animation.
|
|
184
|
+
* @type {import('react').RefObject<TransitionState>}
|
|
185
|
+
*/
|
|
186
|
+
const transitionFromRef = useRef( {
|
|
187
|
+
scaleValue,
|
|
188
|
+
frameSize,
|
|
189
|
+
clientHeight: 0,
|
|
190
|
+
scrollTop: 0,
|
|
191
|
+
scrollHeight: 0,
|
|
192
|
+
} );
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* The ending transition state for the zoom out animation.
|
|
196
|
+
* @type {import('react').RefObject<TransitionState>}
|
|
197
|
+
*/
|
|
198
|
+
const transitionToRef = useRef( {
|
|
199
|
+
scaleValue,
|
|
200
|
+
frameSize,
|
|
201
|
+
clientHeight: 0,
|
|
202
|
+
scrollTop: 0,
|
|
203
|
+
scrollHeight: 0,
|
|
204
|
+
} );
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Start the zoom out animation. This sets the necessary CSS variables
|
|
208
|
+
* for animating the canvas and returns the Animation object.
|
|
209
|
+
*
|
|
210
|
+
* @return {Animation} The animation object for the zoom out animation.
|
|
211
|
+
*/
|
|
212
|
+
const startZoomOutAnimation = useCallback( () => {
|
|
213
|
+
const { scrollTop } = transitionFromRef.current;
|
|
214
|
+
const { scrollTop: scrollTopNext } = transitionToRef.current;
|
|
215
|
+
|
|
216
|
+
iframeDocument.documentElement.style.setProperty(
|
|
217
|
+
'--wp-block-editor-iframe-zoom-out-scroll-top',
|
|
218
|
+
`${ scrollTop }px`
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
iframeDocument.documentElement.style.setProperty(
|
|
222
|
+
'--wp-block-editor-iframe-zoom-out-scroll-top-next',
|
|
223
|
+
`${ scrollTopNext }px`
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
iframeDocument.documentElement.classList.add( 'zoom-out-animation' );
|
|
227
|
+
|
|
228
|
+
return iframeDocument.documentElement.animate(
|
|
229
|
+
getAnimationKeyframes(
|
|
230
|
+
transitionFromRef.current,
|
|
231
|
+
transitionToRef.current
|
|
232
|
+
),
|
|
233
|
+
{
|
|
234
|
+
easing: 'cubic-bezier(0.46, 0.03, 0.52, 0.96)',
|
|
235
|
+
duration: 400,
|
|
236
|
+
}
|
|
237
|
+
);
|
|
238
|
+
}, [ iframeDocument ] );
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Callback when the zoom out animation is finished.
|
|
242
|
+
* - Cleans up animations refs.
|
|
243
|
+
* - Adds final CSS vars for scale and frame size to preserve the state.
|
|
244
|
+
* - Removes the 'zoom-out-animation' class (which has the fixed positioning).
|
|
245
|
+
* - Sets the final scroll position after the canvas is no longer in fixed position.
|
|
246
|
+
* - Removes CSS vars related to the animation.
|
|
247
|
+
* - Sets the transitionFrom to the transitionTo state to be ready for the next animation.
|
|
248
|
+
*/
|
|
249
|
+
const finishZoomOutAnimation = useCallback( () => {
|
|
250
|
+
startAnimationRef.current = false;
|
|
251
|
+
animationRef.current = null;
|
|
252
|
+
|
|
253
|
+
// Add our final scale and frame size now that the animation is done.
|
|
254
|
+
iframeDocument.documentElement.style.setProperty(
|
|
255
|
+
'--wp-block-editor-iframe-zoom-out-scale',
|
|
256
|
+
transitionToRef.current.scaleValue
|
|
257
|
+
);
|
|
258
|
+
iframeDocument.documentElement.style.setProperty(
|
|
259
|
+
'--wp-block-editor-iframe-zoom-out-frame-size',
|
|
260
|
+
`${ transitionToRef.current.frameSize }px`
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
iframeDocument.documentElement.classList.remove( 'zoom-out-animation' );
|
|
264
|
+
|
|
265
|
+
// Set the final scroll position that was just animated to.
|
|
266
|
+
// Disable reason: Eslint isn't smart enough to know that this is a
|
|
267
|
+
// DOM element. https://github.com/facebook/react/issues/31483
|
|
268
|
+
// eslint-disable-next-line react-compiler/react-compiler
|
|
269
|
+
iframeDocument.documentElement.scrollTop =
|
|
270
|
+
transitionToRef.current.scrollTop;
|
|
271
|
+
|
|
272
|
+
iframeDocument.documentElement.style.removeProperty(
|
|
273
|
+
'--wp-block-editor-iframe-zoom-out-scroll-top'
|
|
274
|
+
);
|
|
275
|
+
iframeDocument.documentElement.style.removeProperty(
|
|
276
|
+
'--wp-block-editor-iframe-zoom-out-scroll-top-next'
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
// Update previous values.
|
|
280
|
+
transitionFromRef.current = transitionToRef.current;
|
|
281
|
+
}, [ iframeDocument ] );
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Runs when zoom out mode is toggled, and sets the startAnimation flag
|
|
285
|
+
* so the animation will start when the next useEffect runs. We _only_
|
|
286
|
+
* want to animate when the zoom out mode is toggled, not when the scale
|
|
287
|
+
* changes due to the container resizing.
|
|
288
|
+
*/
|
|
289
|
+
useEffect( () => {
|
|
290
|
+
if ( ! iframeDocument ) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if ( isZoomedOut ) {
|
|
295
|
+
iframeDocument.documentElement.classList.add( 'is-zoomed-out' );
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
startAnimationRef.current = true;
|
|
299
|
+
|
|
300
|
+
return () => {
|
|
301
|
+
iframeDocument.documentElement.classList.remove( 'is-zoomed-out' );
|
|
302
|
+
};
|
|
303
|
+
}, [ iframeDocument, isZoomedOut ] );
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* This handles:
|
|
307
|
+
* 1. Setting the correct scale and vars of the canvas when zoomed out
|
|
308
|
+
* 2. If zoom out mode has been toggled, runs the animation of zooming in/out
|
|
309
|
+
*/
|
|
310
|
+
useEffect( () => {
|
|
311
|
+
if ( ! iframeDocument ) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// We need to update the appropriate scale to exit from. If sidebars have been opened since setting the
|
|
316
|
+
// original scale, we will snap to a much smaller scale due to the scale container immediately changing sizes when exiting.
|
|
317
|
+
if ( isAutoScaled && transitionFromRef.current.scaleValue !== 1 ) {
|
|
318
|
+
// We use containerWidth as the divisor, as scaleContainerWidth will always match the containerWidth when
|
|
319
|
+
// exiting.
|
|
320
|
+
transitionFromRef.current.scaleValue = calculateScale( {
|
|
321
|
+
frameSize: transitionFromRef.current.frameSize,
|
|
322
|
+
containerWidth,
|
|
323
|
+
maxContainerWidth,
|
|
324
|
+
scaleContainerWidth: containerWidth,
|
|
325
|
+
} );
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// If we are not going to animate the transition, set the scale and frame size directly.
|
|
329
|
+
// If we are animating, these values will be set when the animation is finished.
|
|
330
|
+
// Example: Opening sidebars that reduce the scale of the canvas, but we don't want to
|
|
331
|
+
// animate the transition.
|
|
332
|
+
if ( ! startAnimationRef.current ) {
|
|
333
|
+
iframeDocument.documentElement.style.setProperty(
|
|
334
|
+
'--wp-block-editor-iframe-zoom-out-scale',
|
|
335
|
+
scaleValue
|
|
336
|
+
);
|
|
337
|
+
iframeDocument.documentElement.style.setProperty(
|
|
338
|
+
'--wp-block-editor-iframe-zoom-out-frame-size',
|
|
339
|
+
`${ frameSize }px`
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
iframeDocument.documentElement.style.setProperty(
|
|
344
|
+
'--wp-block-editor-iframe-zoom-out-content-height',
|
|
345
|
+
`${ contentHeight }px`
|
|
346
|
+
);
|
|
347
|
+
|
|
348
|
+
const clientHeight = iframeDocument.documentElement.clientHeight;
|
|
349
|
+
iframeDocument.documentElement.style.setProperty(
|
|
350
|
+
'--wp-block-editor-iframe-zoom-out-inner-height',
|
|
351
|
+
`${ clientHeight }px`
|
|
352
|
+
);
|
|
353
|
+
|
|
354
|
+
iframeDocument.documentElement.style.setProperty(
|
|
355
|
+
'--wp-block-editor-iframe-zoom-out-container-width',
|
|
356
|
+
`${ containerWidth }px`
|
|
357
|
+
);
|
|
358
|
+
iframeDocument.documentElement.style.setProperty(
|
|
359
|
+
'--wp-block-editor-iframe-zoom-out-scale-container-width',
|
|
360
|
+
`${ scaleContainerWidth }px`
|
|
361
|
+
);
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Handle the zoom out animation:
|
|
365
|
+
*
|
|
366
|
+
* - Get the current scrollTop position.
|
|
367
|
+
* - Calculate where the same scroll position is after scaling.
|
|
368
|
+
* - Apply fixed positioning to the canvas with a transform offset
|
|
369
|
+
* to keep the canvas centered.
|
|
370
|
+
* - Animate the scale and padding to the new scale and frame size.
|
|
371
|
+
* - After the animation is complete, remove the fixed positioning
|
|
372
|
+
* and set the scroll position that keeps everything centered.
|
|
373
|
+
*/
|
|
374
|
+
if ( startAnimationRef.current ) {
|
|
375
|
+
// Don't allow a new transition to start again unless it was started by the zoom out mode changing.
|
|
376
|
+
startAnimationRef.current = false;
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* If we already have an animation running, reverse it.
|
|
380
|
+
*/
|
|
381
|
+
if ( animationRef.current ) {
|
|
382
|
+
animationRef.current.reverse();
|
|
383
|
+
// Swap the transition to/from refs so that we set the correct values when
|
|
384
|
+
// finishZoomOutAnimation runs.
|
|
385
|
+
const tempTransitionFrom = transitionFromRef.current;
|
|
386
|
+
const tempTransitionTo = transitionToRef.current;
|
|
387
|
+
transitionFromRef.current = tempTransitionTo;
|
|
388
|
+
transitionToRef.current = tempTransitionFrom;
|
|
389
|
+
} else {
|
|
390
|
+
/**
|
|
391
|
+
* Start a new zoom animation.
|
|
392
|
+
*/
|
|
393
|
+
|
|
394
|
+
// We can't trust the set value from contentHeight, as it was measured
|
|
395
|
+
// before the zoom out mode was changed. After zoom out mode is changed,
|
|
396
|
+
// appenders may appear or disappear, so we need to get the height from
|
|
397
|
+
// the iframe at this point when we're about to animate the zoom out.
|
|
398
|
+
// The iframe scrollTop, scrollHeight, and clientHeight will all be
|
|
399
|
+
// the most accurate.
|
|
400
|
+
transitionFromRef.current.clientHeight =
|
|
401
|
+
transitionFromRef.current.clientHeight ?? clientHeight;
|
|
402
|
+
transitionFromRef.current.scrollTop =
|
|
403
|
+
iframeDocument.documentElement.scrollTop;
|
|
404
|
+
transitionFromRef.current.scrollHeight =
|
|
405
|
+
iframeDocument.documentElement.scrollHeight;
|
|
406
|
+
|
|
407
|
+
transitionToRef.current = {
|
|
408
|
+
scaleValue,
|
|
409
|
+
frameSize,
|
|
410
|
+
clientHeight,
|
|
411
|
+
};
|
|
412
|
+
transitionToRef.current.scrollTop = computeScrollTopNext(
|
|
413
|
+
transitionFromRef.current,
|
|
414
|
+
transitionToRef.current
|
|
415
|
+
);
|
|
416
|
+
|
|
417
|
+
animationRef.current = startZoomOutAnimation();
|
|
418
|
+
|
|
419
|
+
// If the user prefers reduced motion, finish the animation immediately and set the final state.
|
|
420
|
+
if ( prefersReducedMotion ) {
|
|
421
|
+
finishZoomOutAnimation();
|
|
422
|
+
} else {
|
|
423
|
+
animationRef.current.onfinish = finishZoomOutAnimation;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
return () => {
|
|
429
|
+
iframeDocument.documentElement.style.removeProperty(
|
|
430
|
+
'--wp-block-editor-iframe-zoom-out-scale'
|
|
431
|
+
);
|
|
432
|
+
iframeDocument.documentElement.style.removeProperty(
|
|
433
|
+
'--wp-block-editor-iframe-zoom-out-frame-size'
|
|
434
|
+
);
|
|
435
|
+
iframeDocument.documentElement.style.removeProperty(
|
|
436
|
+
'--wp-block-editor-iframe-zoom-out-content-height'
|
|
437
|
+
);
|
|
438
|
+
iframeDocument.documentElement.style.removeProperty(
|
|
439
|
+
'--wp-block-editor-iframe-zoom-out-inner-height'
|
|
440
|
+
);
|
|
441
|
+
iframeDocument.documentElement.style.removeProperty(
|
|
442
|
+
'--wp-block-editor-iframe-zoom-out-container-width'
|
|
443
|
+
);
|
|
444
|
+
iframeDocument.documentElement.style.removeProperty(
|
|
445
|
+
'--wp-block-editor-iframe-zoom-out-scale-container-width'
|
|
446
|
+
);
|
|
447
|
+
};
|
|
448
|
+
}, [
|
|
449
|
+
startZoomOutAnimation,
|
|
450
|
+
finishZoomOutAnimation,
|
|
451
|
+
prefersReducedMotion,
|
|
452
|
+
isAutoScaled,
|
|
453
|
+
scaleValue,
|
|
454
|
+
frameSize,
|
|
455
|
+
iframeDocument,
|
|
456
|
+
contentHeight,
|
|
457
|
+
containerWidth,
|
|
458
|
+
maxContainerWidth,
|
|
459
|
+
scaleContainerWidth,
|
|
460
|
+
] );
|
|
461
|
+
|
|
462
|
+
return {
|
|
463
|
+
isZoomedOut,
|
|
464
|
+
scaleContainerWidth,
|
|
465
|
+
contentResizeListener,
|
|
466
|
+
containerResizeListener,
|
|
467
|
+
};
|
|
468
|
+
}
|
|
@@ -10,6 +10,12 @@ import { __, sprintf } from '@wordpress/i18n';
|
|
|
10
10
|
import { store as noticesStore } from '@wordpress/notices';
|
|
11
11
|
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
|
|
12
12
|
|
|
13
|
+
const messages = {
|
|
14
|
+
crop: __( 'Image cropped.' ),
|
|
15
|
+
rotate: __( 'Image rotated.' ),
|
|
16
|
+
cropAndRotate: __( 'Image cropped and rotated.' ),
|
|
17
|
+
};
|
|
18
|
+
|
|
13
19
|
export default function useSaveImage( {
|
|
14
20
|
crop,
|
|
15
21
|
rotation,
|
|
@@ -18,7 +24,8 @@ export default function useSaveImage( {
|
|
|
18
24
|
onSaveImage,
|
|
19
25
|
onFinishEditing,
|
|
20
26
|
} ) {
|
|
21
|
-
const { createErrorNotice } =
|
|
27
|
+
const { createErrorNotice, createSuccessNotice } =
|
|
28
|
+
useDispatch( noticesStore );
|
|
22
29
|
const [ isInProgress, setIsInProgress ] = useState( false );
|
|
23
30
|
|
|
24
31
|
const cancel = useCallback( () => {
|
|
@@ -61,6 +68,9 @@ export default function useSaveImage( {
|
|
|
61
68
|
return;
|
|
62
69
|
}
|
|
63
70
|
|
|
71
|
+
const modifierType =
|
|
72
|
+
modifiers.length === 1 ? modifiers[ 0 ].type : 'cropAndRotate';
|
|
73
|
+
|
|
64
74
|
apiFetch( {
|
|
65
75
|
path: `/wp/v2/media/${ id }/edit`,
|
|
66
76
|
method: 'POST',
|
|
@@ -71,11 +81,25 @@ export default function useSaveImage( {
|
|
|
71
81
|
id: response.id,
|
|
72
82
|
url: response.source_url,
|
|
73
83
|
} );
|
|
84
|
+
createSuccessNotice( messages[ modifierType ], {
|
|
85
|
+
type: 'snackbar',
|
|
86
|
+
actions: [
|
|
87
|
+
{
|
|
88
|
+
label: __( 'Undo' ),
|
|
89
|
+
onClick: () => {
|
|
90
|
+
onSaveImage( {
|
|
91
|
+
id,
|
|
92
|
+
url,
|
|
93
|
+
} );
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
} );
|
|
74
98
|
} )
|
|
75
99
|
.catch( ( error ) => {
|
|
76
100
|
createErrorNotice(
|
|
77
101
|
sprintf(
|
|
78
|
-
/* translators:
|
|
102
|
+
/* translators: %s: Error message. */
|
|
79
103
|
__( 'Could not edit image. %s' ),
|
|
80
104
|
stripHTML( error.message )
|
|
81
105
|
),
|
|
@@ -96,6 +120,7 @@ export default function useSaveImage( {
|
|
|
96
120
|
url,
|
|
97
121
|
onSaveImage,
|
|
98
122
|
createErrorNotice,
|
|
123
|
+
createSuccessNotice,
|
|
99
124
|
onFinishEditing,
|
|
100
125
|
] );
|
|
101
126
|
|