@wordpress/fields 0.40.2-next.v.202606191442.0 → 0.41.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.
Files changed (52) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/README.md +16 -0
  3. package/build/actions/rename-post.cjs +0 -1
  4. package/build/actions/rename-post.cjs.map +2 -2
  5. package/build/components/create-template-part-modal/index.cjs +0 -1
  6. package/build/components/create-template-part-modal/index.cjs.map +2 -2
  7. package/build/fields/description/index.cjs +1 -0
  8. package/build/fields/description/index.cjs.map +2 -2
  9. package/build/fields/index.cjs +5 -0
  10. package/build/fields/index.cjs.map +2 -2
  11. package/build/fields/password/edit.cjs +0 -1
  12. package/build/fields/password/edit.cjs.map +2 -2
  13. package/build/fields/template-author/index.cjs +81 -0
  14. package/build/fields/template-author/index.cjs.map +7 -0
  15. package/build/fields/template-author/view.cjs +101 -0
  16. package/build/fields/template-author/view.cjs.map +7 -0
  17. package/build/types.cjs.map +1 -1
  18. package/build-module/actions/rename-post.mjs +0 -1
  19. package/build-module/actions/rename-post.mjs.map +2 -2
  20. package/build-module/components/create-template-part-modal/index.mjs +0 -1
  21. package/build-module/components/create-template-part-modal/index.mjs.map +2 -2
  22. package/build-module/fields/description/index.mjs +1 -0
  23. package/build-module/fields/description/index.mjs.map +2 -2
  24. package/build-module/fields/index.mjs +6 -0
  25. package/build-module/fields/index.mjs.map +2 -2
  26. package/build-module/fields/password/edit.mjs +0 -1
  27. package/build-module/fields/password/edit.mjs.map +2 -2
  28. package/build-module/fields/template-author/index.mjs +45 -0
  29. package/build-module/fields/template-author/index.mjs.map +7 -0
  30. package/build-module/fields/template-author/view.mjs +75 -0
  31. package/build-module/fields/template-author/view.mjs.map +7 -0
  32. package/build-types/actions/rename-post.d.ts.map +1 -1
  33. package/build-types/components/create-template-part-modal/index.d.ts.map +1 -1
  34. package/build-types/fields/description/index.d.ts.map +1 -1
  35. package/build-types/fields/index.d.ts +1 -0
  36. package/build-types/fields/index.d.ts.map +1 -1
  37. package/build-types/fields/password/edit.d.ts.map +1 -1
  38. package/build-types/fields/template-author/index.d.ts +14 -0
  39. package/build-types/fields/template-author/index.d.ts.map +1 -0
  40. package/build-types/fields/template-author/view.d.ts +8 -0
  41. package/build-types/fields/template-author/view.d.ts.map +1 -0
  42. package/build-types/types.d.ts +7 -2
  43. package/build-types/types.d.ts.map +1 -1
  44. package/package.json +27 -27
  45. package/src/actions/rename-post.tsx +0 -1
  46. package/src/components/create-template-part-modal/index.tsx +0 -1
  47. package/src/fields/description/index.tsx +1 -0
  48. package/src/fields/index.ts +4 -0
  49. package/src/fields/password/edit.tsx +0 -1
  50. package/src/fields/template-author/index.tsx +60 -0
  51. package/src/fields/template-author/view.tsx +95 -0
  52. package/src/types.ts +8 -2
