@wordpress/fields 0.0.2

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 (165) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE.md +788 -0
  3. package/README.md +73 -0
  4. package/build/actions/base-post/duplicate-post.js +146 -0
  5. package/build/actions/base-post/duplicate-post.js.map +1 -0
  6. package/build/actions/base-post/duplicate-post.native.js +9 -0
  7. package/build/actions/base-post/duplicate-post.native.js.map +1 -0
  8. package/build/actions/base-post/index.js +42 -0
  9. package/build/actions/base-post/index.js.map +1 -0
  10. package/build/actions/base-post/reorder-page.js +116 -0
  11. package/build/actions/base-post/reorder-page.js.map +1 -0
  12. package/build/actions/base-post/reorder-page.native.js +9 -0
  13. package/build/actions/base-post/reorder-page.native.js.map +1 -0
  14. package/build/actions/base-post/view-post.js +36 -0
  15. package/build/actions/base-post/view-post.js.map +1 -0
  16. package/build/actions/common/index.js +21 -0
  17. package/build/actions/common/index.js.map +1 -0
  18. package/build/actions/common/permanently-delete-post.js +109 -0
  19. package/build/actions/common/permanently-delete-post.js.map +1 -0
  20. package/build/actions/common/view-post-revisions.js +49 -0
  21. package/build/actions/common/view-post-revisions.js.map +1 -0
  22. package/build/actions/index.js +39 -0
  23. package/build/actions/index.js.map +1 -0
  24. package/build/actions/pattern/duplicate-pattern.js +48 -0
  25. package/build/actions/pattern/duplicate-pattern.js.map +1 -0
  26. package/build/actions/pattern/export-pattern.js +57 -0
  27. package/build/actions/pattern/export-pattern.js.map +1 -0
  28. package/build/actions/pattern/export-pattern.native.js +9 -0
  29. package/build/actions/pattern/export-pattern.native.js.map +1 -0
  30. package/build/actions/pattern/index.js +28 -0
  31. package/build/actions/pattern/index.js.map +1 -0
  32. package/build/actions/utils.js +65 -0
  33. package/build/actions/utils.js.map +1 -0
  34. package/build/fields/index.js +21 -0
  35. package/build/fields/index.js.map +1 -0
  36. package/build/fields/order/index.js +23 -0
  37. package/build/fields/order/index.js.map +1 -0
  38. package/build/fields/title/index.js +27 -0
  39. package/build/fields/title/index.js.map +1 -0
  40. package/build/index.js +28 -0
  41. package/build/index.js.map +1 -0
  42. package/build/index.native.js +28 -0
  43. package/build/index.native.js.map +1 -0
  44. package/build/lock-unlock.js +18 -0
  45. package/build/lock-unlock.js.map +1 -0
  46. package/build/types.js +6 -0
  47. package/build/types.js.map +1 -0
  48. package/build/wordpress-editor.d.js +2 -0
  49. package/build/wordpress-editor.d.js.map +1 -0
  50. package/build-module/actions/base-post/duplicate-post.js +139 -0
  51. package/build-module/actions/base-post/duplicate-post.js.map +1 -0
  52. package/build-module/actions/base-post/duplicate-post.native.js +3 -0
  53. package/build-module/actions/base-post/duplicate-post.native.js.map +1 -0
  54. package/build-module/actions/base-post/index.js +6 -0
  55. package/build-module/actions/base-post/index.js.map +1 -0
  56. package/build-module/actions/base-post/reorder-page.js +111 -0
  57. package/build-module/actions/base-post/reorder-page.js.map +1 -0
  58. package/build-module/actions/base-post/reorder-page.native.js +3 -0
  59. package/build-module/actions/base-post/reorder-page.native.js.map +1 -0
  60. package/build-module/actions/base-post/view-post.js +30 -0
  61. package/build-module/actions/base-post/view-post.js.map +1 -0
  62. package/build-module/actions/common/index.js +3 -0
  63. package/build-module/actions/common/index.js.map +1 -0
  64. package/build-module/actions/common/permanently-delete-post.js +102 -0
  65. package/build-module/actions/common/permanently-delete-post.js.map +1 -0
  66. package/build-module/actions/common/view-post-revisions.js +43 -0
  67. package/build-module/actions/common/view-post-revisions.js.map +1 -0
  68. package/build-module/actions/index.js +4 -0
  69. package/build-module/actions/index.js.map +1 -0
  70. package/build-module/actions/pattern/duplicate-pattern.js +39 -0
  71. package/build-module/actions/pattern/duplicate-pattern.js.map +1 -0
  72. package/build-module/actions/pattern/export-pattern.js +51 -0
  73. package/build-module/actions/pattern/export-pattern.js.map +1 -0
  74. package/build-module/actions/pattern/export-pattern.native.js +3 -0
  75. package/build-module/actions/pattern/export-pattern.native.js.map +1 -0
  76. package/build-module/actions/pattern/index.js +4 -0
  77. package/build-module/actions/pattern/index.js.map +1 -0
  78. package/build-module/actions/utils.js +54 -0
  79. package/build-module/actions/utils.js.map +1 -0
  80. package/build-module/fields/index.js +3 -0
  81. package/build-module/fields/index.js.map +1 -0
  82. package/build-module/fields/order/index.js +17 -0
  83. package/build-module/fields/order/index.js.map +1 -0
  84. package/build-module/fields/title/index.js +22 -0
  85. package/build-module/fields/title/index.js.map +1 -0
  86. package/build-module/index.js +3 -0
  87. package/build-module/index.js.map +1 -0
  88. package/build-module/index.native.js +3 -0
  89. package/build-module/index.native.js.map +1 -0
  90. package/build-module/lock-unlock.js +9 -0
  91. package/build-module/lock-unlock.js.map +1 -0
  92. package/build-module/types.js +2 -0
  93. package/build-module/types.js.map +1 -0
  94. package/build-module/wordpress-editor.d.js +2 -0
  95. package/build-module/wordpress-editor.d.js.map +1 -0
  96. package/build-types/actions/base-post/duplicate-post.d.ts +5 -0
  97. package/build-types/actions/base-post/duplicate-post.d.ts.map +1 -0
  98. package/build-types/actions/base-post/duplicate-post.native.d.ts +3 -0
  99. package/build-types/actions/base-post/duplicate-post.native.d.ts.map +1 -0
  100. package/build-types/actions/base-post/index.d.ts +6 -0
  101. package/build-types/actions/base-post/index.d.ts.map +1 -0
  102. package/build-types/actions/base-post/reorder-page.d.ts +8 -0
  103. package/build-types/actions/base-post/reorder-page.d.ts.map +1 -0
  104. package/build-types/actions/base-post/reorder-page.native.d.ts +3 -0
  105. package/build-types/actions/base-post/reorder-page.native.d.ts.map +1 -0
  106. package/build-types/actions/base-post/view-post.d.ts +8 -0
  107. package/build-types/actions/base-post/view-post.d.ts.map +1 -0
  108. package/build-types/actions/common/index.d.ts +3 -0
  109. package/build-types/actions/common/index.d.ts.map +1 -0
  110. package/build-types/actions/common/permanently-delete-post.d.ts +5 -0
  111. package/build-types/actions/common/permanently-delete-post.d.ts.map +1 -0
  112. package/build-types/actions/common/view-post-revisions.d.ts +8 -0
  113. package/build-types/actions/common/view-post-revisions.d.ts.map +1 -0
  114. package/build-types/actions/index.d.ts +4 -0
  115. package/build-types/actions/index.d.ts.map +1 -0
  116. package/build-types/actions/pattern/duplicate-pattern.d.ts +5 -0
  117. package/build-types/actions/pattern/duplicate-pattern.d.ts.map +1 -0
  118. package/build-types/actions/pattern/export-pattern.d.ts +8 -0
  119. package/build-types/actions/pattern/export-pattern.d.ts.map +1 -0
  120. package/build-types/actions/pattern/export-pattern.native.d.ts +3 -0
  121. package/build-types/actions/pattern/export-pattern.native.d.ts.map +1 -0
  122. package/build-types/actions/pattern/index.d.ts +4 -0
  123. package/build-types/actions/pattern/index.d.ts.map +1 -0
  124. package/build-types/actions/utils.d.ts +23 -0
  125. package/build-types/actions/utils.d.ts.map +1 -0
  126. package/build-types/fields/index.d.ts +3 -0
  127. package/build-types/fields/index.d.ts.map +1 -0
  128. package/build-types/fields/order/index.d.ts +11 -0
  129. package/build-types/fields/order/index.d.ts.map +1 -0
  130. package/build-types/fields/title/index.d.ts +11 -0
  131. package/build-types/fields/title/index.d.ts.map +1 -0
  132. package/build-types/index.d.ts +3 -0
  133. package/build-types/index.d.ts.map +1 -0
  134. package/build-types/index.native.d.ts +3 -0
  135. package/build-types/index.native.d.ts.map +1 -0
  136. package/build-types/lock-unlock.d.ts +2 -0
  137. package/build-types/lock-unlock.d.ts.map +1 -0
  138. package/build-types/types.d.ts +92 -0
  139. package/build-types/types.d.ts.map +1 -0
  140. package/package.json +63 -0
  141. package/src/actions/base-post/duplicate-post.native.tsx +3 -0
  142. package/src/actions/base-post/duplicate-post.tsx +174 -0
  143. package/src/actions/base-post/index.ts +5 -0
  144. package/src/actions/base-post/reorder-page.native.tsx +3 -0
  145. package/src/actions/base-post/reorder-page.tsx +125 -0
  146. package/src/actions/base-post/view-post.tsx +30 -0
  147. package/src/actions/common/index.ts +2 -0
  148. package/src/actions/common/permanently-delete-post.tsx +118 -0
  149. package/src/actions/common/view-post-revisions.tsx +47 -0
  150. package/src/actions/index.ts +3 -0
  151. package/src/actions/pattern/duplicate-pattern.tsx +40 -0
  152. package/src/actions/pattern/export-pattern.native.tsx +3 -0
  153. package/src/actions/pattern/export-pattern.tsx +76 -0
  154. package/src/actions/pattern/index.ts +3 -0
  155. package/src/actions/utils.ts +66 -0
  156. package/src/fields/index.ts +2 -0
  157. package/src/fields/order/index.ts +18 -0
  158. package/src/fields/title/index.ts +21 -0
  159. package/src/index.native.ts +2 -0
  160. package/src/index.ts +2 -0
  161. package/src/lock-unlock.ts +9 -0
  162. package/src/types.ts +85 -0
  163. package/src/wordpress-editor.d.ts +1 -0
  164. package/tsconfig.json +29 -0
  165. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,76 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { paramCase as kebabCase } from 'change-case';
