Skip to content

Commit dea078a

Browse files
⚙️ 0.2.0
1 parent c9af623 commit dea078a

7 files changed

+34
-13
lines changed

dist/class/HTMLCodeBlockElement.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@ class HTMLCodeBlockElement extends HTMLElement {
5858
#language = '';
5959
/** Actual value of the accessor `controls` */
6060
#controls = false;
61+
/** Click event handler of copy button */
62+
#onClickButton = (() => {
63+
let key = -1;
64+
const textarea = document.createElement('textarea');
65+
return () => {
66+
clearTimeout(key);
67+
textarea.value = this.#value.replace(/\n$/, '');
68+
document.body.append(textarea);
69+
textarea.select();
70+
document.execCommand('copy');
71+
textarea.remove();
72+
this.#copyButton.textContent = 'Copied!';
73+
key = window.setTimeout(() => {
74+
this.#copyButton.textContent = 'Copy';
75+
}, 1500);
76+
};
77+
})();
6178
/** Outputs the resulting syntax-highlighted markup to the DOM. */
6279
#render = function () {
6380
if (!this.parentNode) {
@@ -130,7 +147,6 @@ class HTMLCodeBlockElement extends HTMLElement {
130147
return this.#controls;
131148
}
132149
set controls(value) {
133-
// TODO: コピーボタンの表示切り替え
134150
this.#controls = value;
135151
if (this.#controls) {
136152
this.setAttribute('controls', '');
@@ -181,8 +197,11 @@ class HTMLCodeBlockElement extends HTMLElement {
181197
})();
182198
const copyButton = (() => {
183199
const button = document.createElement('button');
200+
button.type = 'button';
184201
button.slot = 'copy-button';
185202
button.textContent = 'Copy';
203+
button.setAttribute('aria-live', 'polite');
204+
button.addEventListener('click', this.#onClickButton);
186205
return button;
187206
})();
188207
const codeElm = (() => {
@@ -226,7 +245,7 @@ class HTMLCodeBlockElement extends HTMLElement {
226245
/* -------------------------------------------------------------------------
227246
* Hard private props initialize
228247
* ---------------------------------------------------------------------- */
229-
this.#value = (this.textContent || '').replace(/^\n/, '').replace(/\n$/, '');
248+
this.#value = (this.textContent || '').replace(/^\n/, '').replace(/\n\n$/, '');
230249
this.#label = a11yName.textContent || '';
231250
this.#language = this.getAttribute('language') || '';
232251
this.#controls = this.getAttribute('controls') !== null;

dist/utils/add-style.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,19 @@ style.textContent = `
3131
all: unset;
3232
outline: revert;
3333
position: absolute;
34-
right: 10px;
35-
top: 10px;
34+
right: 0;
35+
top: 0;
3636
z-index: 1;
37+
padding: 10px;
3738
display: block;
39+
font-family: inherit;
3840
color: #fff;
3941
opacity: 0;
4042
mix-blend-mode: exclusion;
4143
}
4244
4345
code-block:hover button,
44-
code-block:not(:hover) button:focus {
46+
code-block button:focus {
4547
opacity: 1;
4648
}
4749

lib/html-code-block-element.all.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/html-code-block-element.common.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/html-code-block-element.core.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@heppokofrontend/html-code-block-element",
33
"description": "Code block custom element with syntax highlighting and copy button.",
4-
"version": "0.1.0",
4+
"version": "0.2.0",
55
"author": "heppokofrontend",
66
"bugs": {
77
"url": "https://github.com/heppokofrontend/html-code-block-element/issues"

0 commit comments

Comments
 (0)