@wordpress/block-editor 9.0.0 → 9.1.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/README.md +1 -0
- package/build/components/block-alignment-matrix-control/index.js +1 -6
- package/build/components/block-alignment-matrix-control/index.js.map +1 -1
- package/build/components/block-content-overlay/index.js +4 -82
- package/build/components/block-content-overlay/index.js.map +1 -1
- package/build/components/block-draggable/draggable-chip.native.js +64 -0
- package/build/components/block-draggable/draggable-chip.native.js.map +1 -0
- package/build/components/block-draggable/dropping-insertion-point.native.js +157 -0
- package/build/components/block-draggable/dropping-insertion-point.native.js.map +1 -0
- package/build/components/block-draggable/index.native.js +484 -0
- package/build/components/block-draggable/index.native.js.map +1 -0
- package/build/components/block-draggable/use-scroll-when-dragging.native.js +130 -0
- package/build/components/block-draggable/use-scroll-when-dragging.native.js.map +1 -0
- package/build/components/block-list/block-list-context.native.js +195 -0
- package/build/components/block-list/block-list-context.native.js.map +1 -0
- package/build/components/block-list/block-list-item-cell.native.js +67 -0
- package/build/components/block-list/block-list-item-cell.native.js.map +1 -0
- package/build/components/block-list/block-list-item.native.js +12 -9
- package/build/components/block-list/block-list-item.native.js.map +1 -1
- package/build/components/block-list/block.native.js +26 -5
- package/build/components/block-list/block.native.js.map +1 -1
- package/build/components/block-list/index.native.js +75 -23
- package/build/components/block-list/index.native.js.map +1 -1
- package/build/components/block-list/use-block-props/index.js +8 -4
- package/build/components/block-list/use-block-props/index.js.map +1 -1
- package/build/components/block-list/use-in-between-inserter.js +1 -1
- package/build/components/block-list/use-in-between-inserter.js.map +1 -1
- package/build/components/block-mobile-toolbar/index.native.js +9 -3
- package/build/components/block-mobile-toolbar/index.native.js.map +1 -1
- package/build/components/block-mover/index.native.js +17 -4
- package/build/components/block-mover/index.native.js.map +1 -1
- package/build/components/block-popover/inbetween.js +10 -2
- package/build/components/block-popover/inbetween.js.map +1 -1
- package/build/components/block-popover/index.js +4 -16
- package/build/components/block-popover/index.js.map +1 -1
- package/build/components/block-preview/index.js +1 -1
- package/build/components/block-preview/index.js.map +1 -1
- package/build/components/block-tools/selected-block-popover.js +1 -29
- package/build/components/block-tools/selected-block-popover.js.map +1 -1
- package/build/components/border-radius-control/input-controls.js +10 -3
- package/build/components/border-radius-control/input-controls.js.map +1 -1
- package/build/components/colors-gradients/dropdown.js +149 -44
- package/build/components/colors-gradients/dropdown.js.map +1 -1
- package/build/components/iframe/index.js +51 -50
- package/build/components/iframe/index.js.map +1 -1
- package/build/components/image-editor/use-save-image.js +3 -1
- package/build/components/image-editor/use-save-image.js.map +1 -1
- package/build/components/index.js +5 -14
- package/build/components/index.js.map +1 -1
- package/build/components/inserter/index.native.js +1 -1
- package/build/components/inserter/index.native.js.map +1 -1
- package/build/components/link-control/constants.js +11 -1
- package/build/components/link-control/constants.js.map +1 -1
- package/build/components/link-control/search-results.js +4 -3
- package/build/components/link-control/search-results.js.map +1 -1
- package/build/components/link-control/use-search-handler.js +4 -4
- package/build/components/link-control/use-search-handler.js.map +1 -1
- package/build/components/list-view/drop-indicator.js +0 -1
- package/build/components/list-view/drop-indicator.js.map +1 -1
- package/build/components/navigable-toolbar/index.js +12 -2
- package/build/components/navigable-toolbar/index.js.map +1 -1
- package/build/components/rich-text/format-toolbar-container.js +0 -1
- package/build/components/rich-text/format-toolbar-container.js.map +1 -1
- package/build/components/rich-text/index.js +1 -1
- package/build/components/rich-text/index.js.map +1 -1
- package/build/components/url-input/index.js +4 -1
- package/build/components/url-input/index.js.map +1 -1
- package/build/components/use-block-drop-zone/index.native.js +167 -0
- package/build/components/use-block-drop-zone/index.native.js.map +1 -0
- package/build/components/use-on-block-drop/index.native.js +95 -0
- package/build/components/use-on-block-drop/index.native.js.map +1 -0
- package/build/components/warning/index.js +6 -1
- package/build/components/warning/index.js.map +1 -1
- package/build/hooks/anchor.js.map +1 -1
- package/build/hooks/border.js +2 -7
- package/build/hooks/border.js.map +1 -1
- package/build/hooks/color-panel.js +14 -7
- package/build/hooks/color-panel.js.map +1 -1
- package/build/hooks/style.js +14 -13
- package/build/hooks/style.js.map +1 -1
- package/build/hooks/typography.js +6 -2
- package/build/hooks/typography.js.map +1 -1
- package/build-module/components/block-alignment-matrix-control/index.js +1 -6
- package/build-module/components/block-alignment-matrix-control/index.js.map +1 -1
- package/build-module/components/block-content-overlay/index.js +3 -78
- package/build-module/components/block-content-overlay/index.js.map +1 -1
- package/build-module/components/block-draggable/draggable-chip.native.js +50 -0
- package/build-module/components/block-draggable/draggable-chip.native.js.map +1 -0
- package/build-module/components/block-draggable/dropping-insertion-point.native.js +137 -0
- package/build-module/components/block-draggable/dropping-insertion-point.native.js.map +1 -0
- package/build-module/components/block-draggable/index.native.js +449 -0
- package/build-module/components/block-draggable/index.native.js.map +1 -0
- package/build-module/components/block-draggable/use-scroll-when-dragging.native.js +120 -0
- package/build-module/components/block-draggable/use-scroll-when-dragging.native.js.map +1 -0
- package/build-module/components/block-list/block-list-context.native.js +179 -0
- package/build-module/components/block-list/block-list-context.native.js.map +1 -0
- package/build-module/components/block-list/block-list-item-cell.native.js +59 -0
- package/build-module/components/block-list/block-list-item-cell.native.js.map +1 -0
- package/build-module/components/block-list/block-list-item.native.js +12 -9
- package/build-module/components/block-list/block-list-item.native.js.map +1 -1
- package/build-module/components/block-list/block.native.js +25 -5
- package/build-module/components/block-list/block.native.js.map +1 -1
- package/build-module/components/block-list/index.native.js +72 -23
- package/build-module/components/block-list/index.native.js.map +1 -1
- package/build-module/components/block-list/use-block-props/index.js +9 -5
- package/build-module/components/block-list/use-block-props/index.js.map +1 -1
- package/build-module/components/block-list/use-in-between-inserter.js +1 -1
- package/build-module/components/block-list/use-in-between-inserter.js.map +1 -1
- package/build-module/components/block-mobile-toolbar/index.native.js +8 -3
- package/build-module/components/block-mobile-toolbar/index.native.js.map +1 -1
- package/build-module/components/block-mover/index.native.js +18 -5
- package/build-module/components/block-mover/index.native.js.map +1 -1
- package/build-module/components/block-popover/inbetween.js +10 -2
- package/build-module/components/block-popover/inbetween.js.map +1 -1
- package/build-module/components/block-popover/index.js +4 -15
- package/build-module/components/block-popover/index.js.map +1 -1
- package/build-module/components/block-preview/index.js +1 -1
- package/build-module/components/block-preview/index.js.map +1 -1
- package/build-module/components/block-tools/selected-block-popover.js +2 -29
- package/build-module/components/block-tools/selected-block-popover.js.map +1 -1
- package/build-module/components/border-radius-control/input-controls.js +11 -4
- package/build-module/components/border-radius-control/input-controls.js.map +1 -1
- package/build-module/components/colors-gradients/dropdown.js +151 -46
- package/build-module/components/colors-gradients/dropdown.js.map +1 -1
- package/build-module/components/iframe/index.js +52 -51
- package/build-module/components/iframe/index.js.map +1 -1
- package/build-module/components/image-editor/use-save-image.js +2 -1
- package/build-module/components/image-editor/use-save-image.js.map +1 -1
- package/build-module/components/index.js +1 -2
- package/build-module/components/index.js.map +1 -1
- package/build-module/components/inserter/index.native.js +1 -1
- package/build-module/components/inserter/index.native.js.map +1 -1
- package/build-module/components/link-control/constants.js +5 -0
- package/build-module/components/link-control/constants.js.map +1 -1
- package/build-module/components/link-control/search-results.js +3 -4
- package/build-module/components/link-control/search-results.js.map +1 -1
- package/build-module/components/link-control/use-search-handler.js +5 -5
- package/build-module/components/link-control/use-search-handler.js.map +1 -1
- package/build-module/components/list-view/drop-indicator.js +0 -1
- package/build-module/components/list-view/drop-indicator.js.map +1 -1
- package/build-module/components/navigable-toolbar/index.js +12 -2
- package/build-module/components/navigable-toolbar/index.js.map +1 -1
- package/build-module/components/rich-text/format-toolbar-container.js +0 -1
- package/build-module/components/rich-text/format-toolbar-container.js.map +1 -1
- package/build-module/components/rich-text/index.js +1 -1
- package/build-module/components/rich-text/index.js.map +1 -1
- package/build-module/components/url-input/index.js +4 -1
- package/build-module/components/url-input/index.js.map +1 -1
- package/build-module/components/use-block-drop-zone/index.native.js +148 -0
- package/build-module/components/use-block-drop-zone/index.native.js.map +1 -0
- package/build-module/components/use-on-block-drop/index.native.js +83 -0
- package/build-module/components/use-on-block-drop/index.native.js.map +1 -0
- package/build-module/components/warning/index.js +6 -1
- package/build-module/components/warning/index.js.map +1 -1
- package/build-module/hooks/anchor.js.map +1 -1
- package/build-module/hooks/border.js +2 -7
- package/build-module/hooks/border.js.map +1 -1
- package/build-module/hooks/color-panel.js +11 -6
- package/build-module/hooks/color-panel.js.map +1 -1
- package/build-module/hooks/style.js +15 -14
- package/build-module/hooks/style.js.map +1 -1
- package/build-module/hooks/typography.js +6 -2
- package/build-module/hooks/typography.js.map +1 -1
- package/build-style/style-rtl.css +60 -174
- package/build-style/style.css +60 -174
- package/package.json +28 -28
- package/src/components/block-alignment-matrix-control/index.js +1 -5
- package/src/components/block-content-overlay/index.js +8 -95
- package/src/components/block-content-overlay/style.scss +2 -11
- package/src/components/block-draggable/draggable-chip.native.js +49 -0
- package/src/components/block-draggable/dropping-insertion-point.native.js +181 -0
- package/src/components/block-draggable/dropping-insertion-point.native.scss +8 -0
- package/src/components/block-draggable/index.native.js +458 -0
- package/src/components/block-draggable/style.native.scss +19 -0
- package/src/components/block-draggable/use-scroll-when-dragging.native.js +135 -0
- package/src/components/block-list/block-list-context.native.js +175 -0
- package/src/components/block-list/block-list-item-cell.native.js +49 -0
- package/src/components/block-list/block-list-item.native.js +7 -11
- package/src/components/block-list/block.native.js +36 -8
- package/src/components/block-list/index.native.js +54 -13
- package/src/components/block-list/test/block-list-context.native.js +253 -0
- package/src/components/block-list/test/fixtures/block-list-context.native.js +79 -0
- package/src/components/block-list/use-block-props/index.js +10 -5
- package/src/components/block-list/use-in-between-inserter.js +1 -1
- package/src/components/block-mobile-toolbar/index.native.js +8 -1
- package/src/components/block-mover/index.native.js +22 -6
- package/src/components/block-mover/test/__snapshots__/index.native.js.snap +6 -0
- package/src/components/block-popover/inbetween.js +9 -1
- package/src/components/block-popover/index.js +1 -16
- package/src/components/block-popover/style.scss +1 -0
- package/src/components/block-preview/index.js +1 -4
- package/src/components/block-switcher/style.scss +2 -39
- package/src/components/block-tools/selected-block-popover.js +1 -36
- package/src/components/block-tools/style.scss +1 -12
- package/src/components/border-radius-control/input-controls.js +16 -8
- package/src/components/border-radius-control/style.scss +3 -2
- package/src/components/colors-gradients/dropdown.js +156 -62
- package/src/components/colors-gradients/style.scss +51 -23
- package/src/components/duotone-control/style.scss +1 -7
- package/src/components/iframe/index.js +62 -54
- package/src/components/image-editor/use-save-image.js +2 -1
- package/src/components/index.js +1 -2
- package/src/components/inserter/index.native.js +1 -1
- package/src/components/inserter/style.scss +2 -1
- package/src/components/link-control/constants.js +11 -0
- package/src/components/link-control/search-results.js +4 -5
- package/src/components/link-control/use-search-handler.js +11 -5
- package/src/components/list-view/drop-indicator.js +0 -1
- package/src/components/list-view/style.scss +2 -1
- package/src/components/navigable-toolbar/index.js +12 -2
- package/src/components/preview-options/style.scss +0 -4
- package/src/components/rich-text/format-toolbar-container.js +0 -1
- package/src/components/rich-text/index.js +1 -1
- package/src/components/rich-text/style.scss +2 -8
- package/src/components/url-input/index.js +3 -1
- package/src/components/use-block-drop-zone/index.native.js +173 -0
- package/src/components/use-on-block-drop/index.native.js +119 -0
- package/src/components/warning/index.js +47 -42
- package/src/components/warning/test/__snapshots__/index.js.snap +15 -6
- package/src/components/warning/test/index.js +1 -1
- package/src/hooks/anchor.js +1 -1
- package/src/hooks/border.js +2 -11
- package/src/hooks/border.scss +0 -48
- package/src/hooks/color-panel.js +13 -9
- package/src/hooks/color.scss +0 -62
- package/src/hooks/style.js +25 -39
- package/src/hooks/typography.js +2 -0
- package/src/style.scss +0 -1
- package/build/components/colors-gradients/tools-panel-color-dropdown.js +0 -89
- package/build/components/colors-gradients/tools-panel-color-dropdown.js.map +0 -1
- package/build-module/components/colors-gradients/tools-panel-color-dropdown.js +0 -75
- package/build-module/components/colors-gradients/tools-panel-color-dropdown.js.map +0 -1
- package/src/components/block-alignment-matrix-control/style.scss +0 -10
- package/src/components/colors-gradients/tools-panel-color-dropdown.js +0 -85
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { useWindowDimensions } from 'react-native';
|
|
5
|
+
import {
|
|
6
|
+
useSharedValue,
|
|
7
|
+
useAnimatedRef,
|
|
8
|
+
scrollTo,
|
|
9
|
+
useAnimatedReaction,
|
|
10
|
+
withTiming,
|
|
11
|
+
withRepeat,
|
|
12
|
+
cancelAnimation,
|
|
13
|
+
Easing,
|
|
14
|
+
} from 'react-native-reanimated';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Internal dependencies
|
|
18
|
+
*/
|
|
19
|
+
import { useBlockListContext } from '../block-list/block-list-context';
|
|
20
|
+
|
|
21
|
+
const SCROLL_INACTIVE_DISTANCE_PX = 50;
|
|
22
|
+
const SCROLL_INTERVAL_MS = 1000;
|
|
23
|
+
const VELOCITY_MULTIPLIER = 5000;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* React hook that scrolls the scroll container when a block is being dragged.
|
|
27
|
+
*
|
|
28
|
+
* @return {Function[]} `startScrolling`, `scrollOnDragOver`, `stopScrolling`
|
|
29
|
+
* functions to be called in `onDragStart`, `onDragOver`
|
|
30
|
+
* and `onDragEnd` events respectively. Additionally,
|
|
31
|
+
* `scrollHandler` function is returned which should be
|
|
32
|
+
* called in the `onScroll` event of the block list.
|
|
33
|
+
*/
|
|
34
|
+
export default function useScrollWhenDragging() {
|
|
35
|
+
const { scrollRef } = useBlockListContext();
|
|
36
|
+
const animatedScrollRef = useAnimatedRef();
|
|
37
|
+
animatedScrollRef( scrollRef );
|
|
38
|
+
|
|
39
|
+
const { height: windowHeight } = useWindowDimensions();
|
|
40
|
+
|
|
41
|
+
const velocityY = useSharedValue( 0 );
|
|
42
|
+
const offsetY = useSharedValue( 0 );
|
|
43
|
+
const dragStartY = useSharedValue( 0 );
|
|
44
|
+
const animationTimer = useSharedValue( 0 );
|
|
45
|
+
const isAnimationTimerActive = useSharedValue( false );
|
|
46
|
+
const isScrollActive = useSharedValue( false );
|
|
47
|
+
|
|
48
|
+
const scroll = {
|
|
49
|
+
offsetY: useSharedValue( 0 ),
|
|
50
|
+
maxOffsetY: useSharedValue( 0 ),
|
|
51
|
+
};
|
|
52
|
+
const scrollHandler = ( event ) => {
|
|
53
|
+
'worklet';
|
|
54
|
+
const { contentSize, contentOffset, layoutMeasurement } = event;
|
|
55
|
+
scroll.offsetY.value = contentOffset.y;
|
|
56
|
+
scroll.maxOffsetY.value = contentSize.height - layoutMeasurement.height;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const stopScrolling = () => {
|
|
60
|
+
'worklet';
|
|
61
|
+
cancelAnimation( animationTimer );
|
|
62
|
+
|
|
63
|
+
isAnimationTimerActive.value = false;
|
|
64
|
+
isScrollActive.value = false;
|
|
65
|
+
velocityY.value = 0;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const startScrolling = ( y ) => {
|
|
69
|
+
'worklet';
|
|
70
|
+
stopScrolling();
|
|
71
|
+
offsetY.value = scroll.offsetY.value;
|
|
72
|
+
dragStartY.value = y;
|
|
73
|
+
|
|
74
|
+
animationTimer.value = 0;
|
|
75
|
+
animationTimer.value = withRepeat(
|
|
76
|
+
withTiming( 1, {
|
|
77
|
+
duration: SCROLL_INTERVAL_MS,
|
|
78
|
+
easing: Easing.linear,
|
|
79
|
+
} ),
|
|
80
|
+
-1,
|
|
81
|
+
true
|
|
82
|
+
);
|
|
83
|
+
isAnimationTimerActive.value = true;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const scrollOnDragOver = ( y ) => {
|
|
87
|
+
'worklet';
|
|
88
|
+
const dragDistance = Math.max(
|
|
89
|
+
Math.abs( y - dragStartY.value ) - SCROLL_INACTIVE_DISTANCE_PX,
|
|
90
|
+
0
|
|
91
|
+
);
|
|
92
|
+
const distancePercentage = dragDistance / windowHeight;
|
|
93
|
+
|
|
94
|
+
if ( ! isScrollActive.value ) {
|
|
95
|
+
isScrollActive.value = dragDistance > 0;
|
|
96
|
+
} else if ( y > dragStartY.value ) {
|
|
97
|
+
// User is dragging downwards.
|
|
98
|
+
velocityY.value = VELOCITY_MULTIPLIER * distancePercentage;
|
|
99
|
+
} else if ( y < dragStartY.value ) {
|
|
100
|
+
// User is dragging upwards.
|
|
101
|
+
velocityY.value = -VELOCITY_MULTIPLIER * distancePercentage;
|
|
102
|
+
} else {
|
|
103
|
+
velocityY.value = 0;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
useAnimatedReaction(
|
|
108
|
+
() => animationTimer.value,
|
|
109
|
+
( value, previous ) => {
|
|
110
|
+
if ( velocityY.value === 0 ) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const delta = Math.abs( value - previous );
|
|
115
|
+
let newOffset = offsetY.value + delta * velocityY.value;
|
|
116
|
+
|
|
117
|
+
if ( scroll.maxOffsetY.value !== 0 ) {
|
|
118
|
+
newOffset = Math.max(
|
|
119
|
+
0,
|
|
120
|
+
Math.min( scroll.maxOffsetY.value, newOffset )
|
|
121
|
+
);
|
|
122
|
+
} else {
|
|
123
|
+
// Scroll values are empty until receiving the first scroll event.
|
|
124
|
+
// In that case, the max offset is unknown and we can't clamp the
|
|
125
|
+
// new offset value.
|
|
126
|
+
newOffset = Math.max( 0, newOffset );
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
offsetY.value = newOffset;
|
|
130
|
+
scrollTo( animatedScrollRef, 0, offsetY.value, false );
|
|
131
|
+
}
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
return [ startScrolling, scrollOnDragOver, stopScrolling, scrollHandler ];
|
|
135
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { orderBy } from 'lodash';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* WordPress dependencies
|
|
8
|
+
*/
|
|
9
|
+
import { createContext, useContext } from '@wordpress/element';
|
|
10
|
+
|
|
11
|
+
export const DEFAULT_BLOCK_LIST_CONTEXT = {
|
|
12
|
+
scrollRef: null,
|
|
13
|
+
blocksLayouts: { current: {} },
|
|
14
|
+
findBlockLayoutByClientId,
|
|
15
|
+
getBlockLayoutsOrderedByYCoord,
|
|
16
|
+
findBlockLayoutByPosition,
|
|
17
|
+
updateBlocksLayouts,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const Context = createContext( DEFAULT_BLOCK_LIST_CONTEXT );
|
|
21
|
+
const { Provider, Consumer } = Context;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Finds a block's layout data by position.
|
|
25
|
+
*
|
|
26
|
+
* @param {Object} data Blocks layouts object.
|
|
27
|
+
* @param {Object} position Position to use for finding the block.
|
|
28
|
+
* @param {number} position.x X coordinate.
|
|
29
|
+
* @param {number} position.y Y coordinate.
|
|
30
|
+
*
|
|
31
|
+
* @return {Object|undefined} Found block layout data that matches the provided position. If none is found, `undefined` will be returned.
|
|
32
|
+
*/
|
|
33
|
+
function findBlockLayoutByPosition( data, position ) {
|
|
34
|
+
// Only enabled for root level blocks
|
|
35
|
+
return Object.values( data ).find( ( block ) => {
|
|
36
|
+
return (
|
|
37
|
+
position.x >= block.x &&
|
|
38
|
+
position.x <= block.x + block.width &&
|
|
39
|
+
position.y >= block.y &&
|
|
40
|
+
position.y <= block.y + block.height
|
|
41
|
+
);
|
|
42
|
+
} );
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Finds a block's layout data by its client Id.
|
|
47
|
+
*
|
|
48
|
+
* @param {Object} data Blocks layouts object.
|
|
49
|
+
* @param {string} clientId Block's clientId.
|
|
50
|
+
*
|
|
51
|
+
* @return {Object} Found block layout data.
|
|
52
|
+
*/
|
|
53
|
+
function findBlockLayoutByClientId( data, clientId ) {
|
|
54
|
+
return Object.entries( data ).reduce( ( acc, entry ) => {
|
|
55
|
+
const item = entry[ 1 ];
|
|
56
|
+
if ( acc ) {
|
|
57
|
+
return acc;
|
|
58
|
+
}
|
|
59
|
+
if ( item?.clientId === clientId ) {
|
|
60
|
+
return item;
|
|
61
|
+
}
|
|
62
|
+
if ( item?.innerBlocks && Object.keys( item.innerBlocks ).length > 0 ) {
|
|
63
|
+
return findBlockLayoutByClientId( item.innerBlocks, clientId );
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}, null );
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Deletes the layout data of a block by its client Id.
|
|
71
|
+
*
|
|
72
|
+
* @param {Object} data Blocks layouts object.
|
|
73
|
+
* @param {string} clientId Block's clientsId.
|
|
74
|
+
*
|
|
75
|
+
* @return {Object} Updated data object.
|
|
76
|
+
*/
|
|
77
|
+
export function deleteBlockLayoutByClientId( data, clientId ) {
|
|
78
|
+
return Object.keys( data ).reduce( ( acc, key ) => {
|
|
79
|
+
if ( key !== clientId ) {
|
|
80
|
+
acc[ key ] = data[ key ];
|
|
81
|
+
}
|
|
82
|
+
if (
|
|
83
|
+
data[ key ]?.innerBlocks &&
|
|
84
|
+
Object.keys( data[ key ].innerBlocks ).length > 0
|
|
85
|
+
) {
|
|
86
|
+
if ( acc[ key ] ) {
|
|
87
|
+
acc[ key ].innerBlocks = deleteBlockLayoutByClientId(
|
|
88
|
+
data[ key ].innerBlocks,
|
|
89
|
+
clientId
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return acc;
|
|
94
|
+
}, {} );
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Orders the block's layout data by its Y coordinate.
|
|
99
|
+
*
|
|
100
|
+
* @param {Object} data Blocks layouts object.
|
|
101
|
+
*
|
|
102
|
+
* @return {Object} Blocks layouts object ordered by its Y coordinate.
|
|
103
|
+
*/
|
|
104
|
+
function getBlockLayoutsOrderedByYCoord( data ) {
|
|
105
|
+
// Only enabled for root level blocks.
|
|
106
|
+
// Using lodash orderBy due to hermes not having
|
|
107
|
+
// stable support for native .sort(). It will be
|
|
108
|
+
// supported in the React Native version 0.68.0.
|
|
109
|
+
return orderBy( data, [ 'y', 'asc' ] );
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Updates or deletes a block's layout data in the blocksLayouts object,
|
|
114
|
+
* in case of deletion, the layout data is not required.
|
|
115
|
+
*
|
|
116
|
+
* @param {Object} blocksLayouts Blocks layouts object.
|
|
117
|
+
* @param {Object} blockData Block's layout data to add or remove to/from the blockLayouts object.
|
|
118
|
+
* @param {string} blockData.clientId Block's clientId.
|
|
119
|
+
* @param {?string} blockData.rootClientId Optional. Block's rootClientId.
|
|
120
|
+
* @param {?boolean} blockData.shouldRemove Optional. Flag to remove it from the blocksLayout list.
|
|
121
|
+
* @param {number} blockData.width Block's width.
|
|
122
|
+
* @param {number} blockData.height Block's height.
|
|
123
|
+
* @param {number} blockData.x Block's x coordinate (relative to the parent).
|
|
124
|
+
* @param {number} blockData.y Block's y coordinate (relative to the parent).
|
|
125
|
+
*/
|
|
126
|
+
|
|
127
|
+
function updateBlocksLayouts( blocksLayouts, blockData ) {
|
|
128
|
+
const { clientId, rootClientId, shouldRemove, ...layoutProps } = blockData;
|
|
129
|
+
|
|
130
|
+
if ( clientId && shouldRemove ) {
|
|
131
|
+
blocksLayouts.current = deleteBlockLayoutByClientId(
|
|
132
|
+
blocksLayouts.current,
|
|
133
|
+
clientId
|
|
134
|
+
);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if ( clientId && ! rootClientId ) {
|
|
139
|
+
blocksLayouts.current[ clientId ] = {
|
|
140
|
+
clientId,
|
|
141
|
+
rootClientId,
|
|
142
|
+
...layoutProps,
|
|
143
|
+
innerBlocks: {
|
|
144
|
+
...blocksLayouts.current[ clientId ]?.innerBlocks,
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
} else if ( clientId && rootClientId ) {
|
|
148
|
+
const block = findBlockLayoutByClientId(
|
|
149
|
+
blocksLayouts.current,
|
|
150
|
+
rootClientId
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
if ( block ) {
|
|
154
|
+
block.innerBlocks[ clientId ] = {
|
|
155
|
+
clientId,
|
|
156
|
+
rootClientId,
|
|
157
|
+
...layoutProps,
|
|
158
|
+
innerBlocks: {
|
|
159
|
+
...block.innerBlocks[ clientId ]?.innerBlocks,
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export { Provider as BlockListProvider, Consumer as BlockListConsumer };
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Hook that returns the block list context.
|
|
170
|
+
*
|
|
171
|
+
* @return {Object} Block list context
|
|
172
|
+
*/
|
|
173
|
+
export const useBlockListContext = () => {
|
|
174
|
+
return useContext( Context );
|
|
175
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { View } from 'react-native';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* WordPress dependencies
|
|
8
|
+
*/
|
|
9
|
+
import { useEffect, useCallback } from '@wordpress/element';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Internal dependencies
|
|
13
|
+
*/
|
|
14
|
+
import { useBlockListContext } from './block-list-context';
|
|
15
|
+
|
|
16
|
+
function BlockListItemCell( { children, clientId, rootClientId, onLayout } ) {
|
|
17
|
+
const { blocksLayouts, updateBlocksLayouts } = useBlockListContext();
|
|
18
|
+
|
|
19
|
+
useEffect( () => {
|
|
20
|
+
return () => {
|
|
21
|
+
updateBlocksLayouts( blocksLayouts, {
|
|
22
|
+
clientId,
|
|
23
|
+
shouldRemove: true,
|
|
24
|
+
} );
|
|
25
|
+
};
|
|
26
|
+
}, [] );
|
|
27
|
+
|
|
28
|
+
const onCellLayout = useCallback(
|
|
29
|
+
( event ) => {
|
|
30
|
+
const {
|
|
31
|
+
nativeEvent: { layout },
|
|
32
|
+
} = event;
|
|
33
|
+
updateBlocksLayouts( blocksLayouts, {
|
|
34
|
+
clientId,
|
|
35
|
+
rootClientId,
|
|
36
|
+
...layout,
|
|
37
|
+
} );
|
|
38
|
+
|
|
39
|
+
if ( onLayout ) {
|
|
40
|
+
onLayout( event );
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
[ clientId, rootClientId, updateBlocksLayouts, onLayout ]
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
return <View onLayout={ onCellLayout }>{ children }</View>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export default BlockListItemCell;
|
|
@@ -121,6 +121,7 @@ export class BlockListItem extends Component {
|
|
|
121
121
|
blockWidth,
|
|
122
122
|
...restProps
|
|
123
123
|
} = this.props;
|
|
124
|
+
|
|
124
125
|
const readableContentViewStyle =
|
|
125
126
|
contentResizeMode === 'stretch' && stretchStyle;
|
|
126
127
|
const { isContainerRelated } = alignmentHelpers;
|
|
@@ -162,25 +163,20 @@ export class BlockListItem extends Component {
|
|
|
162
163
|
}
|
|
163
164
|
|
|
164
165
|
render() {
|
|
165
|
-
const {
|
|
166
|
-
gridProperties,
|
|
167
|
-
clientId,
|
|
168
|
-
parentWidth,
|
|
169
|
-
items,
|
|
170
|
-
blockWidth,
|
|
171
|
-
} = this.props;
|
|
166
|
+
const { parentWidth, blockWidth, isGridItem } = this.props;
|
|
172
167
|
|
|
173
168
|
if ( ! blockWidth ) {
|
|
174
169
|
return null;
|
|
175
170
|
}
|
|
176
171
|
|
|
177
|
-
if (
|
|
172
|
+
if ( isGridItem ) {
|
|
173
|
+
const { numOfColumns, tileCount, tileIndex } = this.props;
|
|
178
174
|
return (
|
|
179
175
|
<Grid
|
|
180
|
-
numOfColumns={ gridProperties.numColumns }
|
|
181
|
-
tileCount={ items.length }
|
|
182
|
-
index={ items.indexOf( clientId ) }
|
|
183
176
|
maxWidth={ parentWidth }
|
|
177
|
+
numOfColumns={ numOfColumns }
|
|
178
|
+
tileCount={ tileCount }
|
|
179
|
+
index={ tileIndex }
|
|
184
180
|
>
|
|
185
181
|
{ this.renderContent() }
|
|
186
182
|
</Grid>
|
|
@@ -31,6 +31,7 @@ import BlockEdit from '../block-edit';
|
|
|
31
31
|
import BlockInvalidWarning from './block-invalid-warning';
|
|
32
32
|
import BlockMobileToolbar from '../block-mobile-toolbar';
|
|
33
33
|
import { store as blockEditorStore } from '../../store';
|
|
34
|
+
import BlockDraggable from '../block-draggable';
|
|
34
35
|
|
|
35
36
|
const emptyArray = [];
|
|
36
37
|
function BlockForType( {
|
|
@@ -189,6 +190,8 @@ class BlockListBlock extends Component {
|
|
|
189
190
|
marginHorizontal,
|
|
190
191
|
isInnerBlockSelected,
|
|
191
192
|
name,
|
|
193
|
+
draggingEnabled,
|
|
194
|
+
draggingClientId,
|
|
192
195
|
} = this.props;
|
|
193
196
|
|
|
194
197
|
if ( ! attributes || ! blockType ) {
|
|
@@ -256,14 +259,22 @@ class BlockListBlock extends Component {
|
|
|
256
259
|
] }
|
|
257
260
|
/>
|
|
258
261
|
) }
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
262
|
+
<BlockDraggable
|
|
263
|
+
clientId={ clientId }
|
|
264
|
+
draggingClientId={ draggingClientId }
|
|
265
|
+
enabled={ draggingEnabled }
|
|
266
|
+
>
|
|
267
|
+
{ () =>
|
|
268
|
+
isValid ? (
|
|
269
|
+
this.getBlockForType()
|
|
270
|
+
) : (
|
|
271
|
+
<BlockInvalidWarning
|
|
272
|
+
blockTitle={ title }
|
|
273
|
+
icon={ icon }
|
|
274
|
+
/>
|
|
275
|
+
)
|
|
276
|
+
}
|
|
277
|
+
</BlockDraggable>
|
|
267
278
|
<View
|
|
268
279
|
style={ styles.neutralToolbar }
|
|
269
280
|
ref={ this.anchorNodeRef }
|
|
@@ -278,6 +289,7 @@ class BlockListBlock extends Component {
|
|
|
278
289
|
blockWidth={ blockWidth }
|
|
279
290
|
anchorNodeRef={ this.anchorNodeRef.current }
|
|
280
291
|
isFullWidth={ isFullWidthToolbar }
|
|
292
|
+
draggingClientId={ draggingClientId }
|
|
281
293
|
/>
|
|
282
294
|
) }
|
|
283
295
|
</View>
|
|
@@ -308,6 +320,7 @@ export default compose( [
|
|
|
308
320
|
withSelect( ( select, { clientId } ) => {
|
|
309
321
|
const {
|
|
310
322
|
getBlockIndex,
|
|
323
|
+
getBlockCount,
|
|
311
324
|
getSettings,
|
|
312
325
|
isBlockSelected,
|
|
313
326
|
getBlock,
|
|
@@ -315,6 +328,7 @@ export default compose( [
|
|
|
315
328
|
getLowestCommonAncestorWithSelectedBlock,
|
|
316
329
|
getBlockParents,
|
|
317
330
|
hasSelectedInnerBlock,
|
|
331
|
+
getBlockHierarchyRootClientId,
|
|
318
332
|
} = select( blockEditorStore );
|
|
319
333
|
|
|
320
334
|
const order = getBlockIndex( clientId );
|
|
@@ -359,6 +373,18 @@ export default compose( [
|
|
|
359
373
|
const baseGlobalStyles = getSettings()
|
|
360
374
|
?.__experimentalGlobalStylesBaseStyles;
|
|
361
375
|
|
|
376
|
+
const hasInnerBlocks = getBlockCount( clientId ) > 0;
|
|
377
|
+
// For blocks with inner blocks, we only enable the dragging in the nested
|
|
378
|
+
// blocks if any of them are selected. This way we prevent the long-press
|
|
379
|
+
// gesture from being disabled for elements within the block UI.
|
|
380
|
+
const draggingEnabled =
|
|
381
|
+
! hasInnerBlocks ||
|
|
382
|
+
isSelected ||
|
|
383
|
+
! hasSelectedInnerBlock( clientId, true );
|
|
384
|
+
// Dragging nested blocks is not supported yet. For this reason, the block to be dragged
|
|
385
|
+
// will be the top in the hierarchy.
|
|
386
|
+
const draggingClientId = getBlockHierarchyRootClientId( clientId );
|
|
387
|
+
|
|
362
388
|
return {
|
|
363
389
|
icon,
|
|
364
390
|
name: name || 'core/missing',
|
|
@@ -366,6 +392,8 @@ export default compose( [
|
|
|
366
392
|
title,
|
|
367
393
|
attributes,
|
|
368
394
|
blockType,
|
|
395
|
+
draggingClientId,
|
|
396
|
+
draggingEnabled,
|
|
369
397
|
isSelected,
|
|
370
398
|
isInnerBlockSelected,
|
|
371
399
|
isValid,
|
|
@@ -25,10 +25,15 @@ import { __ } from '@wordpress/i18n';
|
|
|
25
25
|
import styles from './style.scss';
|
|
26
26
|
import BlockListAppender from '../block-list-appender';
|
|
27
27
|
import BlockListItem from './block-list-item';
|
|
28
|
+
import BlockListItemCell from './block-list-item-cell';
|
|
29
|
+
import {
|
|
30
|
+
BlockListProvider,
|
|
31
|
+
BlockListConsumer,
|
|
32
|
+
DEFAULT_BLOCK_LIST_CONTEXT,
|
|
33
|
+
} from './block-list-context';
|
|
34
|
+
import { BlockDraggableWrapper } from '../block-draggable';
|
|
28
35
|
import { store as blockEditorStore } from '../../store';
|
|
29
36
|
|
|
30
|
-
const BlockListContext = createContext();
|
|
31
|
-
|
|
32
37
|
export const OnCaretVerticalPositionChange = createContext();
|
|
33
38
|
|
|
34
39
|
const stylesMemo = {};
|
|
@@ -78,6 +83,9 @@ export class BlockList extends Component {
|
|
|
78
83
|
);
|
|
79
84
|
this.renderEmptyList = this.renderEmptyList.bind( this );
|
|
80
85
|
this.getExtraData = this.getExtraData.bind( this );
|
|
86
|
+
this.getCellRendererComponent = this.getCellRendererComponent.bind(
|
|
87
|
+
this
|
|
88
|
+
);
|
|
81
89
|
|
|
82
90
|
this.onLayout = this.onLayout.bind( this );
|
|
83
91
|
|
|
@@ -154,6 +162,18 @@ export class BlockList extends Component {
|
|
|
154
162
|
return this.extraData;
|
|
155
163
|
}
|
|
156
164
|
|
|
165
|
+
getCellRendererComponent( { children, item, onLayout } ) {
|
|
166
|
+
const { rootClientId } = this.props;
|
|
167
|
+
return (
|
|
168
|
+
<BlockListItemCell
|
|
169
|
+
children={ children }
|
|
170
|
+
clientId={ item }
|
|
171
|
+
onLayout={ onLayout }
|
|
172
|
+
rootClientId={ rootClientId }
|
|
173
|
+
/>
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
157
177
|
onLayout( { nativeEvent } ) {
|
|
158
178
|
const { layout } = nativeEvent;
|
|
159
179
|
const { blockWidth } = this.state;
|
|
@@ -170,20 +190,27 @@ export class BlockList extends Component {
|
|
|
170
190
|
}
|
|
171
191
|
|
|
172
192
|
render() {
|
|
173
|
-
const { isRootList } = this.props;
|
|
193
|
+
const { isRootList, isRTL } = this.props;
|
|
174
194
|
// Use of Context to propagate the main scroll ref to its children e.g InnerBlocks.
|
|
175
195
|
const blockList = isRootList ? (
|
|
176
|
-
<
|
|
177
|
-
{
|
|
178
|
-
|
|
196
|
+
<BlockListProvider
|
|
197
|
+
value={ {
|
|
198
|
+
...DEFAULT_BLOCK_LIST_CONTEXT,
|
|
199
|
+
scrollRef: this.scrollViewRef,
|
|
200
|
+
} }
|
|
201
|
+
>
|
|
202
|
+
<BlockDraggableWrapper isRTL={ isRTL }>
|
|
203
|
+
{ ( { onScroll } ) => this.renderList( { onScroll } ) }
|
|
204
|
+
</BlockDraggableWrapper>
|
|
205
|
+
</BlockListProvider>
|
|
179
206
|
) : (
|
|
180
|
-
<
|
|
181
|
-
{ (
|
|
207
|
+
<BlockListConsumer>
|
|
208
|
+
{ ( { scrollRef } ) =>
|
|
182
209
|
this.renderList( {
|
|
183
|
-
parentScrollRef:
|
|
210
|
+
parentScrollRef: scrollRef,
|
|
184
211
|
} )
|
|
185
212
|
}
|
|
186
|
-
</
|
|
213
|
+
</BlockListConsumer>
|
|
187
214
|
);
|
|
188
215
|
|
|
189
216
|
return (
|
|
@@ -212,7 +239,7 @@ export class BlockList extends Component {
|
|
|
212
239
|
contentResizeMode,
|
|
213
240
|
blockWidth,
|
|
214
241
|
} = this.props;
|
|
215
|
-
const { parentScrollRef } = extraProps;
|
|
242
|
+
const { parentScrollRef, onScroll } = extraProps;
|
|
216
243
|
|
|
217
244
|
const {
|
|
218
245
|
blockToolbar,
|
|
@@ -279,6 +306,7 @@ export class BlockList extends Component {
|
|
|
279
306
|
data={ blockClientIds }
|
|
280
307
|
keyExtractor={ identity }
|
|
281
308
|
renderItem={ this.renderItem }
|
|
309
|
+
CellRendererComponent={ this.getCellRendererComponent }
|
|
282
310
|
shouldPreventAutomaticScroll={
|
|
283
311
|
this.shouldFlatListPreventAutomaticScroll
|
|
284
312
|
}
|
|
@@ -286,6 +314,7 @@ export class BlockList extends Component {
|
|
|
286
314
|
ListHeaderComponent={ header }
|
|
287
315
|
ListEmptyComponent={ ! isReadOnly && this.renderEmptyList }
|
|
288
316
|
ListFooterComponent={ this.renderBlockListFooter }
|
|
317
|
+
onScroll={ onScroll }
|
|
289
318
|
/>
|
|
290
319
|
{ this.shouldShowInnerBlockAppender() && (
|
|
291
320
|
<View
|
|
@@ -321,6 +350,15 @@ export class BlockList extends Component {
|
|
|
321
350
|
gridProperties,
|
|
322
351
|
} = this.props;
|
|
323
352
|
const { blockWidth } = this.state;
|
|
353
|
+
|
|
354
|
+
// Extracting the grid item properties here to avoid
|
|
355
|
+
// re-renders in the blockListItem component.
|
|
356
|
+
const isGridItem = !! gridProperties;
|
|
357
|
+
const gridItemProps = gridProperties && {
|
|
358
|
+
numOfColumns: gridProperties.numColumns,
|
|
359
|
+
tileCount: blockClientIds.length,
|
|
360
|
+
tileIndex: blockClientIds.indexOf( clientId ),
|
|
361
|
+
};
|
|
324
362
|
return (
|
|
325
363
|
<BlockListItem
|
|
326
364
|
isStackedHorizontally={ isStackedHorizontally }
|
|
@@ -337,8 +375,8 @@ export class BlockList extends Component {
|
|
|
337
375
|
this.shouldShowInnerBlockAppender
|
|
338
376
|
}
|
|
339
377
|
blockWidth={ blockWidth }
|
|
340
|
-
|
|
341
|
-
|
|
378
|
+
isGridItem={ isGridItem }
|
|
379
|
+
{ ...gridItemProps }
|
|
342
380
|
/>
|
|
343
381
|
);
|
|
344
382
|
}
|
|
@@ -401,6 +439,8 @@ export default compose( [
|
|
|
401
439
|
|
|
402
440
|
const isFloatingToolbarVisible =
|
|
403
441
|
!! selectedBlockClientId && hasRootInnerBlocks;
|
|
442
|
+
const isRTL = getSettings().isRTL;
|
|
443
|
+
|
|
404
444
|
return {
|
|
405
445
|
blockClientIds,
|
|
406
446
|
blockCount,
|
|
@@ -411,6 +451,7 @@ export default compose( [
|
|
|
411
451
|
isFloatingToolbarVisible,
|
|
412
452
|
isStackedHorizontally,
|
|
413
453
|
maxWidth,
|
|
454
|
+
isRTL,
|
|
414
455
|
};
|
|
415
456
|
}
|
|
416
457
|
),
|