@cocreate/text 1.16.5 → 1.17.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/CHANGELOG.md CHANGED
@@ -1,3 +1,29 @@
1
+ ## [1.17.2](https://github.com/CoCreate-app/CoCreate-text/compare/v1.17.1...v1.17.2) (2022-07-09)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * updateDom will sendPosition if the currentContainer = domEl ([c1e9f56](https://github.com/CoCreate-app/CoCreate-text/commit/c1e9f56359c88dbff943c3b82d81746d30833fdb))
7
+
8
+ ## [1.17.1](https://github.com/CoCreate-app/CoCreate-text/compare/v1.17.0...v1.17.1) (2022-06-24)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * imporved handeling of path fallback to html element if no matches amoung parentElements ([5c331d0](https://github.com/CoCreate-app/CoCreate-text/commit/5c331d0d32ffd60587f684b44e69766097193c33))
14
+
15
+ # [1.17.0](https://github.com/CoCreate-app/CoCreate-text/compare/v1.16.5...v1.17.0) (2022-06-23)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * try to update parent element of previous element rather then html tag ([37ab36c](https://github.com/CoCreate-app/CoCreate-text/commit/37ab36ce79fa270b3a43daaf14597e765f42e371))
21
+
22
+
23
+ ### Features
24
+
25
+ * if newEl can not be found using path, update path to query parent element ([2c637f1](https://github.com/CoCreate-app/CoCreate-text/commit/2c637f1e0c3d15f00d1664c4d744d4ee9c4581c6))
26
+
1
27
  ## [1.16.5](https://github.com/CoCreate-app/CoCreate-text/compare/v1.16.4...v1.16.5) (2022-06-22)
2
28
 
3
29
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/text",
3
- "version": "1.16.5",
3
+ "version": "1.17.2",
4
4
  "description": "A simple text component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "text",
@@ -69,6 +69,7 @@
69
69
  "@cocreate/hosting": "^1.3.3",
70
70
  "@cocreate/observer": "^1.5.3",
71
71
  "@cocreate/selection": "^1.4.11",
72
+ "@cocreate/utils": "^1.7.4",
72
73
  "@cocreate/uuid": "^1.2.4"
73
74
  }
74
75
  }
