@wordpress/block-library 9.45.1-next.v.202605131032.0 → 9.47.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/build/button/edit.cjs +7 -4
- package/build/button/edit.cjs.map +3 -3
- package/build/columns/edit.cjs +4 -10
- package/build/columns/edit.cjs.map +2 -2
- package/build/comments/edit/placeholder.cjs +1 -1
- package/build/comments/edit/placeholder.cjs.map +2 -2
- package/build/cover/edit/inspector-controls.cjs +4 -2
- package/build/cover/edit/inspector-controls.cjs.map +2 -2
- package/build/freeform/migration-notice.cjs +1 -1
- package/build/freeform/migration-notice.cjs.map +1 -1
- package/build/home-link/block.json +7 -0
- package/build/home-link/edit.cjs +167 -24
- package/build/home-link/edit.cjs.map +3 -3
- package/build/html/edit.cjs +2 -4
- package/build/html/edit.cjs.map +2 -2
- package/build/html/modal.cjs +0 -4
- package/build/html/modal.cjs.map +2 -2
- package/build/image/block.json +4 -0
- package/build/image/deprecated.cjs +202 -4
- package/build/image/deprecated.cjs.map +3 -3
- package/build/image/image.cjs +80 -27
- package/build/image/image.cjs.map +2 -2
- package/build/image/index.cjs +23 -4
- package/build/image/index.cjs.map +2 -2
- package/build/image/save.cjs +25 -10
- package/build/image/save.cjs.map +2 -2
- package/build/image/transforms.cjs +15 -3
- package/build/image/transforms.cjs.map +2 -2
- package/build/image/use-open-image-media-editor-modal.cjs +29 -12
- package/build/image/use-open-image-media-editor-modal.cjs.map +2 -2
- package/build/list-item/hooks/use-enter.cjs +8 -4
- package/build/list-item/hooks/use-enter.cjs.map +3 -3
- package/build/list-item/hooks/use-space.cjs +8 -4
- package/build/list-item/hooks/use-space.cjs.map +3 -3
- package/build/navigation-link/edit.cjs +2 -1
- package/build/navigation-link/edit.cjs.map +2 -2
- package/build/navigation-link/shared/use-handle-link-change.cjs +19 -3
- package/build/navigation-link/shared/use-handle-link-change.cjs.map +3 -3
- package/build/navigation-submenu/edit.cjs +8 -22
- package/build/navigation-submenu/edit.cjs.map +2 -2
- package/build/paragraph/use-enter.cjs +8 -4
- package/build/paragraph/use-enter.cjs.map +3 -3
- package/build/post-date/edit.cjs +9 -1
- package/build/post-date/edit.cjs.map +2 -2
- package/build/post-featured-image/edit.cjs +0 -1
- package/build/post-featured-image/edit.cjs.map +2 -2
- package/build/site-logo/edit.cjs +1 -1
- package/build/site-logo/edit.cjs.map +2 -2
- package/build/social-link/edit.cjs.map +3 -3
- package/build/tab-list/edit.cjs +2 -0
- package/build/tab-list/edit.cjs.map +2 -2
- package/build/tab-panels/edit.cjs +5 -1
- package/build/tab-panels/edit.cjs.map +2 -2
- package/build/table/edit.cjs +1 -0
- package/build/table/edit.cjs.map +2 -2
- package/build/tabs/edit.cjs +1 -36
- package/build/tabs/edit.cjs.map +2 -2
- package/build-module/button/edit.mjs +12 -5
- package/build-module/button/edit.mjs.map +2 -2
- package/build-module/columns/edit.mjs +4 -10
- package/build-module/columns/edit.mjs.map +2 -2
- package/build-module/comments/edit/placeholder.mjs +1 -1
- package/build-module/comments/edit/placeholder.mjs.map +2 -2
- package/build-module/cover/edit/inspector-controls.mjs +4 -3
- package/build-module/cover/edit/inspector-controls.mjs.map +2 -2
- package/build-module/freeform/migration-notice.mjs +1 -1
- package/build-module/freeform/migration-notice.mjs.map +1 -1
- package/build-module/home-link/block.json +7 -0
- package/build-module/home-link/edit.mjs +181 -26
- package/build-module/home-link/edit.mjs.map +2 -2
- package/build-module/html/edit.mjs +2 -4
- package/build-module/html/edit.mjs.map +2 -2
- package/build-module/html/modal.mjs +0 -4
- package/build-module/html/modal.mjs.map +2 -2
- package/build-module/image/block.json +4 -0
- package/build-module/image/deprecated.mjs +204 -5
- package/build-module/image/deprecated.mjs.map +2 -2
- package/build-module/image/image.mjs +81 -27
- package/build-module/image/image.mjs.map +2 -2
- package/build-module/image/index.mjs +23 -4
- package/build-module/image/index.mjs.map +2 -2
- package/build-module/image/save.mjs +25 -10
- package/build-module/image/save.mjs.map +2 -2
- package/build-module/image/transforms.mjs +15 -3
- package/build-module/image/transforms.mjs.map +2 -2
- package/build-module/image/use-open-image-media-editor-modal.mjs +29 -12
- package/build-module/image/use-open-image-media-editor-modal.mjs.map +2 -2
- package/build-module/list-item/hooks/use-enter.mjs +12 -5
- package/build-module/list-item/hooks/use-enter.mjs.map +2 -2
- package/build-module/list-item/hooks/use-space.mjs +12 -5
- package/build-module/list-item/hooks/use-space.mjs.map +2 -2
- package/build-module/navigation-link/edit.mjs +2 -1
- package/build-module/navigation-link/edit.mjs.map +2 -2
- package/build-module/navigation-link/shared/use-handle-link-change.mjs +19 -3
- package/build-module/navigation-link/shared/use-handle-link-change.mjs.map +2 -2
- package/build-module/navigation-submenu/edit.mjs +9 -23
- package/build-module/navigation-submenu/edit.mjs.map +2 -2
- package/build-module/paragraph/use-enter.mjs +12 -5
- package/build-module/paragraph/use-enter.mjs.map +2 -2
- package/build-module/post-date/edit.mjs +9 -1
- package/build-module/post-date/edit.mjs.map +2 -2
- package/build-module/post-featured-image/edit.mjs +0 -1
- package/build-module/post-featured-image/edit.mjs.map +2 -2
- package/build-module/site-logo/edit.mjs +1 -1
- package/build-module/site-logo/edit.mjs.map +2 -2
- package/build-module/social-link/edit.mjs +2 -2
- package/build-module/social-link/edit.mjs.map +2 -2
- package/build-module/tab-list/edit.mjs +2 -0
- package/build-module/tab-list/edit.mjs.map +2 -2
- package/build-module/tab-panels/edit.mjs +5 -1
- package/build-module/tab-panels/edit.mjs.map +2 -2
- package/build-module/table/edit.mjs +1 -0
- package/build-module/table/edit.mjs.map +2 -2
- package/build-module/tabs/edit.mjs +2 -37
- package/build-module/tabs/edit.mjs.map +2 -2
- package/build-style/breadcrumbs/style-rtl.css +1 -1
- package/build-style/breadcrumbs/style.css +1 -1
- package/build-style/editor-rtl.css +0 -11
- package/build-style/editor.css +0 -11
- package/build-style/gallery/editor-rtl.css +0 -11
- package/build-style/gallery/editor.css +0 -11
- package/build-style/style-rtl.css +1 -1
- package/build-style/style.css +1 -1
- package/package.json +42 -42
- package/src/block/edit-title.native.js +3 -3
- package/src/block/edit.native.js +2 -2
- package/src/breadcrumbs/style.scss +1 -1
- package/src/button/edit.js +14 -5
- package/src/columns/edit.js +3 -9
- package/src/comments/edit/placeholder.js +1 -1
- package/src/cover/controls.native.js +2 -2
- package/src/cover/edit/inspector-controls.js +8 -7
- package/src/cover/edit.native.js +6 -4
- package/src/cover/focal-point-settings-button.native.js +2 -2
- package/src/cover/test/edit.js +32 -31
- package/src/embed/embed-no-preview.native.js +7 -3
- package/src/embed/embed-placeholder.native.js +2 -2
- package/src/file/edit.native.js +2 -2
- package/src/freeform/migration-notice.js +1 -1
- package/src/gallery/editor.scss +0 -14
- package/src/home-link/block.json +7 -0
- package/src/home-link/edit.js +185 -22
- package/src/home-link/index.php +14 -2
- package/src/html/edit.js +14 -12
- package/src/html/modal.js +0 -5
- package/src/image/block.json +4 -0
- package/src/image/deprecated.js +236 -4
- package/src/image/edit.native.js +2 -2
- package/src/image/image.js +116 -41
- package/src/image/index.js +20 -1
- package/src/image/index.php +1 -1
- package/src/image/save.js +39 -12
- package/src/image/test/use-open-image-media-editor-modal.js +60 -0
- package/src/image/transforms.js +21 -5
- package/src/image/use-open-image-media-editor-modal.js +34 -16
- package/src/latest-posts/edit.native.js +2 -2
- package/src/list-item/hooks/use-enter.js +15 -5
- package/src/list-item/hooks/use-space.js +15 -5
- package/src/list-item/list-style-type.native.js +2 -2
- package/src/media-text/media-container.native.js +7 -3
- package/src/missing/edit.native.js +4 -4
- package/src/missing/test/edit.native.js +3 -3
- package/src/navigation/test/use-navigation-menu.js +8 -2
- package/src/navigation-link/edit.js +1 -0
- package/src/navigation-link/shared/test/use-handle-link-change.test.js +212 -0
- package/src/navigation-link/shared/use-handle-link-change.js +36 -9
- package/src/navigation-link/test/__snapshots__/hooks.js.snap +134 -45
- package/src/navigation-submenu/edit.js +11 -28
- package/src/navigation-submenu/index.php +13 -0
- package/src/paragraph/use-enter.js +19 -5
- package/src/post-date/edit.js +7 -3
- package/src/post-featured-image/edit.js +0 -1
- package/src/search/edit.native.js +2 -2
- package/src/search/test/edit.native.js +2 -2
- package/src/site-logo/edit.js +2 -1
- package/src/social-link/edit.js +2 -2
- package/src/tab-list/edit.js +3 -0
- package/src/tab-panels/edit.js +10 -1
- package/src/table/edit.js +1 -0
- package/src/tabs/edit.js +14 -42
- package/src/video/edit.native.js +3 -3
|
@@ -27,6 +27,7 @@ function save({ attributes }) {
|
|
|
27
27
|
linkTarget,
|
|
28
28
|
sizeSlug,
|
|
29
29
|
title,
|
|
30
|
+
isDecorative,
|
|
30
31
|
metadata: { bindings = {} } = {}
|
|
31
32
|
} = attributes;
|
|
32
33
|
const newRel = !rel ? void 0 : rel;
|
|
@@ -49,16 +50,30 @@ function save({ attributes }) {
|
|
|
49
50
|
src: url,
|
|
50
51
|
alt,
|
|
51
52
|
className: imageClasses || void 0,
|
|
52
|
-
style: {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
53
|
+
style: (() => {
|
|
54
|
+
const style = {
|
|
55
|
+
...borderProps.style,
|
|
56
|
+
...shadowProps.style,
|
|
57
|
+
aspectRatio,
|
|
58
|
+
objectFit: scale,
|
|
59
|
+
objectPosition: focalPoint && scale ? mediaPosition(focalPoint) : void 0
|
|
60
|
+
};
|
|
61
|
+
if (width !== void 0 || height !== void 0) {
|
|
62
|
+
if (width === "auto") {
|
|
63
|
+
style.width = "auto";
|
|
64
|
+
} else if (width !== void 0 && width !== null) {
|
|
65
|
+
style.width = typeof width === "number" ? `${width}px` : width;
|
|
66
|
+
}
|
|
67
|
+
if (height === "auto" || height === void 0 || height === null) {
|
|
68
|
+
style.height = "auto";
|
|
69
|
+
} else {
|
|
70
|
+
style.height = typeof height === "number" ? `${height}px` : height;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return style;
|
|
74
|
+
})(),
|
|
75
|
+
title,
|
|
76
|
+
role: isDecorative ? "none" : void 0
|
|
62
77
|
}
|
|
63
78
|
);
|
|
64
79
|
const displayCaption = !RichText.isEmpty(caption) || bindings.caption || bindings?.__default?.source === "core/pattern-overrides";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/image/save.js"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport clsx from 'clsx';\n\n/**\n * WordPress dependencies\n */\nimport {\n\tRichText,\n\tuseBlockProps,\n\t__experimentalGetElementClassName,\n\t__experimentalGetBorderClassesAndStyles as getBorderClassesAndStyles,\n\t__experimentalGetShadowClassesAndStyles as getShadowClassesAndStyles,\n} from '@wordpress/block-editor';\n\n/**\n * Internal dependencies\n */\nimport { mediaPosition } from './utils';\n\nexport default function save( { attributes } ) {\n\tconst {\n\t\turl,\n\t\talt,\n\t\tcaption,\n\t\talign,\n\t\thref,\n\t\trel,\n\t\tlinkClass,\n\t\twidth,\n\t\theight,\n\t\taspectRatio,\n\t\tscale,\n\t\tfocalPoint,\n\t\tid,\n\t\tlinkTarget,\n\t\tsizeSlug,\n\t\ttitle,\n\t\tmetadata: { bindings = {} } = {},\n\t} = attributes;\n\n\tconst newRel = ! rel ? undefined : rel;\n\tconst borderProps = getBorderClassesAndStyles( attributes );\n\tconst shadowProps = getShadowClassesAndStyles( attributes );\n\n\tconst classes = clsx( {\n\t\t// All other align classes are handled by block supports.\n\t\t// `{ align: 'none' }` is unique to transforms for the image block.\n\t\talignnone: 'none' === align,\n\t\t[ `size-${ sizeSlug }` ]: sizeSlug,\n\t\t'is-resized': width || height,\n\t\t'has-custom-border':\n\t\t\t!! borderProps.className ||\n\t\t\t( borderProps.style &&\n\t\t\t\tObject.keys( borderProps.style ).length > 0 ),\n\t} );\n\n\tconst imageClasses = clsx( borderProps.className, {\n\t\t[ `wp-image-${ id }` ]: !! id,\n\t} );\n\n\tconst image = (\n\t\t<img\n\t\t\tsrc={ url }\n\t\t\talt={ alt }\n\t\t\tclassName={ imageClasses || undefined }\n\t\t\tstyle={ {\n\t\t\t\t...borderProps.style,\n\t\t\t\t...shadowProps.style,\n\t\t\t\taspectRatio,\n\t\t\t\tobjectFit: scale,\n\t\t\t\tobjectPosition:\n\t\t\t\t\tfocalPoint && scale\n\t\t\t\t\t\t? mediaPosition( focalPoint )\n\t\t\t\t\t\t: undefined,\n\t\t\t\
|
|
5
|
-
"mappings": ";AAGA,OAAO,UAAU;AAKjB;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA,2CAA2C;AAAA,EAC3C,2CAA2C;AAAA,OACrC;AAKP,SAAS,qBAAqB;
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport clsx from 'clsx';\n\n/**\n * WordPress dependencies\n */\nimport {\n\tRichText,\n\tuseBlockProps,\n\t__experimentalGetElementClassName,\n\t__experimentalGetBorderClassesAndStyles as getBorderClassesAndStyles,\n\t__experimentalGetShadowClassesAndStyles as getShadowClassesAndStyles,\n} from '@wordpress/block-editor';\n\n/**\n * Internal dependencies\n */\nimport { mediaPosition } from './utils';\n\nexport default function save( { attributes } ) {\n\tconst {\n\t\turl,\n\t\talt,\n\t\tcaption,\n\t\talign,\n\t\thref,\n\t\trel,\n\t\tlinkClass,\n\t\twidth,\n\t\theight,\n\t\taspectRatio,\n\t\tscale,\n\t\tfocalPoint,\n\t\tid,\n\t\tlinkTarget,\n\t\tsizeSlug,\n\t\ttitle,\n\t\tisDecorative,\n\t\tmetadata: { bindings = {} } = {},\n\t} = attributes;\n\n\tconst newRel = ! rel ? undefined : rel;\n\tconst borderProps = getBorderClassesAndStyles( attributes );\n\tconst shadowProps = getShadowClassesAndStyles( attributes );\n\n\tconst classes = clsx( {\n\t\t// All other align classes are handled by block supports.\n\t\t// `{ align: 'none' }` is unique to transforms for the image block.\n\t\talignnone: 'none' === align,\n\t\t[ `size-${ sizeSlug }` ]: sizeSlug,\n\t\t'is-resized': width || height,\n\t\t'has-custom-border':\n\t\t\t!! borderProps.className ||\n\t\t\t( borderProps.style &&\n\t\t\t\tObject.keys( borderProps.style ).length > 0 ),\n\t} );\n\n\tconst imageClasses = clsx( borderProps.className, {\n\t\t[ `wp-image-${ id }` ]: !! id,\n\t} );\n\n\tconst image = (\n\t\t<img\n\t\t\tsrc={ url }\n\t\t\talt={ alt }\n\t\t\tclassName={ imageClasses || undefined }\n\t\t\tstyle={ ( () => {\n\t\t\t\tconst style = {\n\t\t\t\t\t...borderProps.style,\n\t\t\t\t\t...shadowProps.style,\n\t\t\t\t\taspectRatio,\n\t\t\t\t\tobjectFit: scale,\n\t\t\t\t\tobjectPosition:\n\t\t\t\t\t\tfocalPoint && scale\n\t\t\t\t\t\t\t? mediaPosition( focalPoint )\n\t\t\t\t\t\t\t: undefined,\n\t\t\t\t};\n\t\t\t\t// Only apply dimension styles when a width or height is provided.\n\t\t\t\tif ( width !== undefined || height !== undefined ) {\n\t\t\t\t\t// Only apply width when explicitly provided.\n\t\t\t\t\tif ( width === 'auto' ) {\n\t\t\t\t\t\tstyle.width = 'auto';\n\t\t\t\t\t} else if ( width !== undefined && width !== null ) {\n\t\t\t\t\t\tstyle.width =\n\t\t\t\t\t\t\ttypeof width === 'number' ? `${ width }px` : width;\n\t\t\t\t\t}\n\t\t\t\t\t// Force height to auto when unspecified to prevent\n\t\t\t\t\t// theme CSS from squishing the image.\n\t\t\t\t\tif (\n\t\t\t\t\t\theight === 'auto' ||\n\t\t\t\t\t\theight === undefined ||\n\t\t\t\t\t\theight === null\n\t\t\t\t\t) {\n\t\t\t\t\t\tstyle.height = 'auto';\n\t\t\t\t\t} else {\n\t\t\t\t\t\tstyle.height =\n\t\t\t\t\t\t\ttypeof height === 'number'\n\t\t\t\t\t\t\t\t? `${ height }px`\n\t\t\t\t\t\t\t\t: height;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn style;\n\t\t\t} )() }\n\t\t\ttitle={ title }\n\t\t\trole={ isDecorative ? 'none' : undefined }\n\t\t/>\n\t);\n\n\tconst displayCaption =\n\t\t! RichText.isEmpty( caption ) ||\n\t\tbindings.caption ||\n\t\tbindings?.__default?.source === 'core/pattern-overrides';\n\n\tconst figure = (\n\t\t<>\n\t\t\t{ href ? (\n\t\t\t\t<a\n\t\t\t\t\tclassName={ linkClass }\n\t\t\t\t\thref={ href }\n\t\t\t\t\ttarget={ linkTarget }\n\t\t\t\t\trel={ newRel }\n\t\t\t\t>\n\t\t\t\t\t{ image }\n\t\t\t\t</a>\n\t\t\t) : (\n\t\t\t\timage\n\t\t\t) }\n\t\t\t{ displayCaption && (\n\t\t\t\t<RichText.Content\n\t\t\t\t\tclassName={ __experimentalGetElementClassName( 'caption' ) }\n\t\t\t\t\ttagName=\"figcaption\"\n\t\t\t\t\tvalue={ caption }\n\t\t\t\t/>\n\t\t\t) }\n\t\t</>\n\t);\n\n\treturn (\n\t\t<figure { ...useBlockProps.save( { className: classes } ) }>\n\t\t\t{ figure }\n\t\t</figure>\n\t);\n}\n"],
|
|
5
|
+
"mappings": ";AAGA,OAAO,UAAU;AAKjB;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA,2CAA2C;AAAA,EAC3C,2CAA2C;AAAA,OACrC;AAKP,SAAS,qBAAqB;AA6C5B,SAoDA,UApDA,KAoDA,YApDA;AA3Ca,SAAR,KAAuB,EAAE,WAAW,GAAI;AAC9C,QAAM;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,UAAU,EAAE,WAAW,CAAC,EAAE,IAAI,CAAC;AAAA,EAChC,IAAI;AAEJ,QAAM,SAAS,CAAE,MAAM,SAAY;AACnC,QAAM,cAAc,0BAA2B,UAAW;AAC1D,Q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,QAAS;AAAA,QACT,KAAM;AAAA,QAEJ;AAAA;AAAA,IACH,IAEA;AAAA,IAEC,kBACD;AAAA,MAAC,SAAS;AAAA,MAAT;AAAA,QACA,WAAY,kCAAmC,SAAU;AAAA,QACzD,SAAQ;AAAA,QACR,OAAQ;AAAA;AAAA,IACT;AAAA,KAEF;AAGD,SACC,oBAAC,YAAS,GAAG,cAAc,KAAM,EAAE,WAAW,QAAQ,CAAE,GACrD,kBACH;AAEF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -23,7 +23,7 @@ function getFirstAnchorAttributeFormHTML(html, attributeName) {
|
|
|
23
23
|
}
|
|
24
24
|
var imageSchema = {
|
|
25
25
|
img: {
|
|
26
|
-
attributes: ["src", "alt", "title"],
|
|
26
|
+
attributes: ["src", "alt", "title", "width", "height"],
|
|
27
27
|
classes: [
|
|
28
28
|
"alignleft",
|
|
29
29
|
"aligncenter",
|
|
@@ -33,6 +33,9 @@ var imageSchema = {
|
|
|
33
33
|
]
|
|
34
34
|
}
|
|
35
35
|
};
|
|
36
|
+
function parsePixelDimension(value) {
|
|
37
|
+
return value && /^\d+$/.test(value) ? `${value}px` : void 0;
|
|
38
|
+
}
|
|
36
39
|
var schema = ({ phrasingContentSchema }) => ({
|
|
37
40
|
figure: {
|
|
38
41
|
require: ["img"],
|
|
@@ -56,7 +59,8 @@ var transforms = {
|
|
|
56
59
|
isMatch: (node) => node.nodeName === "FIGURE" && !!node.querySelector("img"),
|
|
57
60
|
schema,
|
|
58
61
|
transform: (node) => {
|
|
59
|
-
const
|
|
62
|
+
const img = node.querySelector("img");
|
|
63
|
+
const className = node.className + " " + img.className;
|
|
60
64
|
const alignMatches = /(?:^|\s)align(left|center|right)(?:$|\s)/.exec(
|
|
61
65
|
className
|
|
62
66
|
);
|
|
@@ -71,6 +75,12 @@ var transforms = {
|
|
|
71
75
|
const href = anchorElement && anchorElement.href ? anchorElement.href : void 0;
|
|
72
76
|
const rel = anchorElement && anchorElement.rel ? anchorElement.rel : void 0;
|
|
73
77
|
const linkClass = anchorElement && anchorElement.className ? anchorElement.className : void 0;
|
|
78
|
+
const width = parsePixelDimension(
|
|
79
|
+
img.getAttribute("width")
|
|
80
|
+
);
|
|
81
|
+
const height = parsePixelDimension(
|
|
82
|
+
img.getAttribute("height")
|
|
83
|
+
);
|
|
74
84
|
const attributes = getBlockAttributes(
|
|
75
85
|
"core/image",
|
|
76
86
|
node.outerHTML,
|
|
@@ -81,7 +91,9 @@ var transforms = {
|
|
|
81
91
|
href,
|
|
82
92
|
rel,
|
|
83
93
|
linkClass,
|
|
84
|
-
anchor
|
|
94
|
+
anchor,
|
|
95
|
+
width,
|
|
96
|
+
height
|
|
85
97
|
}
|
|
86
98
|
);
|
|
87
99
|
if (isBlobURL(attributes.url)) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/image/transforms.js"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { createBlobURL, isBlobURL } from '@wordpress/blob';\nimport { createBlock, getBlockAttributes } from '@wordpress/blocks';\n\nexport function stripFirstImage( attributes, { shortcode } ) {\n\tconst { body } = document.implementation.createHTMLDocument( '' );\n\n\tbody.innerHTML = shortcode.content;\n\n\tlet nodeToRemove = body.querySelector( 'img' );\n\n\t// If an image has parents, find the topmost node to remove.\n\twhile (\n\t\tnodeToRemove &&\n\t\tnodeToRemove.parentNode &&\n\t\tnodeToRemove.parentNode !== body\n\t) {\n\t\tnodeToRemove = nodeToRemove.parentNode;\n\t}\n\n\tif ( nodeToRemove ) {\n\t\tnodeToRemove.parentNode.removeChild( nodeToRemove );\n\t}\n\n\treturn body.innerHTML.trim();\n}\n\nfunction getFirstAnchorAttributeFormHTML( html, attributeName ) {\n\tconst { body } = document.implementation.createHTMLDocument( '' );\n\n\tbody.innerHTML = html;\n\n\tconst { firstElementChild } = body;\n\n\tif ( firstElementChild && firstElementChild.nodeName === 'A' ) {\n\t\treturn firstElementChild.getAttribute( attributeName ) || undefined;\n\t}\n}\n\nconst imageSchema = {\n\timg: {\n\t\tattributes: [ 'src', 'alt', 'title' ],\n\t\tclasses: [\n\t\t\t'alignleft',\n\t\t\t'aligncenter',\n\t\t\t'alignright',\n\t\t\t'alignnone',\n\t\t\t/^wp-image-\\d+$/,\n\t\t],\n\t},\n};\n\nconst schema = ( { phrasingContentSchema } ) => ( {\n\tfigure: {\n\t\trequire: [ 'img' ],\n\t\tchildren: {\n\t\t\t...imageSchema,\n\t\t\ta: {\n\t\t\t\tattributes: [ 'href', 'rel', 'target' ],\n\t\t\t\tclasses: [ '*' ],\n\t\t\t\tchildren: imageSchema,\n\t\t\t},\n\t\t\tfigcaption: {\n\t\t\t\tchildren: phrasingContentSchema,\n\t\t\t},\n\t\t},\n\t},\n} );\n\nconst transforms = {\n\tfrom: [\n\t\t{\n\t\t\ttype: 'raw',\n\t\t\tisMatch: ( node ) =>\n\t\t\t\tnode.nodeName === 'FIGURE' && !! node.querySelector( 'img' ),\n\t\t\tschema,\n\t\t\ttransform: ( node ) => {\n\t\t\t\t// Search both figure and image classes. Alignment could be\n\t\t\t\t// set on either. ID is set on the image.\n\t\t\t\tconst className
|
|
5
|
-
"mappings": ";AAGA,SAAS,eAAe,iBAAiB;AACzC,SAAS,aAAa,0BAA0B;AAEzC,SAAS,gBAAiB,YAAY,EAAE,UAAU,GAAI;AAC5D,QAAM,EAAE,KAAK,IAAI,SAAS,eAAe,mBAAoB,EAAG;AAEhE,OAAK,YAAY,UAAU;AAE3B,MAAI,eAAe,KAAK,cAAe,KAAM;AAG7C,SACC,gBACA,aAAa,cACb,aAAa,eAAe,MAC3B;AACD,mBAAe,aAAa;AAAA,EAC7B;AAEA,MAAK,cAAe;AACnB,iBAAa,WAAW,YAAa,YAAa;AAAA,EACnD;AAEA,SAAO,KAAK,UAAU,KAAK;AAC5B;AAEA,SAAS,gCAAiC,MAAM,eAAgB;AAC/D,QAAM,EAAE,KAAK,IAAI,SAAS,eAAe,mBAAoB,EAAG;AAEhE,OAAK,YAAY;AAEjB,QAAM,EAAE,kBAAkB,IAAI;AAE9B,MAAK,qBAAqB,kBAAkB,aAAa,KAAM;AAC9D,WAAO,kBAAkB,aAAc,aAAc,KAAK;AAAA,EAC3D;AACD;AAEA,IAAM,cAAc;AAAA,EACnB,KAAK;AAAA,IACJ,YAAY,CAAE,OAAO,OAAO,
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { createBlobURL, isBlobURL } from '@wordpress/blob';\nimport { createBlock, getBlockAttributes } from '@wordpress/blocks';\n\nexport function stripFirstImage( attributes, { shortcode } ) {\n\tconst { body } = document.implementation.createHTMLDocument( '' );\n\n\tbody.innerHTML = shortcode.content;\n\n\tlet nodeToRemove = body.querySelector( 'img' );\n\n\t// If an image has parents, find the topmost node to remove.\n\twhile (\n\t\tnodeToRemove &&\n\t\tnodeToRemove.parentNode &&\n\t\tnodeToRemove.parentNode !== body\n\t) {\n\t\tnodeToRemove = nodeToRemove.parentNode;\n\t}\n\n\tif ( nodeToRemove ) {\n\t\tnodeToRemove.parentNode.removeChild( nodeToRemove );\n\t}\n\n\treturn body.innerHTML.trim();\n}\n\nfunction getFirstAnchorAttributeFormHTML( html, attributeName ) {\n\tconst { body } = document.implementation.createHTMLDocument( '' );\n\n\tbody.innerHTML = html;\n\n\tconst { firstElementChild } = body;\n\n\tif ( firstElementChild && firstElementChild.nodeName === 'A' ) {\n\t\treturn firstElementChild.getAttribute( attributeName ) || undefined;\n\t}\n}\n\nconst imageSchema = {\n\timg: {\n\t\tattributes: [ 'src', 'alt', 'title', 'width', 'height' ],\n\t\tclasses: [\n\t\t\t'alignleft',\n\t\t\t'aligncenter',\n\t\t\t'alignright',\n\t\t\t'alignnone',\n\t\t\t/^wp-image-\\d+$/,\n\t\t],\n\t},\n};\n\n// Read a pixel dimension from an `<img>` HTML attribute and normalise it to\n// the `<value>px` form the Image block stores in its `width` / `height`\n// attributes. Non-integer values (e.g. `50%`) are dropped because the block\n// attribute round-trips through inline styles that expect pixel units.\nfunction parsePixelDimension( value ) {\n\treturn value && /^\\d+$/.test( value ) ? `${ value }px` : undefined;\n}\n\nconst schema = ( { phrasingContentSchema } ) => ( {\n\tfigure: {\n\t\trequire: [ 'img' ],\n\t\tchildren: {\n\t\t\t...imageSchema,\n\t\t\ta: {\n\t\t\t\tattributes: [ 'href', 'rel', 'target' ],\n\t\t\t\tclasses: [ '*' ],\n\t\t\t\tchildren: imageSchema,\n\t\t\t},\n\t\t\tfigcaption: {\n\t\t\t\tchildren: phrasingContentSchema,\n\t\t\t},\n\t\t},\n\t},\n} );\n\nconst transforms = {\n\tfrom: [\n\t\t{\n\t\t\ttype: 'raw',\n\t\t\tisMatch: ( node ) =>\n\t\t\t\tnode.nodeName === 'FIGURE' && !! node.querySelector( 'img' ),\n\t\t\tschema,\n\t\t\ttransform: ( node ) => {\n\t\t\t\tconst img = node.querySelector( 'img' );\n\t\t\t\t// Search both figure and image classes. Alignment could be\n\t\t\t\t// set on either. ID is set on the image.\n\t\t\t\tconst className = node.className + ' ' + img.className;\n\t\t\t\tconst alignMatches =\n\t\t\t\t\t/(?:^|\\s)align(left|center|right)(?:$|\\s)/.exec(\n\t\t\t\t\t\tclassName\n\t\t\t\t\t);\n\t\t\t\tconst anchor = node.id === '' ? undefined : node.id;\n\t\t\t\tconst align = alignMatches ? alignMatches[ 1 ] : undefined;\n\t\t\t\tconst idMatches = /(?:^|\\s)wp-image-(\\d+)(?:$|\\s)/.exec(\n\t\t\t\t\tclassName\n\t\t\t\t);\n\t\t\t\tconst id = idMatches ? Number( idMatches[ 1 ] ) : undefined;\n\t\t\t\tconst anchorElement = node.querySelector( 'a' );\n\t\t\t\tconst linkDestination =\n\t\t\t\t\tanchorElement && anchorElement.href ? 'custom' : undefined;\n\t\t\t\tconst href =\n\t\t\t\t\tanchorElement && anchorElement.href\n\t\t\t\t\t\t? anchorElement.href\n\t\t\t\t\t\t: undefined;\n\t\t\t\tconst rel =\n\t\t\t\t\tanchorElement && anchorElement.rel\n\t\t\t\t\t\t? anchorElement.rel\n\t\t\t\t\t\t: undefined;\n\t\t\t\tconst linkClass =\n\t\t\t\t\tanchorElement && anchorElement.className\n\t\t\t\t\t\t? anchorElement.className\n\t\t\t\t\t\t: undefined;\n\t\t\t\t// Preserve explicit pixel dimensions set on the source `<img>`\n\t\t\t\t// (e.g. legacy \"Add Media\" output: `<img width=\"77\" height=\"77\">`).\n\t\t\t\tconst width = parsePixelDimension(\n\t\t\t\t\timg.getAttribute( 'width' )\n\t\t\t\t);\n\t\t\t\tconst height = parsePixelDimension(\n\t\t\t\t\timg.getAttribute( 'height' )\n\t\t\t\t);\n\t\t\t\tconst attributes = getBlockAttributes(\n\t\t\t\t\t'core/image',\n\t\t\t\t\tnode.outerHTML,\n\t\t\t\t\t{\n\t\t\t\t\t\talign,\n\t\t\t\t\t\tid,\n\t\t\t\t\t\tlinkDestination,\n\t\t\t\t\t\thref,\n\t\t\t\t\t\trel,\n\t\t\t\t\t\tlinkClass,\n\t\t\t\t\t\tanchor,\n\t\t\t\t\t\twidth,\n\t\t\t\t\t\theight,\n\t\t\t\t\t}\n\t\t\t\t);\n\n\t\t\t\tif ( isBlobURL( attributes.url ) ) {\n\t\t\t\t\tattributes.blob = attributes.url;\n\t\t\t\t\tdelete attributes.url;\n\t\t\t\t}\n\n\t\t\t\treturn createBlock( 'core/image', attributes );\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\t// Note: when dragging and dropping multiple files onto a gallery this overrides the\n\t\t\t// gallery transform in order to add new images to the gallery instead of\n\t\t\t// creating a new gallery.\n\t\t\ttype: 'files',\n\t\t\tisMatch( files ) {\n\t\t\t\treturn files.every(\n\t\t\t\t\t( file ) => file.type.indexOf( 'image/' ) === 0\n\t\t\t\t);\n\t\t\t},\n\t\t\ttransform( files ) {\n\t\t\t\tconst blocks = files.map( ( file ) => {\n\t\t\t\t\treturn createBlock( 'core/image', {\n\t\t\t\t\t\tblob: createBlobURL( file ),\n\t\t\t\t\t} );\n\t\t\t\t} );\n\t\t\t\treturn blocks;\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\ttype: 'shortcode',\n\t\t\ttag: 'caption',\n\t\t\tattributes: {\n\t\t\t\turl: {\n\t\t\t\t\ttype: 'string',\n\t\t\t\t\tsource: 'attribute',\n\t\t\t\t\tattribute: 'src',\n\t\t\t\t\tselector: 'img',\n\t\t\t\t},\n\t\t\t\talt: {\n\t\t\t\t\ttype: 'string',\n\t\t\t\t\tsource: 'attribute',\n\t\t\t\t\tattribute: 'alt',\n\t\t\t\t\tselector: 'img',\n\t\t\t\t},\n\t\t\t\tcaption: {\n\t\t\t\t\tshortcode: stripFirstImage,\n\t\t\t\t},\n\t\t\t\thref: {\n\t\t\t\t\tshortcode: ( attributes, { shortcode } ) => {\n\t\t\t\t\t\treturn getFirstAnchorAttributeFormHTML(\n\t\t\t\t\t\t\tshortcode.content,\n\t\t\t\t\t\t\t'href'\n\t\t\t\t\t\t);\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\trel: {\n\t\t\t\t\tshortcode: ( attributes, { shortcode } ) => {\n\t\t\t\t\t\treturn getFirstAnchorAttributeFormHTML(\n\t\t\t\t\t\t\tshortcode.content,\n\t\t\t\t\t\t\t'rel'\n\t\t\t\t\t\t);\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\tlinkClass: {\n\t\t\t\t\tshortcode: ( attributes, { shortcode } ) => {\n\t\t\t\t\t\treturn getFirstAnchorAttributeFormHTML(\n\t\t\t\t\t\t\tshortcode.content,\n\t\t\t\t\t\t\t'class'\n\t\t\t\t\t\t);\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\tid: {\n\t\t\t\t\ttype: 'number',\n\t\t\t\t\tshortcode: ( { named: { id } } ) => {\n\t\t\t\t\t\tif ( ! id ) {\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn parseInt( id.replace( 'attachment_', '' ), 10 );\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\talign: {\n\t\t\t\t\ttype: 'string',\n\t\t\t\t\tshortcode: ( { named: { align = 'alignnone' } } ) => {\n\t\t\t\t\t\treturn align.replace( 'align', '' );\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t],\n};\n\nexport default transforms;\n"],
|
|
5
|
+
"mappings": ";AAGA,SAAS,eAAe,iBAAiB;AACzC,SAAS,aAAa,0BAA0B;AAEzC,SAAS,gBAAiB,YAAY,EAAE,UAAU,GAAI;AAC5D,QAAM,EAAE,KAAK,IAAI,SAAS,eAAe,mBAAoB,EAAG;AAEhE,OAAK,YAAY,UAAU;AAE3B,MAAI,eAAe,KAAK,cAAe,KAAM;AAG7C,SACC,gBACA,aAAa,cACb,aAAa,eAAe,MAC3B;AACD,mBAAe,aAAa;AAAA,EAC7B;AAEA,MAAK,cAAe;AACnB,iBAAa,WAAW,YAAa,YAAa;AAAA,EACnD;AAEA,SAAO,KAAK,UAAU,KAAK;AAC5B;AAEA,SAAS,gCAAiC,MAAM,eAAgB;AAC/D,QAAM,EAAE,KAAK,IAAI,SAAS,eAAe,mBAAoB,EAAG;AAEhE,OAAK,YAAY;AAEjB,QAAM,EAAE,kBAAkB,IAAI;AAE9B,MAAK,qBAAqB,kBAAkB,aAAa,KAAM;AAC9D,WAAO,kBAAkB,aAAc,aAAc,KAAK;AAAA,EAC3D;AACD;AAEA,IAAM,cAAc;AAAA,EACnB,KAAK;AAAA,IACJ,YAAY,CAAE,OAAO,OAAO,SAAS,SAAS,QAAS;AAAA,IACvD,SAAS;AAAA,MACR;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;AACD;AAMA,SAAS,oBAAqB,OAAQ;AACrC,SAAO,SAAS,QAAQ,KAAM,KAAM,IAAI,GAAI,KAAM,OAAO;AAC1D;AAEA,IAAM,SAAS,CAAE,EAAE,sBAAsB,OAAS;AAA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O;AAAA,cACN,UAAU;AAAA,cACV;AAAA,YACD;AAAA,UACD;AAAA,QACD;AAAA,QACA,IAAI;AAAA,UACH,MAAM;AAAA,UACN,WAAW,CAAE,EAAE,OAAO,EAAE,GAAG,EAAE,MAAO;AACnC,gBAAK,CAAE,IAAK;AACX;AAAA,YACD;AAEA,mBAAO,SAAU,GAAG,QAAS,eAAe,EAAG,GAAG,EAAG;AAAA,UACtD;AAAA,QACD;AAAA,QACA,OAAO;AAAA,UACN,MAAM;AAAA,UACN,WAAW,CAAE,EAAE,OAAO,EAAE,QAAQ,YAAY,EAAE,MAAO;AACpD,mBAAO,MAAM,QAAS,SAAS,EAAG;AAAA,UACnC;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;AAEA,IAAO,qBAAQ;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -85,12 +85,22 @@ function useOpenImageMediaEditorModal({ attributes, setAttributes }) {
|
|
|
85
85
|
(select) => select(blockEditorStore).getSettings()[openMediaEditorModalKey],
|
|
86
86
|
[]
|
|
87
87
|
);
|
|
88
|
-
const
|
|
88
|
+
const blockAttributesRef = useRef({
|
|
89
|
+
id,
|
|
90
|
+
url,
|
|
91
|
+
alt,
|
|
92
|
+
caption: caption?.toString()
|
|
93
|
+
});
|
|
89
94
|
const mediaEditorMetadataBaselineRef = useRef();
|
|
90
95
|
const mediaEditorMetadataSyncRequestRef = useRef(0);
|
|
91
96
|
useEffect(() => {
|
|
92
|
-
|
|
93
|
-
|
|
97
|
+
blockAttributesRef.current = {
|
|
98
|
+
id,
|
|
99
|
+
url,
|
|
100
|
+
alt,
|
|
101
|
+
caption: caption?.toString()
|
|
102
|
+
};
|
|
103
|
+
}, [alt, caption, id, url]);
|
|
94
104
|
const getCachedAttachmentRecord = useCallback(
|
|
95
105
|
(attachmentId) => {
|
|
96
106
|
const { getEditedEntityRecord, getEntityRecord } = registry.select(coreStore);
|
|
@@ -154,33 +164,40 @@ function useOpenImageMediaEditorModal({ attributes, setAttributes }) {
|
|
|
154
164
|
mediaEditorMetadataBaselineRef.current = void 0;
|
|
155
165
|
const syncRequest = ++mediaEditorMetadataSyncRequestRef.current;
|
|
156
166
|
const nextAttributes = {};
|
|
157
|
-
|
|
167
|
+
const currentBlockAttributes = blockAttributesRef.current;
|
|
168
|
+
if (newId !== currentBlockAttributes.id) {
|
|
158
169
|
nextAttributes.id = newId;
|
|
159
|
-
nextAttributes.url = newUrl ?? url;
|
|
170
|
+
nextAttributes.url = newUrl ?? currentBlockAttributes.url;
|
|
171
|
+
blockAttributesRef.current = {
|
|
172
|
+
...blockAttributesRef.current,
|
|
173
|
+
id: nextAttributes.id,
|
|
174
|
+
url: nextAttributes.url
|
|
175
|
+
};
|
|
160
176
|
}
|
|
161
177
|
if (originalAttachment) {
|
|
162
178
|
const resolvedAttachment = await resolveFreshAttachmentRecord(newId);
|
|
163
179
|
if (syncRequest !== mediaEditorMetadataSyncRequestRef.current) {
|
|
164
180
|
return;
|
|
165
181
|
}
|
|
182
|
+
const latestBlockAttributes = blockAttributesRef.current;
|
|
166
183
|
const resolvedMetadataAttributes = getSyncedImageBlockAttributes(
|
|
167
|
-
|
|
184
|
+
latestBlockAttributes,
|
|
168
185
|
originalAttachment,
|
|
169
186
|
resolvedAttachment
|
|
170
187
|
);
|
|
171
188
|
if (Object.keys(resolvedMetadataAttributes).length) {
|
|
172
189
|
Object.assign(nextAttributes, resolvedMetadataAttributes);
|
|
173
|
-
blockMetadataRef.current = {
|
|
174
|
-
...blockMetadataRef.current,
|
|
175
|
-
...resolvedMetadataAttributes
|
|
176
|
-
};
|
|
177
190
|
}
|
|
178
191
|
}
|
|
179
192
|
if (Object.keys(nextAttributes).length) {
|
|
193
|
+
blockAttributesRef.current = {
|
|
194
|
+
...blockAttributesRef.current,
|
|
195
|
+
...nextAttributes
|
|
196
|
+
};
|
|
180
197
|
setAttributes(nextAttributes);
|
|
181
198
|
}
|
|
182
199
|
},
|
|
183
|
-
[
|
|
200
|
+
[resolveFreshAttachmentRecord, setAttributes]
|
|
184
201
|
);
|
|
185
202
|
const openImageMediaEditorModal = useCallback(async () => {
|
|
186
203
|
if (!id || !openMediaEditorModal) {
|
|
@@ -188,7 +205,7 @@ function useOpenImageMediaEditorModal({ attributes, setAttributes }) {
|
|
|
188
205
|
}
|
|
189
206
|
const cachedAttachmentRecord = getCachedAttachmentRecord(id);
|
|
190
207
|
const fallbackAttachmentRecord = getAttachmentFallbackForEmptyBlockMetadata(
|
|
191
|
-
|
|
208
|
+
blockAttributesRef.current
|
|
192
209
|
);
|
|
193
210
|
const resolvedAttachmentRecord = hasKnownAttachmentMetadata(
|
|
194
211
|
cachedAttachmentRecord
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/image/use-open-image-media-editor-modal.js"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { store as coreStore } from '@wordpress/core-data';\nimport {\n\tprivateApis as blockEditorPrivateApis,\n\tstore as blockEditorStore,\n} from '@wordpress/block-editor';\nimport { __unstableStripHTML as stripHTML } from '@wordpress/dom';\nimport { useRegistry, useSelect } from '@wordpress/data';\nimport { useCallback, useEffect, useRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { unlock } from '../lock-unlock';\n\nfunction normalizeImageBlockCaption( caption ) {\n\tif ( typeof caption !== 'string' ) {\n\t\treturn '';\n\t}\n\n\tconst textContent = stripHTML( caption ).trim();\n\n\tif ( ! textContent ) {\n\t\treturn '';\n\t}\n\n\treturn caption.replace( /\\n/g, '<br>' );\n}\n\nfunction getAttachmentCaption( attachment ) {\n\tconst caption = attachment?.caption;\n\n\tif ( typeof caption === 'string' ) {\n\t\treturn normalizeImageBlockCaption( caption );\n\t}\n\n\tif (\n\t\tcaption &&\n\t\ttypeof caption === 'object' &&\n\t\tObject.hasOwn( caption, 'raw' )\n\t) {\n\t\treturn normalizeImageBlockCaption( caption.raw );\n\t}\n\n\treturn undefined;\n}\n\nexport function getImageBlockMetadataFromAttachment( attachment ) {\n\treturn {\n\t\talt:\n\t\t\ttypeof attachment?.alt_text === 'string'\n\t\t\t\t? attachment.alt_text\n\t\t\t\t: attachment?.alt || '',\n\t\tcaption: getAttachmentCaption( attachment ),\n\t};\n}\n\nfunction normalizeMetadataAttribute( value ) {\n\treturn value || '';\n}\n\nexport function getSyncedImageBlockAttributes(\n\tcurrentAttributes,\n\toriginalAttachment,\n\tupdatedAttachment\n) {\n\tif ( ! originalAttachment || ! updatedAttachment ) {\n\t\treturn {};\n\t}\n\n\tconst originalMetadata =\n\t\tgetImageBlockMetadataFromAttachment( originalAttachment );\n\tconst updatedMetadata =\n\t\tgetImageBlockMetadataFromAttachment( updatedAttachment );\n\tconst syncedAttributes = {};\n\n\tconst normalizedCurrentAlt = normalizeMetadataAttribute(\n\t\tcurrentAttributes.alt\n\t);\n\tif (\n\t\toriginalMetadata.alt !== updatedMetadata.alt &&\n\t\t( normalizedCurrentAlt === originalMetadata.alt ||\n\t\t\t! normalizedCurrentAlt )\n\t) {\n\t\tsyncedAttributes.alt = updatedMetadata.alt;\n\t}\n\n\tconst normalizedCurrentCaption = normalizeMetadataAttribute(\n\t\tcurrentAttributes.caption\n\t);\n\tif (\n\t\toriginalMetadata.caption !== undefined &&\n\t\tupdatedMetadata.caption !== undefined &&\n\t\toriginalMetadata.caption !== updatedMetadata.caption &&\n\t\t( normalizedCurrentCaption === originalMetadata.caption ||\n\t\t\t! normalizedCurrentCaption )\n\t) {\n\t\tsyncedAttributes.caption = updatedMetadata.caption || undefined;\n\t}\n\n\treturn syncedAttributes;\n}\n\nconst { openMediaEditorModalKey } = unlock( blockEditorPrivateApis );\n// Caption sync needs `caption.raw`; view/default attachment records can contain\n// only rendered caption data or be tied to an in-flight stale resolution.\nconst ATTACHMENT_EDIT_QUERY = { context: 'edit' };\n\nfunction getAttachmentFallbackForEmptyBlockMetadata( { alt, caption } ) {\n\tconst attachment = {};\n\n\tif ( ! alt ) {\n\t\tattachment.alt_text = '';\n\t}\n\n\tif ( ! caption?.toString() ) {\n\t\tattachment.caption = '';\n\t}\n\n\treturn Object.keys( attachment ).length ? attachment : undefined;\n}\n\nfunction hasKnownAttachmentMetadata( attachment ) {\n\tif ( ! attachment ) {\n\t\treturn false;\n\t}\n\n\tconst hasKnownAlt =\n\t\ttypeof attachment.alt_text === 'string' ||\n\t\ttypeof attachment.alt === 'string';\n\tconst hasKnownCaption =\n\t\tgetImageBlockMetadataFromAttachment( attachment ).caption !== undefined;\n\n\treturn hasKnownAlt && hasKnownCaption;\n}\n\nexport function useOpenImageMediaEditorModal( { attributes, setAttributes } ) {\n\t// Keep this hook private to the Image block and pass the block attributes\n\t// object so the callsite stays compact. Destructure only the attributes\n\t// currently used for metadata sync; add more here if the sync policy grows.\n\tconst { id, url, alt, caption } = attributes;\n\tconst registry = useRegistry();\n\tconst openMediaEditorModal = useSelect(\n\t\t( select ) =>\n\t\t\tselect( blockEditorStore ).getSettings()[ openMediaEditorModalKey ],\n\t\t[]\n\t);\n\t// Track the block's current alt and caption in a ref so handleMediaUpdate\n\t// can read the latest values without being listed as a dependency (which\n\t// would recreate the callback and re-register the onUpdate handler on every\n\t// keystroke while the modal is open).\n\tconst blockMetadataRef = useRef( { alt, caption: caption?.toString() } );\n\t// Snapshot of the attachment's metadata taken just before the modal opens,\n\t// used as the baseline for detecting what changed during the editing session.\n\tconst mediaEditorMetadataBaselineRef = useRef();\n\t// Incremented on every handleMediaUpdate call; stale async continuations\n\t// check against this to bail out if a newer update has since started.\n\tconst mediaEditorMetadataSyncRequestRef = useRef( 0 );\n\n\tuseEffect( () => {\n\t\tblockMetadataRef.current = { alt, caption: caption?.toString() };\n\t}, [ alt, caption ] );\n\n\tconst getCachedAttachmentRecord = useCallback(\n\t\t( attachmentId ) => {\n\t\t\tconst { getEditedEntityRecord, getEntityRecord } =\n\t\t\t\tregistry.select( coreStore );\n\t\t\treturn (\n\t\t\t\tgetEditedEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'attachment',\n\t\t\t\t\tattachmentId\n\t\t\t\t) ||\n\t\t\t\tgetEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'attachment',\n\t\t\t\t\tattachmentId,\n\t\t\t\t\tATTACHMENT_EDIT_QUERY\n\t\t\t\t) ||\n\t\t\t\tgetEntityRecord( 'postType', 'attachment', attachmentId )\n\t\t\t);\n\t\t},\n\t\t[ registry ]\n\t);\n\n\tconst resolveAttachmentRecord = useCallback(\n\t\tasync ( attachmentId ) => {\n\t\t\tconst resolveSelect = registry.resolveSelect( coreStore );\n\n\t\t\ttry {\n\t\t\t\treturn (\n\t\t\t\t\t( await resolveSelect.getEntityRecord(\n\t\t\t\t\t\t'postType',\n\t\t\t\t\t\t'attachment',\n\t\t\t\t\t\tattachmentId,\n\t\t\t\t\t\tATTACHMENT_EDIT_QUERY\n\t\t\t\t\t) ) ||\n\t\t\t\t\t( await resolveSelect.getEntityRecord(\n\t\t\t\t\t\t'postType',\n\t\t\t\t\t\t'attachment',\n\t\t\t\t\t\tattachmentId\n\t\t\t\t\t) )\n\t\t\t\t);\n\t\t\t} catch {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t},\n\t\t[ registry ]\n\t);\n\n\tconst resolveFreshAttachmentRecord = useCallback(\n\t\tasync ( attachmentId ) => {\n\t\t\t// Bust cached records so resolveAttachmentRecord fetches the\n\t\t\t// server state that reflects the media editor's saved changes.\n\t\t\tconst { invalidateResolution } = registry.dispatch( coreStore );\n\n\t\t\tinvalidateResolution( 'getEntityRecord', [\n\t\t\t\t'postType',\n\t\t\t\t'attachment',\n\t\t\t\tattachmentId,\n\t\t\t] );\n\t\t\tinvalidateResolution( 'getEntityRecord', [\n\t\t\t\t'postType',\n\t\t\t\t'attachment',\n\t\t\t\tattachmentId,\n\t\t\t\tATTACHMENT_EDIT_QUERY,\n\t\t\t] );\n\t\t\treturn resolveAttachmentRecord( attachmentId );\n\t\t},\n\t\t[ registry, resolveAttachmentRecord ]\n\t);\n\n\tconst handleMediaUpdate = useCallback(\n\t\tasync ( { id: newId, url: newUrl } ) => {\n\t\t\tif ( typeof newId !== 'number' ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Capture and clear the baseline so a rapid second save doesn't\n\t\t\t// reuse a stale snapshot.\n\t\t\tconst originalAttachment = mediaEditorMetadataBaselineRef.current;\n\t\t\tmediaEditorMetadataBaselineRef.current = undefined;\n\t\t\tconst syncRequest = ++mediaEditorMetadataSyncRequestRef.current;\n\t\t\tconst nextAttributes = {};\n\n\t\t\tif ( newId !== id ) {\n\t\t\t\tnextAttributes.id = newId;\n\t\t\t\tnextAttributes.url = newUrl ?? url;\n\t\t\t}\n\n\t\t\tif ( originalAttachment ) {\n\t\t\t\t// Fetch fresh server state so the comparison reflects what\n\t\t\t\t// the media editor actually saved, not a potentially stale\n\t\t\t\t// cache.\n\t\t\t\tconst resolvedAttachment =\n\t\t\t\t\tawait resolveFreshAttachmentRecord( newId );\n\n\t\t\t\t// A newer update started while we were awaiting; discard\n\t\t\t\t// this one.\n\t\t\t\tif (\n\t\t\t\t\tsyncRequest !== mediaEditorMetadataSyncRequestRef.current\n\t\t\t\t) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t// Sync alt text and caption back to the block only when\n\t\t\t\t// they were changed in the media editor. Fields the user\n\t\t\t\t// has independently customised on the block (i.e. values\n\t\t\t\t// that don't match the pre-session attachment metadata)\n\t\t\t\t// are left untouched.\n\t\t\t\tconst resolvedMetadataAttributes =\n\t\t\t\t\tgetSyncedImageBlockAttributes(\n\t\t\t\t\t\tblockMetadataRef.current,\n\t\t\t\t\t\toriginalAttachment,\n\t\t\t\t\t\tresolvedAttachment\n\t\t\t\t\t);\n\n\t\t\t\tif ( Object.keys( resolvedMetadataAttributes ).length ) {\n\t\t\t\t\tObject.assign( nextAttributes, resolvedMetadataAttributes );\n\t\t\t\t\tblockMetadataRef.current = {\n\t\t\t\t\t\t...blockMetadataRef.current,\n\t\t\t\t\t\t...resolvedMetadataAttributes,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif ( Object.keys( nextAttributes ).length ) {\n\t\t\t\tsetAttributes( nextAttributes );\n\t\t\t}\n\t\t},\n\t\t[ id, resolveFreshAttachmentRecord, setAttributes, url ]\n\t);\n\n\tconst openImageMediaEditorModal = useCallback( async () => {\n\t\tif ( ! id || ! openMediaEditorModal ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Snapshot the attachment's current metadata before the user makes\n\t\t// any changes so handleMediaUpdate can compare against it later.\n\t\t// Prefer a freshly resolved edit-context record for accuracy; fall\n\t\t// back to whatever is in the cache, or a minimal object derived from\n\t\t// the block's own attributes when nothing is cached yet.\n\t\tconst cachedAttachmentRecord = getCachedAttachmentRecord( id );\n\t\tconst fallbackAttachmentRecord =\n\t\t\tgetAttachmentFallbackForEmptyBlockMetadata(\n\t\t\t\tblockMetadataRef.current\n\t\t\t);\n\t\tconst resolvedAttachmentRecord = hasKnownAttachmentMetadata(\n\t\t\tcachedAttachmentRecord\n\t\t)\n\t\t\t? undefined\n\t\t\t: await resolveAttachmentRecord( id );\n\n\t\tmediaEditorMetadataBaselineRef.current =\n\t\t\tresolvedAttachmentRecord ||\n\t\t\t( hasKnownAttachmentMetadata( cachedAttachmentRecord )\n\t\t\t\t? cachedAttachmentRecord\n\t\t\t\t: fallbackAttachmentRecord ) ||\n\t\t\tcachedAttachmentRecord;\n\n\t\topenMediaEditorModal( {\n\t\t\tid,\n\t\t\tonUpdate: handleMediaUpdate,\n\t\t} );\n\t}, [\n\t\tgetCachedAttachmentRecord,\n\t\thandleMediaUpdate,\n\t\tid,\n\t\topenMediaEditorModal,\n\t\tresolveAttachmentRecord,\n\t] );\n\n\treturn id && openMediaEditorModal ? openImageMediaEditorModal : undefined;\n}\n"],
|
|
5
|
-
"mappings": ";AAGA,SAAS,SAAS,iBAAiB;AACnC;AAAA,EACC,eAAe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ACA,MACC,iBAAiB,YAAY,UAC7B,gBAAgB,YAAY,UAC5B,iBAAiB,YAAY,gBAAgB,YAC3C,6BAA6B,iBAAiB,WAC/C,CAAE,2BACF;AACD,qBAAiB,UAAU,gBAAgB,WAAW;AAAA,EACvD;AAEA,SAAO;AACR;AAEA,IAAM,EAAE,wBAAwB,IAAI,OAAQ,sBAAuB;AAGnE,IAAM,wBAAwB,EAAE,SAAS,OAAO;AAEhD,SAAS,2CAA4C,EAAE,KAAK,QAAQ,GAAI;AACvE,QAAM,aAAa,CAAC;AAEpB,MAAK,CAAE,KAAM;AACZ,eAAW,WAAW;AAAA,EACvB;AAEA,MAAK,CAAE,SAAS,SAAS,GAAI;AAC5B,eAAW,UAAU;AAAA,EACtB;AAEA,SAAO,OAAO,KAAM,UAAW,EAAE,SAAS,aAAa;AACxD;AAEA,SAAS,2BAA4B,YAAa;AACjD,MAAK,CAAE,YAAa;AACnB,WAAO;AAAA,EACR;AAEA,QAAM,cACL,OAAO,WAAW,aAAa,YAC/B,OAAO,WAAW,QAAQ;AAC3B,QAAM,kBACL,oCAAqC,UAAW,EAAE,YAAY;AAE/D,SAAO,eAAe;AACvB;AAEO,SAAS,6BAA8B,EAAE,YAAY,cAAc,GAAI;AAI7E,QAAM,EAAE,IAAI,KAAK,KAAK,QAAQ,IAAI;AAClC,QAAM,WAAW,YAAY;AAC7B,QAAM,uBAAuB;AAAA,IAC5B,CAAE,WACD,OAAQ,gBAAiB,EAAE,YAAY,EAAG,uBAAwB;AAAA,IACnE,CAAC;AAAA,EACF;AAKA,QAAM,
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { store as coreStore } from '@wordpress/core-data';\nimport {\n\tprivateApis as blockEditorPrivateApis,\n\tstore as blockEditorStore,\n} from '@wordpress/block-editor';\nimport { __unstableStripHTML as stripHTML } from '@wordpress/dom';\nimport { useRegistry, useSelect } from '@wordpress/data';\nimport { useCallback, useEffect, useRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { unlock } from '../lock-unlock';\n\nfunction normalizeImageBlockCaption( caption ) {\n\tif ( typeof caption !== 'string' ) {\n\t\treturn '';\n\t}\n\n\tconst textContent = stripHTML( caption ).trim();\n\n\tif ( ! textContent ) {\n\t\treturn '';\n\t}\n\n\treturn caption.replace( /\\n/g, '<br>' );\n}\n\nfunction getAttachmentCaption( attachment ) {\n\tconst caption = attachment?.caption;\n\n\tif ( typeof caption === 'string' ) {\n\t\treturn normalizeImageBlockCaption( caption );\n\t}\n\n\tif (\n\t\tcaption &&\n\t\ttypeof caption === 'object' &&\n\t\tObject.hasOwn( caption, 'raw' )\n\t) {\n\t\treturn normalizeImageBlockCaption( caption.raw );\n\t}\n\n\treturn undefined;\n}\n\nexport function getImageBlockMetadataFromAttachment( attachment ) {\n\treturn {\n\t\talt:\n\t\t\ttypeof attachment?.alt_text === 'string'\n\t\t\t\t? attachment.alt_text\n\t\t\t\t: attachment?.alt || '',\n\t\tcaption: getAttachmentCaption( attachment ),\n\t};\n}\n\nfunction normalizeMetadataAttribute( value ) {\n\treturn value || '';\n}\n\nexport function getSyncedImageBlockAttributes(\n\tcurrentAttributes,\n\toriginalAttachment,\n\tupdatedAttachment\n) {\n\tif ( ! originalAttachment || ! updatedAttachment ) {\n\t\treturn {};\n\t}\n\n\tconst originalMetadata =\n\t\tgetImageBlockMetadataFromAttachment( originalAttachment );\n\tconst updatedMetadata =\n\t\tgetImageBlockMetadataFromAttachment( updatedAttachment );\n\tconst syncedAttributes = {};\n\n\tconst normalizedCurrentAlt = normalizeMetadataAttribute(\n\t\tcurrentAttributes.alt\n\t);\n\tif (\n\t\toriginalMetadata.alt !== updatedMetadata.alt &&\n\t\t( normalizedCurrentAlt === originalMetadata.alt ||\n\t\t\t! normalizedCurrentAlt )\n\t) {\n\t\tsyncedAttributes.alt = updatedMetadata.alt;\n\t}\n\n\tconst normalizedCurrentCaption = normalizeMetadataAttribute(\n\t\tcurrentAttributes.caption\n\t);\n\tif (\n\t\toriginalMetadata.caption !== undefined &&\n\t\tupdatedMetadata.caption !== undefined &&\n\t\toriginalMetadata.caption !== updatedMetadata.caption &&\n\t\t( normalizedCurrentCaption === originalMetadata.caption ||\n\t\t\t! normalizedCurrentCaption )\n\t) {\n\t\tsyncedAttributes.caption = updatedMetadata.caption || undefined;\n\t}\n\n\treturn syncedAttributes;\n}\n\nconst { openMediaEditorModalKey } = unlock( blockEditorPrivateApis );\n// Caption sync needs `caption.raw`; view/default attachment records can contain\n// only rendered caption data or be tied to an in-flight stale resolution.\nconst ATTACHMENT_EDIT_QUERY = { context: 'edit' };\n\nfunction getAttachmentFallbackForEmptyBlockMetadata( { alt, caption } ) {\n\tconst attachment = {};\n\n\tif ( ! alt ) {\n\t\tattachment.alt_text = '';\n\t}\n\n\tif ( ! caption?.toString() ) {\n\t\tattachment.caption = '';\n\t}\n\n\treturn Object.keys( attachment ).length ? attachment : undefined;\n}\n\nfunction hasKnownAttachmentMetadata( attachment ) {\n\tif ( ! attachment ) {\n\t\treturn false;\n\t}\n\n\tconst hasKnownAlt =\n\t\ttypeof attachment.alt_text === 'string' ||\n\t\ttypeof attachment.alt === 'string';\n\tconst hasKnownCaption =\n\t\tgetImageBlockMetadataFromAttachment( attachment ).caption !== undefined;\n\n\treturn hasKnownAlt && hasKnownCaption;\n}\n\nexport function useOpenImageMediaEditorModal( { attributes, setAttributes } ) {\n\t// Keep this hook private to the Image block and pass the block attributes\n\t// object so the callsite stays compact. Destructure only the attributes\n\t// currently used for metadata sync; add more here if the sync policy grows.\n\tconst { id, url, alt, caption } = attributes;\n\tconst registry = useRegistry();\n\tconst openMediaEditorModal = useSelect(\n\t\t( select ) =>\n\t\t\tselect( blockEditorStore ).getSettings()[ openMediaEditorModalKey ],\n\t\t[]\n\t);\n\t// Track the block's current attachment and metadata in a ref so\n\t// handleMediaUpdate can read the latest values without being listed as\n\t// dependencies (which would recreate the callback and re-register the\n\t// onUpdate handler on every block change while the modal is open).\n\tconst blockAttributesRef = useRef( {\n\t\tid,\n\t\turl,\n\t\talt,\n\t\tcaption: caption?.toString(),\n\t} );\n\t// Snapshot of the attachment's metadata taken just before the modal opens,\n\t// used as the baseline for detecting what changed during the editing session.\n\tconst mediaEditorMetadataBaselineRef = useRef();\n\t// Incremented on every handleMediaUpdate call; stale async continuations\n\t// check against this to bail out if a newer update has since started.\n\tconst mediaEditorMetadataSyncRequestRef = useRef( 0 );\n\n\tuseEffect( () => {\n\t\tblockAttributesRef.current = {\n\t\t\tid,\n\t\t\turl,\n\t\t\talt,\n\t\t\tcaption: caption?.toString(),\n\t\t};\n\t}, [ alt, caption, id, url ] );\n\n\tconst getCachedAttachmentRecord = useCallback(\n\t\t( attachmentId ) => {\n\t\t\tconst { getEditedEntityRecord, getEntityRecord } =\n\t\t\t\tregistry.select( coreStore );\n\t\t\treturn (\n\t\t\t\tgetEditedEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'attachment',\n\t\t\t\t\tattachmentId\n\t\t\t\t) ||\n\t\t\t\tgetEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'attachment',\n\t\t\t\t\tattachmentId,\n\t\t\t\t\tATTACHMENT_EDIT_QUERY\n\t\t\t\t) ||\n\t\t\t\tgetEntityRecord( 'postType', 'attachment', attachmentId )\n\t\t\t);\n\t\t},\n\t\t[ registry ]\n\t);\n\n\tconst resolveAttachmentRecord = useCallback(\n\t\tasync ( attachmentId ) => {\n\t\t\tconst resolveSelect = registry.resolveSelect( coreStore );\n\n\t\t\ttry {\n\t\t\t\treturn (\n\t\t\t\t\t( await resolveSelect.getEntityRecord(\n\t\t\t\t\t\t'postType',\n\t\t\t\t\t\t'attachment',\n\t\t\t\t\t\tattachmentId,\n\t\t\t\t\t\tATTACHMENT_EDIT_QUERY\n\t\t\t\t\t) ) ||\n\t\t\t\t\t( await resolveSelect.getEntityRecord(\n\t\t\t\t\t\t'postType',\n\t\t\t\t\t\t'attachment',\n\t\t\t\t\t\tattachmentId\n\t\t\t\t\t) )\n\t\t\t\t);\n\t\t\t} catch {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t},\n\t\t[ registry ]\n\t);\n\n\tconst resolveFreshAttachmentRecord = useCallback(\n\t\tasync ( attachmentId ) => {\n\t\t\t// Bust cached records so resolveAttachmentRecord fetches the\n\t\t\t// server state that reflects the media editor's saved changes.\n\t\t\tconst { invalidateResolution } = registry.dispatch( coreStore );\n\n\t\t\tinvalidateResolution( 'getEntityRecord', [\n\t\t\t\t'postType',\n\t\t\t\t'attachment',\n\t\t\t\tattachmentId,\n\t\t\t] );\n\t\t\tinvalidateResolution( 'getEntityRecord', [\n\t\t\t\t'postType',\n\t\t\t\t'attachment',\n\t\t\t\tattachmentId,\n\t\t\t\tATTACHMENT_EDIT_QUERY,\n\t\t\t] );\n\t\t\treturn resolveAttachmentRecord( attachmentId );\n\t\t},\n\t\t[ registry, resolveAttachmentRecord ]\n\t);\n\n\tconst handleMediaUpdate = useCallback(\n\t\tasync ( { id: newId, url: newUrl } ) => {\n\t\t\tif ( typeof newId !== 'number' ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Capture and clear the baseline so a rapid second save doesn't\n\t\t\t// reuse a stale snapshot.\n\t\t\tconst originalAttachment = mediaEditorMetadataBaselineRef.current;\n\t\t\tmediaEditorMetadataBaselineRef.current = undefined;\n\t\t\tconst syncRequest = ++mediaEditorMetadataSyncRequestRef.current;\n\t\t\tconst nextAttributes = {};\n\n\t\t\tconst currentBlockAttributes = blockAttributesRef.current;\n\n\t\t\tif ( newId !== currentBlockAttributes.id ) {\n\t\t\t\tnextAttributes.id = newId;\n\t\t\t\tnextAttributes.url = newUrl ?? currentBlockAttributes.url;\n\t\t\t\tblockAttributesRef.current = {\n\t\t\t\t\t...blockAttributesRef.current,\n\t\t\t\t\tid: nextAttributes.id,\n\t\t\t\t\turl: nextAttributes.url,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tif ( originalAttachment ) {\n\t\t\t\t// Fetch fresh server state so the comparison reflects what\n\t\t\t\t// the media editor actually saved, not a potentially stale\n\t\t\t\t// cache.\n\t\t\t\tconst resolvedAttachment =\n\t\t\t\t\tawait resolveFreshAttachmentRecord( newId );\n\n\t\t\t\t// A newer update started while we were awaiting; discard\n\t\t\t\t// this one.\n\t\t\t\tif (\n\t\t\t\t\tsyncRequest !== mediaEditorMetadataSyncRequestRef.current\n\t\t\t\t) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t// Sync alt text and caption back to the block only when\n\t\t\t\t// they were changed in the media editor. Fields the user\n\t\t\t\t// has independently customised on the block (i.e. values\n\t\t\t\t// that don't match the pre-session attachment metadata)\n\t\t\t\t// are left untouched.\n\t\t\t\tconst latestBlockAttributes = blockAttributesRef.current;\n\t\t\t\tconst resolvedMetadataAttributes =\n\t\t\t\t\tgetSyncedImageBlockAttributes(\n\t\t\t\t\t\tlatestBlockAttributes,\n\t\t\t\t\t\toriginalAttachment,\n\t\t\t\t\t\tresolvedAttachment\n\t\t\t\t\t);\n\n\t\t\t\tif ( Object.keys( resolvedMetadataAttributes ).length ) {\n\t\t\t\t\tObject.assign( nextAttributes, resolvedMetadataAttributes );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif ( Object.keys( nextAttributes ).length ) {\n\t\t\t\tblockAttributesRef.current = {\n\t\t\t\t\t...blockAttributesRef.current,\n\t\t\t\t\t...nextAttributes,\n\t\t\t\t};\n\t\t\t\tsetAttributes( nextAttributes );\n\t\t\t}\n\t\t},\n\t\t[ resolveFreshAttachmentRecord, setAttributes ]\n\t);\n\n\tconst openImageMediaEditorModal = useCallback( async () => {\n\t\tif ( ! id || ! openMediaEditorModal ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Snapshot the attachment's current metadata before the user makes\n\t\t// any changes so handleMediaUpdate can compare against it later.\n\t\t// Prefer a freshly resolved edit-context record for accuracy; fall\n\t\t// back to whatever is in the cache, or a minimal object derived from\n\t\t// the block's own attributes when nothing is cached yet.\n\t\tconst cachedAttachmentRecord = getCachedAttachmentRecord( id );\n\t\tconst fallbackAttachmentRecord =\n\t\t\tgetAttachmentFallbackForEmptyBlockMetadata(\n\t\t\t\tblockAttributesRef.current\n\t\t\t);\n\t\tconst resolvedAttachmentRecord = hasKnownAttachmentMetadata(\n\t\t\tcachedAttachmentRecord\n\t\t)\n\t\t\t? undefined\n\t\t\t: await resolveAttachmentRecord( id );\n\n\t\tmediaEditorMetadataBaselineRef.current =\n\t\t\tresolvedAttachmentRecord ||\n\t\t\t( hasKnownAttachmentMetadata( cachedAttachmentRecord )\n\t\t\t\t? cachedAttachmentRecord\n\t\t\t\t: fallbackAttachmentRecord ) ||\n\t\t\tcachedAttachmentRecord;\n\n\t\topenMediaEditorModal( {\n\t\t\tid,\n\t\t\tonUpdate: handleMediaUpdate,\n\t\t} );\n\t}, [\n\t\tgetCachedAttachmentRecord,\n\t\thandleMediaUpdate,\n\t\tid,\n\t\topenMediaEditorModal,\n\t\tresolveAttachmentRecord,\n\t] );\n\n\treturn id && openMediaEditorModal ? openImageMediaEditorModal : undefined;\n}\n"],
|
|
5
|
+
"mappings": ";AAGA,SAAS,SAAS,iBAAiB;AACnC;AAAA,EACC,eAAe;AAAA,EACf,SAAS;AAAA,OACH;AACP,SAAS,uBAAuB,iBAAiB;AACjD,SAAS,aAAa,iBAAiB;AACvC,SAAS,aAAa,WAAW,cAAc;AAK/C,SAAS,cAAc;AAEvB,SAAS,2BAA4B,SAAU;AAC9C,MAAK,OAAO,YAAY,UAAW;AAClC,WAAO;AAAA,EACR;AAEA,QAAM,cAAc,UAAW,OAAQ,EAAE,KAAK;AAE9C,MAAK,CAAE,aAAc;AACpB,WAAO;AAAA,EACR;AAEA,SAAO,QAAQ,QAAS,OAAO,MAAO;AACvC;AAEA,SAAS,qBAAsB,YAAa;AAC3C,QAAM,UAAU,YAAY;AAE5B,MAAK,OAAO,YAAY,UAAW;AAClC,WAAO,2BAA4B,OAAQ;AAAA,EAC5C;AAEA,MACC,WACA,OAAO,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AA2B,EAAE,QAAS;AACvD,iBAAO,OAAQ,gBAAgB,0BAA2B;AAAA,QAC3D;AAAA,MACD;AAEA,UAAK,OAAO,KAAM,cAAe,EAAE,QAAS;AAC3C,2BAAmB,UAAU;AAAA,UAC5B,GAAG,mBAAmB;AAAA,UACtB,GAAG;AAAA,QACJ;AACA,sBAAe,cAAe;AAAA,MAC/B;AAAA,IACD;AAAA,IACA,CAAE,8BAA8B,aAAc;AAAA,EAC/C;AAEA,QAAM,4BAA4B,YAAa,YAAY;AAC1D,QAAK,CAAE,MAAM,CAAE,sBAAuB;AACrC;AAAA,IACD;AAOA,UAAM,yBAAyB,0BAA2B,EAAG;AAC7D,UAAM,2BACL;AAAA,MACC,mBAAmB;AAAA,IACpB;AACD,UAAM,2BAA2B;AAAA,MAChC;AAAA,IACD,IACG,SACA,MAAM,wBAAyB,EAAG;AAErC,mCAA+B,UAC9B,6BACE,2BAA4B,sBAAuB,IAClD,yBACA,6BACH;AAED,yBAAsB;AAAA,MACrB;AAAA,MACA,UAAU;AAAA,IACX,CAAE;AAAA,EACH,GAAG;AAAA,IACF;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD,CAAE;AAEF,SAAO,MAAM,uBAAuB,4BAA4B;AACjE;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -5,11 +5,16 @@ import {
|
|
|
5
5
|
cloneBlock
|
|
6
6
|
} from "@wordpress/blocks";
|
|
7
7
|
import { useRef } from "@wordpress/element";
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
useRefEffect,
|
|
10
|
+
privateApis as composePrivateApis
|
|
11
|
+
} from "@wordpress/compose";
|
|
9
12
|
import { ENTER } from "@wordpress/keycodes";
|
|
10
13
|
import { useSelect, useDispatch } from "@wordpress/data";
|
|
11
14
|
import { store as blockEditorStore } from "@wordpress/block-editor";
|
|
12
15
|
import useOutdentListItem from "./use-outdent-list-item.mjs";
|
|
16
|
+
import { unlock } from "../../lock-unlock.mjs";
|
|
17
|
+
var { subscribeDelegatedListener } = unlock(composePrivateApis);
|
|
13
18
|
function useEnter(props) {
|
|
14
19
|
const { replaceBlocks, selectionChange } = useDispatch(blockEditorStore);
|
|
15
20
|
const { getBlock, getBlockRootClientId, getBlockIndex, getBlockName } = useSelect(blockEditorStore);
|
|
@@ -64,10 +69,12 @@ function useEnter(props) {
|
|
|
64
69
|
);
|
|
65
70
|
selectionChange(middle.clientId);
|
|
66
71
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
72
|
+
return subscribeDelegatedListener(
|
|
73
|
+
element,
|
|
74
|
+
"keydown",
|
|
75
|
+
onKeyDown,
|
|
76
|
+
true
|
|
77
|
+
);
|
|
71
78
|
}, []);
|
|
72
79
|
}
|
|
73
80
|
export {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/list-item/hooks/use-enter.js"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {\n\tcreateBlock,\n\tgetDefaultBlockName,\n\tcloneBlock,\n} from '@wordpress/blocks';\nimport { useRef } from '@wordpress/element';\nimport {
|
|
5
|
-
"mappings": ";AAGA;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,cAAc;AACvB,
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {\n\tcreateBlock,\n\tgetDefaultBlockName,\n\tcloneBlock,\n} from '@wordpress/blocks';\nimport { useRef } from '@wordpress/element';\nimport {\n\tuseRefEffect,\n\tprivateApis as composePrivateApis,\n} from '@wordpress/compose';\nimport { ENTER } from '@wordpress/keycodes';\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\n\n/**\n * Internal dependencies\n */\nimport useOutdentListItem from './use-outdent-list-item';\nimport { unlock } from '../../lock-unlock';\n\nconst { subscribeDelegatedListener } = unlock( composePrivateApis );\n\nexport default function useEnter( props ) {\n\tconst { replaceBlocks, selectionChange } = useDispatch( blockEditorStore );\n\tconst { getBlock, getBlockRootClientId, getBlockIndex, getBlockName } =\n\t\tuseSelect( blockEditorStore );\n\tconst propsRef = useRef( props );\n\tpropsRef.current = props;\n\tconst outdentListItem = useOutdentListItem();\n\treturn useRefEffect( ( element ) => {\n\t\tfunction onKeyDown( event ) {\n\t\t\tif ( event.defaultPrevented || event.keyCode !== ENTER ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst { content, clientId } = propsRef.current;\n\t\t\tif ( content.length ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tevent.preventDefault();\n\t\t\tconst canOutdent =\n\t\t\t\tgetBlockName(\n\t\t\t\t\tgetBlockRootClientId(\n\t\t\t\t\t\tgetBlockRootClientId( propsRef.current.clientId )\n\t\t\t\t\t)\n\t\t\t\t) === 'core/list-item';\n\t\t\tif ( canOutdent ) {\n\t\t\t\toutdentListItem();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\t// Here we are in top level list so we need to split.\n\t\t\tconst topParentListBlock = getBlock(\n\t\t\t\tgetBlockRootClientId( clientId )\n\t\t\t);\n\t\t\tconst blockIndex = getBlockIndex( clientId );\n\t\t\tconst head = cloneBlock( {\n\t\t\t\t...topParentListBlock,\n\t\t\t\tinnerBlocks: topParentListBlock.innerBlocks.slice(\n\t\t\t\t\t0,\n\t\t\t\t\tblockIndex\n\t\t\t\t),\n\t\t\t} );\n\t\t\tconst middle = createBlock( getDefaultBlockName() );\n\t\t\t// Last list item might contain a `list` block innerBlock\n\t\t\t// In that case append remaining innerBlocks blocks.\n\t\t\tconst after = [\n\t\t\t\t...( topParentListBlock.innerBlocks[ blockIndex ]\n\t\t\t\t\t.innerBlocks[ 0 ]?.innerBlocks || [] ),\n\t\t\t\t...topParentListBlock.innerBlocks.slice( blockIndex + 1 ),\n\t\t\t];\n\t\t\tconst tail = after.length\n\t\t\t\t? [\n\t\t\t\t\t\tcloneBlock( {\n\t\t\t\t\t\t\t...topParentListBlock,\n\t\t\t\t\t\t\tinnerBlocks: after,\n\t\t\t\t\t\t} ),\n\t\t\t\t ]\n\t\t\t\t: [];\n\t\t\treplaceBlocks(\n\t\t\t\ttopParentListBlock.clientId,\n\t\t\t\t[ head, middle, ...tail ],\n\t\t\t\t1\n\t\t\t);\n\t\t\t// We manually change the selection here because we are replacing\n\t\t\t// a different block than the selected one.\n\t\t\tselectionChange( middle.clientId );\n\t\t}\n\n\t\t// Capture phase so we run before writing-flow's ancestor-bubble\n\t\t// keydown handlers that gate on `event.defaultPrevented`.\n\t\treturn subscribeDelegatedListener(\n\t\t\telement,\n\t\t\t'keydown',\n\t\t\tonKeyDown,\n\t\t\ttrue\n\t\t);\n\t}, [] );\n}\n"],
|
|
5
|
+
"mappings": ";AAGA;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,cAAc;AACvB;AAAA,EACC;AAAA,EACA,eAAe;AAAA,OACT;AACP,SAAS,aAAa;AACtB,SAAS,WAAW,mBAAmB;AACvC,SAAS,SAAS,wBAAwB;AAK1C,OAAO,wBAAwB;AAC/B,SAAS,cAAc;AAEvB,IAAM,EAAE,2BAA2B,IAAI,OAAQ,kBAAmB;AAEnD,SAAR,SAA2B,OAAQ;AACzC,QAAM,EAAE,eAAe,gBAAgB,IAAI,YAAa,gBAAiB;AACzE,QAAM,EAAE,UAAU,sBAAsB,eAAe,aAAa,IACnE,UAAW,gBAAiB;AAC7B,QAAM,WAAW,OAAQ,KAAM;AAC/B,WAAS,UAAU;AACnB,QAAM,kBAAkB,mBAAmB;AAC3C,SAAO,aAAc,CAAE,YAAa;AACnC,aAAS,UAAW,OAAQ;AAC3B,UAAK,MAAM,oBAAoB,MAAM,YAAY,OAAQ;AACxD;AAAA,MACD;AACA,YAAM,EAAE,SAAS,SAAS,IAAI,SAAS;AACvC,UAAK,QAAQ,QAAS;AACrB;AAAA,MACD;AACA,YAAM,eAAe;AACrB,YAAM,aACL;AAAA,QACC;AAAA,UACC,qBAAsB,SAAS,QAAQ,QAAS;AAAA,QACjD;AAAA,MACD,MAAM;AACP,UAAK,YAAa;AACjB,wBAAgB;AAChB;AAAA,MACD;AAEA,YAAM,qBAAqB;AAAA,QAC1B,qBAAsB,QAAS;AAAA,MAChC;AACA,YAAM,aAAa,cAAe,QAAS;AAC3C,YAAM,OAAO,WAAY;AAAA,QACxB,GAAG;AAAA,QACH,aAAa,mBAAmB,YAAY;AAAA,UAC3C;AAAA,UACA;AAAA,QACD;AAAA,MACD,CAAE;AACF,YAAM,SAAS,YAAa,oBAAoB,CAAE;AAGlD,YAAM,QAAQ;AAAA,QACb,GAAK,mBAAmB,YAAa,UAAW,EAC9C,YAAa,CAAE,GAAG,eAAe,CAAC;AAAA,QACpC,GAAG,mBAAmB,YAAY,MAAO,aAAa,CAAE;AAAA,MACzD;AACA,YAAM,OAAO,MAAM,SAChB;AAAA,QACA,WAAY;AAAA,UACX,GAAG;AAAA,UACH,aAAa;AAAA,QACd,CAAE;AAAA,MACF,IACA,CAAC;AACJ;AAAA,QACC,mBAAmB;AAAA,QACnB,CAAE,MAAM,QAAQ,GAAG,IAAK;AAAA,QACxB;AAAA,MACD;AAGA,sBAAiB,OAAO,QAAS;AAAA,IAClC;AAIA,WAAO;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD,GAAG,CAAC,CAAE;AACP;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
// packages/block-library/src/list-item/hooks/use-space.js
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
useRefEffect,
|
|
4
|
+
privateApis as composePrivateApis
|
|
5
|
+
} from "@wordpress/compose";
|
|
3
6
|
import { SPACE, TAB } from "@wordpress/keycodes";
|
|
4
7
|
import { store as blockEditorStore } from "@wordpress/block-editor";
|
|
5
8
|
import { useSelect } from "@wordpress/data";
|
|
6
9
|
import useIndentListItem from "./use-indent-list-item.mjs";
|
|
7
10
|
import useOutdentListItem from "./use-outdent-list-item.mjs";
|
|
11
|
+
import { unlock } from "../../lock-unlock.mjs";
|
|
12
|
+
var { subscribeDelegatedListener } = unlock(composePrivateApis);
|
|
8
13
|
function useSpace(clientId) {
|
|
9
14
|
const { getSelectionStart, getSelectionEnd, getBlockIndex } = useSelect(blockEditorStore);
|
|
10
15
|
const indentListItem = useIndentListItem(clientId);
|
|
@@ -33,10 +38,12 @@ function useSpace(clientId) {
|
|
|
33
38
|
}
|
|
34
39
|
}
|
|
35
40
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
41
|
+
return subscribeDelegatedListener(
|
|
42
|
+
element,
|
|
43
|
+
"keydown",
|
|
44
|
+
onKeyDown,
|
|
45
|
+
true
|
|
46
|
+
);
|
|
40
47
|
},
|
|
41
48
|
[clientId, indentListItem]
|
|
42
49
|
);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/list-item/hooks/use-space.js"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {
|
|
5
|
-
"mappings": ";AAGA,
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {\n\tuseRefEffect,\n\tprivateApis as composePrivateApis,\n} from '@wordpress/compose';\nimport { SPACE, TAB } from '@wordpress/keycodes';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport useIndentListItem from './use-indent-list-item';\nimport useOutdentListItem from './use-outdent-list-item';\nimport { unlock } from '../../lock-unlock';\n\nconst { subscribeDelegatedListener } = unlock( composePrivateApis );\n\nexport default function useSpace( clientId ) {\n\tconst { getSelectionStart, getSelectionEnd, getBlockIndex } =\n\t\tuseSelect( blockEditorStore );\n\tconst indentListItem = useIndentListItem( clientId );\n\tconst outdentListItem = useOutdentListItem();\n\n\treturn useRefEffect(\n\t\t( element ) => {\n\t\t\tfunction onKeyDown( event ) {\n\t\t\t\tconst { keyCode, shiftKey, altKey, metaKey, ctrlKey } = event;\n\n\t\t\t\tif (\n\t\t\t\t\tevent.defaultPrevented ||\n\t\t\t\t\t( keyCode !== SPACE && keyCode !== TAB ) ||\n\t\t\t\t\t// Only override when no modifiers are pressed.\n\t\t\t\t\taltKey ||\n\t\t\t\t\tmetaKey ||\n\t\t\t\t\tctrlKey\n\t\t\t\t) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tconst selectionStart = getSelectionStart();\n\t\t\t\tconst selectionEnd = getSelectionEnd();\n\t\t\t\tif (\n\t\t\t\t\tselectionStart.offset === 0 &&\n\t\t\t\t\tselectionEnd.offset === 0\n\t\t\t\t) {\n\t\t\t\t\tif ( shiftKey ) {\n\t\t\t\t\t\t// Note that backspace behaviour in defined in onMerge.\n\t\t\t\t\t\tif ( keyCode === TAB ) {\n\t\t\t\t\t\t\tif ( outdentListItem() ) {\n\t\t\t\t\t\t\t\tevent.preventDefault();\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if ( getBlockIndex( clientId ) !== 0 ) {\n\t\t\t\t\t\tif ( indentListItem() ) {\n\t\t\t\t\t\t\tevent.preventDefault();\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Capture phase so we run before writing-flow's ancestor-bubble\n\t\t\t// keydown handlers that gate on `event.defaultPrevented`.\n\t\t\treturn subscribeDelegatedListener(\n\t\t\t\telement,\n\t\t\t\t'keydown',\n\t\t\t\tonKeyDown,\n\t\t\t\ttrue\n\t\t\t);\n\t\t},\n\t\t[ clientId, indentListItem ]\n\t);\n}\n"],
|
|
5
|
+
"mappings": ";AAGA;AAAA,EACC;AAAA,EACA,eAAe;AAAA,OACT;AACP,SAAS,OAAO,WAAW;AAC3B,SAA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