@wordpress/block-library 8.23.0 → 8.24.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.
Files changed (136) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/build/block/edit.js +8 -11
  3. package/build/block/edit.js.map +1 -1
  4. package/build/block/edit.native.js +3 -4
  5. package/build/block/edit.native.js.map +1 -1
  6. package/build/button/edit.js +4 -7
  7. package/build/button/edit.js.map +1 -1
  8. package/build/column/edit.js +1 -1
  9. package/build/column/edit.js.map +1 -1
  10. package/build/column/edit.native.js +1 -1
  11. package/build/column/edit.native.js.map +1 -1
  12. package/build/cover/edit/index.js +2 -1
  13. package/build/cover/edit/index.js.map +1 -1
  14. package/build/file/edit.js +8 -9
  15. package/build/file/edit.js.map +1 -1
  16. package/build/file/view.js +4 -6
  17. package/build/file/view.js.map +1 -1
  18. package/build/group/edit.js +3 -7
  19. package/build/group/edit.js.map +1 -1
  20. package/build/image/edit.js +17 -18
  21. package/build/image/edit.js.map +1 -1
  22. package/build/image/edit.native.js +22 -15
  23. package/build/image/edit.native.js.map +1 -1
  24. package/build/image/image.js +35 -27
  25. package/build/image/image.js.map +1 -1
  26. package/build/image/index.js +2 -3
  27. package/build/image/index.js.map +1 -1
  28. package/build/image/view.js +233 -268
  29. package/build/image/view.js.map +1 -1
  30. package/build/navigation/view.js +153 -176
  31. package/build/navigation/view.js.map +1 -1
  32. package/build/navigation-link/index.js +2 -1
  33. package/build/navigation-link/index.js.map +1 -1
  34. package/build/pattern/edit.js +1 -3
  35. package/build/pattern/edit.js.map +1 -1
  36. package/build/post-template/edit.js +1 -1
  37. package/build/post-template/edit.js.map +1 -1
  38. package/build/query/view.js +52 -60
  39. package/build/query/view.js.map +1 -1
  40. package/build/search/view.js +66 -74
  41. package/build/search/view.js.map +1 -1
  42. package/build/utils/remove-anchor-tag.js +17 -0
  43. package/build/utils/remove-anchor-tag.js.map +1 -0
  44. package/build-module/block/edit.js +8 -11
  45. package/build-module/block/edit.js.map +1 -1
  46. package/build-module/block/edit.native.js +3 -4
  47. package/build-module/block/edit.native.js.map +1 -1
  48. package/build-module/button/edit.js +4 -7
  49. package/build-module/button/edit.js.map +1 -1
  50. package/build-module/column/edit.js +1 -1
  51. package/build-module/column/edit.js.map +1 -1
  52. package/build-module/column/edit.native.js +1 -1
  53. package/build-module/column/edit.native.js.map +1 -1
  54. package/build-module/cover/edit/index.js +2 -1
  55. package/build-module/cover/edit/index.js.map +1 -1
  56. package/build-module/file/edit.js +8 -9
  57. package/build-module/file/edit.js.map +1 -1
  58. package/build-module/file/view.js +5 -7
  59. package/build-module/file/view.js.map +1 -1
  60. package/build-module/group/edit.js +3 -7
  61. package/build-module/group/edit.js.map +1 -1
  62. package/build-module/image/edit.js +18 -19
  63. package/build-module/image/edit.js.map +1 -1
  64. package/build-module/image/edit.native.js +23 -16
  65. package/build-module/image/edit.native.js.map +1 -1
  66. package/build-module/image/image.js +36 -28
  67. package/build-module/image/image.js.map +1 -1
  68. package/build-module/image/index.js +2 -3
  69. package/build-module/image/index.js.map +1 -1
  70. package/build-module/image/view.js +234 -269
  71. package/build-module/image/view.js.map +1 -1
  72. package/build-module/navigation/view.js +154 -177
  73. package/build-module/navigation/view.js.map +1 -1
  74. package/build-module/navigation-link/index.js +2 -1
  75. package/build-module/navigation-link/index.js.map +1 -1
  76. package/build-module/pattern/edit.js +1 -3
  77. package/build-module/pattern/edit.js.map +1 -1
  78. package/build-module/post-template/edit.js +1 -1
  79. package/build-module/post-template/edit.js.map +1 -1
  80. package/build-module/query/view.js +53 -61
  81. package/build-module/query/view.js.map +1 -1
  82. package/build-module/search/view.js +67 -75
  83. package/build-module/search/view.js.map +1 -1
  84. package/build-module/utils/remove-anchor-tag.js +11 -0
  85. package/build-module/utils/remove-anchor-tag.js.map +1 -0
  86. package/build-style/cover/style-rtl.css +14 -14
  87. package/build-style/cover/style.css +14 -14
  88. package/build-style/editor-rtl.css +6 -9
  89. package/build-style/editor.css +6 -9
  90. package/build-style/gallery/style-rtl.css +28 -0
  91. package/build-style/gallery/style.css +28 -0
  92. package/build-style/image/editor-rtl.css +0 -3
  93. package/build-style/image/editor.css +0 -3
  94. package/build-style/style-rtl.css +42 -14
  95. package/build-style/style.css +42 -14
  96. package/package.json +32 -32
  97. package/src/block/edit.js +18 -19
  98. package/src/block/edit.native.js +5 -13
  99. package/src/button/edit.js +6 -6
  100. package/src/buttons/test/__snapshots__/edit.native.js.snap +0 -6
  101. package/src/buttons/test/edit.native.js +0 -27
  102. package/src/column/edit.js +1 -1
  103. package/src/column/edit.native.js +1 -1
  104. package/src/cover/edit/index.js +1 -0
  105. package/src/cover/style.scss +1 -1
  106. package/src/cover/test/edit.js +1 -1
  107. package/src/editor.scss +6 -6
  108. package/src/file/edit.js +11 -10
  109. package/src/file/index.php +30 -11
  110. package/src/file/view.js +5 -7
  111. package/src/gallery/style.scss +1 -0
  112. package/src/group/edit.js +3 -11
  113. package/src/heading/test/__snapshots__/index.native.js.snap +6 -0
  114. package/src/heading/test/index.native.js +40 -0
  115. package/src/image/block.json +2 -3
  116. package/src/image/edit.js +16 -21
  117. package/src/image/edit.native.js +17 -18
  118. package/src/image/editor.scss +0 -7
  119. package/src/image/image.js +48 -51
  120. package/src/image/index.php +54 -45
  121. package/src/image/view.js +278 -334
  122. package/src/navigation/index.php +19 -10
  123. package/src/navigation/view.js +159 -192
  124. package/src/navigation-link/block.json +2 -1
  125. package/src/paragraph/test/edit.native.js +37 -1
  126. package/src/pattern/edit.js +5 -3
  127. package/src/post-template/edit.js +1 -1
  128. package/src/query/index.php +36 -22
  129. package/src/query/view.js +58 -65
  130. package/src/query-pagination-next/index.php +3 -3
  131. package/src/query-pagination-numbers/index.php +1 -1
  132. package/src/query-pagination-previous/index.php +3 -3
  133. package/src/search/index.php +40 -40
  134. package/src/search/view.js +58 -63
  135. package/src/utils/remove-anchor-tag.js +10 -0
  136. package/tsconfig.json +1 -0
