@wordpress/core-data 7.3.0 → 7.5.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 (153) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +54 -6
  3. package/build/actions.js +1 -1
  4. package/build/actions.js.map +1 -1
  5. package/build/entity-context.js +13 -0
  6. package/build/entity-context.js.map +1 -0
  7. package/build/entity-provider.js +4 -189
  8. package/build/entity-provider.js.map +1 -1
  9. package/build/footnotes/get-rich-text-values-cached.js +2 -2
  10. package/build/footnotes/get-rich-text-values-cached.js.map +1 -1
  11. package/build/hooks/index.js +22 -0
  12. package/build/hooks/index.js.map +1 -1
  13. package/build/hooks/use-entity-block-editor.js +140 -0
  14. package/build/hooks/use-entity-block-editor.js.map +1 -0
  15. package/build/hooks/use-entity-id.js +28 -0
  16. package/build/hooks/use-entity-id.js.map +1 -0
  17. package/build/hooks/use-entity-prop.js +65 -0
  18. package/build/hooks/use-entity-prop.js.map +1 -0
  19. package/build/hooks/use-entity-records.js +39 -0
  20. package/build/hooks/use-entity-records.js.map +1 -1
  21. package/build/hooks/use-resource-permissions.js +25 -8
  22. package/build/hooks/use-resource-permissions.js.map +1 -1
  23. package/build/index.js +14 -2
  24. package/build/index.js.map +1 -1
  25. package/build/lock-unlock.js +18 -0
  26. package/build/lock-unlock.js.map +1 -0
  27. package/build/private-apis.js +8 -9
  28. package/build/private-apis.js.map +1 -1
  29. package/build/private-selectors.js +35 -0
  30. package/build/private-selectors.js.map +1 -1
  31. package/build/resolvers.js +64 -60
  32. package/build/resolvers.js.map +1 -1
  33. package/build/selectors.js +16 -8
  34. package/build/selectors.js.map +1 -1
  35. package/build/utils/index.js +19 -0
  36. package/build/utils/index.js.map +1 -1
  37. package/build/utils/user-permissions.js +32 -0
  38. package/build/utils/user-permissions.js.map +1 -0
  39. package/build-module/actions.js +1 -1
  40. package/build-module/actions.js.map +1 -1
  41. package/build-module/entity-context.js +6 -0
  42. package/build-module/entity-context.js.map +1 -0
  43. package/build-module/entity-provider.js +3 -183
  44. package/build-module/entity-provider.js.map +1 -1
  45. package/build-module/footnotes/get-rich-text-values-cached.js +1 -1
  46. package/build-module/footnotes/get-rich-text-values-cached.js.map +1 -1
  47. package/build-module/hooks/index.js +3 -0
  48. package/build-module/hooks/index.js.map +1 -1
  49. package/build-module/hooks/use-entity-block-editor.js +132 -0
  50. package/build-module/hooks/use-entity-block-editor.js.map +1 -0
  51. package/build-module/hooks/use-entity-id.js +22 -0
  52. package/build-module/hooks/use-entity-id.js.map +1 -0
  53. package/build-module/hooks/use-entity-prop.js +58 -0
  54. package/build-module/hooks/use-entity-prop.js.map +1 -0
  55. package/build-module/hooks/use-entity-records.js +38 -0
  56. package/build-module/hooks/use-entity-records.js.map +1 -1
  57. package/build-module/hooks/use-resource-permissions.js +25 -8
  58. package/build-module/hooks/use-resource-permissions.js.map +1 -1
  59. package/build-module/index.js +2 -1
  60. package/build-module/index.js.map +1 -1
  61. package/build-module/lock-unlock.js +9 -0
  62. package/build-module/lock-unlock.js.map +1 -0
  63. package/build-module/private-apis.js +7 -6
  64. package/build-module/private-apis.js.map +1 -1
  65. package/build-module/private-selectors.js +33 -0
  66. package/build-module/private-selectors.js.map +1 -1
  67. package/build-module/resolvers.js +65 -61
  68. package/build-module/resolvers.js.map +1 -1
  69. package/build-module/selectors.js +17 -9
  70. package/build-module/selectors.js.map +1 -1
  71. package/build-module/utils/index.js +1 -0
  72. package/build-module/utils/index.js.map +1 -1
  73. package/build-module/utils/user-permissions.js +24 -0
  74. package/build-module/utils/user-permissions.js.map +1 -0
  75. package/build-types/actions.d.ts +3 -3
  76. package/build-types/actions.d.ts.map +1 -1
  77. package/build-types/batch/create-batch.d.ts.map +1 -1
  78. package/build-types/entities.d.ts.map +1 -1
  79. package/build-types/entity-context.d.ts +2 -0
  80. package/build-types/entity-context.d.ts.map +1 -0
  81. package/build-types/entity-provider.d.ts +0 -47
  82. package/build-types/entity-provider.d.ts.map +1 -1
  83. package/build-types/fetch/__experimental-fetch-url-data.d.ts.map +1 -1
  84. package/build-types/hooks/index.d.ts +3 -0
  85. package/build-types/hooks/index.d.ts.map +1 -1
  86. package/build-types/hooks/use-entity-block-editor.d.ts +22 -0
  87. package/build-types/hooks/use-entity-block-editor.d.ts.map +1 -0
  88. package/build-types/hooks/use-entity-id.d.ts +9 -0
  89. package/build-types/hooks/use-entity-id.d.ts.map +1 -0
  90. package/build-types/hooks/use-entity-prop.d.ts +19 -0
  91. package/build-types/hooks/use-entity-prop.d.ts.map +1 -0
  92. package/build-types/hooks/use-entity-records.d.ts +1 -0
  93. package/build-types/hooks/use-entity-records.d.ts.map +1 -1
  94. package/build-types/hooks/use-resource-permissions.d.ts +8 -70
  95. package/build-types/hooks/use-resource-permissions.d.ts.map +1 -1
  96. package/build-types/index.d.ts +36 -32
  97. package/build-types/index.d.ts.map +1 -1
  98. package/build-types/lock-unlock.d.ts +3 -0
  99. package/build-types/lock-unlock.d.ts.map +1 -0
  100. package/build-types/locks/reducer.d.ts +1 -1
  101. package/build-types/locks/reducer.d.ts.map +1 -1
  102. package/build-types/private-apis.d.ts +1 -2
  103. package/build-types/private-apis.d.ts.map +1 -1
  104. package/build-types/private-selectors.d.ts +15 -0
  105. package/build-types/private-selectors.d.ts.map +1 -1
  106. package/build-types/queried-data/actions.d.ts +1 -1
  107. package/build-types/queried-data/actions.d.ts.map +1 -1
  108. package/build-types/queried-data/get-query-parts.d.ts.map +1 -1
  109. package/build-types/queried-data/reducer.d.ts +1 -1
  110. package/build-types/queried-data/reducer.d.ts.map +1 -1
  111. package/build-types/queried-data/selectors.d.ts +0 -1
  112. package/build-types/queried-data/selectors.d.ts.map +1 -1
  113. package/build-types/reducer.d.ts +13 -13
  114. package/build-types/reducer.d.ts.map +1 -1
  115. package/build-types/resolvers.d.ts +3 -2
  116. package/build-types/resolvers.d.ts.map +1 -1
  117. package/build-types/selectors.d.ts +11 -6
  118. package/build-types/selectors.d.ts.map +1 -1
  119. package/build-types/utils/get-nested-value.d.ts.map +1 -1
  120. package/build-types/utils/get-normalized-comma-separable.d.ts.map +1 -1
  121. package/build-types/utils/if-matching-action.d.ts +1 -1
  122. package/build-types/utils/index.d.ts +1 -0
  123. package/build-types/utils/on-sub-key.d.ts +1 -1
  124. package/build-types/utils/replace-action.d.ts +1 -1
  125. package/build-types/utils/set-nested-value.d.ts.map +1 -1
  126. package/build-types/utils/user-permissions.d.ts +4 -0
  127. package/build-types/utils/user-permissions.d.ts.map +1 -0
  128. package/package.json +18 -17
  129. package/src/actions.js +1 -1
  130. package/src/entity-context.js +6 -0
  131. package/src/entity-provider.js +2 -209
  132. package/src/footnotes/get-rich-text-values-cached.js +1 -1
  133. package/src/hooks/index.ts +3 -0
  134. package/src/hooks/test/use-entity-record.js +5 -3
  135. package/src/hooks/test/use-resource-permissions.js +96 -5
  136. package/src/hooks/use-entity-block-editor.js +148 -0
  137. package/src/hooks/use-entity-id.js +21 -0
  138. package/src/hooks/use-entity-prop.js +60 -0
  139. package/src/hooks/use-entity-records.ts +50 -0
  140. package/src/hooks/use-resource-permissions.ts +46 -9
  141. package/src/index.js +2 -1
  142. package/src/lock-unlock.js +10 -0
  143. package/src/private-apis.js +7 -7
  144. package/src/private-selectors.ts +43 -0
  145. package/src/resolvers.js +85 -67
  146. package/src/selectors.ts +18 -9
  147. package/src/test/entity-provider.js +6 -2
  148. package/src/test/resolvers.js +217 -50
  149. package/src/test/selectors.js +18 -55
  150. package/src/utils/index.js +5 -0
  151. package/src/utils/user-permissions.js +39 -0
  152. package/tsconfig.json +2 -1
  153. package/tsconfig.tsbuildinfo +1 -1
