@zipify/wysiwyg 1.0.0-dev.1

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 (255) hide show
  1. package/.editorconfig +18 -0
  2. package/.eslintrc.js +253 -0
  3. package/.github/actions/deploy-example/action.yaml +61 -0
  4. package/.github/actions/lint-css/action.yaml +15 -0
  5. package/.github/actions/lint-js/action.yaml +22 -0
  6. package/.github/actions/setup/action.yaml +19 -0
  7. package/.github/actions/unit-tests/action.yaml +13 -0
  8. package/.github/actions/unit-tests/jest.config.js +8 -0
  9. package/.github/dependabot.yaml +17 -0
  10. package/.github/pull_request_template.md +17 -0
  11. package/.github/workflows/frontend-ci.yaml +120 -0
  12. package/.husky/pre-commit +4 -0
  13. package/.lintstagedrc +12 -0
  14. package/.release-it.json +6 -0
  15. package/.stylelintrc +110 -0
  16. package/README.md +61 -0
  17. package/babel.config.js +15 -0
  18. package/ci/example/deploy.sh +25 -0
  19. package/config/jest/TestEnvironment.js +27 -0
  20. package/config/jest/matchers/index.js +6 -0
  21. package/config/jest/matchers/toElementHasStyle.js +27 -0
  22. package/config/jest/matchers/toVueContainComponent.js +20 -0
  23. package/config/jest/matchers/toVueContainElement.js +34 -0
  24. package/config/jest/matchers/toVueContainLazyComponent.js +19 -0
  25. package/config/jest/matchers/toVueEmpty.js +16 -0
  26. package/config/jest/matchers/toVuexActionHasBeenDispatched.js +19 -0
  27. package/config/jest/setupMatchers.js +4 -0
  28. package/config/jest/setupTests.js +32 -0
  29. package/config/jest/typing.d.ts +14 -0
  30. package/config/svgo.js +22 -0
  31. package/config/webpack/example.config.js +86 -0
  32. package/config/webpack/loaders/index.js +6 -0
  33. package/config/webpack/loaders/js-loader.js +5 -0
  34. package/config/webpack/loaders/style-loader.js +7 -0
  35. package/config/webpack/loaders/svg-loader.js +4 -0
  36. package/config/webpack/loaders/vue-loader.js +4 -0
  37. package/config/webpack/settings.js +9 -0
  38. package/example/ExampleApp.vue +136 -0
  39. package/example/example.html +17 -0
  40. package/example/example.js +19 -0
  41. package/example/fonts.js +474 -0
  42. package/example/presets.js +245 -0
  43. package/example/tooltip/Tooltip.js +241 -0
  44. package/example/tooltip/TooltipManager.js +132 -0
  45. package/example/tooltip/index.js +3 -0
  46. package/example/tooltip/modifiers/TooltipCloseOnScrollModifier.js +73 -0
  47. package/example/tooltip/modifiers/index.js +1 -0
  48. package/example/tooltip/tooltip.css +95 -0
  49. package/jest.config.js +17 -0
  50. package/lib/Wysiwyg.vue +156 -0
  51. package/lib/__mocks__/svgMock.js +1 -0
  52. package/lib/__tests__/utils/NodeFactory.js +67 -0
  53. package/lib/__tests__/utils/index.js +4 -0
  54. package/lib/__tests__/utils/setReadonlyProperty.js +9 -0
  55. package/lib/__tests__/utils/waitAsyncOperation.js +6 -0
  56. package/lib/__tests__/utils/withComponentContext.js +14 -0
  57. package/lib/assets/icons.svg +69 -0
  58. package/lib/components/base/Button.vue +117 -0
  59. package/lib/components/base/ButtonToggle.vue +40 -0
  60. package/lib/components/base/FieldLabel.vue +28 -0
  61. package/lib/components/base/Icon.vue +67 -0
  62. package/lib/components/base/Modal.vue +116 -0
  63. package/lib/components/base/NumberField.vue +242 -0
  64. package/lib/components/base/Range.vue +196 -0
  65. package/lib/components/base/ScrollView.vue +60 -0
  66. package/lib/components/base/__tests__/Button.test.js +50 -0
  67. package/lib/components/base/__tests__/Icon.test.js +56 -0
  68. package/lib/components/base/__tests__/Modal.test.js +69 -0
  69. package/lib/components/base/__tests__/Range.test.js +39 -0
  70. package/lib/components/base/colorPicker/ColorPicker.vue +93 -0
  71. package/lib/components/base/colorPicker/composables/__tests__/usePickerApi.test.js +81 -0
  72. package/lib/components/base/colorPicker/composables/index.js +2 -0
  73. package/lib/components/base/colorPicker/composables/usePickerApi.js +35 -0
  74. package/lib/components/base/colorPicker/composables/usePickerHotkeys.js +25 -0
  75. package/lib/components/base/colorPicker/index.js +1 -0
  76. package/lib/components/base/composables/__tests__/useActivatedListener.test.js +89 -0
  77. package/lib/components/base/composables/__tests__/useDeselectionLock.test.js +80 -0
  78. package/lib/components/base/composables/__tests__/useElementRef.test.js +29 -0
  79. package/lib/components/base/composables/__tests__/useModalToggler.test.js +55 -0
  80. package/lib/components/base/composables/__tests__/useNumberValue.test.js +153 -0
  81. package/lib/components/base/composables/__tests__/useScrollView.test.js +43 -0
  82. package/lib/components/base/composables/__tests__/useTempValue.test.js +38 -0
  83. package/lib/components/base/composables/index.js +7 -0
  84. package/lib/components/base/composables/useActivatedListener.js +23 -0
  85. package/lib/components/base/composables/useDeselectionLock.js +35 -0
  86. package/lib/components/base/composables/useElementRef.js +5 -0
  87. package/lib/components/base/composables/useModalToggler.js +58 -0
  88. package/lib/components/base/composables/useNumberValue.js +53 -0
  89. package/lib/components/base/composables/useScrollView.js +22 -0
  90. package/lib/components/base/composables/useTempValue.js +11 -0
  91. package/lib/components/base/dropdown/Dropdown.vue +88 -0
  92. package/lib/components/base/dropdown/DropdownActivator.vue +81 -0
  93. package/lib/components/base/dropdown/DropdownDivider.vue +21 -0
  94. package/lib/components/base/dropdown/DropdownGroup.vue +55 -0
  95. package/lib/components/base/dropdown/DropdownMenu.vue +62 -0
  96. package/lib/components/base/dropdown/DropdownOption.vue +91 -0
  97. package/lib/components/base/dropdown/__tests__/DropdownActivator.test.js +54 -0
  98. package/lib/components/base/dropdown/__tests__/DropdownMenu.test.js +67 -0
  99. package/lib/components/base/dropdown/__tests__/DropdownOption.test.js +81 -0
  100. package/lib/components/base/dropdown/composables/__tests__/useActiveOptionManager.test.js +41 -0
  101. package/lib/components/base/dropdown/composables/__tests__/useDropdownEntityTitle.test.js +24 -0
  102. package/lib/components/base/dropdown/composables/index.js +2 -0
  103. package/lib/components/base/dropdown/composables/useActiveOptionManager.js +25 -0
  104. package/lib/components/base/dropdown/composables/useDropdownEntityTitle.js +5 -0
  105. package/lib/components/base/dropdown/index.js +2 -0
  106. package/lib/components/base/dropdown/injectionTokens.js +5 -0
  107. package/lib/components/base/index.js +11 -0
  108. package/lib/components/index.js +1 -0
  109. package/lib/components/toolbar/Toolbar.vue +56 -0
  110. package/lib/components/toolbar/ToolbarDevice.vue +35 -0
  111. package/lib/components/toolbar/ToolbarDivider.vue +50 -0
  112. package/lib/components/toolbar/ToolbarFull.vue +94 -0
  113. package/lib/components/toolbar/ToolbarGroup.vue +18 -0
  114. package/lib/components/toolbar/ToolbarRow.vue +19 -0
  115. package/lib/components/toolbar/__tests__/Toolbar.test.js +33 -0
  116. package/lib/components/toolbar/__tests__/ToolbarDivider.test.js +26 -0
  117. package/lib/components/toolbar/controls/AlignmentControl.vue +72 -0
  118. package/lib/components/toolbar/controls/AlignmentDeviceControl.vue +67 -0
  119. package/lib/components/toolbar/controls/BackgroundColorControl.vue +48 -0
  120. package/lib/components/toolbar/controls/CaseStyleControl.vue +54 -0
  121. package/lib/components/toolbar/controls/FontColorControl.vue +48 -0
  122. package/lib/components/toolbar/controls/FontFamilyControl.vue +96 -0
  123. package/lib/components/toolbar/controls/FontSizeControl.vue +45 -0
  124. package/lib/components/toolbar/controls/FontWeightControl.vue +43 -0
  125. package/lib/components/toolbar/controls/ItalicControl.vue +47 -0
  126. package/lib/components/toolbar/controls/LineHeightControl.vue +102 -0
  127. package/lib/components/toolbar/controls/ListControl.vue +86 -0
  128. package/lib/components/toolbar/controls/RemoveFormatControl.vue +37 -0
  129. package/lib/components/toolbar/controls/StrikeThroughControl.vue +44 -0
  130. package/lib/components/toolbar/controls/StylePresetControl.vue +95 -0
  131. package/lib/components/toolbar/controls/SuperscriptControl.vue +44 -0
  132. package/lib/components/toolbar/controls/UnderlineControl.vue +44 -0
  133. package/lib/components/toolbar/controls/__tests__/AlignmentControl.test.js +51 -0
  134. package/lib/components/toolbar/controls/__tests__/AlignmentDeviceControl.test.js +77 -0
  135. package/lib/components/toolbar/controls/__tests__/BackgroundColorControl.test.js +59 -0
  136. package/lib/components/toolbar/controls/__tests__/CaseStyleControl.test.js +43 -0
  137. package/lib/components/toolbar/controls/__tests__/FontColorControl.test.js +59 -0
  138. package/lib/components/toolbar/controls/__tests__/FontFamilyControl.test.js +57 -0
  139. package/lib/components/toolbar/controls/__tests__/FontSizeControl.test.js +47 -0
  140. package/lib/components/toolbar/controls/__tests__/FontWeightControl.test.js +45 -0
  141. package/lib/components/toolbar/controls/__tests__/ItalicControl.test.js +63 -0
  142. package/lib/components/toolbar/controls/__tests__/LineHeightControl.test.js +120 -0
  143. package/lib/components/toolbar/controls/__tests__/ListControl.test.js +82 -0
  144. package/lib/components/toolbar/controls/__tests__/RemoveFormatControl.test.js +34 -0
  145. package/lib/components/toolbar/controls/__tests__/StrikeThroughControl.test.js +44 -0
  146. package/lib/components/toolbar/controls/__tests__/StylePresetControl.test.js +129 -0
  147. package/lib/components/toolbar/controls/__tests__/SuperscriptControl.test.js +44 -0
  148. package/lib/components/toolbar/controls/__tests__/UnderlineControl.test.js +44 -0
  149. package/lib/components/toolbar/controls/composables/__tests__/useRecentFonts.test.js +69 -0
  150. package/lib/components/toolbar/controls/composables/index.js +1 -0
  151. package/lib/components/toolbar/controls/composables/useRecentFonts.js +18 -0
  152. package/lib/components/toolbar/controls/index.js +16 -0
  153. package/lib/components/toolbar/index.js +1 -0
  154. package/lib/composables/__tests__/__snapshots__/useEditor.test.js.snap +24 -0
  155. package/lib/composables/__tests__/useEditor.test.js +67 -0
  156. package/lib/composables/__tests__/useToolbar.test.js +56 -0
  157. package/lib/composables/index.js +2 -0
  158. package/lib/composables/useEditor.js +21 -0
  159. package/lib/composables/useToolbar.js +44 -0
  160. package/lib/directives/__tests__/outClick.test.js +86 -0
  161. package/lib/directives/__tests__/tooltip.test.js +39 -0
  162. package/lib/directives/index.js +2 -0
  163. package/lib/directives/outClick.js +37 -0
  164. package/lib/directives/tooltip.js +7 -0
  165. package/lib/enums/Alignments.js +6 -0
  166. package/lib/enums/CaseStyles.js +5 -0
  167. package/lib/enums/Devices.js +15 -0
  168. package/lib/enums/ListTypes.js +23 -0
  169. package/lib/enums/NodeTypes.js +12 -0
  170. package/lib/enums/TextSettings.js +30 -0
  171. package/lib/enums/index.js +6 -0
  172. package/lib/extensions/Alignment.js +67 -0
  173. package/lib/extensions/BackgroundColor.js +28 -0
  174. package/lib/extensions/CaseStyle.js +36 -0
  175. package/lib/extensions/DeviceManager.js +16 -0
  176. package/lib/extensions/FontColor.js +36 -0
  177. package/lib/extensions/FontFamily.js +62 -0
  178. package/lib/extensions/FontSize.js +74 -0
  179. package/lib/extensions/FontStyle.js +62 -0
  180. package/lib/extensions/FontWeight.js +56 -0
  181. package/lib/extensions/LineHeight.js +60 -0
  182. package/lib/extensions/StylePreset.js +168 -0
  183. package/lib/extensions/Superscript.js +5 -0
  184. package/lib/extensions/TextDecoration.js +97 -0
  185. package/lib/extensions/__tests__/Alignment.test.js +107 -0
  186. package/lib/extensions/__tests__/BackgroundColor.test.js +75 -0
  187. package/lib/extensions/__tests__/CaseStyle.test.js +58 -0
  188. package/lib/extensions/__tests__/FontColor.test.js +85 -0
  189. package/lib/extensions/__tests__/FontFamily.test.js +171 -0
  190. package/lib/extensions/__tests__/FontSize.test.js +183 -0
  191. package/lib/extensions/__tests__/FontStyle.test.js +136 -0
  192. package/lib/extensions/__tests__/FontWeight.test.js +151 -0
  193. package/lib/extensions/__tests__/LineHeight.test.js +106 -0
  194. package/lib/extensions/__tests__/StylePreset.test.js +400 -0
  195. package/lib/extensions/__tests__/TextDecoration.test.js +258 -0
  196. package/lib/extensions/__tests__/__snapshots__/Alignment.test.js.snap +29 -0
  197. package/lib/extensions/__tests__/__snapshots__/BackgroundColor.test.js.snap +28 -0
  198. package/lib/extensions/__tests__/__snapshots__/CaseStyle.test.js.snap +69 -0
  199. package/lib/extensions/__tests__/__snapshots__/FontColor.test.js.snap +28 -0
  200. package/lib/extensions/__tests__/__snapshots__/FontFamily.test.js.snap +158 -0
  201. package/lib/extensions/__tests__/__snapshots__/FontSize.test.js.snap +140 -0
  202. package/lib/extensions/__tests__/__snapshots__/FontStyle.test.js.snap +87 -0
  203. package/lib/extensions/__tests__/__snapshots__/FontWeight.test.js.snap +140 -0
  204. package/lib/extensions/__tests__/__snapshots__/LineHeight.test.js.snap +29 -0
  205. package/lib/extensions/__tests__/__snapshots__/StylePreset.test.js.snap +310 -0
  206. package/lib/extensions/__tests__/__snapshots__/TextDecoration.test.js.snap +206 -0
  207. package/lib/extensions/core/NodeProcessor.js +32 -0
  208. package/lib/extensions/core/SelectionProcessor.js +37 -0
  209. package/lib/extensions/core/TextProcessor.js +57 -0
  210. package/lib/extensions/core/__tests__/NodeProcessor.test.js +120 -0
  211. package/lib/extensions/core/__tests__/SelectionProcessor.test.js +78 -0
  212. package/lib/extensions/core/__tests__/TextProcessor.test.js +61 -0
  213. package/lib/extensions/core/__tests__/__snapshots__/NodeProcessor.test.js.snap +93 -0
  214. package/lib/extensions/core/__tests__/__snapshots__/TextProcessor.test.js.snap +43 -0
  215. package/lib/extensions/core/index.js +17 -0
  216. package/lib/extensions/core/inputRules/closeDoubleQuote.js +6 -0
  217. package/lib/extensions/core/inputRules/closeSingleQuote.js +6 -0
  218. package/lib/extensions/core/inputRules/copyright.js +6 -0
  219. package/lib/extensions/core/inputRules/ellipsis.js +6 -0
  220. package/lib/extensions/core/inputRules/emDash.js +6 -0
  221. package/lib/extensions/core/inputRules/index.js +9 -0
  222. package/lib/extensions/core/inputRules/openDoubleQuote.js +6 -0
  223. package/lib/extensions/core/inputRules/openSingleQuote.js +6 -0
  224. package/lib/extensions/core/inputRules/registeredTrademark.js +6 -0
  225. package/lib/extensions/core/inputRules/trademark.js +6 -0
  226. package/lib/extensions/index.js +49 -0
  227. package/lib/extensions/list/List.js +81 -0
  228. package/lib/extensions/list/ListItem.js +12 -0
  229. package/lib/extensions/list/__tests__/List.test.js +130 -0
  230. package/lib/extensions/list/__tests__/__snapshots__/List.test.js.snap +212 -0
  231. package/lib/extensions/list/index.js +1 -0
  232. package/lib/index.js +1 -0
  233. package/lib/injectionTokens.js +7 -0
  234. package/lib/models/Font.js +37 -0
  235. package/lib/models/__tests__/Font.test.js +58 -0
  236. package/lib/models/index.js +1 -0
  237. package/lib/services/FavoriteColors.js +6 -0
  238. package/lib/services/JsonSerializer.js +15 -0
  239. package/lib/services/Storage.js +49 -0
  240. package/lib/services/index.js +3 -0
  241. package/lib/styles/content.css +39 -0
  242. package/lib/styles/helpers/common.css +3 -0
  243. package/lib/styles/helpers/offsets.css +3 -0
  244. package/lib/styles/helpers/text.css +6 -0
  245. package/lib/styles/main.css +5 -0
  246. package/lib/styles/variables.css +57 -0
  247. package/lib/utils/__tests__/__snapshots__/renderInlineSetting.test.js.snap +40 -0
  248. package/lib/utils/__tests__/capitalize.test.js +11 -0
  249. package/lib/utils/__tests__/renderInlineSetting.test.js +39 -0
  250. package/lib/utils/capitalize.js +3 -0
  251. package/lib/utils/createCommand.js +3 -0
  252. package/lib/utils/createKeyboardShortcut.js +6 -0
  253. package/lib/utils/index.js +4 -0
  254. package/lib/utils/renderInlineSetting.js +17 -0
  255. package/package.json +75 -0
