-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
48 changed files
with
1,197 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { ShadowlessLitElement } from "components/shadowless_lit_element"; | ||
import { html, PropertyValues, TemplateResult } from "lit"; | ||
import { customElement, property } from "lit/decorators.js"; | ||
import { initTooltips, ready } from "util.js"; | ||
|
||
/** | ||
* A button that copies the text content of a given element to the clipboard. | ||
* The button is styled as a small icon button. | ||
* The button is a tooltip that shows the current status of the copy operation. | ||
* | ||
* @element d-copy-button | ||
* | ||
* @property {HTMLElement} codeElement - The element whose text content is copied to the clipboard. | ||
*/ | ||
@customElement("d-copy-button") | ||
export class CopyButton extends ShadowlessLitElement { | ||
@property({ type: Object }) | ||
codeElement: HTMLElement; | ||
|
||
get code(): string { | ||
return this.codeElement.textContent; | ||
} | ||
|
||
@property({ state: true }) | ||
status: "idle" | "success" | "error" = "idle"; | ||
|
||
async copyCode(): Promise<void> { | ||
try { | ||
await navigator.clipboard.writeText(this.code); | ||
this.status = "success"; | ||
} catch (err) { | ||
window.getSelection().selectAllChildren(this.codeElement); | ||
this.status = "error"; | ||
} | ||
} | ||
|
||
get tooltip(): string { | ||
switch (this.status) { | ||
case "success": | ||
return I18n.t("js.copy-success"); | ||
case "error": | ||
return I18n.t("js.copy-fail"); | ||
default: | ||
return I18n.t("js.code.copy-to-clipboard"); | ||
} | ||
} | ||
|
||
protected updated(_changedProperties: PropertyValues): void { | ||
super.updated(_changedProperties); | ||
initTooltips(this); | ||
} | ||
|
||
constructor() { | ||
super(); | ||
|
||
// Reload when I18n is loaded | ||
ready.then(() => this.requestUpdate()); | ||
} | ||
|
||
protected render(): TemplateResult { | ||
return html`<button class="btn btn-icon copy-btn" | ||
@click=${() => this.copyCode()} | ||
@focusout=${() => this.status = "idle"} | ||
data-bs-placement="top" | ||
data-bs-toggle="tooltip" | ||
title="${this.tooltip}"> | ||
<i class="mdi mdi-clipboard-outline"></i> | ||
</button>`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.