@webtides/element-js 0.7.4 → 1.0.0-alpha.10
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/README.md +2 -2
- package/docs/README.md +174 -0
- package/docs/concepts/classes.md +167 -0
- package/docs/concepts/hierarchy.md +61 -0
- package/docs/concepts/lifecycle.md +45 -0
- package/docs/concepts/reactivity.md +14 -0
- package/docs/features/computed-properties.md +28 -0
- package/docs/features/dependency-injection-context-protocol.md +132 -0
- package/docs/features/events.md +155 -0
- package/docs/features/global-styles.md +131 -0
- package/docs/features/methods.md +34 -0
- package/docs/features/properties.md +56 -0
- package/docs/features/refs.md +47 -0
- package/docs/features/renderless.md +70 -0
- package/docs/features/store.md +143 -0
- package/docs/features/styles.md +169 -0
- package/docs/features/templates.md +308 -0
- package/docs/features/watch.md +54 -0
- package/docs/getting-started/upgrade-to-v1.md +243 -0
- package/docs/guide/guide.md +112 -0
- package/index.js +9 -2
- package/package.json +19 -27
- package/src/BaseElement.js +176 -86
- package/src/StyledElement.js +66 -55
- package/src/TemplateElement.js +34 -8
- package/src/dom-parts/AttributePart.js +167 -0
- package/src/dom-parts/ChildNodePart.js +329 -0
- package/src/dom-parts/NodePart.js +38 -0
- package/src/dom-parts/Part.js +12 -0
- package/src/dom-parts/TemplatePart.js +134 -0
- package/src/dom-parts/TemplateResult.js +342 -0
- package/src/dom-parts/directives.js +111 -0
- package/src/dom-parts/html.js +12 -0
- package/src/dom-parts/render.js +10 -0
- package/src/util/AttributeParser.js +62 -29
- package/src/util/DOMHelper.js +253 -5
- package/src/util/Directive.js +42 -0
- package/src/util/GlobalStylesStore.js +66 -0
- package/src/util/SerializeStateHelper.js +103 -0
- package/src/util/Store.js +92 -9
- package/src/util/defineElement.js +2 -2
- package/src/util/toString.js +2 -2
- package/CHANGELOG.md +0 -154
- package/dist/index.js +0 -1673
- package/dist/index.js.map +0 -7
- package/src/renderer/vanilla/TemplateElement.js +0 -50
- package/src/renderer/vanilla/index.js +0 -18
- package/src/renderer/vanilla/util/directives.js +0 -78
- package/src/renderer/vanilla/util/html.js +0 -65
- package/src/renderer/vanilla/util/render.js +0 -149
- package/src/util/i18n.js +0 -20
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { TemplateElement } from '../TemplateElement.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Parses the given template string and returns a real DOM element.
|
|
5
|
-
* @param {string} template
|
|
6
|
-
* @returns {HTMLElement}
|
|
7
|
-
*/
|
|
8
|
-
const convertStringToHTML = (template) => {
|
|
9
|
-
const parser = new DOMParser();
|
|
10
|
-
const document = parser.parseFromString(template.toString(), 'text/html');
|
|
11
|
-
return document.body;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Diff the attributes for a live DOM element against a template DOM element
|
|
16
|
-
* @param {Element} templateElement The new template
|
|
17
|
-
* @param {Element} domElement The existing DOM node
|
|
18
|
-
*/
|
|
19
|
-
const diffAttributes = function (templateElement, domElement) {
|
|
20
|
-
// remove or update previous attributes
|
|
21
|
-
for (/** @type {Attr} */ const attribute of Array.from(domElement.attributes)) {
|
|
22
|
-
if (templateElement.hasAttribute(attribute.name)) {
|
|
23
|
-
const oldValue = attribute.value;
|
|
24
|
-
const newValue = templateElement.getAttribute(attribute.name) || '';
|
|
25
|
-
if (oldValue !== newValue) {
|
|
26
|
-
domElement.setAttribute(attribute.name, newValue);
|
|
27
|
-
}
|
|
28
|
-
} else {
|
|
29
|
-
domElement.removeAttribute(attribute.name);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// set newly added attributes
|
|
34
|
-
for (/** @type {Attr} */ const attribute of Array.from(templateElement.attributes)) {
|
|
35
|
-
if (!domElement.hasAttribute(attribute.name)) {
|
|
36
|
-
domElement.setAttribute(attribute.name, attribute.value);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Checks whether the given element or node is a TemplateElement or not
|
|
43
|
-
* @param {Element|Node} element
|
|
44
|
-
* @returns {boolean}
|
|
45
|
-
*/
|
|
46
|
-
const isTemplateElement = (element) => {
|
|
47
|
-
const tagName = element.tagName?.toLowerCase() ?? false;
|
|
48
|
-
if (!tagName || !tagName.includes('-') || element.shadowRoot) {
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
return element instanceof TemplateElement;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Diff a live DOM node against a template DOM node
|
|
56
|
-
* @param {Node} templateNode
|
|
57
|
-
* @param {Node} domNode
|
|
58
|
-
*/
|
|
59
|
-
const diffNodes = function (templateNode, domNode) {
|
|
60
|
-
const templateChildNodes = [...templateNode.childNodes];
|
|
61
|
-
const domChildNodes = [...domNode.childNodes];
|
|
62
|
-
|
|
63
|
-
// Diff each node in the child node lists
|
|
64
|
-
const length = Math.max(templateChildNodes.length, domChildNodes.length);
|
|
65
|
-
for (let index = 0; index < length; index++) {
|
|
66
|
-
const templateChildNode = templateChildNodes[index];
|
|
67
|
-
const domChildNode = domChildNodes[index];
|
|
68
|
-
|
|
69
|
-
// If the DOM node doesn't exist, append/copy the template node
|
|
70
|
-
if (!domChildNode) {
|
|
71
|
-
domNode.appendChild(templateChildNode);
|
|
72
|
-
continue;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// If the template node doesn't exist, remove the node in the DOM
|
|
76
|
-
if (!templateChildNode) {
|
|
77
|
-
domChildNode.parentNode.removeChild(domChildNode);
|
|
78
|
-
continue;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// If DOM node is equal to the template node, don't do anything
|
|
82
|
-
if (domChildNode.isEqualNode(templateChildNode)) {
|
|
83
|
-
continue;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// If node types are not the same, replace the DOM node with the template node
|
|
87
|
-
if (templateChildNode.nodeType !== domChildNode.nodeType) {
|
|
88
|
-
domChildNode.parentNode.replaceChild(templateChildNode, domChildNode);
|
|
89
|
-
continue;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// If the element tag names are not the same, replace the DOM node with the template node
|
|
93
|
-
if (templateChildNode.nodeType === 1 && templateChildNode.tagName !== domChildNode.tagName) {
|
|
94
|
-
domChildNode.parentNode.replaceChild(templateChildNode, domChildNode);
|
|
95
|
-
continue;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// If the node is an SVG element, don't even think about diffing it, just replace it
|
|
99
|
-
if (templateChildNode.nodeType === 1 && templateChildNode.tagName === 'SVG') {
|
|
100
|
-
domChildNode.parentNode.replaceChild(templateChildNode, domChildNode);
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// If the node is an Element node, diff the attributes
|
|
105
|
-
if (templateChildNode.nodeType === 1) {
|
|
106
|
-
diffAttributes(templateChildNode, domChildNode);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// If the node is a Text node or a Comment node, update it
|
|
110
|
-
if (domChildNode.nodeType === 3 || domChildNode.nodeType === 8) {
|
|
111
|
-
domChildNode.textContent = templateChildNode.textContent;
|
|
112
|
-
continue;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// If the DOM node should be empty, remove all child nodes
|
|
116
|
-
if (domChildNode.hasChildNodes() && !templateChildNode.hasChildNodes()) {
|
|
117
|
-
domChildNode.replaceChildren();
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// If the DOM node is empty, but should have children, add child nodes from the template node
|
|
122
|
-
if (!domChildNode.hasChildNodes() && templateChildNode.hasChildNodes()) {
|
|
123
|
-
domChildNode.replaceChildren(...templateChildNode.childNodes);
|
|
124
|
-
continue;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// If there are child nodes, diff them recursively
|
|
128
|
-
if (templateChildNode.hasChildNodes()) {
|
|
129
|
-
// https://stackoverflow.com/questions/46898739/why-domparser-doesnt-use-the-customelements-registered
|
|
130
|
-
// we have to use the domChildNode here because the templateChildNode is not upgraded because it was not
|
|
131
|
-
// imported into a document yet and therefore instanceOf will not work...
|
|
132
|
-
if (!isTemplateElement(domChildNode)) {
|
|
133
|
-
diffNodes(templateChildNode, domChildNode);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Render a template string into the given DOM node
|
|
141
|
-
* @param {string} template
|
|
142
|
-
* @param {Node} domNode
|
|
143
|
-
*/
|
|
144
|
-
const render = (template, domNode) => {
|
|
145
|
-
const templateNode = convertStringToHTML(template);
|
|
146
|
-
diffNodes(templateNode, domNode);
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
export { render };
|
package/src/util/i18n.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Retrieves a translated key from a dictionary on the window object
|
|
3
|
-
* Example: ${i18n('CustomElement.buttonLabel', 'Label')}
|
|
4
|
-
* @param key to be translated
|
|
5
|
-
* @param fallback to be used if key is not defined
|
|
6
|
-
* @return String of the translated key or fallback or original key
|
|
7
|
-
*/
|
|
8
|
-
export function i18n(key, fallback) {
|
|
9
|
-
try {
|
|
10
|
-
const translations = window.elementjs.i18n();
|
|
11
|
-
if (translations[key] === undefined) {
|
|
12
|
-
throw 'Translation Missing';
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return translations[key];
|
|
16
|
-
} catch (err) {
|
|
17
|
-
if (fallback) return fallback;
|
|
18
|
-
else return key;
|
|
19
|
-
}
|
|
20
|
-
}
|