package/src/updateDom.js CHANGED
@@ -11,9 +11,28 @@ export function updateDom({domTextEditor, value, start, end, html}) {
11
11
  let {element, path, position, type} = getElementPosition(domTextEditor.htmlString, start, end);
12
12
  parseHtml(domTextEditor, html);
13
13
 
14
- let domEl, oldEl, curCaret;
15
- let newEl = domTextEditor.newHtml.querySelector(path);
14
+ let domEl, oldEl, curCaret, newEl;
15
+ // console.log('element', element)
16
+ // console.log('path', path)
17
+
18
+ try {
19
+ newEl = domTextEditor.newHtml.querySelector(path);
20
+ } catch(err){
21
+ console.log('error', err)
22
+ }
23
+
24
+ if (path && !newEl){
25
+ let index
26
+ do {
27
+ index = path.lastIndexOf(' >')
28
+ if (index != -1)
29
+ path = path.slice(0, index)
30
+ newEl = domTextEditor.newHtml.querySelector(path);
31
+ } while (!newEl && index != -1)
32
+ }
33
+
16
34
  if(!newEl){
35
+ // console.log("no newEL", path)
17
36
  newEl = domTextEditor.cloneNode(true);
18
37
  if (html != undefined)
19
38
  newEl.innerHTML = html;
@@ -23,90 +42,136 @@ export function updateDom({domTextEditor, value, start, end, html}) {
23
42
  type = 'innerHTML';
24
43
  }
25
44
  else if(element.tagName == 'HTML') {
45
+ // console.log('element = html')
26
46
  domEl = domTextEditor;
27
47
  type = 'innerHTML';
28
48
  }
29
49
  else if(path) {
50
+ // console.log("else path", path)
30
51
  domEl = domTextEditor.querySelector(path);
31
- oldEl = domTextEditor.oldHtml.querySelector(path);
32
- if (!domEl || !oldEl){
33
- let eid = newEl.getAttribute('eid');
34
- if (!domEl && eid){
35
- domEl = domTextEditor.querySelector(`[eid='${eid}']`);
36
- }
37
- if (!oldEl && eid){
38
- oldEl = domTextEditor.oldHtml.querySelector(`[eid='${eid}']`);
39
- }
40
- }
52
+ // if (!domEl || !oldEl){
53
+ // let eid = newEl.getAttribute('eid');
54
+ // if (!domEl && eid){
55
+ // domEl = domTextEditor.querySelector(`[eid='${eid}']`);
56
+ // }
57
+ // if (!oldEl && eid){
58
+ // oldEl = domTextEditor.oldHtml.querySelector(`[eid='${eid}']`);
59
+ // }
60
+ // }
41
61
  }
42
62
 
43
- let activeElement = domEl.ownerDocument.activeElement;
44
- if (activeElement == domEl)
45
- curCaret = getSelection(activeElement);
46
- else if (activeElement.tagName == 'BODY')
47
- curCaret = getSelection(domEl);
48
- else
49
- curCaret = getSelection(activeElement);
50
-
51
- if (!value && type != 'isStartTag' && type != 'textNode'){
52
- type = 'innerHTML';
63
+ if (!domEl) {
64
+ // console.log('no domEl')
65
+ let index
66
+ do {
67
+ index = path.lastIndexOf(' >')
68
+ if (index != -1)
69
+ path = path.slice(0, index)
70
+ domEl = domTextEditor.querySelector(path);
71
+ } while (!domEl && index != -1)
72
+
73
+ if (domEl) {
74
+ newEl = domTextEditor.newHtml.querySelector(path);
75
+ }
76
+
77
+ if (!domEl || !newEl){
78
+ newEl = domTextEditor.cloneNode(true);
79
+ if (html != undefined)
80
+ newEl.innerHTML = html;
81
+ else
82
+ newEl.innerHTML = domTextEditor.htmlString;
83
+ domEl = domTextEditor;
84
+ type = 'innerHTML';
85
+ }
53
86
  }
54
-
87
+
55
88
  if(domEl && newEl) {
89
+ let activeElement = domEl.ownerDocument.activeElement;
90
+ if (activeElement == domEl)
91
+ curCaret = getSelection(activeElement);
92
+ else if (activeElement && activeElement.tagName == 'BODY')
93
+ curCaret = getSelection(domEl);
94
+ else
95
+ curCaret = getSelection(activeElement);
96
+
97
+
98
+ if (!value && type != 'isStartTag' && type != 'textNode'){
99
+ type = 'innerHTML';
100
+ }
101
+
102
+ // console.log('domEl', domEl)
103
+ // console.log('newEl', newEl)
56
104
  if(start != end && type == 'innerHTML') {
57
105
  domTextEditor.htmlString = html;
58
106
  if (domEl.tagName != 'HTML'){
59
- if (newEl.parentElement)
107
+ if (newEl.parentElement) {
60
108
  domEl.parentElement.replaceChildren(...newEl.parentElement.childNodes);
61
- else
109
+ // console.log('parent', domEl.parentElement)
110
+ } else {
62
111
  domEl.replaceChildren(...newEl.childNodes);
112
+ // console.log('domEl', domEl)
113
+ }
63
114
  }
64
- else
115
+ else {
65
116
  domEl.replaceChildren(...newEl.childNodes);
66
- // domEl = newEl;
117
+ // console.log('Html tag', domEl)
118
+ }
67
119
  if (curCaret && curCaret.range) {
68
120
  curCaret.range.startContainer = domEl;
69
121
  curCaret.range.endContainer = domEl;
70
122
  }
71
123
  }
72
- else if (type == 'isStartTag')
124
+ else if (type == 'isStartTag') {
125
+ oldEl = domTextEditor.oldHtml.querySelector(path);
126
+ if (!oldEl && domEl.tagName == 'HTML')
127
+ oldEl = domTextEditor.oldHtml
73
128
  assignAttributes(newEl, oldEl, domEl);
74
- else if (type == 'insertAdjacent')
129
+ // console.log('isStartTag', domEl, newEl)
130
+
131
+ }
132
+ else if (type == 'insertAdjacent') {
75
133
  domEl.insertAdjacentHTML(position, value);
134
+ // console.log('insertAdjacent', domEl, value)
135
+ }
76
136
  else if (type == 'textNode'){
77
137
  if(start != end)
78
138
  domTextEditor.htmlString = html;
79
139
  domEl.innerHTML = newEl.innerHTML;
140
+ // console.log('textnode', domEl.innerHTML, newEl.innerHTML)
141
+
80
142
  }
81
143
  else if (type == 'innerHTML') {
82
144
  domEl.replaceChildren(...newEl.childNodes);
145
+ // console.log('innerHtml', domEl, newEl)
83
146
  }
84
147
  domTextEditor.htmlString = html;
85
- }
86
- if(curCaret && start >= 0 && end >= 0) {
87
- if (curCaret.range && curCaret.start >= curCaret.range.startOffset) {
88
- let p = processSelection(domEl, value, curCaret.start, curCaret.end, start, end, curCaret.range);
89
- sendPosition(domEl);
90
- _dispatchInputEvent(p.element, p.value, p.start, p.end, p.prev_start, p.prev_end);
91
- }
92
- }
93
-
94
- if (newEl.tagName == 'HTML' || 'HEAD' || 'BODY' || 'SCRIPT'){
95
- let scripts;
96
- if (newEl.tagName == 'SCRIPT'){
97
- scripts = [newEl];
98
- }
99
- else{
100
- scripts = domEl.querySelectorAll('script');
148
+
149
+ if(curCaret && start >= 0 && end >= 0) {
150
+ if (curCaret.range.startContainer == domEl)
151
+ if (curCaret.range && curCaret.start >= curCaret.range.startOffset) {
152
+ let p = processSelection(domEl, value, curCaret.start, curCaret.end, start, end, curCaret.range);
153
+ sendPosition(domEl);
154
+ _dispatchInputEvent(p.element, p.value, p.start, p.end, p.prev_start, p.prev_end);
155
+ }
101
156
  }
102
- for (let script of scripts) {
103
- let newScript = domEl.ownerDocument.createElement('script');
104
- for(let attribute of script.attributes) {
105
- newScript.setAttribute(attribute.name, attribute.value);
157
+
158
+ if (['HTML', 'HEAD', 'BODY', 'SCRIPT'].includes(newEl.tagName)){
159
+ let scripts;
160
+ if (newEl.tagName == 'SCRIPT'){
161
+ scripts = [newEl];
106
162
  }
107
- newScript.innerHTML = script.innerHTML;
108
- script.replaceWith(newScript);
109
- }
163
+ else{
164
+ scripts = domEl.querySelectorAll('script');
165
+ }
166
+ for (let script of scripts) {
167
+ let newScript = domEl.ownerDocument.createElement('script');
168
+ for(let attribute of script.attributes) {
169
+ newScript.setAttribute(attribute.name, attribute.value);
170
+ }
171
+ newScript.innerHTML = script.innerHTML;
172
+ script.replaceWith(newScript);
173
+ }
174
+ }
110
175
  }
111
176
  }
112
177