@zipify/wysiwyg 2.5.1 → 2.5.3

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.
@@ -1,14 +1,15 @@
1
1
  import { ref } from 'vue';
2
2
  import { getSchema } from '@tiptap/core';
3
3
  import { DOMParser } from 'prosemirror-model';
4
- import { JSDOM } from 'jsdom';
5
4
  import { buildExtensions } from '../extensions';
6
5
  import { Devices } from '../enums';
7
- import { ContentNormalizer } from '../services';
6
+ import { ContentNormalizer, ContextWindow } from '../services';
8
7
  import { NodeDomParser } from './NodeDomParser';
9
8
 
10
9
  export class ContentSerializer {
11
10
  static build(options) {
11
+ ContextWindow.use(NodeDomParser.createWindow());
12
+
12
13
  const extensions = buildExtensions({
13
14
  fonts: options.fonts,
14
15
  minFontSize: 0,
@@ -21,7 +22,7 @@ export class ContentSerializer {
21
22
  baseListClass: options.baseListClass,
22
23
  deviceRef: ref(Devices.DESKTOP),
23
24
  pageBlocksRef: ref([]),
24
- wrapperRef: JSDOM.fragment('<p></p>').firstElementChild
25
+ wrapperRef: ContextWindow.document.createElement('p')
25
26
  });
26
27
  const schema = getSchema(extensions);
27
28
 
@@ -1,6 +1,10 @@
1
1
  import { JSDOM } from 'jsdom';
2
2
 
3
3
  export class NodeDomParser {
4
+ static createWindow() {
5
+ return new JSDOM().window;
6
+ }
7
+
4
8
  types;
5
9
 
6
10
  parse(html) {
@@ -8,6 +8,7 @@ const createEditor = (text, destination) => ({
8
8
  commands: {
9
9
  storeSelection: jest.fn(),
10
10
  restoreSelection: jest.fn(),
11
+ expandSelectionToLink: jest.fn(),
11
12
  getSelectedText: jest.fn(() => text ?? ''),
12
13
  focus: jest.fn().mockReturnThis(),
13
14
  unsetLink: jest.fn().mockReturnThis(),
@@ -3,6 +3,7 @@ import { LinkTargets, LinkDestinations, TextSettings } from '../../../../../enum
3
3
  import { InjectionTokens } from '../../../../../injectionTokens';
4
4
  import { RegExps } from '../../../../../regExps';
5
5
 
6
+ // TODO NEED refactor it
6
7
  export function useLink() {
7
8
  const editor = inject(InjectionTokens.EDITOR);
8
9
  const pageBlocks = inject(InjectionTokens.PAGE_BLOCKS);
@@ -16,15 +17,14 @@ export function useLink() {
16
17
  }
17
18
 
18
19
  function prepareInitialFields() {
19
- const link = editor.getAttributes(TextSettings.LINK);
20
-
20
+ editor.commands.expandSelectionToLink();
21
21
  linkData.value.text = editor.commands.getSelectedText();
22
- currentDestination.value.id = link.destination || LinkDestinations.URL;
23
- linkData.value.target = link.target || LinkTargets.SELF;
24
22
 
25
- if (link.href) {
26
- destinationHrefs.value[currentDestination.value.id] = link.href;
27
- }
23
+ const { destination, href, target } = editor.getAttributes(TextSettings.LINK);
24
+
25
+ currentDestination.value.id = destination || LinkDestinations.URL;
26
+ linkData.value.target = target || LinkTargets.SELF;
27
+ destinationHrefs.value[currentDestination.value.id] = href || '';
28
28
  }
29
29
 
30
30
  function resetDestinations() {
@@ -36,7 +36,7 @@ export const LineHeight = Extension.create({
36
36
  const wrapperEl = unref(this.options.wrapperRef);
37
37
  const converted = convertLineHeight(value, element, wrapperEl);
38
38
 
39
- return { desktop: converted, tablet: converted, mobile: null };
39
+ return converted ? { desktop: converted, tablet: converted, mobile: null } : null;
40
40
  },
41
41
 
42
42
  renderHTML(attrs) {
@@ -63,27 +63,28 @@ export const Link = Base.extend({
63
63
 
64
64
  return chain()
65
65
  .applyMark(this.name, attributes)
66
- .transformText(() => attributes.text)
67
- .extendMarkRange(TextSettings.LINK)
66
+ .expandSelectionToLink()
67
+ .command(({ tr }) => {
68
+ tr.insertText(attributes.text, tr.selection.from, tr.selection.to);
69
+ return true;
70
+ })
68
71
  .run();
69
72
  }),
70
73
 
71
- isLink: createCommand(({ editor }) => computed(() => editor.isActive(TextSettings.LINK))),
74
+ expandSelectionToLink: createCommand(({ commands }) => {
75
+ commands.expandSelection(({ node }) => this.type.isInSet(node.marks));
76
+ }),
77
+
78
+ isLink: createCommand(({ commands }) => commands.hasMark(this.name)),
72
79
  getLinkPreset: createCommand(() => computed(() => this.options.preset))
73
80
  };
74
81
  },
75
82
 
76
83
  renderHTML({ HTMLAttributes: attrs }) {
77
84
  const href = attrs.destination === LinkDestinations.BLOCK ? `#${attrs.href}` : attrs.href;
78
-
79
- const presetClass = this.options.basePresetClass + this.options.preset.id;
85
+ const presetClass = unref(this.options.basePresetClass) + unref(this.options.preset).id;
80
86
  const classes = `${presetClass} zw-style`;
81
-
82
- const linkAttrs = {
83
- href,
84
- target: attrs.target,
85
- class: classes
86
- };
87
+ const linkAttrs = { href, target: attrs.target, class: classes };
87
88
 
88
89
  return ['a', linkAttrs, 0];
89
90
  }
@@ -1,8 +1,15 @@
1
1
  import { ref } from 'vue';
2
- import { Editor } from '@tiptap/vue-2';
2
+ import { Editor, Mark } from '@tiptap/vue-2';
3
3
  import { buildTestExtensions } from '../../__tests__/utils';
4
4
  import { ContentNormalizer, NodeFactory } from '../../services';
5
5
  import { Link } from '../Link';
6
+ import { TextSettings } from '../../enums';
7
+
8
+ const MockFontWeight = Mark.create({
9
+ name: TextSettings.FONT_WEIGHT,
10
+ addAttributes: () => ({ value: { required: true } }),
11
+ renderHTML: () => ['span', {}, 0]
12
+ });
6
13
 
7
14
  function createEditor({ content }) {
8
15
  return new Editor({
@@ -13,42 +20,91 @@ function createEditor({ content }) {
13
20
  preset: 'link',
14
21
  baseClass: 'zw ts-',
15
22
  pageBlocks: ref([{ id: 987654 }])
16
- })
23
+ }),
24
+ MockFontWeight
17
25
  ]
18
26
  })
19
27
  });
20
28
  }
21
29
 
22
- const createContent = (text) => NodeFactory.doc([
23
- NodeFactory.paragraph([text])
24
- ]);
25
-
26
- const linkAttributes = (href) => ({
30
+ const createLink = (href) => ({
27
31
  href: href ?? '/test',
28
32
  text: 'Hello world link',
29
33
  target: '_self',
30
34
  destination: 'url'
31
35
  });
32
36
 
37
+ describe('get link', () => {
38
+ test('should check is link active', () => {
39
+ const editor = createEditor({
40
+ content: NodeFactory.doc([
41
+ NodeFactory.paragraph([
42
+ NodeFactory.text('hello world', [
43
+ NodeFactory.mark(TextSettings.LINK, createLink())
44
+ ])
45
+ ])
46
+ ])
47
+ });
48
+
49
+ editor.commands.selectAll();
50
+
51
+ expect(editor.commands.isLink().value).toBe(true);
52
+ });
53
+
54
+ test('should check is link inactive', () => {
55
+ const editor = createEditor({
56
+ content: NodeFactory.doc([
57
+ NodeFactory.paragraph('hello world')
58
+ ])
59
+ });
60
+
61
+ editor.commands.selectAll();
62
+
63
+ expect(editor.commands.isLink().value).toBe(false);
64
+ });
65
+ });
66
+
33
67
  describe('apply link', () => {
34
68
  test('should apply link', () => {
35
69
  const editor = createEditor({
36
- content: createContent(NodeFactory.text('hello world'))
70
+ content: NodeFactory.doc([
71
+ NodeFactory.paragraph('hello world')
72
+ ])
37
73
  });
38
74
 
39
75
  editor.commands.selectAll();
40
- editor.commands.applyLink(linkAttributes());
76
+ editor.commands.applyLink(createLink());
41
77
 
42
78
  expect(editor.getJSON()).toMatchSnapshot();
43
79
  });
44
80
 
45
81
  test('should apply link when no selected text', () => {
46
82
  const editor = createEditor({
47
- content: createContent(NodeFactory.text('Some text'))
83
+ content: NodeFactory.doc([
84
+ NodeFactory.paragraph('Some text')
85
+ ])
48
86
  });
49
87
 
50
88
  editor.commands.setTextSelection(6);
51
- editor.commands.applyLink(linkAttributes());
89
+ editor.commands.applyLink(createLink());
90
+
91
+ expect(editor.getJSON()).toMatchSnapshot();
92
+ });
93
+
94
+ test('should apply link to multiple selected nodes', () => {
95
+ const editor = createEditor({
96
+ content: NodeFactory.doc([
97
+ NodeFactory.paragraph([
98
+ NodeFactory.text('hello', [
99
+ NodeFactory.mark(TextSettings.FONT_WEIGHT, { value: '700' })
100
+ ]),
101
+ NodeFactory.text(' world')
102
+ ])
103
+ ])
104
+ });
105
+
106
+ editor.commands.selectAll();
107
+ editor.commands.applyLink(createLink());
52
108
 
53
109
  expect(editor.getJSON()).toMatchSnapshot();
54
110
  });
@@ -27,6 +27,39 @@ Object {
27
27
  }
28
28
  `;
29
29
 
30
+ exports[`apply link should apply link to multiple selected nodes 1`] = `
31
+ Object {
32
+ "content": Array [
33
+ Object {
34
+ "content": Array [
35
+ Object {
36
+ "marks": Array [
37
+ Object {
38
+ "attrs": Object {
39
+ "destination": "url",
40
+ "href": "/test",
41
+ "target": "_self",
42
+ },
43
+ "type": "link",
44
+ },
45
+ Object {
46
+ "attrs": Object {
47
+ "value": "700",
48
+ },
49
+ "type": "font_weight",
50
+ },
51
+ ],
52
+ "text": "Hello world link",
53
+ "type": "text",
54
+ },
55
+ ],
56
+ "type": "paragraph",
57
+ },
58
+ ],
59
+ "type": "doc",
60
+ }
61
+ `;
62
+
30
63
  exports[`apply link should apply link when no selected text 1`] = `
31
64
  Object {
32
65
  "content": Array [
@@ -110,6 +110,12 @@ export const NodeProcessor = Extension.create({
110
110
  return computed(() => unref(marksRef)[0] ?? null);
111
111
  }),
112
112
 
113
+ hasMark: createCommand(({ commands }, name) => {
114
+ const mark = commands.getMark(name);
115
+
116
+ return computed(() => !!unref(mark));
117
+ }),
118
+
113
119
  getCommonSettingMark: createCommand(({ commands }, name, defaultRef) => {
114
120
  const selectionRef = commands.getMark(name);
115
121
 
@@ -1,5 +1,5 @@
1
1
  import { Extension } from '@tiptap/vue-2';
2
- import { createCommand } from '../../utils';
2
+ import { createCommand, resolvePositionOffset } from '../../utils';
3
3
  import { NodeTypes } from '../../enums';
4
4
 
5
5
  export const SelectionProcessor = Extension.create({
@@ -19,18 +19,24 @@ export const SelectionProcessor = Extension.create({
19
19
  this.storage.selection && commands.setTextSelection(this.storage.selection);
20
20
  }),
21
21
 
22
- expandSelectionToBlock: createCommand(({ state, commands }) => {
23
- let from = state.selection.from;
24
- let to = state.selection.to;
22
+ expandSelection: createCommand(({ tr, commands }, predicate) => {
23
+ let from = tr.selection.from;
24
+ let to = tr.selection.to;
25
25
 
26
- state.doc.nodesBetween(from, to, (node, position, parent) => {
27
- if (parent.type.name !== NodeTypes.DOCUMENT) return;
26
+ tr.doc.nodesBetween(from, to, (node, position, parent) => {
27
+ if (predicate({ node, parent })) {
28
+ const offset = node.isText ? 0 : resolvePositionOffset(tr.doc, position);
28
29
 
29
- from = Math.min(from, position + 1);
30
- to = Math.max(to, position + node.nodeSize - 1);
30
+ from = Math.min(from, position + offset);
31
+ to = Math.max(to, position + node.nodeSize - offset);
32
+ }
31
33
  });
32
34
 
33
35
  commands.setTextSelection({ from, to });
36
+ }),
37
+
38
+ expandSelectionToBlock: createCommand(({ commands }) => {
39
+ commands.expandSelection(({ parent }) => parent.type.name === NodeTypes.DOCUMENT);
34
40
  })
35
41
  };
36
42
  }
@@ -442,6 +442,32 @@ describe('get mark', () => {
442
442
  });
443
443
  });
444
444
 
445
+ describe('has mark', () => {
446
+ test('should detect mark', () => {
447
+ const editor = createEditor({
448
+ content: NodeFactory.doc([
449
+ NodeFactory.paragraph([
450
+ NodeFactory.text('lorem ipsum', [
451
+ NodeFactory.mark(TextSettings.FONT_WEIGHT, { value: '700' })
452
+ ])
453
+ ])
454
+ ])
455
+ });
456
+
457
+ expect(editor.commands.hasMark(TextSettings.FONT_WEIGHT).value).toBe(true);
458
+ });
459
+
460
+ test('should not detect mark', () => {
461
+ const editor = createEditor({
462
+ content: NodeFactory.doc([
463
+ NodeFactory.paragraph('lorem ipsum')
464
+ ])
465
+ });
466
+
467
+ expect(editor.commands.hasMark(TextSettings.FONT_WEIGHT).value).toBe(false);
468
+ });
469
+ });
470
+
445
471
  describe('get setting mark', () => {
446
472
  test('should get common setting mark', () => {
447
473
  const editor = createEditor({
@@ -49,8 +49,22 @@ describe('store/restore selection', () => {
49
49
  });
50
50
  });
51
51
 
52
- describe('expand selection to block', () => {
53
- test('should exand selection to one block', () => {
52
+ describe('expand selection', () => {
53
+ test('should expand selection by condition', () => {
54
+ const editor = createEditor();
55
+
56
+ editor
57
+ .chain()
58
+ .setTextSelection({ from: 10, to: 20 })
59
+ .expandSelection(({ node }) => node.isText)
60
+ .run();
61
+
62
+ expect(editor.state.selection).toEqual(
63
+ expect.objectContaining({ from: 1, to: 27 })
64
+ );
65
+ });
66
+
67
+ test('should expand selection to one block', () => {
54
68
  const editor = createEditor();
55
69
 
56
70
  editor
@@ -1,7 +1,9 @@
1
+ import { ContextWindow } from '../services';
2
+
1
3
  export function convertFontSize(value, wrapperEl) {
2
4
  if (!value.includes('em')) return parseInt(value);
3
5
 
4
- const containerValue = window.getComputedStyle(wrapperEl).fontSize;
6
+ const containerValue = ContextWindow.getComputedStyle(wrapperEl).fontSize;
5
7
  const size = parseFloat(value) * parseFloat(containerValue);
6
8
 
7
9
  return Math.round(size);
@@ -1,10 +1,11 @@
1
+ import { ContextWindow } from '../services';
1
2
  import { convertFontSize } from './convertFontSize';
2
3
 
3
4
  function getFontSize(sourceEl, wrapperEl) {
4
5
  const source = sourceEl.firstElementChild || sourceEl;
5
6
  const fontSize = source.style.fontSize;
6
7
 
7
- return fontSize || window.getComputedStyle(wrapperEl).fontSize;
8
+ return fontSize || ContextWindow.getComputedStyle(wrapperEl).fontSize;
8
9
  }
9
10
 
10
11
  export function convertLineHeight(value, sourceEl, wrapperEl) {
@@ -13,5 +14,5 @@ export function convertLineHeight(value, sourceEl, wrapperEl) {
13
14
  const rawFontSize = getFontSize(sourceEl, wrapperEl);
14
15
  const fontSize = convertFontSize(rawFontSize, wrapperEl);
15
16
 
16
- return (parseInt(value) / fontSize).toFixed(2);
17
+ return fontSize ? (parseInt(value) / fontSize).toFixed(2) : null;
17
18
  }
@@ -9,6 +9,7 @@ export { convertAlignment } from './convertAlignment';
9
9
  export { importIcon } from './importIcon';
10
10
  export { isWysiwygContent, markWysiwygContent, unmarkWysiwygContent } from './isWysiwygContent';
11
11
  export { resolveTextPosition } from './resolveTextPosition';
12
+ export { resolvePositionOffset } from './resolvePositionOffset';
12
13
  export { isNodeFullySelected } from './isNodeFullySelected';
13
14
  export { isMarkAppliedToParent } from './isMarkAppliedToParent';
14
15
  export { findMarkByType } from './findMarkByType';
@@ -1,5 +1,7 @@
1
+ import { resolvePositionOffset } from './resolvePositionOffset';
2
+
1
3
  export function isNodeFullySelected(doc, selection, node, position) {
2
- const offset = doc.resolve(position).depth + 1;
4
+ const offset = resolvePositionOffset(doc, position);
3
5
  const isFromMatch = selection.from - offset <= position;
4
6
  const isToMatch = selection.to + offset >= node.nodeSize + position;
5
7
 
@@ -0,0 +1,3 @@
1
+ export function resolvePositionOffset(doc, position) {
2
+ return doc.resolve(position).depth + 1;
3
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zipify/wysiwyg",
3
- "version": "2.5.1",
3
+ "version": "2.5.3",
4
4
  "description": "Zipify modification of TipTap text editor",
5
5
  "main": "dist/wysiwyg.mjs",
6
6
  "bin": {