@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
package/.eslintrc.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /* (c) Copyright Frontify Ltd., all rights reserved. */
2
2
 
3
3
  module.exports = {
4
- extends: ['@frontify/eslint-config-typescript'],
4
+ extends: ['@frontify/eslint-config-react'],
5
5
  plugins: ['notice'],
6
6
  overrides: [
7
7
  {
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # @frontify/guideline-blocks-settings
2
2
 
3
+ ## 0.28.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#453](https://github.com/Frontify/brand-sdk/pull/453) [`0807349`](https://github.com/Frontify/brand-sdk/commit/0807349856fe972953493c253ac8e4ce352d3ede) Thanks [@fulopdaniel](https://github.com/fulopdaniel)! - Added components, helpers and utilities to simplify Blocks development
8
+
9
+ - [#466](https://github.com/Frontify/brand-sdk/pull/466) [`9f81a23`](https://github.com/Frontify/brand-sdk/commit/9f81a2363cd3c842d0ac5c014e0b07c46a3a1cca) Thanks [@triggertoo](https://github.com/triggertoo)! - Remove downloadAsset helper since it's unrelated to the settings
10
+
11
+ ### Patch Changes
12
+
13
+ - [#482](https://github.com/Frontify/brand-sdk/pull/482) [`6b18a74`](https://github.com/Frontify/brand-sdk/commit/6b18a74794612f61bd2e34ea8fdd4572ebb5274e) Thanks [@fulopdaniel](https://github.com/fulopdaniel)! - feat(\*): update shared folder with latest changes
14
+
15
+ - Updated dependencies [[`4105251`](https://github.com/Frontify/brand-sdk/commit/4105251969f8ae639014a013a2cbc88df8e4935d), [`af49f65`](https://github.com/Frontify/brand-sdk/commit/af49f6572592522a550f970c98e3d8fd2d377cc3)]:
16
+ - @frontify/sidebar-settings@0.6.0
17
+
3
18
  ## 0.27.0
4
19
 
5
20
  ### Minor Changes
package/README.md CHANGED
@@ -7,10 +7,9 @@ Provides the block settings types for the guideline-blocks.
7
7
  ```ts
8
8
  /* (c) Copyright Frontify Ltd., all rights reserved. */
9
9
 
10
- import { BlockSettings, Bundle } from '@frontify/guideline-blocks-settings';
11
- import { IconEnum } from '@frontify/fondue';
10
+ import { IconEnum, defineSettings } from '@frontify/guideline-blocks-settings';
12
11
 
13
- export const settings: BlockSettings = {
12
+ export const settings = defineSettings({
14
13
  main: [
15
14
  {
16
15
  id: 'example',
@@ -29,7 +28,7 @@ export const settings: BlockSettings = {
29
28
  label: 'Line',
30
29
  },
31
30
  ],
32
- onChange: (bundle: Bundle): void => {
31
+ onChange: (bundle): void => {
33
32
  const blockWidth = Number(bundle.getBlock('widthCustom')?.value);
34
33
  if (!Number.isNaN(blockWidth)) {
35
34
  bundle.setBlockValue('widthCustom', `${blockWidth}%`);
@@ -37,5 +36,25 @@ export const settings: BlockSettings = {
37
36
  },
38
37
  },
39
38
  ],
40
- };
39
+ });
41
40
  ```
41
+
42
+ # Block development helpers
43
+
44
+ ## Code Structure
45
+
46
+ - `components` (React components which are reused across multiple blocks)
47
+ - `utilities` (functionality without business logic)
48
+ - `helpers` (functionality with business logic)
49
+
50
+ ## Using components
51
+
52
+ To use React components, you need to add the styles import from the `@frontify/guideline-blocks-settings` package:
53
+
54
+ ```
55
+ import '@frontify/guideline-blocks-settings/styles';
56
+ ```
57
+
58
+ ## Development
59
+
60
+ Run `pnpm build` to make changes directly available to the linked packages.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frontify/guideline-blocks-settings",
3
- "version": "0.27.0",
3
+ "version": "0.28.0",
4
4
  "description": "Provides types and helpers for the guideline block development",
5
5
  "sideEffects": false,
6
6
  "main": "dist/index.umd.js",
@@ -14,22 +14,56 @@
14
14
  "url": "https://github.com/Frontify/brand-sdk",
15
15
  "directory": "packages/guideline-blocks-settings"
16
16
  },
17
+ "exports": {
18
+ ".": {
19
+ "import": "./dist/index.es.js",
20
+ "require": "./dist/index.umd.js"
21
+ },
22
+ "./styles": "./dist/style.css"
23
+ },
17
24
  "devDependencies": {
18
- "@frontify/eslint-config-typescript": "^0.15.7",
19
- "@types/react": "^18.2.0",
20
- "@types/react-dom": "^18.2.1",
21
- "eslint": "^8.39.0",
25
+ "@babel/core": "^7.22.5",
26
+ "@frontify/eslint-config-typescript": "^0.15.8",
27
+ "@testing-library/react": "^14.0.0",
28
+ "@types/escape-html": "^1.0.2",
29
+ "@types/node": "^18.7.21",
30
+ "@types/react": "^18.2.14",
31
+ "@types/react-dom": "^18.2.6",
32
+ "@types/sinon": "^10.0.15",
33
+ "@vitejs/plugin-react": "^4.0.1",
34
+ "autoprefixer": "^10.4.14",
35
+ "cypress": "^12.16.0",
36
+ "cypress-real-events": "^1.8.1",
37
+ "eslint": "^8.43.0",
22
38
  "eslint-plugin-notice": "^0.9.10",
39
+ "happy-dom": "^9.20.3",
40
+ "mitt": "^3.0.0",
41
+ "msw": "^1.2.2",
42
+ "postcss": "^8.4.24",
23
43
  "prettier": "^2.8.8",
24
44
  "react": "^18.2.0",
25
45
  "react-dom": "^18.2.0",
26
- "typescript": "^5.0.4",
27
- "vite": "^4.3.2",
28
- "vite-plugin-dts": "^2.3.0"
46
+ "sinon": "15.2.0",
47
+ "tailwindcss": "^3.3.2",
48
+ "typescript": "^5.1.5",
49
+ "vite": "^4.3.9",
50
+ "vite-plugin-dts": "^2.3.0",
51
+ "vitest": "^0.32.2"
29
52
  },
30
53
  "dependencies": {
31
- "@frontify/app-bridge": "^3.0.0-beta.66",
32
- "@frontify/sidebar-settings": "^0.5.0"
54
+ "@ctrl/tinycolor": "^3.6.0",
55
+ "@dnd-kit/core": "^6.0.8",
56
+ "@dnd-kit/modifiers": "^6.0.1",
57
+ "@dnd-kit/sortable": "^7.0.2",
58
+ "@frontify/fondue": "^12.0.0-beta.287",
59
+ "@react-aria/focus": "^3.13.0",
60
+ "@react-stately/overlays": "^3.6.0",
61
+ "@udecode/plate": "^21",
62
+ "escape-html": "^1.0.3",
63
+ "slate": "^0.94.1",
64
+ "slate-react": "^0.97.1",
65
+ "@frontify/app-bridge": "3.0.0-beta.86",
66
+ "@frontify/sidebar-settings": "0.6.0"
33
67
  },
34
68
  "peerDependencies": {
35
69
  "react": "^18",
@@ -41,6 +75,8 @@
41
75
  "lint:fix": "eslint --fix .",
42
76
  "prettier": "prettier --check .",
43
77
  "prettier:fix": "prettier --write .",
78
+ "test": "vitest run --silent",
79
+ "test:watch": "vitest",
44
80
  "typecheck": "tsc --noEmit"
45
81
  }
46
82
  }
@@ -0,0 +1,8 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ module.exports = {
4
+ plugins: {
5
+ tailwindcss: {},
6
+ autoprefixer: {},
7
+ },
8
+ };
package/setupTests.ts ADDED
@@ -0,0 +1,13 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import { afterEach, vi } from 'vitest';
4
+
5
+ vi.stubGlobal('crypto', {
6
+ getRandomValues: vi.fn(),
7
+ });
8
+
9
+ afterEach(() => {
10
+ vi.restoreAllMocks();
11
+ });
12
+
13
+ globalThis.structuredClone = (data: unknown) => JSON.parse(JSON.stringify(data));
@@ -0,0 +1,257 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import { MutableRefObject, forwardRef, useEffect, useState } from 'react';
4
+ import { Asset, useAssetUpload, useFileInput } from '@frontify/app-bridge';
5
+ import { useSortable } from '@dnd-kit/sortable';
6
+ import { useFocusRing } from '@react-aria/focus';
7
+
8
+ import {
9
+ ActionMenu,
10
+ Button,
11
+ ButtonEmphasis,
12
+ FOCUS_STYLE,
13
+ Flyout,
14
+ FlyoutPlacement,
15
+ IconArrowCircleUp20,
16
+ IconDocument24,
17
+ IconGrabHandle20,
18
+ IconImage24,
19
+ IconImageStack20,
20
+ IconMusicNote24,
21
+ IconPen20,
22
+ IconPlayFrame24,
23
+ IconTrashBin20,
24
+ LoadingCircle,
25
+ LoadingCircleSize,
26
+ MenuItemContentSize,
27
+ MenuItemStyle,
28
+ } from '@frontify/fondue';
29
+ import { AttachmentItemProps, SortableAttachmentItemProps } from './types';
30
+ import { joinClassNames } from '../../utilities';
31
+
32
+ const getDecorator = (type: string) => {
33
+ if (type === 'IMAGE') {
34
+ return <IconImage24 />;
35
+ } else if (type === 'VIDEO') {
36
+ return <IconPlayFrame24 />;
37
+ } else if (type === 'AUDIO') {
38
+ return <IconMusicNote24 />;
39
+ } else {
40
+ return <IconDocument24 />;
41
+ }
42
+ };
43
+
44
+ export const AttachmentItem = forwardRef<HTMLButtonElement, AttachmentItemProps>(
45
+ (
46
+ {
47
+ item,
48
+ isEditing,
49
+ draggableProps,
50
+ transformStyle,
51
+ isDragging,
52
+ isOverlay,
53
+ isLoading,
54
+ onDelete,
55
+ onReplaceWithBrowse,
56
+ onReplaceWithUpload,
57
+ },
58
+ ref
59
+ ) => {
60
+ const [selectedAsset, setSelectedAsset] = useState<Asset | undefined>();
61
+ const [openFileDialog, { selectedFiles }] = useFileInput({ multiple: true, accept: 'image/*' });
62
+ const [uploadFile, { results: uploadResults, doneAll }] = useAssetUpload();
63
+ const { focusProps, isFocusVisible } = useFocusRing();
64
+
65
+ useEffect(() => {
66
+ if (selectedFiles) {
67
+ uploadFile(selectedFiles[0]);
68
+ }
69
+ // eslint-disable-next-line react-hooks/exhaustive-deps
70
+ }, [selectedFiles]);
71
+
72
+ useEffect(() => {
73
+ if (doneAll) {
74
+ onReplaceWithUpload(uploadResults[0]);
75
+ }
76
+ // eslint-disable-next-line react-hooks/exhaustive-deps
77
+ }, [doneAll, uploadResults]);
78
+
79
+ const download = (url: string, filename: string) => {
80
+ fetch(url).then((response) => {
81
+ response.blob().then((blob) => {
82
+ const url = URL.createObjectURL(blob);
83
+ const a = document.createElement('a');
84
+ a.href = url;
85
+ a.download = filename;
86
+ a.click();
87
+ });
88
+ });
89
+ };
90
+
91
+ const showLoadingCircle = isLoading || (selectedFiles && !doneAll);
92
+
93
+ return (
94
+ <button
95
+ aria-label="Download attachment"
96
+ data-test-id="attachments-item"
97
+ onClick={() => download(item.genericUrl, item.fileName)}
98
+ ref={ref}
99
+ style={{
100
+ ...transformStyle,
101
+ opacity: isDragging && !isOverlay ? 0.3 : 1,
102
+ fontFamily: 'var(-f-theme-settings-body-font-family)',
103
+ }}
104
+ className={joinClassNames([
105
+ 'tw-cursor-pointer tw-text-left tw-w-full tw-relative tw-flex tw-gap-3 tw-px-5 tw-py-3 tw-items-center tw-group hover:tw-bg-box-neutral-hover',
106
+ isDragging ? 'tw-bg-box-neutral-hover' : '',
107
+ ])}
108
+ >
109
+ <div className="tw-text-text-weak group-hover:tw-text-box-neutral-inverse-hover">
110
+ {showLoadingCircle ? (
111
+ <LoadingCircle size={LoadingCircleSize.Small} />
112
+ ) : (
113
+ getDecorator(item.objectType)
114
+ )}
115
+ </div>
116
+ <div className="tw-text-s tw-flex-1 tw-min-w-0">
117
+ <div className="tw-whitespace-nowrap tw-overflow-hidden tw-text-ellipsis tw-font-bold tw-text-text-weak group-hover:tw-text-box-neutral-inverse-hover">
118
+ {item.title}
119
+ </div>
120
+ <div className="tw-text-text-weak">{`${item.fileSizeHumanReadable} - ${item.extension}`}</div>
121
+ </div>
122
+ {isEditing && (
123
+ <div
124
+ data-test-id="attachments-actionbar"
125
+ className={joinClassNames([
126
+ 'tw-flex tw-gap-0.5 group-focus:tw-opacity-100 focus-visible:tw-opacity-100 focus-within:tw-opacity-100 group-hover:tw-opacity-100',
127
+ isOverlay || selectedAsset?.id === item.id ? 'tw-opacity-100' : 'tw-opacity-0',
128
+ ])}
129
+ >
130
+ <button
131
+ {...focusProps}
132
+ {...draggableProps}
133
+ aria-label="Drag attachment"
134
+ className={joinClassNames([
135
+ ' tw-border-button-border tw-bg-button-background active:tw-bg-button-background-pressed tw-group tw-border tw-box-box tw-relative tw-flex tw-items-center tw-justify-center tw-outline-none tw-font-medium tw-rounded tw-h-9 tw-w-9 ',
136
+ isDragging || isOverlay
137
+ ? 'tw-cursor-grabbing tw-bg-button-background-pressed hover:tw-bg-button-background-pressed'
138
+ : 'tw-cursor-grab hover:tw-bg-button-background-hover',
139
+ isFocusVisible && FOCUS_STYLE,
140
+ isFocusVisible && 'tw-z-[2]',
141
+ ])}
142
+ >
143
+ <IconGrabHandle20 />
144
+ </button>
145
+ <div data-test-id="attachments-actionbar-flyout">
146
+ <Flyout
147
+ placement={FlyoutPlacement.Right}
148
+ isOpen={selectedAsset?.id === item.id}
149
+ fitContent
150
+ legacyFooter={false}
151
+ onOpenChange={(isOpen) => setSelectedAsset(isOpen ? item : undefined)}
152
+ trigger={(_, ref) => (
153
+ <Button
154
+ ref={ref as MutableRefObject<HTMLButtonElement>}
155
+ icon={<IconPen20 />}
156
+ emphasis={ButtonEmphasis.Default}
157
+ onClick={() => setSelectedAsset(item)}
158
+ />
159
+ )}
160
+ >
161
+ <ActionMenu
162
+ menuBlocks={[
163
+ {
164
+ id: 'menu',
165
+ menuItems: [
166
+ {
167
+ id: 'upload',
168
+ size: MenuItemContentSize.XSmall,
169
+ title: 'Replace with upload',
170
+ onClick: () => {
171
+ openFileDialog();
172
+ setSelectedAsset(undefined);
173
+ },
174
+
175
+ initialValue: true,
176
+ decorator: (
177
+ <div className="tw-mr-2">
178
+ <IconArrowCircleUp20 />
179
+ </div>
180
+ ),
181
+ },
182
+
183
+ {
184
+ id: 'asset',
185
+ size: MenuItemContentSize.XSmall,
186
+ title: 'Replace with asset',
187
+ onClick: () => {
188
+ onReplaceWithBrowse();
189
+ setSelectedAsset(undefined);
190
+ },
191
+ initialValue: true,
192
+ decorator: (
193
+ <div className="tw-mr-2">
194
+ <IconImageStack20 />
195
+ </div>
196
+ ),
197
+ },
198
+ ],
199
+ },
200
+ {
201
+ id: 'menu-delete',
202
+ menuItems: [
203
+ {
204
+ id: 'delete',
205
+ size: MenuItemContentSize.XSmall,
206
+ title: 'Delete',
207
+ style: MenuItemStyle.Danger,
208
+ onClick: () => {
209
+ onDelete();
210
+ setSelectedAsset(undefined);
211
+ },
212
+
213
+ initialValue: true,
214
+ decorator: (
215
+ <div className="tw-mr-2">
216
+ <IconTrashBin20 />
217
+ </div>
218
+ ),
219
+ },
220
+ ],
221
+ },
222
+ ]}
223
+ />
224
+ </Flyout>
225
+ </div>
226
+ </div>
227
+ )}
228
+ </button>
229
+ );
230
+ }
231
+ );
232
+
233
+ AttachmentItem.displayName = 'AttachmentItem';
234
+
235
+ export const SortableAttachmentItem = (props: SortableAttachmentItemProps) => {
236
+ const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({
237
+ id: props.item.id,
238
+ });
239
+
240
+ const transformStyle = {
241
+ transform: transform ? `translate(${transform.x}px, ${transform.y}px)` : '',
242
+ transition,
243
+ zIndex: isDragging ? 2 : 1,
244
+ };
245
+
246
+ const draggableProps = { ...attributes, ...listeners };
247
+
248
+ return (
249
+ <AttachmentItem
250
+ ref={setNodeRef}
251
+ isDragging={isDragging}
252
+ transformStyle={transformStyle}
253
+ draggableProps={draggableProps}
254
+ {...props}
255
+ />
256
+ );
257
+ };
@@ -0,0 +1,151 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import { AssetDummy, getAppBridgeBlockStub } from '@frontify/app-bridge';
4
+ import { mount } from 'cypress/react18';
5
+ import { Attachments as AttachmentsComponent } from './Attachments';
6
+ import { AttachmentsProps } from './types';
7
+ import { SinonStub } from 'cypress/types/sinon';
8
+
9
+ const FlyoutButtonSelector = '[data-test-id="attachments-flyout-button"]';
10
+ const AssetInputSelector = '[data-test-id="asset-input-placeholder"]';
11
+ const ActionBarSelector = '[data-test-id="attachments-actionbar"]';
12
+ const DragHandleSelector = '[data-test-id="attachments-actionbar"] > button';
13
+ const FlyoutTriggerSelector = '[data-test-id="attachments-actionbar-flyout"] button';
14
+ const AttachmentItemSelector = '[data-test-id="attachments-item"]';
15
+ const MenuItemSelector = '[data-test-id="menu-item"]';
16
+ const LoadingCircleSelector = '[data-test-id="loading-circle"]';
17
+
18
+ const Attachments = ({
19
+ appBridge = getAppBridgeBlockStub(),
20
+ onDelete = cy.stub(),
21
+ items,
22
+ onReplaceWithBrowse = cy.stub(),
23
+ onReplaceWithUpload = cy.stub(),
24
+ onSorted = cy.stub(),
25
+ onBrowse = cy.stub(),
26
+ onUpload = cy.stub(),
27
+ }: Partial<AttachmentsProps>) => {
28
+ return (
29
+ <AttachmentsComponent
30
+ appBridge={appBridge}
31
+ onDelete={onDelete}
32
+ items={items}
33
+ onReplaceWithBrowse={onReplaceWithBrowse}
34
+ onReplaceWithUpload={onReplaceWithUpload}
35
+ onSorted={onSorted}
36
+ onBrowse={onBrowse}
37
+ onUpload={onUpload}
38
+ />
39
+ );
40
+ };
41
+
42
+ describe('Attachments', () => {
43
+ it('renders attachments flyout if it is in edit mode', () => {
44
+ mount(<Attachments appBridge={getAppBridgeBlockStub({ editorState: true })} />);
45
+ cy.get(FlyoutButtonSelector).should('exist');
46
+ });
47
+
48
+ it('renders attachments flyout if it has attachments', () => {
49
+ mount(<Attachments items={[AssetDummy.with(1)]} />);
50
+ cy.get(FlyoutButtonSelector).should('exist');
51
+ });
52
+
53
+ it('does not render attachments flyout if there are no attachments', () => {
54
+ mount(<Attachments items={[]} />);
55
+ cy.get(FlyoutButtonSelector).should('not.exist');
56
+ });
57
+
58
+ it('renders asset input if in edit mode', () => {
59
+ mount(<Attachments appBridge={getAppBridgeBlockStub({ editorState: true })} items={[AssetDummy.with(1)]} />);
60
+ cy.get(FlyoutButtonSelector).click();
61
+ cy.get(AssetInputSelector).should('exist');
62
+ });
63
+
64
+ it('does not render asset input if in view mode', () => {
65
+ mount(<Attachments items={[AssetDummy.with(1)]} />);
66
+ cy.get(FlyoutButtonSelector).click();
67
+ cy.get(AssetInputSelector).should('not.exist');
68
+ });
69
+
70
+ it('renders asset action buttons if in edit mode', () => {
71
+ mount(<Attachments appBridge={getAppBridgeBlockStub({ editorState: true })} items={[AssetDummy.with(1)]} />);
72
+ cy.get(FlyoutButtonSelector).click();
73
+ cy.get(ActionBarSelector).should('exist');
74
+ });
75
+
76
+ it('does not render asset action buttons if in view mode', () => {
77
+ mount(<Attachments items={[AssetDummy.with(1)]} />);
78
+ cy.get(FlyoutButtonSelector).click();
79
+ cy.get(ActionBarSelector).should('not.exist');
80
+ });
81
+
82
+ it('renders an attachment item for each asset', () => {
83
+ mount(<Attachments items={[AssetDummy.with(1), AssetDummy.with(2), AssetDummy.with(3)]} />);
84
+ cy.get(FlyoutButtonSelector).click();
85
+ cy.get(AttachmentItemSelector).should('have.length', 3);
86
+ });
87
+
88
+ it('renders loading circle for attachment item', () => {
89
+ const appBridge = getAppBridgeBlockStub({
90
+ editorState: true,
91
+ });
92
+ (appBridge.openAssetChooser as SinonStub) = cy.stub().callsArgWith(0, AssetDummy.with(4));
93
+
94
+ cy.clock();
95
+ const replaceStub = () =>
96
+ new Promise<void>((resolve) =>
97
+ setTimeout(() => {
98
+ resolve();
99
+ }, 2000)
100
+ );
101
+
102
+ mount(
103
+ <Attachments
104
+ onReplaceWithBrowse={replaceStub}
105
+ items={[AssetDummy.with(1), AssetDummy.with(2), AssetDummy.with(3)]}
106
+ appBridge={appBridge}
107
+ />
108
+ );
109
+
110
+ cy.get(FlyoutButtonSelector).click();
111
+ cy.get(AttachmentItemSelector).eq(0).focus();
112
+ cy.get(FlyoutTriggerSelector).eq(1).click();
113
+ cy.get(MenuItemSelector).eq(1).click();
114
+ cy.get(LoadingCircleSelector).should('exist');
115
+ cy.tick(2000);
116
+ cy.get(LoadingCircleSelector).should('not.exist');
117
+ });
118
+
119
+ it('renders focus ring on flyout button while tabbing and open it', () => {
120
+ mount(
121
+ <Attachments
122
+ appBridge={getAppBridgeBlockStub({ editorState: true })}
123
+ items={[AssetDummy.with(1), AssetDummy.with(2), AssetDummy.with(3)]}
124
+ />
125
+ );
126
+ cy.get(FlyoutButtonSelector).click();
127
+ cy.realPress('Tab');
128
+ cy.realPress('Tab');
129
+ cy.realPress('Tab');
130
+ cy.get(FlyoutTriggerSelector).eq(0).should('have.class', 'focus-visible:tw-ring-blue');
131
+ cy.get(FlyoutTriggerSelector).eq(0).type('{enter}');
132
+ cy.get(MenuItemSelector).should('exist');
133
+ });
134
+
135
+ it('reorders items using only keyboard events', () => {
136
+ const onSortStub = cy.stub();
137
+ mount(
138
+ <Attachments
139
+ appBridge={getAppBridgeBlockStub({ editorState: true })}
140
+ items={[{ ...AssetDummy.with(1), title: 'Moved item' }, AssetDummy.with(2), AssetDummy.with(3)]}
141
+ onSorted={onSortStub}
142
+ />
143
+ );
144
+ cy.get(FlyoutButtonSelector).click();
145
+ cy.realPress('Tab');
146
+ cy.realPress('Tab');
147
+ cy.realPress('Tab');
148
+ cy.get(DragHandleSelector).eq(0).type(' {downarrow}{downarrow} ');
149
+ cy.get(AttachmentItemSelector).eq(1).should('contain.text', 'Moved item');
150
+ });
151
+ });