@@ -1 +1 @@
1
- {"version":3,"names":["_element","require","_data","_blocks2","_name","_footnotes","_jsxRuntime","EMPTY_ARRAY","EntityContext","createContext","EntityProvider","kind","type","name","id","children","parent","useContext","childContext","useMemo","jsx","Provider","value","useEntityId","context","useEntityProp","prop","_id","providerId","fullValue","useSelect","select","getEntityRecord","getEditedEntityRecord","STORE_NAME","record","editedRecord","editEntityRecord","useDispatch","setValue","useCallback","newValue","parsedBlocksCache","WeakMap","useEntityBlockEditor","getEntityRecordEdits","content","editedBlocks","meta","blocks","__unstableCreateUndoLevel","undefined","edits","isUnedited","Object","keys","length","cackeKey","_blocks","get","parse","set","updateFootnotes","updateFootnotesFromMeta","onChange","newBlocks","options","noChange","selection","rest","blocksForSerialization","__unstableSerializeAndClean","isCached","onInput","footnotesChanges"],"sources":["@wordpress/core-data/src/entity-provider.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport {\n\tcreateContext,\n\tuseContext,\n\tuseCallback,\n\tuseMemo,\n} from '@wordpress/element';\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { parse, __unstableSerializeAndClean } from '@wordpress/blocks';\n\n/**\n * Internal dependencies\n */\nimport { STORE_NAME } from './name';\nimport { updateFootnotesFromMeta } from './footnotes';\n\nconst EMPTY_ARRAY = [];\n\nconst EntityContext = createContext( {} );\n\n/**\n * Context provider component for providing\n * an entity for a specific entity.\n *\n * @param {Object} props The component's props.\n * @param {string} props.kind The entity kind.\n * @param {string} props.type The entity name.\n * @param {number} props.id The entity ID.\n * @param {*} props.children The children to wrap.\n *\n * @return {Object} The provided children, wrapped with\n * the entity's context provider.\n */\nexport default function EntityProvider( { kind, type: name, id, children } ) {\n\tconst parent = useContext( EntityContext );\n\tconst childContext = useMemo(\n\t\t() => ( {\n\t\t\t...parent,\n\t\t\t[ kind ]: {\n\t\t\t\t...parent?.[ kind ],\n\t\t\t\t[ name ]: id,\n\t\t\t},\n\t\t} ),\n\t\t[ parent, kind, name, id ]\n\t);\n\treturn (\n\t\t<EntityContext.Provider value={ childContext }>\n\t\t\t{ children }\n\t\t</EntityContext.Provider>\n\t);\n}\n\n/**\n * Hook that returns the ID for the nearest\n * provided entity of the specified type.\n *\n * @param {string} kind The entity kind.\n * @param {string} name The entity name.\n */\nexport function useEntityId( kind, name ) {\n\tconst context = useContext( EntityContext );\n\treturn context?.[ kind ]?.[ name ];\n}\n\n/**\n * Hook that returns the value and a setter for the\n * specified property of the nearest provided\n * entity of the specified type.\n *\n * @param {string} kind The entity kind.\n * @param {string} name The entity name.\n * @param {string} prop The property name.\n * @param {string} [_id] An entity ID to use instead of the context-provided one.\n *\n * @return {[*, Function, *]} An array where the first item is the\n * property value, the second is the\n * setter and the third is the full value\n * \t\t\t\t\t\t\t object from REST API containing more\n * \t\t\t\t\t\t\t information like `raw`, `rendered` and\n * \t\t\t\t\t\t\t `protected` props.\n */\nexport function useEntityProp( kind, name, prop, _id ) {\n\tconst providerId = useEntityId( kind, name );\n\tconst id = _id ?? providerId;\n\n\tconst { value, fullValue } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getEntityRecord, getEditedEntityRecord } =\n\t\t\t\tselect( STORE_NAME );\n\t\t\tconst record = getEntityRecord( kind, name, id ); // Trigger resolver.\n\t\t\tconst editedRecord = getEditedEntityRecord( kind, name, id );\n\t\t\treturn record && editedRecord\n\t\t\t\t? {\n\t\t\t\t\t\tvalue: editedRecord[ prop ],\n\t\t\t\t\t\tfullValue: record[ prop ],\n\t\t\t\t }\n\t\t\t\t: {};\n\t\t},\n\t\t[ kind, name, id, prop ]\n\t);\n\tconst { editEntityRecord } = useDispatch( STORE_NAME );\n\tconst setValue = useCallback(\n\t\t( newValue ) => {\n\t\t\teditEntityRecord( kind, name, id, {\n\t\t\t\t[ prop ]: newValue,\n\t\t\t} );\n\t\t},\n\t\t[ editEntityRecord, kind, name, id, prop ]\n\t);\n\n\treturn [ value, setValue, fullValue ];\n}\n\nconst parsedBlocksCache = new WeakMap();\n\n/**\n * Hook that returns block content getters and setters for\n * the nearest provided entity of the specified type.\n *\n * The return value has the shape `[ blocks, onInput, onChange ]`.\n * `onInput` is for block changes that don't create undo levels\n * or dirty the post, non-persistent changes, and `onChange` is for\n * persistent changes. They map directly to the props of a\n * `BlockEditorProvider` and are intended to be used with it,\n * or similar components or hooks.\n *\n * @param {string} kind The entity kind.\n * @param {string} name The entity name.\n * @param {Object} options\n * @param {string} [options.id] An entity ID to use instead of the context-provided one.\n *\n * @return {[unknown[], Function, Function]} The block array and setters.\n */\nexport function useEntityBlockEditor( kind, name, { id: _id } = {} ) {\n\tconst providerId = useEntityId( kind, name );\n\tconst id = _id ?? providerId;\n\tconst { getEntityRecord, getEntityRecordEdits } = useSelect( STORE_NAME );\n\tconst { content, editedBlocks, meta } = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! id ) {\n\t\t\t\treturn {};\n\t\t\t}\n\t\t\tconst { getEditedEntityRecord } = select( STORE_NAME );\n\t\t\tconst editedRecord = getEditedEntityRecord( kind, name, id );\n\t\t\treturn {\n\t\t\t\teditedBlocks: editedRecord.blocks,\n\t\t\t\tcontent: editedRecord.content,\n\t\t\t\tmeta: editedRecord.meta,\n\t\t\t};\n\t\t},\n\t\t[ kind, name, id ]\n\t);\n\tconst { __unstableCreateUndoLevel, editEntityRecord } =\n\t\tuseDispatch( STORE_NAME );\n\n\tconst blocks = useMemo( () => {\n\t\tif ( ! id ) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tif ( editedBlocks ) {\n\t\t\treturn editedBlocks;\n\t\t}\n\n\t\tif ( ! content || typeof content !== 'string' ) {\n\t\t\treturn EMPTY_ARRAY;\n\t\t}\n\n\t\t// If there's an edit, cache the parsed blocks by the edit.\n\t\t// If not, cache by the original enity record.\n\t\tconst edits = getEntityRecordEdits( kind, name, id );\n\t\tconst isUnedited = ! edits || ! Object.keys( edits ).length;\n\t\tconst cackeKey = isUnedited ? getEntityRecord( kind, name, id ) : edits;\n\t\tlet _blocks = parsedBlocksCache.get( cackeKey );\n\n\t\tif ( ! _blocks ) {\n\t\t\t_blocks = parse( content );\n\t\t\tparsedBlocksCache.set( cackeKey, _blocks );\n\t\t}\n\n\t\treturn _blocks;\n\t}, [\n\t\tkind,\n\t\tname,\n\t\tid,\n\t\teditedBlocks,\n\t\tcontent,\n\t\tgetEntityRecord,\n\t\tgetEntityRecordEdits,\n\t] );\n\n\tconst updateFootnotes = useCallback(\n\t\t( _blocks ) => updateFootnotesFromMeta( _blocks, meta ),\n\t\t[ meta ]\n\t);\n\n\tconst onChange = useCallback(\n\t\t( newBlocks, options ) => {\n\t\t\tconst noChange = blocks === newBlocks;\n\t\t\tif ( noChange ) {\n\t\t\t\treturn __unstableCreateUndoLevel( kind, name, id );\n\t\t\t}\n\t\t\tconst { selection, ...rest } = options;\n\n\t\t\t// We create a new function here on every persistent edit\n\t\t\t// to make sure the edit makes the post dirty and creates\n\t\t\t// a new undo level.\n\t\t\tconst edits = {\n\t\t\t\tselection,\n\t\t\t\tcontent: ( { blocks: blocksForSerialization = [] } ) =>\n\t\t\t\t\t__unstableSerializeAndClean( blocksForSerialization ),\n\t\t\t\t...updateFootnotes( newBlocks ),\n\t\t\t};\n\n\t\t\teditEntityRecord( kind, name, id, edits, {\n\t\t\t\tisCached: false,\n\t\t\t\t...rest,\n\t\t\t} );\n\t\t},\n\t\t[\n\t\t\tkind,\n\t\t\tname,\n\t\t\tid,\n\t\t\tblocks,\n\t\t\tupdateFootnotes,\n\t\t\t__unstableCreateUndoLevel,\n\t\t\teditEntityRecord,\n\t\t]\n\t);\n\n\tconst onInput = useCallback(\n\t\t( newBlocks, options ) => {\n\t\t\tconst { selection, ...rest } = options;\n\t\t\tconst footnotesChanges = updateFootnotes( newBlocks );\n\t\t\tconst edits = { selection, ...footnotesChanges };\n\n\t\t\teditEntityRecord( kind, name, id, edits, {\n\t\t\t\tisCached: true,\n\t\t\t\t...rest,\n\t\t\t} );\n\t\t},\n\t\t[ kind, name, id, updateFootnotes, editEntityRecord ]\n\t);\n\n\treturn [ blocks, onInput, onChange ];\n}\n"],"mappings":";;;;;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AAMA,IAAAC,KAAA,GAAAD,OAAA;AACA,IAAAE,QAAA,GAAAF,OAAA;AAKA,IAAAG,KAAA,GAAAH,OAAA;AACA,IAAAI,UAAA,GAAAJ,OAAA;AAAsD,IAAAK,WA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AAgB,CAAE1B,IAAI,EAAEE,IAAI,EAAEC,EAAE,EAAEsC,KAAK,EAAE;MACxCoB,QAAQ,EAAE,KAAK;MACf,GAAGH;IACJ,CAAE,CAAC;EACJ,CAAC,EACD,CACC1D,IAAI,EACJE,IAAI,EACJC,EAAE,EACFmC,MAAM,EACNa,eAAe,EACfZ,yBAAyB,EACzBb,gBAAgB,CAElB,CAAC;EAED,MAAMoC,OAAO,GAAG,IAAAjC,oBAAW,EAC1B,CAAEyB,SAAS,EAAEC,OAAO,KAAM;IACzB,MAAM;MAAEE,SAAS;MAAE,GAAGC;IAAK,CAAC,GAAGH,OAAO;IACtC,MAAMQ,gBAAgB,GAAGZ,eAAe,CAAEG,SAAU,CAAC;IACrD,MAAMb,KAAK,GAAG;MAAEgB,SAAS;MAAE,GAAGM;IAAiB,CAAC;IAEhDrC,gBAAgB,CAAE1B,IAAI,EAAEE,IAAI,EAAEC,EAAE,EAAEsC,KAAK,EAAE;MACxCoB,QAAQ,EAAE,IAAI;MACd,GAAGH;IACJ,CAAE,CAAC;EACJ,CAAC,EACD,CAAE1D,IAAI,EAAEE,IAAI,EAAEC,EAAE,EAAEgD,eAAe,EAAEzB,gBAAgB,CACpD,CAAC;EAED,OAAO,CAAEY,MAAM,EAAEwB,OAAO,EAAET,QAAQ,CAAE;AACrC","ignoreList":[]}
1
+ {"version":3,"names":["_element","require","_entityContext","_jsxRuntime","EntityProvider","kind","type","name","id","children","parent","useContext","EntityContext","childContext","useMemo","jsx","Provider","value"],"sources":["@wordpress/core-data/src/entity-provider.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useContext, useMemo } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { EntityContext } from './entity-context';\n\n/**\n * Context provider component for providing\n * an entity for a specific entity.\n *\n * @param {Object} props The component's props.\n * @param {string} props.kind The entity kind.\n * @param {string} props.type The entity name.\n * @param {number} props.id The entity ID.\n * @param {*} props.children The children to wrap.\n *\n * @return {Object} The provided children, wrapped with\n * the entity's context provider.\n */\nexport default function EntityProvider( { kind, type: name, id, children } ) {\n\tconst parent = useContext( EntityContext );\n\tconst childContext = useMemo(\n\t\t() => ( {\n\t\t\t...parent,\n\t\t\t[ kind ]: {\n\t\t\t\t...parent?.[ kind ],\n\t\t\t\t[ name ]: id,\n\t\t\t},\n\t\t} ),\n\t\t[ parent, kind, name, id ]\n\t);\n\treturn (\n\t\t<EntityContext.Provider value={ childContext }>\n\t\t\t{ children }\n\t\t</EntityContext.Provider>\n\t);\n}\n"],"mappings":";;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AAKA,IAAAC,cAAA,GAAAD,OAAA;AAAiD,IAAAE,WAAA,GAAAF,OAAA;AARjD;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,GACe,SAASG,cAAcA,CAAE;EAAEC,IAAI;EAAEC,IAAI,EAAEC,IAAI;EAAEC,EAAE;EAAEC;AAAS,CAAC,EAAG;EAC5E,MAAMC,MAAM,GAAG,IAAAC,mBAAU,EAAEC,4BAAc,CAAC;EAC1C,MAAMC,YAAY,GAAG,IAAAC,gBAAO,EAC3B,OAAQ;IACP,GAAGJ,MAAM;IACT,CAAEL,IAAI,GAAI;MACT,GAAGK,MAAM,GAAIL,IAAI,CAAE;MACnB,CAAEE,IAAI,GAAIC;IACX;EACD,CAAC,CAAE,EACH,CAAEE,MAAM,EAAEL,IAAI,EAAEE,IAAI,EAAEC,EAAE,CACzB,CAAC;EACD,oBACC,IAAAL,WAAA,CAAAY,GAAA,EAACb,cAAA,CAAAU,aAAa,CAACI,QAAQ;IAACC,KAAK,EAAGJ,YAAc;IAAAJ,QAAA,EAC3CA;EAAQ,CACa,CAAC;AAE3B","ignoreList":[]}
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = getRichTextValuesCached;
7
7
  var _blockEditor = require("@wordpress/block-editor");
8
- var _privateApis = require("../private-apis");
8
+ var _lockUnlock = require("../lock-unlock");
9
9
  /**
10
10
  * WordPress dependencies
11
11
  */
@@ -28,7 +28,7 @@ let unlockedApis;
28
28
  const cache = new WeakMap();
