@zipify/wysiwyg 2.4.4-0 → 2.4.4

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/dist/wysiwyg.mjs CHANGED
@@ -14460,8 +14460,11 @@ const resolveTextPosition = ($from, $to, node, position) => ({
14460
14460
  from: Math.max(position, $from.pos),
14461
14461
  to: Math.min(position + node.nodeSize, $to.pos)
14462
14462
  });
14463
+ function resolvePositionOffset(doc2, position) {
14464
+ return doc2.resolve(position).depth + 1;
14465
+ }
14463
14466
  function isNodeFullySelected(doc2, selection, node, position) {
14464
- const offset2 = doc2.resolve(position).depth + 1;
14467
+ const offset2 = resolvePositionOffset(doc2, position);
14465
14468
  const isFromMatch = selection.from - offset2 <= position;
14466
14469
  const isToMatch = selection.to + offset2 >= node.nodeSize + position;
14467
14470
  return isFromMatch && isToMatch;
@@ -23787,13 +23790,12 @@ function useLink() {
23787
23790
  linkData.value.target = isChecked ? LinkTargets.BLANK : LinkTargets.SELF;
23788
23791
  }
23789
23792
  function prepareInitialFields() {
23790
- const link = editor.getAttributes(TextSettings.LINK);
23793
+ editor.commands.expandSelectionToLink();
23791
23794
  linkData.value.text = editor.commands.getSelectedText();
23792
- currentDestination.value.id = link.destination || LinkDestinations.URL;
23793
- linkData.value.target = link.target || LinkTargets.SELF;
23794
- if (link.href) {
23795
- destinationHrefs.value[currentDestination.value.id] = link.href;
23796
- }
23795
+ const { destination, href, target } = editor.getAttributes(TextSettings.LINK);
23796
+ currentDestination.value.id = destination || LinkDestinations.URL;
23797
+ linkData.value.target = target || LinkTargets.SELF;
23798
+ destinationHrefs.value[currentDestination.value.id] = href || "";
23797
23799
  }
23798
23800
  function resetDestinations() {
23799
23801
  destinationHrefs.value.block = pageBlocks.value[0].id;
@@ -25726,16 +25728,20 @@ const SelectionProcessor = Extension.create({
25726
25728
  restoreSelection: createCommand(({ commands: commands2 }) => {
25727
25729
  this.storage.selection && commands2.setTextSelection(this.storage.selection);
25728
25730
  }),
25729
- expandSelectionToBlock: createCommand(({ state, commands: commands2 }) => {
25730
- let from2 = state.selection.from;
25731
- let to = state.selection.to;
25732
- state.doc.nodesBetween(from2, to, (node, position, parent) => {
25733
- if (parent.type.name !== NodeTypes.DOCUMENT)
25734
- return;
25735
- from2 = Math.min(from2, position + 1);
25736
- to = Math.max(to, position + node.nodeSize - 1);
25731
+ expandSelection: createCommand(({ tr, commands: commands2 }, predicate) => {
25732
+ let from2 = tr.selection.from;
25733
+ let to = tr.selection.to;
25734
+ tr.doc.nodesBetween(from2, to, (node, position, parent) => {
25735
+ if (predicate({ node, parent })) {
25736
+ const offset2 = node.isText ? 0 : resolvePositionOffset(tr.doc, position);
25737
+ from2 = Math.min(from2, position + offset2);
25738
+ to = Math.max(to, position + node.nodeSize - offset2);
25739
+ }
25737
25740
  });
25738
25741
  commands2.setTextSelection({ from: from2, to });
25742
+ }),
25743
+ expandSelectionToBlock: createCommand(({ commands: commands2 }) => {
25744
+ commands2.expandSelection(({ parent }) => parent.type.name === NodeTypes.DOCUMENT);
25739
25745
  })
25740
25746
  };
25741
25747
  }
@@ -27738,7 +27744,13 @@ const Link = Link$1.extend({
27738
27744
  NodeFactory.mark(TextSettings.LINK, attributes)
27739
27745
  ]));
27740
27746
  }
27741
- return chain().applyMark(this.name, attributes).transformText(() => attributes.text).extendMarkRange(TextSettings.LINK).run();
27747
+ return chain().applyMark(this.name, attributes).expandSelectionToLink().command(({ tr }) => {
27748
+ tr.insertText(attributes.text, tr.selection.from, tr.selection.to);
27749
+ return true;
27750
+ }).run();
27751
+ }),
27752
+ expandSelectionToLink: createCommand(({ commands: commands2 }) => {
27753
+ commands2.expandSelection(({ node }) => this.type.isInSet(node.marks));
27742
27754
  }),
27743
27755
  isLink: createCommand(({ commands: commands2 }) => commands2.hasMark(this.name)),
27744
27756
  getLinkPreset: createCommand(() => computed(() => this.options.preset))
@@ -27746,13 +27758,9 @@ const Link = Link$1.extend({
27746
27758
  },
27747
27759
  renderHTML({ HTMLAttributes: attrs }) {
27748
27760
  const href = attrs.destination === LinkDestinations.BLOCK ? `#${attrs.href}` : attrs.href;
27749
- const presetClass = this.options.basePresetClass + this.options.preset.id;
27761
+ const presetClass = unref(this.options.basePresetClass) + unref(this.options.preset).id;
27750
27762
  const classes = `${presetClass} zw-style`;
27751
- const linkAttrs = {
27752
- href,
27753
- target: attrs.target,
27754
- class: classes
27755
- };
27763
+ const linkAttrs = { href, target: attrs.target, class: classes };
27756
27764
  return ["a", linkAttrs, 0];
27757
27765
  }
27758
27766
  });
@@ -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() {
@@ -63,11 +63,18 @@ 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
 
74
+ expandSelectionToLink: createCommand(({ commands }) => {
75
+ commands.expandSelection(({ node }) => this.type.isInSet(node.marks));
76
+ }),
77
+
71
78
  isLink: createCommand(({ commands }) => commands.hasMark(this.name)),
72
79
  getLinkPreset: createCommand(() => computed(() => this.options.preset))
73
80
  };
@@ -75,15 +82,9 @@ export const Link = Base.extend({
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 [
@@ -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
@@ -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.4.4-0",
3
+ "version": "2.4.4",
4
4
  "description": "Zipify modification of TipTap text editor",
5
5
  "main": "dist/wysiwyg.mjs",
6
6
  "bin": {