@livingdocs/editable.js 3.1.0 → 3.1.1

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,7 +1,7 @@
1
1
  {
2
2
  "name": "@livingdocs/editable.js",
3
3
  "description": "Friendly contenteditable API",
4
- "version": "3.1.0",
4
+ "version": "3.1.1",
5
5
  "dependencies": {
6
6
  "@babel/runtime": "^7.16.7",
7
7
  "bowser": "2.11.0",
@@ -83,11 +83,11 @@
83
83
  "isCi": true,
84
84
  "name": "Drone",
85
85
  "service": "drone",
86
- "commit": "2e7b8ed5efd8fb418a159ded67be45ddf9941999",
87
- "build": "482",
86
+ "commit": "9957094d8242121b94e2584b57ba04cd1c7067ed",
87
+ "build": "495",
88
88
  "branch": "master",
89
89
  "isPr": false,
90
90
  "slug": "livingdocsIO/editable.js",
91
- "date": "2022-03-22T20:40:52.111Z"
91
+ "date": "2022-05-17T13:22:55.296Z"
92
92
  }
93
93
  }
package/src/core.js CHANGED
@@ -400,7 +400,7 @@ export class Editable {
400
400
  )
401
401
  return -1
402
402
  }
403
- return highlightSupport.highlightRange(editableHost, highlightId, textRange.start, textRange.end, raiseEvents ? this.dispatcher : undefined, type)
403
+ return highlightSupport.highlightRange(editableHost, text, highlightId, textRange.start, textRange.end, raiseEvents ? this.dispatcher : undefined, type)
404
404
  }
405
405
 
406
406
  /**
@@ -12,7 +12,6 @@ const highlightSupport = {
12
12
 
13
13
  highlightText (editableHost, text, highlightId, type) {
14
14
  if (this.hasHighlight(editableHost, highlightId)) return
15
-
16
15
  const blockText = highlightText.extractText(editableHost)
17
16
 
18
17
  const marker = `<span class="highlight-${type}"></span>`
@@ -28,12 +27,23 @@ const highlightSupport = {
28
27
  }
29
28
  },
30
29
 
31
- highlightRange (editableHost, highlightId, startIndex, endIndex, dispatcher, type = 'comment') {
30
+
31
+ // This function was changed to track matches when text is added to the start of a component, but multiple white spaces break it in a strict sense
32
+ // The function works in the editor and in browsers, but tests with multiple white spaces will fail.
33
+ // Browsers change the white spaces to &nbsp and the function works, and the tests in highlight.spec.js have been updated to represent this.
34
+
35
+ highlightRange (editableHost, text, highlightId, startIndex, endIndex, dispatcher, type = 'comment') {
32
36
  if (this.hasHighlight(editableHost, highlightId)) {
33
37
  this.removeHighlight(editableHost, highlightId)
34
38
  }
39
+ const firstMarker = `<span class="highlight-${type}"></span>`
40
+ const markerNode = highlightSupport.createMarkerNode(firstMarker, type, this.win)
41
+ const textSearch = new TextHighlighting(markerNode, 'text')
42
+ const blockText = highlightText.extractText(editableHost)
43
+ const matchesArray = textSearch.findMatches(blockText, [text])
44
+ const {actualStartIndex, actualEndIndex} = this.getIndex(matchesArray, startIndex, endIndex)
35
45
  const range = rangy.createRange()
36
- range.selectCharacters(editableHost, startIndex, endIndex)
46
+ range.selectCharacters(editableHost, actualStartIndex, actualEndIndex)
37
47
 
38
48
  if (!isInHost(range.commonAncestorContainer, editableHost)) {
39
49
  return -1
@@ -52,7 +62,7 @@ const highlightSupport = {
52
62
  if (dispatcher) {
53
63
  dispatcher.notify('change', editableHost)
54
64
  }
55
- return startIndex
65
+ return actualStartIndex
56
66
  },
57
67
 
58
68
  updateHighlight (editableHost, highlightId, addCssClass, removeCssClass) {
@@ -136,6 +146,18 @@ const highlightSupport = {
136
146
  marker.setAttribute('data-editable', 'ui-unwrap')
137
147
  marker.setAttribute('data-highlight', highlightType)
138
148
  return marker
149
+ },
150
+
151
+ // This function checks to see if text has been added to the component before the comment
152
+ // If it has, the start index is updated, otherwise it remains the same
153
+ getIndex (matchesArray, startIndex, endIndex) {
154
+ const newStartIndex = matchesArray.find((match) => {
155
+ return match.startIndex >= startIndex // checks if the startIndex has increased
156
+ })
157
+ const actualStartIndex = newStartIndex ? newStartIndex.startIndex : startIndex
158
+ const actualEndIndex = actualStartIndex ? (actualStartIndex + (endIndex - startIndex)) : endIndex
159
+
160
+ return {actualStartIndex, actualEndIndex}
139
161
  }
140
162
 
141
163
  }