@cocreate/text 1.17.0 → 1.17.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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [1.17.1](https://github.com/CoCreate-app/CoCreate-text/compare/v1.17.0...v1.17.1) (2022-06-24)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * imporved handeling of path fallback to html element if no matches amoung parentElements ([5c331d0](https://github.com/CoCreate-app/CoCreate-text/commit/5c331d0d32ffd60587f684b44e69766097193c33))
7
+
1
8
  # [1.17.0](https://github.com/CoCreate-app/CoCreate-text/compare/v1.16.5...v1.17.0) (2022-06-23)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/text",
3
- "version": "1.17.0",
3
+ "version": "1.17.1",
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
@@ -12,28 +12,27 @@ export function updateDom({domTextEditor, value, start, end, html}) {
12
12
  parseHtml(domTextEditor, html);
13
13
 
14
14
  let domEl, oldEl, curCaret, newEl;
15
- if (path && path.includes('<')) {
16
- let index = path.lastIndexOf(' >')
17
- if (index != -1)
18
- path = path.slice(0, index)
19
- else{
20
- index = path.lastIndexOf('<')
21
- path = path.slice(0, index)
22
- }
23
- }
15
+ // console.log('element', element)
16
+ // console.log('path', path)
24
17
 
25
18
  try {
26
19
  newEl = domTextEditor.newHtml.querySelector(path);
27
20
  } catch(err){
28
21
  console.log('error', err)
29
22
  }
23
+
30
24
  if (path && !newEl){
31
- let index = path.lastIndexOf(' >')
32
- if (index != -1)
33
- path = path.slice(0, index)
34
- newEl = domTextEditor.newHtml.querySelector(path);
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)
35
32
  }
33
+
36
34
  if(!newEl){
35
+ // console.log("no newEL", path)
37
36
  newEl = domTextEditor.cloneNode(true);
38
37
  if (html != undefined)
39
38
  newEl.innerHTML = html;
@@ -43,38 +42,65 @@ export function updateDom({domTextEditor, value, start, end, html}) {
43
42
  type = 'innerHTML';
44
43
  }
45
44
  else if(element.tagName == 'HTML') {
45
+ // console.log('element = html')
46
46
  domEl = domTextEditor;
47
47
  type = 'innerHTML';
48
48
  }
49
49
  else if(path) {
50
+ // console.log("else path", path)
50
51
  domEl = domTextEditor.querySelector(path);
51
- oldEl = domTextEditor.oldHtml.querySelector(path);
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
- }
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
+ // }
61
+ }
62
+
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';
60
85
  }
61
86
  }
62
87
 
63
- if (domEl) {
88
+ if(domEl && newEl) {
64
89
  let activeElement = domEl.ownerDocument.activeElement;
65
90
  if (activeElement == domEl)
66
91
  curCaret = getSelection(activeElement);
67
- else if (activeElement.tagName == 'BODY')
92
+ else if (activeElement && activeElement.tagName == 'BODY')
68
93
  curCaret = getSelection(domEl);
69
94
  else
70
95
  curCaret = getSelection(activeElement);
71
- }
96
+
72
97
 
73
- if (!value && type != 'isStartTag' && type != 'textNode'){
74
- type = 'innerHTML';
75
- }
98
+ if (!value && type != 'isStartTag' && type != 'textNode'){
99
+ type = 'innerHTML';
100
+ }
76
101
 
77
- if(domEl && newEl) {
102
+ // console.log('domEl', domEl)
103
+ // console.log('newEl', newEl)
78
104
  if(start != end && type == 'innerHTML') {
79
105
  domTextEditor.htmlString = html;
80
106
  if (domEl.tagName != 'HTML'){
@@ -96,6 +122,9 @@ export function updateDom({domTextEditor, value, start, end, html}) {
96
122
  }
97
123
  }
98
124
  else if (type == 'isStartTag') {
125
+ oldEl = domTextEditor.oldHtml.querySelector(path);
126
+ if (!oldEl && domEl.tagName == 'HTML')
127
+ oldEl = domTextEditor.oldHtml
99
128
  assignAttributes(newEl, oldEl, domEl);
100
129
  // console.log('isStartTag', domEl, newEl)
101
130
 
@@ -116,31 +145,32 @@ export function updateDom({domTextEditor, value, start, end, html}) {
116
145
  // console.log('innerHtml', domEl, newEl)
117
146
  }
118
147
  domTextEditor.htmlString = html;
119
- }
120
- if(curCaret && start >= 0 && end >= 0) {
121
- if (curCaret.range && curCaret.start >= curCaret.range.startOffset) {
122
- let p = processSelection(domEl, value, curCaret.start, curCaret.end, start, end, curCaret.range);
123
- sendPosition(domEl);
124
- _dispatchInputEvent(p.element, p.value, p.start, p.end, p.prev_start, p.prev_end);
125
- }
126
- }
127
-
128
- if (newEl.tagName == 'HTML' || 'HEAD' || 'BODY' || 'SCRIPT'){
129
- let scripts;
130
- if (newEl.tagName == 'SCRIPT'){
131
- scripts = [newEl];
132
- }
133
- else{
134
- scripts = domEl.querySelectorAll('script');
148
+
149
+ if(curCaret && start >= 0 && end >= 0) {
150
+ if (curCaret.range && curCaret.start >= curCaret.range.startOffset) {
151
+ let p = processSelection(domEl, value, curCaret.start, curCaret.end, start, end, curCaret.range);
152
+ sendPosition(domEl);
153
+ _dispatchInputEvent(p.element, p.value, p.start, p.end, p.prev_start, p.prev_end);
154
+ }
135
155
  }
136
- for (let script of scripts) {
137
- let newScript = domEl.ownerDocument.createElement('script');
138
- for(let attribute of script.attributes) {
139
- newScript.setAttribute(attribute.name, attribute.value);
156
+
157
+ if (['HTML', 'HEAD', 'BODY', 'SCRIPT'].includes(newEl.tagName)){
158
+ let scripts;
159
+ if (newEl.tagName == 'SCRIPT'){
160
+ scripts = [newEl];
140
161
  }
141
- newScript.innerHTML = script.innerHTML;
142
- script.replaceWith(newScript);
143
- }
162
+ else{
163
+ scripts = domEl.querySelectorAll('script');
164
+ }
165
+ for (let script of scripts) {
166
+ let newScript = domEl.ownerDocument.createElement('script');
167
+ for(let attribute of script.attributes) {
168
+ newScript.setAttribute(attribute.name, attribute.value);
169
+ }
170
+ newScript.innerHTML = script.innerHTML;
171
+ script.replaceWith(newScript);
172
+ }
173
+ }
144
174
  }
145
175
  }
146
176