Skip to content

Commit 41814a6

Browse files
committed
fix: refactor: Format setValue.js and refactor setValue function
The commit message describes the changes made to the file `setValue.js`. The code formatting has been updated and setValue function has been refactored to include additional elements that were not previously incorporated.
1 parent 129d31b commit 41814a6

File tree

1 file changed

+172
-172
lines changed

1 file changed

+172
-172
lines changed

src/setValue.js

Lines changed: 172 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -1,198 +1,198 @@
1-
import {getAttributes} from '@cocreate/utils';
1+
import { getAttributes } from '@cocreate/utils';
22

33

4-
HTMLElement.prototype.setValue = function(value) {
5-
setValue(this, value)
4+
HTMLElement.prototype.setValue = function (value) {
5+
setValue(this, value)
66
};
77

8-
HTMLInputElement.prototype.setValue = function(value) {
9-
setValue(this, value)
8+
HTMLInputElement.prototype.setValue = function (value) {
9+
setValue(this, value)
1010
};
1111

1212

13-
HTMLHeadingElement.prototype.setValue = function(value) {
14-
setValue(this, value)
13+
HTMLHeadingElement.prototype.setValue = function (value) {
14+
setValue(this, value)
1515
};
1616

1717

1818
const setValue = (el, value) => {
19-
if (value === null || value === undefined) return;
20-
let valueType = el.getAttribute('value-type');
21-
let prefix = el.getAttribute('value-prefix') || "";
22-
let suffix = el.getAttribute('value-suffix') || "";
23-
if (prefix)
24-
value = text.replace(prefix, "");
25-
if (suffix)
26-
value = text.replace(suffix, "");
27-
28-
if (el.tagName == 'INPUT' || el.tagName == 'TEXTAREA' || el.tagName == 'SELECT') {
29-
let {isCrdt} = getAttributes(el)
30-
if (isCrdt == null || isCrdt == undefined)
31-
isCrdt = el.getAttribute('crdt')
32-
if (isCrdt == "true" || el.type === 'file') return;
33-
34-
if (el.type == 'checkbox') {
35-
let inputs = [el]
36-
let name = el.getAttribute('name');
37-
if (name)
38-
inputs = document.querySelectorAll(`input[type="${el.type}"][name="${name}"]`);
39-
40-
for (let i = 0; i < inputs.length; i++) {
41-
if (inputs[i].value) {
42-
if (value.includes(inputs[i].value))
43-
inputs[i].checked = true;
44-
else
45-
inputs[i].checked = false;
46-
} else {
47-
if (value === 'true' || value === true || value === 'checked')
48-
inputs[i].checked = true;
49-
else
50-
inputs[i].checked = false;
51-
52-
}
53-
}
54-
}
55-
else if (el.type === 'radio') {
56-
el.value == value ? el.checked = true : el.checked = false;
57-
}
58-
else if (el.type === 'password') {
59-
el.value = __decryptPassword(value);
60-
}
61-
else if (el.tagName == "SELECT" && el.hasAttribute('multiple') && Array.isArray(value)) {
62-
let options = el.options;
63-
for (let i = 0; i < options.length; i++) {
64-
if (value.includes(options[i].value)) {
65-
options[i].selected = "selected";
66-
} else {
67-
options[i].selected = "";
68-
}
69-
}
70-
}
71-
else
72-
el.value = value;
73-
dispatchEvents(el)
74-
}
75-
else if (el.tagName === 'IMG' || el.tagName === 'SOURCE')
76-
el.src = value;
77-
78-
else if (el.tagName === 'IFRAME')
79-
el.srcdoc = value;
80-
81-
else if (el.tagName === 'SCRIPT')
82-
setScript(el, value);
83-
84-
else {
85-
if (el.hasAttribute('contenteditable') && el == document.activeElement) return;
86-
// if (el.tagName === 'DIV') {
87-
// if (!el.classList.contains('domEditor')
88-
// return
89-
// }
90-
91-
if (valueType == 'string' || valueType == 'text')
92-
el.textContent = value;
93-
else {
94-
let newElement = document.createElement("div");
95-
newElement.innerHTML = value;
96-
setPass(newElement)
97-
98-
let CoCreateJS = newElement.querySelector('script[src*="CoCreate.js"], script[src*="CoCreate.min.js"]')
99-
if (CoCreateJS)
100-
CoCreateJS.remove()
101-
102-
let CoCreateCSS = newElement.querySelector('link[href*="CoCreate.css"], link[href*="CoCreate.min.css"]')
103-
if (CoCreateCSS)
104-
CoCreateCSS.remove()
105-
106-
let css = newElement.querySelector('link[collection], link[document]')
107-
if (css)
108-
css.remove()
109-
110-
if (el.getAttribute('domEditor') == "replace") {
111-
let parentNode = el.parentNode;
112-
if (parentNode) {
113-
if (newElement.children[0]) {
114-
parentNode.replaceChild(newElement.children[0], el);
115-
}
116-
else {
117-
parentNode.replaceChild(newElement, el);
118-
}
119-
}
120-
} else {
121-
el.innerHTML = newElement.innerHTML;
122-
}
123-
}
124-
125-
if (el.hasAttribute("value")) {
126-
el.setAttribute("value", value);
127-
}
128-
}
129-
130-
if (el.getAttribute('contenteditable'))
131-
dispatchEvents(el);
132-
133-
if (el.tagName == 'HEAD' || el.tagName == 'BODY') {
134-
el.removeAttribute('collection');
135-
el.removeAttribute('document_id');
136-
el.removeAttribute('pass_id');
137-
138-
let scripts = el.querySelectorAll('script');
139-
for (let script of scripts) {
140-
setScript(script)
141-
}
142-
}
19+
if (value === null || value === undefined) return;
20+
let valueType = el.getAttribute('value-type');
21+
let prefix = el.getAttribute('value-prefix') || "";
22+
let suffix = el.getAttribute('value-suffix') || "";
23+
if (prefix)
24+
value = value.replace(prefix, "");
25+
if (suffix)
26+
value = value.replace(suffix, "");
27+
28+
if (el.tagName == 'INPUT' || el.tagName == 'TEXTAREA' || el.tagName == 'SELECT') {
29+
let { isCrdt } = getAttributes(el)
30+
if (isCrdt == null || isCrdt == undefined)
31+
isCrdt = el.getAttribute('crdt')
32+
if (isCrdt == "true" || el.type === 'file') return;
33+
34+
if (el.type == 'checkbox') {
35+
let inputs = [el]
36+
let name = el.getAttribute('name');
37+
if (name)
38+
inputs = document.querySelectorAll(`input[type="${el.type}"][name="${name}"]`);
39+
40+
for (let i = 0; i < inputs.length; i++) {
41+
if (inputs[i].value) {
42+
if (value.includes(inputs[i].value))
43+
inputs[i].checked = true;
44+
else
45+
inputs[i].checked = false;
46+
} else {
47+
if (value === 'true' || value === true || value === 'checked')
48+
inputs[i].checked = true;
49+
else
50+
inputs[i].checked = false;
51+
52+
}
53+
}
54+
}
55+
else if (el.type === 'radio') {
56+
el.value == value ? el.checked = true : el.checked = false;
57+
}
58+
else if (el.type === 'password') {
59+
el.value = __decryptPassword(value);
60+
}
61+
else if (el.tagName == "SELECT" && el.hasAttribute('multiple') && Array.isArray(value)) {
62+
let options = el.options;
63+
for (let i = 0; i < options.length; i++) {
64+
if (value.includes(options[i].value)) {
65+
options[i].selected = "selected";
66+
} else {
67+
options[i].selected = "";
68+
}
69+
}
70+
}
71+
else
72+
el.value = value;
73+
dispatchEvents(el)
74+
}
75+
else if (el.tagName === 'IMG' || el.tagName === 'SOURCE')
76+
el.src = value;
77+
78+
else if (el.tagName === 'IFRAME')
79+
el.srcdoc = value;
80+
81+
else if (el.tagName === 'SCRIPT')
82+
setScript(el, value);
83+
84+
else {
85+
if (el.hasAttribute('contenteditable') && el == document.activeElement) return;
86+
// if (el.tagName === 'DIV') {
87+
// if (!el.classList.contains('domEditor')
88+
// return
89+
// }
90+
91+
if (valueType == 'string' || valueType == 'text')
92+
el.textContent = value;
93+
else {
94+
let newElement = document.createElement("div");
95+
newElement.innerHTML = value;
96+
setPass(newElement)
97+
98+
let CoCreateJS = newElement.querySelector('script[src*="CoCreate.js"], script[src*="CoCreate.min.js"]')
99+
if (CoCreateJS)
100+
CoCreateJS.remove()
101+
102+
let CoCreateCSS = newElement.querySelector('link[href*="CoCreate.css"], link[href*="CoCreate.min.css"]')
103+
if (CoCreateCSS)
104+
CoCreateCSS.remove()
105+
106+
let css = newElement.querySelector('link[collection], link[document]')
107+
if (css)
108+
css.remove()
109+
110+
if (el.getAttribute('domEditor') == "replace") {
111+
let parentNode = el.parentNode;
112+
if (parentNode) {
113+
if (newElement.children[0]) {
114+
parentNode.replaceChild(newElement.children[0], el);
115+
}
116+
else {
117+
parentNode.replaceChild(newElement, el);
118+
}
119+
}
120+
} else {
121+
el.innerHTML = newElement.innerHTML;
122+
}
123+
}
124+
125+
if (el.hasAttribute("value")) {
126+
el.setAttribute("value", value);
127+
}
128+
}
129+
130+
if (el.getAttribute('contenteditable'))
131+
dispatchEvents(el);
132+
133+
if (el.tagName == 'HEAD' || el.tagName == 'BODY') {
134+
el.removeAttribute('collection');
135+
el.removeAttribute('document_id');
136+
el.removeAttribute('pass_id');
137+
138+
let scripts = el.querySelectorAll('script');
139+
for (let script of scripts) {
140+
setScript(script)
141+
}
142+
}
143143
};
144144

145145
function setPass(el) {
146-
if (CoCreate.pass) {
147-
let passElements = el.querySelectorAll('[pass_id]');
148-
if (passElements)
149-
CoCreate.pass.initElements(passElements)
150-
}
146+
if (CoCreate.pass) {
147+
let passElements = el.querySelectorAll('[pass_id]');
148+
if (passElements)
149+
CoCreate.pass.initElements(passElements)
150+
}
151151
}
152152

153153
function setScript(script, value) {
154-
let newScript = document.createElement('script');
155-
newScript.attributes = script.attributes;
156-
newScript.innerHTML = script.innerHTML;
157-
if (value) {
158-
if (script.hasAttribute("src"))
159-
newScript.src = value;
160-
else
161-
newScript.innerHTML = value;
162-
}
163-
script.replaceWith(newScript);
154+
let newScript = document.createElement('script');
155+
newScript.attributes = script.attributes;
156+
newScript.innerHTML = script.innerHTML;
157+
if (value) {
158+
if (script.hasAttribute("src"))
159+
newScript.src = value;
160+
else
161+
newScript.innerHTML = value;
162+
}
163+
script.replaceWith(newScript);
164164
}
165165

166166
function __decryptPassword(str) {
167-
if (!str) return "";
168-
let decode_str = atob(str);
169-
return decode_str;
167+
if (!str) return "";
168+
let decode_str = atob(str);
169+
return decode_str;
170170
}
171171

172172
function dispatchEvents(el) {
173-
let inputEvent = new CustomEvent('input', {
174-
bubbles: true,
175-
detail: {
176-
skip: true
177-
},
178-
});
179-
Object.defineProperty(inputEvent, 'target', {
180-
writable: false,
181-
value: el
182-
});
183-
el.dispatchEvent(inputEvent);
184-
185-
let changeEvent = new CustomEvent('change', {
186-
bubbles: true,
187-
detail: {
188-
skip: true
189-
},
190-
});
191-
Object.defineProperty(changeEvent, 'target', {
192-
writable: false,
193-
value: el
194-
});
195-
el.dispatchEvent(changeEvent);
173+
let inputEvent = new CustomEvent('input', {
174+
bubbles: true,
175+
detail: {
176+
skip: true
177+
},
178+
});
179+
Object.defineProperty(inputEvent, 'target', {
180+
writable: false,
181+
value: el
182+
});
183+
el.dispatchEvent(inputEvent);
184+
185+
let changeEvent = new CustomEvent('change', {
186+
bubbles: true,
187+
detail: {
188+
skip: true
189+
},
190+
});
191+
Object.defineProperty(changeEvent, 'target', {
192+
writable: false,
193+
value: el
194+
});
195+
el.dispatchEvent(changeEvent);
196196
}
197197

198198

0 commit comments

Comments
 (0)