@github/template-parts
Version:
An implementation of the TemplateInstance proposal (https://github.com/w3c/webcomponents/blob/159b1600bab02fe9cd794825440a98537d53b389/proposals/Template-Instantiation.md)
40 lines • 1.32 kB
JavaScript
const parts = new WeakMap();
export class NodeTemplatePart {
constructor(node, expression) {
this.expression = expression;
parts.set(this, [node]);
node.textContent = '';
}
get value() {
return parts
.get(this)
.map(node => node.textContent)
.join('');
}
set value(string) {
this.replace(string);
}
get previousSibling() {
return parts.get(this)[0].previousSibling;
}
get nextSibling() {
return parts.get(this)[parts.get(this).length - 1].nextSibling;
}
replace(...nodes) {
var _a, _b;
const normalisedNodes = nodes.map(node => {
if (typeof node === 'string')
return new Text(node);
return node;
});
if (!normalisedNodes.length)
normalisedNodes.push(new Text(''));
const node = parts.get(this)[0];
for (const normalisedNode of normalisedNodes)
(_a = node.parentNode) === null || _a === void 0 ? void 0 : _a.insertBefore(normalisedNode, node);
for (const part of parts.get(this))
(_b = part.parentNode) === null || _b === void 0 ? void 0 : _b.removeChild(part);
parts.set(this, normalisedNodes);
}
}
//# sourceMappingURL=node-template-part.js.map