@frontify/guideline-blocks-settings 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,53 +1,63 @@
1
- import { jsx as a } from "react/jsx-runtime";
1
+ import { jsx as i } from "react/jsx-runtime";
2
2
  import { useBlockAssets as p } from "@frontify/app-bridge";
3
- import { createContext as w, useContext as u } from "react";
4
- const l = (t, e) => {
5
- const { blockAssets: n, addAssetIdsToKey: s, deleteAssetIdsFromKey: h, updateAssetIdsFromKey: d } = p(t), i = (n == null ? void 0 : n[e]) || [];
3
+ import { createContext as u, useContext as w } from "react";
4
+ const l = (t, o) => {
5
+ const { blockAssets: e, addAssetIdsToKey: n, deleteAssetIdsFromKey: d, updateAssetIdsFromKey: a } = o, r = (e == null ? void 0 : e[t]) || [];
6
6
  return {
7
- onAttachmentsAdd: async (o) => {
8
- await s(
9
- e,
10
- o.map((r) => r.id)
7
+ onAttachmentsAdd: async (s) => {
8
+ await n(
9
+ t,
10
+ s.map((c) => c.id)
11
11
  );
12
12
  },
13
- onAttachmentDelete: async (o) => {
14
- await h(e, [o.id]);
13
+ onAttachmentDelete: async (s) => {
14
+ await d(t, [s.id]);
15
15
  },
16
- onAttachmentReplace: async (o, r) => {
17
- const c = i.map(
18
- (m) => m.id === o.id ? r.id : m.id
16
+ onAttachmentReplace: async (s, c) => {
17
+ const m = r.map(
18
+ (A) => A.id === s.id ? c.id : A.id
19
19
  );
20
- await d(e, c);
20
+ await a(t, m);
21
21
  },
22
- onAttachmentsSorted: async (o) => {
23
- const r = o.map((c) => c.id);
24
- await d(e, r);
22
+ onAttachmentsSorted: async (s) => {
23
+ const c = s.map((m) => m.id);
24
+ await a(t, c);
25
25
  },
26
- attachments: i,
26
+ attachments: r
27
+ };
28
+ }, x = (t, o) => {
29
+ const { onAttachmentsAdd: e, onAttachmentDelete: n, onAttachmentReplace: d, onAttachmentsSorted: a, attachments: r } = l(o, p(t));
30
+ return {
31
+ onAttachmentsAdd: e,
32
+ onAttachmentDelete: n,
33
+ onAttachmentReplace: d,
34
+ onAttachmentsSorted: a,
35
+ attachments: r,
27
36
  appBridge: t
28
37
  };
29
- }, A = w(null), x = ({
38
+ }, h = u(null), C = ({
30
39
  appBridge: t,
31
- children: e,
32
- assetId: n
40
+ children: o,
41
+ assetId: e
33
42
  }) => {
34
- const s = l(t, n);
35
- return /* @__PURE__ */ a(A.Provider, { value: s, children: e });
36
- }, D = () => {
37
- const t = u(A);
43
+ const n = x(t, e);
44
+ return /* @__PURE__ */ i(h.Provider, { value: n, children: o });
45
+ }, S = () => {
46
+ const t = w(h);
38
47
  if (!t)
39
48
  throw new Error(
40
49
  "No AttachmentsContext Provided. Component must be wrapped in an 'AttachmentsProvider' or the 'withAttachmentsProvider' HOC"
41
50
  );
42
51
  return t;
43
- }, F = (t, e) => {
44
- const n = (s) => /* @__PURE__ */ a(x, { appBridge: s.appBridge, assetId: e, children: /* @__PURE__ */ a(t, { ...s }) });
45
- return n.displayName = "withAttachmentsProvider", n;
52
+ }, B = (t, o) => {
53
+ const e = (n) => /* @__PURE__ */ i(C, { appBridge: n.appBridge, assetId: o, children: /* @__PURE__ */ i(t, { ...n }) });
54
+ return e.displayName = "withAttachmentsProvider", e;
46
55
  };
47
56
  export {
48
- x as AttachmentsProvider,
49
- l as useAttachments,
50
- D as useAttachmentsContext,
51
- F as withAttachmentsProvider
57
+ C as AttachmentsProvider,
58
+ l as useAttachmentOperations,
59
+ x as useAttachments,
60
+ S as useAttachmentsContext,
61
+ B as withAttachmentsProvider
52
62
  };
53
63
  //# sourceMappingURL=useAttachments.es.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useAttachments.es.js","sources":["../../src/hooks/useAttachments.tsx"],"sourcesContent":["/* (c) Copyright Frontify Ltd., all rights reserved. */\n\nimport { type AppBridgeBlock, type Asset, useBlockAssets } from '@frontify/app-bridge';\nimport { type ReactNode, createContext, useContext } from 'react';\n\nimport { type BlockProps } from '../index';\n\nexport const useAttachments = (appBridge: AppBridgeBlock, attachmentKey: string) => {\n const { blockAssets, addAssetIdsToKey, deleteAssetIdsFromKey, updateAssetIdsFromKey } = useBlockAssets(appBridge);\n const attachments = blockAssets?.[attachmentKey] || [];\n\n const onAttachmentsAdd = async (newAssets: Asset[]) => {\n await addAssetIdsToKey(\n attachmentKey,\n newAssets.map((asset) => asset.id),\n );\n };\n\n const onAttachmentDelete = async (assetToDelete: Asset) => {\n await deleteAssetIdsFromKey(attachmentKey, [assetToDelete.id]);\n };\n\n const onAttachmentReplace = async (attachmentToReplace: Asset, newAsset: Asset) => {\n const newAssetIds = attachments.map((attachment) =>\n attachment.id === attachmentToReplace.id ? newAsset.id : attachment.id,\n );\n\n await updateAssetIdsFromKey(attachmentKey, newAssetIds);\n };\n\n const onAttachmentsSorted = async (assets: Asset[]) => {\n const newAssetIds = assets.map((asset) => asset.id);\n\n await updateAssetIdsFromKey(attachmentKey, newAssetIds);\n };\n\n return {\n onAttachmentsAdd,\n onAttachmentDelete,\n onAttachmentReplace,\n onAttachmentsSorted,\n attachments,\n appBridge,\n };\n};\n\nconst AttachmentsContext = createContext<ReturnType<typeof useAttachments> | null>(null);\n\nexport const AttachmentsProvider = ({\n appBridge,\n children,\n assetId,\n}: {\n appBridge: AppBridgeBlock;\n children: ReactNode;\n assetId: string;\n}) => {\n const attachmentContext = useAttachments(appBridge, assetId);\n\n return <AttachmentsContext.Provider value={attachmentContext}>{children}</AttachmentsContext.Provider>;\n};\n\nexport const useAttachmentsContext = () => {\n const context = useContext(AttachmentsContext);\n\n if (!context) {\n throw new Error(\n \"No AttachmentsContext Provided. Component must be wrapped in an 'AttachmentsProvider' or the 'withAttachmentsProvider' HOC\",\n );\n }\n\n return context;\n};\n\n/**\n * Block-level HOC for cases when there is only one attachment asset field related to the block.\n * Recommended for most cases.\n * If finer control is required over attachments, use {@link AttachmentsProvider} component.\n */\nexport const withAttachmentsProvider = <T extends BlockProps>(Component: (props: T) => ReactNode, assetId: string) => {\n const wrappedComponent = (props: T) => (\n <AttachmentsProvider appBridge={props.appBridge} assetId={assetId}>\n <Component {...props} />\n </AttachmentsProvider>\n );\n\n wrappedComponent.displayName = 'withAttachmentsProvider';\n\n return wrappedComponent;\n};\n"],"names":["useAttachments","appBridge","attachmentKey","blockAssets","addAssetIdsToKey","deleteAssetIdsFromKey","updateAssetIdsFromKey","useBlockAssets","attachments","newAssets","asset","assetToDelete","attachmentToReplace","newAsset","newAssetIds","attachment","assets","AttachmentsContext","createContext","AttachmentsProvider","children","assetId","attachmentContext","useAttachmentsContext","context","useContext","withAttachmentsProvider","Component","wrappedComponent","props","jsx"],"mappings":";;;AAOa,MAAAA,IAAiB,CAACC,GAA2BC,MAA0B;AAChF,QAAM,EAAE,aAAAC,GAAa,kBAAAC,GAAkB,uBAAAC,GAAuB,uBAAAC,MAA0BC,EAAeN,CAAS,GAC1GO,KAAcL,KAAA,gBAAAA,EAAcD,OAAkB,CAAA;AA2B7C,SAAA;AAAA,IACH,kBA1BqB,OAAOO,MAAuB;AAC7C,YAAAL;AAAA,QACFF;AAAA,QACAO,EAAU,IAAI,CAACC,MAAUA,EAAM,EAAE;AAAA,MAAA;AAAA,IACrC;AAAA,IAuBA,oBApBuB,OAAOC,MAAyB;AACvD,YAAMN,EAAsBH,GAAe,CAACS,EAAc,EAAE,CAAC;AAAA,IAAA;AAAA,IAoB7D,qBAjBwB,OAAOC,GAA4BC,MAAoB;AAC/E,YAAMC,IAAcN,EAAY;AAAA,QAAI,CAACO,MACjCA,EAAW,OAAOH,EAAoB,KAAKC,EAAS,KAAKE,EAAW;AAAA,MAAA;AAGlE,YAAAT,EAAsBJ,GAAeY,CAAW;AAAA,IAAA;AAAA,IAatD,qBAVwB,OAAOE,MAAoB;AACnD,YAAMF,IAAcE,EAAO,IAAI,CAACN,MAAUA,EAAM,EAAE;AAE5C,YAAAJ,EAAsBJ,GAAeY,CAAW;AAAA,IAAA;AAAA,IAQtD,aAAAN;AAAA,IACA,WAAAP;AAAA,EAAA;AAER,GAEMgB,IAAqBC,EAAwD,IAAI,GAE1EC,IAAsB,CAAC;AAAA,EAChC,WAAAlB;AAAA,EACA,UAAAmB;AAAA,EACA,SAAAC;AACJ,MAIM;AACI,QAAAC,IAAoBtB,EAAeC,GAAWoB,CAAO;AAE3D,2BAAQJ,EAAmB,UAAnB,EAA4B,OAAOK,GAAoB,UAAAF,EAAS,CAAA;AAC5E,GAEaG,IAAwB,MAAM;AACjC,QAAAC,IAAUC,EAAWR,CAAkB;AAE7C,MAAI,CAACO;AACD,UAAM,IAAI;AAAA,MACN;AAAA,IAAA;AAID,SAAAA;AACX,GAOaE,IAA0B,CAAuBC,GAAoCN,MAAoB;AAClH,QAAMO,IAAmB,CAACC,MACtB,gBAAAC,EAACX,GAAoB,EAAA,WAAWU,EAAM,WAAW,SAAAR,GAC7C,UAAA,gBAAAS,EAACH,GAAW,EAAA,GAAGE,GAAO,EAC1B,CAAA;AAGJ,SAAAD,EAAiB,cAAc,2BAExBA;AACX;"}
1
+ {"version":3,"file":"useAttachments.es.js","sources":["../../src/hooks/useAttachments.tsx"],"sourcesContent":["/* (c) Copyright Frontify Ltd., all rights reserved. */\n\nimport { type AppBridgeBlock, type Asset, useBlockAssets } from '@frontify/app-bridge';\nimport { type ReactNode, createContext, useContext } from 'react';\n\nimport { type BlockProps } from '../index';\n\nexport const useAttachmentOperations = (attachmentKey: string, blockAssetBundle: ReturnType<typeof useBlockAssets>) => {\n const { blockAssets, addAssetIdsToKey, deleteAssetIdsFromKey, updateAssetIdsFromKey } = blockAssetBundle;\n const attachments = blockAssets?.[attachmentKey] || [];\n\n const onAttachmentsAdd = async (newAssets: Asset[]) => {\n await addAssetIdsToKey(\n attachmentKey,\n newAssets.map((asset) => asset.id),\n );\n };\n\n const onAttachmentDelete = async (assetToDelete: Asset) => {\n await deleteAssetIdsFromKey(attachmentKey, [assetToDelete.id]);\n };\n\n const onAttachmentReplace = async (attachmentToReplace: Asset, newAsset: Asset) => {\n const newAssetIds = attachments.map((attachment) =>\n attachment.id === attachmentToReplace.id ? newAsset.id : attachment.id,\n );\n\n await updateAssetIdsFromKey(attachmentKey, newAssetIds);\n };\n\n const onAttachmentsSorted = async (assets: Asset[]) => {\n const newAssetIds = assets.map((asset) => asset.id);\n\n await updateAssetIdsFromKey(attachmentKey, newAssetIds);\n };\n\n return {\n onAttachmentsAdd,\n onAttachmentDelete,\n onAttachmentReplace,\n onAttachmentsSorted,\n attachments,\n };\n};\n\nexport const useAttachments = (appBridge: AppBridgeBlock, attachmentKey: string) => {\n const { onAttachmentsAdd, onAttachmentDelete, onAttachmentReplace, onAttachmentsSorted, attachments } =\n useAttachmentOperations(attachmentKey, useBlockAssets(appBridge));\n\n return {\n onAttachmentsAdd,\n onAttachmentDelete,\n onAttachmentReplace,\n onAttachmentsSorted,\n attachments,\n appBridge,\n };\n};\n\nconst AttachmentsContext = createContext<ReturnType<typeof useAttachments> | null>(null);\n\nexport const AttachmentsProvider = ({\n appBridge,\n children,\n assetId,\n}: {\n appBridge: AppBridgeBlock;\n children: ReactNode;\n assetId: string;\n}) => {\n const attachmentContext = useAttachments(appBridge, assetId);\n\n return <AttachmentsContext.Provider value={attachmentContext}>{children}</AttachmentsContext.Provider>;\n};\n\nexport const useAttachmentsContext = () => {\n const context = useContext(AttachmentsContext);\n\n if (!context) {\n throw new Error(\n \"No AttachmentsContext Provided. Component must be wrapped in an 'AttachmentsProvider' or the 'withAttachmentsProvider' HOC\",\n );\n }\n\n return context;\n};\n\n/**\n * Block-level HOC for cases when there is only one attachment asset field related to the block.\n * Recommended for most cases.\n * If finer control is required over attachments, use {@link AttachmentsProvider} component.\n */\nexport const withAttachmentsProvider = <T extends BlockProps>(Component: (props: T) => ReactNode, assetId: string) => {\n const wrappedComponent = (props: T) => (\n <AttachmentsProvider appBridge={props.appBridge} assetId={assetId}>\n <Component {...props} />\n </AttachmentsProvider>\n );\n\n wrappedComponent.displayName = 'withAttachmentsProvider';\n\n return wrappedComponent;\n};\n"],"names":["useAttachmentOperations","attachmentKey","blockAssetBundle","blockAssets","addAssetIdsToKey","deleteAssetIdsFromKey","updateAssetIdsFromKey","attachments","newAssets","asset","assetToDelete","attachmentToReplace","newAsset","newAssetIds","attachment","assets","useAttachments","appBridge","onAttachmentsAdd","onAttachmentDelete","onAttachmentReplace","onAttachmentsSorted","useBlockAssets","AttachmentsContext","createContext","AttachmentsProvider","children","assetId","attachmentContext","useAttachmentsContext","context","useContext","withAttachmentsProvider","Component","wrappedComponent","props","jsx"],"mappings":";;;AAOa,MAAAA,IAA0B,CAACC,GAAuBC,MAAwD;AACnH,QAAM,EAAE,aAAAC,GAAa,kBAAAC,GAAkB,uBAAAC,GAAuB,uBAAAC,MAA0BJ,GAClFK,KAAcJ,KAAA,gBAAAA,EAAcF,OAAkB,CAAA;AA2B7C,SAAA;AAAA,IACH,kBA1BqB,OAAOO,MAAuB;AAC7C,YAAAJ;AAAA,QACFH;AAAA,QACAO,EAAU,IAAI,CAACC,MAAUA,EAAM,EAAE;AAAA,MAAA;AAAA,IACrC;AAAA,IAuBA,oBApBuB,OAAOC,MAAyB;AACvD,YAAML,EAAsBJ,GAAe,CAACS,EAAc,EAAE,CAAC;AAAA,IAAA;AAAA,IAoB7D,qBAjBwB,OAAOC,GAA4BC,MAAoB;AAC/E,YAAMC,IAAcN,EAAY;AAAA,QAAI,CAACO,MACjCA,EAAW,OAAOH,EAAoB,KAAKC,EAAS,KAAKE,EAAW;AAAA,MAAA;AAGlE,YAAAR,EAAsBL,GAAeY,CAAW;AAAA,IAAA;AAAA,IAatD,qBAVwB,OAAOE,MAAoB;AACnD,YAAMF,IAAcE,EAAO,IAAI,CAACN,MAAUA,EAAM,EAAE;AAE5C,YAAAH,EAAsBL,GAAeY,CAAW;AAAA,IAAA;AAAA,IAQtD,aAAAN;AAAA,EAAA;AAER,GAEaS,IAAiB,CAACC,GAA2BhB,MAA0B;AAC1E,QAAA,EAAE,kBAAAiB,GAAkB,oBAAAC,GAAoB,qBAAAC,GAAqB,qBAAAC,GAAqB,aAAAd,MACpFP,EAAwBC,GAAeqB,EAAeL,CAAS,CAAC;AAE7D,SAAA;AAAA,IACH,kBAAAC;AAAA,IACA,oBAAAC;AAAA,IACA,qBAAAC;AAAA,IACA,qBAAAC;AAAA,IACA,aAAAd;AAAA,IACA,WAAAU;AAAA,EAAA;AAER,GAEMM,IAAqBC,EAAwD,IAAI,GAE1EC,IAAsB,CAAC;AAAA,EAChC,WAAAR;AAAA,EACA,UAAAS;AAAA,EACA,SAAAC;AACJ,MAIM;AACI,QAAAC,IAAoBZ,EAAeC,GAAWU,CAAO;AAE3D,2BAAQJ,EAAmB,UAAnB,EAA4B,OAAOK,GAAoB,UAAAF,EAAS,CAAA;AAC5E,GAEaG,IAAwB,MAAM;AACjC,QAAAC,IAAUC,EAAWR,CAAkB;AAE7C,MAAI,CAACO;AACD,UAAM,IAAI;AAAA,MACN;AAAA,IAAA;AAID,SAAAA;AACX,GAOaE,IAA0B,CAAuBC,GAAoCN,MAAoB;AAClH,QAAMO,IAAmB,CAACC,MACtB,gBAAAC,EAACX,GAAoB,EAAA,WAAWU,EAAM,WAAW,SAAAR,GAC7C,UAAA,gBAAAS,EAACH,GAAW,EAAA,GAAGE,GAAO,EAC1B,CAAA;AAGJ,SAAAD,EAAiB,cAAc,2BAExBA;AACX;"}