@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,73 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import {
4
+ ELEMENT_LINK,
5
+ FloatingLinkProps,
6
+ HTMLPropsAs,
7
+ LinkPlugin,
8
+ floatingLinkActions,
9
+ getPluginOptions,
10
+ getSelectionBoundingClientRect,
11
+ triggerFloatingLinkInsert,
12
+ useComposedRef,
13
+ useEditorRef,
14
+ useFloatingLinkEscape,
15
+ useFloatingLinkSelectors,
16
+ useHotkeys,
17
+ useVirtualFloatingLink,
18
+ } from '@udecode/plate';
19
+ import { useEffect } from 'react';
20
+ import { useFocused } from 'slate-react';
21
+
22
+ export const useFloatingLinkInsert = ({ floatingOptions, ...props }: FloatingLinkProps): HTMLPropsAs<'div'> => {
23
+ const editor = useEditorRef();
24
+ const focused = useFocused();
25
+ const mode = useFloatingLinkSelectors().mode();
26
+ const open = useFloatingLinkSelectors().isOpen(editor.id);
27
+
28
+ const { triggerFloatingLinkHotkeys } = getPluginOptions<LinkPlugin>(editor, ELEMENT_LINK);
29
+
30
+ useHotkeys(
31
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
32
+ triggerFloatingLinkHotkeys!,
33
+ (e) => {
34
+ if (triggerFloatingLinkInsert(editor, { focused })) {
35
+ e.preventDefault();
36
+ }
37
+ },
38
+ {
39
+ enableOnContentEditable: true,
40
+ },
41
+ [focused]
42
+ );
43
+
44
+ const { update, style, floating } = useVirtualFloatingLink({
45
+ editorId: editor.id,
46
+ open: open && mode === 'insert',
47
+ getBoundingClientRect: getSelectionBoundingClientRect,
48
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
49
+ whileElementsMounted: () => {},
50
+ ...floatingOptions,
51
+ });
52
+
53
+ // wait for update before focusing input
54
+ useEffect(() => {
55
+ if (open) {
56
+ update();
57
+ floatingLinkActions.updated(true);
58
+ } else {
59
+ floatingLinkActions.updated(false);
60
+ }
61
+ }, [open, update]);
62
+
63
+ useFloatingLinkEscape();
64
+
65
+ return {
66
+ style: {
67
+ ...style,
68
+ zIndex: 1000,
69
+ },
70
+ ...props,
71
+ ref: useComposedRef<HTMLElement | null>(props.ref, floating),
72
+ };
73
+ };
@@ -0,0 +1,136 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import { AppBridgeBlock } from '@frontify/app-bridge';
4
+ import { CheckboxState } from '@frontify/fondue';
5
+ import {
6
+ ELEMENT_LINK,
7
+ floatingLinkActions,
8
+ floatingLinkSelectors,
9
+ getPluginOptions,
10
+ submitFloatingLink,
11
+ useEditorRef,
12
+ useHotkeys,
13
+ } from '@udecode/plate';
14
+ import { Dispatch, Reducer, useEffect, useReducer } from 'react';
15
+ import { getLegacyUrl, getUrl } from '../../utils';
16
+ import { InsertModalDispatchType, InsertModalStateProps } from './types';
17
+ import { addHttps } from '../../../../../../helpers';
18
+ import { isValidUrlOrEmpty } from '../../utils/url';
19
+
20
+ const initialState: InsertModalStateProps = {
21
+ url: '',
22
+ text: '',
23
+ newTab: CheckboxState.Unchecked,
24
+ };
25
+
26
+ export const InsertModalState = (): [InsertModalStateProps, Dispatch<InsertModalDispatchType>] => {
27
+ const [state, dispatch] = useReducer<Reducer<InsertModalStateProps, InsertModalDispatchType>>((state, action) => {
28
+ const { type, payload } = action;
29
+
30
+ switch (type) {
31
+ case 'NEW_TAB':
32
+ return {
33
+ ...state,
34
+ newTab: CheckboxState.Checked,
35
+ };
36
+ case 'SAME_TAB':
37
+ return {
38
+ ...state,
39
+ newTab: CheckboxState.Unchecked,
40
+ };
41
+ case 'URL':
42
+ case 'TEXT':
43
+ case 'INIT':
44
+ return {
45
+ ...state,
46
+ ...payload,
47
+ };
48
+ default:
49
+ return state;
50
+ }
51
+ }, initialState);
52
+
53
+ return [state, dispatch];
54
+ };
55
+
56
+ export const useInsertModal = () => {
57
+ const editor = useEditorRef();
58
+ const [state, dispatch] = InsertModalState();
59
+
60
+ useEffect(() => {
61
+ const legacyUrl = getLegacyUrl(editor);
62
+ const url = getUrl(editor);
63
+
64
+ dispatch({
65
+ type: 'INIT',
66
+ payload: {
67
+ text: floatingLinkSelectors.text(),
68
+ newTab: floatingLinkSelectors.newTab() ? CheckboxState.Checked : CheckboxState.Unchecked,
69
+ url: legacyUrl && url === '' ? legacyUrl : floatingLinkSelectors.url(),
70
+ },
71
+ });
72
+ }, [dispatch, editor]);
73
+
74
+ const onTextChange = (value: string) => {
75
+ dispatch({
76
+ type: 'TEXT',
77
+ payload: { text: value },
78
+ });
79
+ };
80
+
81
+ const onUrlChange = (value: string) => {
82
+ dispatch({
83
+ type: 'URL',
84
+ payload: { url: value },
85
+ });
86
+ };
87
+
88
+ const onToggleTab = (checked: boolean) => {
89
+ checked ? dispatch({ type: 'NEW_TAB' }) : dispatch({ type: 'SAME_TAB' });
90
+ };
91
+
92
+ const onCancel = () => {
93
+ floatingLinkActions.hide();
94
+ };
95
+
96
+ const onSave = (event: React.MouseEvent<HTMLButtonElement, MouseEvent> | KeyboardEvent | undefined) => {
97
+ if (!isValidUrlOrEmpty(state.url) || !hasValues) {
98
+ return;
99
+ }
100
+
101
+ const urlToSave = addHttps(state.url);
102
+
103
+ floatingLinkActions.text(state.text);
104
+ floatingLinkActions.url(urlToSave);
105
+ floatingLinkActions.newTab(state.newTab === CheckboxState.Checked);
106
+
107
+ if (submitFloatingLink(editor)) {
108
+ event?.preventDefault();
109
+ }
110
+ };
111
+
112
+ const hasValues = state.url !== '' && state.text !== '';
113
+
114
+ const { appBridge } = getPluginOptions<{ appBridge: AppBridgeBlock }>(editor, ELEMENT_LINK);
115
+
116
+ useHotkeys(
117
+ 'enter',
118
+ onSave,
119
+ {
120
+ enableOnFormTags: ['INPUT'],
121
+ },
122
+ []
123
+ );
124
+
125
+ return {
126
+ state,
127
+ onTextChange,
128
+ onUrlChange,
129
+ onToggleTab,
130
+ onCancel,
131
+ onSave,
132
+ hasValues,
133
+ isValidUrlOrEmpty,
134
+ appBridge,
135
+ };
136
+ };
@@ -0,0 +1,38 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import {
4
+ IconLink,
5
+ IconSize,
6
+ PluginButtonProps,
7
+ getButtonClassNames,
8
+ getHotkeyByPlatform,
9
+ getTooltip,
10
+ } from '@frontify/fondue';
11
+ import { LinkToolbarButton, isRangeInSameBlock, useEventPlateId, usePlateEditorState } from '@udecode/plate';
12
+
13
+ export const LinkButton = ({ id, editorId }: PluginButtonProps) => {
14
+ const editor = usePlateEditorState(useEventPlateId(editorId));
15
+ const isEnabled = !!isRangeInSameBlock(editor, {
16
+ at: editor.selection,
17
+ });
18
+
19
+ return (
20
+ <div data-plugin-id={id}>
21
+ <LinkToolbarButton
22
+ tooltip={getTooltip(
23
+ isEnabled
24
+ ? `Link\n${getHotkeyByPlatform('Ctrl+K')}`
25
+ : 'Links can only be set for a single text block.'
26
+ )}
27
+ icon={
28
+ <span className="tw-p-2 tw-h-8 tw-justify-center tw-items-center tw-flex">
29
+ <IconLink size={IconSize.Size16} />
30
+ </span>
31
+ }
32
+ classNames={getButtonClassNames(isEnabled)}
33
+ styles={{ root: { width: '24px', height: '24px' } }}
34
+ actionHandler="onMouseDown"
35
+ />
36
+ </div>
37
+ );
38
+ };
@@ -0,0 +1,36 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import { HTMLPropsAs, LinkRootProps, useElementProps } from '@udecode/plate';
4
+ import { MouseEvent } from 'react';
5
+ import { TLinkElement } from '../types';
6
+ import { BlockStyles } from '../../styles';
7
+ import { LINK_PLUGIN } from '../id';
8
+
9
+ const useLink = (props: LinkRootProps): HTMLPropsAs<'a'> => {
10
+ const _props = useElementProps<TLinkElement, 'a'>({
11
+ ...props,
12
+ elementToAttributes: (element) => ({
13
+ href: element.url || element.chosenLink?.searchResult?.link || '',
14
+ target: element.target || '_blank',
15
+ }),
16
+ });
17
+
18
+ return {
19
+ ..._props,
20
+ // quick fix: hovering <a> with href loses the editor focus
21
+ onMouseOver: (event: MouseEvent) => {
22
+ event.stopPropagation();
23
+ },
24
+ };
25
+ };
26
+
27
+ export const LinkMarkupElementNode = (props: LinkRootProps) => {
28
+ const htmlProps = useLink(props);
29
+ const { attributes, children } = props;
30
+
31
+ return (
32
+ <a {...attributes} href={htmlProps.href} target={htmlProps.target} style={BlockStyles[LINK_PLUGIN]}>
33
+ {children}
34
+ </a>
35
+ );
36
+ };
@@ -0,0 +1,11 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import { MarkupElement } from '@frontify/fondue';
4
+ import { ELEMENT_LINK } from '@udecode/plate';
5
+ import { LinkMarkupElementNode } from './LinkMarkupElementNode';
6
+
7
+ export class LinkMarkupElement extends MarkupElement {
8
+ constructor(id = ELEMENT_LINK, node = LinkMarkupElementNode) {
9
+ super(id, node);
10
+ }
11
+ }
@@ -0,0 +1,3 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ export const LINK_PLUGIN = 'link-plugin';
@@ -0,0 +1,48 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import { AppBridgeBlock } from '@frontify/app-bridge';
4
+ import { Plugin, PluginProps } from '@frontify/fondue';
5
+ import { createLinkPlugin as createPlateLinkPlugin, createPluginFactory } from '@udecode/plate';
6
+ import { CustomFloatingLink } from './FloatingLink/CustomFloatingLink';
7
+ import { LINK_PLUGIN } from './id';
8
+ import { LinkButton } from './LinkButton';
9
+ import { LinkMarkupElement } from './LinkMarkupElement';
10
+ import { isValidUrl } from './utils/url';
11
+ import { CSSProperties } from 'react';
12
+ import { BlockStyles } from '../styles';
13
+
14
+ export const createLinkPlugin = (appBridge: AppBridgeBlock) =>
15
+ createPluginFactory({
16
+ ...createPlateLinkPlugin(),
17
+ renderAfterEditable: CustomFloatingLink,
18
+ options: {
19
+ isUrl: isValidUrl,
20
+ rangeBeforeOptions: {
21
+ matchString: ' ',
22
+ skipInvalid: true,
23
+ afterMatch: true,
24
+ },
25
+ triggerFloatingLinkHotkeys: 'command+k, ctrl+k',
26
+ appBridge,
27
+ },
28
+ })();
29
+
30
+ export type LinkPluginProps = PluginProps & { appBridge: AppBridgeBlock };
31
+
32
+ export class LinkPlugin extends Plugin {
33
+ private appBridge: AppBridgeBlock;
34
+ public styles: CSSProperties = {};
35
+ constructor(props?: LinkPluginProps, styles = BlockStyles[LINK_PLUGIN]) {
36
+ super(LINK_PLUGIN, {
37
+ button: LinkButton,
38
+ markupElement: new LinkMarkupElement(),
39
+ ...props,
40
+ });
41
+ this.styles = styles;
42
+ this.appBridge = props?.appBridge as AppBridgeBlock;
43
+ }
44
+
45
+ plugins() {
46
+ return [createLinkPlugin(this.appBridge)];
47
+ }
48
+ }
@@ -0,0 +1,12 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import { TLinkElement as TPlateLinkElement } from '@udecode/plate';
4
+
5
+ export type TLinkElement = TPlateLinkElement & {
6
+ chosenLink?: {
7
+ searchResult?: {
8
+ link?: string;
9
+ };
10
+ openInNewTab?: boolean;
11
+ };
12
+ };
@@ -0,0 +1,30 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import { ELEMENT_LINK, PlateEditor, getAboveNode } from '@udecode/plate';
4
+ import { TLinkElement } from '../types';
5
+
6
+ const getLinkNode = (editor: PlateEditor, cb: (link: TLinkElement) => string): string => {
7
+ const linkNode = getAboveNode<TLinkElement>(editor, { match: { type: ELEMENT_LINK } });
8
+
9
+ if (!Array.isArray(linkNode)) {
10
+ return '';
11
+ }
12
+
13
+ return cb(linkNode[0]);
14
+ };
15
+
16
+ export const getLegacyUrl = (editor: PlateEditor) => {
17
+ return getLinkNode(editor, (link) => link.chosenLink?.searchResult?.link || '');
18
+ };
19
+
20
+ export const getUrl = (editor: PlateEditor) => {
21
+ return getLinkNode(editor, (link) => link.url || '');
22
+ };
23
+
24
+ export const getUrlFromLinkOrLegacyLink = (link: TLinkElement): string => {
25
+ return link.url || link.chosenLink?.searchResult?.link || '';
26
+ };
27
+
28
+ export const getUrlFromEditor = (editor: PlateEditor) => {
29
+ return getLinkNode(editor, getUrlFromLinkOrLegacyLink);
30
+ };
@@ -0,0 +1,4 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ export * from './getUrl';
4
+ export * from './relativeUrlRegex';
@@ -0,0 +1,35 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import { describe, expect, it } from 'vitest';
4
+ import { relativeUrlRegex } from './relativeUrlRegex';
5
+
6
+ describe('Regex values', () => {
7
+ it('should only match internal documents starting with /r/ or /document/', () => {
8
+ const testData = [
9
+ {
10
+ input: '/document/123',
11
+ shouldMatch: true,
12
+ },
13
+ {
14
+ input: '/r/123',
15
+ shouldMatch: true,
16
+ },
17
+ {
18
+ input: '/wrong/123',
19
+ shouldMatch: false,
20
+ },
21
+ {
22
+ input: '/r/',
23
+ shouldMatch: false,
24
+ },
25
+ {
26
+ input: '/document/',
27
+ shouldMatch: false,
28
+ },
29
+ ];
30
+ for (const data of testData) {
31
+ const isMatching = relativeUrlRegex.test(data.input);
32
+ expect(isMatching).toBe(data.shouldMatch);
33
+ }
34
+ });
35
+ });
@@ -0,0 +1,3 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ export const relativeUrlRegex = /^\/(document|r)\/\S+$/i;
@@ -0,0 +1,75 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import { describe, expect, it } from 'vitest';
4
+ import { isValidUrl } from './url';
5
+
6
+ describe('LinkPlugin URL utils', () => {
7
+ it('should validate isValidUrl', () => {
8
+ const testData = [
9
+ {
10
+ input: 'https://frontify.com',
11
+ shouldBeValid: true,
12
+ },
13
+ {
14
+ input: 'http://frontify.com',
15
+ shouldBeValid: true,
16
+ },
17
+ {
18
+ input: 'http://frontify.com/any/path',
19
+ shouldBeValid: true,
20
+ },
21
+ {
22
+ input: 'https://product.frontify.com/any/path/with#3/',
23
+ shouldBeValid: true,
24
+ },
25
+ {
26
+ input: 'mailto:hello@frontify.com',
27
+ shouldBeValid: true,
28
+ },
29
+ {
30
+ input: 'tel:+123456789',
31
+ shouldBeValid: true,
32
+ },
33
+ {
34
+ input: 'tel:',
35
+ shouldBeValid: false,
36
+ },
37
+ {
38
+ input: 'anyword:',
39
+ shouldBeValid: false,
40
+ },
41
+ {
42
+ input: '/r/123',
43
+ shouldBeValid: true,
44
+ },
45
+ {
46
+ input: '/document/123',
47
+ shouldBeValid: true,
48
+ },
49
+ {
50
+ input: '/wrongpage/123',
51
+ shouldBeValid: false,
52
+ },
53
+ {
54
+ input: '+123456789',
55
+ shouldBeValid: false,
56
+ },
57
+ {
58
+ input: 'hello@frontify.com',
59
+ shouldBeValid: false,
60
+ },
61
+ {
62
+ input: 'frontify.com',
63
+ shouldBeValid: false,
64
+ },
65
+ {
66
+ input: 'frontify',
67
+ shouldBeValid: false,
68
+ },
69
+ ];
70
+ for (const data of testData) {
71
+ const isValid = isValidUrl(data.input);
72
+ expect(isValid).toBe(data.shouldBeValid);
73
+ }
74
+ });
75
+ });
@@ -0,0 +1,21 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import { addHttps } from '../../../../../helpers';
4
+ import { relativeUrlRegex } from './relativeUrlRegex';
5
+
6
+ export const isValidUrl = (url: string) => {
7
+ if (relativeUrlRegex.test(url)) {
8
+ return true;
9
+ }
10
+ try {
11
+ const parsedUrl = new URL(url);
12
+ const validProtocols = ['http:', 'https:', 'mailto:', 'tel:'];
13
+ return validProtocols.includes(parsedUrl.protocol) && parsedUrl.pathname !== '';
14
+ } catch (error) {
15
+ return false;
16
+ }
17
+ };
18
+
19
+ export const isValidUrlOrEmpty = (url: string) => {
20
+ return isValidUrl(addHttps(url)) || url === '';
21
+ };
@@ -0,0 +1,61 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import { createPluginFactory } from '@udecode/plate';
4
+ import { CSSProperties } from 'react';
5
+ import {
6
+ MarkupElement,
7
+ Plugin,
8
+ PluginProps,
9
+ TextStyleRenderElementProps,
10
+ alignmentClassnames,
11
+ getColumnBreakClasses,
12
+ merge,
13
+ } from '@frontify/fondue';
14
+ import { BlockStyles, TextStyles } from '../styles';
15
+
16
+ const ID = 'textstyle-custom1-plugin';
17
+
18
+ export class Custom1Plugin extends Plugin {
19
+ public styles: CSSProperties = {};
20
+ constructor({ styles = BlockStyles.custom1, ...props }: PluginProps = {}) {
21
+ super(TextStyles.custom1, {
22
+ label: 'Custom 1',
23
+ markupElement: new Custom1MarkupElement(),
24
+ ...props,
25
+ });
26
+ this.styles = styles;
27
+ }
28
+
29
+ plugins() {
30
+ return [createCustom1Plugin(this.styles)];
31
+ }
32
+ }
33
+
34
+ class Custom1MarkupElement extends MarkupElement {
35
+ constructor(id = ID, node = Custom1MarkupElementNode) {
36
+ super(id, node);
37
+ }
38
+ }
39
+
40
+ const Custom1MarkupElementNode = ({ element, attributes, children, styles }: TextStyleRenderElementProps) => {
41
+ const align = element.align as string;
42
+ return (
43
+ <p
44
+ {...attributes}
45
+ className={merge([align && alignmentClassnames[align], getColumnBreakClasses(element), 'a-custom1'])}
46
+ >
47
+ <span style={styles}>{children}</span>
48
+ </p>
49
+ );
50
+ };
51
+
52
+ const createCustom1Plugin = (styles: CSSProperties) =>
53
+ createPluginFactory({
54
+ key: TextStyles.custom1,
55
+ isElement: true,
56
+ deserializeHtml: {
57
+ rules: [{ validClassName: TextStyles.custom1 }],
58
+ },
59
+ })({
60
+ component: (props: TextStyleRenderElementProps) => <Custom1MarkupElementNode {...props} styles={styles} />,
61
+ });
@@ -0,0 +1,61 @@
1
+ /* (c) Copyright Frontify Ltd., all rights reserved. */
2
+
3
+ import { createPluginFactory } from '@udecode/plate';
4
+ import { CSSProperties } from 'react';
5
+ import {
6
+ MarkupElement,
7
+ Plugin,
8
+ PluginProps,
9
+ TextStyleRenderElementProps,
10
+ alignmentClassnames,
11
+ getColumnBreakClasses,
12
+ merge,
13
+ } from '@frontify/fondue';
14
+ import { BlockStyles, TextStyles } from '../styles';
15
+
16
+ const ID = 'textstyle-custom2-plugin';
17
+
18
+ export class Custom2Plugin extends Plugin {
19
+ public styles: CSSProperties = {};
20
+ constructor({ styles = BlockStyles.custom2, ...props }: PluginProps = {}) {
21
+ super(TextStyles.custom2, {
22
+ label: 'Custom 2',
23
+ markupElement: new Custom2MarkupElement(),
24
+ ...props,
25
+ });
26
+ this.styles = styles;
27
+ }
28
+
29
+ plugins() {
30
+ return [createCustom2Plugin(this.styles)];
31
+ }
32
+ }
33
+
34
+ class Custom2MarkupElement extends MarkupElement {
35
+ constructor(id = ID, node = Custom2MarkupElementNode) {
36
+ super(id, node);
37
+ }
38
+ }
39
+
40
+ const Custom2MarkupElementNode = ({ element, attributes, children, styles }: TextStyleRenderElementProps) => {
41
+ const align = element.align as string;
42
+ return (
43
+ <p
44
+ {...attributes}
45
+ className={merge([align && alignmentClassnames[align], getColumnBreakClasses(element), 'a-custom2'])}
46
+ >
47
+ <span style={styles}>{children}</span>
48
+ </p>
49
+ );
50
+ };
51
+
52
+ const createCustom2Plugin = (styles: CSSProperties) =>
53
+ createPluginFactory({
54
+ key: TextStyles.custom2,
55
+ isElement: true,
56
+ deserializeHtml: {
57
+ rules: [{ validClassName: TextStyles.custom2 }],
58
+ },
59
+ })({
60
+ component: (props: TextStyleRenderElementProps) => <Custom2MarkupElementNode {...props} styles={styles} />,
61
+ });