@@ -0,0 +1,60 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import type { Field } from '@wordpress/dataviews';
5
+ import { __ } from '@wordpress/i18n';
6
+ import { resolveSelect } from '@wordpress/data';
7
+ import { store as coreStore } from '@wordpress/core-data';
8
+
9
+ /**
10
+ * Internal dependencies
11
+ */
12
+ import TemplateAuthorView from './view';
13
+ import type { Template, TemplatePart } from '../../types';
14
+
15
+ async function getAuthorElements(
16
+ postType: 'wp_template' | 'wp_template_part'
17
+ ) {
18
+ const records = ( await resolveSelect( coreStore ).getEntityRecords(
19
+ 'postType',
20
+ postType,
21
+ { per_page: -1, _fields: 'id,author_text' }
22
+ ) ) as ( Template | TemplatePart )[] | null;
23
+
24
+ const seen = new Set< string >();
25
+ const elements: { value: string; label: string }[] = [];
26
+ for ( const record of records ?? [] ) {
27
+ const value = record.author_text;
28
+ if ( value && ! seen.has( value ) ) {
29
+ seen.add( value );
30
+ elements.push( { value, label: value } );
31
+ }
32
+ }
33
+ return elements;
34
+ }
35
+
36
+ /**
37
+ * Author field for templates.
38
+ */
39
+ export const templateAuthorField: Field< Template > = {
40
+ label: __( 'Author' ),
41
+ id: 'author',
42
+ getValue: ( { item } ) => item.author_text,
43
+ render: TemplateAuthorView,
44
+ getElements: () => getAuthorElements( 'wp_template' ),
45
+ };
46
+
47
+ /**
48
+ * Author field for template parts.
49
+ */
50
+ export const templatePartAuthorField: Field< TemplatePart > = {
51
+ label: __( 'Author' ),
52
+ id: 'author',
53
+ getValue: ( { item } ) => item.author_text,
54
+ render: TemplateAuthorView,
55
+ enableSorting: false,
56
+ filterBy: {
57
+ isPrimary: true,
58
+ },
59
+ getElements: () => getAuthorElements( 'wp_template_part' ),
60
+ };
@@ -0,0 +1,95 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import clsx from 'clsx';
5
+
6
+ /**
7
+ * WordPress dependencies
8
+ */
9
+ import { useState } from '@wordpress/element';
10
+ import { useSelect } from '@wordpress/data';
11
+ import { store as coreStore } from '@wordpress/core-data';
12
+ import {
13
+ commentAuthorAvatar as authorIcon,
14
+ layout as themeIcon,
15
+ plugins as pluginIcon,
16
+ globe as globeIcon,
17
+ } from '@wordpress/icons';
18
+ import { Icon, Stack } from '@wordpress/ui';
19
+
20
+ /**
21
+ * Internal dependencies
22
+ */
23
+ import type { Template, TemplatePart } from '../../types';
24
+
25
+ function getIconForSource( originalSource: Template[ 'original_source' ] ) {
26
+ switch ( originalSource ) {
27
+ case 'theme':
28
+ return themeIcon;
29
+ case 'plugin':
30
+ return pluginIcon;
31
+ case 'site':
32
+ return globeIcon;
33
+ default:
34
+ return authorIcon;
35
+ }
36
+ }
37
+
38
+ export default function TemplateAuthorView( {
39
+ item,
40
+ }: {
41
+ item: Template | TemplatePart;
42
+ } ) {
43
+ const [ isImageLoaded, setIsImageLoaded ] = useState( false );
44
+ const originalSource = item.original_source;
45
+ const icon = getIconForSource( originalSource );
46
+ const text = item.author_text;
47
+ const authorId = item.author;
48
+
49
+ const imageUrl = useSelect(
50
+ ( select ) => {
51
+ if ( originalSource === 'site' ) {
52
+ const siteData = select( coreStore ).getEntityRecord< {
53
+ site_logo?: number;
54
+ } >( 'root', '__unstableBase' );
55
+ const logoId = siteData?.site_logo;
56
+ if ( ! logoId ) {
57
+ return undefined;
58
+ }
59
+ return select( coreStore ).getEntityRecord< {
60
+ source_url: string;
61
+ } >( 'postType', 'attachment', logoId )?.source_url;
62
+ }
63
+
64
+ if ( originalSource !== 'user' || ! authorId ) {
65
+ return undefined;
66
+ }
67
+ return select( coreStore ).getUser( authorId )?.avatar_urls?.[ 48 ];
68
+ },
69
+ [ originalSource, authorId ]
70
+ );
71
+
72
+ return (
73
+ <Stack direction="row" align="center">
74
+ { imageUrl && (
75
+ <div
76
+ className={ clsx( 'fields-controls__author-avatar', {
77
+ 'is-loaded': isImageLoaded,
78
+ } ) }
79
+ >
80
+ <img
81
+ onLoad={ () => setIsImageLoaded( true ) }
82
+ alt=""
83
+ src={ imageUrl }
84
+ />
85
+ </div>
86
+ ) }
87
+ { ! imageUrl && (
88
+ <div className="fields-controls__author-icon">
89
+ <Icon icon={ icon } />
90
+ </div>
91
+ ) }
92
+ <span className="fields-controls__author-name">{ text }</span>
93
+ </Stack>
94
+ );
95
+ }
package/src/types.ts CHANGED
@@ -89,7 +89,13 @@ export interface BasePostWithEmbeddedFeaturedMedia extends BasePost {
89
89
  _embedded: EmbeddedFeaturedMedia;
90
90
  }
91
91
 
92
- export interface Template extends CommonPost {
92
+ interface TemplateAuthorFields {
93
+ author?: number;
94
+ author_text: string;
95
+ original_source?: 'theme' | 'plugin' | 'site' | 'user';
96
+ }
97
+
98
+ export interface Template extends CommonPost, TemplateAuthorFields {
93
99
  type: 'wp_template';
94
100
  is_custom: boolean;
95
101
  source: string;
@@ -100,7 +106,7 @@ export interface Template extends CommonPost {
100
106
  description?: string;
101
107
  }
102
108
 
103
- export interface TemplatePart extends CommonPost {
109
+ export interface TemplatePart extends CommonPost, TemplateAuthorFields {
104
110
  type: 'wp_template_part';
105
111
  source: string;
106
112
  origin: string;