@writergate/quill-image-uploader-nextjs 0.0.12 → 0.1.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@writergate/quill-image-uploader-nextjs",
3
- "version": "0.0.12",
3
+ "version": "0.1.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/enlear/quill-image-uploader-nextjs.git"
@@ -11,16 +11,79 @@ class ImageUploader {
11
11
  "[Missing config] upload function that returns a promise is required"
12
12
  );
13
13
 
14
+ if (typeof this.options.newComment !== "function")
15
+ console.warn(
16
+ "[Missing config] newComment function that returns a promise is required"
17
+ );
18
+
19
+ if (typeof this.options.showComments !== "function")
20
+ console.warn(
21
+ "[Missing config] showComments function that returns a promise is required"
22
+ );
23
+
14
24
  var toolbar = this.quill.getModule("toolbar");
15
25
  toolbar.addHandler("image", this.selectLocalImage.bind(this));
16
26
  toolbar.addHandler("code-block", this.fixHighlighter.bind(this));
27
+ toolbar.addHandler("clean", this.clean.bind(this));
28
+ toolbar.addHandler("underline", this.addComment.bind(this));
17
29
 
18
30
  this.handleDrop = this.handleDrop.bind(this);
19
31
  this.handlePaste = this.handlePaste.bind(this);
20
32
 
21
33
  this.quill.root.addEventListener("drop", this.handleDrop, false);
22
34
  this.quill.root.addEventListener("paste", this.handlePaste, false);
35
+ this.quill.on('text-change', this.renderComments.bind(this));
36
+ }
23
37
 
38
+ addComment() {
39
+ var range = this.quill.getSelection();
40
+
41
+ if (range && range.length > 0 && this.options.newComment) {
42
+ range.top = this.quill.getBounds(range.index, range.length).top
43
+ this.options.newComment(range, this.quill);
44
+ }
45
+ this.quill.theme.tooltip.hide();
46
+ }
47
+
48
+ calculateIndexChange(delta) {
49
+ var index = delta.ops[0].retain || 0;
50
+ var insert = delta.ops.filter(function (i) {
51
+ return i.insert;
52
+ }).map(function (i) { return i.attributes && i.attributes['code-block'] ? i.insert.replaceAll('\n', '') : i.insert; }).join()
53
+ var insLength = insert ? insert.length : 0;
54
+ var delLength = delta.ops.filter(function (i) {
55
+ return i.delete;
56
+ }).map(function (i) { return i.delete }).reduce((a, b) => a + b, 0);
57
+ return { index: index, change: insLength - delLength };
58
+ }
59
+
60
+ adjustIndex(currentIndex, indexDelta) {
61
+ if (indexDelta.change > 0 && currentIndex < indexDelta.index) {
62
+ // comment index is before the modified text and modification didn't shift content before its starting point
63
+ return currentIndex;
64
+ } else if (indexDelta.change < 0 && currentIndex < (indexDelta.index + indexDelta.change)) {
65
+ // code shifted before its starting point but still the comment index is before the modified text
66
+ return currentIndex;
67
+ } else {
68
+ return currentIndex + indexDelta.change;
69
+ }
70
+ }
71
+
72
+ renderComments(delta, oldDelta, source) {
73
+ if (source === "user" && this.options.comments && Object.keys(this.options.comments()).length !== 0) {
74
+ var indexDelta = this.calculateIndexChange(delta);
75
+ var commentObjs = {};
76
+ for (const [key, value] of Object.entries((this.options.comments()))) {
77
+ var newIndex = this.adjustIndex(value.range.index, indexDelta);
78
+ var length = value.range.length;
79
+ if (newIndex > 0) {
80
+ commentObjs[key] = { range: { index: newIndex, length: length, top: this.quill.getBounds(newIndex, length).top }, message: value.message };
81
+ }
82
+ }
83
+ if (this.options.showComments) {
84
+ this.options.showComments(commentObjs, this.quill);
85
+ }
86
+ }
24
87
  }
25
88
 
26
89
  fixHighlighter() {
@@ -32,25 +95,17 @@ class ImageUploader {
32
95
  };
33
96
 
34
97
  // if it was a code-block, and the user meant to remove it
35
- // then do all the work we did up top in the keybinding function
36
- this.quill.formatLine(range.index, range.length, 'code-block', false, 'user');
37
- const { domNode } = this.quill.getLines(range.index, range.length)[0];
38
- let nodes = [domNode];
39
-
40
- while (nodes.length > 0) {
41
- const node = nodes.shift();
42
- // first append children to queue
43
- nodes = nodes.concat(Array.from(node.children));
44
-
45
- // go through class list of current node removing its "hljs" classes
46
- for (let i = 0; i < node.classList.length; i++) {
47
- const c = node.classList[i];
48
- if (c.indexOf('hljs') >= 0) {
49
- node.classList.remove(c);
50
- i--;
51
- }
52
- }
53
- }
98
+ this.quill.removeFormat(range.index, range.length, 'user');
99
+ // running it twise to remove colors
100
+ this.quill.removeFormat(range.index, range.length, 'user');
101
+ }
102
+
103
+ clean() {
104
+ const range = this.quill.getSelection(true);
105
+ const formats = this.quill.getFormat(range);
106
+ // running it twise to remove colors
107
+ this.quill.removeFormat(range.index, range.length, 'user');
108
+ this.quill.removeFormat(range.index, range.length, 'user');
54
109
  }
55
110
 
56
111
  selectLocalImage() {