@frontify/guideline-blocks-settings 0.27.0 → 0.28.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 (214) hide show
  1. package/.eslintrc.js +1 -1
  2. package/CHANGELOG.md +15 -0
  3. package/README.md +24 -5
  4. package/package.json +46 -10
  5. package/postcss.config.js +8 -0
  6. package/setupTests.ts +13 -0
  7. package/src/components/Attachments/AttachmentItem.tsx +257 -0
  8. package/src/components/Attachments/Attachments.spec.ct.tsx +151 -0
  9. package/src/components/Attachments/Attachments.tsx +221 -0
  10. package/src/components/Attachments/index.ts +4 -0
  11. package/src/components/Attachments/types.ts +30 -0
  12. package/src/components/BlockInjectButton/BlockInjectButton.spec.ct.tsx +48 -0
  13. package/src/components/BlockInjectButton/BlockInjectButton.tsx +212 -0
  14. package/src/components/BlockInjectButton/index.ts +4 -0
  15. package/src/components/BlockInjectButton/types.ts +18 -0
  16. package/src/components/BlockItemWrapper/BlockItemWrapper.spec.ct.tsx +146 -0
  17. package/src/components/BlockItemWrapper/BlockItemWrapper.tsx +76 -0
  18. package/src/components/BlockItemWrapper/Toolbar.tsx +128 -0
  19. package/src/components/BlockItemWrapper/constants.ts +4 -0
  20. package/src/components/BlockItemWrapper/index.ts +5 -0
  21. package/src/components/BlockItemWrapper/types.ts +46 -0
  22. package/src/components/DownloadButton/DownloadButton.spec.ct.tsx +20 -0
  23. package/src/components/DownloadButton/DownloadButton.tsx +36 -0
  24. package/src/components/DownloadButton/index.ts +3 -0
  25. package/src/components/DownloadButton/types.ts +5 -0
  26. package/src/components/RichTextEditor/RichTextEditor.spec.ct.tsx +204 -0
  27. package/src/components/RichTextEditor/RichTextEditor.tsx +62 -0
  28. package/src/components/RichTextEditor/SerializedText.tsx +25 -0
  29. package/src/components/RichTextEditor/constants.ts +3 -0
  30. package/src/components/RichTextEditor/index.ts +6 -0
  31. package/src/components/RichTextEditor/pluginPresets/defaultPluginsWithLinkChooser.tsx +53 -0
  32. package/src/components/RichTextEditor/pluginPresets/index.ts +3 -0
  33. package/src/components/RichTextEditor/plugins/ButtonPlugin/ButtonMarkupElement/ButtonMarkupElementNode.tsx +74 -0
  34. package/src/components/RichTextEditor/plugins/ButtonPlugin/ButtonMarkupElement/index.ts +11 -0
  35. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/ButtonButton.tsx +20 -0
  36. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/ButtonToolbarButton.tsx +56 -0
  37. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/FloatingButton/CustomFloatingButton.tsx +19 -0
  38. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/FloatingButton/EditButtonModal/EditModal.tsx +42 -0
  39. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/FloatingButton/FloatingButton.tsx +37 -0
  40. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/FloatingButton/FloatingButtonEditButton.tsx +22 -0
  41. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/FloatingButton/FloatingButtonUrlInput.tsx +30 -0
  42. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/FloatingButton/InsertButtonModal/InsertButtonModal.tsx +81 -0
  43. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/FloatingButton/InsertButtonModal/types.ts +13 -0
  44. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/FloatingButton/InsertButtonModal/useInsertModal.ts +143 -0
  45. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/FloatingButton/UnlinkButton.tsx +31 -0
  46. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/FloatingButton/floatingButtonStore.ts +46 -0
  47. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/FloatingButton/index.ts +12 -0
  48. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/FloatingButton/useFloatingButtonEdit.ts +113 -0
  49. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/FloatingButton/useFloatingButtonEnter.ts +21 -0
  50. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/FloatingButton/useFloatingButtonEscape.ts +30 -0
  51. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/FloatingButton/useFloatingButtonInsert.ts +71 -0
  52. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/FloatingButton/useVirtualFloatingButton.ts +22 -0
  53. package/src/components/RichTextEditor/plugins/ButtonPlugin/components/index.ts +3 -0
  54. package/src/components/RichTextEditor/plugins/ButtonPlugin/createButtonPlugin.ts +116 -0
  55. package/src/components/RichTextEditor/plugins/ButtonPlugin/index.ts +7 -0
  56. package/src/components/RichTextEditor/plugins/ButtonPlugin/transforms/index.ts +8 -0
  57. package/src/components/RichTextEditor/plugins/ButtonPlugin/transforms/insertButton.ts +17 -0
  58. package/src/components/RichTextEditor/plugins/ButtonPlugin/transforms/submitFloatingButton.ts +40 -0
  59. package/src/components/RichTextEditor/plugins/ButtonPlugin/transforms/unwrapButton.ts +68 -0
  60. package/src/components/RichTextEditor/plugins/ButtonPlugin/transforms/upsertButton.ts +198 -0
  61. package/src/components/RichTextEditor/plugins/ButtonPlugin/transforms/upsertButtonText.ts +40 -0
  62. package/src/components/RichTextEditor/plugins/ButtonPlugin/transforms/wrapButton.ts +30 -0
  63. package/src/components/RichTextEditor/plugins/ButtonPlugin/types.ts +13 -0
  64. package/src/components/RichTextEditor/plugins/ButtonPlugin/utils/createButtonNode.ts +28 -0
  65. package/src/components/RichTextEditor/plugins/ButtonPlugin/utils/getButtonStyle.ts +14 -0
  66. package/src/components/RichTextEditor/plugins/ButtonPlugin/utils/getUrl.ts +18 -0
  67. package/src/components/RichTextEditor/plugins/ButtonPlugin/utils/index.ts +8 -0
  68. package/src/components/RichTextEditor/plugins/ButtonPlugin/utils/styles.ts +77 -0
  69. package/src/components/RichTextEditor/plugins/ButtonPlugin/utils/triggerFloatingButton.ts +23 -0
  70. package/src/components/RichTextEditor/plugins/ButtonPlugin/utils/triggerFloatingButtonEdit.ts +30 -0
  71. package/src/components/RichTextEditor/plugins/ButtonPlugin/utils/triggerFloatingButtonInsert.ts +45 -0
  72. package/src/components/RichTextEditor/plugins/ButtonPlugin/withButton.ts +106 -0
  73. package/src/components/RichTextEditor/plugins/LinkPlugin/FloatingLink/CustomFloatingLink.tsx +26 -0
  74. package/src/components/RichTextEditor/plugins/LinkPlugin/FloatingLink/EditLinkModal/EditModal.tsx +43 -0
  75. package/src/components/RichTextEditor/plugins/LinkPlugin/FloatingLink/EditLinkModal/index.ts +4 -0
  76. package/src/components/RichTextEditor/plugins/LinkPlugin/FloatingLink/EditLinkModal/useFloatingLinkEdit.ts +113 -0
  77. package/src/components/RichTextEditor/plugins/LinkPlugin/FloatingLink/FloatingLink.tsx +45 -0
  78. package/src/components/RichTextEditor/plugins/LinkPlugin/FloatingLink/InsertLinkModal/InsertLinkModal.tsx +5 -0
  79. package/src/components/RichTextEditor/plugins/LinkPlugin/FloatingLink/InsertLinkModal/InsertModal.tsx +105 -0
  80. package/src/components/RichTextEditor/plugins/LinkPlugin/FloatingLink/InsertLinkModal/index.ts +4 -0
  81. package/src/components/RichTextEditor/plugins/LinkPlugin/FloatingLink/InsertLinkModal/types.ts +16 -0
  82. package/src/components/RichTextEditor/plugins/LinkPlugin/FloatingLink/InsertLinkModal/useFloatingLinkInsert.ts +73 -0
  83. package/src/components/RichTextEditor/plugins/LinkPlugin/FloatingLink/InsertLinkModal/useInsertModal.ts +136 -0
  84. package/src/components/RichTextEditor/plugins/LinkPlugin/LinkButton.tsx +38 -0
  85. package/src/components/RichTextEditor/plugins/LinkPlugin/LinkMarkupElement/LinkMarkupElementNode.tsx +36 -0
  86. package/src/components/RichTextEditor/plugins/LinkPlugin/LinkMarkupElement/index.ts +11 -0
  87. package/src/components/RichTextEditor/plugins/LinkPlugin/id.ts +3 -0
  88. package/src/components/RichTextEditor/plugins/LinkPlugin/index.ts +48 -0
  89. package/src/components/RichTextEditor/plugins/LinkPlugin/types.ts +12 -0
  90. package/src/components/RichTextEditor/plugins/LinkPlugin/utils/getUrl.ts +30 -0
  91. package/src/components/RichTextEditor/plugins/LinkPlugin/utils/index.ts +4 -0
  92. package/src/components/RichTextEditor/plugins/LinkPlugin/utils/relativeUrlRegex.spec.ts +35 -0
  93. package/src/components/RichTextEditor/plugins/LinkPlugin/utils/relativeUrlRegex.ts +3 -0
  94. package/src/components/RichTextEditor/plugins/LinkPlugin/utils/url.spec.ts +75 -0
  95. package/src/components/RichTextEditor/plugins/LinkPlugin/utils/url.ts +21 -0
  96. package/src/components/RichTextEditor/plugins/TextStylePlugins/custom1Plugin.tsx +61 -0
  97. package/src/components/RichTextEditor/plugins/TextStylePlugins/custom2Plugin.tsx +61 -0
  98. package/src/components/RichTextEditor/plugins/TextStylePlugins/custom3Plugin.tsx +62 -0
  99. package/src/components/RichTextEditor/plugins/TextStylePlugins/heading1Plugin.tsx +61 -0
  100. package/src/components/RichTextEditor/plugins/TextStylePlugins/heading2Plugin.tsx +58 -0
  101. package/src/components/RichTextEditor/plugins/TextStylePlugins/heading3Plugin.tsx +58 -0
  102. package/src/components/RichTextEditor/plugins/TextStylePlugins/heading4Plugin.tsx +59 -0
  103. package/src/components/RichTextEditor/plugins/TextStylePlugins/helpers.tsx +44 -0
  104. package/src/components/RichTextEditor/plugins/TextStylePlugins/imageCaptionPlugin.tsx +61 -0
  105. package/src/components/RichTextEditor/plugins/TextStylePlugins/imageTitlePlugin.tsx +61 -0
  106. package/src/components/RichTextEditor/plugins/TextStylePlugins/index.ts +15 -0
  107. package/src/components/RichTextEditor/plugins/TextStylePlugins/paragraphPlugin.tsx +58 -0
  108. package/src/components/RichTextEditor/plugins/TextStylePlugins/quotePlugin.tsx +62 -0
  109. package/src/components/RichTextEditor/plugins/index.ts +6 -0
  110. package/src/components/RichTextEditor/plugins/shared/LinkSelector/DocumentLink.tsx +80 -0
  111. package/src/components/RichTextEditor/plugins/shared/LinkSelector/DocumentLinks.tsx +97 -0
  112. package/src/components/RichTextEditor/plugins/shared/LinkSelector/LinkSelector.spec.ct.tsx +138 -0
  113. package/src/components/RichTextEditor/plugins/shared/LinkSelector/LinkSelector.tsx +80 -0
  114. package/src/components/RichTextEditor/plugins/shared/LinkSelector/PageLink.tsx +83 -0
  115. package/src/components/RichTextEditor/plugins/shared/LinkSelector/PageLinks.tsx +68 -0
  116. package/src/components/RichTextEditor/plugins/shared/LinkSelector/SectionLink.tsx +37 -0
  117. package/src/components/RichTextEditor/plugins/shared/LinkSelector/index.ts +3 -0
  118. package/src/components/RichTextEditor/plugins/styles.ts +179 -0
  119. package/src/components/RichTextEditor/serializer/index.ts +3 -0
  120. package/src/components/RichTextEditor/serializer/nodes/button.ts +25 -0
  121. package/src/components/RichTextEditor/serializer/nodes/checkItemNode.ts +29 -0
  122. package/src/components/RichTextEditor/serializer/nodes/default.ts +52 -0
  123. package/src/components/RichTextEditor/serializer/nodes/link.ts +25 -0
  124. package/src/components/RichTextEditor/serializer/nodes/mentionHtmlNode.ts +17 -0
  125. package/src/components/RichTextEditor/serializer/serializeNodesToHtmlRecursive.ts +134 -0
  126. package/src/components/RichTextEditor/serializer/serializeToHtml.ts +49 -0
  127. package/src/components/RichTextEditor/serializer/utlis/reactCssPropsToCss.ts +21 -0
  128. package/src/components/RichTextEditor/serializer/utlis/serializeLeafToHtml.ts +32 -0
  129. package/src/components/RichTextEditor/types.ts +23 -0
  130. package/src/components/index.ts +7 -0
  131. package/src/helpers/addHttps.spec.ts +42 -0
  132. package/src/helpers/addHttps.ts +15 -0
  133. package/src/helpers/convertToRichTextValue.spec.ts +32 -0
  134. package/src/helpers/convertToRichTextValue.ts +6 -0
  135. package/src/helpers/customCoordinatesGetterFactory.spec.ts +69 -0
  136. package/src/helpers/customCoordinatesGetterFactory.ts +39 -0
  137. package/src/helpers/hasRichTextValue.spec.ts +63 -0
  138. package/src/helpers/hasRichTextValue.ts +29 -0
  139. package/src/helpers/index.ts +8 -0
  140. package/src/helpers/isDownloadable.spec.ts +47 -0
  141. package/src/helpers/isDownloadable.ts +7 -0
  142. package/src/helpers/mapColorPalettes.spec.ts +146 -0
  143. package/src/helpers/mapColorPalettes.ts +22 -0
  144. package/src/hooks/index.ts +4 -0
  145. package/src/hooks/useAttachments.spec.ts +79 -0
  146. package/src/hooks/useAttachments.ts +46 -0
  147. package/src/hooks/useDndSensors.spec.ts +40 -0
  148. package/src/hooks/useDndSensors.ts +23 -0
  149. package/src/index.ts +8 -0
  150. package/src/settings/background.spec.ts +173 -0
  151. package/src/settings/background.ts +49 -0
  152. package/src/settings/border.spec.ts +76 -0
  153. package/src/settings/border.ts +90 -0
  154. package/src/settings/borderRadius.spec.ts +30 -0
  155. package/src/settings/borderRadius.ts +73 -0
  156. package/src/settings/borderRadiusExtended.spec.ts +52 -0
  157. package/src/settings/borderRadiusExtended.ts +84 -0
  158. package/src/settings/defaultValues.ts +21 -0
  159. package/src/settings/gutter.spec.ts +60 -0
  160. package/src/settings/gutter.ts +75 -0
  161. package/src/settings/index.ts +14 -0
  162. package/src/settings/margin.spec.ts +42 -0
  163. package/src/settings/margin.ts +72 -0
  164. package/src/settings/marginExtended.spec.ts +45 -0
  165. package/src/settings/marginExtended.ts +91 -0
  166. package/src/settings/padding.spec.ts +42 -0
  167. package/src/settings/padding.ts +73 -0
  168. package/src/settings/paddingExtended.spec.ts +45 -0
  169. package/src/settings/paddingExtended.ts +91 -0
  170. package/src/settings/security.spec.ts +87 -0
  171. package/src/settings/security.ts +61 -0
  172. package/src/settings/securityDownloadable.spec.ts +46 -0
  173. package/src/settings/securityDownloadable.ts +33 -0
  174. package/src/settings/securityGlobalControl.ts +42 -0
  175. package/src/settings/types.ts +128 -0
  176. package/src/utilities/color/getReadableColor.spec.ts +32 -0
  177. package/src/utilities/color/getReadableColor.ts +34 -0
  178. package/src/utilities/color/index.ts +10 -0
  179. package/src/utilities/color/isDark.spec.ts +33 -0
  180. package/src/utilities/color/isDark.ts +29 -0
  181. package/src/utilities/color/setAlpha.spec.ts +28 -0
  182. package/src/utilities/color/setAlpha.ts +14 -0
  183. package/src/utilities/color/toColorObject.spec.ts +19 -0
  184. package/src/utilities/color/toColorObject.ts +16 -0
  185. package/src/utilities/color/toHex8String.spec.ts +17 -0
  186. package/src/utilities/color/toHex8String.ts +14 -0
  187. package/src/utilities/color/toHexString.spec.ts +17 -0
  188. package/src/utilities/color/toHexString.ts +10 -0
  189. package/src/utilities/color/toRgbaString.spec.ts +12 -0
  190. package/src/utilities/color/toRgbaString.ts +14 -0
  191. package/src/utilities/color/toShortRgba.spec.ts +16 -0
  192. package/src/utilities/color/toShortRgba.ts +35 -0
  193. package/src/utilities/index.ts +5 -0
  194. package/src/utilities/moveItemInArray.spec.ts +17 -0
  195. package/src/utilities/moveItemInArray.ts +21 -0
  196. package/src/utilities/react/getBackgroundColorStyles.spec.ts +18 -0
  197. package/src/utilities/react/getBackgroundColorStyles.ts +11 -0
  198. package/src/utilities/react/getBorderStyles.spec.ts +39 -0
  199. package/src/utilities/react/getBorderStyles.ts +21 -0
  200. package/src/utilities/react/getRadiusStyles.spec.ts +25 -0
  201. package/src/utilities/react/getRadiusStyles.ts +8 -0
  202. package/src/utilities/react/index.ts +6 -0
  203. package/src/utilities/react/joinClassNames.spec.ts +18 -0
  204. package/src/utilities/react/joinClassNames.ts +10 -0
  205. package/tailwind.config.js +27 -0
  206. package/tsconfig.json +3 -1
  207. package/vite.config.ts +11 -1
  208. package/dist/index.cjs.js +0 -2
  209. package/dist/index.cjs.js.map +0 -1
  210. package/dist/index.d.ts +0 -147
  211. package/dist/index.es.js +0 -9
  212. package/dist/index.es.js.map +0 -1
  213. package/dist/index.umd.js +0 -2
  214. package/dist/index.umd.js.map +0 -1
