Skip to content

Commit d8a8f2e

Browse files
:azp: Add attributes
1 parent 1c933d4 commit d8a8f2e

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

src/class/HTMLCodeBlockElement.ts

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,17 @@ export default class HTMLCodeBlockElement extends HTMLElement {
3939
return endgine.highlightAuto(src);
4040
}
4141

42-
4342
#shadowRoot: ShadowRoot;
4443
#codeBlock: HTMLElement;
4544
#codeWrap: HTMLPreElement;
4645
/** Actual value of the accessor `value` */
4746
#value: string = '';
47+
/** Actual value of the accessor `label` */
48+
#label: string = '';
4849
/** Actual value of the accessor `language` */
4950
#language: string = '';
51+
/** Actual value of the accessor `controls` */
52+
#controls: boolean = false;
5053

5154
/** Outputs the resulting syntax-highlighted markup to the DOM. */
5255
#render() {
@@ -76,6 +79,27 @@ export default class HTMLCodeBlockElement extends HTMLElement {
7679
this.#render();
7780
}
7881

82+
/**
83+
* The name of code block
84+
* @returns - The value of the label attribute
85+
*/
86+
get label() {
87+
return this.#label;
88+
}
89+
90+
set label(name: string) {
91+
// TODO: Accessiblity Treeにアクセシブルネームを提供する
92+
this.#label = name || '';
93+
94+
if (this.#label) {
95+
this.setAttribute('label', name);
96+
} else {
97+
this.removeAttribute('label');
98+
}
99+
100+
this.#render();
101+
}
102+
79103
/**
80104
* Language Mode
81105
* @returns - The value of the language attribute
@@ -96,10 +120,32 @@ export default class HTMLCodeBlockElement extends HTMLElement {
96120
this.#render();
97121
}
98122

123+
/**
124+
* Flag to display the UI
125+
* @returns - With or without controls attribute
126+
* */
127+
get controls() {
128+
return this.#controls;
129+
}
130+
131+
set controls(flag: boolean) {
132+
// TODO: コピーボタン、ラベルの表示切り替え
133+
this.#controls = flag;
134+
135+
if (this.#controls) {
136+
this.setAttribute('controls', '');
137+
} else {
138+
this.removeAttribute('controls');
139+
}
140+
141+
this.#render();
142+
}
143+
99144
static get observedAttributes() {
100145
return [
146+
'label',
101147
'language',
102-
// 'controls',
148+
'controls',
103149
];
104150
}
105151

@@ -115,10 +161,16 @@ export default class HTMLCodeBlockElement extends HTMLElement {
115161
// When the value of the attribute being observed changes,
116162
// pass value to accessors.
117163
switch (attrName) {
164+
// string
165+
case 'label':
118166
case 'language':
119167
this[attrName] = newValue || '';
120168

121169
break;
170+
171+
// boolean
172+
case 'controls':
173+
this[attrName] = typeof newValue === 'string';
122174
}
123175
}
124176

@@ -141,6 +193,8 @@ export default class HTMLCodeBlockElement extends HTMLElement {
141193
return slot;
142194
}
143195
const slots = [
196+
mkslot('label'),
197+
mkslot('copy-button'),
144198
mkslot('code'),
145199
];
146200
const pre = document.createElement('pre');
@@ -153,7 +207,9 @@ export default class HTMLCodeBlockElement extends HTMLElement {
153207

154208
// Hard private props initialize
155209
this.#value = (this.textContent || '').replace(/^\n/, '');
210+
this.#label = this.getAttribute('label') || '';
156211
this.#language = this.getAttribute('language') || '';
212+
this.#controls = this.getAttribute('controls') !== null;
157213
this.#shadowRoot = this.attachShadow({
158214
mode: 'closed',
159215
});

0 commit comments

Comments
 (0)