@livingdocs/editable.js 3.0.7 → 3.0.11
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/editable.js +1 -1
- package/dist/editable.js.map +1 -1
- package/package.json +4 -4
- package/src/content.js +15 -2
- package/src/highlight-support.js +2 -1
- package/src/range-save-restore.js +39 -5
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.0.
|
|
4
|
+
"version": "3.0.11",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@babel/runtime": "^7.13.17",
|
|
7
7
|
"bowser": "2.11.0",
|
|
@@ -83,11 +83,11 @@
|
|
|
83
83
|
"isCi": true,
|
|
84
84
|
"name": "Drone",
|
|
85
85
|
"service": "drone",
|
|
86
|
-
"commit": "
|
|
87
|
-
"build": "
|
|
86
|
+
"commit": "d8207571aa6ea4a4bfcbd7f5755a95c907df53ef",
|
|
87
|
+
"build": "416",
|
|
88
88
|
"branch": "master",
|
|
89
89
|
"isPr": false,
|
|
90
90
|
"slug": "livingdocsIO/editable.js",
|
|
91
|
-
"date": "2021-
|
|
91
|
+
"date": "2021-12-29T16:06:04.948Z"
|
|
92
92
|
}
|
|
93
93
|
}
|
package/src/content.js
CHANGED
|
@@ -29,6 +29,12 @@ export function tidyHtml (element) {
|
|
|
29
29
|
export function normalizeTags (element) {
|
|
30
30
|
const fragment = document.createDocumentFragment()
|
|
31
31
|
|
|
32
|
+
// Remove line breaks at the beginning of a content block
|
|
33
|
+
removeWhitespaces(element, 'firstChild')
|
|
34
|
+
|
|
35
|
+
// Remove line breaks at the end of a content block
|
|
36
|
+
removeWhitespaces(element, 'lastChild')
|
|
37
|
+
|
|
32
38
|
for (const node of element.childNodes) {
|
|
33
39
|
// skip empty tags, so they'll get removed
|
|
34
40
|
if (node.nodeName !== 'BR' && !node.textContent) continue
|
|
@@ -136,7 +142,7 @@ export function cloneRangeContents (range) {
|
|
|
136
142
|
return fragment
|
|
137
143
|
}
|
|
138
144
|
|
|
139
|
-
function removeWhitespaces (node, type) {
|
|
145
|
+
function removeWhitespaces (node, type, firstCall = true) {
|
|
140
146
|
let elem
|
|
141
147
|
while ((elem = node[type])) {
|
|
142
148
|
if (elem.nodeType === nodeType.textNode) {
|
|
@@ -145,10 +151,17 @@ function removeWhitespaces (node, type) {
|
|
|
145
151
|
} else if (elem.nodeName === 'BR') {
|
|
146
152
|
elem.remove()
|
|
147
153
|
} else {
|
|
148
|
-
if (elem[type]) removeWhitespaces(elem, type)
|
|
154
|
+
if (elem[type]) removeWhitespaces(elem, type, false)
|
|
149
155
|
break
|
|
150
156
|
}
|
|
151
157
|
}
|
|
158
|
+
|
|
159
|
+
if (!firstCall) return
|
|
160
|
+
elem = node[type]
|
|
161
|
+
if (elem?.nodeType !== nodeType.textNode) return
|
|
162
|
+
// Remove whitespaces at the end or start of a block with content
|
|
163
|
+
// e.g. ' Hello world' > 'Hello World'
|
|
164
|
+
elem.textContent = elem.textContent.replace(type.startsWith('last') ? /\s+$/ : /^\s+/, '')
|
|
152
165
|
}
|
|
153
166
|
|
|
154
167
|
// Remove elements that were inserted for internal or user interface purposes
|
package/src/highlight-support.js
CHANGED
|
@@ -11,17 +11,37 @@ let boundaryMarkerId = 0
|
|
|
11
11
|
// (U+FEFF) zero width no-break space
|
|
12
12
|
const markerTextChar = '\ufeff'
|
|
13
13
|
|
|
14
|
+
function isChildOf (parent, possibleChild) {
|
|
15
|
+
for (let i = 0; i < parent.children.length; i++) {
|
|
16
|
+
if (parent.children[i] === possibleChild) return true
|
|
17
|
+
}
|
|
18
|
+
return false
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function startContainerIsChild (range) {
|
|
22
|
+
const parent = range.commonAncestorContainer
|
|
23
|
+
if (parent.nodeType === 3) return false // if we are on the text node it can't be a parent
|
|
24
|
+
const possibleChild = range.startContainer.parentElement
|
|
25
|
+
return isChildOf(parent, possibleChild)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function endContainerIsChild (range) {
|
|
29
|
+
const parent = range.commonAncestorContainer
|
|
30
|
+
if (parent.nodeType === 3) return false // if we are on the text node it can't be a parent
|
|
31
|
+
const possibleChild = range.endContainer.parentElement
|
|
32
|
+
return isChildOf(parent, possibleChild)
|
|
33
|
+
}
|
|
34
|
+
|
|
14
35
|
export function insertRangeBoundaryMarker (range, atStart) {
|
|
15
36
|
const container = range.commonAncestorContainer
|
|
37
|
+
const directlyBeforeFormatTag = startContainerIsChild(range) && atStart
|
|
38
|
+
const directlyAfterFormatTag = endContainerIsChild(range) && !atStart
|
|
39
|
+
|
|
16
40
|
// If ownerDocument is null the commonAncestorContainer is window.document
|
|
17
41
|
if (container.ownerDocument === null || container.ownerDocument === undefined) {
|
|
18
42
|
error('Cannot save range: range is emtpy')
|
|
19
43
|
}
|
|
20
44
|
|
|
21
|
-
// Clone the Range and collapse to the appropriate boundary point
|
|
22
|
-
const boundaryRange = range.cloneRange()
|
|
23
|
-
boundaryRange.collapse(atStart)
|
|
24
|
-
|
|
25
45
|
// Create the marker element containing a single
|
|
26
46
|
// invisible character using DOM methods and insert it
|
|
27
47
|
const doc = container.ownerDocument.defaultView.document
|
|
@@ -32,7 +52,21 @@ export function insertRangeBoundaryMarker (range, atStart) {
|
|
|
32
52
|
markerEl.style.display = 'none'
|
|
33
53
|
markerEl.appendChild(doc.createTextNode(markerTextChar))
|
|
34
54
|
|
|
35
|
-
|
|
55
|
+
// if another tag is trailing exactly at the start or end, prepend
|
|
56
|
+
// or append the marker element directly on the parent in order to prevent breaking HTML markup.
|
|
57
|
+
if (directlyBeforeFormatTag) {
|
|
58
|
+
range.endContainer.parentElement.insertBefore(markerEl, range.startContainer.parentElement)
|
|
59
|
+
} else if (directlyAfterFormatTag) {
|
|
60
|
+
// emulating insertAfter with nextSibling
|
|
61
|
+
range.startContainer.parentElement.insertBefore(markerEl,
|
|
62
|
+
range.endContainer.parentElement.nextSibling)
|
|
63
|
+
} else {
|
|
64
|
+
// Clone the Range and collapse to the appropriate boundary point
|
|
65
|
+
const boundaryRange = range.cloneRange()
|
|
66
|
+
boundaryRange.collapse(atStart)
|
|
67
|
+
boundaryRange.insertNode(markerEl)
|
|
68
|
+
}
|
|
69
|
+
|
|
36
70
|
return markerEl
|
|
37
71
|
}
|
|
38
72
|
|