@zipify/wysiwyg 2.5.0 → 2.5.2

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.css CHANGED
@@ -564,10 +564,10 @@
564
564
  width: 72px;
565
565
  }
566
566
 
567
- .zw-alignment-control__modal[data-v-cef30e1a] {
567
+ .zw-alignment-control__modal[data-v-4aefb465] {
568
568
  padding: var(--zw-offset-xxs);
569
569
  }
570
- .zw-alignment-control__toggle[data-v-cef30e1a] {
570
+ .zw-alignment-control__toggle[data-v-4aefb465] {
571
571
  flex-direction: column;
572
572
  }
573
573
 
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;
@@ -23303,7 +23306,7 @@ var render$c = function __render__33() {
23303
23306
  }
23304
23307
  }, [_c("Icon", {
23305
23308
  attrs: {
23306
- "name": "alignment-".concat(_vm.currentValue),
23309
+ "name": _vm.icon,
23307
23310
  "size": "28px",
23308
23311
  "auto-color": ""
23309
23312
  }
@@ -23338,11 +23341,19 @@ const __vue2_script$c = {
23338
23341
  const currentValue = editor.commands.getAlignment();
23339
23342
  const wrapperRef = ref(null);
23340
23343
  const modalRef = ref(null);
23341
- const toggler = useModalToggler({ wrapperRef, modalRef });
23344
+ const toggler = useModalToggler({
23345
+ wrapperRef,
23346
+ modalRef
23347
+ });
23348
+ const icon = computed(() => {
23349
+ const name = unref(currentValue) || Alignments.LEFT;
23350
+ return `alignment-${name}`;
23351
+ });
23342
23352
  return {
23343
23353
  wrapperRef,
23344
23354
  modalRef,
23345
23355
  currentValue,
23356
+ icon,
23346
23357
  toggler,
23347
23358
  isOpened: toggler.isOpened
23348
23359
  };
@@ -23355,7 +23366,7 @@ var __component__$c = /* @__PURE__ */ normalizeComponent(
23355
23366
  staticRenderFns$c,
23356
23367
  false,
23357
23368
  __vue2_injectStyles$c,
23358
- "cef30e1a",
23369
+ "4aefb465",
23359
23370
  null,
23360
23371
  null
23361
23372
  );
@@ -23779,13 +23790,12 @@ function useLink() {
23779
23790
  linkData.value.target = isChecked ? LinkTargets.BLANK : LinkTargets.SELF;
23780
23791
  }
23781
23792
  function prepareInitialFields() {
23782
- const link = editor.getAttributes(TextSettings.LINK);
23793
+ editor.commands.expandSelectionToLink();
23783
23794
  linkData.value.text = editor.commands.getSelectedText();
23784
- currentDestination.value.id = link.destination || LinkDestinations.URL;
23785
- linkData.value.target = link.target || LinkTargets.SELF;
23786
- if (link.href) {
23787
- destinationHrefs.value[currentDestination.value.id] = link.href;
23788
- }
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 || "";
23789
23799
  }
23790
23800
  function resetDestinations() {
23791
23801
  destinationHrefs.value.block = pageBlocks.value[0].id;
@@ -25629,6 +25639,10 @@ const NodeProcessor = Extension.create({
25629
25639
  return (_a = unref(marksRef)[0]) != null ? _a : null;
25630
25640
  });
25631
25641
  }),
25642
+ hasMark: createCommand(({ commands: commands2 }, name) => {
25643
+ const mark = commands2.getMark(name);
25644
+ return computed(() => !!unref(mark));
25645
+ }),
25632
25646
  getCommonSettingMark: createCommand(({ commands: commands2 }, name, defaultRef) => {
25633
25647
  const selectionRef = commands2.getMark(name);
25634
25648
  return computed(() => {
@@ -25714,16 +25728,20 @@ const SelectionProcessor = Extension.create({
25714
25728
  restoreSelection: createCommand(({ commands: commands2 }) => {
25715
25729
  this.storage.selection && commands2.setTextSelection(this.storage.selection);
25716
25730
  }),
25717
- expandSelectionToBlock: createCommand(({ state, commands: commands2 }) => {
25718
- let from2 = state.selection.from;
25719
- let to = state.selection.to;
25720
- state.doc.nodesBetween(from2, to, (node, position, parent) => {
25721
- if (parent.type.name !== NodeTypes.DOCUMENT)
25722
- return;
25723
- from2 = Math.min(from2, position + 1);
25724
- 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
+ }
25725
25740
  });
25726
25741
  commands2.setTextSelection({ from: from2, to });
25742
+ }),
25743
+ expandSelectionToBlock: createCommand(({ commands: commands2 }) => {
25744
+ commands2.expandSelection(({ parent }) => parent.type.name === NodeTypes.DOCUMENT);
25727
25745
  })
25728
25746
  };
25729
25747
  }
@@ -27726,21 +27744,23 @@ const Link = Link$1.extend({
27726
27744
  NodeFactory.mark(TextSettings.LINK, attributes)
27727
27745
  ]));
27728
27746
  }
27729
- 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));
27730
27754
  }),
27731
- isLink: createCommand(({ editor }) => computed(() => editor.isActive(TextSettings.LINK))),
27755
+ isLink: createCommand(({ commands: commands2 }) => commands2.hasMark(this.name)),
27732
27756
  getLinkPreset: createCommand(() => computed(() => this.options.preset))
27733
27757
  };