29
29
  function getRichTextValuesCached(block) {
30
30
  if (!unlockedApis) {
31
- unlockedApis = (0, _privateApis.unlock)(_blockEditor.privateApis);
31
+ unlockedApis = (0, _lockUnlock.unlock)(_blockEditor.privateApis);
32
32
  }
33
33
  if (!cache.has(block)) {
34
34
  const values = unlockedApis.getRichTextValues([block]);
@@ -1 +1 @@
1
- {"version":3,"names":["_blockEditor","require","_privateApis","unlockedApis","cache","WeakMap","getRichTextValuesCached","block","unlock","blockEditorPrivateApis","has","values","getRichTextValues","set","get"],"sources":["@wordpress/core-data/src/footnotes/get-rich-text-values-cached.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';\n\n/**\n * Internal dependencies\n */\nimport { unlock } from '../private-apis';\n\n// TODO: The following line should have been:\n//\n// const unlockedApis = unlock( blockEditorPrivateApis );\n//\n// But there are hidden circular dependencies in RNMobile code, specifically in\n// certain native components in the `components` package that depend on\n// `block-editor`. What follows is a workaround that defers the `unlock` call\n// to prevent native code from failing.\n//\n// Fix once https://github.com/WordPress/gutenberg/issues/52692 is closed.\nlet unlockedApis;\n\nconst cache = new WeakMap();\n\nexport default function getRichTextValuesCached( block ) {\n\tif ( ! unlockedApis ) {\n\t\tunlockedApis = unlock( blockEditorPrivateApis );\n\t}\n\n\tif ( ! cache.has( block ) ) {\n\t\tconst values = unlockedApis.getRichTextValues( [ block ] );\n\t\tcache.set( block, values );\n\t}\n\treturn cache.get( block );\n}\n"],"mappings":";;;;;;AAGA,IAAAA,YAAA,GAAAC,OAAA;AAKA,IAAAC,YAAA,GAAAD,OAAA;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,IAAIE,YAAY;AAEhB,MAAMC,KAAK,GAAG,IAAIC,OAAO,CAAC,CAAC;AAEZ,SAASC,uBAAuBA,CAAEC,KAAK,EAAG;EACxD,IAAK,CAAEJ,YAAY,EAAG;IACrBA,YAAY,GAAG,IAAAK,mBAAM,EAAEC,wBAAuB,CAAC;EAChD;EAEA,IAAK,CAAEL,KAAK,CAACM,GAAG,CAAEH,KAAM,CAAC,EAAG;IAC3B,MAAMI,MAAM,GAAGR,YAAY,CAACS,iBAAiB,CAAE,CAAEL,KAAK,CAAG,CAAC;IAC1DH,KAAK,CAACS,GAAG,CAAEN,KAAK,EAAEI,MAAO,CAAC;EAC3B;EACA,OAAOP,KAAK,CAACU,GAAG,CAAEP,KAAM,CAAC;AAC1B","ignoreList":[]}
1
+ {"version":3,"names":["_blockEditor","require","_lockUnlock","unlockedApis","cache","WeakMap","getRichTextValuesCached","block","unlock","blockEditorPrivateApis","has","values","getRichTextValues","set","get"],"sources":["@wordpress/core-data/src/footnotes/get-rich-text-values-cached.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';\n\n/**\n * Internal dependencies\n */\nimport { unlock } from '../lock-unlock';\n\n// TODO: The following line should have been:\n//\n// const unlockedApis = unlock( blockEditorPrivateApis );\n//\n// But there are hidden circular dependencies in RNMobile code, specifically in\n// certain native components in the `components` package that depend on\n// `block-editor`. What follows is a workaround that defers the `unlock` call\n// to prevent native code from failing.\n//\n// Fix once https://github.com/WordPress/gutenberg/issues/52692 is closed.\nlet unlockedApis;\n\nconst cache = new WeakMap();\n\nexport default function getRichTextValuesCached( block ) {\n\tif ( ! unlockedApis ) {\n\t\tunlockedApis = unlock( blockEditorPrivateApis );\n\t}\n\n\tif ( ! cache.has( block ) ) {\n\t\tconst values = unlockedApis.getRichTextValues( [ block ] );\n\t\tcache.set( block, values );\n\t}\n\treturn cache.get( block );\n}\n"],"mappings":";;;;;;AAGA,IAAAA,YAAA,GAAAC,OAAA;AAKA,IAAAC,WAAA,GAAAD,OAAA;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,IAAIE,YAAY;AAEhB,MAAMC,KAAK,GAAG,IAAIC,OAAO,CAAC,CAAC;AAEZ,SAASC,uBAAuBA,CAAEC,KAAK,EAAG;EACxD,IAAK,CAAEJ,YAAY,EAAG;IACrBA,YAAY,GAAG,IAAAK,kBAAM,EAAEC,wBAAuB,CAAC;EAChD;EAEA,IAAK,CAAEL,KAAK,CAACM,GAAG,CAAEH,KAAM,CAAC,EAAG;IAC3B,MAAMI,MAAM,GAAGR,YAAY,CAACS,iBAAiB,CAAE,CAAEL,KAAK,CAAG,CAAC;IAC1DH,KAAK,CAACS,GAAG,CAAEN,KAAK,EAAEI,MAAO,CAAC;EAC3B;EACA,OAAOP,KAAK,CAACU,GAAG,CAAEP,KAAM,CAAC;AAC1B","ignoreList":[]}
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
3
4
  Object.defineProperty(exports, "__esModule", {
4
5
  value: true
5
6
  });
@@ -21,6 +22,24 @@ Object.defineProperty(exports, "__experimentalUseResourcePermissions", {
21
22
  return _useResourcePermissions.__experimentalUseResourcePermissions;
22
23
  }
23
24
  });
25
+ Object.defineProperty(exports, "useEntityBlockEditor", {
26
+ enumerable: true,
27
+ get: function () {
28
+ return _useEntityBlockEditor.default;
29
+ }
30
+ });
31
+ Object.defineProperty(exports, "useEntityId", {
32
+ enumerable: true,
33
+ get: function () {
34
+ return _useEntityId.default;
35
+ }
36
+ });
37
+ Object.defineProperty(exports, "useEntityProp", {
38
+ enumerable: true,
39
+ get: function () {
40
+ return _useEntityProp.default;
41
+ }
42
+ });
24
43
  Object.defineProperty(exports, "useEntityRecord", {
25
44
  enumerable: true,
26
45
  get: function () {
@@ -42,6 +61,9 @@ Object.defineProperty(exports, "useResourcePermissions", {
42
61
  var _useEntityRecord = _interopRequireWildcard(require("./use-entity-record"));
43
62
  var _useEntityRecords = _interopRequireWildcard(require("./use-entity-records"));
44
63
  var _useResourcePermissions = _interopRequireWildcard(require("./use-resource-permissions"));
64
+ var _useEntityBlockEditor = _interopRequireDefault(require("./use-entity-block-editor"));
65
+ var _useEntityId = _interopRequireDefault(require("./use-entity-id"));
66
+ var _useEntityProp = _interopRequireDefault(require("./use-entity-prop"));
45
67
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
46
68
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
47
69
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_useEntityRecord","_interopRequireWildcard","require","_useEntityRecords","_useResourcePermissions","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","hasOwnProperty","call","i","set"],"sources":["@wordpress/core-data/src/hooks/index.ts"],"sourcesContent":["export {\n\tdefault as useEntityRecord,\n\t__experimentalUseEntityRecord,\n} from './use-entity-record';\nexport {\n\tdefault as useEntityRecords,\n\t__experimentalUseEntityRecords,\n} from './use-entity-records';\nexport {\n\tdefault as useResourcePermissions,\n\t__experimentalUseResourcePermissions,\n} from './use-resource-permissions';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,gBAAA,GA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Record","_interopRequireWildcard","require","_useEntityRecords","_useResourcePermissions","_useEntityBlockEditor","_interopRequireDefault","_useEntityId","_useEntityProp","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","hasOwnProperty","call","i","set"],"sources":["@wordpress/core-data/src/hooks/index.ts"],"sourcesContent":["export {\n\tdefault as useEntityRecord,\n\t__experimentalUseEntityRecord,\n} from './use-entity-record';\nexport {\n\tdefault as useEntityRecords,\n\t__experimentalUseEntityRecords,\n} from './use-entity-records';\nexport {\n\tdefault as useResourcePermissions,\n\t__experimentalUseResourcePermissions,\n} from './use-resource-permissions';\nexport { default as useEntityBlockEditor } from './use-entity-block-editor';\nexport { default as useEntityId } from './use-entity-id';\nexport { default as useEntityProp } from './use-entity-prop';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,gBAAA,GAAAC,uBAAA,CAAAC,OAAA;AAIA,IAAAC,iBAAA,GAAAF,uBAAA,CAAAC,OAAA;AAIA,IAAAE,uBAAA,GAAAH,uBAAA,CAAAC,OAAA;AAIA,IAAAG,qBAAA,GAAAC,sBAAA,CAAAJ,OAAA;AACA,IAAAK,YAAA,GAAAD,sBAAA,CAAAJ,OAAA;AACA,IAAAM,cAAA,GAAAF,sBAAA,CAAAJ,OAAA;AAA6D,SAAAO,yBAAAC,CAAA,6BAAAC,OAAA,mBAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,CAAA,WAAAA,CAAA,GAAAG,CAAA,GAAAD,CAAA,KAAAF,CAAA;AAAA,SAAAT,wBAAAS,CAAA,EAAAE,CAAA,SAAAA,CAAA,IAAAF,CAAA,IAAAA,CAAA,CAAAI,UAAA,SAAAJ,CAAA,eAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA,WAAAK,OAAA,EAAAL,CAAA,QAAAG,CAAA,GAAAJ,wBAAA,CAAAG,CAAA,OAAAC,CAAA,IAAAA,CAAA,CAAAG,GAAA,CAAAN,CAAA,UAAAG,CAAA,CAAAI,GAAA,CAAAP,CAAA,OAAAQ,CAAA,KAAAC,SAAA,UAAAC,CAAA,GAAAC,MAAA,CAAAC,cAAA,IAAAD,MAAA,CAAAE,wBAAA,WAAAC,CAAA,IAAAd,CAAA,oBAAAc,CAAA,OAAAC,cAAA,CAAAC,IAAA,CAAAhB,CAAA,EAAAc,CAAA,SAAAG,CAAA,GAAAP,CAAA,GAAAC,MAAA,CAAAE,wBAAA,CAAAb,CAAA,EAAAc,CAAA,UAAAG,CAAA,KAAAA,CAAA,CAAAV,GAAA,IAAAU,CAAA,CAAAC,GAAA,IAAAP,MAAA,CAAAC,cAAA,CAAAJ,CAAA,EAAAM,CAAA,EAAAG,CAAA,IAAAT,CAAA,CAAAM,CAAA,IAAAd,CAAA,CAAAc,CAAA,YAAAN,CAAA,CAAAH,OAAA,GAAAL,CAAA,EAAAG,CAAA,IAAAA,CAAA,CAAAe,GAAA,CAAAlB,CAAA,EAAAQ,CAAA,GAAAA,CAAA","ignoreList":[]}
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = useEntityBlockEditor;
8
+ var _element = require("@wordpress/element");
9
+ var _data = require("@wordpress/data");
10
+ var _blocks2 = require("@wordpress/blocks");
11
+ var _name = require("../name");
12
+ var _useEntityId = _interopRequireDefault(require("./use-entity-id"));
13
+ var _footnotes = require("../footnotes");
14
+ /**
15
+ * WordPress dependencies
16
+ */
17
+
18
+ /**
19
+ * Internal dependencies
20
+ */
21
+
22
+ const EMPTY_ARRAY = [];
23
+ const parsedBlocksCache = new WeakMap();
24
+
25
+ /**
26
+ * Hook that returns block content getters and setters for
27
+ * the nearest provided entity of the specified type.
28
+ *
29
+ * The return value has the shape `[ blocks, onInput, onChange ]`.
30
+ * `onInput` is for block changes that don't create undo levels
31
+ * or dirty the post, non-persistent changes, and `onChange` is for
32
+ * persistent changes. They map directly to the props of a
33
+ * `BlockEditorProvider` and are intended to be used with it,
34
+ * or similar components or hooks.
35
+ *
36
+ * @param {string} kind The entity kind.
37
+ * @param {string} name The entity name.
38
+ * @param {Object} options
39
+ * @param {string} [options.id] An entity ID to use instead of the context-provided one.
40
+ *
41
+ * @return {[unknown[], Function, Function]} The block array and setters.
42
+ */
43
+ function useEntityBlockEditor(kind, name, {
44
+ id: _id
45
+ } = {}) {
46
+ const providerId = (0, _useEntityId.default)(kind, name);
47
+ const id = _id !== null && _id !== void 0 ? _id : providerId;
48
+ const {
49
+ getEntityRecord,
50
+ getEntityRecordEdits
51
+ } = (0, _data.useSelect)(_name.STORE_NAME);
52
+ const {
53
+ content,
54
+ editedBlocks,
55
+ meta
56
+ } = (0, _data.useSelect)(select => {
57
+ if (!id) {
58
+ return {};
59
+ }
60
+ const {
61
+ getEditedEntityRecord
62
+ } = select(_name.STORE_NAME);
63
+ const editedRecord = getEditedEntityRecord(kind, name, id);
64
+ return {
65
+ editedBlocks: editedRecord.blocks,
66
+ content: editedRecord.content,
67
+ meta: editedRecord.meta
68
+ };
69
+ }, [kind, name, id]);
70
+ const {
71
+ __unstableCreateUndoLevel,
72
+ editEntityRecord
73
+ } = (0, _data.useDispatch)(_name.STORE_NAME);
74
+ const blocks = (0, _element.useMemo)(() => {
75
+ if (!id) {
76
+ return undefined;
77
+ }
78
+ if (editedBlocks) {
79
+ return editedBlocks;
80
+ }
81
+ if (!content || typeof content !== 'string') {
82
+ return EMPTY_ARRAY;
83
+ }
84
+
85
+ // If there's an edit, cache the parsed blocks by the edit.
86
+ // If not, cache by the original enity record.
87
+ const edits = getEntityRecordEdits(kind, name, id);
88
+ const isUnedited = !edits || !Object.keys(edits).length;
89
+ const cackeKey = isUnedited ? getEntityRecord(kind, name, id) : edits;
90
+ let _blocks = parsedBlocksCache.get(cackeKey);
91
+ if (!_blocks) {
92
+ _blocks = (0, _blocks2.parse)(content);
93
+ parsedBlocksCache.set(cackeKey, _blocks);
94
+ }
95
+ return _blocks;
96
+ }, [kind, name, id, editedBlocks, content, getEntityRecord, getEntityRecordEdits]);
97
+ const updateFootnotes = (0, _element.useCallback)(_blocks => (0, _footnotes.updateFootnotesFromMeta)(_blocks, meta), [meta]);
98
+ const onChange = (0, _element.useCallback)((newBlocks, options) => {
99
+ const noChange = blocks === newBlocks;
100
+ if (noChange) {
101
+ return __unstableCreateUndoLevel(kind, name, id);
102
+ }
103
+ const {
104
+ selection,
105
+ ...rest
106
+ } = options;
107
+
108
+ // We create a new function here on every persistent edit
109
+ // to make sure the edit makes the post dirty and creates
110
+ // a new undo level.
111
+ const edits = {
112
+ selection,
113
+ content: ({
114
+ blocks: blocksForSerialization = []
115
+ }) => (0, _blocks2.__unstableSerializeAndClean)(blocksForSerialization),
116
+ ...updateFootnotes(newBlocks)
117
+ };
118
+ editEntityRecord(kind, name, id, edits, {
119
+ isCached: false,
120
+ ...rest
121
+ });
122
+ }, [kind, name, id, blocks, updateFootnotes, __unstableCreateUndoLevel, editEntityRecord]);
123
+ const onInput = (0, _element.useCallback)((newBlocks, options) => {
124
+ const {
125
+ selection,
126
+ ...rest
127
+ } = options;
128
+ const footnotesChanges = updateFootnotes(newBlocks);
129
+ const edits = {
130
+ selection,
131
+ ...footnotesChanges
132
+ };
133
+ editEntityRecord(kind, name, id, edits, {
134
+ isCached: true,
135
+ ...rest
136
+ });
137
+ }, [kind, name, id, updateFootnotes, editEntityRecord]);
138
+ return [blocks, onInput, onChange];
139
+ }
140
+ //# sourceMappingURL=use-entity-block-editor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_element","require","_data","_blocks2","_name","_useEntityId","_interopRequireDefault","_footnotes","EMPTY_ARRAY","parsedBlocksCache","WeakMap","useEntityBlockEditor","kind","name","id","_id","providerId","useEntityId","getEntityRecord","getEntityRecordEdits","useSelect","STORE_NAME","content","editedBlocks","meta","select","getEditedEntityRecord","editedRecord","blocks","__unstableCreateUndoLevel","editEntityRecord","useDispatch","useMemo","undefined","edits","isUnedited","Object","keys","length","cackeKey","_blocks","get","parse","set","updateFootnotes","useCallback","updateFootnotesFromMeta","onChange","newBlocks","options","noChange","selection","rest","blocksForSerialization","__unstableSerializeAndClean","isCached","onInput","footnotesChanges"],"sources":["@wordpress/core-data/src/hooks/use-entity-block-editor.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useCallback, useMemo } from '@wordpress/element';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport { parse, __unstableSerializeAndClean } from '@wordpress/blocks';\n\n/**\n * Internal dependencies\n */\nimport { STORE_NAME } from '../name';\nimport useEntityId from './use-entity-id';\nimport { updateFootnotesFromMeta } from '../footnotes';\n\nconst EMPTY_ARRAY = [];\nconst parsedBlocksCache = new WeakMap();\n\n/**\n * Hook that returns block content getters and setters for\n * the nearest provided entity of the specified type.\n *\n * The return value has the shape `[ blocks, onInput, onChange ]`.\n * `onInput` is for block changes that don't create undo levels\n * or dirty the post, non-persistent changes, and `onChange` is for\n * persistent changes. They map directly to the props of a\n * `BlockEditorProvider` and are intended to be used with it,\n * or similar components or hooks.\n *\n * @param {string} kind The entity kind.\n * @param {string} name The entity name.\n * @param {Object} options\n * @param {string} [options.id] An entity ID to use instead of the context-provided one.\n *\n * @return {[unknown[], Function, Function]} The block array and setters.\n */\nexport default function useEntityBlockEditor( kind, name, { id: _id } = {} ) {\n\tconst providerId = useEntityId( kind, name );\n\tconst id = _id ?? providerId;\n\tconst { getEntityRecord, getEntityRecordEdits } = useSelect( STORE_NAME );\n\tconst { content, editedBlocks, meta } = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! id ) {\n\t\t\t\treturn {};\n\t\t\t}\n\t\t\tconst { getEditedEntityRecord } = select( STORE_NAME );\n\t\t\tconst editedRecord = getEditedEntityRecord( kind, name, id );\n\t\t\treturn {\n\t\t\t\teditedBlocks: editedRecord.blocks,\n\t\t\t\tcontent: editedRecord.content,\n\t\t\t\tmeta: editedRecord.meta,\n\t\t\t};\n\t\t},\n\t\t[ kind, name, id ]\n\t);\n\tconst { __unstableCreateUndoLevel, editEntityRecord } =\n\t\tuseDispatch( STORE_NAME );\n\n\tconst blocks = useMemo( () => {\n\t\tif ( ! id ) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tif ( editedBlocks ) {\n\t\t\treturn editedBlocks;\n\t\t}\n\n\t\tif ( ! content || typeof content !== 'string' ) {\n\t\t\treturn EMPTY_ARRAY;\n\t\t}\n\n\t\t// If there's an edit, cache the parsed blocks by the edit.\n\t\t// If not, cache by the original enity record.\n\t\tconst edits = getEntityRecordEdits( kind, name, id );\n\t\tconst isUnedited = ! edits || ! Object.keys( edits ).length;\n\t\tconst cackeKey = isUnedited ? getEntityRecord( kind, name, id ) : edits;\n\t\tlet _blocks = parsedBlocksCache.get( cackeKey );\n\n\t\tif ( ! _blocks ) {\n\t\t\t_blocks = parse( content );\n\t\t\tparsedBlocksCache.set( cackeKey, _blocks );\n\t\t}\n\n\t\treturn _blocks;\n\t}, [\n\t\tkind,\n\t\tname,\n\t\tid,\n\t\teditedBlocks,\n\t\tcontent,\n\t\tgetEntityRecord,\n\t\tgetEntityRecordEdits,\n\t] );\n\n\tconst updateFootnotes = useCallback(\n\t\t( _blocks ) => updateFootnotesFromMeta( _blocks, meta ),\n\t\t[ meta ]\n\t);\n\n\tconst onChange = useCallback(\n\t\t( newBlocks, options ) => {\n\t\t\tconst noChange = blocks === newBlocks;\n\t\t\tif ( noChange ) {\n\t\t\t\treturn __unstableCreateUndoLevel( kind, name, id );\n\t\t\t}\n\t\t\tconst { selection, ...rest } = options;\n\n\t\t\t// We create a new function here on every persistent edit\n\t\t\t// to make sure the edit makes the post dirty and creates\n\t\t\t// a new undo level.\n\t\t\tconst edits = {\n\t\t\t\tselection,\n\t\t\t\tcontent: ( { blocks: blocksForSerialization = [] } ) =>\n\t\t\t\t\t__unstableSerializeAndClean( blocksForSerialization ),\n\t\t\t\t...updateFootnotes( newBlocks ),\n\t\t\t};\n\n\t\t\teditEntityRecord( kind, name, id, edits, {\n\t\t\t\tisCached: false,\n\t\t\t\t...rest,\n\t\t\t} );\n\t\t},\n\t\t[\n\t\t\tkind,\n\t\t\tname,\n\t\t\tid,\n\t\t\tblocks,\n\t\t\tupdateFootnotes,\n\t\t\t__unstableCreateUndoLevel,\n\t\t\teditEntityRecord,\n\t\t]\n\t);\n\n\tconst onInput = useCallback(\n\t\t( newBlocks, options ) => {\n\t\t\tconst { selection, ...rest } = options;\n\t\t\tconst footnotesChanges = updateFootnotes( newBlocks );\n\t\t\tconst edits = { selection, ...footnotesChanges };\n\n\t\t\teditEntityRecord( kind, name, id, edits, {\n\t\t\t\tisCached: true,\n\t\t\t\t...rest,\n\t\t\t} );\n\t\t},\n\t\t[ kind, name, id, updateFootnotes, editEntityRecord ]\n\t);\n\n\treturn [ blocks, onInput, onChange ];\n}\n"],"mappings":";;;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AACA,IAAAC,KAAA,GAAAD,OAAA;AACA,IAAAE,QAAA,GAAAF,OAAA;AAKA,IAAAG,KAAA,GAAAH,OAAA;AACA,IAAAI,YAAA,GAAAC,sBAAA,CAAAL,OAAA;AACA,IAAAM,UAAA,GAAAN,OAAA;AAZA;AACA;AACA;;AAKA;AACA;AACA;;AAKA,MAAMO,WAAW,GAAG,EAAE;AACtB,MAAMC,iBAAiB,GAAG,IAAIC,OAAO,CAAC,CAAC;;AAEvC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASC,oBAAoBA,CAAEC,IAAI,EAAEC,IAAI,EAAE;EAAEC,EAAE,EAAEC;AAAI,CAAC,GAAG,CAAC,CAAC,EAAG;EAC5E,MAAMC,UAAU,GAAG,IAAAC,oBAAW,EAAEL,IAA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H;IACJ,CAAE,CAAC;EACJ,CAAC,EACD,CACCxC,IAAI,EACJC,IAAI,EACJC,EAAE,EACFc,MAAM,EACNgB,eAAe,EACff,yBAAyB,EACzBC,gBAAgB,CAElB,CAAC;EAED,MAAM0B,OAAO,GAAG,IAAAX,oBAAW,EAC1B,CAAEG,SAAS,EAAEC,OAAO,KAAM;IACzB,MAAM;MAAEE,SAAS;MAAE,GAAGC;IAAK,CAAC,GAAGH,OAAO;IACtC,MAAMQ,gBAAgB,GAAGb,eAAe,CAAEI,SAAU,CAAC;IACrD,MAAMd,KAAK,GAAG;MAAEiB,SAAS;MAAE,GAAGM;IAAiB,CAAC;IAEhD3B,gBAAgB,CAAElB,IAAI,EAAEC,IAAI,EAAEC,EAAE,EAAEoB,KAAK,EAAE;MACxCqB,QAAQ,EAAE,IAAI;MACd,GAAGH;IACJ,CAAE,CAAC;EACJ,CAAC,EACD,CAAExC,IAAI,EAAEC,IAAI,EAAEC,EAAE,EAAE8B,eAAe,EAAEd,gBAAgB,CACpD,CAAC;EAED,OAAO,CAAEF,MAAM,EAAE4B,OAAO,EAAET,QAAQ,CAAE;AACrC","ignoreList":[]}
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = useEntityId;
7
+ var _element = require("@wordpress/element");
8
+ var _entityContext = require("../entity-context");
9
+ /**
10
+ * WordPress dependencies
11
+ */
12
+
13
+ /**
14
+ * Internal dependencies
15
+ */
16
+
17
+ /**
18
+ * Hook that returns the ID for the nearest
19
+ * provided entity of the specified type.
20
+ *
21
+ * @param {string} kind The entity kind.
22
+ * @param {string} name The entity name.
23
+ */
24
+ function useEntityId(kind, name) {
25
+ const context = (0, _element.useContext)(_entityContext.EntityContext);
26
+ return context?.[kind]?.[name];
27
+ }
28
+ //# sourceMappingURL=use-entity-id.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_element","require","_entityContext","useEntityId","kind","name","context","useContext","EntityContext"],"sources":["@wordpress/core-data/src/hooks/use-entity-id.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useContext } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { EntityContext } from '../entity-context';\n\n/**\n * Hook that returns the ID for the nearest\n * provided entity of the specified type.\n *\n * @param {string} kind The entity kind.\n * @param {string} name The entity name.\n */\nexport default function useEntityId( kind, name ) {\n\tconst context = useContext( EntityContext );\n\treturn context?.[ kind ]?.[ name ];\n}\n"],"mappings":";;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AAKA,IAAAC,cAAA,GAAAD,OAAA;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASE,WAAWA,CAAEC,IAAI,EAAEC,IAAI,EAAG;EACjD,MAAMC,OAAO,GAAG,IAAAC,mBAAU,EAAEC,4BAAc,CAAC;EAC3C,OAAOF,OAAO,GAAIF,IAAI,CAAE,GAAIC,IAAI,CAAE;AACnC","ignoreList":[]}
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = useEntityProp;
8
+ var _element = require("@wordpress/element");
9
+ var _data = require("@wordpress/data");
10
+ var _name = require("../name");
11
+ var _useEntityId = _interopRequireDefault(require("./use-entity-id"));
12
+ /**
13
+ * WordPress dependencies
14
+ */
15
+
16
+ /**
17
+ * Internal dependencies
18
+ */
19
+
20
+ /**
21
+ * Hook that returns the value and a setter for the
22
+ * specified property of the nearest provided
23
+ * entity of the specified type.
24
+ *
25
+ * @param {string} kind The entity kind.
26
+ * @param {string} name The entity name.
27
+ * @param {string} prop The property name.
28
+ * @param {string} [_id] An entity ID to use instead of the context-provided one.
29
+ *
30
+ * @return {[*, Function, *]} An array where the first item is the
31
+ * property value, the second is the
32
+ * setter and the third is the full value
33
+ * object from REST API containing more
34
+ * information like `raw`, `rendered` and
35
+ * `protected` props.
36
+ */
37
+ function useEntityProp(kind, name, prop, _id) {
38
+ const providerId = (0, _useEntityId.default)(kind, name);
39
+ const id = _id !== null && _id !== void 0 ? _id : providerId;
40
+ const {
41
+ value,
42
+ fullValue
43
+ } = (0, _data.useSelect)(select => {
44
+ const {
45
+ getEntityRecord,
46
+ getEditedEntityRecord
47
+ } = select(_name.STORE_NAME);
48
+ const record = getEntityRecord(kind, name, id); // Trigger resolver.
49
+ const editedRecord = getEditedEntityRecord(kind, name, id);
50
+ return record && editedRecord ? {
51
+ value: editedRecord[prop],
52
+ fullValue: record[prop]
53
+ } : {};
54
+ }, [kind, name, id, prop]);
55
+ const {
56
+ editEntityRecord
57
+ } = (0, _data.useDispatch)(_name.STORE_NAME);
58
+ const setValue = (0, _element.useCallback)(newValue => {
59
+ editEntityRecord(kind, name, id, {
60
+ [prop]: newValue
61
+ });
62
+ }, [editEntityRecord, kind, name, id, prop]);
63
+ return [value, setValue, fullValue];
64
+ }
65
+ //# sourceMappingURL=use-entity-prop.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_element","require","_data","_name","_useEntityId","_interopRequireDefault","useEntityProp","kind","name","prop","_id","providerId","useEntityId","id","value","fullValue","useSelect","select","getEntityRecord","getEditedEntityRecord","STORE_NAME","record","editedRecord","editEntityRecord","useDispatch","setValue","useCallback","newValue"],"sources":["@wordpress/core-data/src/hooks/use-entity-prop.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useCallback } from '@wordpress/element';\nimport { useDispatch, useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { STORE_NAME } from '../name';\nimport useEntityId from './use-entity-id';\n\n/**\n * Hook that returns the value and a setter for the\n * specified property of the nearest provided\n * entity of the specified type.\n *\n * @param {string} kind The entity kind.\n * @param {string} name The entity name.\n * @param {string} prop The property name.\n * @param {string} [_id] An entity ID to use instead of the context-provided one.\n *\n * @return {[*, Function, *]} An array where the first item is the\n * property value, the second is the\n * setter and the third is the full value\n * \t\t\t\t\t\t\t object from REST API containing more\n * \t\t\t\t\t\t\t information like `raw`, `rendered` and\n * \t\t\t\t\t\t\t `protected` props.\n */\nexport default function useEntityProp( kind, name, prop, _id ) {\n\tconst providerId = useEntityId( kind, name );\n\tconst id = _id ?? providerId;\n\n\tconst { value, fullValue } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getEntityRecord, getEditedEntityRecord } =\n\t\t\t\tselect( STORE_NAME );\n\t\t\tconst record = getEntityRecord( kind, name, id ); // Trigger resolver.\n\t\t\tconst editedRecord = getEditedEntityRecord( kind, name, id );\n\t\t\treturn record && editedRecord\n\t\t\t\t? {\n\t\t\t\t\t\tvalue: editedRecord[ prop ],\n\t\t\t\t\t\tfullValue: record[ prop ],\n\t\t\t\t }\n\t\t\t\t: {};\n\t\t},\n\t\t[ kind, name, id, prop ]\n\t);\n\tconst { editEntityRecord } = useDispatch( STORE_NAME );\n\tconst setValue = useCallback(\n\t\t( newValue ) => {\n\t\t\teditEntityRecord( kind, name, id, {\n\t\t\t\t[ prop ]: newValue,\n\t\t\t} );\n\t\t},\n\t\t[ editEntityRecord, kind, name, id, prop ]\n\t);\n\n\treturn [ value, setValue, fullValue ];\n}\n"],"mappings":";;;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AACA,IAAAC,KAAA,GAAAD,OAAA;AAKA,IAAAE,KAAA,GAAAF,OAAA;AACA,IAAAG,YAAA,GAAAC,sBAAA,CAAAJ,OAAA;AAVA;AACA;AACA;;AAIA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASK,aAAaA,CAAEC,IAAI,EAAEC,IAAI,EAAEC,IAAI,EAAEC,GAAG,EAAG;EAC9D,MAAMC,UAAU,GAAG,IAAAC,oBAAW,EAAEL,IAAI,EAAEC,IAAK,CAAC;EAC5C,MAAMK,EAAE,GAAGH,GAAG,aAAHA,GAAG,cAAHA,GAAG,GAAIC,UAAU;EAE5B,MAAM;IAAEG,KAAK;IAAEC;EAAU,CAAC,GAAG,IAAAC,eAAS,EACnCC,MAAM,IAAM;IACb,MAAM;MAAEC,eAAe;MAAEC;IAAsB,CAAC,GAC/CF,MAAM,CAAEG,gBAAW,CAAC;IACrB,MAAMC,MAAM,GAAGH,eAAe,CAAEX,IAAI,EAAEC,IAAI,EAAEK,EAAG,CAAC,CAAC,CAAC;IAClD,MAAMS,YAAY,GAAGH,qBAAqB,CAAEZ,IAAI,EAAEC,IAAI,EAAEK,EAAG,CAAC;IAC5D,OAAOQ,MAAM,IAAIC,YAAY,GAC1B;MACAR,KAAK,EAAEQ,YAAY,CAAEb,IAAI,CAAE;MAC3BM,SAAS,EAAEM,MAAM,CAAEZ,IAAI;IACvB,CAAC,GACD,CAAC,CAAC;EACN,CAAC,EACD,CAAEF,IAAI,EAAEC,IAAI,EAAEK,EAAE,EAAEJ,IAAI,CACvB,CAAC;EACD,MAAM;IAAEc;EAAiB,CAAC,GAAG,IAAAC,iBAAW,EAAEJ,gBAAW,CAAC;EACtD,MAAMK,QAAQ,GAAG,IAAAC,oBAAW,EACzBC,QAAQ,IAAM;IACfJ,gBAAgB,CAAEhB,IAAI,EAAEC,IAAI,EAAEK,EAAE,EAAE;MACjC,CAAEJ,IAAI,GAAIkB;IACX,CAAE,CAAC;EACJ,CAAC,EACD,CAAEJ,gBAAgB,EAAEhB,IAAI,EAAEC,IAAI,EAAEK,EAAE,EAAEJ,IAAI,CACzC,CAAC;EAED,OAAO,CAAEK,KAAK,EAAEW,QAAQ,EAAEV,SAAS,CAAE;AACtC","ignoreList":[]}
@@ -6,11 +6,14 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  exports.__experimentalUseEntityRecords = __experimentalUseEntityRecords;
8
8
  exports.default = useEntityRecords;
9
+ exports.useEntityRecordsWithPermissions = useEntityRecordsWithPermissions;
9
10
  var _url = require("@wordpress/url");
10
11
  var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
11
12
  var _data = require("@wordpress/data");
13
+ var _element = require("@wordpress/element");
12
14
  var _useQuerySelect = _interopRequireDefault(require("./use-query-select"));
13
15
  var _ = require("../");
16
+ var _lockUnlock = require("../lock-unlock");
14
17
  /**
15
18
  * WordPress dependencies
16
19
  */
@@ -110,4 +113,40 @@ function __experimentalUseEntityRecords(kind, name, queryArgs, options) {
110
113
  });
111
114
  return useEntityRecords(kind, name, queryArgs, options);
112
115
  }
116
+ function useEntityRecordsWithPermissions(kind, name, queryArgs = {}, options = {
117
+ enabled: true
118
+ }) {
119
+ const entityConfig = (0, _data.useSelect)(select => select(_.store).getEntityConfig(kind, name), [kind, name]);
120
+ const {
121
+ records: data,
122
+ ...ret
123
+ } = useEntityRecords(kind, name, queryArgs, options);
124
+ const ids = (0, _element.useMemo)(() => {
125
+ var _data$map;
126
+ return (_data$map = data?.map(
127
+ // @ts-ignore
128
+ record => {
129
+ var _entityConfig$key;
130
+ return record[(_entityConfig$key = entityConfig?.key) !== null && _entityConfig$key !== void 0 ? _entityConfig$key : 'id'];
131
+ })) !== null && _data$map !== void 0 ? _data$map : [];
132
+ }, [data, entityConfig?.key]);
133
+ const permissions = (0, _data.useSelect)(select => {
134
+ const {
135
+ getEntityRecordsPermissions
136
+ } = (0, _lockUnlock.unlock)(select(_.store));
137
+ return getEntityRecordsPermissions(kind, name, ids);
138
+ }, [ids, kind, name]);
139
+ const dataWithPermissions = (0, _element.useMemo)(() => {
140
+ var _data$map2;
141
+ return (_data$map2 = data?.map((record, index) => ({
142
+ // @ts-ignore
143
+ ...record,
144
+ permissions: permissions[index]
145
+ }))) !== null && _data$map2 !== void 0 ? _data$map2 : [];
146
+ }, [data, permissions]);
147
+ return {
148
+ records: dataWithPermissions,
149
+ ...ret
150
+ };
151
+ }
113
152
  //# sourceMappingURL=use-entity-records.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_url","require","_deprecated","_interopRequireDefault","_data","_useQuerySelect","_","EMPTY_ARRAY","useEntityRecords","kind","name","queryArgs","options","enabled","queryAsString","addQueryArgs","data","records","rest","useQuerySelect","query","coreStore","getEntityRecords","totalItems","totalPages","useSelect","select","getEntityRecordsTotalItems","getEntityRecordsTotalPages","__experimentalUseEntityRecords","deprecated","alternative","since"],"sources":["@wordpress/core-data/src/hooks/use-entity-records.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs } from '@wordpress/url';\nimport deprecated from '@wordpress/deprecated';\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport useQuerySelect from './use-query-select';\nimport { store as coreStore } from '../';\nimport type { Options } from './use-entity-record';\nimport type { Status } from './constants';\n\ninterface EntityRecordsResolution< RecordType > {\n\t/** The requested entity record */\n\trecords: RecordType[] | null;\n\n\t/**\n\t * Is the record still being resolved?\n\t */\n\tisResolving: boolean;\n\n\t/**\n\t * Is the record resolved by now?\n\t */\n\thasResolved: boolean;\n\n\t/** Resolution status */\n\tstatus: Status;\n\n\t/**\n\t * The total number of available items (if not paginated).\n\t */\n\ttotalItems: number | null;\n\n\t/**\n\t * The total number of pages.\n\t */\n\ttotalPages: number | null;\n}\n\nconst EMPTY_ARRAY = [];\n\n/**\n * Resolves the specified entity records.\n *\n * @since 6.1.0 Introduced in WordPress core.\n *\n * @param kind Kind of the entity, e.g. `root` or a `postType`. See rootEntitiesConfig in ../entities.ts for a list of available kinds.\n * @param name Name of the entity, e.g. `plugin` or a `post`. See rootEntitiesConfig in ../entities.ts for a list of available names.\n * @param queryArgs Optional HTTP query description for how to fetch the data, passed to the requested API endpoint.\n * @param options Optional hook options.\n * @example\n * ```js\n * import { useEntityRecords } from '@wordpress/core-data';\n *\n * function PageTitlesList() {\n * const { records, isResolving } = useEntityRecords( 'postType', 'page' );\n *\n * if ( isResolving ) {\n * return 'Loading...';\n * }\n *\n * return (\n * <ul>\n * {records.map(( page ) => (\n * <li>{ page.title }</li>\n * ))}\n * </ul>\n * );\n * }\n *\n * // Rendered in the application:\n * // <PageTitlesList />\n * ```\n *\n * In the above example, when `PageTitlesList` is rendered into an\n * application, the list of records and the resolution details will be retrieved from\n * the store state using `getEntityRecords()`, or resolved if missing.\n *\n * @return Entity records data.\n * @template RecordType\n */\nexport default function useEntityRecords< RecordType >(\n\tkind: string,\n\tname: string,\n\tqueryArgs: Record< string, unknown > = {},\n\toptions: Options = { enabled: true }\n): EntityRecordsResolution< RecordType > {\n\t// Serialize queryArgs to a string that can be safely used as a React dep.\n\t// We can't just pass queryArgs as one of the deps, because if it is passed\n\t// as an object literal, then it will be a different object on each call even\n\t// if the values remain the same.\n\tconst queryAsString = addQueryArgs( '', queryArgs );\n\n\tconst { data: records, ...rest } = useQuerySelect(\n\t\t( query ) => {\n\t\t\tif ( ! options.enabled ) {\n\t\t\t\treturn {\n\t\t\t\t\t// Avoiding returning a new reference on every execution.\n\t\t\t\t\tdata: EMPTY_ARRAY,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn query( coreStore ).getEntityRecords( kind, name, queryArgs );\n\t\t},\n\t\t[ kind, name, queryAsString, options.enabled ]\n\t);\n\n\tconst { totalItems, totalPages } = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! options.enabled ) {\n\t\t\t\treturn {\n\t\t\t\t\ttotalItems: null,\n\t\t\t\t\ttotalPages: null,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn {\n\t\t\t\ttotalItems: select( coreStore ).getEntityRecordsTotalItems(\n\t\t\t\t\tkind,\n\t\t\t\t\tname,\n\t\t\t\t\tqueryArgs\n\t\t\t\t),\n\t\t\t\ttotalPages: select( coreStore ).getEntityRecordsTotalPages(\n\t\t\t\t\tkind,\n\t\t\t\t\tname,\n\t\t\t\t\tqueryArgs\n\t\t\t\t),\n\t\t\t};\n\t\t},\n\t\t[ kind, name, queryAsString, options.enabled ]\n\t);\n\n\treturn {\n\t\trecords,\n\t\ttotalItems,\n\t\ttotalPages,\n\t\t...rest,\n\t};\n}\n\nexport function __experimentalUseEntityRecords(\n\tkind: string,\n\tname: string,\n\tqueryArgs: any,\n\toptions: any\n) {\n\tdeprecated( `wp.data.__experimentalUseEntityRecords`, {\n\t\talternative: 'wp.data.useEntityRecords',\n\t\tsince: '6.1',\n\t} );\n\treturn useEntityRecords( kind, name, queryArgs, options );\n}\n"],"mappings":";;;;;;;;AAGA,IAAAA,IAAA,GAAAC,OAAA;AACA,IAAAC,WAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,KAAA,GAAAH,OAAA;AAKA,IAAAI,eAAA,GAAAF,sBAAA,CAAAF,OAAA;AACA,IAAAK,CAAA,GAAAL,OAAA;AAXA;AACA;AACA;;AAKA;AACA;AACA;;AAkCA,MAAMM,WAAW,GAAG,EAAE;;AAEtB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASC,gBAAgBA,CACvCC,IAAY,EACZC,IAAY,EACZC,SAAoC,GAAG,CAAC,CAAC,EACzCC,OAAgB,GAAG;EAAEC,OAAO,EAAE;AAAK,CAAC,EACI;EACxC;EACA;EACA;EACA;EACA,MAAMC,aAAa,GAAG,IAAAC,iBAAY,EAAE,EAAE,EAAEJ,SAAU,CAAC;EAEnD,MAAM;IAAEK,IAAI,EAAEC,OAAO;IAAE,GAAGC;EAAK,CAAC,GAAG,IAAAC,uBAAc,EAC9CC,KAAK,IAAM;IACZ,IAAK,CAAER,OAAO,CAACC,OAAO,EAAG;MACxB,OAAO;QACN;QACAG,IAAI,EAAET;MACP,CAAC;IACF;IACA,OAAOa,KAAK,CAAEC,OAAU,CAAC,CAACC,gBAAgB,CAAEb,IAAI,EAAEC,IAAI,EAAEC,SAAU,CAAC;EACpE,CAAC,EACD,CAAEF,IAAI,EAAEC,IAAI,EAAEI,aAAa,EAAEF,OAAO,CAACC,OAAO,CAC7C,CAAC;EAED,MAAM;IAAEU,UAAU;IAAEC;EAAW,CAAC,GAAG,IAAAC,eAAS,EACzCC,MAAM,IAAM;IACb,IAAK,CAAEd,OAAO,CAACC,OAAO,EAAG;MACxB,OAAO;QACNU,UAAU,EAAE,IAAI;QAChBC,UAAU,EAAE;MACb,CAAC;IACF;IACA,OAAO;MACND,UAAU,EAAEG,MAAM,CAAEL,OAAU,CAAC,CAACM,0BAA0B,CACzDlB,IAAI,EACJC,IAAI,EACJC,SACD,CAAC;MACDa,UAAU,EAAEE,MAAM,CAAEL,OAAU,CAAC,CAACO,0BAA0B,CACzDnB,IAAI,EACJC,IAAI,EACJC,SACD;IACD,CAAC;EACF,CAAC,EACD,CAAEF,IAAI,EAAEC,IAAI,EAAEI,aAAa,EAAEF,OAAO,CAACC,OAAO,CAC7C,CAAC;EAED,OAAO;IACNI,OAAO;IACPM,UAAU;IACVC,UAAU;IACV,GAAGN;EACJ,CAAC;AACF;AAEO,SAASW,8BAA8BA,CAC7CpB,IAAY,EACZC,IAAY,EACZC,SAAc,EACdC,OAAY,EACX;EACD,IAAAkB,mBAAU,EAAG,wCAAuC,EAAE;IACrDC,WAAW,EAAE,0BAA0B;IACvCC,KAAK,EAAE;EACR,CAAE,CAAC;EACH,OAAOxB,gBAAgB,CAAEC,IAAI,EAAEC,IAAI,EAAEC,SAAS,EAAEC,OAAQ,CAAC;AAC1D","ignoreList":[]}
1
+ {"version":3,"names":["_url","require","_deprecated","_interopRequireDefault","_data","_element","_useQuerySelect","_","_lockUnlock","EMPTY_ARRAY","useEntityRecords","kind","name","queryArgs","options","enabled","queryAsString","addQueryArgs","data","records","rest","useQuerySelect","query","coreStore","getEntityRecords","totalItems","totalPages","useSelect","select","getEntityRecordsTotalItems","getEntityRecordsTotalPages","__experimentalUseEntityRecords","deprecated","alternative","since","useEntityRecordsWithPermissions","entityConfig","getEntityConfig","ret","ids","useMemo","_data$map","map","record","_entityConfig$key","key","permissions","getEntityRecordsPermissions","unlock","dataWithPermissions","_data$map2","index"],"sources":["@wordpress/core-data/src/hooks/use-entity-records.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs } from '@wordpress/url';\nimport deprecated from '@wordpress/deprecated';\nimport { useSelect } from '@wordpress/data';\nimport { useMemo } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport useQuerySelect from './use-query-select';\nimport { store as coreStore } from '../';\nimport type { Options } from './use-entity-record';\nimport type { Status } from './constants';\nimport { unlock } from '../lock-unlock';\n\ninterface EntityRecordsResolution< RecordType > {\n\t/** The requested entity record */\n\trecords: RecordType[] | null;\n\n\t/**\n\t * Is the record still being resolved?\n\t */\n\tisResolving: boolean;\n\n\t/**\n\t * Is the record resolved by now?\n\t */\n\thasResolved: boolean;\n\n\t/** Resolution status */\n\tstatus: Status;\n\n\t/**\n\t * The total number of available items (if not paginated).\n\t */\n\ttotalItems: number | null;\n\n\t/**\n\t * The total number of pages.\n\t */\n\ttotalPages: number | null;\n}\n\nconst EMPTY_ARRAY = [];\n\n/**\n * Resolves the specified entity records.\n *\n * @since 6.1.0 Introduced in WordPress core.\n *\n * @param kind Kind of the entity, e.g. `root` or a `postType`. See rootEntitiesConfig in ../entities.ts for a list of available kinds.\n * @param name Name of the entity, e.g. `plugin` or a `post`. See rootEntitiesConfig in ../entities.ts for a list of available names.\n * @param queryArgs Optional HTTP query description for how to fetch the data, passed to the requested API endpoint.\n * @param options Optional hook options.\n * @example\n * ```js\n * import { useEntityRecords } from '@wordpress/core-data';\n *\n * function PageTitlesList() {\n * const { records, isResolving } = useEntityRecords( 'postType', 'page' );\n *\n * if ( isResolving ) {\n * return 'Loading...';\n * }\n *\n * return (\n * <ul>\n * {records.map(( page ) => (\n * <li>{ page.title }</li>\n * ))}\n * </ul>\n * );\n * }\n *\n * // Rendered in the application:\n * // <PageTitlesList />\n * ```\n *\n * In the above example, when `PageTitlesList` is rendered into an\n * application, the list of records and the resolution details will be retrieved from\n * the store state using `getEntityRecords()`, or resolved if missing.\n *\n * @return Entity records data.\n * @template RecordType\n */\nexport default function useEntityRecords< RecordType >(\n\tkind: string,\n\tname: string,\n\tqueryArgs: Record< string, unknown > = {},\n\toptions: Options = { enabled: true }\n): EntityRecordsResolution< RecordType > {\n\t// Serialize queryArgs to a string that can be safely used as a React dep.\n\t// We can't just pass queryArgs as one of the deps, because if it is passed\n\t// as an object literal, then it will be a different object on each call even\n\t// if the values remain the same.\n\tconst queryAsString = addQueryArgs( '', queryArgs );\n\n\tconst { data: records, ...rest } = useQuerySelect(\n\t\t( query ) => {\n\t\t\tif ( ! options.enabled ) {\n\t\t\t\treturn {\n\t\t\t\t\t// Avoiding returning a new reference on every execution.\n\t\t\t\t\tdata: EMPTY_ARRAY,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn query( coreStore ).getEntityRecords( kind, name, queryArgs );\n\t\t},\n\t\t[ kind, name, queryAsString, options.enabled ]\n\t);\n\n\tconst { totalItems, totalPages } = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! options.enabled ) {\n\t\t\t\treturn {\n\t\t\t\t\ttotalItems: null,\n\t\t\t\t\ttotalPages: null,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn {\n\t\t\t\ttotalItems: select( coreStore ).getEntityRecordsTotalItems(\n\t\t\t\t\tkind,\n\t\t\t\t\tname,\n\t\t\t\t\tqueryArgs\n\t\t\t\t),\n\t\t\t\ttotalPages: select( coreStore ).getEntityRecordsTotalPages(\n\t\t\t\t\tkind,\n\t\t\t\t\tname,\n\t\t\t\t\tqueryArgs\n\t\t\t\t),\n\t\t\t};\n\t\t},\n\t\t[ kind, name, queryAsString, options.enabled ]\n\t);\n\n\treturn {\n\t\trecords,\n\t\ttotalItems,\n\t\ttotalPages,\n\t\t...rest,\n\t};\n}\n\nexport function __experimentalUseEntityRecords(\n\tkind: string,\n\tname: string,\n\tqueryArgs: any,\n\toptions: any\n) {\n\tdeprecated( `wp.data.__experimentalUseEntityRecords`, {\n\t\talternative: 'wp.data.useEntityRecords',\n\t\tsince: '6.1',\n\t} );\n\treturn useEntityRecords( kind, name, queryArgs, options );\n}\n\nexport function useEntityRecordsWithPermissions< RecordType >(\n\tkind: string,\n\tname: string,\n\tqueryArgs: Record< string, unknown > = {},\n\toptions: Options = { enabled: true }\n): EntityRecordsResolution< RecordType > {\n\tconst entityConfig = useSelect(\n\t\t( select ) => select( coreStore ).getEntityConfig( kind, name ),\n\t\t[ kind, name ]\n\t);\n\tconst { records: data, ...ret } = useEntityRecords(\n\t\tkind,\n\t\tname,\n\t\tqueryArgs,\n\t\toptions\n\t);\n\tconst ids = useMemo(\n\t\t() =>\n\t\t\tdata?.map(\n\t\t\t\t// @ts-ignore\n\t\t\t\t( record: RecordType ) => record[ entityConfig?.key ?? 'id' ]\n\t\t\t) ?? [],\n\t\t[ data, entityConfig?.key ]\n\t);\n\n\tconst permissions = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getEntityRecordsPermissions } = unlock(\n\t\t\t\tselect( coreStore )\n\t\t\t);\n\t\t\treturn getEntityRecordsPermissions( kind, name, ids );\n\t\t},\n\t\t[ ids, kind, name ]\n\t);\n\n\tconst dataWithPermissions = useMemo(\n\t\t() =>\n\t\t\tdata?.map( ( record, index ) => ( {\n\t\t\t\t// @ts-ignore\n\t\t\t\t...record,\n\t\t\t\tpermissions: permissions[ index ],\n\t\t\t} ) ) ?? [],\n\t\t[ data, permissions ]\n\t);\n\n\treturn { records: dataWithPermissions, ...ret };\n}\n"],"mappings":";;;;;;;;;AAGA,IAAAA,IAAA,GAAAC,OAAA;AACA,IAAAC,WAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,KAAA,GAAAH,OAAA;AACA,IAAAI,QAAA,GAAAJ,OAAA;AAKA,IAAAK,eAAA,GAAAH,sBAAA,CAAAF,OAAA;AACA,IAAAM,CAAA,GAAAN,OAAA;AAGA,IAAAO,WAAA,GAAAP,OAAA;AA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AA,cAAAA,SAAA,GAAI,EAAE;EAAA,GACR,CAAEvB,IAAI,EAAEkB,YAAY,EAAES,GAAG,CAC1B,CAAC;EAED,MAAMC,WAAW,GAAG,IAAAnB,eAAS,EAC1BC,MAAM,IAAM;IACb,MAAM;MAAEmB;IAA4B,CAAC,GAAG,IAAAC,kBAAM,EAC7CpB,MAAM,CAAEL,OAAU,CACnB,CAAC;IACD,OAAOwB,2BAA2B,CAAEpC,IAAI,EAAEC,IAAI,EAAE2B,GAAI,CAAC;EACtD,CAAC,EACD,CAAEA,GAAG,EAAE5B,IAAI,EAAEC,IAAI,CAClB,CAAC;EAED,MAAMqC,mBAAmB,GAAG,IAAAT,gBAAO,EAClC;IAAA,IAAAU,UAAA;IAAA,QAAAA,UAAA,GACChC,IAAI,EAAEwB,GAAG,CAAE,CAAEC,MAAM,EAAEQ,KAAK,MAAQ;MACjC;MACA,GAAGR,MAAM;MACTG,WAAW,EAAEA,WAAW,CAAEK,KAAK;IAChC,CAAC,CAAG,CAAC,cAAAD,UAAA,cAAAA,UAAA,GAAI,EAAE;EAAA,GACZ,CAAEhC,IAAI,EAAE4B,WAAW,CACpB,CAAC;EAED,OAAO;IAAE3B,OAAO,EAAE8B,mBAAmB;IAAE,GAAGX;EAAI,CAAC;AAChD","ignoreList":[]}
@@ -5,8 +5,9 @@ Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
7
  exports.__experimentalUseResourcePermissions = __experimentalUseResourcePermissions;
8
- exports.default = useResourcePermissions;
8
+ exports.default = void 0;
9
9
  var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
10
+ var _warning = _interopRequireDefault(require("@wordpress/warning"));
10
11
  var _ = require("../");
11
12
  var _constants = require("./constants");
12
13
  var _useQuerySelect = _interopRequireDefault(require("./use-query-select"));
@@ -27,15 +28,17 @@ var _useQuerySelect = _interopRequireDefault(require("./use-query-select"));
27
28
  *
28
29
  * @since 6.1.0 Introduced in WordPress core.
29
30
  *
30
- * @param resource The resource in question, e.g. media.
31
- * @param id ID of a specific resource entry, if needed, e.g. 10.
31
+ * @param resource Entity resource to check. Accepts entity object `{ kind: 'root', name: 'media', id: 1 }`
32
+ * or REST base as a string - `media`.
33
+ * @param id Optional ID of the resource to check, e.g. 10. Note: This argument is discouraged
34
+ * when using an entity object as a resource to check permissions and will be ignored.
32
35
  *
33
36
  * @example
34
37
  * ```js
35
38
  * import { useResourcePermissions } from '@wordpress/core-data';
36
39
  *
37
40
  * function PagesList() {
38
- * const { canCreate, isResolving } = useResourcePermissions( 'pages' );
41
+ * const { canCreate, isResolving } = useResourcePermissions( { kind: 'postType', name: 'page' } );
39
42
  *
40
43
  * if ( isResolving ) {
41
44
  * return 'Loading ...';
@@ -63,7 +66,7 @@ var _useQuerySelect = _interopRequireDefault(require("./use-query-select"));
63
66
  * canUpdate,
64
67
  * canDelete,
65
68
  * isResolving
66
- * } = useResourcePermissions( 'pages', pageId );
69
+ * } = useResourcePermissions( { kind: 'postType', name: 'page', id: pageId } );
67
70
  *
68
71
  * if ( isResolving ) {
69
72
  * return 'Loading ...';
@@ -91,12 +94,25 @@ var _useQuerySelect = _interopRequireDefault(require("./use-query-select"));
91
94
  * @template IdType
92
95
  */
93
96
  function useResourcePermissions(resource, id) {
97
+ // Serialize `resource` to a string that can be safely used as a React dep.
98
+ // We can't just pass `resource` as one of the deps, because if it is passed
99
+ // as an object literal, then it will be a different object on each call even
100
+ // if the values remain the same.
101
+ const isEntity = typeof resource === 'object';
102
+ const resourceAsString = isEntity ? JSON.stringify(resource) : resource;
103
+ if (isEntity && typeof id !== 'undefined') {
104
+ globalThis.SCRIPT_DEBUG === true ? (0, _warning.default)(`When 'resource' is an entity object, passing 'id' as a separate argument isn't supported.`) : void 0;
105
+ }
94
106
  return (0, _useQuerySelect.default)(resolve => {
107
+ const hasId = isEntity ? !!resource.id : !!id;
95
108
  const {
96
109
  canUser
97
110
  } = resolve(_.store);
98
- const create = canUser('create', resource);
99
- if (!id) {
111
+ const create = canUser('create', isEntity ? {
112
+ kind: resource.kind,
113
+ name: resource.name
114
+ } : resource);
115
+ if (!hasId) {
100
116
  const read = canUser('read', resource);
101
117
  const isResolving = create.isResolving || read.isResolving;
102
118
  const hasResolved = create.hasResolved && read.hasResolved;
@@ -134,8 +150,9 @@ function useResourcePermissions(resource, id) {
134
150
  canUpdate: hasResolved && update.data,
135
151
  canDelete: hasResolved && _delete.data
136
152
  };
137
- }, [resource, id]);
153
+ }, [resourceAsString, id]);
138
154
  }
155
+ var _default = exports.default = useResourcePermissions;
139
156
  function __experimentalUseResourcePermissions(resource, id) {
140
157
  (0, _deprecated.default)(`wp.data.__experimentalUseResourcePermissions`, {
141
158
  alternative: 'wp.data.useResourcePermissions',
@@ -1 +1 @@
1
- {"version":3,"names":["_deprecated","_interopRequireDefault","require","_","_constants","_useQuerySelect","useResourcePermissions","resource","id","useQuerySelect","resolve","canUser","coreStore","create","read","isResolving","hasResolved","status","Status","Idle","Resolving","Success","canCreate","data","canRead","update","_delete","canUpdate","canDelete","__experimentalUseResourcePermissions","deprecated","alternative","since"],"sources":["@wordpress/core-data/src/hooks/use-resource-permissions.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Internal dependencies\n */\nimport { store as coreStore } from '../';\nimport { Status } from './constants';\nimport useQuerySelect from './use-query-select';\n\ninterface GlobalResourcePermissionsResolution {\n\t/** Can the current user create new resources of this type? */\n\tcanCreate: boolean;\n}\ninterface SpecificResourcePermissionsResolution {\n\t/** Can the current user update resources of this type? */\n\tcanUpdate: boolean;\n\t/** Can the current user delete resources of this type? */\n\tcanDelete: boolean;\n}\ninterface ResolutionDetails {\n\t/** Resolution status */\n\tstatus: Status;\n\t/**\n\t * Is the data still being resolved?\n\t */\n\tisResolving: boolean;\n}\n\n/**\n * Is the data resolved by now?\n */\ntype HasResolved = boolean;\n\ntype ResourcePermissionsResolution< IdType > = [\n\tHasResolved,\n\tResolutionDetails &\n\t\tGlobalResourcePermissionsResolution &\n\t\t( IdType extends void ? SpecificResourcePermissionsResolution : {} ),\n];\n\n/**\n * Resolves resource permissions.\n *\n * @since 6.1.0 Introduced in WordPress core.\n *\n * @param resource The resource in question, e.g. media.\n * @param id ID of a specific resource entry, if needed, e.g. 10.\n *\n * @example\n * ```js\n * import { useResourcePermissions } from '@wordpress/core-data';\n *\n * function PagesList() {\n * const { canCreate, isResolving } = useResourcePermissions( 'pages' );\n *\n * if ( isResolving ) {\n * return 'Loading ...';\n * }\n *\n * return (\n * <div>\n * {canCreate ? (<button>+ Create a new page</button>) : false}\n * // ...\n * </div>\n * );\n * }\n *\n * // Rendered in the application:\n * // <PagesList />\n * ```\n *\n * @example\n * ```js\n * import { useResourcePermissions } from '@wordpress/core-data';\n *\n * function Page({ pageId }) {\n * const {\n * canCreate,\n * canUpdate,\n * canDelete,\n * isResolving\n * } = useResourcePermissions( 'pages', pageId );\n *\n * if ( isResolving ) {\n * return 'Loading ...';\n * }\n *\n * return (\n * <div>\n * {canCreate ? (<button>+ Create a new page</button>) : false}\n * {canUpdate ? (<button>Edit page</button>) : false}\n * {canDelete ? (<button>Delete page</button>) : false}\n * // ...\n * </div>\n * );\n * }\n *\n * // Rendered in the application:\n * // <Page pageId={ 15 } />\n * ```\n *\n * In the above example, when `PagesList` is rendered into an\n * application, the appropriate permissions and the resolution details will be retrieved from\n * the store state using `canUser()`, or resolved if missing.\n *\n * @return Entity records data.\n * @template IdType\n */\nexport default function useResourcePermissions< IdType = void >(\n\tresource: string,\n\tid?: IdType\n): ResourcePermissionsResolution< IdType > {\n\treturn useQuerySelect(\n\t\t( resolve ) => {\n\t\t\tconst { canUser } = resolve( coreStore );\n\t\t\tconst create = canUser( 'create', resource );\n\t\t\tif ( ! id ) {\n\t\t\t\tconst read = canUser( 'read', resource );\n\n\t\t\t\tconst isResolving = create.isResolving || read.isResolving;\n\t\t\t\tconst hasResolved = create.hasResolved && read.hasResolved;\n\t\t\t\tlet status = Status.Idle;\n\t\t\t\tif ( isResolving ) {\n\t\t\t\t\tstatus = Status.Resolving;\n\t\t\t\t} else if ( hasResolved ) {\n\t\t\t\t\tstatus = Status.Success;\n\t\t\t\t}\n\n\t\t\t\treturn {\n\t\t\t\t\tstatus,\n\t\t\t\t\tisResolving,\n\t\t\t\t\thasResolved,\n\t\t\t\t\tcanCreate: create.hasResolved && create.data,\n\t\t\t\t\tcanRead: read.hasResolved && read.data,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst read = canUser( 'read', resource, id );\n\t\t\tconst update = canUser( 'update', resource, id );\n\t\t\tconst _delete = canUser( 'delete', resource, id );\n\t\t\tconst isResolving =\n\t\t\t\tread.isResolving ||\n\t\t\t\tcreate.isResolving ||\n\t\t\t\tupdate.isResolving ||\n\t\t\t\t_delete.isResolving;\n\t\t\tconst hasResolved =\n\t\t\t\tread.hasResolved &&\n\t\t\t\tcreate.hasResolved &&\n\t\t\t\tupdate.hasResolved &&\n\t\t\t\t_delete.hasResolved;\n\n\t\t\tlet status = Status.Idle;\n\t\t\tif ( isResolving ) {\n\t\t\t\tstatus = Status.Resolving;\n\t\t\t} else if ( hasResolved ) {\n\t\t\t\tstatus = Status.Success;\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tstatus,\n\t\t\t\tisResolving,\n\t\t\t\thasResolved,\n\t\t\t\tcanRead: hasResolved && read.data,\n\t\t\t\tcanCreate: hasResolved && create.data,\n\t\t\t\tcanUpdate: hasResolved && update.data,\n\t\t\t\tcanDelete: hasResolved && _delete.data,\n\t\t\t};\n\t\t},\n\t\t[ resource, id ]\n\t);\n}\n\nexport function __experimentalUseResourcePermissions(\n\tresource: string,\n\tid?: unknown\n) {\n\tdeprecated( `wp.data.__experimentalUseResourcePermissions`, {\n\t\talternative: 'wp.data.useResourcePermissions',\n\t\tsince: '6.1',\n\t} );\n\treturn useResourcePermissions( resource, id );\n}\n"],"mappings":";;;;;;;;AAGA,IAAAA,WAAA,GAAAC,sBAAA,CAAAC,OAAA;AAKA,IAA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gC;IAC7CC,KAAK,EAAE;EACR,CAAE,CAAC;EACH,OAAO1B,sBAAsB,CAAEC,QAAQ,EAAEC,EAAG,CAAC;AAC9C","ignoreList":[]}
1
+ {"version":3,"names":["_deprecated","_interopRequireDefault","require","_warning","_","_constants","_useQuerySelect","useResourcePermissions","resource","id","isEntity","resourceAsString","JSON","stringify","globalThis","SCRIPT_DEBUG","warning","useQuerySelect","resolve","hasId","canUser","coreStore","create","kind","name","read","isResolving","hasResolved","status","Status","Idle","Resolving","Success","canCreate","data","canRead","update","_delete","canUpdate","canDelete","_default","exports","default","__experimentalUseResourcePermissions","deprecated","alternative","since"],"sources":["@wordpress/core-data/src/hooks/use-resource-permissions.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport deprecated from '@wordpress/deprecated';\nimport warning from '@wordpress/warning';\n\n/**\n * Internal dependencies\n */\nimport { store as coreStore } from '../';\nimport { Status } from './constants';\nimport useQuerySelect from './use-query-select';\n\ninterface GlobalResourcePermissionsResolution {\n\t/** Can the current user create new resources of this type? */\n\tcanCreate: boolean;\n}\ninterface SpecificResourcePermissionsResolution {\n\t/** Can the current user update resources of this type? */\n\tcanUpdate: boolean;\n\t/** Can the current user delete resources of this type? */\n\tcanDelete: boolean;\n}\ninterface ResolutionDetails {\n\t/** Resolution status */\n\tstatus: Status;\n\t/**\n\t * Is the data still being resolved?\n\t */\n\tisResolving: boolean;\n}\n\n/**\n * Is the data resolved by now?\n */\ntype HasResolved = boolean;\n\ntype ResourcePermissionsResolution< IdType > = [\n\tHasResolved,\n\tResolutionDetails &\n\t\tGlobalResourcePermissionsResolution &\n\t\t( IdType extends void ? SpecificResourcePermissionsResolution : {} ),\n];\n\ntype EntityResource = { kind: string; name: string; id?: string | number };\n\nfunction useResourcePermissions< IdType = void >(\n\tresource: string,\n\tid?: IdType\n): ResourcePermissionsResolution< IdType >;\n\nfunction useResourcePermissions< IdType = void >(\n\tresource: EntityResource,\n\tid?: never\n): ResourcePermissionsResolution< IdType >;\n\n/**\n * Resolves resource permissions.\n *\n * @since 6.1.0 Introduced in WordPress core.\n *\n * @param resource Entity resource to check. Accepts entity object `{ kind: 'root', name: 'media', id: 1 }`\n * or REST base as a string - `media`.\n * @param id Optional ID of the resource to check, e.g. 10. Note: This argument is discouraged\n * when using an entity object as a resource to check permissions and will be ignored.\n *\n * @example\n * ```js\n * import { useResourcePermissions } from '@wordpress/core-data';\n *\n * function PagesList() {\n * const { canCreate, isResolving } = useResourcePermissions( { kind: 'postType', name: 'page' } );\n *\n * if ( isResolving ) {\n * return 'Loading ...';\n * }\n *\n * return (\n * <div>\n * {canCreate ? (<button>+ Create a new page</button>) : false}\n * // ...\n * </div>\n * );\n * }\n *\n * // Rendered in the application:\n * // <PagesList />\n * ```\n *\n * @example\n * ```js\n * import { useResourcePermissions } from '@wordpress/core-data';\n *\n * function Page({ pageId }) {\n * const {\n * canCreate,\n * canUpdate,\n * canDelete,\n * isResolving\n * } = useResourcePermissions( { kind: 'postType', name: 'page', id: pageId } );\n *\n * if ( isResolving ) {\n * return 'Loading ...';\n * }\n *\n * return (\n * <div>\n * {canCreate ? (<button>+ Create a new page</button>) : false}\n * {canUpdate ? (<button>Edit page</button>) : false}\n * {canDelete ? (<button>Delete page</button>) : false}\n * // ...\n * </div>\n * );\n * }\n *\n * // Rendered in the application:\n * // <Page pageId={ 15 } />\n * ```\n *\n * In the above example, when `PagesList` is rendered into an\n * application, the appropriate permissions and the resolution details will be retrieved from\n * the store state using `canUser()`, or resolved if missing.\n *\n * @return Entity records data.\n * @template IdType\n */\nfunction useResourcePermissions< IdType = void >(\n\tresource: string | EntityResource,\n\tid?: IdType\n): ResourcePermissionsResolution< IdType > {\n\t// Serialize `resource` to a string that can be safely used as a React dep.\n\t// We can't just pass `resource` as one of the deps, because if it is passed\n\t// as an object literal, then it will be a different object on each call even\n\t// if the values remain the same.\n\tconst isEntity = typeof resource === 'object';\n\tconst resourceAsString = isEntity ? JSON.stringify( resource ) : resource;\n\n\tif ( isEntity && typeof id !== 'undefined' ) {\n\t\twarning(\n\t\t\t`When 'resource' is an entity object, passing 'id' as a separate argument isn't supported.`\n\t\t);\n\t}\n\n\treturn useQuerySelect(\n\t\t( resolve ) => {\n\t\t\tconst hasId = isEntity ? !! resource.id : !! id;\n\t\t\tconst { canUser } = resolve( coreStore );\n\t\t\tconst create = canUser(\n\t\t\t\t'create',\n\t\t\t\tisEntity\n\t\t\t\t\t? { kind: resource.kind, name: resource.name }\n\t\t\t\t\t: resource\n\t\t\t);\n\n\t\t\tif ( ! hasId ) {\n\t\t\t\tconst read = canUser( 'read', resource );\n\n\t\t\t\tconst isResolving = create.isResolving || read.isResolving;\n\t\t\t\tconst hasResolved = create.hasResolved && read.hasResolved;\n\t\t\t\tlet status = Status.Idle;\n\t\t\t\tif ( isResolving ) {\n\t\t\t\t\tstatus = Status.Resolving;\n\t\t\t\t} else if ( hasResolved ) {\n\t\t\t\t\tstatus = Status.Success;\n\t\t\t\t}\n\n\t\t\t\treturn {\n\t\t\t\t\tstatus,\n\t\t\t\t\tisResolving,\n\t\t\t\t\thasResolved,\n\t\t\t\t\tcanCreate: create.hasResolved && create.data,\n\t\t\t\t\tcanRead: read.hasResolved && read.data,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst read = canUser( 'read', resource, id );\n\t\t\tconst update = canUser( 'update', resource, id );\n\t\t\tconst _delete = canUser( 'delete', resource, id );\n\t\t\tconst isResolving =\n\t\t\t\tread.isResolving ||\n\t\t\t\tcreate.isResolving ||\n\t\t\t\tupdate.isResolving ||\n\t\t\t\t_delete.isResolving;\n\t\t\tconst hasResolved =\n\t\t\t\tread.hasResolved &&\n\t\t\t\tcreate.hasResolved &&\n\t\t\t\tupdate.hasResolved &&\n\t\t\t\t_delete.hasResolved;\n\n\t\t\tlet status = Status.Idle;\n\t\t\tif ( isResolving ) {\n\t\t\t\tstatus = Status.Resolving;\n\t\t\t} else if ( hasResolved ) {\n\t\t\t\tstatus = Status.Success;\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tstatus,\n\t\t\t\tisResolving,\n\t\t\t\thasResolved,\n\t\t\t\tcanRead: hasResolved && read.data,\n\t\t\t\tcanCreate: hasResolved && create.data,\n\t\t\t\tcanUpdate: hasResolved && update.data,\n\t\t\t\tcanDelete: hasResolved && _delete.data,\n\t\t\t};\n\t\t},\n\t\t[ resourceAsString, id ]\n\t);\n}\n\nexport default useResourcePermissions;\n\nexport function __experimentalUseResourcePermissions(\n\tresource: string,\n\tid?: unknown\n) {\n\tdeprecated( `wp.data.__experimentalUseResourcePermissions`, {\n\t\talternative: 'wp.data.useResourcePermissions',\n\t\tsince: '6.1',\n\t} );\n\treturn useResourcePermissions( resource, id );\n}\n"],"mappings":";;;;;;;;AAGA,IAAAA,WAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,QAAA,GAAAF,sBAAA,CAAAC,OAAA;AAKA,IAAAE,CAAA,GAAAF,OAAA;AACA,IAAAG,UAAA,GAAAH,OAAA;AACA,IAAAI,eAAA,GAAAL,sBAAA,CAAAC,OAAA;AAXA;AACA;AACA;;AAIA;AACA;AACA;;AAwBA;AACA;AACA;;AAsBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASK,sBAAsBA,CAC9BC,QAAiC,EACjCC,EAAW,EAC+B;EAC1C;EACA;EACA;EACA;EACA,MAAMC,QAAQ,GAAG,OAAOF,QAAQ,KAAK,QAAQ;EAC7C,MAAMG,gBAAgB,GAAGD,QAAQ,GAAGE,IAAI,CAACC,S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