PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 4.0.2
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v4.0.2
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 2.3.5 2.3.6 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.15 2.4.16 2.4.17 2.4.18 2.4.19 2.4.2 2.4.20 2.4.21 All 88 releases
post-carousel / src / blocks / container / resizableComponents.js

resizableComponents.js in Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News 4.0.2, at src/blocks/container/resizableComponents.js

152 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { ResizableBox } from "@wordpress/components";
2 import { store as blockEditorStore } from "@wordpress/block-editor";
3 import { useSelect } from "@wordpress/data";
4 import { rangerCss } from "../shared/helpFn";
5 import { useDeviceType } from "../../controls/controls";
6 import { dispatch } from "@wordpress/data";
7 import { useEffect, useRef, useState } from "@wordpress/element";
8
9 const ResizableComponents = ({ attributes, setAttributes, clientId }) => {
10 const { columns, containerFlexDirection, columnsGap } = attributes;
11 const deviceType = useDeviceType();
12 const { getBlockOrder, getBlock } = useSelect((select) => select("core/block-editor"), [ columns ]);
13 const { updateBlockAttributes } = dispatch("core/block-editor");
14 const innerBlocks = getBlockOrder(clientId);
15 const [ containerResizeWidth, setContainerResizeWidth ] = useState(0);
16 const resizableRef = useRef(null);
17
18 const flexDirection = containerFlexDirection?.device?.[ deviceType ];
19 const childBlockArray = flexDirection !== "row-reverse" ? innerBlocks : [...innerBlocks].reverse();
20
21 useEffect( () => {
22 if ( !resizableRef?.current ) {
23 return;
24 }
25 const measure = () => {
26 const containerEl = resizableRef.current.closest(".sp-smart-post-container-parent-block");
27 const containerWidth = containerEl?.offsetWidth || resizableRef?.current?.offsetWidth || 0;
28 setContainerResizeWidth( containerWidth );
29 }
30 measure();
31 const node = resizableRef.current?.closest?.(".sp-smart-post-container-parent-block");
32 let ro;
33 if (node && "ResizeObserver" in window) {
34 ro = new ResizeObserver(measure);
35 ro.observe(node);
36 }
37 window.addEventListener("resize", measure);
38 return () => {
39 window.removeEventListener("resize", measure);
40 if (ro) {
41 ro.disconnect();
42 }
43 };
44 }, []);
45
46 const getPctDelta = (totalWidth, delta) => {
47 const width = totalWidth || 0;
48 if (!width) {
49 return 0;
50 }
51 return Math.round(((delta?.width || 0) / width) * 100);
52 };
53
54 const minMaxWidth = ( currentIndex, minMax ) => {
55 let width = 0;
56 if (!Array.isArray(innerBlocks) || innerBlocks.length === 0) {
57 return width;
58 }
59 if ( minMax === "max" ) {
60 for ( let i = 0; i <= currentIndex; i++ ) {
61 const childWidth = getBlock( childBlockArray[i] )?.attributes?.columnWidth?.device?.[deviceType];
62 width = childWidth ? (width + childWidth ) : width - 8;
63 }
64 const currentChildWidth = getBlock( childBlockArray[ currentIndex + 1] )?.attributes?.columnWidth?.device?.[deviceType];
65 width = currentChildWidth ? (width + currentChildWidth - 8) : width - 8;
66 }
67 return width;
68 }
69 const getPreviousWidth = ( currentIndex ) => {
70 if ( ! currentIndex || currentIndex === 0 ) {
71 return 0;
72 }
73 let width = 0;
74 for ( let i = currentIndex; i > 0; i-- ) {
75 const childWidth = getBlock( childBlockArray[i - 1] )?.attributes?.columnWidth?.device?.[deviceType];
76 width = childWidth ? (width + childWidth ) : width;
77 }
78 return width;
79 }
80 const columnResizeHandler = ( event, direction, elt, delta ) => {
81 }
82
83 const columnResizeHandlerStop = ( event, direction, elt, delta ) => {
84 const childIndex = parseInt( elt.dataset?.column );
85 const resizableWidth = getPctDelta(containerResizeWidth, delta );
86 const childBlockWidth = getBlock(childBlockArray[ childIndex ])?.attributes?.columnWidth;
87 const updateWidth = childBlockWidth?.device?.[deviceType] + resizableWidth;
88 const nextChildBlockWidth = getBlock(childBlockArray[ childIndex + 1 ])?.attributes?.columnWidth;
89 const nextWidth = nextChildBlockWidth?.device?.[deviceType] - resizableWidth;
90
91 updateBlockAttributes( childBlockArray[ childIndex ], {
92 // columnWidth: {
93 // ...childBlockWidth,
94 // device: {
95 // ...childBlockWidth.device,
96 // [deviceType]: updateWidth,
97 // }
98 // }
99 } )
100 updateBlockAttributes( childBlockArray[ childIndex + 1 ], {
101 // columnWidth: {
102 // ...nextChildBlockWidth,
103 // device: {
104 // ...nextChildBlockWidth.device,
105 // [deviceType]: nextWidth,
106 // }
107 // }
108 } )
109 }
110
111 return (
112 <div
113 ref={ resizableRef }
114 className="sp-resizable-box"
115 style={{
116 position: "absolute",
117 top: 0,
118 left: "auto",
119 bottom: 0,
120 width: containerResizeWidth,
121 height: "auto",
122 }}>
123 { childBlockArray?.length > 1 && childBlockArray.slice( 0, -1 ).map( ( childId, index ) => {
124 const childBlock = getBlock( childId );
125 const childAttributes = childBlock.attributes;
126 const { columnWidth } = childAttributes;
127 const reduceGapWidth = (columnsGap?.device?.[ deviceType ] / childBlockArray.length ) * ( childBlockArray.length - ( index + 1 ) );
128 const reducePtc = parseFloat( reduceGapWidth / containerResizeWidth * 100 );
129 const widthStyle = `${columnWidth?.device?.[ deviceType ] + getPreviousWidth( index ) - reducePtc }%`;
130 const isLast = index === ( innerBlocks.length - 1 );
131 const minWidth = `${getPreviousWidth( index ) + 8}%`;
132 const maxWidth = `${minMaxWidth( index, "max")}%`;
133
134 return(
135 <ResizableBox
136 key={ index }
137 enable={{ right: !isLast }}
138 size={{ width: widthStyle, height: "100%" }}
139 style={{ position: "absolute"}}
140 minWidth={ minWidth }
141 maxWidth={ maxWidth }
142 onResize={ columnResizeHandler }
143 onResizeStop={ columnResizeHandlerStop }
144 data-column={ index }
145 />
146 )}
147 )}
148 </div>
149 )
150 }
151
152 export default ResizableComponents