@wordpress/fields 0.5.0 → 0.5.1-next.cd6172eb0.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.
Files changed (125) hide show
  1. package/README.md +29 -0
  2. package/build/actions/duplicate-template-part.js +68 -0
  3. package/build/actions/duplicate-template-part.js.map +1 -0
  4. package/build/actions/index.js +7 -0
  5. package/build/actions/index.js.map +1 -1
  6. package/build/actions/permanently-delete-post.js +0 -1
  7. package/build/actions/permanently-delete-post.js.map +1 -1
  8. package/build/actions/rename-post.js +2 -2
  9. package/build/actions/rename-post.js.map +1 -1
  10. package/build/actions/reset-post.js +3 -3
  11. package/build/actions/reset-post.js.map +1 -1
  12. package/build/actions/restore-post.js +0 -1
  13. package/build/actions/restore-post.js.map +1 -1
  14. package/build/actions/trash-post.js +0 -1
  15. package/build/actions/trash-post.js.map +1 -1
  16. package/build/actions/utils.js +4 -12
  17. package/build/actions/utils.js.map +1 -1
  18. package/build/components/create-template-part-modal/index.js +213 -0
  19. package/build/components/create-template-part-modal/index.js.map +1 -0
  20. package/build/components/create-template-part-modal/utils.js +64 -0
  21. package/build/components/create-template-part-modal/utils.js.map +1 -0
  22. package/build/fields/index.js +7 -0
  23. package/build/fields/index.js.map +1 -1
  24. package/build/fields/slug/slug-view.js +1 -1
  25. package/build/fields/slug/slug-view.js.map +1 -1
  26. package/build/fields/slug/utils.js +3 -0
  27. package/build/fields/slug/utils.js.map +1 -1
  28. package/build/fields/template/index.js +28 -0
  29. package/build/fields/template/index.js.map +1 -0
  30. package/build/fields/template/template-edit.js +154 -0
  31. package/build/fields/template/template-edit.js.map +1 -0
  32. package/build/fields/title/title-view.js +1 -1
  33. package/build/fields/title/title-view.js.map +1 -1
  34. package/build/index.js +13 -0
  35. package/build/index.js.map +1 -1
  36. package/build/mutation/index.js +0 -1
  37. package/build/mutation/index.js.map +1 -1
  38. package/build/types.js.map +1 -1
  39. package/build-module/actions/duplicate-template-part.js +63 -0
  40. package/build-module/actions/duplicate-template-part.js.map +1 -0
  41. package/build-module/actions/index.js +1 -0
  42. package/build-module/actions/index.js.map +1 -1
  43. package/build-module/actions/permanently-delete-post.js +0 -1
  44. package/build-module/actions/permanently-delete-post.js.map +1 -1
  45. package/build-module/actions/rename-post.js +3 -3
  46. package/build-module/actions/rename-post.js.map +1 -1
  47. package/build-module/actions/reset-post.js +4 -4
  48. package/build-module/actions/reset-post.js.map +1 -1
  49. package/build-module/actions/restore-post.js +0 -1
  50. package/build-module/actions/restore-post.js.map +1 -1
  51. package/build-module/actions/trash-post.js +0 -1
  52. package/build-module/actions/trash-post.js.map +1 -1
  53. package/build-module/actions/utils.js +4 -11
  54. package/build-module/actions/utils.js.map +1 -1
  55. package/build-module/components/create-template-part-modal/index.js +204 -0
  56. package/build-module/components/create-template-part-modal/index.js.map +1 -0
  57. package/build-module/components/create-template-part-modal/utils.js +55 -0
  58. package/build-module/components/create-template-part-modal/utils.js.map +1 -0
  59. package/build-module/fields/index.js +1 -0
  60. package/build-module/fields/index.js.map +1 -1
  61. package/build-module/fields/slug/slug-view.js +1 -1
  62. package/build-module/fields/slug/slug-view.js.map +1 -1
  63. package/build-module/fields/slug/utils.js +3 -0
  64. package/build-module/fields/slug/utils.js.map +1 -1
  65. package/build-module/fields/template/index.js +21 -0
  66. package/build-module/fields/template/index.js.map +1 -0
  67. package/build-module/fields/template/template-edit.js +144 -0
  68. package/build-module/fields/template/template-edit.js.map +1 -0
  69. package/build-module/fields/title/title-view.js +1 -1
  70. package/build-module/fields/title/title-view.js.map +1 -1
  71. package/build-module/index.js +1 -0
  72. package/build-module/index.js.map +1 -1
  73. package/build-module/mutation/index.js +0 -1
  74. package/build-module/mutation/index.js.map +1 -1
  75. package/build-module/types.js.map +1 -1
  76. package/build-style/style-rtl.css +74 -0
  77. package/build-style/style.css +74 -0
  78. package/build-types/actions/duplicate-template-part.d.ts +11 -0
  79. package/build-types/actions/duplicate-template-part.d.ts.map +1 -0
  80. package/build-types/actions/index.d.ts +1 -0
  81. package/build-types/actions/index.d.ts.map +1 -1
  82. package/build-types/actions/rename-post.d.ts.map +1 -1
  83. package/build-types/actions/reset-post.d.ts.map +1 -1
  84. package/build-types/actions/utils.d.ts +7 -8
  85. package/build-types/actions/utils.d.ts.map +1 -1
  86. package/build-types/components/create-template-part-modal/index.d.ts +34 -0
  87. package/build-types/components/create-template-part-modal/index.d.ts.map +1 -0
  88. package/build-types/components/create-template-part-modal/utils.d.ts +4 -0
  89. package/build-types/components/create-template-part-modal/utils.d.ts.map +1 -0
  90. package/build-types/fields/index.d.ts +1 -0
  91. package/build-types/fields/index.d.ts.map +1 -1
  92. package/build-types/fields/slug/utils.d.ts.map +1 -1
  93. package/build-types/fields/template/index.d.ts +8 -0
  94. package/build-types/fields/template/index.d.ts.map +1 -0
  95. package/build-types/fields/template/template-edit.d.ts +4 -0
  96. package/build-types/fields/template/template-edit.d.ts.map +1 -0
  97. package/build-types/fields/title/title-view.d.ts.map +1 -1
  98. package/build-types/index.d.ts +2 -1
  99. package/build-types/index.d.ts.map +1 -1
  100. package/build-types/lock-unlock.d.ts +1 -1
  101. package/build-types/lock-unlock.d.ts.map +1 -1
  102. package/build-types/types.d.ts +3 -0
  103. package/build-types/types.d.ts.map +1 -1
  104. package/package.json +24 -23
  105. package/src/actions/duplicate-template-part.tsx +72 -0
  106. package/src/actions/index.ts +1 -0
  107. package/src/actions/rename-post.tsx +3 -6
  108. package/src/actions/reset-post.tsx +4 -9
  109. package/src/actions/utils.ts +7 -15
  110. package/src/components/create-template-part-modal/index.tsx +274 -0
  111. package/src/components/create-template-part-modal/style.scss +63 -0
  112. package/src/components/create-template-part-modal/test/utils.js +60 -0
  113. package/src/components/create-template-part-modal/utils.js +68 -0
  114. package/src/fields/index.ts +1 -0
  115. package/src/fields/slug/slug-view.tsx +1 -1
  116. package/src/fields/slug/utils.ts +4 -0
  117. package/src/fields/template/index.ts +22 -0
  118. package/src/fields/template/style.scss +23 -0
  119. package/src/fields/template/template-edit.tsx +210 -0
  120. package/src/fields/title/title-view.tsx +3 -4
  121. package/src/index.ts +2 -1
  122. package/src/style.scss +2 -0
  123. package/src/types.ts +3 -0
  124. package/tsconfig.json +2 -0
  125. package/tsconfig.tsbuildinfo +1 -1