@@ -1,10 +1,10 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
- import { store } from '@wordpress/interactivity';
4
+ import { store, getContext, getElement } from '@wordpress/interactivity';
5
5
  const focusableSelectors = ['a[href]', 'area[href]', 'input:not([disabled]):not([type="hidden"]):not([aria-hidden])', 'select:not([disabled]):not([aria-hidden])', 'textarea:not([disabled]):not([aria-hidden])', 'button:not([disabled]):not([aria-hidden])', 'iframe', 'object', 'embed', '[contenteditable]', '[tabindex]:not([tabindex^="-"])'];
6
6
 
7
- /*
7
+ /**
8
8
  * Stores a context-bound scroll handler.
9
9
  *
10
10
  * This callback could be defined inline inside of the store
@@ -19,7 +19,7 @@ const focusableSelectors = ['a[href]', 'area[href]', 'input:not([disabled]):not(
19
19
  */
20
20
  let scrollCallback;
21
21
 
22
- /*
22
+ /**
23
23
  * Tracks whether user is touching screen; used to
24
24
  * differentiate behavior for touch and mouse input.
25
25
  *
@@ -27,7 +27,7 @@ let scrollCallback;
27
27
  */
28
28
  let isTouching = false;
29
29
 
30
- /*
30
+ /**
31
31
  * Tracks the last time the screen was touched; used to
32
32
  * differentiate behavior for touch and mouse input.
33
33
  *
@@ -35,7 +35,7 @@ let isTouching = false;
35
35
  */
