@zipify/wysiwyg 1.0.0-dev.8 → 1.0.0-dev.9

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.
@@ -29,6 +29,7 @@
29
29
  :make-preset-variable="$options.makePresetVariable"
30
30
  :favorite-colors="favoriteColors"
31
31
  :device="device"
32
+ :active="isActive"
32
33
  @updateFavoriteColors="updateFavoriteColors"
33
34
  />
34
35
  <pre class="zw-content-structure" v-html="structurePreview" />
@@ -66,6 +67,7 @@ export default {
66
67
  const presets = ref(PRESETS);
67
68
  const device = ref('desktop');
68
69
  const updatedAt = new Date(ZW_UPDATED_AT).toLocaleString('ua-UA');
70
+ const isActive = ref(false);
69
71
 
70
72
  const structurePreview = computed(() => {
71
73
  const json = JSON.stringify(content.value, null, ' ');
@@ -94,6 +96,10 @@ export default {
94
96
  }
95
97
  }
96
98
 
99
+ document.body.addEventListener('click', (event) => {
100
+ isActive.value = wswgRef.value.$el.contains(event.target);
101
+ });
102
+
97
103
  return {
98
104
  wswgRef,
99
105
  content,
@@ -104,7 +110,8 @@ export default {
104
110
  loadContent,
105
111
  device,
106
112
  updatedAt,
107
- presets
113
+ presets,
114
+ isActive
108
115
  };
109
116
  }
110
117
  };
package/lib/Wysiwyg.vue CHANGED
@@ -1,11 +1,7 @@
1
1
  <template>
2
- <div
3
- class="zw-wysiwyg"
4
- ref="wysiwygRef"
5
- v-out-click="{ onOutClick: hideToolbar, isDisabled: !toolbar.isShow.value }"
6
- >
2
+ <div class="zw-wysiwyg" ref="wysiwygRef">
7
3
  <Toolbar
8
- v-show="toolbar.isShow.value"
4
+ v-show="active"
9
5
  :editor="editor"
10
6
  :device="device"
11
7
  ref="toolbarRef"
@@ -72,6 +68,11 @@ export default {
72
68
  required: true
73
69
  },
74
70
 
71
+ active: {
72
+ type: Boolean,
73
+ required: true
74
+ },
75
+
75
76
  device: {
76
77
  type: String,
77
78
  required: false,
@@ -106,7 +107,8 @@ export default {
106
107
  const toolbar = useToolbar({
107
108
  wrapperRef: wysiwygRef,
108
109
  popperRef: toolbarRef,
109
- offsets: toRef(props, 'toolbarOffsets').value
110
+ isActiveRef: toRef(props, 'active'),
111
+ offsets: props.toolbarOffsets
110
112
  });
111
113
 
112
114
  function onChange(content) {
@@ -117,7 +119,6 @@ export default {
117
119
  const editor = useEditor({
118
120
  content: toRef(props, 'value'),
119
121
  onChange: (content) => onChange(content),
120
- onFocus: () => toolbar.show(),
121
122
 
122
123
  extensions: getExtensions({
123
124
  fonts,
@@ -132,12 +133,6 @@ export default {
132
133
  })
133
134
  });
134
135
 
135
- function hideToolbar() {
136
- if (editor.isFocused) return;
137
-
138
- toolbar.hide();
139
- }
140
-
141
136
  const fontSizes = new Array(MAX_FONT_SIZE - MIN_FONT_SIZE + 1)
142
137
  .fill(0)
143
138
  .map((_, index) => String(index + MIN_FONT_SIZE));
@@ -153,7 +148,7 @@ export default {
153
148
  provide(InjectionTokens.LOCAL_STORAGE, new Storage(localStorage));
154
149
  provide(InjectionTokens.FAVORITE_COLORS, favoriteColors);
155
150
 
156
- return { editor, toolbarRef, wysiwygRef, toolbar, hideToolbar };
151
+ return { editor, toolbarRef, wysiwygRef, toolbar };
157
152
  }
158
153
  };
159
154
  </script>
@@ -2,13 +2,12 @@ import { Editor } from '@tiptap/vue-2';
2
2
  import { onUnmounted, watch, reactive } from '@vue/composition-api';
3
3
  import { ContentNormalizer } from '../services';
4
4
 
5
- export function useEditor({ content, onChange, onFocus, extensions }) {
5
+ export function useEditor({ content, onChange, extensions }) {
6
6
  const normalizer = new ContentNormalizer();
7
7
 
8
8
  const editor = reactive(new Editor({
9
9
  content: normalizer.normalize(content.value),
10
10
  onUpdate: () => onChange(editor.getJSON()),
11
- onFocus: () => onFocus(),
12
11
  extensions
13
12
  }));
14
13
 
@@ -1,10 +1,9 @@
1
1
  import { createPopper } from '@popperjs/core';
2
- import { onUnmounted, onMounted, ref } from '@vue/composition-api';
2
+ import { onUnmounted, onMounted, watch } from '@vue/composition-api';
3
3
  import { useElementRef } from '../components/base/composables';
4
4
 
5
- export function useToolbar({ wrapperRef, popperRef, offsets }) {
5
+ export function useToolbar({ wrapperRef, popperRef, offsets, isActiveRef }) {
6
6
  let toolbar;
7
- const isShow = ref(false);
8
7
 
9
8
  onMounted(() => {
10
9
  const wrapperEl = useElementRef(wrapperRef).value;
@@ -22,23 +21,13 @@ export function useToolbar({ wrapperRef, popperRef, offsets }) {
22
21
  ]
23
22
  });
24
23
  });
24
+ const update = () => toolbar.update();
25
25
 
26
- onUnmounted(() => {
27
- toolbar.destroy();
26
+ watch(isActiveRef, () => {
27
+ if (isActiveRef.value) update();
28
28
  });
29
29
 
30
- function hide() {
31
- isShow.value = false;
32
- }
30
+ onUnmounted(() => toolbar.destroy());
33
31
 
34
- function show() {
35
- isShow.value = true;
36
- this.update();
37
- }
38
-
39
- function update() {
40
- toolbar.update();
41
- }
42
-
43
- return { toolbar, show, hide, update, isShow };
32
+ return { toolbar, update };
44
33
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zipify/wysiwyg",
3
- "version": "1.0.0-dev.8",
3
+ "version": "1.0.0-dev.9",
4
4
  "description": "Zipify modification of TipTap text editor",
5
5
  "main": "dist/wysiwyg.js",
6
6
  "repository": {
@@ -21,7 +21,7 @@
21
21
  "optimize-svg": "svgo --config ./config/svgo.js",
22
22
  "gzip": "gzipper compress",
23
23
  "prepare": "husky install",
24
- "release": "export $(cat ./.env | xargs) && release-it"
24
+ "release": "export $(cat ./.env | xargs) && npm run lib:build && release-it"
25
25
  },
26
26
  "dependencies": {
27
27
  "@popperjs/core": "^2.11.5",
@@ -1,56 +0,0 @@
1
- import { createPopper } from '@popperjs/core';
2
- import { ref, nextTick } from '@vue/composition-api';
3
- import { withComponentContext } from '../../__tests__/utils';
4
- import { useToolbar } from '../useToolbar';
5
- import { useElementRef } from '../../components/base/composables';
6
-
7
- jest.mock('@popperjs/core', () => ({
8
- createPopper: jest.fn(() => ({
9
- update: jest.fn()
10
- }))
11
- }));
12
-
13
- function createWrapper() {
14
- const el = document.createElement('div');
15
- const elementRef = useElementRef(ref(el));
16
-
17
- return withComponentContext(() => {
18
- return useToolbar({
19
- wrapperRef: elementRef,
20
- popperRef: elementRef
21
- });
22
- });
23
- }
24
-
25
- describe('manage opened state', () => {
26
- test('should show toolbar', () => {
27
- const toolbarComponent = createWrapper();
28
-
29
- toolbarComponent.show();
30
-
31
- expect(toolbarComponent.isShow.value).toBe(true);
32
- });
33
-
34
- test('should hide toolbar', () => {
35
- const toolbarComponent = createWrapper();
36
-
37
- toolbarComponent.show();
38
- toolbarComponent.hide();
39
-
40
- expect(toolbarComponent.isShow.value).toBe(false);
41
- });
42
-
43
- test('should update toolbar on open', async () => {
44
- const popper = { update: jest.fn() };
45
-
46
- createPopper.mockReturnValueOnce(popper);
47
-
48
- const toolbarComponent = createWrapper();
49
-
50
- await nextTick();
51
-
52
- toolbarComponent.show();
53
-
54
- expect(popper.update).toHaveBeenCalled();
55
- });
56
- });