27734
27758
  },
27735
27759
  renderHTML({ HTMLAttributes: attrs }) {
27736
27760
  const href = attrs.destination === LinkDestinations.BLOCK ? `#${attrs.href}` : attrs.href;
27737
- const presetClass = this.options.basePresetClass + this.options.preset.id;
27761
+ const presetClass = unref(this.options.basePresetClass) + unref(this.options.preset).id;
27738
27762
  const classes = `${presetClass} zw-style`;
27739
- const linkAttrs = {
27740
- href,
27741
- target: attrs.target,
27742
- class: classes
27743
- };
27763
+ const linkAttrs = { href, target: attrs.target, class: classes };
27744
27764
  return ["a", linkAttrs, 0];
27745
27765
  }
27746
27766
  });
@@ -7,7 +7,7 @@
7
7
  @click="toggler.open"
8
8
  v-tooltip="'Alignment'"
9
9
  >
10
- <Icon :name="`alignment-${currentValue}`" size="28px" auto-color />
10
+ <Icon :name="icon" size="28px" auto-color />
11
11
  </Button>
12
12
 
13
13
  <Modal class="zw-alignment-control__modal" ref="modalRef" :toggler="toggler">
@@ -17,10 +17,11 @@
17
17
  </template>
18
18
 
19
19
  <script>
20
- import { inject, ref } from 'vue';
20
+ import { computed, inject, ref, unref } from 'vue';
21
21
  import { InjectionTokens } from '../../../injectionTokens';
22
- import { Button, Icon, Modal, useModalToggler } from '../../base';
23
22
  import { tooltip } from '../../../directives';
23
+ import { Alignments } from '../../../enums';
24
+ import { Button, Icon, Modal, useModalToggler } from '../../base';
24
25
  import AlignmentControl from './AlignmentControl';
25
26
 
26
27
  export default {
@@ -43,12 +44,22 @@ export default {
43
44
  const wrapperRef = ref(null);
44
45
  const modalRef = ref(null);
45
46
 
46
- const toggler = useModalToggler({ wrapperRef, modalRef });
47
+ const toggler = useModalToggler({
48
+ wrapperRef,
49
+ modalRef
50
+ });
51
+
52
+ const icon = computed(() => {
53
+ const name = unref(currentValue) || Alignments.LEFT;
54
+
55
+ return `alignment-${name}`;
56
+ });
47
57
 
48
58
  return {
49
59
  wrapperRef,
50
60
  modalRef,
51
61
  currentValue,
62
+ icon,
52
63
  toggler,
53
64
  isOpened: toggler.isOpened
54
65
  };
@@ -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,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
@@ -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.0",
3
+ "version": "2.5.2",
4
4
  "description": "Zipify modification of TipTap text editor",
5
5
  "main": "dist/wysiwyg.mjs",
6
6
  "bin": {