5
+ import { downloadZip } from 'client-zip';
6
+
7
+ /**
8
+ * WordPress dependencies
9
+ */
10
+ import { downloadBlob } from '@wordpress/blob';
11
+ import { __ } from '@wordpress/i18n';
12
+ import { download } from '@wordpress/icons';
13
+ import type { Action } from '@wordpress/dataviews';
14
+
15
+ /**
16
+ * Internal dependencies
17
+ */
18
+ import type { Pattern } from '../../types';
19
+ import { getItemTitle } from '../utils';
20
+
21
+ function getJsonFromItem( item: Pattern ) {
22
+ return JSON.stringify(
23
+ {
24
+ __file: item.type,
25
+ title: getItemTitle( item ),
26
+ content:
27
+ typeof item.content === 'string'
28
+ ? item.content
29
+ : item.content?.raw,
30
+ syncStatus: item.wp_pattern_sync_status,
31
+ },
32
+ null,
33
+ 2
34
+ );
35
+ }
36
+
37
+ const exportPattern: Action< Pattern > = {
38
+ id: 'export-pattern',
39
+ label: __( 'Export as JSON' ),
40
+ icon: download,
41
+ supportsBulk: true,
42
+ isEligible: ( item ) => item.type === 'wp_block',
43
+ callback: async ( items ) => {
44
+ if ( items.length === 1 ) {
45
+ return downloadBlob(
46
+ `${ kebabCase(
47
+ getItemTitle( items[ 0 ] ) || items[ 0 ].slug
48
+ ) }.json`,
49
+ getJsonFromItem( items[ 0 ] ),
50
+ 'application/json'
51
+ );
52
+ }
53
+ const nameCount: Record< string, number > = {};
54
+ const filesToZip = items.map( ( item ) => {
55
+ const name = kebabCase( getItemTitle( item ) || item.slug );
56
+ nameCount[ name ] = ( nameCount[ name ] || 0 ) + 1;
57
+ return {
58
+ name: `${
59
+ name +
60
+ ( nameCount[ name ] > 1
61
+ ? '-' + ( nameCount[ name ] - 1 )
62
+ : '' )
63
+ }.json`,
64
+ lastModified: new Date(),
65
+ input: getJsonFromItem( item ),
66
+ };
67
+ } );
68
+ return downloadBlob(
69
+ __( 'patterns-export' ) + '.zip',
70
+ await downloadZip( filesToZip ).blob(),
71
+ 'application/zip'
72
+ );
73
+ },
74
+ };
75
+
76
+ export default exportPattern;
@@ -0,0 +1,3 @@
1
+ export { default as duplicatePattern } from './duplicate-pattern';
2
+ export { default as exportPattern } from './export-pattern';
3
+ export { default as exportPatternNative } from './export-pattern.native';
@@ -0,0 +1,66 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { decodeEntities } from '@wordpress/html-entities';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import type { Post, TemplatePart, Template } from '../types';
10
+
11
+ export const TEMPLATE_POST_TYPE = 'wp_template';
12
+ export const TEMPLATE_PART_POST_TYPE = 'wp_template_part';
13
+ export const TEMPLATE_ORIGINS = {
14
+ custom: 'custom',
15
+ theme: 'theme',
16
+ plugin: 'plugin',
17
+ };
18
+
19
+ export function isTemplate( post: Post ): post is Template {
20
+ return post.type === TEMPLATE_POST_TYPE;
21
+ }
22
+
23
+ export function isTemplatePart( post: Post ): post is TemplatePart {
24
+ return post.type === TEMPLATE_PART_POST_TYPE;
25
+ }
26
+
27
+ export function isTemplateOrTemplatePart(
28
+ p: Post
29
+ ): p is Template | TemplatePart {
30
+ return p.type === TEMPLATE_POST_TYPE || p.type === TEMPLATE_PART_POST_TYPE;
31
+ }
32
+
33
+ export function getItemTitle( item: Post ) {
34
+ if ( typeof item.title === 'string' ) {
35
+ return decodeEntities( item.title );
36
+ }
37
+ if ( 'rendered' in item.title ) {
38
+ return decodeEntities( item.title.rendered );
39
+ }
40
+ if ( 'raw' in item.title ) {
41
+ return decodeEntities( item.title.raw );
42
+ }
43
+ return '';
44
+ }
45
+
46
+ /**
47
+ * Check if a template is removable.
48
+ *
49
+ * @param template The template entity to check.
50
+ * @return Whether the template is removable.
51
+ */
52
+ export function isTemplateRemovable( template: Template | TemplatePart ) {
53
+ if ( ! template ) {
54
+ return false;
55
+ }
56
+ // In patterns list page we map the templates parts to a different object
57
+ // than the one returned from the endpoint. This is why we need to check for
58
+ // two props whether is custom or has a theme file.
59
+ return (
60
+ [ template.source, template.source ].includes(
61
+ TEMPLATE_ORIGINS.custom
62
+ ) &&
63
+ ! Boolean( template.type === 'wp_template' && template?.plugin ) &&
64
+ ! template.has_theme_file
65
+ );
66
+ }
@@ -0,0 +1,2 @@
1
+ export { default as titleField } from './title';
2
+ export { default as orderField } from './order';
@@ -0,0 +1,18 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import type { Field } from '@wordpress/dataviews';
5
+ import { __ } from '@wordpress/i18n';
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import type { BasePost } from '../../types';
10
+
11
+ const orderField: Field< BasePost > = {
12
+ type: 'integer',
13
+ id: 'menu_order',
14
+ label: __( 'Order' ),
15
+ description: __( 'Determines the order of pages.' ),
16
+ };
17
+
18
+ export default orderField;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import type { Field } from '@wordpress/dataviews';
5
+ import { __ } from '@wordpress/i18n';
6
+
7
+ /**
8
+ * Internal dependencies
9
+ */
10
+ import type { BasePost } from '../../types';
11
+ import { getItemTitle } from '../../actions/utils';
12
+
13
+ const titleField: Field< BasePost > = {
14
+ type: 'text',
15
+ id: 'title',
16
+ label: __( 'Title' ),
17
+ placeholder: __( 'No title' ),
18
+ getValue: ( { item } ) => getItemTitle( item ),
19
+ };
20
+
21
+ export default titleField;
@@ -0,0 +1,2 @@
1
+ export * from './actions/base-post/duplicate-post.native';
2
+ export * from './actions/base-post/reorder-page.native';
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './fields';
2
+ export * from './actions';
@@ -0,0 +1,9 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';
5
+ export const { lock, unlock } =
6
+ __dangerousOptInToUnstableAPIsOnlyForCoreModules(
7
+ 'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.',
8
+ '@wordpress/fields'
9
+ );
package/src/types.ts ADDED
@@ -0,0 +1,85 @@
1
+ type PostStatus =
2
+ | 'published'
3
+ | 'draft'
4
+ | 'pending'
5
+ | 'private'
6
+ | 'future'
7
+ | 'auto-draft'
8
+ | 'trash';
9
+
10
+ export interface CommonPost {
11
+ status?: PostStatus;
12
+ title: string | { rendered: string } | { raw: string };
13
+ content: string | { raw: string; rendered: string };
14
+ type: string;
15
+ id: string | number;
16
+ blocks?: Object[];
17
+ _links?: Links;
18
+ }
19
+
20
+ interface Links {
21
+ 'predecessor-version'?: { href: string; id: number }[];
22
+ 'version-history'?: { href: string; count: number }[];
23
+ [ key: string ]: { href: string }[] | undefined;
24
+ }
25
+
26
+ export interface BasePost extends CommonPost {
27
+ comment_status?: 'open' | 'closed';
28
+ excerpt?: string | { raw: string; rendered: string };
29
+ meta?: Record< string, any >;
30
+ parent?: number;
31
+ password?: string;
32
+ template?: string;
33
+ format?: string;
34
+ featured_media?: number;
35
+ menu_order?: number;
36
+ ping_status?: 'open' | 'closed';
37
+ link?: string;
38
+ }
39
+
40
+ export interface Template extends CommonPost {
41
+ type: 'wp_template';
42
+ is_custom: boolean;
43
+ source: string;
44
+ origin: string;
45
+ plugin?: string;
46
+ has_theme_file: boolean;
47
+ id: string;
48
+ }
49
+
50
+ export interface TemplatePart extends CommonPost {
51
+ type: 'wp_template_part';
52
+ source: string;
53
+ origin: string;
54
+ has_theme_file: boolean;
55
+ id: string;
56
+ area: string;
57
+ }
58
+
59
+ export interface Pattern extends CommonPost {
60
+ slug: string;
61
+ title: { raw: string };
62
+ wp_pattern_sync_status: string;
63
+ }
64
+
65
+ export type Post = Template | TemplatePart | Pattern | BasePost;
66
+
67
+ export type PostWithPermissions = Post & {
68
+ permissions: {
69
+ delete: boolean;
70
+ update: boolean;
71
+ };
72
+ };
73
+
74
+ export interface PostType {
75
+ slug: string;
76
+ viewable: boolean;
77
+ supports?: {
78
+ 'page-attributes'?: boolean;
79
+ title?: boolean;
80
+ revisions?: boolean;
81
+ };
82
+ }
83
+
84
+ // Will be unnecessary after typescript 5.0 upgrade.
85
+ export type CoreDataError = { message?: string; code?: string };
@@ -0,0 +1 @@
1
+ declare module '@wordpress/editor';
package/tsconfig.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig.json",
3
+ "extends": "../../tsconfig.base.json",
4
+ "compilerOptions": {
5
+ "rootDir": "src",
6
+ "declarationDir": "build-types",
7
+ "checkJs": false
8
+ },
9
+ "references": [
10
+ { "path": "../components" },
11
+ { "path": "../compose" },
12
+ { "path": "../data" },
13
+ { "path": "../element" },
14
+ { "path": "../i18n" },
15
+ { "path": "../icons" },
16
+ { "path": "../primitives" },
17
+ { "path": "../private-apis" },
18
+ { "path": "../warning" },
19
+ { "path": "../url" },
20
+ { "path": "../notices" },
21
+ { "path": "../dataviews" },
22
+ { "path": "../blob" },
23
+ { "path": "../core-data" },
24
+ { "path": "../hooks" },
25
+ { "path": "../html-entities" }
26
+ ],
27
+ "include": [ "src" ],
28
+ "exclude": [ "@wordpress/editor" ]
29
+ }