Skip to content

Commit 64b596e

Browse files
✨ Support to edit by textContent
1 parent 24fdc1f commit 64b596e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/class/HTMLCodeBlockElement.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ export default class HTMLCodeBlockElement extends HTMLElement {
6969
};
7070
})();
7171

72+
/**
73+
* True when rendered at least once.
74+
* The purpose of this flag is to available the operation the following usage.
75+
*
76+
* Specifically, this is the case where an element is rendered
77+
* on the screen without ever using the value property.
78+
*
79+
* ```js
80+
* const cb = document.createElement('code-block');
81+
*
82+
* cb.language = 'json';
83+
* cb.textContent = '{"a": 100}';
84+
* document.body.prepend(cb);
85+
* ```
86+
*/
87+
#rendered = false;
88+
7289
/** Pure DOM content */
7390
#a11yName: HTMLElement;
7491

@@ -275,6 +292,14 @@ export default class HTMLCodeBlockElement extends HTMLElement {
275292
}
276293

277294
connectedCallback() {
295+
if (
296+
this.#rendered === false &&
297+
this.#value === ''
298+
) {
299+
this.#value = this.textContent || '';
300+
}
301+
302+
this.#rendered = true;
278303
this.#render();
279304
}
280305

0 commit comments

Comments
 (0)