@@ -1 +1 @@
1
- {"version":3,"names":["trash","useDispatch","store","coreStore","__","_n","sprintf","_x","noticesStore","useState","Button","__experimentalText","Text","__experimentalHStack","HStack","__experimentalVStack","VStack","getItemTitle","isTemplateOrTemplatePart","jsx","_jsx","jsxs","_jsxs","trashPost","id","label","isPrimary","icon","isEligible","item","type","status","includes","permissions","delete","supportsBulk","hideModalHeader","RenderModal","items","closeModal","onActionPerformed","isBusy","setIsBusy","createSuccessNotice","createErrorNotice","deleteEntityRecord","spacing","children","length","justify","__next40pxDefaultSize","variant","onClick","disabled","accessibleWhenDisabled","promiseResult","Promise","allSettled","map","toString","throwOnError","every","successMessage","errorMessage","typedError","reason","message","errorMessages","Set","failedPromises","filter","failedPromise","add","size","join"],"sources":["@wordpress/fields/src/actions/trash-post.tsx"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { trash } from '@wordpress/icons';\nimport { useDispatch } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { __, _n, sprintf, _x } from '@wordpress/i18n';\nimport { store as noticesStore } from '@wordpress/notices';\nimport { useState } from '@wordpress/element';\nimport {\n\tButton,\n\t__experimentalText as Text,\n\t__experimentalHStack as HStack,\n\t__experimentalVStack as VStack,\n} from '@wordpress/components';\nimport type { Action } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport { getItemTitle, isTemplateOrTemplatePart } from './utils';\nimport type { CoreDataError, PostWithPermissions } from '../types';\n\nconst trashPost: Action< PostWithPermissions > = {\n\tid: 'move-to-trash',\n\tlabel: __( 'Move to trash' ),\n\tisPrimary: true,\n\ticon: trash,\n\tisEligible( item ) {\n\t\tif ( isTemplateOrTemplatePart( item ) || item.type === 'wp_block' ) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn (\n\t\t\t!! item.status &&\n\t\t\t! [ 'auto-draft', 'trash' ].includes( item.status ) &&\n\t\t\titem.permissions?.delete\n\t\t);\n\t},\n\tsupportsBulk: true,\n\thideModalHeader: true,\n\tRenderModal: ( { items, closeModal, onActionPerformed } ) => {\n\t\tconst [ isBusy, setIsBusy ] = useState( false );\n\t\tconst { createSuccessNotice, createErrorNotice } =\n\t\t\tuseDispatch( noticesStore );\n\t\tconst { deleteEntityRecord } = useDispatch( coreStore );\n\t\treturn (\n\t\t\t<VStack spacing=\"5\">\n\t\t\t\t<Text>\n\t\t\t\t\t{ items.length === 1\n\t\t\t\t\t\t? sprintf(\n\t\t\t\t\t\t\t\t// translators: %s: The item's title.\n\t\t\t\t\t\t\t\t__(\n\t\t\t\t\t\t\t\t\t'Are you sure you want to move \"%s\" to the trash?'\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\tgetItemTitle( items[ 0 ] )\n\t\t\t\t\t\t )\n\t\t\t\t\t\t: sprintf(\n\t\t\t\t\t\t\t\t// translators: %d: The number of items (2 or more).\n\t\t\t\t\t\t\t\t_n(\n\t\t\t\t\t\t\t\t\t'Are you sure you want to move %d item to the trash ?',\n\t\t\t\t\t\t\t\t\t'Are you sure you want to move %d items to the trash ?',\n\t\t\t\t\t\t\t\t\titems.length\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\titems.length\n\t\t\t\t\t\t ) }\n\t\t\t\t</Text>\n\t\t\t\t<HStack justify=\"right\">\n\t\t\t\t\t<Button\n\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\tvariant=\"tertiary\"\n\t\t\t\t\t\tonClick={ closeModal }\n\t\t\t\t\t\tdisabled={ isBusy }\n\t\t\t\t\t\taccessibleWhenDisabled\n\t\t\t\t\t>\n\t\t\t\t\t\t{ __( 'Cancel' ) }\n\t\t\t\t\t</Button>\n\t\t\t\t\t<Button\n\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\tvariant=\"primary\"\n\t\t\t\t\t\tonClick={ async () => {\n\t\t\t\t\t\t\tsetIsBusy( true );\n\t\t\t\t\t\t\tconst promiseResult = await Promise.allSettled(\n\t\t\t\t\t\t\t\titems.map( ( item ) =>\n\t\t\t\t\t\t\t\t\tdeleteEntityRecord(\n\t\t\t\t\t\t\t\t\t\t'postType',\n\t\t\t\t\t\t\t\t\t\titem.type,\n\t\t\t\t\t\t\t\t\t\titem.id.toString(),\n\t\t\t\t\t\t\t\t\t\t{},\n\t\t\t\t\t\t\t\t\t\t{ throwOnError: true }\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t// If all the promises were fulfilled with success.\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\tpromiseResult.every(\n\t\t\t\t\t\t\t\t\t( { status } ) => status === 'fulfilled'\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tlet successMessage;\n\t\t\t\t\t\t\t\tif ( promiseResult.length === 1 ) {\n\t\t\t\t\t\t\t\t\tsuccessMessage = sprintf(\n\t\t\t\t\t\t\t\t\t\t/* translators: The item's title. */\n\t\t\t\t\t\t\t\t\t\t__( '\"%s\" moved to the trash.' ),\n\t\t\t\t\t\t\t\t\t\tgetItemTitle( items[ 0 ] )\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tsuccessMessage = sprintf(\n\t\t\t\t\t\t\t\t\t\t/* translators: The number of items. */\n\t\t\t\t\t\t\t\t\t\t_n(\n\t\t\t\t\t\t\t\t\t\t\t'%s item moved to the trash.',\n\t\t\t\t\t\t\t\t\t\t\t'%s items moved to the trash.',\n\t\t\t\t\t\t\t\t\t\t\titems.length\n\t\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t\t\titems.length\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tcreateSuccessNotice( successMessage, {\n\t\t\t\t\t\t\t\t\ttype: 'snackbar',\n\t\t\t\t\t\t\t\t\tid: 'move-to-trash-action',\n\t\t\t\t\t\t\t\t} );\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t// If there was at least one failure.\n\t\t\t\t\t\t\t\tlet errorMessage;\n\t\t\t\t\t\t\t\t// If we were trying to delete a single item.\n\t\t\t\t\t\t\t\tif ( promiseResult.length === 1 ) {\n\t\t\t\t\t\t\t\t\tconst typedError = promiseResult[ 0 ] as {\n\t\t\t\t\t\t\t\t\t\treason?: CoreDataError;\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t\tif ( typedError.reason?.message ) {\n\t\t\t\t\t\t\t\t\t\terrorMessage =\n\t\t\t\t\t\t\t\t\t\t\ttypedError.reason.message;\n\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\terrorMessage = __(\n\t\t\t\t\t\t\t\t\t\t\t'An error occurred while moving the item to the trash.'\n\t\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t// If we were trying to delete multiple items.\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tconst errorMessages = new Set();\n\t\t\t\t\t\t\t\t\tconst failedPromises = promiseResult.filter(\n\t\t\t\t\t\t\t\t\t\t( { status } ) => status === 'rejected'\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\tfor ( const failedPromise of failedPromises ) {\n\t\t\t\t\t\t\t\t\t\tconst typedError = failedPromise as {\n\t\t\t\t\t\t\t\t\t\t\treason?: CoreDataError;\n\t\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t\t\tif ( typedError.reason?.message ) {\n\t\t\t\t\t\t\t\t\t\t\terrorMessages.add(\n\t\t\t\t\t\t\t\t\t\t\t\ttypedError.reason.message\n\t\t\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\tif ( errorMessages.size === 0 ) {\n\t\t\t\t\t\t\t\t\t\terrorMessage = __(\n\t\t\t\t\t\t\t\t\t\t\t'An error occurred while moving the items to the trash.'\n\t\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\t} else if ( errorMessages.size === 1 ) {\n\t\t\t\t\t\t\t\t\t\terrorMessage = sprintf(\n\t\t\t\t\t\t\t\t\t\t\t/* translators: %s: an error message */\n\t\t\t\t\t\t\t\t\t\t\t__(\n\t\t\t\t\t\t\t\t\t\t\t\t'An error occurred while moving the item to the trash: %s'\n\t\t\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t\t\t\t[ ...errorMessages ][ 0 ]\n\t\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\terrorMessage = sprintf(\n\t\t\t\t\t\t\t\t\t\t\t/* translators: %s: a list of comma separated error messages */\n\t\t\t\t\t\t\t\t\t\t\t__(\n\t\t\t\t\t\t\t\t\t\t\t\t'Some errors occurred while moving the items to the trash: %s'\n\t\t\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t\t\t\t[ ...errorMessages ].join( ',' )\n\t\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tcreateErrorNotice( errorMessage, {\n\t\t\t\t\t\t\t\t\ttype: 'snackbar',\n\t\t\t\t\t\t\t\t} );\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif ( onActionPerformed ) {\n\t\t\t\t\t\t\t\tonActionPerformed( items );\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tsetIsBusy( false );\n\t\t\t\t\t\t\tcloseModal?.();\n\t\t\t\t\t\t} }\n\t\t\t\t\t\tisBusy={ isBusy }\n\t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D,KAAM,MAAMwC,aAAa,IAAIF,cAAc,EAAG;kBAC7C,MAAML,UAAU,GAAGO,aAElB;kBACD,IAAKP,UAAU,CAACC,MAAM,EAAEC,OAAO,EAAG;oBACjCC,aAAa,CAACK,GAAG,CAChBR,UAAU,CAACC,MAAM,CAACC,OACnB,CAAC;kBACF;gBACD;gBACA,IAAKC,aAAa,CAACM,IAAI,KAAK,CAAC,EAAG;kBAC/BV,YAAY,GAAG3D,EAAE,CAChB,wDACD,CAAC;gBACF,CAAC,MAAM,IAAK+D,aAAa,CAACM,IAAI,KAAK,CAAC,EAAG;kBACtCV,YAAY,GAAGzD,OAAO,CACrB;kBACAF,EAAE,CACD,0DACD,CAAC,EACD,CAAE,GAAG+D,aAAa,CAAE,CAAE,CAAC,CACxB,CAAC;gBACF,CAAC,MAAM;kBACNJ,YAAY,GAAGzD,OAAO,CACrB;kBACAF,EAAE,CACD,8DACD,CAAC,EACD,CAAE,GAAG+D,aAAa,CAAE,CAACO,IAAI,CAAE,GAAI,CAChC,CAAC;gBACF;cACD;cACA9B,iBAAiB,CAAEmB,YAAY,EAAE;gBAChCjC,IAAI,EAAE;cACP,CAAE,CAAC;YACJ;YACA,IAAKU,iBAAiB,EAAG;cACxBA,iBAAiB,CAAEF,KAAM,CAAC;YAC3B;YACAI,SAAS,CAAE,KAAM,CAAC;YAClBH,UAAU,GAAG,CAAC;UACf,CAAG;UACHE,MAAM,EAAGA,MAAQ;UACjBY,QAAQ,EAAGZ,MAAQ;UACnBa,sBAAsB;UAAAP,QAAA,EAEpBxC,EAAE,CAAE,OAAO,EAAE,MAAO;QAAC,CAChB,CAAC;MAAA,CACF,CAAC;IAAA,CACF,CAAC;EAEX;AACD,CAAC;AAED,eAAegB,SAAS","ignoreList":[]}
1
+ {"version":3,"names":["trash","useDispatch","store","coreStore","__","_n","sprintf","_x","noticesStore","useState","Button","__experimentalText","Text","__experimentalHStack","HStack","__experimentalVStack","VStack","getItemTitle","isTemplateOrTemplatePart","jsx","_jsx","jsxs","_jsxs","trashPost","id","label","isPrimary","icon","isEligible","item","type","status","includes","permissions","delete","supportsBulk","hideModalHeader","RenderModal","items","closeModal","onActionPerformed","isBusy","setIsBusy","createSuccessNotice","createErrorNotice","deleteEntityRecord","spacing","children","length","justify","__next40pxDefaultSize","variant","onClick","disabled","accessibleWhenDisabled","promiseResult","Promise","allSettled","map","toString","throwOnError","every","successMessage","errorMessage","typedError","reason","message","errorMessages","Set","failedPromises","filter","failedPromise","add","size","join"],"sources":["@wordpress/fields/src/actions/trash-post.tsx"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { trash } from '@wordpress/icons';\nimport { useDispatch } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { __, _n, sprintf, _x } from '@wordpress/i18n';\nimport { store as noticesStore } from '@wordpress/notices';\nimport { useState } from '@wordpress/element';\nimport {\n\tButton,\n\t__experimentalText as Text,\n\t__experimentalHStack as HStack,\n\t__experimentalVStack as VStack,\n} from '@wordpress/components';\nimport type { Action } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport { getItemTitle, isTemplateOrTemplatePart } from './utils';\nimport type { CoreDataError, PostWithPermissions } from '../types';\n\nconst trashPost: Action< PostWithPermissions > = {\n\tid: 'move-to-trash',\n\tlabel: __( 'Move to trash' ),\n\tisPrimary: true,\n\ticon: trash,\n\tisEligible( item ) {\n\t\tif ( isTemplateOrTemplatePart( item ) || item.type === 'wp_block' ) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn (\n\t\t\t!! item.status &&\n\t\t\t! [ 'auto-draft', 'trash' ].includes( item.status ) &&\n\t\t\titem.permissions?.delete\n\t\t);\n\t},\n\tsupportsBulk: true,\n\thideModalHeader: true,\n\tRenderModal: ( { items, closeModal, onActionPerformed } ) => {\n\t\tconst [ isBusy, setIsBusy ] = useState( false );\n\t\tconst { createSuccessNotice, createErrorNotice } =\n\t\t\tuseDispatch( noticesStore );\n\t\tconst { deleteEntityRecord } = useDispatch( coreStore );\n\t\treturn (\n\t\t\t<VStack spacing=\"5\">\n\t\t\t\t<Text>\n\t\t\t\t\t{ items.length === 1\n\t\t\t\t\t\t? sprintf(\n\t\t\t\t\t\t\t\t// translators: %s: The item's title.\n\t\t\t\t\t\t\t\t__(\n\t\t\t\t\t\t\t\t\t'Are you sure you want to move \"%s\" to the trash?'\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\tgetItemTitle( items[ 0 ] )\n\t\t\t\t\t\t )\n\t\t\t\t\t\t: sprintf(\n\t\t\t\t\t\t\t\t// translators: %d: The number of items (2 or more).\n\t\t\t\t\t\t\t\t_n(\n\t\t\t\t\t\t\t\t\t'Are you sure you want to move %d item to the trash ?',\n\t\t\t\t\t\t\t\t\t'Are you sure you want to move %d items to the trash ?',\n\t\t\t\t\t\t\t\t\titems.length\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\titems.length\n\t\t\t\t\t\t ) }\n\t\t\t\t</Text>\n\t\t\t\t<HStack justify=\"right\">\n\t\t\t\t\t<Button\n\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\tvariant=\"tertiary\"\n\t\t\t\t\t\tonClick={ closeModal }\n\t\t\t\t\t\tdisabled={ isBusy }\n\t\t\t\t\t\taccessibleWhenDisabled\n\t\t\t\t\t>\n\t\t\t\t\t\t{ __( 'Cancel' ) }\n\t\t\t\t\t</Button>\n\t\t\t\t\t<Button\n\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\tvariant=\"primary\"\n\t\t\t\t\t\tonClick={ async () => {\n\t\t\t\t\t\t\tsetIsBusy( true );\n\t\t\t\t\t\t\tconst promiseResult = await Promise.allSettled(\n\t\t\t\t\t\t\t\titems.map( ( item ) =>\n\t\t\t\t\t\t\t\t\tdeleteEntityRecord(\n\t\t\t\t\t\t\t\t\t\t'postType',\n\t\t\t\t\t\t\t\t\t\titem.type,\n\t\t\t\t\t\t\t\t\t\titem.id.toString(),\n\t\t\t\t\t\t\t\t\t\t{},\n\t\t\t\t\t\t\t\t\t\t{ throwOnError: true }\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t// If all the promises were fulfilled with success.\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\tpromiseResult.every(\n\t\t\t\t\t\t\t\t\t( { status } ) => status === 'fulfilled'\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tlet successMessage;\n\t\t\t\t\t\t\t\tif ( promiseResult.length === 1 ) {\n\t\t\t\t\t\t\t\t\tsuccessMessage = sprintf(\n\t\t\t\t\t\t\t\t\t\t/* translators: The item's title. */\n\t\t\t\t\t\t\t\t\t\t__( '\"%s\" moved to the trash.' ),\n\t\t\t\t\t\t\t\t\t\tgetItemTitle( items[ 0 ] )\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tsuccessMessage = sprintf(\n\t\t\t\t\t\t\t\t\t\t/* translators: The number of items. */\n\t\t\t\t\t\t\t\t\t\t_n(\n\t\t\t\t\t\t\t\t\t\t\t'%s item moved to the trash.',\n\t\t\t\t\t\t\t\t\t\t\t'%s items moved to the trash.',\n\t\t\t\t\t\t\t\t\t\t\titems.length\n\t\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t\t\titems.length\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tcreateSuccessNotice( successMessage, {\n\t\t\t\t\t\t\t\t\ttype: 'snackbar',\n\t\t\t\t\t\t\t\t\tid: 'move-to-trash-action',\n\t\t\t\t\t\t\t\t} );\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t// If there was at least one failure.\n\t\t\t\t\t\t\t\tlet errorMessage;\n\t\t\t\t\t\t\t\t// If we were trying to delete a single item.\n\t\t\t\t\t\t\t\tif ( promiseResult.length === 1 ) {\n\t\t\t\t\t\t\t\t\tconst typedError = promiseResult[ 0 ] as {\n\t\t\t\t\t\t\t\t\t\treason?: CoreDataError;\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t\tif ( typedError.reason?.message ) {\n\t\t\t\t\t\t\t\t\t\terrorMessage =\n\t\t\t\t\t\t\t\t\t\t\ttypedError.reason.message;\n\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\terrorMessage = __(\n\t\t\t\t\t\t\t\t\t\t\t'An error occurred while moving the item to the trash.'\n\t\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t// If we were trying to delete multiple items.\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tconst errorMessages = new Set();\n\t\t\t\t\t\t\t\t\tconst failedPromises = promiseResult.filter(\n\t\t\t\t\t\t\t\t\t\t( { status } ) => status === 'rejected'\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\tfor ( const failedPromise of failedPromises ) {\n\t\t\t\t\t\t\t\t\t\tconst typedError = failedPromise as {\n\t\t\t\t\t\t\t\t\t\t\treason?: CoreDataError;\n\t\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t\t\tif ( typedError.reason?.message ) {\n\t\t\t\t\t\t\t\t\t\t\terrorMessages.add(\n\t\t\t\t\t\t\t\t\t\t\t\ttypedError.reason.message\n\t\t\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\tif ( errorMessages.size === 0 ) {\n\t\t\t\t\t\t\t\t\t\terrorMessage = __(\n\t\t\t\t\t\t\t\t\t\t\t'An error occurred while moving the items to the trash.'\n\t\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\t} else if ( errorMessages.size === 1 ) {\n\t\t\t\t\t\t\t\t\t\terrorMessage = sprintf(\n\t\t\t\t\t\t\t\t\t\t\t/* translators: %s: an error message */\n\t\t\t\t\t\t\t\t\t\t\t__(\n\t\t\t\t\t\t\t\t\t\t\t\t'An error occurred while moving the item to the trash: %s'\n\t\t\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t\t\t\t[ ...errorMessages ][ 0 ]\n\t\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\terrorMessage = sprintf(\n\t\t\t\t\t\t\t\t\t\t\t/* translators: %s: a list of comma separated error messages */\n\t\t\t\t\t\t\t\t\t\t\t__(\n\t\t\t\t\t\t\t\t\t\t\t\t'Some errors occurred while moving the items to the trash: %s'\n\t\t\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t\t\t\t[ ...errorMessages ].join( ',' )\n\t\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tcreateErrorNotice( errorMessage, {\n\t\t\t\t\t\t\t\t\ttype: 'snackbar',\n\t\t\t\t\t\t\t\t} );\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif ( onActionPerformed ) {\n\t\t\t\t\t\t\t\tonActionPerformed( items );\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tsetIsBusy( false );\n\t\t\t\t\t\t\tcloseModal?.();\n\t\t\t\t\t\t} }\n\t\t\t\t\t\tisBusy={ isBusy }\n\t\t\t\t\t\tdisabled={ isBusy }\n\t\t\t\t\t\taccessibleWhenDisabled\n\t\t\t\t\t>\n\t\t\t\t\t\t{ _x( 'Trash', 'verb' ) }\n\t\t\t\t\t</Button>\n\t\t\t\t</HStack>\n\t\t\t</VStack>\n\t\t);\n\t},\n};\n\nexport default trashPost;\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,KAAK,QAAQ,kBAAkB;AACxC,SAASC,WAAW,QAAQ,iBAAiB;AAC7C,SAASC,KAAK,IA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CAAC,MAAM,IAAK+D,aAAa,CAACM,IAAI,KAAK,CAAC,EAAG;kBACtCV,YAAY,GAAGzD,OAAO,CACrB;kBACAF,EAAE,CACD,0DACD,CAAC,EACD,CAAE,GAAG+D,aAAa,CAAE,CAAE,CAAC,CACxB,CAAC;gBACF,CAAC,MAAM;kBACNJ,YAAY,GAAGzD,OAAO,CACrB;kBACAF,EAAE,CACD,8DACD,CAAC,EACD,CAAE,GAAG+D,aAAa,CAAE,CAACO,IAAI,CAAE,GAAI,CAChC,CAAC;gBACF;cACD;cACA9B,iBAAiB,CAAEmB,YAAY,EAAE;gBAChCjC,IAAI,EAAE;cACP,CAAE,CAAC;YACJ;YACA,IAAKU,iBAAiB,EAAG;cACxBA,iBAAiB,CAAEF,KAAM,CAAC;YAC3B;YACAI,SAAS,CAAE,KAAM,CAAC;YAClBH,UAAU,GAAG,CAAC;UACf,CAAG;UACHE,MAAM,EAAGA,MAAQ;UACjBY,QAAQ,EAAGZ,MAAQ;UACnBa,sBAAsB;UAAAP,QAAA,EAEpBxC,EAAE,CAAE,OAAO,EAAE,MAAO;QAAC,CAChB,CAAC;MAAA,CACF,CAAC;IAAA,CACF,CAAC;EAEX;AACD,CAAC;AAED,eAAegB,SAAS","ignoreList":[]}
@@ -7,21 +7,14 @@ import { decodeEntities } from '@wordpress/html-entities';
7
7
  * Internal dependencies
8
8
  */
9
9
 
10
- export const TEMPLATE_POST_TYPE = 'wp_template';
11
- export const TEMPLATE_PART_POST_TYPE = 'wp_template_part';
12
- export const TEMPLATE_ORIGINS = {
13
- custom: 'custom',
14
- theme: 'theme',
15
- plugin: 'plugin'
16
- };
17
10
  export function isTemplate(post) {
18
- return post.type === TEMPLATE_POST_TYPE;
11
+ return post.type === 'wp_template';
19
12
  }
20
13
  export function isTemplatePart(post) {
21
- return post.type === TEMPLATE_PART_POST_TYPE;
14
+ return post.type === 'wp_template_part';
22
15
  }
23
16
  export function isTemplateOrTemplatePart(p) {
24
- return p.type === TEMPLATE_POST_TYPE || p.type === TEMPLATE_PART_POST_TYPE;
17
+ return p.type === 'wp_template' || p.type === 'wp_template_part';
25
18
  }
26
19
  export function getItemTitle(item) {
27
20
  if (typeof item.title === 'string') {
@@ -49,6 +42,6 @@ export function isTemplateRemovable(template) {
49
42
  // In patterns list page we map the templates parts to a different object
50
43
  // than the one returned from the endpoint. This is why we need to check for
51
44
  // two props whether is custom or has a theme file.
52
- return [template.source, template.source].includes(TEMPLATE_ORIGINS.custom) && !Boolean(template.type === 'wp_template' && template?.plugin) && !template.has_theme_file;
45
+ return [template.source, template.source].includes('custom') && !Boolean(template.type === 'wp_template' && template?.plugin) && !template.has_theme_file;
53
46
  }
54
47
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["decodeEntities","TEMPLATE_POST_TYPE","TEMPLATE_PART_POST_TYPE","TEMPLATE_ORIGINS","custom","theme","plugin","isTemplate","post","type","isTemplatePart","isTemplateOrTemplatePart","p","getItemTitle","item","title","rendered","raw","isTemplateRemovable","template","source","includes","Boolean","has_theme_file"],"sources":["@wordpress/fields/src/actions/utils.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { decodeEntities } from '@wordpress/html-entities';\n\n/**\n * Internal dependencies\n */\nimport type { Post, TemplatePart, Template } from '../types';\n\nexport const TEMPLATE_POST_TYPE = 'wp_template';\nexport const TEMPLATE_PART_POST_TYPE = 'wp_template_part';\nexport const TEMPLATE_ORIGINS = {\n\tcustom: 'custom',\n\ttheme: 'theme',\n\tplugin: 'plugin',\n};\n\nexport function isTemplate( post: Post ): post is Template {\n\treturn post.type === TEMPLATE_POST_TYPE;\n}\n\nexport function isTemplatePart( post: Post ): post is TemplatePart {\n\treturn post.type === TEMPLATE_PART_POST_TYPE;\n}\n\nexport function isTemplateOrTemplatePart(\n\tp: Post\n): p is Template | TemplatePart {\n\treturn p.type === TEMPLATE_POST_TYPE || p.type === TEMPLATE_PART_POST_TYPE;\n}\n\nexport function getItemTitle( item: Post ) {\n\tif ( typeof item.title === 'string' ) {\n\t\treturn decodeEntities( item.title );\n\t}\n\tif ( item.title && 'rendered' in item.title ) {\n\t\treturn decodeEntities( item.title.rendered );\n\t}\n\tif ( item.title && 'raw' in item.title ) {\n\t\treturn decodeEntities( item.title.raw );\n\t}\n\treturn '';\n}\n\n/**\n * Check if a template is removable.\n *\n * @param template The template entity to check.\n * @return Whether the template is removable.\n */\nexport function isTemplateRemovable( template: Template | TemplatePart ) {\n\tif ( ! template ) {\n\t\treturn false;\n\t}\n\t// In patterns list page we map the templates parts to a different object\n\t// than the one returned from the endpoint. This is why we need to check for\n\t// two props whether is custom or has a theme file.\n\treturn (\n\t\t[ template.source, template.source ].includes(\n\t\t\tTEMPLATE_ORIGINS.custom\n\t\t) &&\n\t\t! Boolean( template.type === 'wp_template' && template?.plugin ) &&\n\t\t! template.has_theme_file\n\t);\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,cAAc,QAAQ,0BAA0B;;AAEzD;AACA;AACA;;AAGA,OAAO,MAAMC,kBAAkB,GAAG,aAAa;AAC/C,OAAO,MAAMC,uBAAuB,GAAG,kBAAkB;AACzD,OAAO,MAAMC,gBAAgB,GAAG;EAC/BC,MAAM,EAAE,QAAQ;EAChBC,KAAK,EAAE,OAAO;EACdC,MAAM,EAAE;AACT,CAAC;AAED,OAAO,SAASC,UAAUA,CAAEC,IAAU,EAAqB;EAC1D,OAAOA,IAAI,CAACC,IAAI,KAAKR,kBAAkB;AACxC;AAEA,OAAO,SAASS,cAAcA,CAAEF,IAAU,EAAyB;EAClE,OAAOA,IAAI,CAACC,IAAI,KAAKP,uBAAuB;AAC7C;AAEA,OAAO,SAASS,wBAAwBA,CACvCC,CAAO,EACwB;EAC/B,OAAOA,CAAC,CAACH,IAAI,KAAKR,kBAAkB,IAAIW,CAAC,CAACH,IAAI,KAAKP,uBAAuB;AAC3E;AAEA,OAAO,SAASW,YAAYA,CAAEC,IAAU,EAAG;EAC1C,IAAK,OAAOA,IAAI,CAACC,KAAK,KAAK,QAAQ,EAAG;IACrC,OAAOf,cAAc,CAAEc,IAAI,CAACC,KAAM,CAAC;EACpC;EACA,IAAKD,IAAI,CAACC,KAAK,IAAI,UAAU,IAAID,IAAI,CAACC,KAAK,EAAG;IAC7C,OAAOf,cAAc,CAAEc,IAAI,CAACC,KAAK,CAACC,QAAS,CAAC;EAC7C;EACA,IAAKF,IAAI,CAACC,KAAK,IAAI,KAAK,IAAID,IAAI,CAACC,KAAK,EAAG;IACxC,OAAOf,cAAc,CAAEc,IAAI,CAACC,KAAK,CAACE,GAAI,CAAC;EACxC;EACA,OAAO,EAAE;AACV;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,mBAAmBA,CAAEC,QAAiC,EAAG;EACxE,IAAK,CAAEA,QAAQ,EAAG;IACjB,OAAO,KAAK;EACb;EACA;EACA;EACA;EACA,OACC,CAAEA,QAAQ,CAACC,MAAM,EAAED,QAAQ,CAACC,MAAM,CAAE,CAACC,QAAQ,CAC5ClB,gBAAgB,CAACC,MAClB,CAAC,IACD,CAAEkB,OAAO,CAAEH,QAAQ,CAACV,IAAI,KAAK,aAAa,IAAIU,QAAQ,EAAEb,MAAO,CAAC,IAChE,CAAEa,QAAQ,CAACI,cAAc;AAE3B","ignoreList":[]}
1
+ {"version":3,"names":["decodeEntities","isTemplate","post","type","isTemplatePart","isTemplateOrTemplatePart","p","getItemTitle","item","title","rendered","raw","isTemplateRemovable","template","source","includes","Boolean","plugin","has_theme_file"],"sources":["@wordpress/fields/src/actions/utils.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { decodeEntities } from '@wordpress/html-entities';\n\n/**\n * Internal dependencies\n */\nimport type { Post, TemplatePart, Template } from '../types';\n\nexport function isTemplate( post: Post ): post is Template {\n\treturn post.type === 'wp_template';\n}\n\nexport function isTemplatePart( post: Post ): post is TemplatePart {\n\treturn post.type === 'wp_template_part';\n}\n\nexport function isTemplateOrTemplatePart(\n\tp: Post\n): p is Template | TemplatePart {\n\treturn p.type === 'wp_template' || p.type === 'wp_template_part';\n}\n\nexport function getItemTitle( item: {\n\ttitle: string | { rendered: string } | { raw: string };\n} ) {\n\tif ( typeof item.title === 'string' ) {\n\t\treturn decodeEntities( item.title );\n\t}\n\tif ( item.title && 'rendered' in item.title ) {\n\t\treturn decodeEntities( item.title.rendered );\n\t}\n\tif ( item.title && 'raw' in item.title ) {\n\t\treturn decodeEntities( item.title.raw );\n\t}\n\treturn '';\n}\n\n/**\n * Check if a template is removable.\n *\n * @param template The template entity to check.\n * @return Whether the template is removable.\n */\nexport function isTemplateRemovable( template: Template | TemplatePart ) {\n\tif ( ! template ) {\n\t\treturn false;\n\t}\n\t// In patterns list page we map the templates parts to a different object\n\t// than the one returned from the endpoint. This is why we need to check for\n\t// two props whether is custom or has a theme file.\n\treturn (\n\t\t[ template.source, template.source ].includes( 'custom' ) &&\n\t\t! Boolean( template.type === 'wp_template' && template?.plugin ) &&\n\t\t! template.has_theme_file\n\t);\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,cAAc,QAAQ,0BAA0B;;AAEzD;AACA;AACA;;AAGA,OAAO,SAASC,UAAUA,CAAEC,IAAU,EAAqB;EAC1D,OAAOA,IAAI,CAACC,IAAI,KAAK,aAAa;AACnC;AAEA,OAAO,SAASC,cAAcA,CAAEF,IAAU,EAAyB;EAClE,OAAOA,IAAI,CAACC,IAAI,KAAK,kBAAkB;AACxC;AAEA,OAAO,SAASE,wBAAwBA,CACvCC,CAAO,EACwB;EAC/B,OAAOA,CAAC,CAACH,IAAI,KAAK,aAAa,IAAIG,CAAC,CAACH,IAAI,KAAK,kBAAkB;AACjE;AAEA,OAAO,SAASI,YAAYA,CAAEC,IAE7B,EAAG;EACH,IAAK,OAAOA,IAAI,CAACC,KAAK,KAAK,QAAQ,EAAG;IACrC,OAAOT,cAAc,CAAEQ,IAAI,CAACC,KAAM,CAAC;EACpC;EACA,IAAKD,IAAI,CAACC,KAAK,IAAI,UAAU,IAAID,IAAI,CAACC,KAAK,EAAG;IAC7C,OAAOT,cAAc,CAAEQ,IAAI,CAACC,KAAK,CAACC,QAAS,CAAC;EAC7C;EACA,IAAKF,IAAI,CAACC,KAAK,IAAI,KAAK,IAAID,IAAI,CAACC,KAAK,EAAG;IACxC,OAAOT,cAAc,CAAEQ,IAAI,CAACC,KAAK,CAACE,GAAI,CAAC;EACxC;EACA,OAAO,EAAE;AACV;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,mBAAmBA,CAAEC,QAAiC,EAAG;EACxE,IAAK,CAAEA,QAAQ,EAAG;IACjB,OAAO,KAAK;EACb;EACA;EACA;EACA;EACA,OACC,CAAEA,QAAQ,CAACC,MAAM,EAAED,QAAQ,CAACC,MAAM,CAAE,CAACC,QAAQ,CAAE,QAAS,CAAC,IACzD,CAAEC,OAAO,CAAEH,QAAQ,CAACV,IAAI,KAAK,aAAa,IAAIU,QAAQ,EAAEI,MAAO,CAAC,IAChE,CAAEJ,QAAQ,CAACK,cAAc;AAE3B","ignoreList":[]}
@@ -0,0 +1,204 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { Icon, BaseControl, TextControl, Flex, FlexItem, FlexBlock, Button, Modal, __experimentalRadioGroup as RadioGroup, __experimentalRadio as Radio, __experimentalHStack as HStack, __experimentalVStack as VStack } from '@wordpress/components';
5
+ import { useInstanceId } from '@wordpress/compose';
6
+ import { store as coreStore } from '@wordpress/core-data';
7
+ import { useDispatch, useSelect } from '@wordpress/data';
8
+ import { useState } from '@wordpress/element';
9
+ import { __ } from '@wordpress/i18n';
10
+ import { check, footer as footerIcon, header as headerIcon, sidebar as sidebarIcon, symbolFilled as symbolFilledIcon } from '@wordpress/icons';
11
+ import { store as noticesStore } from '@wordpress/notices';
12
+ // @ts-ignore
13
+ import { serialize } from '@wordpress/blocks';
14
+
15
+ /**
16
+ * Internal dependencies
17
+ */
18
+ import { getCleanTemplatePartSlug, getUniqueTemplatePartTitle, useExistingTemplateParts } from './utils';
19
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
20
+ /**
21
+ * A React component that renders a modal for creating a template part. The modal displays a title and the contents for creating the template part.
22
+ * This component should not live in this package, it should be moved to a dedicated package responsible for managing template.
23
+ * @param {Object} props The component props.
24
+ * @param props.modalTitle
25
+ */
26
+ export default function CreateTemplatePartModal({
27
+ modalTitle,
28
+ ...restProps
29
+ }) {
30
+ const defaultModalTitle = useSelect(select =>
31
+ // @ts-ignore
32
+ select(coreStore).getPostType('wp_template_part')?.labels?.add_new_item, []);
33
+ return /*#__PURE__*/_jsx(Modal, {
34
+ title: modalTitle || defaultModalTitle,
35
+ onRequestClose: restProps.closeModal,
36
+ overlayClassName: "fields-create-template-part-modal",
37
+ focusOnMount: "firstContentElement",
38
+ size: "medium",
39
+ children: /*#__PURE__*/_jsx(CreateTemplatePartModalContents, {
40
+ ...restProps
41
+ })
42
+ });
43
+ }
44
+ const getTemplatePartIcon = iconName => {
45
+ if ('header' === iconName) {
46
+ return headerIcon;
47
+ } else if ('footer' === iconName) {
48
+ return footerIcon;
49
+ } else if ('sidebar' === iconName) {
50
+ return sidebarIcon;
51
+ }
52
+ return symbolFilledIcon;
53
+ };
54
+
55
+ /**
56
+ * A React component that renders the content of a model for creating a template part.
57
+ * This component should not live in this package; it should be moved to a dedicated package responsible for managing template.
58
+ *
59
+ * @param {Object} props - The component props.
60
+ * @param {string} [props.defaultArea=uncategorized] - The default area for the template part.
61
+ * @param {Array} [props.blocks=[]] - The blocks to be included in the template part.
62
+ * @param {string} [props.confirmLabel='Add'] - The label for the confirm button.
63
+ * @param {Function} props.closeModal - Function to close the modal.
64
+ * @param {Function} props.onCreate - Function to call when the template part is successfully created.
65
+ * @param {Function} [props.onError] - Function to call when there is an error creating the template part.
66
+ * @param {string} [props.defaultTitle=''] - The default title for the template part.
67
+ */
68
+ export function CreateTemplatePartModalContents({
69
+ defaultArea = 'uncategorized',
70
+ blocks = [],
71
+ confirmLabel = __('Add'),
72
+ closeModal,
73
+ onCreate,
74
+ onError,
75
+ defaultTitle = ''
76
+ }) {
77
+ const {
78
+ createErrorNotice
79
+ } = useDispatch(noticesStore);
80
+ const {
81
+ saveEntityRecord
82
+ } = useDispatch(coreStore);
83
+ const existingTemplateParts = useExistingTemplateParts();
84
+ const [title, setTitle] = useState(defaultTitle);
85
+ const [area, setArea] = useState(defaultArea);
86
+ const [isSubmitting, setIsSubmitting] = useState(false);
87
+ const instanceId = useInstanceId(CreateTemplatePartModal);
88
+ const defaultTemplatePartAreas = useSelect(select => {
89
+ const areas =
90
+ // @ts-expect-error getEntityRecord is not typed with unstableBase as argument.
91
+ select(coreStore).getEntityRecord('root', '__unstableBase')?.default_template_part_areas || [];
92
+ return areas.map(item => {
93
+ return {
94
+ ...item,
95
+ icon: getTemplatePartIcon(item.icon)
96
+ };
97
+ });
98
+ }, []);
99
+ async function createTemplatePart() {
100
+ if (!title || isSubmitting) {
101
+ return;
102
+ }
103
+ try {
104
+ setIsSubmitting(true);
105
+ const uniqueTitle = getUniqueTemplatePartTitle(title, existingTemplateParts);
106
+ const cleanSlug = getCleanTemplatePartSlug(uniqueTitle);
107
+ const templatePart = await saveEntityRecord('postType', 'wp_template_part', {
108
+ slug: cleanSlug,
109
+ title: uniqueTitle,
110
+ content: serialize(blocks),
111
+ area
112
+ }, {
113
+ throwOnError: true
114
+ });
115
+ await onCreate(templatePart);
116
+
117
+ // TODO: Add a success notice?
118
+ } catch (error) {
119
+ const errorMessage = error instanceof Error && 'code' in error && error.message && error.code !== 'unknown_error' ? error.message : __('An error occurred while creating the template part.');
120
+ createErrorNotice(errorMessage, {
121
+ type: 'snackbar'
122
+ });
123
+ onError?.();
124
+ } finally {
125
+ setIsSubmitting(false);
126
+ }
127
+ }
128
+ return /*#__PURE__*/_jsx("form", {
129
+ onSubmit: async event => {
130
+ event.preventDefault();
131
+ await createTemplatePart();
132
+ },
133
+ children: /*#__PURE__*/_jsxs(VStack, {
134
+ spacing: "4",
135
+ children: [/*#__PURE__*/_jsx(TextControl, {
136
+ __next40pxDefaultSize: true,
137
+ __nextHasNoMarginBottom: true,
138
+ label: __('Name'),
139
+ value: title,
140
+ onChange: setTitle,
141
+ required: true
142
+ }), /*#__PURE__*/_jsx(BaseControl, {
143
+ __nextHasNoMarginBottom: true,
144
+ label: __('Area'),
145
+ id: `fields-create-template-part-modal__area-selection-${instanceId}`,
146
+ className: "fields-create-template-part-modal__area-base-control",
147
+ children: /*#__PURE__*/_jsx(RadioGroup, {
148
+ label: __('Area'),
149
+ className: "fields-create-template-part-modal__area-radio-group",
150
+ id: `fields-create-template-part-modal__area-selection-${instanceId}`,
151
+ onChange: value => value && typeof value === 'string' ? setArea(value) : () => void 0,
152
+ checked: area,
153
+ children: defaultTemplatePartAreas.map(({
154
+ icon,
155
+ label,
156
+ area: value,
157
+ description
158
+ }) => /*#__PURE__*/_jsx(Radio, {
159
+ __next40pxDefaultSize: true,
160
+ value: value,
161
+ className: "fields-create-template-part-modal__area-radio",
162
+ children: /*#__PURE__*/_jsxs(Flex, {
163
+ align: "start",
164
+ justify: "start",
165
+ children: [/*#__PURE__*/_jsx(FlexItem, {
166
+ children: /*#__PURE__*/_jsx(Icon, {
167
+ icon: icon
168
+ })
169
+ }), /*#__PURE__*/_jsxs(FlexBlock, {
170
+ className: "fields-create-template-part-modal__option-label",
171
+ children: [label, /*#__PURE__*/_jsx("div", {
172
+ children: description
173
+ })]
174
+ }), /*#__PURE__*/_jsx(FlexItem, {
175
+ className: "fields-create-template-part-modal__checkbox",
176
+ children: area === value && /*#__PURE__*/_jsx(Icon, {
177
+ icon: check
178
+ })
179
+ })]
180
+ })
181
+ }, label))
182
+ })
183
+ }), /*#__PURE__*/_jsxs(HStack, {
184
+ justify: "right",
185
+ children: [/*#__PURE__*/_jsx(Button, {
186
+ __next40pxDefaultSize: true,
187
+ variant: "tertiary",
188
+ onClick: () => {
189
+ closeModal();
190
+ },
191
+ children: __('Cancel')
192
+ }), /*#__PURE__*/_jsx(Button, {
193
+ __next40pxDefaultSize: true,
194
+ variant: "primary",
195
+ type: "submit",
196
+ "aria-disabled": !title || isSubmitting,
197
+ isBusy: isSubmitting,
198
+ children: confirmLabel
199
+ })]
200
+ })]
201
+ })
202
+ });
203
+ }
204
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["Icon","BaseControl","TextControl","Flex","FlexItem","FlexBlock","Button","Modal","__experimentalRadioGroup","RadioGroup","__experimentalRadio","Radio","__experimentalHStack","HStack","__experimentalVStack","VStack","useInstanceId","store","coreStore","useDispatch","useSelect","useState","__","check","footer","footerIcon","header","headerIcon","sidebar","sidebarIcon","symbolFilled","symbolFilledIcon","noticesStore","serialize","getCleanTemplatePartSlug","getUniqueTemplatePartTitle","useExistingTemplateParts","jsx","_jsx","jsxs","_jsxs","CreateTemplatePartModal","modalTitle","restProps","defaultModalTitle","select","getPostType","labels","add_new_item","title","onRequestClose","closeModal","overlayClassName","focusOnMount","size","children","CreateTemplatePartModalContents","getTemplatePartIcon","iconName","defaultArea","blocks","confirmLabel","onCreate","onError","defaultTitle","createErrorNotice","saveEntityRecord","existingTemplateParts","setTitle","area","setArea","isSubmitting","setIsSubmitting","instanceId","defaultTemplatePartAreas","areas","getEntityRecord","default_template_part_areas","map","item","icon","createTemplatePart","uniqueTitle","cleanSlug","templatePart","slug","content","throwOnError","error","errorMessage","Error","message","code","type","onSubmit","event","preventDefault","spacing","__next40pxDefaultSize","__nextHasNoMarginBottom","label","value","onChange","required","id","className","checked","description","align","justify","variant","onClick","isBusy"],"sources":["@wordpress/fields/src/components/create-template-part-modal/index.tsx"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport {\n\tIcon,\n\tBaseControl,\n\tTextControl,\n\tFlex,\n\tFlexItem,\n\tFlexBlock,\n\tButton,\n\tModal,\n\t__experimentalRadioGroup as RadioGroup,\n\t__experimentalRadio as Radio,\n\t__experimentalHStack as HStack,\n\t__experimentalVStack as VStack,\n} from '@wordpress/components';\nimport { useInstanceId } from '@wordpress/compose';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport { useState } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\nimport {\n\tcheck,\n\tfooter as footerIcon,\n\theader as headerIcon,\n\tsidebar as sidebarIcon,\n\tsymbolFilled as symbolFilledIcon,\n} from '@wordpress/icons';\nimport { store as noticesStore } from '@wordpress/notices';\n// @ts-ignore\nimport { serialize } from '@wordpress/blocks';\n\n/**\n * Internal dependencies\n */\nimport {\n\tgetCleanTemplatePartSlug,\n\tgetUniqueTemplatePartTitle,\n\tuseExistingTemplateParts,\n} from './utils';\n\ntype CreateTemplatePartModalContentsProps = {\n\tdefaultArea?: string;\n\tblocks: any[];\n\tconfirmLabel?: string;\n\tcloseModal: () => void;\n\tonCreate: ( templatePart: any ) => void;\n\tonError?: () => void;\n\tdefaultTitle?: string;\n};\n\n/**\n * A React component that renders a modal for creating a template part. The modal displays a title and the contents for creating the template part.\n * This component should not live in this package, it should be moved to a dedicated package responsible for managing template.\n * @param {Object} props The component props.\n * @param props.modalTitle\n */\nexport default function CreateTemplatePartModal( {\n\tmodalTitle,\n\t...restProps\n}: {\n\tmodalTitle: string;\n} & CreateTemplatePartModalContentsProps ) {\n\tconst defaultModalTitle = useSelect(\n\t\t( select ) =>\n\t\t\t// @ts-ignore\n\t\t\tselect( coreStore ).getPostType( 'wp_template_part' )?.labels\n\t\t\t\t?.add_new_item,\n\t\t[]\n\t);\n\treturn (\n\t\t<Modal\n\t\t\ttitle={ modalTitle || defaultModalTitle }\n\t\t\tonRequestClose={ restProps.closeModal }\n\t\t\toverlayClassName=\"fields-create-template-part-modal\"\n\t\t\tfocusOnMount=\"firstContentElement\"\n\t\t\tsize=\"medium\"\n\t\t>\n\t\t\t{ /* @ts-ignore */ }\n\t\t\t<CreateTemplatePartModalContents { ...restProps } />\n\t\t</Modal>\n\t);\n}\n\nconst getTemplatePartIcon = ( iconName: string ) => {\n\tif ( 'header' === iconName ) {\n\t\treturn headerIcon;\n\t} else if ( 'footer' === iconName ) {\n\t\treturn footerIcon;\n\t} else if ( 'sidebar' === iconName ) {\n\t\treturn sidebarIcon;\n\t}\n\treturn symbolFilledIcon;\n};\n\n/**\n * A React component that renders the content of a model for creating a template part.\n * This component should not live in this package; it should be moved to a dedicated package responsible for managing template.\n *\n * @param {Object} props - The component props.\n * @param {string} [props.defaultArea=uncategorized] - The default area for the template part.\n * @param {Array} [props.blocks=[]] - The blocks to be included in the template part.\n * @param {string} [props.confirmLabel='Add'] - The label for the confirm button.\n * @param {Function} props.closeModal - Function to close the modal.\n * @param {Function} props.onCreate - Function to call when the template part is successfully created.\n * @param {Function} [props.onError] - Function to call when there is an error creating the template part.\n * @param {string} [props.defaultTitle=''] - The default title for the template part.\n */\nexport function CreateTemplatePartModalContents( {\n\tdefaultArea = 'uncategorized',\n\tblocks = [],\n\tconfirmLabel = __( 'Add' ),\n\tcloseModal,\n\tonCreate,\n\tonError,\n\tdefaultTitle = '',\n}: CreateTemplatePartModalContentsProps ) {\n\tconst { createErrorNotice } = useDispatch( noticesStore );\n\tconst { saveEntityRecord } = useDispatch( coreStore );\n\tconst existingTemplateParts = useExistingTemplateParts();\n\n\tconst [ title, setTitle ] = useState( defaultTitle );\n\tconst [ area, setArea ] = useState( defaultArea );\n\tconst [ isSubmitting, setIsSubmitting ] = useState( false );\n\tconst instanceId = useInstanceId( CreateTemplatePartModal );\n\n\tconst defaultTemplatePartAreas = useSelect( ( select ) => {\n\t\tconst areas =\n\t\t\t// @ts-expect-error getEntityRecord is not typed with unstableBase as argument.\n\t\t\tselect( coreStore ).getEntityRecord< {\n\t\t\t\tdefault_template_part_areas: Array< {\n\t\t\t\t\tarea: string;\n\t\t\t\t\tlabel: string;\n\t\t\t\t\ticon: string;\n\t\t\t\t\tdescription: string;\n\t\t\t\t} >;\n\t\t\t} >( 'root', '__unstableBase' )?.default_template_part_areas || [];\n\n\t\treturn areas.map( ( item ) => {\n\t\t\treturn { ...item, icon: getTemplatePartIcon( item.icon ) };\n\t\t} );\n\t}, [] );\n\n\tasync function createTemplatePart() {\n\t\tif ( ! title || isSubmitting ) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tsetIsSubmitting( true );\n\t\t\tconst uniqueTitle = getUniqueTemplatePartTitle(\n\t\t\t\ttitle,\n\t\t\t\texistingTemplateParts\n\t\t\t);\n\t\t\tconst cleanSlug = getCleanTemplatePartSlug( uniqueTitle );\n\n\t\t\tconst templatePart = await saveEntityRecord(\n\t\t\t\t'postType',\n\t\t\t\t'wp_template_part',\n\t\t\t\t{\n\t\t\t\t\tslug: cleanSlug,\n\t\t\t\t\ttitle: uniqueTitle,\n\t\t\t\t\tcontent: serialize( blocks ),\n\t\t\t\t\tarea,\n\t\t\t\t},\n\t\t\t\t{ throwOnError: true }\n\t\t\t);\n\t\t\tawait onCreate( templatePart );\n\n\t\t\t// TODO: Add a success notice?\n\t\t} catch ( error ) {\n\t\t\tconst errorMessage =\n\t\t\t\terror instanceof Error &&\n\t\t\t\t'code' in error &&\n\t\t\t\terror.message &&\n\t\t\t\terror.code !== 'unknown_error'\n\t\t\t\t\t? error.message\n\t\t\t\t\t: __(\n\t\t\t\t\t\t\t'An error occurred while creating the template part.'\n\t\t\t\t\t );\n\n\t\t\tcreateErrorNotice( errorMessage, { type: 'snackbar' } );\n\n\t\t\tonError?.();\n\t\t} finally {\n\t\t\tsetIsSubmitting( false );\n\t\t}\n\t}\n\treturn (\n\t\t<form\n\t\t\tonSubmit={ async ( event ) => {\n\t\t\t\tevent.preventDefault();\n\t\t\t\tawait createTemplatePart();\n\t\t\t} }\n\t\t>\n\t\t\t<VStack spacing=\"4\">\n\t\t\t\t<TextControl\n\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t__nextHasNoMarginBottom\n\t\t\t\t\tlabel={ __( 'Name' ) }\n\t\t\t\t\tvalue={ title }\n\t\t\t\t\tonChange={ setTitle }\n\t\t\t\t\trequired\n\t\t\t\t/>\n\t\t\t\t<BaseControl\n\t\t\t\t\t__nextHasNoMarginBottom\n\t\t\t\t\tlabel={ __( 'Area' ) }\n\t\t\t\t\tid={ `fields-create-template-part-modal__area-selection-${ instanceId }` }\n\t\t\t\t\tclassName=\"fields-create-template-part-modal__area-base-control\"\n\t\t\t\t>\n\t\t\t\t\t<RadioGroup\n\t\t\t\t\t\tlabel={ __( 'Area' ) }\n\t\t\t\t\t\tclassName=\"fields-create-template-part-modal__area-radio-group\"\n\t\t\t\t\t\tid={ `fields-create-template-part-modal__area-selection-${ instanceId }` }\n\t\t\t\t\t\tonChange={ ( value ) =>\n\t\t\t\t\t\t\tvalue && typeof value === 'string'\n\t\t\t\t\t\t\t\t? setArea( value )\n\t\t\t\t\t\t\t\t: () => void 0\n\t\t\t\t\t\t}\n\t\t\t\t\t\tchecked={ area }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ defaultTemplatePartAreas.map(\n\t\t\t\t\t\t\t( { icon, label, area: value, description } ) => (\n\t\t\t\t\t\t\t\t<Radio\n\t\t\t\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\t\t\t\tkey={ label }\n\t\t\t\t\t\t\t\t\tvalue={ value }\n\t\t\t\t\t\t\t\t\tclassName=\"fields-create-template-part-modal__area-radio\"\n\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t<Flex align=\"start\" justify=\"start\">\n\t\t\t\t\t\t\t\t\t\t<FlexItem>\n\t\t\t\t\t\t\t\t\t\t\t<Icon icon={ icon } />\n\t\t\t\t\t\t\t\t\t\t</FlexItem>\n\t\t\t\t\t\t\t\t\t\t<FlexBlock className=\"fields-create-template-part-modal__option-label\">\n\t\t\t\t\t\t\t\t\t\t\t{ label }\n\t\t\t\t\t\t\t\t\t\t\t<div>{ description }</div>\n\t\t\t\t\t\t\t\t\t\t</FlexBlock>\n\n\t\t\t\t\t\t\t\t\t\t<FlexItem className=\"fields-create-template-part-modal__checkbox\">\n\t\t\t\t\t\t\t\t\t\t\t{ area === value && (\n\t\t\t\t\t\t\t\t\t\t\t\t<Icon icon={ check } />\n\t\t\t\t\t\t\t\t\t\t\t) }\n\t\t\t\t\t\t\t\t\t\t</FlexItem>\n\t\t\t\t\t\t\t\t\t</Flex>\n\t\t\t\t\t\t\t\t</Radio>\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t) }\n\t\t\t\t\t</RadioGroup>\n\t\t\t\t</BaseControl>\n\t\t\t\t<HStack justify=\"right\">\n\t\t\t\t\t<Button\n\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\tvariant=\"tertiary\"\n\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\tcloseModal();\n\t\t\t\t\t\t} }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ __( 'Cancel' ) }\n\t\t\t\t\t</Button>\n\t\t\t\t\t<Button\n\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\tvariant=\"primary\"\n\t\t\t\t\t\ttype=\"submit\"\n\t\t\t\t\t\taria-disabled={ ! title || isSubmitting }\n\t\t\t\t\t\tisBusy={ isSubmitting }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ confirmLabel }\n\t\t\t\t\t</Button>\n\t\t\t\t</HStack>\n\t\t\t</VStack>\n\t\t</form>\n\t);\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SACCA,IAAI,EACJC,WAAW,EACXC,WAAW,EACXC,IAAI,EACJC,QAAQ,EACRC,SAAS,EACTC,MAAM,EACNC,KAAK,EACLC,wBAAwB,IAAIC,UAAU,EACtCC,mBAAmB,IAAIC,KAAK,EAC5BC,oBAAoB,IAAIC,MAAM,EAC9BC,oBAAoB,IAAIC,MAAM,QACxB,uBAAuB;AAC9B,SAASC,aAAa,QAAQ,oBAAoB;AAClD,SAASC,KAAK,IAAIC,SAAS,QAAQ,sBAAsB;AACzD,SAASC,WAAW,EAAEC,SAAS,QAAQ,iBAAiB;AACxD,SAASC,QAAQ,QAAQ,oB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6CAA6C;gBAAAlD,QAAA,EAC9Dc,IAAI,KAAKgC,KAAK,iBACf/D,IAAA,CAACtC,IAAI;kBAACgF,IAAI,EAAGzD;gBAAO,CAAE;cACtB,CACQ,CAAC;YAAA,CACN;UAAC,GAlBD6E,KAmBA,CAET;QAAC,CACU;MAAC,CACD,CAAC,eACd5D,KAAA,CAAC3B,MAAM;QAACgG,OAAO,EAAC,OAAO;QAAAtD,QAAA,gBACtBjB,IAAA,CAAChC,MAAM;UACN4F,qBAAqB;UACrBY,OAAO,EAAC,UAAU;UAClBC,OAAO,EAAGA,CAAA,KAAM;YACf5D,UAAU,CAAC,CAAC;UACb,CAAG;UAAAI,QAAA,EAEDjC,EAAE,CAAE,QAAS;QAAC,CACT,CAAC,eACTgB,IAAA,CAAChC,MAAM;UACN4F,qBAAqB;UACrBY,OAAO,EAAC,SAAS;UACjBjB,IAAI,EAAC,QAAQ;UACb,iBAAgB,CAAE5C,KAAK,IAAIsB,YAAc;UACzCyC,MAAM,EAAGzC,YAAc;UAAAhB,QAAA,EAErBM;QAAY,CACP,CAAC;MAAA,CACF,CAAC;IAAA,CACF;EAAC,CACJ,CAAC;AAET","ignoreList":[]}
@@ -0,0 +1,55 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { paramCase as kebabCase } from 'change-case';
5
+
6
+ /**
7
+ * WordPress dependencies
8
+ */
9
+ import { useSelect } from '@wordpress/data';
10
+ import { store as coreStore } from '@wordpress/core-data';
11
+
12
+ /**
13
+ * Internal dependencies
14
+ */
15
+
16
+ export const useExistingTemplateParts = () => {
17
+ var _useSelect;
18
+ return (_useSelect = useSelect(select => select(coreStore).getEntityRecords('postType', 'wp_template_part', {
19
+ per_page: -1
20
+ }), [])) !== null && _useSelect !== void 0 ? _useSelect : [];
21
+ };
22
+
23
+ /**
24
+ * Return a unique template part title based on
25
+ * the given title and existing template parts.
26
+ *
27
+ * @param {string} title The original template part title.
28
+ * @param {Object} templateParts The array of template part entities.
29
+ * @return {string} A unique template part title.
30
+ */
31
+ export const getUniqueTemplatePartTitle = (title, templateParts) => {
32
+ const lowercaseTitle = title.toLowerCase();
33
+ const existingTitles = templateParts.map(templatePart => templatePart.title.rendered.toLowerCase());
34
+ if (!existingTitles.includes(lowercaseTitle)) {
35
+ return title;
36
+ }
37
+ let suffix = 2;
38
+ while (existingTitles.includes(`${lowercaseTitle} ${suffix}`)) {
39
+ suffix++;
40
+ }
41
+ return `${title} ${suffix}`;
42
+ };
43
+
44
+ /**
45
+ * Get a valid slug for a template part.
46
+ * Currently template parts only allow latin chars.
47
+ * The fallback slug will receive suffix by default.
48
+ *
49
+ * @param {string} title The template part title.
50
+ * @return {string} A valid template part slug.
51
+ */
52
+ export const getCleanTemplatePartSlug = title => {
53
+ return kebabCase(title).replace(/[^\w-]+/g, '') || 'wp-custom-part';
54
+ };
55
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["paramCase","kebabCase","useSelect","store","coreStore","useExistingTemplateParts","_useSelect","select","getEntityRecords","per_page","getUniqueTemplatePartTitle","title","templateParts","lowercaseTitle","toLowerCase","existingTitles","map","templatePart","rendered","includes","suffix","getCleanTemplatePartSlug","replace"],"sources":["@wordpress/fields/src/components/create-template-part-modal/utils.js"],"sourcesContent":["/**\n * External dependencies\n */\nimport { paramCase as kebabCase } from 'change-case';\n\n/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\n\n/**\n * Internal dependencies\n */\n\nexport const useExistingTemplateParts = () => {\n\treturn (\n\t\tuseSelect(\n\t\t\t( select ) =>\n\t\t\t\tselect( coreStore ).getEntityRecords(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_template_part',\n\t\t\t\t\t{\n\t\t\t\t\t\tper_page: -1,\n\t\t\t\t\t}\n\t\t\t\t),\n\t\t\t[]\n\t\t) ?? []\n\t);\n};\n\n/**\n * Return a unique template part title based on\n * the given title and existing template parts.\n *\n * @param {string} title The original template part title.\n * @param {Object} templateParts The array of template part entities.\n * @return {string} A unique template part title.\n */\nexport const getUniqueTemplatePartTitle = ( title, templateParts ) => {\n\tconst lowercaseTitle = title.toLowerCase();\n\tconst existingTitles = templateParts.map( ( templatePart ) =>\n\t\ttemplatePart.title.rendered.toLowerCase()\n\t);\n\n\tif ( ! existingTitles.includes( lowercaseTitle ) ) {\n\t\treturn title;\n\t}\n\n\tlet suffix = 2;\n\twhile ( existingTitles.includes( `${ lowercaseTitle } ${ suffix }` ) ) {\n\t\tsuffix++;\n\t}\n\n\treturn `${ title } ${ suffix }`;\n};\n\n/**\n * Get a valid slug for a template part.\n * Currently template parts only allow latin chars.\n * The fallback slug will receive suffix by default.\n *\n * @param {string} title The template part title.\n * @return {string} A valid template part slug.\n */\nexport const getCleanTemplatePartSlug = ( title ) => {\n\treturn kebabCase( title ).replace( /[^\\w-]+/g, '' ) || 'wp-custom-part';\n};\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,SAAS,IAAIC,SAAS,QAAQ,aAAa;;AAEpD;AACA;AACA;AACA,SAASC,SAAS,QAAQ,iBAAiB;AAC3C,SAASC,KAAK,IAAIC,SAAS,QAAQ,sBAAsB;;AAEzD;AACA;AACA;;AAEA,OAAO,MAAMC,wBAAwB,GAAGA,CAAA,KAAM;EAAA,IAAAC,UAAA;EAC7C,QAAAA,UAAA,GACCJ,SAAS,CACNK,MAAM,IACPA,MAAM,CAAEH,SAAU,CAAC,CAACI,gBAAgB,CACnC,UAAU,EACV,kBAAkB,EAClB;IACCC,QAAQ,EAAE,CAAC;EACZ,CACD,CAAC,EACF,EACD,CAAC,cAAAH,UAAA,cAAAA,UAAA,GAAI,EAAE;AAET,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMI,0BAA0B,GAAGA,CAAEC,KAAK,EAAEC,aAAa,KAAM;EACrE,MAAMC,cAAc,GAAGF,KAAK,CAACG,WAAW,CAAC,CAAC;EAC1C,MAAMC,cAAc,GAAGH,aAAa,CAACI,GAAG,CAAIC,YAAY,IACvDA,YAAY,CAACN,KAAK,CAACO,QAAQ,CAACJ,WAAW,CAAC,CACzC,CAAC;EAED,IAAK,CAAEC,cAAc,CAACI,QAAQ,CAAEN,cAAe,CAAC,EAAG;IAClD,OAAOF,KAAK;EACb;EAEA,IAAIS,MAAM,GAAG,CAAC;EACd,OAAQL,cAAc,CAACI,QAAQ,CAAE,GAAIN,cAAc,IAAMO,MAAM,EAAI,CAAC,EAAG;IACtEA,MAAM,EAAE;EACT;EAEA,OAAO,GAAIT,KAAK,IAAMS,MAAM,EAAG;AAChC,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,wBAAwB,GAAKV,KAAK,IAAM;EACpD,OAAOV,SAAS,CAAEU,KAAM,CAAC,CAACW,OAAO,CAAE,UAAU,EAAE,EAAG,CAAC,IAAI,gBAAgB;AACxE,CAAC","ignoreList":[]}
@@ -2,6 +2,7 @@ export { default as slugField } from './slug';
2
2
  export { default as titleField } from './title';
3
3
  export { default as orderField } from './order';
4
4
  export { default as featuredImageField } from './featured-image';
5
+ export { default as templateField } from './template';
5
6
  export { default as parentField } from './parent';
6
7
  export { default as passwordField } from './password';
7
8
  export { default as statusField } from './status';
@@ -1 +1 @@
1
- {"version":3,"names":["default","slugField","titleField","orderField","featuredImageField","parentField","passwordField","statusField","commentStatusField","dateField","authorField"],"sources":["@wordpress/fields/src/fields/index.ts"],"sourcesContent":["export { default as slugField } from './slug';\nexport { default as titleField } from './title';\nexport { default as orderField } from './order';\nexport { default as featuredImageField } from './featured-image';\nexport { default as parentField } from './parent';\nexport { default as passwordField } from './password';\nexport { default as statusField } from './status';\nexport { default as commentStatusField } from './comment-status';\nexport { default as dateField } from './date';\nexport { default as authorField } from './author';\n"],"mappings":"AAAA,SAASA,OAAO,IAAIC,SAAS,QAAQ,QAAQ;AAC7C,SAASD,OAAO,IAAIE,UAAU,QAAQ,SAAS;AAC/C,SAASF,OAAO,IAAIG,UAAU,QAAQ,SAAS;AAC/C,SAASH,OAAO,IAAII,kBAAkB,QAAQ,kBAAkB;AAChE,SAASJ,OAAO,IAAIK,WAAW,QAAQ,UAAU;AACjD,SAASL,OAAO,IAAIM,aAAa,QAAQ,YAAY;AACrD,SAASN,OAAO,IAAIO,WAAW,QAAQ,UAAU;AACjD,SAASP,OAAO,IAAIQ,kBAAkB,QAAQ,kBAAkB;AAChE,SAASR,OAAO,IAAIS,SAAS,QAAQ,QAAQ;AAC7C,SAAST,OAAO,IAAIU,WAAW,QAAQ,UAAU","ignoreList":[]}
1
+ {"version":3,"names":["default","slugField","titleField","orderField","featuredImageField","templateField","parentField","passwordField","statusField","commentStatusField","dateField","authorField"],"sources":["@wordpress/fields/src/fields/index.ts"],"sourcesContent":["export { default as slugField } from './slug';\nexport { default as titleField } from './title';\nexport { default as orderField } from './order';\nexport { default as featuredImageField } from './featured-image';\nexport { default as templateField } from './template';\nexport { default as parentField } from './parent';\nexport { default as passwordField } from './password';\nexport { default as statusField } from './status';\nexport { default as commentStatusField } from './comment-status';\nexport { default as dateField } from './date';\nexport { default as authorField } from './author';\n"],"mappings":"AAAA,SAASA,OAAO,IAAIC,SAAS,QAAQ,QAAQ;AAC7C,SAASD,OAAO,IAAIE,UAAU,QAAQ,SAAS;AAC/C,SAASF,OAAO,IAAIG,UAAU,QAAQ,SAAS;AAC/C,SAASH,OAAO,IAAII,kBAAkB,QAAQ,kBAAkB;AAChE,SAASJ,OAAO,IAAIK,aAAa,QAAQ,YAAY;AACrD,SAASL,OAAO,IAAIM,WAAW,QAAQ,UAAU;AACjD,SAASN,OAAO,IAAIO,aAAa,QAAQ,YAAY;AACrD,SAASP,OAAO,IAAIQ,WAAW,QAAQ,UAAU;AACjD,SAASR,OAAO,IAAIS,kBAAkB,QAAQ,kBAAkB;AAChE,SAAST,OAAO,IAAIU,SAAS,QAAQ,QAAQ;AAC7C,SAASV,OAAO,IAAIW,WAAW,QAAQ,UAAU","ignoreList":[]}
@@ -11,7 +11,7 @@ import { getSlug } from './utils';
11
11
  const SlugView = ({
12
12
  item
13
13
  }) => {
14
- const slug = typeof item === 'object' ? getSlug(item) : '';
14
+ const slug = getSlug(item);
15
15
  const originalSlugRef = useRef(slug);
16
16
  useEffect(() => {
17
17
  if (slug && originalSlugRef.current === undefined) {
@@ -1 +1 @@
1
- {"version":3,"names":["useEffect","useRef","getSlug","SlugView","item","slug","originalSlugRef","current","undefined","slugToDisplay"],"sources":["@wordpress/fields/src/fields/slug/slug-view.tsx"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useEffect, useRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport type { BasePost } from '../../types';\nimport { getSlug } from './utils';\n\nconst SlugView = ( { item }: { item: BasePost } ) => {\n\tconst slug = typeof item === 'object' ? getSlug( item ) : '';\n\tconst originalSlugRef = useRef( slug );\n\n\tuseEffect( () => {\n\t\tif ( slug && originalSlugRef.current === undefined ) {\n\t\t\toriginalSlugRef.current = slug;\n\t\t}\n\t}, [ slug ] );\n\n\tconst slugToDisplay = slug || originalSlugRef.current;\n\n\treturn `${ slugToDisplay }`;\n};\n\nexport default SlugView;\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,SAAS,EAAEC,MAAM,QAAQ,oBAAoB;;AAEtD;AACA;AACA;;AAEA,SAASC,OAAO,QAAQ,SAAS;AAEjC,MAAMC,QAAQ,GAAGA,CAAE;EAAEC;AAAyB,CAAC,KAAM;EACpD,MAAMC,IAAI,GAAG,OAAOD,IAAI,KAAK,QAAQ,GAAGF,OAAO,CAAEE,IAAK,CAAC,GAAG,EAAE;EAC5D,MAAME,eAAe,GAAGL,MAAM,CAAEI,IAAK,CAAC;EAEtCL,SAAS,CAAE,MAAM;IAChB,IAAKK,IAAI,IAAIC,eAAe,CAACC,OAAO,KAAKC,SAAS,EAAG;MACpDF,eAAe,CAACC,OAAO,GAAGF,IAAI;IAC/B;EACD,CAAC,EAAE,CAAEA,IAAI,CAAG,CAAC;EAEb,MAAMI,aAAa,GAAGJ,IAAI,IAAIC,eAAe,CAACC,OAAO;EAErD,OAAO,GAAIE,aAAa,EAAG;AAC5B,CAAC;AAED,eAAeN,QAAQ","ignoreList":[]}
1
+ {"version":3,"names":["useEffect","useRef","getSlug","SlugView","item","slug","originalSlugRef","current","undefined","slugToDisplay"],"sources":["@wordpress/fields/src/fields/slug/slug-view.tsx"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useEffect, useRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport type { BasePost } from '../../types';\nimport { getSlug } from './utils';\n\nconst SlugView = ( { item }: { item: BasePost } ) => {\n\tconst slug = getSlug( item );\n\tconst originalSlugRef = useRef( slug );\n\n\tuseEffect( () => {\n\t\tif ( slug && originalSlugRef.current === undefined ) {\n\t\t\toriginalSlugRef.current = slug;\n\t\t}\n\t}, [ slug ] );\n\n\tconst slugToDisplay = slug || originalSlugRef.current;\n\n\treturn `${ slugToDisplay }`;\n};\n\nexport default SlugView;\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,SAAS,EAAEC,MAAM,QAAQ,oBAAoB;;AAEtD;AACA;AACA;;AAEA,SAASC,OAAO,QAAQ,SAAS;AAEjC,MAAMC,QAAQ,GAAGA,CAAE;EAAEC;AAAyB,CAAC,KAAM;EACpD,MAAMC,IAAI,GAAGH,OAAO,CAAEE,IAAK,CAAC;EAC5B,MAAME,eAAe,GAAGL,MAAM,CAAEI,IAAK,CAAC;EAEtCL,SAAS,CAAE,MAAM;IAChB,IAAKK,IAAI,IAAIC,eAAe,CAACC,OAAO,KAAKC,SAAS,EAAG;MACpDF,eAAe,CAACC,OAAO,GAAGF,IAAI;IAC/B;EACD,CAAC,EAAE,CAAEA,IAAI,CAAG,CAAC;EAEb,MAAMI,aAAa,GAAGJ,IAAI,IAAIC,eAAe,CAACC,OAAO;EAErD,OAAO,GAAIE,aAAa,EAAG;AAC5B,CAAC;AAED,eAAeN,QAAQ","ignoreList":[]}
@@ -8,6 +8,9 @@ import { cleanForSlug } from '@wordpress/url';
8
8
 
9
9
  import { getItemTitle } from '../../actions/utils';
10
10
  export const getSlug = item => {
11
+ if (typeof item !== 'object') {
12
+ return '';
13
+ }
11
14
  return item.slug || cleanForSlug(getItemTitle(item)) || item.id.toString();
12
15
  };
13
16
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["cleanForSlug","getItemTitle","getSlug","item","slug","id","toString"],"sources":["@wordpress/fields/src/fields/slug/utils.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { cleanForSlug } from '@wordpress/url';\n/**\n * Internal dependencies\n */\nimport type { BasePost } from '../../types';\nimport { getItemTitle } from '../../actions/utils';\n\nexport const getSlug = ( item: BasePost ): string => {\n\treturn (\n\t\titem.slug || cleanForSlug( getItemTitle( item ) ) || item.id.toString()\n\t);\n};\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,YAAY,QAAQ,gBAAgB;AAC7C;AACA;AACA;;AAEA,SAASC,YAAY,QAAQ,qBAAqB;AAElD,OAAO,MAAMC,OAAO,GAAKC,IAAc,IAAc;EACpD,OACCA,IAAI,CAACC,IAAI,IAAIJ,YAAY,CAAEC,YAAY,CAAEE,IAAK,CAAE,CAAC,IAAIA,IAAI,CAACE,EAAE,CAACC,QAAQ,CAAC,CAAC;AAEzE,CAAC","ignoreList":[]}
1
+ {"version":3,"names":["cleanForSlug","getItemTitle","getSlug","item","slug","id","toString"],"sources":["@wordpress/fields/src/fields/slug/utils.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { cleanForSlug } from '@wordpress/url';\n/**\n * Internal dependencies\n */\nimport type { BasePost } from '../../types';\nimport { getItemTitle } from '../../actions/utils';\n\nexport const getSlug = ( item: BasePost ): string => {\n\tif ( typeof item !== 'object' ) {\n\t\treturn '';\n\t}\n\n\treturn (\n\t\titem.slug || cleanForSlug( getItemTitle( item ) ) || item.id.toString()\n\t);\n};\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,YAAY,QAAQ,gBAAgB;AAC7C;AACA;AACA;;AAEA,SAASC,YAAY,QAAQ,qBAAqB;AAElD,OAAO,MAAMC,OAAO,GAAKC,IAAc,IAAc;EACpD,IAAK,OAAOA,IAAI,KAAK,QAAQ,EAAG;IAC/B,OAAO,EAAE;EACV;EAEA,OACCA,IAAI,CAACC,IAAI,IAAIJ,YAAY,CAAEC,YAAY,CAAEE,IAAK,CAAE,CAAC,IAAIA,IAAI,CAACE,EAAE,CAACC,QAAQ,CAAC,CAAC;AAEzE,CAAC","ignoreList":[]}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+
5
+ /**
6
+ * Internal dependencies
7
+ */
8
+ import { __ } from '@wordpress/i18n';
9
+ import { TemplateEdit } from './template-edit';
10
+ const templateField = {
11
+ id: 'template',
12
+ type: 'text',
13
+ label: __('Template'),
14
+ getValue: ({
15
+ item
16
+ }) => item.template,
17
+ Edit: TemplateEdit,
18
+ enableSorting: false
19
+ };
20
+ export default templateField;
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["__","TemplateEdit","templateField","id","type","label","getValue","item","template","Edit","enableSorting"],"sources":["@wordpress/fields/src/fields/template/index.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport type { Field } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport { __ } from '@wordpress/i18n';\nimport type { BasePost } from '../../types';\nimport { TemplateEdit } from './template-edit';\n\nconst templateField: Field< BasePost > = {\n\tid: 'template',\n\ttype: 'text',\n\tlabel: __( 'Template' ),\n\tgetValue: ( { item } ) => item.template,\n\tEdit: TemplateEdit,\n\tenableSorting: false,\n};\n\nexport default templateField;\n"],"mappings":"AAAA;AACA;AACA;;AAGA;AACA;AACA;AACA,SAASA,EAAE,QAAQ,iBAAiB;AAEpC,SAASC,YAAY,QAAQ,iBAAiB;AAE9C,MAAMC,aAAgC,GAAG;EACxCC,EAAE,EAAE,UAAU;EACdC,IAAI,EAAE,MAAM;EACZC,KAAK,EAAEL,EAAE,CAAE,UAAW,CAAC;EACvBM,QAAQ,EAAEA,CAAE;IAAEC;EAAK,CAAC,KAAMA,IAAI,CAACC,QAAQ;EACvCC,IAAI,EAAER,YAAY;EAClBS,aAAa,EAAE;AAChB,CAAC;AAED,eAAeR,aAAa","ignoreList":[]}