@wordpress/block-library 8.18.1-next.5a1d1283.0 → 8.19.1
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 -1
- package/build/block/edit.js +53 -2
- package/build/block/edit.js.map +1 -1
- package/build/button/edit.js +7 -4
- package/build/button/edit.js.map +1 -1
- package/build/button/index.js +11 -2
- package/build/button/index.js.map +1 -1
- package/build/button/save.js +10 -4
- package/build/button/save.js.map +1 -1
- package/build/buttons/edit.js +12 -3
- package/build/buttons/edit.js.map +1 -1
- package/build/code/edit.js +8 -2
- package/build/code/edit.js.map +1 -1
- package/build/code/index.js +7 -1
- package/build/code/index.js.map +1 -1
- package/build/column/index.js +1 -0
- package/build/column/index.js.map +1 -1
- package/build/columns/index.js +2 -0
- package/build/columns/index.js.map +1 -1
- package/build/file/edit.native.js +1 -1
- package/build/file/edit.native.js.map +1 -1
- package/build/file/index.js +2 -1
- package/build/file/index.js.map +1 -1
- package/build/file/utils/index.js +1 -15
- package/build/file/utils/index.js.map +1 -1
- package/build/file/view.js +14 -1
- package/build/file/view.js.map +1 -1
- package/build/footnotes/edit.js +12 -2
- package/build/footnotes/edit.js.map +1 -1
- package/build/gallery/edit.js +1 -1
- package/build/gallery/edit.js.map +1 -1
- package/build/group/edit.js +0 -1
- package/build/group/edit.js.map +1 -1
- package/build/group/index.js +4 -0
- package/build/group/index.js.map +1 -1
- package/build/heading/edit.js +0 -1
- package/build/heading/edit.js.map +1 -1
- package/build/heading/index.js +2 -0
- package/build/heading/index.js.map +1 -1
- package/build/heading/transforms.js +3 -6
- package/build/heading/transforms.js.map +1 -1
- package/build/image/deprecated.js +191 -7
- package/build/image/deprecated.js.map +1 -1
- package/build/image/edit.js +27 -12
- package/build/image/edit.js.map +1 -1
- package/build/image/image.js +75 -40
- package/build/image/image.js.map +1 -1
- package/build/image/index.js +7 -4
- package/build/image/index.js.map +1 -1
- package/build/image/{view-interactivity.js → view.js} +1 -1
- package/build/image/view.js.map +1 -0
- package/build/list/deprecated.js +93 -1
- package/build/list/deprecated.js.map +1 -1
- package/build/list/edit.js +10 -10
- package/build/list/edit.js.map +1 -1
- package/build/list/ordered-list-settings.js +10 -10
- package/build/list/ordered-list-settings.js.map +1 -1
- package/build/list/save.js +4 -2
- package/build/list/save.js.map +1 -1
- package/build/list/utils.js +21 -2
- package/build/list/utils.js.map +1 -1
- package/build/navigation/constants.js +1 -1
- package/build/navigation/constants.js.map +1 -1
- package/build/navigation/index.js +4 -2
- package/build/navigation/index.js.map +1 -1
- package/build/navigation/view.js +168 -82
- package/build/navigation/view.js.map +1 -1
- package/build/navigation-link/edit.js +3 -9
- package/build/navigation-link/edit.js.map +1 -1
- package/build/navigation-link/transforms.js +24 -0
- package/build/navigation-link/transforms.js.map +1 -1
- package/build/page-list/edit.js +2 -1
- package/build/page-list/edit.js.map +1 -1
- package/build/pattern/edit.js +2 -0
- package/build/pattern/edit.js.map +1 -1
- package/build/post-comments-form/edit.js +10 -2
- package/build/post-comments-form/edit.js.map +1 -1
- package/build/post-comments-form/form.js +5 -3
- package/build/post-comments-form/form.js.map +1 -1
- package/build/post-content/edit.js +8 -7
- package/build/post-content/edit.js.map +1 -1
- package/build/post-content/index.js +3 -0
- package/build/post-content/index.js.map +1 -1
- package/build/post-featured-image/overlay.js +2 -1
- package/build/post-featured-image/overlay.js.map +1 -1
- package/build/preformatted/edit.js +4 -1
- package/build/preformatted/edit.js.map +1 -1
- package/build/preformatted/index.js +1 -1
- package/build/preformatted/index.js.map +1 -1
- package/build/pullquote/deprecated.js +6 -7
- package/build/pullquote/deprecated.js.map +1 -1
- package/build/query/edit/enhanced-pagination-modal.js +56 -0
- package/build/query/edit/enhanced-pagination-modal.js.map +1 -0
- package/build/query/edit/inspector-controls/enhanced-pagination-control.js +42 -0
- package/build/query/edit/inspector-controls/enhanced-pagination-control.js.map +1 -0
- package/build/query/edit/inspector-controls/index.js +8 -25
- package/build/query/edit/inspector-controls/index.js.map +1 -1
- package/build/query/edit/query-content.js +8 -2
- package/build/query/edit/query-content.js.map +1 -1
- package/build/query/utils.js +19 -0
- package/build/query/utils.js.map +1 -1
- package/build/query/view.js +21 -6
- package/build/query/view.js.map +1 -1
- package/build/search/index.js +1 -0
- package/build/search/index.js.map +1 -1
- package/build/search/view.js +81 -150
- package/build/search/view.js.map +1 -1
- package/build/social-link/edit.native.js +3 -1
- package/build/social-link/edit.native.js.map +1 -1
- package/build/table/edit.js +7 -7
- package/build/table/edit.js.map +1 -1
- package/build/table-of-contents/edit.js +2 -130
- package/build/table-of-contents/edit.js.map +1 -1
- package/build/table-of-contents/hooks.js +145 -0
- package/build/table-of-contents/hooks.js.map +1 -0
- package/build/table-of-contents/index.js +2 -1
- package/build/table-of-contents/index.js.map +1 -1
- package/build/verse/edit.js +4 -1
- package/build/verse/edit.js.map +1 -1
- package/build/verse/index.js +1 -1
- package/build/verse/index.js.map +1 -1
- package/build-module/block/edit.js +54 -3
- package/build-module/block/edit.js.map +1 -1
- package/build-module/button/edit.js +7 -4
- package/build-module/button/edit.js.map +1 -1
- package/build-module/button/index.js +11 -2
- package/build-module/button/index.js.map +1 -1
- package/build-module/button/save.js +10 -4
- package/build-module/button/save.js.map +1 -1
- package/build-module/buttons/edit.js +12 -3
- package/build-module/buttons/edit.js.map +1 -1
- package/build-module/code/edit.js +8 -2
- package/build-module/code/edit.js.map +1 -1
- package/build-module/code/index.js +7 -1
- package/build-module/code/index.js.map +1 -1
- package/build-module/column/index.js +1 -0
- package/build-module/column/index.js.map +1 -1
- package/build-module/columns/index.js +2 -0
- package/build-module/columns/index.js.map +1 -1
- package/build-module/file/edit.native.js +1 -1
- package/build-module/file/edit.native.js.map +1 -1
- package/build-module/file/index.js +2 -1
- package/build-module/file/index.js.map +1 -1
- package/build-module/file/utils/index.js +0 -13
- package/build-module/file/utils/index.js.map +1 -1
- package/build-module/file/view.js +14 -2
- package/build-module/file/view.js.map +1 -1
- package/build-module/footnotes/edit.js +12 -2
- package/build-module/footnotes/edit.js.map +1 -1
- package/build-module/gallery/edit.js +1 -1
- package/build-module/gallery/edit.js.map +1 -1
- package/build-module/group/edit.js +0 -1
- package/build-module/group/edit.js.map +1 -1
- package/build-module/group/index.js +4 -0
- package/build-module/group/index.js.map +1 -1
- package/build-module/heading/edit.js +0 -1
- package/build-module/heading/edit.js.map +1 -1
- package/build-module/heading/index.js +2 -0
- package/build-module/heading/index.js.map +1 -1
- package/build-module/heading/transforms.js +3 -6
- package/build-module/heading/transforms.js.map +1 -1
- package/build-module/image/deprecated.js +191 -7
- package/build-module/image/deprecated.js.map +1 -1
- package/build-module/image/edit.js +27 -12
- package/build-module/image/edit.js.map +1 -1
- package/build-module/image/image.js +77 -42
- package/build-module/image/image.js.map +1 -1
- package/build-module/image/index.js +7 -4
- package/build-module/image/index.js.map +1 -1
- package/build-module/image/{view-interactivity.js → view.js} +1 -1
- package/build-module/image/view.js.map +1 -0
- package/build-module/list/deprecated.js +95 -3
- package/build-module/list/deprecated.js.map +1 -1
- package/build-module/list/edit.js +10 -10
- package/build-module/list/edit.js.map +1 -1
- package/build-module/list/ordered-list-settings.js +10 -10
- package/build-module/list/ordered-list-settings.js.map +1 -1
- package/build-module/list/save.js +4 -2
- package/build-module/list/save.js.map +1 -1
- package/build-module/list/utils.js +20 -2
- package/build-module/list/utils.js.map +1 -1
- package/build-module/navigation/constants.js +1 -1
- package/build-module/navigation/constants.js.map +1 -1
- package/build-module/navigation/index.js +4 -2
- package/build-module/navigation/index.js.map +1 -1
- package/build-module/navigation/view.js +168 -83
- package/build-module/navigation/view.js.map +1 -1
- package/build-module/navigation-link/edit.js +3 -9
- package/build-module/navigation-link/edit.js.map +1 -1
- package/build-module/navigation-link/transforms.js +24 -0
- package/build-module/navigation-link/transforms.js.map +1 -1
- package/build-module/page-list/edit.js +2 -1
- package/build-module/page-list/edit.js.map +1 -1
- package/build-module/pattern/edit.js +2 -0
- package/build-module/pattern/edit.js.map +1 -1
- package/build-module/post-comments-form/edit.js +10 -2
- package/build-module/post-comments-form/edit.js.map +1 -1
- package/build-module/post-comments-form/form.js +5 -3
- package/build-module/post-comments-form/form.js.map +1 -1
- package/build-module/post-content/edit.js +8 -7
- package/build-module/post-content/edit.js.map +1 -1
- package/build-module/post-content/index.js +3 -0
- package/build-module/post-content/index.js.map +1 -1
- package/build-module/post-featured-image/overlay.js +2 -1
- package/build-module/post-featured-image/overlay.js.map +1 -1
- package/build-module/preformatted/edit.js +4 -1
- package/build-module/preformatted/edit.js.map +1 -1
- package/build-module/preformatted/index.js +1 -1
- package/build-module/preformatted/index.js.map +1 -1
- package/build-module/pullquote/deprecated.js +6 -7
- package/build-module/pullquote/deprecated.js.map +1 -1
- package/build-module/query/edit/enhanced-pagination-modal.js +50 -0
- package/build-module/query/edit/enhanced-pagination-modal.js.map +1 -0
- package/build-module/query/edit/inspector-controls/enhanced-pagination-control.js +35 -0
- package/build-module/query/edit/inspector-controls/enhanced-pagination-control.js.map +1 -0
- package/build-module/query/edit/inspector-controls/index.js +9 -26
- package/build-module/query/edit/inspector-controls/index.js.map +1 -1
- package/build-module/query/edit/query-content.js +8 -2
- package/build-module/query/edit/query-content.js.map +1 -1
- package/build-module/query/utils.js +17 -0
- package/build-module/query/utils.js.map +1 -1
- package/build-module/query/view.js +21 -6
- package/build-module/query/view.js.map +1 -1
- package/build-module/search/index.js +1 -0
- package/build-module/search/index.js.map +1 -1
- package/build-module/search/view.js +81 -151
- package/build-module/search/view.js.map +1 -1
- package/build-module/social-link/edit.native.js +3 -1
- package/build-module/social-link/edit.native.js.map +1 -1
- package/build-module/table/edit.js +7 -7
- package/build-module/table/edit.js.map +1 -1
- package/build-module/table-of-contents/edit.js +3 -131
- package/build-module/table-of-contents/edit.js.map +1 -1
- package/build-module/table-of-contents/hooks.js +137 -0
- package/build-module/table-of-contents/hooks.js.map +1 -0
- package/build-module/table-of-contents/index.js +2 -1
- package/build-module/table-of-contents/index.js.map +1 -1
- package/build-module/verse/edit.js +4 -1
- package/build-module/verse/edit.js.map +1 -1
- package/build-module/verse/index.js +1 -1
- package/build-module/verse/index.js.map +1 -1
- package/build-style/cover/style-rtl.css +9 -0
- package/build-style/cover/style.css +9 -0
- package/build-style/editor-rtl.css +15 -9
- package/build-style/editor.css +15 -9
- package/build-style/file/style-rtl.css +5 -0
- package/build-style/file/style.css +5 -0
- package/build-style/heading/style-rtl.css +13 -0
- package/build-style/heading/style.css +13 -0
- package/build-style/navigation-link/editor-rtl.css +0 -9
- package/build-style/navigation-link/editor.css +0 -9
- package/build-style/paragraph/editor-rtl.css +5 -0
- package/build-style/paragraph/editor.css +5 -0
- package/build-style/paragraph/style-rtl.css +5 -0
- package/build-style/paragraph/style.css +5 -0
- package/build-style/post-navigation-link/style-rtl.css +3 -0
- package/build-style/post-navigation-link/style.css +3 -0
- package/build-style/query/editor-rtl.css +10 -0
- package/build-style/query/editor.css +10 -0
- package/build-style/query/style-rtl.css +1 -1
- package/build-style/query/style.css +1 -1
- package/build-style/reset-rtl.css +27 -27
- package/build-style/reset.css +27 -27
- package/build-style/style-rtl.css +35 -0
- package/build-style/style.css +35 -0
- package/package.json +33 -34
- package/src/audio/test/__snapshots__/edit.native.js.snap +78 -106
- package/src/avatar/index.php +2 -2
- package/src/block/edit.js +58 -2
- package/src/block/test/edit.native.js +2 -3
- package/src/button/block.json +11 -2
- package/src/button/edit.js +27 -11
- package/src/button/save.js +10 -4
- package/src/buttons/edit.js +13 -3
- package/src/buttons/test/edit.native.js +28 -24
- package/src/calendar/index.php +2 -2
- package/src/code/block.json +2 -1
- package/src/code/edit.js +13 -1
- package/src/code/index.js +5 -0
- package/src/column/block.json +1 -0
- package/src/columns/block.json +2 -0
- package/src/comment-template/index.php +1 -1
- package/src/comments-pagination-next/index.php +1 -1
- package/src/comments-pagination-previous/index.php +1 -1
- package/src/cover/style.scss +12 -1
- package/src/cover/test/edit.js +10 -10
- package/src/cover/test/edit.native.js +18 -27
- package/src/embed/test/index.native.js +30 -45
- package/src/file/block.json +2 -1
- package/src/file/edit.native.js +1 -1
- package/src/file/index.php +21 -21
- package/src/file/style.scss +6 -1
- package/src/file/test/__snapshots__/edit.native.js.snap +162 -218
- package/src/file/utils/index.js +0 -15
- package/src/file/view.js +14 -5
- package/src/footnotes/edit.js +13 -1
- package/src/gallery/edit.js +1 -1
- package/src/gallery/index.php +1 -1
- package/src/group/block.json +4 -0
- package/src/group/edit.js +0 -1
- package/src/heading/block.json +2 -0
- package/src/heading/edit.js +0 -1
- package/src/heading/style.scss +4 -0
- package/src/heading/transforms.js +1 -6
- package/src/image/block.json +7 -4
- package/src/image/deprecated.js +210 -7
- package/src/image/edit.js +43 -29
- package/src/image/image.js +86 -43
- package/src/image/index.php +253 -28
- package/src/latest-posts/index.php +1 -1
- package/src/list/deprecated.js +84 -3
- package/src/list/edit.js +6 -3
- package/src/list/ordered-list-settings.js +20 -5
- package/src/list/save.js +10 -1
- package/src/list/test/edit.native.js +2 -4
- package/src/list/utils.js +22 -1
- package/src/navigation/block.json +4 -2
- package/src/navigation/constants.js +1 -0
- package/src/navigation/index.php +87 -123
- package/src/navigation/view.js +192 -96
- package/src/navigation-link/edit.js +21 -36
- package/src/navigation-link/editor.scss +0 -8
- package/src/navigation-link/transforms.js +22 -0
- package/src/page-list/edit.js +2 -1
- package/src/paragraph/editor.scss +5 -0
- package/src/paragraph/style.scss +5 -0
- package/src/pattern/edit.js +1 -0
- package/src/pattern/index.php +5 -5
- package/src/post-comments-form/edit.js +10 -0
- package/src/post-comments-form/form.js +7 -1
- package/src/post-content/block.json +3 -0
- package/src/post-content/edit.js +14 -6
- package/src/post-excerpt/index.php +2 -2
- package/src/post-featured-image/index.php +2 -2
- package/src/post-featured-image/overlay.js +1 -0
- package/src/post-navigation-link/index.php +0 -5
- package/src/post-navigation-link/style.scss +4 -0
- package/src/post-template/index.php +1 -1
- package/src/post-terms/index.php +5 -2
- package/src/preformatted/edit.js +5 -0
- package/src/preformatted/index.js +1 -1
- package/src/pullquote/deprecated.js +8 -13
- package/src/query/edit/enhanced-pagination-modal.js +65 -0
- package/src/query/edit/inspector-controls/enhanced-pagination-control.js +49 -0
- package/src/query/edit/inspector-controls/index.js +9 -45
- package/src/query/edit/query-content.js +8 -0
- package/src/query/editor.scss +10 -0
- package/src/query/index.php +31 -16
- package/src/query/style.scss +2 -2
- package/src/query/utils.js +22 -0
- package/src/query/view.js +19 -10
- package/src/query-pagination-next/index.php +2 -1
- package/src/query-pagination-previous/index.php +2 -1
- package/src/reset.scss +1 -1
- package/src/search/block.json +1 -0
- package/src/search/index.php +47 -4
- package/src/search/test/__snapshots__/edit.native.js.snap +265 -363
- package/src/search/view.js +70 -169
- package/src/social-link/edit.native.js +1 -1
- package/src/social-link/editor.native.scss +4 -0
- package/src/table/edit.js +21 -19
- package/src/table-of-contents/block.json +2 -1
- package/src/table-of-contents/edit.js +5 -162
- package/src/table-of-contents/hooks.js +156 -0
- package/src/verse/edit.js +5 -0
- package/src/verse/index.js +1 -1
- package/src/verse/test/edit.native.js +2 -3
- package/build/file/view-interactivity.js +0 -22
- package/build/file/view-interactivity.js.map +0 -1
- package/build/image/view-interactivity.js.map +0 -1
- package/build/navigation/view-interactivity.js +0 -172
- package/build/navigation/view-interactivity.js.map +0 -1
- package/build/navigation/view-modal.js +0 -115
- package/build/navigation/view-modal.js.map +0 -1
- package/build-module/file/view-interactivity.js +0 -18
- package/build-module/file/view-interactivity.js.map +0 -1
- package/build-module/image/view-interactivity.js.map +0 -1
- package/build-module/navigation/view-interactivity.js +0 -169
- package/build-module/navigation/view-interactivity.js.map +0 -1
- package/build-module/navigation/view-modal.js +0 -112
- package/build-module/navigation/view-modal.js.map +0 -1
- package/src/file/view-interactivity.js +0 -18
- package/src/navigation/view-interactivity.js +0 -196
- package/src/navigation/view-modal.js +0 -127
- /package/src/image/{view-interactivity.js → view.js} +0 -0
package/src/search/view.js
CHANGED
|
@@ -1,172 +1,73 @@
|
|
|
1
|
-
/*eslint-env browser*/
|
|
2
|
-
|
|
3
|
-
/** @type {?HTMLFormElement} */
|
|
4
|
-
let expandedSearchBlock = null;
|
|
5
|
-
|
|
6
|
-
const hiddenClass = 'wp-block-search__searchfield-hidden';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Toggles aria-label with data-toggled-aria-label.
|
|
10
|
-
*
|
|
11
|
-
* @param {HTMLElement} element
|
|
12
|
-
*/
|
|
13
|
-
function toggleAriaLabel( element ) {
|
|
14
|
-
if ( ! ( 'toggledAriaLabel' in element.dataset ) ) {
|
|
15
|
-
throw new Error( 'Element lacks toggledAriaLabel in dataset.' );
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const ariaLabel = element.dataset.toggledAriaLabel;
|
|
19
|
-
element.dataset.toggledAriaLabel = element.ariaLabel;
|
|
20
|
-
element.ariaLabel = ariaLabel;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Gets search input.
|
|
25
|
-
*
|
|
26
|
-
* @param {HTMLFormElement} block Search block.
|
|
27
|
-
* @return {HTMLInputElement} Search input.
|
|
28
|
-
*/
|
|
29
|
-
function getSearchInput( block ) {
|
|
30
|
-
return block.querySelector( '.wp-block-search__input' );
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Gets search button.
|
|
35
|
-
*
|
|
36
|
-
* @param {HTMLFormElement} block Search block.
|
|
37
|
-
* @return {HTMLButtonElement} Search button.
|
|
38
|
-
*/
|
|
39
|
-
function getSearchButton( block ) {
|
|
40
|
-
return block.querySelector( '.wp-block-search__button' );
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Handles keydown event to collapse an expanded Search block (when pressing Escape key).
|
|
45
|
-
*
|
|
46
|
-
* @param {KeyboardEvent} event
|
|
47
|
-
*/
|
|
48
|
-
function handleKeydownEvent( event ) {
|
|
49
|
-
if ( ! expandedSearchBlock ) {
|
|
50
|
-
// In case the event listener wasn't removed in time.
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if ( event.key === 'Escape' ) {
|
|
55
|
-
const block = expandedSearchBlock; // This is nullified by collapseExpandedSearchBlock().
|
|
56
|
-
collapseExpandedSearchBlock();
|
|
57
|
-
getSearchButton( block ).focus();
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Handles keyup event to collapse an expanded Search block (e.g. when tabbing out of expanded Search block).
|
|
63
|
-
*
|
|
64
|
-
* @param {KeyboardEvent} event
|
|
65
|
-
*/
|
|
66
|
-
function handleKeyupEvent( event ) {
|
|
67
|
-
if ( ! expandedSearchBlock ) {
|
|
68
|
-
// In case the event listener wasn't removed in time.
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if ( event.target.closest( '.wp-block-search' ) !== expandedSearchBlock ) {
|
|
73
|
-
collapseExpandedSearchBlock();
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Expands search block.
|
|
79
|
-
*
|
|
80
|
-
* Inverse of what is done in collapseExpandedSearchBlock().
|
|
81
|
-
*
|
|
82
|
-
* @param {HTMLFormElement} block Search block.
|
|
83
|
-
*/
|
|
84
|
-
function expandSearchBlock( block ) {
|
|
85
|
-
// Make sure only one is open at a time.
|
|
86
|
-
if ( expandedSearchBlock ) {
|
|
87
|
-
collapseExpandedSearchBlock();
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const searchField = getSearchInput( block );
|
|
91
|
-
const searchButton = getSearchButton( block );
|
|
92
|
-
|
|
93
|
-
searchButton.type = 'submit';
|
|
94
|
-
searchField.ariaHidden = 'false';
|
|
95
|
-
searchField.tabIndex = 0;
|
|
96
|
-
searchButton.ariaExpanded = 'true';
|
|
97
|
-
searchButton.removeAttribute( 'aria-controls' ); // Note: Seemingly not reflected with searchButton.ariaControls.
|
|
98
|
-
toggleAriaLabel( searchButton );
|
|
99
|
-
block.classList.remove( hiddenClass );
|
|
100
|
-
|
|
101
|
-
searchField.focus(); // Note that Chrome seems to do this automatically.
|
|
102
|
-
|
|
103
|
-
// The following two must be inverse of what is done in collapseExpandedSearchBlock().
|
|
104
|
-
document.addEventListener( 'keydown', handleKeydownEvent, {
|
|
105
|
-
passive: true,
|
|
106
|
-
} );
|
|
107
|
-
document.addEventListener( 'keyup', handleKeyupEvent, {
|
|
108
|
-
passive: true,
|
|
109
|
-
} );
|
|
110
|
-
|
|
111
|
-
expandedSearchBlock = block;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
1
|
/**
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
* Inverse of what is done in expandSearchBlock().
|
|
2
|
+
* WordPress dependencies
|
|
118
3
|
*/
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
4
|
+
import { store as wpStore } from '@wordpress/interactivity';
|
|
5
|
+
|
|
6
|
+
wpStore( {
|
|
7
|
+
selectors: {
|
|
8
|
+
core: {
|
|
9
|
+
search: {
|
|
10
|
+
ariaLabel: ( { context } ) => {
|
|
11
|
+
const { ariaLabelCollapsed, ariaLabelExpanded } =
|
|
12
|
+
context.core.search;
|
|
13
|
+
return context.core.search.isSearchInputVisible
|
|
14
|
+
? ariaLabelExpanded
|
|
15
|
+
: ariaLabelCollapsed;
|
|
16
|
+
},
|
|
17
|
+
ariaControls: ( { context } ) => {
|
|
18
|
+
return context.core.search.isSearchInputVisible
|
|
19
|
+
? null
|
|
20
|
+
: context.core.search.inputId;
|
|
21
|
+
},
|
|
22
|
+
type: ( { context } ) => {
|
|
23
|
+
return context.core.search.isSearchInputVisible
|
|
24
|
+
? 'submit'
|
|
25
|
+
: 'button';
|
|
26
|
+
},
|
|
27
|
+
tabindex: ( { context } ) => {
|
|
28
|
+
return context.core.search.isSearchInputVisible
|
|
29
|
+
? '0'
|
|
30
|
+
: '-1';
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
actions: {
|
|
36
|
+
core: {
|
|
37
|
+
search: {
|
|
38
|
+
openSearchInput: ( { context, event, ref } ) => {
|
|
39
|
+
if ( ! context.core.search.isSearchInputVisible ) {
|
|
40
|
+
event.preventDefault();
|
|
41
|
+
context.core.search.isSearchInputVisible = true;
|
|
42
|
+
ref.parentElement.querySelector( 'input' ).focus();
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
closeSearchInput: ( { context } ) => {
|
|
46
|
+
context.core.search.isSearchInputVisible = false;
|
|
47
|
+
},
|
|
48
|
+
handleSearchKeydown: ( store ) => {
|
|
49
|
+
const { actions, event, ref } = store;
|
|
50
|
+
// If Escape close the menu.
|
|
51
|
+
if ( event?.key === 'Escape' ) {
|
|
52
|
+
actions.core.search.closeSearchInput( store );
|
|
53
|
+
ref.querySelector( 'button' ).focus();
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
handleSearchFocusout: ( store ) => {
|
|
57
|
+
const { actions, event, ref } = store;
|
|
58
|
+
// If focus is outside search form, and in the document, close menu
|
|
59
|
+
// event.target === The element losing focus
|
|
60
|
+
// event.relatedTarget === The element receiving focus (if any)
|
|
61
|
+
// When focusout is outside the document,
|
|
62
|
+
// `window.document.activeElement` doesn't change.
|
|
63
|
+
if (
|
|
64
|
+
! ref.contains( event.relatedTarget ) &&
|
|
65
|
+
event.target !== window.document.activeElement
|
|
66
|
+
) {
|
|
67
|
+
actions.core.search.closeSearchInput( store );
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
},
|
|
170
72
|
},
|
|
171
|
-
|
|
172
|
-
);
|
|
73
|
+
} );
|
package/src/table/edit.js
CHANGED
|
@@ -349,7 +349,7 @@ function TableEdit( {
|
|
|
349
349
|
useEffect( () => {
|
|
350
350
|
if ( hasTableCreated ) {
|
|
351
351
|
tableRef?.current
|
|
352
|
-
?.querySelector( 'td[contentEditable="true"]' )
|
|
352
|
+
?.querySelector( 'td div[contentEditable="true"]' )
|
|
353
353
|
?.focus();
|
|
354
354
|
setHasTableCreated( false );
|
|
355
355
|
}
|
|
@@ -414,31 +414,33 @@ function TableEdit( {
|
|
|
414
414
|
},
|
|
415
415
|
columnIndex
|
|
416
416
|
) => (
|
|
417
|
-
<
|
|
418
|
-
tagName={ CellTag }
|
|
417
|
+
<CellTag
|
|
419
418
|
key={ columnIndex }
|
|
419
|
+
scope={ CellTag === 'th' ? scope : undefined }
|
|
420
|
+
colSpan={ colspan }
|
|
421
|
+
rowSpan={ rowspan }
|
|
420
422
|
className={ classnames(
|
|
421
423
|
{
|
|
422
424
|
[ `has-text-align-${ align }` ]: align,
|
|
423
425
|
},
|
|
424
426
|
'wp-block-table__cell-content'
|
|
425
427
|
) }
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
428
|
+
>
|
|
429
|
+
<RichText
|
|
430
|
+
value={ content }
|
|
431
|
+
onChange={ onChange }
|
|
432
|
+
onFocus={ () => {
|
|
433
|
+
setSelectedCell( {
|
|
434
|
+
sectionName: name,
|
|
435
|
+
rowIndex,
|
|
436
|
+
columnIndex,
|
|
437
|
+
type: 'cell',
|
|
438
|
+
} );
|
|
439
|
+
} }
|
|
440
|
+
aria-label={ cellAriaLabel[ name ] }
|
|
441
|
+
placeholder={ placeholder[ name ] }
|
|
442
|
+
/>
|
|
443
|
+
</CellTag>
|
|
442
444
|
)
|
|
443
445
|
) }
|
|
444
446
|
</tr>
|
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import fastDeepEqual from 'fast-deep-equal/es6';
|
|
5
|
-
|
|
6
1
|
/**
|
|
7
2
|
* WordPress dependencies
|
|
8
3
|
*/
|
|
@@ -22,10 +17,8 @@ import {
|
|
|
22
17
|
ToolbarGroup,
|
|
23
18
|
} from '@wordpress/components';
|
|
24
19
|
import { useDispatch, useSelect } from '@wordpress/data';
|
|
25
|
-
import {
|
|
26
|
-
import { renderToString, useEffect } from '@wordpress/element';
|
|
20
|
+
import { renderToString } from '@wordpress/element';
|
|
27
21
|
import { __ } from '@wordpress/i18n';
|
|
28
|
-
import { addQueryArgs, removeQueryArgs } from '@wordpress/url';
|
|
29
22
|
|
|
30
23
|
/**
|
|
31
24
|
* Internal dependencies
|
|
@@ -33,6 +26,7 @@ import { addQueryArgs, removeQueryArgs } from '@wordpress/url';
|
|
|
33
26
|
import icon from './icon';
|
|
34
27
|
import TableOfContentsList from './list';
|
|
35
28
|
import { linearToNestedHeadingList } from './utils';
|
|
29
|
+
import { useObserveHeadings } from './hooks';
|
|
36
30
|
|
|
37
31
|
/** @typedef {import('./utils').HeadingData} HeadingData */
|
|
38
32
|
|
|
@@ -53,6 +47,8 @@ export default function TableOfContentsEdit( {
|
|
|
53
47
|
clientId,
|
|
54
48
|
setAttributes,
|
|
55
49
|
} ) {
|
|
50
|
+
useObserveHeadings( clientId );
|
|
51
|
+
|
|
56
52
|
const blockProps = useBlockProps();
|
|
57
53
|
|
|
58
54
|
const canInsertList = useSelect(
|
|
@@ -66,160 +62,7 @@ export default function TableOfContentsEdit( {
|
|
|
66
62
|
[ clientId ]
|
|
67
63
|
);
|
|
68
64
|
|
|
69
|
-
const {
|
|
70
|
-
useDispatch( blockEditorStore );
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* The latest heading data, or null if the new data deeply equals the saved
|
|
74
|
-
* headings attribute.
|
|
75
|
-
*
|
|
76
|
-
* Since useSelect forces a re-render when its return value is shallowly
|
|
77
|
-
* inequal to its prior call, we would be re-rendering this block every time
|
|
78
|
-
* the stores change, even if the latest headings were deeply equal to the
|
|
79
|
-
* ones saved in the block attributes.
|
|
80
|
-
*
|
|
81
|
-
* By returning null when they're equal, we reduce that to 2 renders: one
|
|
82
|
-
* when there are new latest headings (and so it returns them), and one when
|
|
83
|
-
* they haven't changed (so it returns null). As long as the latest heading
|
|
84
|
-
* data remains the same, further calls of the useSelect callback will
|
|
85
|
-
* continue to return null, thus preventing any forced re-renders.
|
|
86
|
-
*/
|
|
87
|
-
const latestHeadings = useSelect(
|
|
88
|
-
( select ) => {
|
|
89
|
-
const {
|
|
90
|
-
getBlockAttributes,
|
|
91
|
-
getBlockName,
|
|
92
|
-
getClientIdsWithDescendants,
|
|
93
|
-
__experimentalGetGlobalBlocksByName: getGlobalBlocksByName,
|
|
94
|
-
} = select( blockEditorStore );
|
|
95
|
-
|
|
96
|
-
// FIXME: @wordpress/block-library should not depend on @wordpress/editor.
|
|
97
|
-
// Blocks can be loaded into a *non-post* block editor, so to avoid
|
|
98
|
-
// declaring @wordpress/editor as a dependency, we must access its
|
|
99
|
-
// store by string. When the store is not available, editorSelectors
|
|
100
|
-
// will be null, and the block's saved markup will lack permalinks.
|
|
101
|
-
// eslint-disable-next-line @wordpress/data-no-store-string-literals
|
|
102
|
-
const editorSelectors = select( 'core/editor' );
|
|
103
|
-
|
|
104
|
-
const pageBreakClientIds = getGlobalBlocksByName( 'core/nextpage' );
|
|
105
|
-
|
|
106
|
-
const isPaginated = pageBreakClientIds.length !== 0;
|
|
107
|
-
|
|
108
|
-
// Get the client ids of all blocks in the editor.
|
|
109
|
-
const allBlockClientIds = getClientIdsWithDescendants();
|
|
110
|
-
|
|
111
|
-
// If onlyIncludeCurrentPage is true, calculate the page (of a paginated post) this block is part of, so we know which headings to include; otherwise, skip the calculation.
|
|
112
|
-
let tocPage = 1;
|
|
113
|
-
|
|
114
|
-
if ( isPaginated && onlyIncludeCurrentPage ) {
|
|
115
|
-
// We can't use getBlockIndex because it only returns the index
|
|
116
|
-
// relative to sibling blocks.
|
|
117
|
-
const tocIndex = allBlockClientIds.indexOf( clientId );
|
|
118
|
-
|
|
119
|
-
for ( const [
|
|
120
|
-
blockIndex,
|
|
121
|
-
blockClientId,
|
|
122
|
-
] of allBlockClientIds.entries() ) {
|
|
123
|
-
// If we've reached blocks after the Table of Contents, we've
|
|
124
|
-
// finished calculating which page the block is on.
|
|
125
|
-
if ( blockIndex >= tocIndex ) {
|
|
126
|
-
break;
|
|
127
|
-
}
|
|
128
|
-
if ( getBlockName( blockClientId ) === 'core/nextpage' ) {
|
|
129
|
-
tocPage++;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const _latestHeadings = [];
|
|
135
|
-
|
|
136
|
-
/** The page (of a paginated post) a heading will be part of. */
|
|
137
|
-
let headingPage = 1;
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* A permalink to the current post. If the core/editor store is
|
|
141
|
-
* unavailable, this variable will be null.
|
|
142
|
-
*/
|
|
143
|
-
const permalink = editorSelectors?.getPermalink() ?? null;
|
|
144
|
-
|
|
145
|
-
let headingPageLink = null;
|
|
146
|
-
|
|
147
|
-
// If the core/editor store is available, we can add permalinks to the
|
|
148
|
-
// generated table of contents.
|
|
149
|
-
if ( typeof permalink === 'string' ) {
|
|
150
|
-
headingPageLink = isPaginated
|
|
151
|
-
? addQueryArgs( permalink, { page: headingPage } )
|
|
152
|
-
: permalink;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
for ( const blockClientId of allBlockClientIds ) {
|
|
156
|
-
const blockName = getBlockName( blockClientId );
|
|
157
|
-
if ( blockName === 'core/nextpage' ) {
|
|
158
|
-
headingPage++;
|
|
159
|
-
|
|
160
|
-
// If we're only including headings from the current page (of
|
|
161
|
-
// a paginated post), then exit the loop if we've reached the
|
|
162
|
-
// pages after the one with the Table of Contents block.
|
|
163
|
-
if ( onlyIncludeCurrentPage && headingPage > tocPage ) {
|
|
164
|
-
break;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if ( typeof permalink === 'string' ) {
|
|
168
|
-
headingPageLink = addQueryArgs(
|
|
169
|
-
removeQueryArgs( permalink, [ 'page' ] ),
|
|
170
|
-
{ page: headingPage }
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
// If we're including all headings or we've reached headings on
|
|
175
|
-
// the same page as the Table of Contents block, add them to the
|
|
176
|
-
// list.
|
|
177
|
-
else if (
|
|
178
|
-
! onlyIncludeCurrentPage ||
|
|
179
|
-
headingPage === tocPage
|
|
180
|
-
) {
|
|
181
|
-
if ( blockName === 'core/heading' ) {
|
|
182
|
-
const headingAttributes =
|
|
183
|
-
getBlockAttributes( blockClientId );
|
|
184
|
-
|
|
185
|
-
const canBeLinked =
|
|
186
|
-
typeof headingPageLink === 'string' &&
|
|
187
|
-
typeof headingAttributes.anchor === 'string' &&
|
|
188
|
-
headingAttributes.anchor !== '';
|
|
189
|
-
|
|
190
|
-
_latestHeadings.push( {
|
|
191
|
-
// Convert line breaks to spaces, and get rid of HTML tags in the headings.
|
|
192
|
-
content: stripHTML(
|
|
193
|
-
headingAttributes.content.replace(
|
|
194
|
-
/(<br *\/?>)+/g,
|
|
195
|
-
' '
|
|
196
|
-
)
|
|
197
|
-
),
|
|
198
|
-
level: headingAttributes.level,
|
|
199
|
-
link: canBeLinked
|
|
200
|
-
? `${ headingPageLink }#${ headingAttributes.anchor }`
|
|
201
|
-
: null,
|
|
202
|
-
} );
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
if ( fastDeepEqual( headings, _latestHeadings ) ) {
|
|
208
|
-
return null;
|
|
209
|
-
}
|
|
210
|
-
return _latestHeadings;
|
|
211
|
-
},
|
|
212
|
-
[ clientId, onlyIncludeCurrentPage, headings ]
|
|
213
|
-
);
|
|
214
|
-
|
|
215
|
-
useEffect( () => {
|
|
216
|
-
if ( latestHeadings !== null ) {
|
|
217
|
-
// This is required to keep undo working and not create 2 undo steps
|
|
218
|
-
// for each heading change.
|
|
219
|
-
__unstableMarkNextChangeAsNotPersistent();
|
|
220
|
-
setAttributes( { headings: latestHeadings } );
|
|
221
|
-
}
|
|
222
|
-
}, [ latestHeadings ] );
|
|
65
|
+
const { replaceBlocks } = useDispatch( blockEditorStore );
|
|
223
66
|
|
|
224
67
|
const headingTree = linearToNestedHeadingList( headings );
|
|
225
68
|
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import fastDeepEqual from 'fast-deep-equal/es6';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* WordPress dependencies
|
|
8
|
+
*/
|
|
9
|
+
import { useRegistry } from '@wordpress/data';
|
|
10
|
+
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
|
|
11
|
+
import { useEffect } from '@wordpress/element';
|
|
12
|
+
import { addQueryArgs, removeQueryArgs } from '@wordpress/url';
|
|
13
|
+
import { store as blockEditorStore } from '@wordpress/block-editor';
|
|
14
|
+
|
|
15
|
+
function getLatestHeadings( select, clientId ) {
|
|
16
|
+
const {
|
|
17
|
+
getBlockAttributes,
|
|
18
|
+
getBlockName,
|
|
19
|
+
getClientIdsWithDescendants,
|
|
20
|
+
__experimentalGetGlobalBlocksByName: getGlobalBlocksByName,
|
|
21
|
+
} = select( blockEditorStore );
|
|
22
|
+
|
|
23
|
+
// FIXME: @wordpress/block-library should not depend on @wordpress/editor.
|
|
24
|
+
// Blocks can be loaded into a *non-post* block editor, so to avoid
|
|
25
|
+
// declaring @wordpress/editor as a dependency, we must access its
|
|
26
|
+
// store by string. When the store is not available, editorSelectors
|
|
27
|
+
// will be null, and the block's saved markup will lack permalinks.
|
|
28
|
+
// eslint-disable-next-line @wordpress/data-no-store-string-literals
|
|
29
|
+
const permalink = select( 'core/editor' ).getPermalink() ?? null;
|
|
30
|
+
|
|
31
|
+
const isPaginated = getGlobalBlocksByName( 'core/nextpage' ).length !== 0;
|
|
32
|
+
const { onlyIncludeCurrentPage } = getBlockAttributes( clientId ) ?? {};
|
|
33
|
+
|
|
34
|
+
// Get the client ids of all blocks in the editor.
|
|
35
|
+
const allBlockClientIds = getClientIdsWithDescendants();
|
|
36
|
+
|
|
37
|
+
// If onlyIncludeCurrentPage is true, calculate the page (of a paginated post) this block is part of, so we know which headings to include; otherwise, skip the calculation.
|
|
38
|
+
let tocPage = 1;
|
|
39
|
+
|
|
40
|
+
if ( isPaginated && onlyIncludeCurrentPage ) {
|
|
41
|
+
// We can't use getBlockIndex because it only returns the index
|
|
42
|
+
// relative to sibling blocks.
|
|
43
|
+
const tocIndex = allBlockClientIds.indexOf( clientId );
|
|
44
|
+
|
|
45
|
+
for ( const [
|
|
46
|
+
blockIndex,
|
|
47
|
+
blockClientId,
|
|
48
|
+
] of allBlockClientIds.entries() ) {
|
|
49
|
+
// If we've reached blocks after the Table of Contents, we've
|
|
50
|
+
// finished calculating which page the block is on.
|
|
51
|
+
if ( blockIndex >= tocIndex ) {
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
if ( getBlockName( blockClientId ) === 'core/nextpage' ) {
|
|
55
|
+
tocPage++;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const latestHeadings = [];
|
|
61
|
+
|
|
62
|
+
/** The page (of a paginated post) a heading will be part of. */
|
|
63
|
+
let headingPage = 1;
|
|
64
|
+
let headingPageLink = null;
|
|
65
|
+
|
|
66
|
+
// If the core/editor store is available, we can add permalinks to the
|
|
67
|
+
// generated table of contents.
|
|
68
|
+
if ( typeof permalink === 'string' ) {
|
|
69
|
+
headingPageLink = isPaginated
|
|
70
|
+
? addQueryArgs( permalink, { page: headingPage } )
|
|
71
|
+
: permalink;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
for ( const blockClientId of allBlockClientIds ) {
|
|
75
|
+
const blockName = getBlockName( blockClientId );
|
|
76
|
+
if ( blockName === 'core/nextpage' ) {
|
|
77
|
+
headingPage++;
|
|
78
|
+
|
|
79
|
+
// If we're only including headings from the current page (of
|
|
80
|
+
// a paginated post), then exit the loop if we've reached the
|
|
81
|
+
// pages after the one with the Table of Contents block.
|
|
82
|
+
if ( onlyIncludeCurrentPage && headingPage > tocPage ) {
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if ( typeof permalink === 'string' ) {
|
|
87
|
+
headingPageLink = addQueryArgs(
|
|
88
|
+
removeQueryArgs( permalink, [ 'page' ] ),
|
|
89
|
+
{ page: headingPage }
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// If we're including all headings or we've reached headings on
|
|
94
|
+
// the same page as the Table of Contents block, add them to the
|
|
95
|
+
// list.
|
|
96
|
+
else if ( ! onlyIncludeCurrentPage || headingPage === tocPage ) {
|
|
97
|
+
if ( blockName === 'core/heading' ) {
|
|
98
|
+
const headingAttributes = getBlockAttributes( blockClientId );
|
|
99
|
+
|
|
100
|
+
const canBeLinked =
|
|
101
|
+
typeof headingPageLink === 'string' &&
|
|
102
|
+
typeof headingAttributes.anchor === 'string' &&
|
|
103
|
+
headingAttributes.anchor !== '';
|
|
104
|
+
|
|
105
|
+
latestHeadings.push( {
|
|
106
|
+
// Convert line breaks to spaces, and get rid of HTML tags in the headings.
|
|
107
|
+
content: stripHTML(
|
|
108
|
+
headingAttributes.content.replace(
|
|
109
|
+
/(<br *\/?>)+/g,
|
|
110
|
+
' '
|
|
111
|
+
)
|
|
112
|
+
),
|
|
113
|
+
level: headingAttributes.level,
|
|
114
|
+
link: canBeLinked
|
|
115
|
+
? `${ headingPageLink }#${ headingAttributes.anchor }`
|
|
116
|
+
: null,
|
|
117
|
+
} );
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return latestHeadings;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function observeCallback( select, dispatch, clientId ) {
|
|
126
|
+
const { getBlockAttributes } = select( blockEditorStore );
|
|
127
|
+
const { updateBlockAttributes, __unstableMarkNextChangeAsNotPersistent } =
|
|
128
|
+
dispatch( blockEditorStore );
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* If the block no longer exists in the store, skip the update.
|
|
132
|
+
* The "undo" action recreates the block and provides a new `clientId`.
|
|
133
|
+
* The hook still might be observing the changes while the old block unmounts.
|
|
134
|
+
*/
|
|
135
|
+
const attributes = getBlockAttributes( clientId );
|
|
136
|
+
if ( attributes === null ) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const headings = getLatestHeadings( select, clientId );
|
|
141
|
+
if ( ! fastDeepEqual( headings, attributes.headings ) ) {
|
|
142
|
+
__unstableMarkNextChangeAsNotPersistent();
|
|
143
|
+
updateBlockAttributes( clientId, { headings } );
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function useObserveHeadings( clientId ) {
|
|
148
|
+
const registry = useRegistry();
|
|
149
|
+
useEffect( () => {
|
|
150
|
+
// Todo: Limit subscription to block editor store when data no longer depends on `getPermalink`.
|
|
151
|
+
// See: https://github.com/WordPress/gutenberg/pull/45513
|
|
152
|
+
return registry.subscribe( () =>
|
|
153
|
+
observeCallback( registry.select, registry.dispatch, clientId )
|
|
154
|
+
);
|
|
155
|
+
}, [ registry, clientId ] );
|
|
156
|
+
}
|