@zipify/wysiwyg 1.0.0-dev.12 → 1.0.0-dev.13

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.
package/lib/Wysiwyg.vue CHANGED
@@ -17,7 +17,7 @@ import { Toolbar } from './components';
17
17
  import { useToolbar, useEditor } from './composables';
18
18
  import { getExtensions } from './extensions';
19
19
  import { InjectionTokens } from './injectionTokens';
20
- import { FavoriteColors, Storage } from './services';
20
+ import { ContextWindow, FavoriteColors, Storage } from './services';
21
21
  import { Devices } from './enums';
22
22
  import { outClick } from './directives';
23
23
  import { Font } from './models';
@@ -95,10 +95,18 @@ export default {
95
95
  type: String,
96
96
  required: false,
97
97
  default: 'zw-list--'
98
+ },
99
+
100
+ window: {
101
+ type: Window,
102
+ required: false,
103
+ default: () => window
98
104
  }
99
105
  },
100
106
 
101
107
  setup(props, { emit }) {
108
+ ContextWindow.use(props.window);
109
+
102
110
  const fonts = props.fonts.map((font) => new Font(font));
103
111
 
104
112
  const toolbarRef = ref(null);
@@ -1,4 +1,5 @@
1
1
  import { ref } from '@vue/composition-api';
2
+ import { ContextWindow } from '../../../../services';
2
3
  import { useActivatedListener } from '../../composables';
3
4
 
4
5
  export function usePickerHotkeys({ isOpenedRef, onCancel, onApply }) {
@@ -16,7 +17,7 @@ export function usePickerHotkeys({ isOpenedRef, onCancel, onApply }) {
16
17
  }
17
18
 
18
19
  useActivatedListener({
19
- targetRef: ref(document),
20
+ targetRef: ref(ContextWindow.document),
20
21
  isActiveRef: isOpenedRef,
21
22
  event: 'keydown',
22
23
  onEvent: handle,
@@ -1,5 +1,21 @@
1
+ import { ContextWindow } from '../services';
2
+
1
3
  const dataStorage = new WeakMap();
2
4
 
5
+ function attachListener(onClick) {
6
+ if (ContextWindow.window !== window) {
7
+ window.document.addEventListener('click', onClick);
8
+ }
9
+ ContextWindow.document.addEventListener('click', onClick);
10
+ }
11
+
12
+ function removeListener(onClick) {
13
+ if (ContextWindow.window !== window) {
14
+ window.document.removeEventListener('click', onClick);
15
+ }
16
+ ContextWindow.document.removeEventListener('click', onClick);
17
+ }
18
+
3
19
  export const outClick = {
4
20
  bind(el, { value }) {
5
21
  const onOutClick = value.onOutClick || value;
@@ -14,7 +30,7 @@ export const outClick = {
14
30
  dataStorage.set(el, { callback: onClick, isEnabled });
15
31
 
16
32
  if (isEnabled) {
17
- setTimeout(() => document.addEventListener('click', onClick));
33
+ setTimeout(() => attachListener(onClick));
18
34
  }
19
35
  },
20
36
 
@@ -25,13 +41,13 @@ export const outClick = {
25
41
  if (isEnabled === data.isEnabled) return;
26
42
 
27
43
  isEnabled
28
- ? document.addEventListener('click', data.callback)
29
- : document.removeEventListener('click', data.callback);
44
+ ? attachListener(data.callback)
45
+ : removeListener(data.callback);
30
46
 
31
47
  dataStorage.set(el, { callback: data.callback, isEnabled });
32
48
  },
33
49
 
34
50
  unbind(el) {
35
- document.removeEventListener('click', dataStorage.get(el).callback);
51
+ removeListener(dataStorage.get(el).callback);
36
52
  }
37
53
  };
@@ -2,6 +2,7 @@ import { Extension } from '@tiptap/vue-2';
2
2
  import { computed } from '@vue/composition-api';
3
3
  import { createCommand, renderInlineSetting } from '../utils';
4
4
  import { NodeTypes, TextSettings } from '../enums';
5
+ import { ContextWindow } from '../services';
5
6
 
6
7
  const DEFAULTS = {
7
8
  mobile: null,
@@ -30,7 +31,7 @@ export const LineHeight = Extension.create({
30
31
 
31
32
  // element is not connected to window so getComputedStyle is not working on element
32
33
  const childFontSize = element.firstElementChild.style.fontSize;
33
- const fontSize = childFontSize || window.getComputedStyle(document.body).fontSize;
34
+ const fontSize = childFontSize || ContextWindow.getComputedStyle(ContextWindow.body).fontSize;
34
35
  const relative = (parseFloat(value) / parseFloat(fontSize)).toFixed(2);
35
36
 
36
37
  return { desktop: relative, tablet: relative, mobile: relative };
@@ -3,6 +3,7 @@ import { computed, toRef } from '@vue/composition-api';
3
3
  import { Heading } from '@tiptap/extension-heading';
4
4
  import { createCommand } from '../utils';
5
5
  import { Devices, NodeTypes, TextSettings } from '../enums';
6
+ import { ContextWindow } from '../services';
6
7
 
7
8
  function makePresetClass(base, preset) {
8
9
  const baseClass = base.split(' ').map((part) => `.${part}`).join('');
@@ -162,14 +163,14 @@ export const StylePreset = Extension.create({
162
163
  },
163
164
 
164
165
  onCreate() {
165
- const existingStyleEl = document.querySelector('[data-zw-styles]');
166
+ const existingStyleEl = ContextWindow.document.querySelector('[data-zw-styles]');
166
167
 
167
168
  if (existingStyleEl) {
168
169
  this.storage.presetStyleEl = existingStyleEl;
169
170
  return;
170
171
  }
171
172
 
172
- this.storage.presetStyleEl = document.createElement('style');
173
+ this.storage.presetStyleEl = ContextWindow.document.createElement('style');
173
174
  this.storage.presetStyleEl.dataset.zwStyles = '';
174
175
 
175
176
  for (const preset of this.options.presetsRef.value) {
@@ -190,6 +191,6 @@ export const StylePreset = Extension.create({
190
191
  this.storage.presetStyleEl.innerHTML += css.join(' ');
191
192
  }
192
193
 
193
- document.head.append(this.storage.presetStyleEl);
194
+ ContextWindow.head.append(this.storage.presetStyleEl);
194
195
  }
195
196
  });
@@ -1,3 +1,5 @@
1
+ import { ContextWindow } from './ContextWidnow';
2
+
1
3
  export class ContentNormalizer {
2
4
  static NORMALIZING_STYLE_BLACKLIST = ['text-align', 'line-height'];
3
5
 
@@ -83,7 +85,7 @@ export class ContentNormalizer {
83
85
  _wrapTextNode(parent, node) {
84
86
  if (node.nodeType !== Node.TEXT_NODE) return node;
85
87
 
86
- const span = document.createElement('span');
88
+ const span = ContextWindow.document.createElement('span');
87
89
 
88
90
  span.append(node.cloneNode());
89
91
  parent.replaceChild(span, node);
@@ -0,0 +1,23 @@
1
+ export class ContextWindow {
2
+ static window = window;
3
+
4
+ static use(window) {
5
+ this.window = window;
6
+ }
7
+
8
+ static get document() {
9
+ return this.window.document;
10
+ }
11
+
12
+ static get body() {
13
+ return this.document.body;
14
+ }
15
+
16
+ static get head() {
17
+ return this.document.head;
18
+ }
19
+
20
+ static getComputedStyle(element) {
21
+ return this.window.getComputedStyle(element);
22
+ }
23
+ }
@@ -2,3 +2,4 @@ export { JsonSerializer } from './JsonSerializer';
2
2
  export { Storage } from './Storage';
3
3
  export { FavoriteColors } from './FavoriteColors';
4
4
  export { ContentNormalizer } from './ContentNormalizer';
5
+ export { ContextWindow } from './ContextWidnow';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zipify/wysiwyg",
3
- "version": "1.0.0-dev.12",
3
+ "version": "1.0.0-dev.13",
4
4
  "description": "Zipify modification of TipTap text editor",
5
5
  "main": "dist/wysiwyg.js",
6
6
  "repository": {