@ideasonpurpose/wp-theme-addons 0.4.0 → 0.5.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.
@@ -0,0 +1,336 @@
1
+ import "./carousel.css";
2
+ import { InspectorControls } from "@wordpress/block-editor";
3
+ import {
4
+ getBlockType,
5
+ getBlockTypes,
6
+ registerBlockVariation,
7
+ } from "@wordpress/blocks";
8
+ import {
9
+ PanelBody,
10
+ RangeControl,
11
+ ToggleControl,
12
+ __experimentalNumberControl as NumberControl,
13
+ } from "@wordpress/components";
14
+ import { createHigherOrderComponent } from "@wordpress/compose";
15
+ import { useSelect } from "@wordpress/data";
16
+ import { addFilter } from "@wordpress/hooks";
17
+ import { __ } from "@wordpress/i18n";
18
+ import { Path, SVG } from "@wordpress/primitives";
19
+ import { CAROUSEL_DEFAULTS as DEFAULTS } from "../../../../../lib/carousel-defaults.js";
20
+
21
+ const NAMESPACE = "ideasonpurpose/carousel";
22
+ const VIEWPORT = "ideasonpurpose/carousel-viewport";
23
+ const CONTAINER = "ideasonpurpose/carousel-container";
24
+
25
+ const GROUP_ATTRIBUTES = {
26
+ namespace: { type: "string", default: "" },
27
+ carouselAutoplay: { type: "boolean", default: DEFAULTS.autoplay },
28
+ carouselDuration: { type: "number", default: DEFAULTS.duration },
29
+ carouselArrows: { type: "boolean", default: DEFAULTS.arrows },
30
+ carouselDots: { type: "boolean", default: DEFAULTS.dots },
31
+ carouselLoop: { type: "boolean", default: DEFAULTS.loop },
32
+ carouselRandom: { type: "number", default: DEFAULTS.random },
33
+ };
34
+
35
+ const icon = (
36
+ <SVG xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 48 48">
37
+ <Path d="M18.75 14.11h10.5v11.334h-10.5V14.11Z" />
38
+ <Path
39
+ fillRule="evenodd"
40
+ clipRule="evenodd"
41
+ d="M29.906 12H18.094a2 2 0 0 0-2 2v20a2 2 0 0 0 2 2h11.812a2 2 0 0 0 2-2V14a2 2 0 0 0-2-2Zm-11.812-2a4 4 0 0 0-4 4v20a4 4 0 0 0 4 4h11.812a4 4 0 0 0 4-4V14a4 4 0 0 0-4-4H18.094Z"
42
+ />
43
+ <Path d="M18.75 27.476h10.5v2h-10.5zM18.75 31.263h8.406v2H18.75zM35.906 26.622h2v2h-2z" />
44
+ <Path d="M33.906 30.263h6.041a2 2 0 0 0 2-2v-9.636a2 2 0 0 0-2-2h-6.04v-2h6.04a4 4 0 0 1 4 4v9.636a4 4 0 0 1-4 4h-6.04v-2Z" />
45
+ <Path d="M39.906 18.622v6h-4v-6h4ZM12.116 26.622h-4v2h4v-2Z" />
46
+ <Path d="M14.094 30.263H8.053a2 2 0 0 1-2-2v-9.636a2 2 0 0 1 2-2h6.04v-2h-6.04a4 4 0 0 0-4 4v9.636a4 4 0 0 0 4 4h6.04v-2Z" />
47
+ <Path d="M8.094 18.622v6h4v-6h-4Z" />
48
+ </SVG>
49
+ );
50
+
51
+ const partIcon = (
52
+ <SVG xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 48 48">
53
+ <Path
54
+ clipRule="evenodd"
55
+ fillRule="evenodd"
56
+ d="M29.906 14H18.094a2 2 0 0 0-2 2v15.563a2 2 0 0 0 2 2h11.812a2 2 0 0 0 2-2V16a2 2 0 0 0-2-2Zm-11.812-2a4 4 0 0 0-4 4v15.563a4 4 0 0 0 4 4h11.812a4 4 0 0 0 4-4V16a4 4 0 0 0-4-4H18.094Z"
57
+ />
58
+ <Path d="M33.906 29.827h5.205a2 2 0 0 0 2-2v-7.654a2 2 0 0 0-2-2h-5.205v-2h5.205a4 4 0 0 1 4 4v7.654a4 4 0 0 1-4 4h-5.205v-2ZM14.44 18.173H9.235a2 2 0 0 0-2 2v7.654a2 2 0 0 0 2 2h5.205v2H9.235a4 4 0 0 1-4-4v-7.654a4 4 0 0 1 4-4h5.205v2ZM26.787 27.188a5.314 5.314 0 0 0 1.137-.954l1.195.69 1.088-1.885-1.193-.689a5.25 5.25 0 0 0 .258-1.463l1.357-.239-.378-2.143-1.357.239a5.277 5.277 0 0 0-.744-1.285l.887-1.057-1.667-1.399-.887 1.056a5.242 5.242 0 0 0-1.395-.51v-1.376h-2.176v1.377a5.242 5.242 0 0 0-1.396.51l-.886-1.057-1.667 1.4.887 1.056c-.305.388-.557.82-.744 1.285l-1.357-.24-.378 2.144 1.357.24c.017.508.106 1 .258 1.462l-1.193.689 1.088 1.885 1.195-.69c.332.369.714.69 1.137.954l-.472 1.297 2.045.744.471-1.294a5.308 5.308 0 0 0 1.486 0l.471 1.294 2.045-.745-.472-1.296ZM24 25.491a2.782 2.782 0 0 1-2.78-2.78A2.783 2.783 0 0 1 24 19.934a2.783 2.783 0 0 1 2.78 2.779 2.782 2.782 0 0 1-2.78 2.78Z" />
59
+ </SVG>
60
+ );
61
+
62
+ const groupMargin = { spacing: { margin: { top: "0", bottom: "0" } } };
63
+
64
+ function slideTemplate() {
65
+ return [
66
+ "core/group",
67
+ { className: "iop-carousel__slide", style: groupMargin },
68
+ [["core/paragraph", { placeholder: __("Slide") }]],
69
+ ];
70
+ }
71
+
72
+ function addAttributes(settings, name) {
73
+ const attributes = { ...settings.attributes };
74
+ if (!attributes.slideDuration) {
75
+ attributes.slideDuration = { type: "number" };
76
+ }
77
+ if (name === "core/group") {
78
+ for (const [key, value] of Object.entries(GROUP_ATTRIBUTES)) {
79
+ if (!attributes[key]) attributes[key] = value;
80
+ }
81
+ }
82
+ return { ...settings, attributes };
83
+ }
84
+
85
+ function patchRegisteredBlocks() {
86
+ for (const block of getBlockTypes()) {
87
+ if (!block.attributes.slideDuration) {
88
+ block.attributes.slideDuration = { type: "number" };
89
+ }
90
+ }
91
+ const group = getBlockType("core/group");
92
+ if (!group) return;
93
+ for (const [key, value] of Object.entries(GROUP_ATTRIBUTES)) {
94
+ if (!group.attributes[key]) group.attributes[key] = value;
95
+ }
96
+ }
97
+
98
+ let started = false;
99
+
100
+ export function initCarouselBlock() {
101
+ if (started) return;
102
+ started = true;
103
+
104
+ addFilter(
105
+ "blocks.registerBlockType",
106
+ "ideasonpurpose/carousel/attributes",
107
+ addAttributes,
108
+ );
109
+ patchRegisteredBlocks();
110
+
111
+ registerBlockVariation("core/group", {
112
+ name: "carousel",
113
+ title: __("Carousel"),
114
+ description: __(
115
+ "Slideshow of any blocks. Each direct child of the container is a slide.",
116
+ ),
117
+ keywords: ["slider", "slideshow"],
118
+ icon,
119
+ isActive: ["namespace"],
120
+ attributes: {
121
+ tagName: "section",
122
+ className: "iop-carousel",
123
+ namespace: NAMESPACE,
124
+ carouselAutoplay: DEFAULTS.autoplay,
125
+ carouselDuration: DEFAULTS.duration,
126
+ carouselArrows: DEFAULTS.arrows,
127
+ carouselDots: DEFAULTS.dots,
128
+ carouselLoop: DEFAULTS.loop,
129
+ carouselRandom: DEFAULTS.random,
130
+ },
131
+ innerBlocks: [
132
+ [
133
+ "core/group",
134
+ {
135
+ className: "iop-carousel__viewport",
136
+ namespace: VIEWPORT,
137
+ style: groupMargin,
138
+ },
139
+ [
140
+ [
141
+ "core/group",
142
+ {
143
+ className: "iop-carousel__container",
144
+ namespace: CONTAINER,
145
+ style: groupMargin,
146
+ },
147
+ [slideTemplate(), slideTemplate()],
148
+ ],
149
+ ],
150
+ ],
151
+ ],
152
+ });
153
+
154
+ registerBlockVariation("core/group", {
155
+ name: "carousel-viewport",
156
+ title: __("Carousel Viewport"),
157
+ description: __("Visible window of a carousel. Not inserted on its own."),
158
+ icon: partIcon,
159
+ isActive: ["namespace"],
160
+ scope: [],
161
+ attributes: {
162
+ className: "iop-carousel__viewport",
163
+ namespace: VIEWPORT,
164
+ },
165
+ });
166
+
167
+ registerBlockVariation("core/group", {
168
+ name: "carousel-container",
169
+ title: __("Carousel Container"),
170
+ description: __("Slide track. Each direct child is one slide."),
171
+ icon: partIcon,
172
+ isActive: ["namespace"],
173
+ scope: [],
174
+ attributes: {
175
+ className: "iop-carousel__container",
176
+ namespace: CONTAINER,
177
+ },
178
+ });
179
+
180
+ addFilter(
181
+ "editor.BlockEdit",
182
+ "ideasonpurpose/carousel/controls",
183
+ withCarouselControls,
184
+ );
185
+
186
+ addFilter(
187
+ "blocks.getSaveContent.extraProps",
188
+ "ideasonpurpose/carousel/slide-duration",
189
+ (extraProps, _blockType, attributes) => {
190
+ const seconds = Number(attributes?.slideDuration);
191
+ if (!Number.isFinite(seconds) || seconds <= 0) return extraProps;
192
+ extraProps["data-slide-duration"] = attributes.slideDuration;
193
+ return extraProps;
194
+ },
195
+ );
196
+ }
197
+
198
+ const withCarouselControls = createHigherOrderComponent((BlockEdit) => {
199
+ return (props) => {
200
+ const { attributes, setAttributes, clientId } = props;
201
+ const className = attributes?.className || "";
202
+ const slideMarked = className.includes("iop-carousel__slide");
203
+ const isCarousel =
204
+ props.name === "core/group" && attributes?.namespace === NAMESPACE;
205
+
206
+ const { isSlide, duration } = useSelect(
207
+ (select) => {
208
+ const store = select("core/block-editor");
209
+ const fallback = {
210
+ isSlide: slideMarked,
211
+ duration: DEFAULTS.duration,
212
+ };
213
+ if (!clientId || !store?.getBlockRootClientId) return fallback;
214
+
215
+ const parentId = store.getBlockRootClientId(clientId);
216
+ const parent = parentId ? store.getBlock(parentId) : null;
217
+ const isDirectSlide = parent?.attributes?.namespace === CONTAINER;
218
+ if (!isDirectSlide && !slideMarked) return fallback;
219
+
220
+ let id = parentId;
221
+ let carouselDuration = DEFAULTS.duration;
222
+ while (id) {
223
+ const block = store.getBlock(id);
224
+ if (block?.attributes?.namespace === NAMESPACE) {
225
+ carouselDuration =
226
+ block.attributes.carouselDuration || DEFAULTS.duration;
227
+ break;
228
+ }
229
+ id = store.getBlockRootClientId(id);
230
+ }
231
+ return { isSlide: true, duration: carouselDuration };
232
+ },
233
+ [clientId, slideMarked],
234
+ );
235
+
236
+ if (!isCarousel && !isSlide) return <BlockEdit {...props} />;
237
+
238
+ const autoplay = attributes.carouselAutoplay !== false;
239
+
240
+ return (
241
+ <>
242
+ <BlockEdit {...props} />
243
+ {isCarousel && (
244
+ <InspectorControls>
245
+ <PanelBody title={__("Carousel")}>
246
+ <ToggleControl
247
+ label={__("Autoplay")}
248
+ checked={autoplay}
249
+ onChange={(value) => setAttributes({ carouselAutoplay: value })}
250
+ __nextHasNoMarginBottom
251
+ />
252
+ {autoplay && (
253
+ <RangeControl
254
+ label={__("Slide duration (seconds)")}
255
+ value={attributes.carouselDuration || DEFAULTS.duration}
256
+ onChange={(value) =>
257
+ setAttributes({ carouselDuration: value })
258
+ }
259
+ min={1}
260
+ max={20}
261
+ step={1}
262
+ __nextHasNoMarginBottom
263
+ __next40pxDefaultSize
264
+ />
265
+ )}
266
+ <ToggleControl
267
+ label={__("Arrows")}
268
+ checked={attributes.carouselArrows !== false}
269
+ onChange={(value) => setAttributes({ carouselArrows: value })}
270
+ __nextHasNoMarginBottom
271
+ />
272
+ <ToggleControl
273
+ label={__("Pagination")}
274
+ checked={attributes.carouselDots !== false}
275
+ onChange={(value) => setAttributes({ carouselDots: value })}
276
+ __nextHasNoMarginBottom
277
+ />
278
+ <ToggleControl
279
+ label={__("Loop")}
280
+ checked={attributes.carouselLoop !== false}
281
+ onChange={(value) => setAttributes({ carouselLoop: value })}
282
+ __nextHasNoMarginBottom
283
+ />
284
+ <NumberControl
285
+ label={__("Random slides")}
286
+ help={__(
287
+ "Keep this many slides, chosen at random on each visit. 0 keeps all of them.",
288
+ )}
289
+ value={attributes.carouselRandom || 0}
290
+ min={0}
291
+ step={1}
292
+ onChange={(value) => {
293
+ const count = Number(value);
294
+ setAttributes({
295
+ carouselRandom:
296
+ Number.isFinite(count) && count > 0
297
+ ? Math.floor(count)
298
+ : 0,
299
+ });
300
+ }}
301
+ __next40pxDefaultSize
302
+ />
303
+ </PanelBody>
304
+ </InspectorControls>
305
+ )}
306
+ {isSlide && (
307
+ <InspectorControls>
308
+ <PanelBody title={__("Carousel slide")}>
309
+ <NumberControl
310
+ label={__("Slide duration (seconds)")}
311
+ help={__(
312
+ "Overrides the carousel duration for this slide. Blank uses the carousel setting.",
313
+ )}
314
+ placeholder={String(duration)}
315
+ value={attributes.slideDuration ?? ""}
316
+ min={0.25}
317
+ max={30}
318
+ step={0.25}
319
+ onChange={(value) => {
320
+ const seconds = Number(value);
321
+ setAttributes({
322
+ slideDuration:
323
+ Number.isFinite(seconds) && seconds > 0
324
+ ? seconds
325
+ : undefined,
326
+ });
327
+ }}
328
+ __next40pxDefaultSize
329
+ />
330
+ </PanelBody>
331
+ </InspectorControls>
332
+ )}
333
+ </>
334
+ );
335
+ };
336
+ }, "withCarouselControls");
@@ -20,6 +20,7 @@ class LinkedGroup
20
20
  * Callbacks are per block, and cannot be specific to a single variation
