Skip to content

Commit 7731003

Browse files
committed
create TextComponent
1 parent 2810ea1 commit 7731003

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/TextComponent.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright © 2024 Cloudnode OÜ
3+
*
4+
* This file is part of @cldn/components.
5+
*
6+
* \@cldn/components is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser
7+
* General Public License as published by the Free Software Foundation, either version 3 of the License,
8+
* or (at your option) any later version.
9+
*
10+
* \@cldn/components is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
11+
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12+
* for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License along with @cldn/components.
15+
* If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
import {NodeComponent} from "./NodeComponent.js";
18+
19+
/**
20+
* A text node component
21+
*/
22+
export class TextComponent extends NodeComponent<Text> {
23+
/**
24+
* Create component
25+
* @param text Text node instance or text content string
26+
*/
27+
public constructor(text: Text | string) {
28+
super(typeof text === "string" ? document.createTextNode(text) : text);
29+
}
30+
31+
/**
32+
* @deprecated Cannot add children to a TextComponent
33+
*
34+
* @throws {@link !DOMException} Always
35+
*/
36+
public override append(...components: NodeComponent<any>[]): typeof this {
37+
throw new DOMException(`TextComponent.append: Cannot add children to a ${this.constructor.name}`);
38+
}
39+
40+
/**
41+
* Get the text content
42+
*/
43+
public override toString() {
44+
return this.node.textContent;
45+
}
46+
}

0 commit comments

Comments
 (0)