36
36
  let lastTouchTime = 0;
37
37
 
38
- /*
38
+ /**
39
39
  * Lightbox page-scroll handler: prevents scrolling.
40
40
  *
41
41
  * This handler is added to prevent scrolling behaviors that
@@ -51,304 +51,269 @@ let lastTouchTime = 0;
51
51
  * instead to not rely on JavaScript, but this seems to be the best approach
52
52
  * for now that provides the best visual experience.
53
53
  *
54
- * @param {Object} context Interactivity page context?
54
+ * @param {Object} ctx Context object with the `core/image` namespace.
55
55
  */
56
- function handleScroll(context) {
56
+ function handleScroll(ctx) {
57
57
  // We can't override the scroll behavior on mobile devices
58
58
  // because doing so breaks the pinch to zoom functionality, and we
59
59
  // want to allow users to zoom in further on the high-res image.
60
60
  if (!isTouching && Date.now() - lastTouchTime > 450) {
61
61
  // We are unable to use event.preventDefault() to prevent scrolling
62
62
  // because the scroll event can't be canceled, so we reset the position instead.
63
- window.scrollTo(context.core.image.scrollLeftReset, context.core.image.scrollTopReset);
63
+ window.scrollTo(ctx.scrollLeftReset, ctx.scrollTopReset);
64
64
  }
65
65
  }
66
- store({
66
+ const {
67
+ state,
68
+ actions,
69
+ callbacks
70
+ } = store('core/image', {
67
71
  state: {
68
- core: {
69
- image: {
70
- windowWidth: window.innerWidth,
71
- windowHeight: window.innerHeight
72
+ windowWidth: window.innerWidth,
73
+ windowHeight: window.innerHeight,
74
+ get roleAttribute() {
75
+ const ctx = getContext();
76
+ return ctx.lightboxEnabled ? 'dialog' : null;
77
+ },
78
+ get ariaModal() {
79
+ const ctx = getContext();
80
+ return ctx.lightboxEnabled ? 'true' : null;
81
+ },
82
+ get dialogLabel() {
83
+ const ctx = getContext();
84
+ return ctx.lightboxEnabled ? ctx.dialogLabel : null;
85
+ },
86
+ get lightboxObjectFit() {
87
+ const ctx = getContext();
88
+ if (ctx.initialized) {
89
+ return 'cover';
72
90
  }
91
+ },
92
+ get enlargedImgSrc() {
93
+ const ctx = getContext();
94
+ return ctx.initialized ? ctx.imageUploadedSrc : 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=';
73
95
  }
74
96
  },
75
97
  actions: {
76
- core: {
77
- image: {
78
- showLightbox: ({
79
- context,
80
- event
81
- }) => {
82
- // We can't initialize the lightbox until the reference
83
- // image is loaded, otherwise the UX is broken.
84
- if (!context.core.image.imageLoaded) {
85
- return;
86
- }
87
- context.core.image.initialized = true;
88
- context.core.image.lastFocusedElement = window.document.activeElement;
89
- context.core.image.scrollDelta = 0;
90
- context.core.image.pointerType = event.pointerType;
91
- context.core.image.lightboxEnabled = true;
92
- setStyles(context, context.core.image.imageRef);
93
- context.core.image.scrollTopReset = window.pageYOffset || document.documentElement.scrollTop;
98
+ showLightbox(event) {
99
+ const ctx = getContext();
100
+ // We can't initialize the lightbox until the reference
101
+ // image is loaded, otherwise the UX is broken.
102
+ if (!ctx.imageLoaded) {
103
+ return;
104
+ }
105
+ ctx.initialized = true;
106
+ ctx.lastFocusedElement = window.document.activeElement;
107
+ ctx.scrollDelta = 0;
108
+ ctx.pointerType = event.pointerType;
109
+ ctx.lightboxEnabled = true;
110
+ setStyles(ctx, ctx.imageRef);
111
+ ctx.scrollTopReset = window.pageYOffset || document.documentElement.scrollTop;
94
112
 
95
- // In most cases, this value will be 0, but this is included
96
- // in case a user has created a page with horizontal scrolling.
97
- context.core.image.scrollLeftReset = window.pageXOffset || document.documentElement.scrollLeft;
113
+ // In most cases, this value will be 0, but this is included
114
+ // in case a user has created a page with horizontal scrolling.
115
+ ctx.scrollLeftReset = window.pageXOffset || document.documentElement.scrollLeft;
98
116
 
99
- // We define and bind the scroll callback here so
100
- // that we can pass the context and as an argument.
101
- // We may be able to change this in the future if we
102
- // define the scroll callback in the store instead, but
103
- // this approach seems to tbe clearest for now.
104
- scrollCallback = handleScroll.bind(null, context);
117
+ // We define and bind the scroll callback here so
118
+ // that we can pass the context and as an argument.
119
+ // We may be able to change this in the future if we
120
+ // define the scroll callback in the store instead, but
121
+ // this approach seems to tbe clearest for now.
122
+ scrollCallback = handleScroll.bind(null, ctx);
105
123
 
106
- // We need to add a scroll event listener to the window
107
- // here because we are unable to otherwise access it via
108
- // the Interactivity API directives. If we add a native way
109
- // to access the window, we can remove this.
110
- window.addEventListener('scroll', scrollCallback, false);
111
- },
112
- hideLightbox: async ({
113
- context
114
- }) => {
115
- context.core.image.hideAnimationEnabled = true;
116
- if (context.core.image.lightboxEnabled) {
117
- // We want to wait until the close animation is completed
118
- // before allowing a user to scroll again. The duration of this
119
- // animation is defined in the styles.scss and depends on if the
120
- // animation is 'zoom' or 'fade', but in any case we should wait
121
- // a few milliseconds longer than the duration, otherwise a user
122
- // may scroll too soon and cause the animation to look sloppy.
123
- setTimeout(function () {
124
- window.removeEventListener('scroll', scrollCallback);
125
- // If we don't delay before changing the focus,
126
- // the focus ring will appear on Firefox before
127
- // the image has finished animating, which looks broken.
128
- context.core.image.lightboxTriggerRef.focus({
129
- preventScroll: true
130
- });
131
- }, 450);
132
- context.core.image.lightboxEnabled = false;
133
- }
134
- },
135
- handleKeydown: ({
136
- context,
137
- actions,
138
- event
139
- }) => {
140
- if (context.core.image.lightboxEnabled) {
141
- if (event.key === 'Tab' || event.keyCode === 9) {
142
- // If shift + tab it change the direction
143
- if (event.shiftKey && window.document.activeElement === context.core.image.firstFocusableElement) {
144
- event.preventDefault();
145
- context.core.image.lastFocusableElement.focus();
146
- } else if (!event.shiftKey && window.document.activeElement === context.core.image.lastFocusableElement) {
147
- event.preventDefault();
148
- context.core.image.firstFocusableElement.focus();
149
- }
150
- }
151
- if (event.key === 'Escape' || event.keyCode === 27) {
152
- actions.core.image.hideLightbox({
153
- context,
154
- event
155
- });
156
- }
157
- }
158
- },
159
- // This is fired just by lazily loaded
160
- // images on the page, not all images.
161
- handleLoad: ({
162
- context,
163
- effects,
164
- ref
165
- }) => {
166
- context.core.image.imageLoaded = true;
167
- context.core.image.imageCurrentSrc = ref.currentSrc;
168
- effects.core.image.setButtonStyles({
169
- context,
170
- ref
124
+ // We need to add a scroll event listener to the window
125
+ // here because we are unable to otherwise access it via
126
+ // the Interactivity API directives. If we add a native way
127
+ // to access the window, we can remove this.
128
+ window.addEventListener('scroll', scrollCallback, false);
129
+ },
130
+ hideLightbox() {
131
+ const ctx = getContext();
132
+ ctx.hideAnimationEnabled = true;
133
+ if (ctx.lightboxEnabled) {
134
+ // We want to wait until the close animation is completed
135
+ // before allowing a user to scroll again. The duration of this
136
+ // animation is defined in the styles.scss and depends on if the
137
+ // animation is 'zoom' or 'fade', but in any case we should wait
138
+ // a few milliseconds longer than the duration, otherwise a user
139
+ // may scroll too soon and cause the animation to look sloppy.
140
+ setTimeout(function () {
141
+ window.removeEventListener('scroll', scrollCallback);
142
+ // If we don't delay before changing the focus,
143
+ // the focus ring will appear on Firefox before
144
+ // the image has finished animating, which looks broken.
145
+ ctx.lightboxTriggerRef.focus({
146
+ preventScroll: true
171
147
  });
172
- },
173
- handleTouchStart: () => {
174
- isTouching = true;
175
- },
176
- handleTouchMove: ({
177
- context,
178
- event
179
- }) => {
180
- // On mobile devices, we want to prevent triggering the
181
- // scroll event because otherwise the page jumps around as
182
- // we reset the scroll position. This also means that closing
183
- // the lightbox requires that a user perform a simple tap. This
184
- // may be changed in the future if we find a better alternative
185
- // to override or reset the scroll position during swipe actions.
186
- if (context.core.image.lightboxEnabled) {
148
+ }, 450);
149
+ ctx.lightboxEnabled = false;
150
+ }
151
+ },
152
+ handleKeydown(event) {
153
+ const ctx = getContext();
154
+ if (ctx.lightboxEnabled) {
155
+ if (event.key === 'Tab' || event.keyCode === 9) {
156
+ // If shift + tab it change the direction
157
+ if (event.shiftKey && window.document.activeElement === ctx.firstFocusableElement) {
158
+ event.preventDefault();
159
+ ctx.lastFocusableElement.focus();
160
+ } else if (!event.shiftKey && window.document.activeElement === ctx.lastFocusableElement) {
187
161
  event.preventDefault();
162
+ ctx.firstFocusableElement.focus();
188
163
  }
189
- },
190
- handleTouchEnd: () => {
191
- // We need to wait a few milliseconds before resetting
192
- // to ensure that pinch to zoom works consistently
193
- // on mobile devices when the lightbox is open.
194
- lastTouchTime = Date.now();
195
- isTouching = false;
196
164
  }
197
- }
198
- }
199
- },
200
- selectors: {
201
- core: {
202
- image: {
203
- roleAttribute: ({
204
- context
205
- }) => {
206
- return context.core.image.lightboxEnabled ? 'dialog' : null;
207
- },
208
- ariaModal: ({
209
- context
210
- }) => {
211
- return context.core.image.lightboxEnabled ? 'true' : null;
212
- },
213
- dialogLabel: ({
214
- context
215
- }) => {
216
- return context.core.image.lightboxEnabled ? context.core.image.dialogLabel : null;
217
- },
218
- lightboxObjectFit: ({
219
- context
220
- }) => {
221
- if (context.core.image.initialized) {
222
- return 'cover';
223
- }
224
- },
225
- enlargedImgSrc: ({
226
- context
227
- }) => {
228
- return context.core.image.initialized ? context.core.image.imageUploadedSrc : 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=';
165
+ if (event.key === 'Escape' || event.keyCode === 27) {
166
+ actions.hideLightbox(event);
229
167
  }
230
168
  }
169
+ },
170
+ // This is fired just by lazily loaded
171
+ // images on the page, not all images.
172
+ handleLoad() {
173
+ const ctx = getContext();
174
+ const {
175
+ ref
176
+ } = getElement();
177
+ ctx.imageLoaded = true;
178
+ ctx.imageCurrentSrc = ref.currentSrc;
179
+ callbacks.setButtonStyles();
180
+ },
181
+ handleTouchStart() {
182
+ isTouching = true;
183
+ },
184
+ handleTouchMove(event) {
185
+ const ctx = getContext();
186
+ // On mobile devices, we want to prevent triggering the
187
+ // scroll event because otherwise the page jumps around as
188
+ // we reset the scroll position. This also means that closing
189
+ // the lightbox requires that a user perform a simple tap. This
190
+ // may be changed in the future if we find a better alternative
191
+ // to override or reset the scroll position during swipe actions.
192
+ if (ctx.lightboxEnabled) {
193
+ event.preventDefault();
194
+ }
195
+ },
196
+ handleTouchEnd() {
197
+ // We need to wait a few milliseconds before resetting
198
+ // to ensure that pinch to zoom works consistently
199
+ // on mobile devices when the lightbox is open.
200
+ lastTouchTime = Date.now();
201
+ isTouching = false;
231
202
  }
232
203
  },
233
- effects: {
234
- core: {
235
- image: {
236
- initOriginImage: ({
237
- context,
238
- ref
239
- }) => {
240
- context.core.image.imageRef = ref;
241
- context.core.image.lightboxTriggerRef = ref.parentElement.querySelector('.lightbox-trigger');
242
- if (ref.complete) {
243
- context.core.image.imageLoaded = true;
244
- context.core.image.imageCurrentSrc = ref.currentSrc;
245
- }
246
- },
247
- initLightbox: async ({
248
- context,
249
- ref
250
- }) => {
251
- if (context.core.image.lightboxEnabled) {
252
- const focusableElements = ref.querySelectorAll(focusableSelectors);
253
- context.core.image.firstFocusableElement = focusableElements[0];
254
- context.core.image.lastFocusableElement = focusableElements[focusableElements.length - 1];
204
+ callbacks: {
205
+ initOriginImage() {
206
+ const ctx = getContext();
207
+ const {
208
+ ref
209
+ } = getElement();
210
+ ctx.imageRef = ref;
211
+ ctx.lightboxTriggerRef = ref.parentElement.querySelector('.lightbox-trigger');
212
+ if (ref.complete) {
213
+ ctx.imageLoaded = true;
214
+ ctx.imageCurrentSrc = ref.currentSrc;
215
+ }
216
+ },
217
+ initLightbox() {
218
+ const ctx = getContext();
219
+ const {
220
+ ref
221
+ } = getElement();
222
+ if (ctx.lightboxEnabled) {
223
+ const focusableElements = ref.querySelectorAll(focusableSelectors);
224
+ ctx.firstFocusableElement = focusableElements[0];
225
+ ctx.lastFocusableElement = focusableElements[focusableElements.length - 1];
255
226
 
256
- // Move focus to the dialog when opening it.
257
- ref.focus();
258
- }
259
- },
260
- setButtonStyles: ({
261
- context,
262
- ref
263
- }) => {
264
- const {
265
- naturalWidth,
266
- naturalHeight,
267
- offsetWidth,
268
- offsetHeight
269
- } = ref;
227
+ // Move focus to the dialog when opening it.
228
+ ref.focus();
229
+ }
230
+ },
231
+ setButtonStyles() {
232
+ const {
233
+ ref
234
+ } = getElement();
235
+ const {
236
+ naturalWidth,
237
+ naturalHeight,
238
+ offsetWidth,
239
+ offsetHeight
240
+ } = ref;
270
241
 
271
- // If the image isn't loaded yet, we can't
272
- // calculate where the button should be.
273
- if (naturalWidth === 0 || naturalHeight === 0) {
274
- return;
275
- }
276
- const figure = ref.parentElement;
277
- const figureWidth = ref.parentElement.clientWidth;
242
+ // If the image isn't loaded yet, we can't
243
+ // calculate where the button should be.
244
+ if (naturalWidth === 0 || naturalHeight === 0) {
245
+ return;
246
+ }
247
+ const figure = ref.parentElement;
248
+ const figureWidth = ref.parentElement.clientWidth;
278
249
 
279
- // We need special handling for the height because
280
- // a caption will cause the figure to be taller than
281
- // the image, which means we need to account for that
282
- // when calculating the placement of the button in the
283
- // top right corner of the image.
284
- let figureHeight = ref.parentElement.clientHeight;
285
- const caption = figure.querySelector('figcaption');
286
- if (caption) {
287
- const captionComputedStyle = window.getComputedStyle(caption);
288
- if (!['absolute', 'fixed'].includes(captionComputedStyle.position)) {
289
- figureHeight = figureHeight - caption.offsetHeight - parseFloat(captionComputedStyle.marginTop) - parseFloat(captionComputedStyle.marginBottom);
290
- }
291
- }
292
- const buttonOffsetTop = figureHeight - offsetHeight;
293
- const buttonOffsetRight = figureWidth - offsetWidth;
250
+ // We need special handling for the height because
251
+ // a caption will cause the figure to be taller than
252
+ // the image, which means we need to account for that
253
+ // when calculating the placement of the button in the
254
+ // top right corner of the image.
255
+ let figureHeight = ref.parentElement.clientHeight;
256
+ const caption = figure.querySelector('figcaption');
257
+ if (caption) {
258
+ const captionComputedStyle = window.getComputedStyle(caption);
259
+ if (!['absolute', 'fixed'].includes(captionComputedStyle.position)) {
260
+ figureHeight = figureHeight - caption.offsetHeight - parseFloat(captionComputedStyle.marginTop) - parseFloat(captionComputedStyle.marginBottom);
261
+ }
262
+ }
263
+ const buttonOffsetTop = figureHeight - offsetHeight;
264
+ const buttonOffsetRight = figureWidth - offsetWidth;
265
+ const ctx = getContext();
294
266
 
295
- // In the case of an image with object-fit: contain, the
296
- // size of the <img> element can be larger than the image itself,
297
- // so we need to calculate where to place the button.
298
- if (context.core.image.scaleAttr === 'contain') {
299
- // Natural ratio of the image.
300
- const naturalRatio = naturalWidth / naturalHeight;
301
- // Offset ratio of the image.
302
- const offsetRatio = offsetWidth / offsetHeight;
303
- if (naturalRatio >= offsetRatio) {
304
- // If it reaches the width first, keep
305
- // the width and compute the height.
306
- const referenceHeight = offsetWidth / naturalRatio;
307
- context.core.image.imageButtonTop = (offsetHeight - referenceHeight) / 2 + buttonOffsetTop + 16;
308
- context.core.image.imageButtonRight = buttonOffsetRight + 16;
309
- } else {
310
- // If it reaches the height first, keep
311
- // the height and compute the width.
312
- const referenceWidth = offsetHeight * naturalRatio;
313
- context.core.image.imageButtonTop = buttonOffsetTop + 16;
314
- context.core.image.imageButtonRight = (offsetWidth - referenceWidth) / 2 + buttonOffsetRight + 16;
315
- }
316
- } else {
317
- context.core.image.imageButtonTop = buttonOffsetTop + 16;
318
- context.core.image.imageButtonRight = buttonOffsetRight + 16;
319
- }
320
- },
321
- setStylesOnResize: ({
322
- state,
323
- context,
324
- ref
325
- }) => {
326
- if (context.core.image.lightboxEnabled && (state.core.image.windowWidth || state.core.image.windowHeight)) {
327
- setStyles(context, ref);
328
- }
267
+ // In the case of an image with object-fit: contain, the
268
+ // size of the <img> element can be larger than the image itself,
269
+ // so we need to calculate where to place the button.
270
+ if (ctx.scaleAttr === 'contain') {
271
+ // Natural ratio of the image.
272
+ const naturalRatio = naturalWidth / naturalHeight;
273
+ // Offset ratio of the image.
274
+ const offsetRatio = offsetWidth / offsetHeight;
275
+ if (naturalRatio >= offsetRatio) {
276
+ // If it reaches the width first, keep
277
+ // the width and compute the height.
278
+ const referenceHeight = offsetWidth / naturalRatio;
279
+ ctx.imageButtonTop = (offsetHeight - referenceHeight) / 2 + buttonOffsetTop + 16;
280
+ ctx.imageButtonRight = buttonOffsetRight + 16;
281
+ } else {
282
+ // If it reaches the height first, keep
283
+ // the height and compute the width.
284
+ const referenceWidth = offsetHeight * naturalRatio;
285
+ ctx.imageButtonTop = buttonOffsetTop + 16;
286
+ ctx.imageButtonRight = (offsetWidth - referenceWidth) / 2 + buttonOffsetRight + 16;
329
287
  }
288
+ } else {
289
+ ctx.imageButtonTop = buttonOffsetTop + 16;
290
+ ctx.imageButtonRight = buttonOffsetRight + 16;
291
+ }
292
+ },
293
+ setStylesOnResize() {
294
+ const ctx = getContext();
295
+ const {
296
+ ref
297
+ } = getElement();
298
+ if (ctx.lightboxEnabled && (state.windowWidth || state.windowHeight)) {
299
+ setStyles(ctx, ref);
330
300
  }
331
301
  }
332
302
  }
333
- }, {
334
- afterLoad: ({
335
- state
336
- }) => {
337
- window.addEventListener('resize', debounce(() => {
338
- state.core.image.windowWidth = window.innerWidth;
339
- state.core.image.windowHeight = window.innerHeight;
340
- }));
341
- }
342
303
  });
304
+ window.addEventListener('resize', debounce(() => {
305
+ state.windowWidth = window.innerWidth;
306
+ state.windowHeight = window.innerHeight;
307
+ }));
343
308
 
344
- /*
309
+ /**
345
310
  * Computes styles for the lightbox and adds them to the document.
346
311
  *
347
312
  * @function
348
- * @param {Object} context - An Interactivity API context
349
- * @param {Object} event - A triggering event
313
+ * @param {Object} ctx - Context for the `core/image` namespace.
314
+ * @param {Object} ref - The element reference.
350
315
  */
351
- function setStyles(context, ref) {
316
+ function setStyles(ctx, ref) {
352
317
  // The reference img element lies adjacent
353
318
  // to the event target button in the DOM.
354
319
  let {
@@ -369,7 +334,7 @@ function setStyles(context, ref) {
369
334
 
370
335
  // If it has object-fit: contain, recalculate the original sizes
371
336
  // and the screen position without the blank spaces.
372
- if (context.core.image.scaleAttr === 'contain') {
337
+ if (ctx.scaleAttr === 'contain') {
373
338
  if (naturalRatio > originalRatio) {
374
339
  const heightWithoutSpace = originalWidth / naturalRatio;
375
340
  // Recalculate screen position without the top space.
@@ -388,8 +353,8 @@ function setStyles(context, ref) {
388
353
  // dimensions have not been set (i.e. an external image with only one size),
389
354
  // the image's dimensions in the lightbox are the same
390
355
  // as those of the image in the content.
391
- let imgMaxWidth = parseFloat(context.core.image.targetWidth !== 'none' ? context.core.image.targetWidth : naturalWidth);
392
- let imgMaxHeight = parseFloat(context.core.image.targetHeight !== 'none' ? context.core.image.targetHeight : naturalHeight);
356
+ let imgMaxWidth = parseFloat(ctx.targetWidth !== 'none' ? ctx.targetWidth : naturalWidth);
357
+ let imgMaxHeight = parseFloat(ctx.targetHeight !== 'none' ? ctx.targetHeight : naturalHeight);
393
358
 
394
359
  // Ratio of the biggest image stored in the database.
395
360
  let imgRatio = imgMaxWidth / imgMaxHeight;
@@ -500,12 +465,12 @@ function setStyles(context, ref) {
500
465
  `;
501
466
  }
502
467
 
503
- /*
468
+ /**
504
469
  * Debounces a function call.
505
470
  *
506
471
  * @function
507
472
  * @param {Function} func - A function to be called
508
- * @param {number} wait - The time to wait before calling the function
473
+ * @param {number} wait - The time to wait before calling the function
509
474
  */
510
475
  function debounce(func, wait = 50) {
511
476
  let timeout;