21
21
  */
22
22
  add_filter('register_block_type_args', [$this, 'wrap_render_callback'], 10, 2);
23
+ add_action('wp_enqueue_scripts', [$this, 'enqueue_styles']);
23
24
  }
24
25
 
25
26
  public function wrap_render_callback($args, $block_type)
@@ -73,4 +74,26 @@ class LinkedGroup
73
74
 
74
75
  return $blockHTML;
75
76
  }
77
+
78
+ /**
79
+ * Inline the same CSS the editor imports. Construction is the opt-in,
80
+ * so this runs on wp_enqueue_scripts and prints in the head.
81
+ */
82
+ public function enqueue_styles()
83
+ {
84
+ static $done = false;
85
+ if ($done) {
86
+ return;
87
+ }
88
+ $done = true;
89
+
90
+ $css = file_get_contents(__DIR__ . '/linked-group.css');
91
+ if (!$css) {
92
+ return;
93
+ }
94
+
95
+ wp_register_style('iop-linked-group', false);
96
+ wp_add_inline_style('iop-linked-group', $css);
97
+ wp_enqueue_style('iop-linked-group');
98
+ }
76
99
  }
@@ -0,0 +1,22 @@
1
+ .iop-linked-group {
2
+ position: relative;
3
+ }
4
+
5
+ .iop-linked-group a.iop-linked-group__link {
6
+ position: absolute;
7
+ top: 0;
8
+ left: 0;
9
+ z-index: 10;
10
+
11
+ width: 100%;
12
+ max-width: 100%;
13
+ height: 100%;
14
+ margin: 0;
15
+ }
16
+
17
+ /* every clickable element in the group needs to be position:relative */
18
+ .iop-linked-group button,
19
+ .iop-linked-group a:not(.iop-linked-group__link) {
20
+ position: relative;
21
+ z-index: 20;
22
+ }
@@ -1,6 +1,6 @@
1
1
  // @ts-check
2
2
 
3
- import "./linked-group.scss";
3
+ import "./linked-group.css";
4
4
  import {
5
5
  BlockControls,
6
6
  InspectorControls,
@@ -0,0 +1,23 @@
1
+ export interface CarouselOptions {
2
+ autoplay: boolean;
3
+ duration: number;
4
+ arrows: boolean;
5
+ dots: boolean;
6
+ loop: boolean;
7
+ random: number;
8
+ }
9
+
10
+ export function initCarousels(root?: ParentNode): void;
11
+
12
+ export function readCarouselOptions(wrapper: HTMLElement): CarouselOptions;
13
+
14
+ export function selectRandomSlides<T>(
15
+ slides: ArrayLike<T>,
16
+ count: number,
17
+ random?: () => number,
18
+ ): T[];
19
+
20
+ export function slideDelayMs(
21
+ slide: HTMLElement,
22
+ fallbackSeconds: number,
23
+ ): number;