@@ -0,0 +1,55 @@
1
+ import { ref } from '@vue/composition-api';
2
+ import { withComponentContext } from '../../../../__tests__/utils';
3
+ import { InjectionTokens } from '../../../../injectionTokens';
4
+ import { useModalToggler } from '../useModalToggler';
5
+ import { useElementRef } from '../useElementRef';
6
+
7
+ const createEditor = () => ({
8
+ commands: {
9
+ storeSelection: jest.fn(),
10
+ restoreSelection: jest.fn()
11
+ }
12
+ });
13
+
14
+ const el = document.createElement('div');
15
+ const elementRef = useElementRef(ref(el));
16
+
17
+ function createWrapper() {
18
+ return withComponentContext(() => {
19
+ return useModalToggler({
20
+ onClosed: jest.fn(),
21
+ onBeforeOpened: jest.fn(),
22
+ wrapperRef: elementRef,
23
+ modalRef: elementRef
24
+ });
25
+ }, {
26
+ provide: { [InjectionTokens.EDITOR]: createEditor() }
27
+ });
28
+ }
29
+
30
+ describe('manage opened state', () => {
31
+ test('should open modal', () => {
32
+ const toggler = createWrapper();
33
+
34
+ toggler.open();
35
+
36
+ expect(toggler.isOpened.value).toBe(true);
37
+ });
38
+
39
+ test('should close modal', () => {
40
+ const toggler = createWrapper();
41
+
42
+ toggler.open();
43
+ toggler.close();
44
+
45
+ expect(toggler.isOpened.value).toBe(false);
46
+ });
47
+
48
+ test('should toggle modal', () => {
49
+ const toggler = createWrapper();
50
+
51
+ toggler.toggle(true);
52
+
53
+ expect(toggler.isOpened.value).toBe(true);
54
+ });
55
+ });
@@ -0,0 +1,153 @@
1
+ import { ref } from '@vue/composition-api';
2
+ import { useNumberValue } from '../useNumberValue';
3
+
4
+ describe('change', () => {
5
+ test('should set value', () => {
6
+ const number = useNumberValue({
7
+ min: 1,
8
+ max: 10,
9
+ digits: 2,
10
+ onChange: jest.fn(),
11
+ valueRef: ref(5)
12
+ });
13
+
14
+ number.change('7');
15
+
16
+ expect(number.temp.value).toBe(7);
17
+ });
18
+
19
+ test('should set min value if passed empty', () => {
20
+ const number = useNumberValue({
21
+ min: 1,
22
+ max: 10,
23
+ digits: 2,
24
+ onChange: jest.fn(),
25
+ valueRef: ref(5)
26
+ });
27
+
28
+ number.change('');
29
+
30
+ expect(number.temp.value).toBe(1);
31
+ });
32
+
33
+ test('should set min value', () => {
34
+ const number = useNumberValue({
35
+ min: 1,
36
+ max: 10,
37
+ digits: 2,
38
+ onChange: jest.fn(),
39
+ valueRef: ref(5)
40
+ });
41
+
42
+ number.change('-10');
43
+
44
+ expect(number.temp.value).toBe(1);
45
+ });
46
+
47
+ test('should set max value', () => {
48
+ const number = useNumberValue({
49
+ min: 1,
50
+ max: 10,
51
+ digits: 2,
52
+ onChange: jest.fn(),
53
+ valueRef: ref(5)
54
+ });
55
+
56
+ number.change('1000');
57
+
58
+ expect(number.temp.value).toBe(10);
59
+ });
60
+
61
+ test('should format float value', () => {
62
+ const number = useNumberValue({
63
+ min: 1,
64
+ max: 10,
65
+ digits: 2,
66
+ onChange: jest.fn(),
67
+ valueRef: ref(5)
68
+ });
69
+
70
+ number.change('1.123');
71
+
72
+ expect(number.temp.value).toBe(1.12);
73
+ });
74
+
75
+ test('should format integer value', () => {
76
+ const number = useNumberValue({
77
+ min: 1,
78
+ max: 10,
79
+ digits: 2,
80
+ onChange: jest.fn(),
81
+ valueRef: ref(5)
82
+ });
83
+
84
+ number.change('1.00');
85
+
86
+ expect(number.temp.value).toBe(1);
87
+ });
88
+ });
89
+
90
+ describe('increment', () => {
91
+ test('should increment value', () => {
92
+ const number = useNumberValue({
93
+ min: 1,
94
+ max: 10,
95
+ digits: 2,
96
+ onChange: jest.fn(),
97
+ valueRef: ref(5)
98
+ });
99
+
100
+ number.increment(1);
101
+
102
+ expect(number.temp.value).toBe(6);
103
+ });
104
+
105
+ test('should set max value', () => {
106
+ const number = useNumberValue({
107
+ min: 1,
108
+ max: 10,
109
+ digits: 2,
110
+ onChange: jest.fn(),
111
+ valueRef: ref(9)
112
+ });
113
+
114
+ number.increment(1);
115
+ number.increment(1);
116
+ number.increment(1);
117
+
118
+ expect(number.temp.value).toBe(10);
119
+ });
120
+ });
121
+
122
+
123
+ describe('decrement', () => {
124
+ test('should decrement value', () => {
125
+ const number = useNumberValue({
126
+ min: 1,
127
+ max: 10,
128
+ digits: 2,
129
+ onChange: jest.fn(),
130
+ valueRef: ref(5)
131
+ });
132
+
133
+ number.decrement(1);
134
+
135
+ expect(number.temp.value).toBe(4);
136
+ });
137
+
138
+ test('should set min value', () => {
139
+ const number = useNumberValue({
140
+ min: 1,
141
+ max: 10,
142
+ digits: 2,
143
+ onChange: jest.fn(),
144
+ valueRef: ref(2)
145
+ });
146
+
147
+ number.decrement(1);
148
+ number.decrement(1);
149
+ number.decrement(1);
150
+
151
+ expect(number.temp.value).toBe(1);
152
+ });
153
+ });
@@ -0,0 +1,43 @@
1
+ import { ref } from '@vue/composition-api';
2
+ import { SCROLL_VIEW, useScrollView } from '../useScrollView';
3
+ import { withComponentContext } from '../../../../__tests__/utils';
4
+
5
+ function useComposable({ scrollerEl }) {
6
+ return withComponentContext(() => useScrollView(), {
7
+ provide: { [SCROLL_VIEW]: ref(scrollerEl) }
8
+ });
9
+ }
10
+
11
+ const createScrollerEl = () => document.createElement('div');
12
+
13
+ beforeEach(() => HTMLElement.prototype.scrollTo.mockClear());
14
+
15
+ describe('scroll to element', () => {
16
+ const createScrollTargetEl = () => document.createElement('div');
17
+
18
+ test('should scroll to element top', () => {
19
+ const scrollerEl = createScrollerEl();
20
+ const scrollTargetEl = createScrollTargetEl();
21
+ const scrollView = useComposable({ scrollerEl });
22
+
23
+ jest.spyOn(scrollerEl, 'getBoundingClientRect').mockReturnValueOnce({ top: 100 });
24
+ jest.spyOn(scrollTargetEl, 'getBoundingClientRect').mockReturnValueOnce({ top: 150 });
25
+
26
+ scrollView.scrollToElement(scrollTargetEl);
27
+
28
+ expect(scrollerEl.scrollTo).toHaveBeenCalledWith({ top: 50 });
29
+ });
30
+
31
+ test('should scroll to element top with offset', () => {
32
+ const scrollerEl = createScrollerEl();
33
+ const scrollTargetEl = createScrollTargetEl();
34
+ const scrollView = useComposable({ scrollerEl });
35
+
36
+ jest.spyOn(scrollerEl, 'getBoundingClientRect').mockReturnValueOnce({ top: 100 });
37
+ jest.spyOn(scrollTargetEl, 'getBoundingClientRect').mockReturnValueOnce({ top: 150 });
38
+
39
+ scrollView.scrollToElement(scrollTargetEl, { offset: 33 });
40
+
41
+ expect(scrollerEl.scrollTo).toHaveBeenCalledWith({ top: 17 });
42
+ });
43
+ });
@@ -0,0 +1,38 @@
1
+ import { nextTick, ref } from '@vue/composition-api';
2
+ import { useTempValue } from '../useTempValue';
3
+
4
+ describe('sync with original', () => {
5
+ test('should sync on create', () => {
6
+ const valueRef = ref(123);
7
+ const temp = useTempValue({ valueRef });
8
+
9
+ expect(temp.value).toBe(123);
10
+ });
11
+
12
+ test('should format original on create', () => {
13
+ const valueRef = ref('123');
14
+ const temp = useTempValue({ valueRef, format: Number });
15
+
16
+ expect(temp.value).toBe(123);
17
+ });
18
+
19
+ test('should sync on change', async () => {
20
+ const valueRef = ref('old');
21
+ const temp = useTempValue({ valueRef });
22
+
23
+ valueRef.value = 'new';
24
+ await nextTick();
25
+
26
+ expect(temp.value).toBe('new');
27
+ });
28
+
29
+ test('should format original on change', async () => {
30
+ const valueRef = ref('100');
31
+ const temp = useTempValue({ valueRef, format: Number });
32
+
33
+ valueRef.value = '123';
34
+ await nextTick();
35
+
36
+ expect(temp.value).toBe(123);
37
+ });
38
+ });
@@ -0,0 +1,7 @@
1
+ export { useTempValue } from './useTempValue';
2
+ export { useElementRef } from './useElementRef';
3
+ export { useNumberValue } from './useNumberValue';
4
+ export { useModalToggler } from './useModalToggler';
5
+ export { useActivatedListener } from './useActivatedListener';
6
+ export { useDeselectionLock } from './useDeselectionLock';
7
+ export { useScrollView, SCROLL_VIEW } from './useScrollView';
@@ -0,0 +1,23 @@
1
+ import { onUnmounted, watch } from '@vue/composition-api';
2
+
3
+ export function useActivatedListener({ targetRef, isActiveRef, event, onEvent, options }) {
4
+ function addListener() {
5
+ targetRef.value.addEventListener(event, onEvent, options);
6
+ }
7
+
8
+ function removeListener() {
9
+ targetRef.value?.removeEventListener(event, onEvent, options);
10
+ }
11
+
12
+ function updateListener() {
13
+ if (!targetRef.value) return;
14
+
15
+ isActiveRef.value ? addListener() : removeListener();
16
+ }
17
+
18
+ watch(isActiveRef, updateListener);
19
+ watch(targetRef, updateListener);
20
+ onUnmounted(removeListener);
21
+
22
+ updateListener();
23
+ }
@@ -0,0 +1,35 @@
1
+ import { inject } from '@vue/composition-api';
2
+ import { InjectionTokens } from '../../../injectionTokens';
3
+ import { useActivatedListener } from './useActivatedListener';
4
+ import { useElementRef } from './useElementRef';
5
+
6
+ const INTERACTIVE_TAGS = [
7
+ 'BUTTON',
8
+ 'INPUT',
9
+ 'LABEL',
10
+ 'A'
11
+ ];
12
+
13
+ // Prevent wswg text deselection
14
+ export function useDeselectionLock({ hostRef, isActiveRef }) {
15
+ const editor = inject(InjectionTokens.EDITOR);
16
+ const targetRef = useElementRef(hostRef);
17
+
18
+ function onEvent(event) {
19
+ if (INTERACTIVE_TAGS.includes(event.target.tagName)) return;
20
+
21
+ if (editor.isFocused) {
22
+ event.stopPropagation();
23
+ event.preventDefault();
24
+ }
25
+
26
+ editor.chain().restoreSelection().run();
27
+ }
28
+
29
+ useActivatedListener({
30
+ event: 'mousedown',
31
+ targetRef,
32
+ isActiveRef,
33
+ onEvent
34
+ });
35
+ }
@@ -0,0 +1,5 @@
1
+ import { computed } from '@vue/composition-api';
2
+
3
+ export function useElementRef(ref) {
4
+ return computed(() => ref.value?.$el || ref.value);
5
+ }
@@ -0,0 +1,58 @@
1
+ import { createPopper } from '@popperjs/core';
2
+ import { inject, nextTick, ref, onUnmounted } from '@vue/composition-api';
3
+ import { InjectionTokens } from '../../../injectionTokens';
4
+ import { useElementRef } from './useElementRef';
5
+
6
+ export function useModalToggler({ onBeforeOpened, onClosed, wrapperRef, modalRef } = {}) {
7
+ const editor = inject(InjectionTokens.EDITOR);
8
+ const isOpened = ref(false);
9
+ let popper;
10
+
11
+ function initPopper() {
12
+ const wrapperEl = useElementRef(wrapperRef);
13
+ const modalEl = useElementRef(modalRef);
14
+
15
+ popper = createPopper(wrapperEl.value, modalEl.value, {
16
+ placement: 'bottom',
17
+ modifiers: [
18
+ {
19
+ name: 'offset',
20
+ options: {
21
+ offset: [0, 4]
22
+ }
23
+ },
24
+
25
+ {
26
+ name: 'preventOverflow',
27
+ options: {
28
+ padding: 16
29
+ }
30
+ }
31
+ ]
32
+ });
33
+ }
34
+
35
+ async function open() {
36
+ onBeforeOpened?.();
37
+ editor.commands.storeSelection();
38
+ isOpened.value = true;
39
+
40
+ await nextTick();
41
+
42
+ initPopper();
43
+ }
44
+
45
+ function close() {
46
+ isOpened.value = false;
47
+ editor.commands.restoreSelection();
48
+ onClosed?.();
49
+ }
50
+
51
+ onUnmounted(() => {
52
+ popper?.destroy();
53
+ });
54
+
55
+ const toggle = (toOpen) => toOpen ? open() : close();
56
+
57
+ return { isOpened, open, close, toggle };
58
+ }
@@ -0,0 +1,53 @@
1
+ import { useTempValue } from './useTempValue';
2
+
3
+ export function useNumberValue({ valueRef, digits, min, max, onChange }) {
4
+ function format(number) {
5
+ return Number(parseFloat(number).toFixed(digits));
6
+ }
7
+
8
+ const temp = useTempValue({ valueRef, format });
9
+
10
+ function validateNumber(number) {
11
+ if (min !== null && number < min) return min;
12
+ if (max !== null && number > max) return max;
13
+
14
+ return number;
15
+ }
16
+
17
+ function change(value) {
18
+ temp.value = value;
19
+
20
+ if (temp.value === '') {
21
+ temp.value = min ?? 1;
22
+ }
23
+
24
+ const numericValue = Number.parseFloat(temp.value);
25
+ const validatedNumber = validateNumber(numericValue);
26
+
27
+ temp.value = format(validatedNumber);
28
+ onChange(temp.value);
29
+ }
30
+
31
+ function increment(step) {
32
+ const newValue = temp.value + Number(step);
33
+ const validatedNumber = validateNumber(newValue);
34
+
35
+ temp.value = format(validatedNumber);
36
+ onChange(temp.value);
37
+ }
38
+
39
+ function decrement(step) {
40
+ const newValue = temp.value - Number(step);
41
+ const validatedNumber = validateNumber(newValue);
42
+
43
+ temp.value = format(validatedNumber);
44
+ onChange(temp.value);
45
+ }
46
+
47
+ return {
48
+ temp,
49
+ change,
50
+ increment,
51
+ decrement
52
+ };
53
+ }
@@ -0,0 +1,22 @@
1
+ import { inject } from '@vue/composition-api';
2
+
3
+ export const SCROLL_VIEW = Symbol('scrollView');
4
+
5
+ export function useScrollView() {
6
+ const scrollerRef = inject(SCROLL_VIEW);
7
+
8
+ function scrollToElement(element, { offset } = {}) {
9
+ if (!scrollerRef.value) return;
10
+
11
+ const rootRect = scrollerRef.value.getBoundingClientRect();
12
+ const elementRect = element.getBoundingClientRect();
13
+ const position = elementRect.top - rootRect.top;
14
+
15
+ scrollerRef.value.scrollTo({
16
+ top: position - (offset || 0)
17
+ });
18
+ }
19
+
20
+ return { scrollToElement };
21
+ }
22
+
@@ -0,0 +1,11 @@
1
+ import { ref, watch } from '@vue/composition-api';
2
+
3
+ const noFormatter = (value) => value;
4
+
5
+ export function useTempValue({ valueRef, format = noFormatter }) {
6
+ const tempValue = ref(format(valueRef.value));
7
+
8
+ watch(valueRef, () => tempValue.value = format(valueRef.value));
9
+
10
+ return tempValue;
11
+ }
@@ -0,0 +1,88 @@
1
+ <template>
2
+ <div class="zw-dropdown" ref="dropdownRef">
3
+ <DropdownActivator>
4
+ <template #default="attrs">
5
+ <slot name="activator" v-bind="attrs" />
6
+ </template>
7
+ </DropdownActivator>
8
+
9
+ <Modal max-height="300" max-width="156" :toggler="toggler" ref="modalRef">
10
+ <DropdownMenu :options="options">
11
+ <template #option="attrs">
12
+ <slot name="option" v-bind="attrs" />
13
+ </template>
14
+ </DropdownMenu>
15
+ </Modal>
16
+ </div>
17
+ </template>
18
+
19
+ <script>
20
+ import { provide, toRef, ref } from '@vue/composition-api';
21
+ import Modal from '../Modal';
22
+ import { useModalToggler } from '../composables';
23
+ import { InjectionTokens } from './injectionTokens';
24
+ import DropdownActivator from './DropdownActivator';
25
+ import DropdownMenu from './DropdownMenu';
26
+ import { useActiveOptionManager } from './composables';
27
+
28
+ export default {
29
+ name: 'Dropdown',
30
+
31
+ components: {
32
+ Modal,
33
+ DropdownActivator,
34
+ DropdownMenu
35
+ },
36
+
37
+ model: {
38
+ event: 'change'
39
+ },
40
+
41
+ props: {
42
+ value: {
43
+ type: String,
44
+ required: false,
45
+ default: null
46
+ },
47
+
48
+ options: {
49
+ type: Array,
50
+ required: true
51
+ }
52
+ },
53
+
54
+ setup(props, { emit }) {
55
+ const dropdownRef = ref(null);
56
+ const modalRef = ref(null);
57
+
58
+ const activeOptionManager = useActiveOptionManager({
59
+ optionsRef: toRef(props, 'options'),
60
+ inputRef: toRef(props, 'value'),
61
+ stateless: props.value === null,
62
+ onChange: (value) => emit('change', value.id)
63
+ });
64
+
65
+ const toggler = useModalToggler({
66
+ wrapperRef: dropdownRef,
67
+ modalRef
68
+ });
69
+
70
+ provide(InjectionTokens.ACTIVE_MANAGER, activeOptionManager);
71
+ provide(InjectionTokens.TOGGLER, toggler);
72
+
73
+ return {
74
+ dropdownRef,
75
+ modalRef,
76
+ toggler
77
+ };
78
+ }
79
+ };
80
+ </script>
81
+
82
+ <style scoped>
83
+ .zw-dropdown {
84
+ position: relative;
85
+ font-size: var(--zw-font-size-xs);
86
+ line-height: var(--zw-line-height-xxs);
87
+ }
88
+ </style>
@@ -0,0 +1,81 @@
1
+ <template>
2
+ <div>
3
+ <slot :open="open" :isOpened="isOpened">
4
+ <Button
5
+ skin="toolbar"
6
+ class="zw-dropdown__activator"
7
+ :class="dropdownClasses"
8
+ :active="isOpened"
9
+ @click="open"
10
+ >
11
+ <span class="zw-dropdown__activator-title zw-text--truncate">
12
+ {{ activeOptionTitle }}
13
+ </span>
14
+
15
+ <Icon
16
+ class="zw-dropdown__activator-arrow"
17
+ name="arrow"
18
+ size="8px"
19
+ auto-color
20
+ />
21
+ </Button>
22
+ </slot>
23
+ </div>
24
+ </template>
25
+
26
+ <script>
27
+ import { computed, inject } from '@vue/composition-api';
28
+ import Button from '../Button';
29
+ import Icon from '../Icon';
30
+ import { InjectionTokens } from './injectionTokens';
31
+ import { useDropdownEntityTitle } from './composables';
32
+
33
+ export default {
34
+ name: 'DropdownActivator',
35
+
36
+ components: {
37
+ Icon,
38
+ Button
39
+ },
40
+
41
+ model: {
42
+ event: 'change'
43
+ },
44
+
45
+ setup() {
46
+ const activeOptionManager = inject(InjectionTokens.ACTIVE_MANAGER);
47
+ const dropdownToggler = inject(InjectionTokens.TOGGLER);
48
+
49
+ const activeOptionTitle = useDropdownEntityTitle(activeOptionManager.activeOption);
50
+
51
+ const dropdownClasses = computed(() => ({
52
+ 'zw-dropdown__activator--active': dropdownToggler.isOpened.value
53
+ }));
54
+
55
+ return {
56
+ activeOptionTitle,
57
+ isOpened: dropdownToggler.isOpened,
58
+ open: dropdownToggler.open,
59
+ dropdownClasses
60
+ };
61
+ }
62
+ };
63
+ </script>
64
+
65
+ <style scoped>
66
+ .zw-dropdown__activator {
67
+ width: 100%;
68
+ }
69
+
70
+ .zw-dropdown__activator-title {
71
+ margin-right: var(--zw-offset-xs);
72
+ }
73
+
74
+ .zw-dropdown__activator-arrow {
75
+ margin-left: auto;
76
+ }
77
+
78
+ .zw-dropdown__activator--active .zw-dropdown__activator-arrow {
79
+ transform: rotateX(180deg);
80
+ }
81
+ </style>