@@ -0,0 +1,221 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import { useEffect, useState } from 'react';
4
+ import {
5
+ DndContext,
6
+ DragEndEvent,
7
+ DragOverlay,
8
+ DragStartEvent,
9
+ KeyboardSensor,
10
+ PointerSensor,
11
+ closestCenter,
12
+ useSensor,
13
+ useSensors,
14
+ } from '@dnd-kit/core';
15
+ import { SortableContext, arrayMove, rectSortingStrategy } from '@dnd-kit/sortable';
16
+ import { Asset, useAssetUpload, useEditorState } from '@frontify/app-bridge';
17
+ import {
18
+ AssetInput,
19
+ AssetInputSize,
20
+ Flyout,
21
+ FlyoutPlacement,
22
+ IconCaretDown12,
23
+ IconPaperclip16,
24
+ Tooltip,
25
+ TooltipPosition,
26
+ } from '@frontify/fondue';
27
+ import { AttachmentItem, SortableAttachmentItem } from './AttachmentItem';
28
+ import { AttachmentsProps } from './types';
29
+ import { restrictToWindowEdges } from '@dnd-kit/modifiers';
30
+
31
+ export const Attachments = ({
32
+ items = [],
33
+ onDelete,
34
+ onReplaceWithBrowse,
35
+ onReplaceWithUpload,
36
+ onBrowse,
37
+ onUpload,
38
+ onSorted,
39
+ appBridge,
40
+ }: AttachmentsProps) => {
41
+ const [internalItems, setInternalItems] = useState<Asset[]>(items);
42
+ const [isFlyoutOpen, setIsFlyoutOpen] = useState(false);
43
+ const sensors = useSensors(useSensor(PointerSensor), useSensor(KeyboardSensor));
44
+ const [draggedAssetId, setDraggedAssetId] = useState<number | undefined>(undefined);
45
+ const [isUploadLoading, setIsUploadLoading] = useState(false);
46
+ const [assetIdsLoading, setAssetIdsLoading] = useState<number[]>([]);
47
+ const [selectedFiles, setSelectedFiles] = useState<FileList | null>(null);
48
+ const isEditing = useEditorState(appBridge);
49
+
50
+ const draggedItem = internalItems?.find((item) => item.id === draggedAssetId);
51
+
52
+ const [uploadFile, { results: uploadResults, doneAll }] = useAssetUpload({
53
+ onUploadProgress: () => !isUploadLoading && setIsUploadLoading(true),
54
+ });
55
+
56
+ useEffect(() => {
57
+ setInternalItems(items);
58
+ }, [items]);
59
+
60
+ useEffect(() => {
61
+ if (selectedFiles) {
62
+ setIsUploadLoading(true);
63
+ uploadFile(selectedFiles);
64
+ }
65
+ // eslint-disable-next-line react-hooks/exhaustive-deps
66
+ }, [selectedFiles]);
67
+
68
+ useEffect(() => {
69
+ const uploadDone = async () => {
70
+ if (doneAll) {
71
+ await onUpload(uploadResults);
72
+ setIsUploadLoading(false);
73
+ }
74
+ };
75
+ uploadDone();
76
+ // eslint-disable-next-line react-hooks/exhaustive-deps
77
+ }, [doneAll, uploadResults]);
78
+
79
+ const onOpenAssetChooser = () => {
80
+ setIsFlyoutOpen(false);
81
+ appBridge.openAssetChooser(
82
+ (result: Asset[]) => {
83
+ onBrowse(result);
84
+ appBridge.closeAssetChooser();
85
+ setIsFlyoutOpen(true);
86
+ },
87
+ {
88
+ multiSelection: true,
89
+ selectedValueIds: internalItems.map((internalItem) => internalItem.id),
90
+ }
91
+ );
92
+ };
93
+
94
+ const onReplaceItemWithBrowse = (toReplace: Asset) => {
95
+ setIsFlyoutOpen(false);
96
+ appBridge.openAssetChooser(
97
+ async (result: Asset[]) => {
98
+ setIsFlyoutOpen(true);
99
+ appBridge.closeAssetChooser();
100
+ setAssetIdsLoading([...assetIdsLoading, toReplace.id]);
101
+ await onReplaceWithBrowse(toReplace, result[0]);
102
+ setAssetIdsLoading(assetIdsLoading.filter((id) => id !== toReplace.id));
103
+ },
104
+ {
105
+ multiSelection: false,
106
+ selectedValueIds: internalItems.map((internalItem) => internalItem.id),
107
+ }
108
+ );
109
+ };
110
+
111
+ const onReplaceItemWithUpload = async (toReplace: Asset, uploadedAsset: Asset) => {
112
+ setAssetIdsLoading([...assetIdsLoading, toReplace.id]);
113
+ await onReplaceWithUpload(toReplace, uploadedAsset);
114
+ setAssetIdsLoading(assetIdsLoading.filter((id) => id !== toReplace.id));
115
+ };
116
+
117
+ const handleDragStart = (event: DragStartEvent) => {
118
+ const { active } = event;
119
+ setDraggedAssetId(active.id as number);
120
+ };
121
+
122
+ const handleDragEnd = (event: DragEndEvent) => {
123
+ const { active, over } = event;
124
+ if (over && active.id !== over.id && internalItems) {
125
+ const oldIndex = internalItems.findIndex((i) => i.id === active.id);
126
+ const newIndex = internalItems.findIndex((i) => i.id === over.id);
127
+ const sortedItems = arrayMove(internalItems, oldIndex, newIndex);
128
+ setInternalItems(sortedItems);
129
+ onSorted(sortedItems);
130
+ }
131
+ setDraggedAssetId(undefined);
132
+ };
133
+
134
+ return isEditing || (internalItems?.length ?? 0) > 0 ? (
135
+ <Tooltip
136
+ withArrow
137
+ position={TooltipPosition.Top}
138
+ content="Attachments"
139
+ disabled={isFlyoutOpen}
140
+ enterDelay={500}
141
+ triggerElement={
142
+ <div data-test-id="attachments-flyout-button">
143
+ <Flyout
144
+ placement={FlyoutPlacement.BottomRight}
145
+ onOpenChange={(isOpen) => setIsFlyoutOpen(!!draggedItem ? true : isOpen)}
146
+ isOpen={isFlyoutOpen}
147
+ hug={false}
148
+ fitContent
149
+ legacyFooter={false}
150
+ trigger={
151
+ <div className="tw-flex tw-text-[13px] tw-font-body tw-items-center tw-gap-1 tw-rounded-full tw-bg-box-neutral-strong-inverse hover:tw-bg-box-neutral-strong-inverse-hover active:tw-bg-box-neutral-strong-inverse-pressed tw-text-box-neutral-strong tw-outline tw-outline-1 tw-outline-offset-[1px] tw-p-[6px] tw-outline-line">
152
+ <IconPaperclip16 />
153
+ <div>{items.length > 0 ? items.length : 'Add'}</div>
154
+ <IconCaretDown12 />
155
+ </div>
156
+ }
157
+ >
158
+ <div className="tw-w-[300px]">
159
+ {internalItems.length > 0 && (
160
+ <DndContext
161
+ sensors={sensors}
162
+ collisionDetection={closestCenter}
163
+ onDragStart={handleDragStart}
164
+ onDragEnd={handleDragEnd}
165
+ modifiers={[restrictToWindowEdges]}
166
+ >
167
+ <SortableContext items={internalItems} strategy={rectSortingStrategy}>
168
+ <div className="tw-border-b tw-border-b-line">
169
+ {internalItems.map((item) => (
170
+ <SortableAttachmentItem
171
+ isEditing={isEditing}
172
+ isLoading={assetIdsLoading.includes(item.id)}
173
+ key={item.id}
174
+ item={item}
175
+ onDelete={() => onDelete(item)}
176
+ onReplaceWithBrowse={() => onReplaceItemWithBrowse(item)}
177
+ onReplaceWithUpload={(uploadedAsset: Asset) =>
178
+ onReplaceItemWithUpload(item, uploadedAsset)
179
+ }
180
+ />
181
+ ))}
182
+ </div>
183
+ </SortableContext>
184
+ <DragOverlay>
185
+ {draggedItem && (
186
+ <AttachmentItem
187
+ isOverlay={true}
188
+ isEditing={isEditing}
189
+ key={draggedAssetId}
190
+ item={draggedItem}
191
+ isDragging={true}
192
+ onDelete={() => onDelete(draggedItem)}
193
+ onReplaceWithBrowse={() => onReplaceItemWithBrowse(draggedItem)}
194
+ onReplaceWithUpload={(uploadedAsset: Asset) =>
195
+ onReplaceItemWithUpload(draggedItem, uploadedAsset)
196
+ }
197
+ />
198
+ )}
199
+ </DragOverlay>
200
+ </DndContext>
201
+ )}
202
+ {isEditing && (
203
+ <div className="tw-px-5 tw-py-3">
204
+ <div className="tw-font-body tw-font-medium tw-text-text tw-text-s tw-my-4">
205
+ Add attachments
206
+ </div>
207
+ <AssetInput
208
+ isLoading={isUploadLoading}
209
+ size={AssetInputSize.Small}
210
+ onUploadClick={(fileList) => setSelectedFiles(fileList)}
211
+ onLibraryClick={onOpenAssetChooser}
212
+ />
213
+ </div>
214
+ )}
215
+ </div>
216
+ </Flyout>
217
+ </div>
218
+ }
219
+ />
220
+ ) : null;
221
+ };
@@ -0,0 +1,4 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ export * from './Attachments';
4
+ export * from './types';
@@ -0,0 +1,30 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import { AppBridgeBlock, Asset } from '@frontify/app-bridge';
4
+
5
+ export type AttachmentsProps = {
6
+ items?: Asset[];
7
+ appBridge: AppBridgeBlock;
8
+ onReplaceWithUpload: (attachmentToReplace: Asset, newAsset: Asset) => Promise<void>;
9
+ onReplaceWithBrowse: (attachmentToReplace: Asset, newAsset: Asset) => Promise<void>;
10
+ onDelete: (attachmentToDelete: Asset) => void;
11
+ onUpload: (uploadedAttachments: Asset[]) => Promise<void>;
12
+ onBrowse: (browserAttachments: Asset[]) => void;
13
+ onSorted: (sortedAttachments: Asset[]) => void;
14
+ };
15
+
16
+ export type AttachmentItemProps = SortableAttachmentItemProps & {
17
+ isDragging?: boolean;
18
+ transformStyle?: Record<string, unknown>;
19
+ draggableProps?: Record<string, unknown>;
20
+ isOverlay?: boolean;
21
+ };
22
+
23
+ export type SortableAttachmentItemProps = {
24
+ item: Asset;
25
+ isEditing: boolean;
26
+ onDelete: () => void;
27
+ isLoading?: boolean;
28
+ onReplaceWithBrowse: () => void;
29
+ onReplaceWithUpload: (uploadedAsset: Asset) => void;
30
+ };
@@ -0,0 +1,48 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import { withAppBridgeBlockStubs } from '@frontify/app-bridge';
4
+ import { mount } from 'cypress/react18';
5
+ import { BlockInjectButton } from './BlockInjectButton';
6
+ import { IconAdobeCreativeCloud } from '@frontify/fondue';
7
+
8
+ const BlockInjectButtonSelector = '[data-test-id="block-inject-button"]';
9
+ const ActionMenuItemSelector = '[data-test-id="menu-item"]';
10
+
11
+ describe('Block Inject Button', () => {
12
+ it('renders a simple block inject button', () => {
13
+ const [BlockInjectButtonWithStubs] = withAppBridgeBlockStubs(BlockInjectButton, {});
14
+
15
+ mount(
16
+ <BlockInjectButtonWithStubs
17
+ label="label"
18
+ icon={<IconAdobeCreativeCloud />}
19
+ secondaryLabel="second label"
20
+ withMenu={false}
21
+ />
22
+ );
23
+ cy.get(BlockInjectButtonSelector)
24
+ .should('exist')
25
+ .should('have.class', 'first:tw-rounded-tl last:tw-rounded-br', 'first:tw-rounded-bl last:tw-rounded-tr');
26
+ });
27
+
28
+ it('renders a block inject button with Menu upload and asset', () => {
29
+ const [BlockInjectButtonWithStubs] = withAppBridgeBlockStubs(BlockInjectButton, {});
30
+
31
+ mount(
32
+ <BlockInjectButtonWithStubs
33
+ label="label"
34
+ secondaryLabel="second label"
35
+ onAssetChooseClick={cy.stub().as('onClickOpenAssetChooser')}
36
+ onUploadClick={cy.stub().as('onClickonUploadClick')}
37
+ />
38
+ );
39
+ cy.get(BlockInjectButtonSelector).click();
40
+ cy.get(ActionMenuItemSelector).should('have.length', 2);
41
+ cy.get(ActionMenuItemSelector).eq(0).should('have.text', 'Upload asset').click();
42
+ cy.get('@onClickonUploadClick').should('have.been.calledOnce');
43
+
44
+ cy.get(BlockInjectButtonSelector).click();
45
+ cy.get(ActionMenuItemSelector).eq(1).should('have.text', 'Browse asset').click();
46
+ cy.get('@onClickOpenAssetChooser').should('have.been.calledOnce');
47
+ });
48
+ });
@@ -0,0 +1,212 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import {
4
+ ActionMenu,
5
+ Flyout,
6
+ IconArrowCircleUp20,
7
+ IconExclamationMarkTriangle,
8
+ IconImageStack20,
9
+ LoadingCircle,
10
+ MenuItemContentSize,
11
+ } from '@frontify/fondue';
12
+ import { DragEventHandler, MouseEventHandler, useRef, useState } from 'react';
13
+ import { joinClassNames } from '../../utilities/react/joinClassNames';
14
+ import { BlockInjectButtonProps } from './types';
15
+ import { FileExtension, FileExtensionSets } from '@frontify/app-bridge';
16
+
17
+ export const BlockInjectButton = ({
18
+ onDrop,
19
+ label,
20
+ icon,
21
+ secondaryLabel,
22
+ isLoading,
23
+ fillParentContainer,
24
+ onAssetChooseClick,
25
+ onUploadClick,
26
+ withMenu = true,
27
+ onClick,
28
+ validFileType,
29
+ verticalLayout,
30
+ }: BlockInjectButtonProps) => {
31
+ const [isDraggingOver, setIsDraggingOver] = useState(false);
32
+ const [menuPosition, setMenuPosition] = useState<[number, number] | undefined>();
33
+ const buttonRef = useRef<HTMLButtonElement>(null);
34
+ const [errorMsg, setErrorMsg] = useState<string | undefined>(undefined);
35
+
36
+ const handleDrop: DragEventHandler<HTMLButtonElement> = (event) => {
37
+ event.preventDefault();
38
+ setIsDraggingOver(false);
39
+ if (!isValidAsset(event.dataTransfer.files)) {
40
+ setErrorMsg('Invalid');
41
+ setTimeout(() => {
42
+ setErrorMsg(undefined);
43
+ }, 1000);
44
+ return;
45
+ }
46
+ onDrop?.(event.dataTransfer.files);
47
+ };
48
+
49
+ const isValidAsset = (files: FileList) => {
50
+ if (!validFileType) {
51
+ return true;
52
+ }
53
+ for (let i = 0; i < files.length; i++) {
54
+ const droppedFileExtension = (files[i].name.split('.').pop() ?? '') as FileExtension;
55
+ if (!FileExtensionSets[validFileType].includes(droppedFileExtension)) {
56
+ return false;
57
+ }
58
+ }
59
+ return true;
60
+ };
61
+
62
+ const openMenu: MouseEventHandler<HTMLButtonElement> = (event) => {
63
+ if (!buttonRef.current || isLoading) {
64
+ return;
65
+ }
66
+ const { left, top } = buttonRef.current.getBoundingClientRect();
67
+ const XInsideComponent = event.clientX - left;
68
+ const YInsideComponent = event.clientY - top;
69
+ setMenuPosition([XInsideComponent, YInsideComponent]);
70
+ };
71
+
72
+ return (
73
+ <button
74
+ ref={buttonRef}
75
+ data-test-id="block-inject-button"
76
+ className={joinClassNames([
77
+ 'tw-font-body tw-relative tw-text-sm tw-leading-4 tw-border tw-flex tw-items-center tw-justify-center tw-cursor-pointer tw-gap-3 tw-w-full first:tw-rounded-tl last:tw-rounded-br',
78
+ verticalLayout
79
+ ? '[&:not(:first-child)]:tw-border-t-0 first:tw-rounded-tr last:tw-rounded-bl'
80
+ : '[&:not(:first-child)]:tw-border-l-0 first:tw-rounded-bl last:tw-rounded-tr',
81
+ fillParentContainer ? 'tw-h-full' : 'tw-h-[72px]',
82
+ isDraggingOver && !isLoading ? 'tw-border-dashed' : 'tw-border-solid',
83
+ menuPosition && 'tw-bg-blank-state-pressed-inverse',
84
+ isDraggingOver && 'tw-bg-blank-state-weak-inverse',
85
+ errorMsg ? '!tw-border-red-50 !tw-cursor-not-allowed' : ' tw-border-blank-state-line',
86
+ isLoading || menuPosition || isDraggingOver || errorMsg
87
+ ? ''
88
+ : 'tw-text-text-weak hover:tw-text-blank-state-hover hover:tw-bg-blank-state-hover-inverse hover:tw-border-blank-state-line-hover active:tw-text-blank-state-pressed active:tw-bg-blank-state-pressed-inverse active:tw-border-blank-state-line-hover',
89
+ (isDraggingOver || !!menuPosition) && !errorMsg
90
+ ? '[&>*]:tw-pointer-events-none tw-border-blank-state-line-hover'
91
+ : 'tw-bg-blank-state-shaded-inverse tw-text-blank-state-shaded',
92
+ ])}
93
+ onDragEnter={
94
+ onDrop
95
+ ? (event) => {
96
+ setIsDraggingOver(true);
97
+ // is now only used for images, can be adapted if needed
98
+ if (validFileType === 'Images') {
99
+ for (const item of Array.from(event.dataTransfer.items)) {
100
+ if (!item?.type?.startsWith('image/')) {
101
+ setErrorMsg('Invalid');
102
+ } else {
103
+ setErrorMsg(undefined);
104
+ }
105
+ }
106
+ }
107
+ }
108
+ : undefined
109
+ }
110
+ onDragLeave={
111
+ onDrop
112
+ ? () => {
113
+ setIsDraggingOver(false);
114
+ setErrorMsg(undefined);
115
+ }
116
+ : undefined
117
+ }
118
+ onDrop={onDrop ? handleDrop : undefined}
119
+ onClick={(event) => {
120
+ withMenu && openMenu(event);
121
+ onClick?.();
122
+ }}
123
+ >
124
+ {isLoading ? (
125
+ <LoadingCircle />
126
+ ) : // eslint-disable-next-line unicorn/no-nested-ternary
127
+ errorMsg ? (
128
+ <div className=" tw-flex tw-items-center tw-justify-center tw-text-red-60 tw-font-medium">
129
+ <IconExclamationMarkTriangle />
130
+ {errorMsg}
131
+ </div>
132
+ ) : (
133
+ <>
134
+ {icon && <div>{icon}</div>}
135
+ {(label || secondaryLabel) && (
136
+ <div className="tw-flex tw-flex-col tw-items-start">
137
+ {label && <div className="tw-font-medium">{label}</div>}
138
+ {secondaryLabel && <div className="tw-font-normal">{secondaryLabel}</div>}
139
+ </div>
140
+ )}
141
+ </>
142
+ )}
143
+ {menuPosition && (
144
+ <div
145
+ className="tw-absolute tw-left-0 tw-top-full tw-z-20"
146
+ style={{
147
+ left: menuPosition[0],
148
+ top: menuPosition[1],
149
+ }}
150
+ >
151
+ <Flyout
152
+ onOpenChange={(isOpen) => !isOpen && setMenuPosition(undefined)}
153
+ isOpen={true}
154
+ fitContent
155
+ hug={false}
156
+ legacyFooter={false}
157
+ trigger={<div />}
158
+ >
159
+ <ActionMenu
160
+ menuBlocks={[
161
+ {
162
+ id: 'menu',
163
+ menuItems: [
164
+ ...(onUploadClick
165
+ ? [
166
+ {
167
+ id: 'upload',
168
+ size: MenuItemContentSize.XSmall,
169
+ title: 'Upload asset',
170
+ onClick: () => {
171
+ onUploadClick();
172
+ setMenuPosition(undefined);
173
+ },
174
+
175
+ initialValue: true,
176
+ decorator: (
177
+ <div className="tw-mr-2">
178
+ <IconArrowCircleUp20 />
179
+ </div>
180
+ ),
181
+ },
182
+ ]
183
+ : []),
184
+ ...(onAssetChooseClick
185
+ ? [
186
+ {
187
+ id: 'asset',
188
+ size: MenuItemContentSize.XSmall,
189
+ title: 'Browse asset',
190
+ onClick: () => {
191
+ onAssetChooseClick();
192
+ setMenuPosition(undefined);
193
+ },
194
+ initialValue: true,
195
+ decorator: (
196
+ <div className="tw-mr-2">
197
+ <IconImageStack20 />
198
+ </div>
199
+ ),
200
+ },
201
+ ]
202
+ : []),
203
+ ],
204
+ },
205
+ ]}
206
+ />
207
+ </Flyout>
208
+ </div>
209
+ )}
210
+ </button>
211
+ );
212
+ };
@@ -0,0 +1,4 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ export * from './BlockInjectButton';
4
+ export * from './types';
@@ -0,0 +1,18 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import type { FileType } from '@frontify/app-bridge';
4
+
5
+ export type BlockInjectButtonProps = {
6
+ isLoading?: boolean;
7
+ label?: string;
8
+ validFileType?: keyof typeof FileType;
9
+ secondaryLabel?: string;
10
+ icon?: JSX.Element;
11
+ onDrop?: (files: FileList) => void;
12
+ fillParentContainer?: boolean;
13
+ onUploadClick?: () => void;
14
+ onAssetChooseClick?: () => void;
15
+ withMenu?: boolean;
16
+ onClick?: () => void;
17
+ verticalLayout?: boolean;
18
+ };