@wordpress/media-fields 0.13.2-next.v.202606191442.0 → 0.14.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/build/attached_to/edit.cjs +8 -5
- package/build/attached_to/edit.cjs.map +2 -2
- package/build-module/attached_to/edit.mjs +8 -5
- package/build-module/attached_to/edit.mjs.map +2 -2
- package/build-types/attached_to/edit.d.ts.map +1 -1
- package/package.json +15 -15
- package/src/attached_to/edit.tsx +22 -19
package/CHANGELOG.md
CHANGED
|
@@ -60,6 +60,7 @@ function MediaAttachedToEdit({
|
|
|
60
60
|
post: 0,
|
|
61
61
|
_embedded: { ...data?._embedded, "wp:attached-to": void 0 }
|
|
62
62
|
});
|
|
63
|
+
setValue(null);
|
|
63
64
|
setOptions([]);
|
|
64
65
|
};
|
|
65
66
|
const onValueChange = async (filterValue) => {
|
|
@@ -74,13 +75,14 @@ function MediaAttachedToEdit({
|
|
|
74
75
|
{}
|
|
75
76
|
);
|
|
76
77
|
setSearchResults(results);
|
|
77
|
-
const
|
|
78
|
+
const suggestions = results.map((result) => {
|
|
78
79
|
return {
|
|
79
80
|
label: result.title,
|
|
80
81
|
value: result.id.toString()
|
|
81
82
|
};
|
|
82
83
|
});
|
|
83
|
-
|
|
84
|
+
const includeCurrent = !filterValue && suggestions.findIndex((s) => s.value === value) === -1;
|
|
85
|
+
setOptions(suggestions.concat(includeCurrent ? defaultPost : []));
|
|
84
86
|
setIsLoading(false);
|
|
85
87
|
};
|
|
86
88
|
const handleSelectOption = (selectedPostId) => {
|
|
@@ -115,7 +117,7 @@ function MediaAttachedToEdit({
|
|
|
115
117
|
}
|
|
116
118
|
}
|
|
117
119
|
};
|
|
118
|
-
const help =
|
|
120
|
+
const help = (0, import_element.createInterpolateElement)(
|
|
119
121
|
(0, import_i18n.__)(
|
|
120
122
|
"Search for a post or page to attach this media to or <button>detach current</button>."
|
|
121
123
|
),
|
|
@@ -126,11 +128,12 @@ function MediaAttachedToEdit({
|
|
|
126
128
|
__next40pxDefaultSize: true,
|
|
127
129
|
onClick: handleDetach,
|
|
128
130
|
variant: "link",
|
|
129
|
-
accessibleWhenDisabled: true
|
|
131
|
+
accessibleWhenDisabled: true,
|
|
132
|
+
disabled: !value
|
|
130
133
|
}
|
|
131
134
|
)
|
|
132
135
|
}
|
|
133
|
-
)
|
|
136
|
+
);
|
|
134
137
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
135
138
|
import_components.ComboboxControl,
|
|
136
139
|
{
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/attached_to/edit.tsx"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {\n\t__experimentalFetchLinkSuggestions as fetchLinkSuggestions,\n\tstore as coreStore,\n} from '@wordpress/core-data';\nimport { Button, ComboboxControl } from '@wordpress/components';\nimport { __ } from '@wordpress/i18n';\nimport { useState, createInterpolateElement } from '@wordpress/element';\nimport { debounce } from '@wordpress/compose';\nimport { useSelect } from '@wordpress/data';\nimport type { DataFormControlProps } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport type { MediaItem } from '../types';\nimport { getRenderedContent } from '../utils/get-rendered-content';\n\nexport type SearchResult = {\n\t/**\n\t * Post or term id.\n\t */\n\tid: number;\n\t/**\n\t * Link url.\n\t */\n\turl: string;\n\t/**\n\t * Title of the link.\n\t */\n\ttitle: string;\n\t/**\n\t * The taxonomy or post type slug or type URL.\n\t */\n\ttype: string;\n\t/**\n\t * Link kind of post-type or taxonomy\n\t */\n\tkind?: string;\n};\n\nexport default function MediaAttachedToEdit( {\n\tdata,\n\tonChange,\n}: DataFormControlProps< MediaItem > ) {\n\tconst defaultPost =\n\t\t!! data.post && !! data?._embedded?.[ 'wp:attached-to' ]?.[ 0 ]\n\t\t\t? [\n\t\t\t\t\t{\n\t\t\t\t\t\tlabel: getRenderedContent(\n\t\t\t\t\t\t\tdata._embedded?.[ 'wp:attached-to' ]?.[ 0 ]?.title\n\t\t\t\t\t\t),\n\t\t\t\t\t\tvalue: data.post.toString(),\n\t\t\t\t\t},\n\t\t\t ]\n\t\t\t: [];\n\tconst [ options, setOptions ] =\n\t\tuseState< { label: string; value: string }[] >( defaultPost );\n\tconst [ searchResults, setSearchResults ] = useState< SearchResult[] >(\n\t\t[]\n\t);\n\tconst [ isLoading, setIsLoading ] = useState( false );\n\tconst [ value, setValue ] = useState< string | null >(\n\t\tdata?.post?.toString() ?? null\n\t);\n\n\tconst postTypes = useSelect(\n\t\t( select ) => select( coreStore ).getPostTypes(),\n\t\t[]\n\t);\n\tconst handleDetach = () => {\n\t\tonChange( {\n\t\t\tpost: 0,\n\t\t\t_embedded: { ...data?._embedded, 'wp:attached-to': undefined },\n\t\t} );\n\t\tsetOptions( [] );\n\t};\n\n\tconst onValueChange = async ( filterValue: string ) => {\n\t\tsetIsLoading( true );\n\t\tconst results = await fetchLinkSuggestions(\n\t\t\tfilterValue,\n\t\t\t/*\n\t\t\t * @TODO `fetchLinkSuggestions()` should accept `perPage` as an option argument.\n\t\t\t * `isInitialSuggestions` limits the result to 3, otherwise it's hardcoded to 20.\n\t\t\t */\n\t\t\t{ type: 'post', isInitialSuggestions: true },\n\t\t\t{}\n\t\t);\n\t\tsetSearchResults( results );\n\t\tconst
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,uBAGO;AACP,wBAAwC;AACxC,kBAAmB;AACnB,qBAAmD;AACnD,qBAAyB;AACzB,kBAA0B;AAO1B,kCAAmC;
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {\n\t__experimentalFetchLinkSuggestions as fetchLinkSuggestions,\n\tstore as coreStore,\n} from '@wordpress/core-data';\nimport { Button, ComboboxControl } from '@wordpress/components';\nimport { __ } from '@wordpress/i18n';\nimport { useState, createInterpolateElement } from '@wordpress/element';\nimport { debounce } from '@wordpress/compose';\nimport { useSelect } from '@wordpress/data';\nimport type { DataFormControlProps } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport type { MediaItem } from '../types';\nimport { getRenderedContent } from '../utils/get-rendered-content';\n\nexport type SearchResult = {\n\t/**\n\t * Post or term id.\n\t */\n\tid: number;\n\t/**\n\t * Link url.\n\t */\n\turl: string;\n\t/**\n\t * Title of the link.\n\t */\n\ttitle: string;\n\t/**\n\t * The taxonomy or post type slug or type URL.\n\t */\n\ttype: string;\n\t/**\n\t * Link kind of post-type or taxonomy\n\t */\n\tkind?: string;\n};\n\nexport default function MediaAttachedToEdit( {\n\tdata,\n\tonChange,\n}: DataFormControlProps< MediaItem > ) {\n\tconst defaultPost =\n\t\t!! data.post && !! data?._embedded?.[ 'wp:attached-to' ]?.[ 0 ]\n\t\t\t? [\n\t\t\t\t\t{\n\t\t\t\t\t\tlabel: getRenderedContent(\n\t\t\t\t\t\t\tdata._embedded?.[ 'wp:attached-to' ]?.[ 0 ]?.title\n\t\t\t\t\t\t),\n\t\t\t\t\t\tvalue: data.post.toString(),\n\t\t\t\t\t},\n\t\t\t ]\n\t\t\t: [];\n\tconst [ options, setOptions ] =\n\t\tuseState< { label: string; value: string }[] >( defaultPost );\n\tconst [ searchResults, setSearchResults ] = useState< SearchResult[] >(\n\t\t[]\n\t);\n\tconst [ isLoading, setIsLoading ] = useState( false );\n\tconst [ value, setValue ] = useState< string | null >(\n\t\tdata?.post?.toString() ?? null\n\t);\n\n\tconst postTypes = useSelect(\n\t\t( select ) => select( coreStore ).getPostTypes(),\n\t\t[]\n\t);\n\tconst handleDetach = () => {\n\t\tonChange( {\n\t\t\tpost: 0,\n\t\t\t_embedded: { ...data?._embedded, 'wp:attached-to': undefined },\n\t\t} );\n\t\tsetValue( null );\n\t\tsetOptions( [] );\n\t};\n\n\tconst onValueChange = async ( filterValue: string ) => {\n\t\tsetIsLoading( true );\n\t\tconst results = await fetchLinkSuggestions(\n\t\t\tfilterValue,\n\t\t\t/*\n\t\t\t * @TODO `fetchLinkSuggestions()` should accept `perPage` as an option argument.\n\t\t\t * `isInitialSuggestions` limits the result to 3, otherwise it's hardcoded to 20.\n\t\t\t */\n\t\t\t{ type: 'post', isInitialSuggestions: true },\n\t\t\t{}\n\t\t);\n\t\tsetSearchResults( results );\n\t\tconst suggestions = results.map( ( result ) => {\n\t\t\treturn {\n\t\t\t\tlabel: result.title,\n\t\t\t\tvalue: result.id.toString(),\n\t\t\t};\n\t\t} );\n\t\tconst includeCurrent =\n\t\t\t! filterValue &&\n\t\t\tsuggestions.findIndex( ( s ) => s.value === value ) === -1;\n\t\tsetOptions( suggestions.concat( includeCurrent ? defaultPost : [] ) );\n\t\tsetIsLoading( false );\n\t};\n\n\t/**\n\t * Handle selection.\n\t *\n\t * @param {Object} selectedPostId The selected post id.\n\t */\n\tconst handleSelectOption = (\n\t\tselectedPostId: string | null | undefined\n\t) => {\n\t\tif ( ! selectedPostId ) {\n\t\t\thandleDetach();\n\t\t\treturn;\n\t\t}\n\t\tsetValue( selectedPostId );\n\t\tif ( selectedPostId ) {\n\t\t\tconst selectedPost = searchResults.find(\n\t\t\t\t( result ) => result.id === Number( selectedPostId )\n\t\t\t);\n\t\t\t// Although unlikely, it's technically possible for selectedPost to not be found.\n\t\t\t// E.g. if the user selects an option just as new search results are loaded.\n\t\t\t// TODO: Add error handling for when selectedPost is not found.\n\t\t\tif ( selectedPost && postTypes ) {\n\t\t\t\tconst postType = postTypes.find(\n\t\t\t\t\t( _postType: { slug: string } ) =>\n\t\t\t\t\t\t_postType.slug === selectedPost?.type\n\t\t\t\t);\n\n\t\t\t\tconst attachedTo = {\n\t\t\t\t\t...( postType && { type: postType.slug } ),\n\t\t\t\t\tid: Number( selectedPostId ),\n\t\t\t\t\ttitle: {\n\t\t\t\t\t\traw: selectedPost.title,\n\t\t\t\t\t\trendered: selectedPost.title,\n\t\t\t\t\t},\n\t\t\t\t};\n\n\t\t\t\tonChange( {\n\t\t\t\t\tpost: Number( selectedPostId ),\n\t\t\t\t\t_embedded: {\n\t\t\t\t\t\t...data?._embedded,\n\t\t\t\t\t\t'wp:attached-to': [ attachedTo ],\n\t\t\t\t\t},\n\t\t\t\t} );\n\t\t\t}\n\t\t}\n\t};\n\n\tconst help = createInterpolateElement(\n\t\t__(\n\t\t\t'Search for a post or page to attach this media to or <button>detach current</button>.'\n\t\t),\n\t\t{\n\t\t\tbutton: (\n\t\t\t\t<Button\n\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\tonClick={ handleDetach }\n\t\t\t\t\tvariant=\"link\"\n\t\t\t\t\taccessibleWhenDisabled\n\t\t\t\t\tdisabled={ ! value }\n\t\t\t\t/>\n\t\t\t),\n\t\t}\n\t);\n\n\treturn (\n\t\t<ComboboxControl\n\t\t\tclassName=\"dataviews-media-field__attached-to\"\n\t\t\t__next40pxDefaultSize\n\t\t\tisLoading={ isLoading }\n\t\t\tlabel={ __( 'Attached to' ) }\n\t\t\thelp={ help }\n\t\t\tvalue={ value }\n\t\t\toptions={ options }\n\t\t\tonFilterValueChange={ debounce(\n\t\t\t\t( filterValue: unknown ) =>\n\t\t\t\t\tonValueChange( filterValue as string ),\n\t\t\t\t300\n\t\t\t) }\n\t\t\tonChange={ handleSelectOption }\n\t\t\thideLabelFromVision\n\t\t/>\n\t);\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,uBAGO;AACP,wBAAwC;AACxC,kBAAmB;AACnB,qBAAmD;AACnD,qBAAyB;AACzB,kBAA0B;AAO1B,kCAAmC;AA4I/B;AAnHW,SAAR,oBAAsC;AAAA,EAC5C;AAAA,EACA;AACD,GAAuC;AACtC,QAAM,cACL,CAAC,CAAE,KAAK,QAAQ,CAAC,CAAE,MAAM,YAAa,gBAAiB,IAAK,CAAE,IAC3D;AAAA,IACA;AAAA,MACC,WAAO;AAAA,QACN,KAAK,YA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@ function MediaAttachedToEdit({
|
|
|
39
39
|
post: 0,
|
|
40
40
|
_embedded: { ...data?._embedded, "wp:attached-to": void 0 }
|
|
41
41
|
});
|
|
42
|
+
setValue(null);
|
|
42
43
|
setOptions([]);
|
|
43
44
|
};
|
|
44
45
|
const onValueChange = async (filterValue) => {
|
|
@@ -53,13 +54,14 @@ function MediaAttachedToEdit({
|
|
|
53
54
|
{}
|
|
54
55
|
);
|
|
55
56
|
setSearchResults(results);
|
|
56
|
-
const
|
|
57
|
+
const suggestions = results.map((result) => {
|
|
57
58
|
return {
|
|
58
59
|
label: result.title,
|
|
59
60
|
value: result.id.toString()
|
|
60
61
|
};
|
|
61
62
|
});
|
|
62
|
-
|
|
63
|
+
const includeCurrent = !filterValue && suggestions.findIndex((s) => s.value === value) === -1;
|
|
64
|
+
setOptions(suggestions.concat(includeCurrent ? defaultPost : []));
|
|
63
65
|
setIsLoading(false);
|
|
64
66
|
};
|
|
65
67
|
const handleSelectOption = (selectedPostId) => {
|
|
@@ -94,7 +96,7 @@ function MediaAttachedToEdit({
|
|
|
94
96
|
}
|
|
95
97
|
}
|
|
96
98
|
};
|
|
97
|
-
const help =
|
|
99
|
+
const help = createInterpolateElement(
|
|
98
100
|
__(
|
|
99
101
|
"Search for a post or page to attach this media to or <button>detach current</button>."
|
|
100
102
|
),
|
|
@@ -105,11 +107,12 @@ function MediaAttachedToEdit({
|
|
|
105
107
|
__next40pxDefaultSize: true,
|
|
106
108
|
onClick: handleDetach,
|
|
107
109
|
variant: "link",
|
|
108
|
-
accessibleWhenDisabled: true
|
|
110
|
+
accessibleWhenDisabled: true,
|
|
111
|
+
disabled: !value
|
|
109
112
|
}
|
|
110
113
|
)
|
|
111
114
|
}
|
|
112
|
-
)
|
|
115
|
+
);
|
|
113
116
|
return /* @__PURE__ */ jsx(
|
|
114
117
|
ComboboxControl,
|
|
115
118
|
{
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/attached_to/edit.tsx"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {\n\t__experimentalFetchLinkSuggestions as fetchLinkSuggestions,\n\tstore as coreStore,\n} from '@wordpress/core-data';\nimport { Button, ComboboxControl } from '@wordpress/components';\nimport { __ } from '@wordpress/i18n';\nimport { useState, createInterpolateElement } from '@wordpress/element';\nimport { debounce } from '@wordpress/compose';\nimport { useSelect } from '@wordpress/data';\nimport type { DataFormControlProps } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport type { MediaItem } from '../types';\nimport { getRenderedContent } from '../utils/get-rendered-content';\n\nexport type SearchResult = {\n\t/**\n\t * Post or term id.\n\t */\n\tid: number;\n\t/**\n\t * Link url.\n\t */\n\turl: string;\n\t/**\n\t * Title of the link.\n\t */\n\ttitle: string;\n\t/**\n\t * The taxonomy or post type slug or type URL.\n\t */\n\ttype: string;\n\t/**\n\t * Link kind of post-type or taxonomy\n\t */\n\tkind?: string;\n};\n\nexport default function MediaAttachedToEdit( {\n\tdata,\n\tonChange,\n}: DataFormControlProps< MediaItem > ) {\n\tconst defaultPost =\n\t\t!! data.post && !! data?._embedded?.[ 'wp:attached-to' ]?.[ 0 ]\n\t\t\t? [\n\t\t\t\t\t{\n\t\t\t\t\t\tlabel: getRenderedContent(\n\t\t\t\t\t\t\tdata._embedded?.[ 'wp:attached-to' ]?.[ 0 ]?.title\n\t\t\t\t\t\t),\n\t\t\t\t\t\tvalue: data.post.toString(),\n\t\t\t\t\t},\n\t\t\t ]\n\t\t\t: [];\n\tconst [ options, setOptions ] =\n\t\tuseState< { label: string; value: string }[] >( defaultPost );\n\tconst [ searchResults, setSearchResults ] = useState< SearchResult[] >(\n\t\t[]\n\t);\n\tconst [ isLoading, setIsLoading ] = useState( false );\n\tconst [ value, setValue ] = useState< string | null >(\n\t\tdata?.post?.toString() ?? null\n\t);\n\n\tconst postTypes = useSelect(\n\t\t( select ) => select( coreStore ).getPostTypes(),\n\t\t[]\n\t);\n\tconst handleDetach = () => {\n\t\tonChange( {\n\t\t\tpost: 0,\n\t\t\t_embedded: { ...data?._embedded, 'wp:attached-to': undefined },\n\t\t} );\n\t\tsetOptions( [] );\n\t};\n\n\tconst onValueChange = async ( filterValue: string ) => {\n\t\tsetIsLoading( true );\n\t\tconst results = await fetchLinkSuggestions(\n\t\t\tfilterValue,\n\t\t\t/*\n\t\t\t * @TODO `fetchLinkSuggestions()` should accept `perPage` as an option argument.\n\t\t\t * `isInitialSuggestions` limits the result to 3, otherwise it's hardcoded to 20.\n\t\t\t */\n\t\t\t{ type: 'post', isInitialSuggestions: true },\n\t\t\t{}\n\t\t);\n\t\tsetSearchResults( results );\n\t\tconst
|
|
5
|
-
"mappings": ";AAGA;AAAA,EACC,sCAAsC;AAAA,EACtC,SAAS;AAAA,OACH;AACP,SAAS,QAAQ,uBAAuB;AACxC,SAAS,UAAU;AACnB,SAAS,UAAU,gCAAgC;AACnD,SAAS,gBAAgB;AACzB,SAAS,iBAAiB;AAO1B,SAAS,0BAA0B;
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {\n\t__experimentalFetchLinkSuggestions as fetchLinkSuggestions,\n\tstore as coreStore,\n} from '@wordpress/core-data';\nimport { Button, ComboboxControl } from '@wordpress/components';\nimport { __ } from '@wordpress/i18n';\nimport { useState, createInterpolateElement } from '@wordpress/element';\nimport { debounce } from '@wordpress/compose';\nimport { useSelect } from '@wordpress/data';\nimport type { DataFormControlProps } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport type { MediaItem } from '../types';\nimport { getRenderedContent } from '../utils/get-rendered-content';\n\nexport type SearchResult = {\n\t/**\n\t * Post or term id.\n\t */\n\tid: number;\n\t/**\n\t * Link url.\n\t */\n\turl: string;\n\t/**\n\t * Title of the link.\n\t */\n\ttitle: string;\n\t/**\n\t * The taxonomy or post type slug or type URL.\n\t */\n\ttype: string;\n\t/**\n\t * Link kind of post-type or taxonomy\n\t */\n\tkind?: string;\n};\n\nexport default function MediaAttachedToEdit( {\n\tdata,\n\tonChange,\n}: DataFormControlProps< MediaItem > ) {\n\tconst defaultPost =\n\t\t!! data.post && !! data?._embedded?.[ 'wp:attached-to' ]?.[ 0 ]\n\t\t\t? [\n\t\t\t\t\t{\n\t\t\t\t\t\tlabel: getRenderedContent(\n\t\t\t\t\t\t\tdata._embedded?.[ 'wp:attached-to' ]?.[ 0 ]?.title\n\t\t\t\t\t\t),\n\t\t\t\t\t\tvalue: data.post.toString(),\n\t\t\t\t\t},\n\t\t\t ]\n\t\t\t: [];\n\tconst [ options, setOptions ] =\n\t\tuseState< { label: string; value: string }[] >( defaultPost );\n\tconst [ searchResults, setSearchResults ] = useState< SearchResult[] >(\n\t\t[]\n\t);\n\tconst [ isLoading, setIsLoading ] = useState( false );\n\tconst [ value, setValue ] = useState< string | null >(\n\t\tdata?.post?.toString() ?? null\n\t);\n\n\tconst postTypes = useSelect(\n\t\t( select ) => select( coreStore ).getPostTypes(),\n\t\t[]\n\t);\n\tconst handleDetach = () => {\n\t\tonChange( {\n\t\t\tpost: 0,\n\t\t\t_embedded: { ...data?._embedded, 'wp:attached-to': undefined },\n\t\t} );\n\t\tsetValue( null );\n\t\tsetOptions( [] );\n\t};\n\n\tconst onValueChange = async ( filterValue: string ) => {\n\t\tsetIsLoading( true );\n\t\tconst results = await fetchLinkSuggestions(\n\t\t\tfilterValue,\n\t\t\t/*\n\t\t\t * @TODO `fetchLinkSuggestions()` should accept `perPage` as an option argument.\n\t\t\t * `isInitialSuggestions` limits the result to 3, otherwise it's hardcoded to 20.\n\t\t\t */\n\t\t\t{ type: 'post', isInitialSuggestions: true },\n\t\t\t{}\n\t\t);\n\t\tsetSearchResults( results );\n\t\tconst suggestions = results.map( ( result ) => {\n\t\t\treturn {\n\t\t\t\tlabel: result.title,\n\t\t\t\tvalue: result.id.toString(),\n\t\t\t};\n\t\t} );\n\t\tconst includeCurrent =\n\t\t\t! filterValue &&\n\t\t\tsuggestions.findIndex( ( s ) => s.value === value ) === -1;\n\t\tsetOptions( suggestions.concat( includeCurrent ? defaultPost : [] ) );\n\t\tsetIsLoading( false );\n\t};\n\n\t/**\n\t * Handle selection.\n\t *\n\t * @param {Object} selectedPostId The selected post id.\n\t */\n\tconst handleSelectOption = (\n\t\tselectedPostId: string | null | undefined\n\t) => {\n\t\tif ( ! selectedPostId ) {\n\t\t\thandleDetach();\n\t\t\treturn;\n\t\t}\n\t\tsetValue( selectedPostId );\n\t\tif ( selectedPostId ) {\n\t\t\tconst selectedPost = searchResults.find(\n\t\t\t\t( result ) => result.id === Number( selectedPostId )\n\t\t\t);\n\t\t\t// Although unlikely, it's technically possible for selectedPost to not be found.\n\t\t\t// E.g. if the user selects an option just as new search results are loaded.\n\t\t\t// TODO: Add error handling for when selectedPost is not found.\n\t\t\tif ( selectedPost && postTypes ) {\n\t\t\t\tconst postType = postTypes.find(\n\t\t\t\t\t( _postType: { slug: string } ) =>\n\t\t\t\t\t\t_postType.slug === selectedPost?.type\n\t\t\t\t);\n\n\t\t\t\tconst attachedTo = {\n\t\t\t\t\t...( postType && { type: postType.slug } ),\n\t\t\t\t\tid: Number( selectedPostId ),\n\t\t\t\t\ttitle: {\n\t\t\t\t\t\traw: selectedPost.title,\n\t\t\t\t\t\trendered: selectedPost.title,\n\t\t\t\t\t},\n\t\t\t\t};\n\n\t\t\t\tonChange( {\n\t\t\t\t\tpost: Number( selectedPostId ),\n\t\t\t\t\t_embedded: {\n\t\t\t\t\t\t...data?._embedded,\n\t\t\t\t\t\t'wp:attached-to': [ attachedTo ],\n\t\t\t\t\t},\n\t\t\t\t} );\n\t\t\t}\n\t\t}\n\t};\n\n\tconst help = createInterpolateElement(\n\t\t__(\n\t\t\t'Search for a post or page to attach this media to or <button>detach current</button>.'\n\t\t),\n\t\t{\n\t\t\tbutton: (\n\t\t\t\t<Button\n\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\tonClick={ handleDetach }\n\t\t\t\t\tvariant=\"link\"\n\t\t\t\t\taccessibleWhenDisabled\n\t\t\t\t\tdisabled={ ! value }\n\t\t\t\t/>\n\t\t\t),\n\t\t}\n\t);\n\n\treturn (\n\t\t<ComboboxControl\n\t\t\tclassName=\"dataviews-media-field__attached-to\"\n\t\t\t__next40pxDefaultSize\n\t\t\tisLoading={ isLoading }\n\t\t\tlabel={ __( 'Attached to' ) }\n\t\t\thelp={ help }\n\t\t\tvalue={ value }\n\t\t\toptions={ options }\n\t\t\tonFilterValueChange={ debounce(\n\t\t\t\t( filterValue: unknown ) =>\n\t\t\t\t\tonValueChange( filterValue as string ),\n\t\t\t\t300\n\t\t\t) }\n\t\t\tonChange={ handleSelectOption }\n\t\t\thideLabelFromVision\n\t\t/>\n\t);\n}\n"],
|
|
5
|
+
"mappings": ";AAGA;AAAA,EACC,sCAAsC;AAAA,EACtC,SAAS;AAAA,OACH;AACP,SAAS,QAAQ,uBAAuB;AACxC,SAAS,UAAU;AACnB,SAAS,UAAU,gCAAgC;AACnD,SAAS,gBAAgB;AACzB,SAAS,iBAAiB;AAO1B,SAAS,0BAA0B;AA4I/B;AAnHW,SAAR,oBAAsC;AAAA,EAC5C;AAAA,EACA;AACD,GAAuC;AACtC,QAAM,cACL,CAAC,CAAE,KAAK,QAAQ,CAAC,CAAE,MAAM,YAAa,gBAAiB,IAAK,CAAE,IAC3D;AAAA,IACA;AAAA,MACC,OAAO;AAAA,QACN,KAAK,YAAa,gBAAiB,IAAK,CAAE,GAAG;AAAA,MAC9C;AAAA,MACA,OAAO,KAAK,KAAK,SAAS;AAAA,IAC3B;AAAA,EACA,IACA,CAAC;AACL,QAAM,CAAE,SAAS,UAAW,IAC3B,SAAgD,WAAY;AAC7D,QAAM,CAAE,eAAe,gBAAiB,IAAI;AAAA,IAC3C,CAAC;AAAA,EACF;AACA,QAAM,CAAE,WAAW,YAAa,IAAI,SAAU,KAAM;AACpD,QAAM,CAAE,OAAO,QAAS,IAAI;AAAA,IAC3B,MAAM,MAAM,SAAS,KAAK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,UAC3B,OAAO;AAAA,YACN,KAAK,aAAa;AAAA,YAClB,UAAU,aAAa;AAAA,UACxB;AAAA,QACD;AAEA,iBAAU;AAAA,UACT,MAAM,OAAQ,cAAe;AAAA,UAC7B,WAAW;AAAA,YACV,GAAG,MAAM;AAAA,YACT,kBAAkB,CAAE,UAAW;AAAA,UAChC;AAAA,QACD,CAAE;AAAA,MACH;AAAA,IACD;AAAA,EACD;AAEA,QAAM,OAAO;AAAA,IACZ;AAAA,MACC;AAAA,IACD;AAAA,IACA;AAAA,MACC,QACC;AAAA,QAAC;AAAA;AAAA,UACA,uBAAqB;AAAA,UACrB,SAAU;AAAA,UACV,SAAQ;AAAA,UACR,wBAAsB;AAAA,UACtB,UAAW,CAAE;AAAA;AAAA,MACd;AAAA,IAEF;AAAA,EACD;AAEA,SACC;AAAA,IAAC;AAAA;AAAA,MACA,WAAU;AAAA,MACV,uBAAqB;AAAA,MACrB;AAAA,MACA,OAAQ,GAAI,aAAc;AAAA,MAC1B;AAAA,MACA;AAAA,MACA;AAAA,MACA,qBAAsB;AAAA,QACrB,CAAE,gBACD,cAAe,WAAsB;AAAA,QACtC;AAAA,MACD;AAAA,MACA,UAAW;AAAA,MACX,qBAAmB;AAAA;AAAA,EACpB;AAEF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"edit.d.ts","sourceRoot":"","sources":["../../src/attached_to/edit.tsx"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAEjE;;GAEG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAG1C,MAAM,MAAM,YAAY,GAAG;IAC1B;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;IACX;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IACd;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,CAAC,OAAO,UAAU,mBAAmB,CAAE,EAC5C,IAAI,EACJ,QAAQ,EACR,EAAE,oBAAoB,CAAE,SAAS,CAAE,+
|
|
1
|
+
{"version":3,"file":"edit.d.ts","sourceRoot":"","sources":["../../src/attached_to/edit.tsx"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAEjE;;GAEG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAG1C,MAAM,MAAM,YAAY,GAAG;IAC1B;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;IACX;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IACd;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,CAAC,OAAO,UAAU,mBAAmB,CAAE,EAC5C,IAAI,EACJ,QAAQ,EACR,EAAE,oBAAoB,CAAE,SAAS,CAAE,+BA6InC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/media-fields",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.1",
|
|
4
4
|
"description": "Reusable field definitions for displaying and editing media attachment properties in WordPress.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -48,19 +48,19 @@
|
|
|
48
48
|
"src/**/*.scss"
|
|
49
49
|
],
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@wordpress/base-styles": "^10.
|
|
52
|
-
"@wordpress/components": "^
|
|
53
|
-
"@wordpress/compose": "^8.
|
|
54
|
-
"@wordpress/core-data": "^7.
|
|
55
|
-
"@wordpress/data": "^10.
|
|
56
|
-
"@wordpress/dataviews": "^17.0.1
|
|
57
|
-
"@wordpress/date": "^5.
|
|
58
|
-
"@wordpress/element": "^8.
|
|
59
|
-
"@wordpress/i18n": "^6.
|
|
60
|
-
"@wordpress/icons": "^
|
|
61
|
-
"@wordpress/primitives": "^4.
|
|
62
|
-
"@wordpress/ui": "^0.16.1
|
|
63
|
-
"@wordpress/url": "^4.
|
|
51
|
+
"@wordpress/base-styles": "^10.1.0",
|
|
52
|
+
"@wordpress/components": "^36.0.1",
|
|
53
|
+
"@wordpress/compose": "^8.2.0",
|
|
54
|
+
"@wordpress/core-data": "^7.49.1",
|
|
55
|
+
"@wordpress/data": "^10.49.0",
|
|
56
|
+
"@wordpress/dataviews": "^17.0.1",
|
|
57
|
+
"@wordpress/date": "^5.49.0",
|
|
58
|
+
"@wordpress/element": "^8.1.0",
|
|
59
|
+
"@wordpress/i18n": "^6.22.0",
|
|
60
|
+
"@wordpress/icons": "^15.0.0",
|
|
61
|
+
"@wordpress/primitives": "^4.49.0",
|
|
62
|
+
"@wordpress/ui": "^0.16.1",
|
|
63
|
+
"@wordpress/url": "^4.49.0",
|
|
64
64
|
"clsx": "^2.1.1"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
@@ -81,5 +81,5 @@
|
|
|
81
81
|
"publishConfig": {
|
|
82
82
|
"access": "public"
|
|
83
83
|
},
|
|
84
|
-
"gitHead": "
|
|
84
|
+
"gitHead": "9a75283890dda96ae1d37197b5070fae8c9cf66f"
|
|
85
85
|
}
|
package/src/attached_to/edit.tsx
CHANGED
|
@@ -75,6 +75,7 @@ export default function MediaAttachedToEdit( {
|
|
|
75
75
|
post: 0,
|
|
76
76
|
_embedded: { ...data?._embedded, 'wp:attached-to': undefined },
|
|
77
77
|
} );
|
|
78
|
+
setValue( null );
|
|
78
79
|
setOptions( [] );
|
|
79
80
|
};
|
|
80
81
|
|
|
@@ -90,13 +91,16 @@ export default function MediaAttachedToEdit( {
|
|
|
90
91
|
{}
|
|
91
92
|
);
|
|
92
93
|
setSearchResults( results );
|
|
93
|
-
const
|
|
94
|
+
const suggestions = results.map( ( result ) => {
|
|
94
95
|
return {
|
|
95
96
|
label: result.title,
|
|
96
97
|
value: result.id.toString(),
|
|
97
98
|
};
|
|
98
99
|
} );
|
|
99
|
-
|
|
100
|
+
const includeCurrent =
|
|
101
|
+
! filterValue &&
|
|
102
|
+
suggestions.findIndex( ( s ) => s.value === value ) === -1;
|
|
103
|
+
setOptions( suggestions.concat( includeCurrent ? defaultPost : [] ) );
|
|
100
104
|
setIsLoading( false );
|
|
101
105
|
};
|
|
102
106
|
|
|
@@ -146,23 +150,22 @@ export default function MediaAttachedToEdit( {
|
|
|
146
150
|
}
|
|
147
151
|
};
|
|
148
152
|
|
|
149
|
-
const help =
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
: __( 'Search for a post or page to attach this media to.' );
|
|
153
|
+
const help = createInterpolateElement(
|
|
154
|
+
__(
|
|
155
|
+
'Search for a post or page to attach this media to or <button>detach current</button>.'
|
|
156
|
+
),
|
|
157
|
+
{
|
|
158
|
+
button: (
|
|
159
|
+
<Button
|
|
160
|
+
__next40pxDefaultSize
|
|
161
|
+
onClick={ handleDetach }
|
|
162
|
+
variant="link"
|
|
163
|
+
accessibleWhenDisabled
|
|
164
|
+
disabled={ ! value }
|
|
165
|
+
/>
|
|
166
|
+
),
|
|
167
|
+
}
|
|
168
|
+
);
|
|
166
169
|
|
|
167
170
|
return (
|
|
168
171
|
<ComboboxControl
|