@tenphi/starlight 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1,57 @@
1
+ //#region src/client/code-copy.ts
2
+ var CookbookCodeBlock = class extends HTMLElement {
3
+ #resetTimer;
4
+ connectedCallback() {
5
+ this.addEventListener("click", this.#copy);
6
+ }
7
+ disconnectedCallback() {
8
+ this.removeEventListener("click", this.#copy);
9
+ if (this.#resetTimer !== void 0) window.clearTimeout(this.#resetTimer);
10
+ }
11
+ #copy = async (event) => {
12
+ const target = event.target;
13
+ const button = target instanceof Element ? target.closest("[data-copy-code]") : void 0;
14
+ const code = this.querySelector("pre code");
15
+ if (!button || !this.contains(button) || !code) return;
16
+ try {
17
+ await writeClipboard(code);
18
+ } catch {
19
+ button.dataset.copyState = "error";
20
+ button.setAttribute("aria-label", "Could not copy code");
21
+ button.title = "Could not copy code";
22
+ return;
23
+ }
24
+ button.dataset.copyState = "copied";
25
+ button.setAttribute("aria-label", "Code copied");
26
+ button.title = "Code copied";
27
+ if (this.#resetTimer !== void 0) window.clearTimeout(this.#resetTimer);
28
+ this.#resetTimer = window.setTimeout(() => {
29
+ delete button.dataset.copyState;
30
+ button.setAttribute("aria-label", "Copy code");
31
+ button.title = "Copy code";
32
+ this.#resetTimer = void 0;
33
+ }, 2e3);
34
+ };
35
+ };
36
+ async function writeClipboard(code) {
37
+ const value = code.textContent ?? "";
38
+ if (navigator.clipboard?.writeText) {
39
+ await navigator.clipboard.writeText(value);
40
+ return;
41
+ }
42
+ const selection = window.getSelection();
43
+ const previousRanges = selection ? Array.from({ length: selection.rangeCount }, (_, index) => selection.getRangeAt(index)) : [];
44
+ const range = document.createRange();
45
+ range.selectNodeContents(code);
46
+ selection?.removeAllRanges();
47
+ selection?.addRange(range);
48
+ const copied = document.execCommand("copy");
49
+ selection?.removeAllRanges();
50
+ for (const previousRange of previousRanges) selection?.addRange(previousRange);
51
+ if (!copied) throw new Error("Clipboard unavailable");
52
+ }
53
+ if (!customElements.get("cookbook-code-block")) customElements.define("cookbook-code-block", CookbookCodeBlock);
54
+ //#endregion
55
+ export {};
56
+
57
+ //# sourceMappingURL=code-copy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"code-copy.js","names":[],"sources":["../../src/client/code-copy.ts"],"sourcesContent":["export {};\n\nclass CookbookCodeBlock extends HTMLElement {\n #resetTimer: number | undefined;\n\n connectedCallback() {\n this.addEventListener(\"click\", this.#copy);\n }\n\n disconnectedCallback() {\n this.removeEventListener(\"click\", this.#copy);\n if (this.#resetTimer !== undefined) {\n window.clearTimeout(this.#resetTimer);\n }\n }\n\n #copy = async (event: MouseEvent) => {\n const target = event.target;\n const button =\n target instanceof Element\n ? target.closest<HTMLButtonElement>(\"[data-copy-code]\")\n : undefined;\n const code = this.querySelector<HTMLElement>(\"pre code\");\n if (!button || !this.contains(button) || !code) return;\n\n try {\n await writeClipboard(code);\n } catch {\n button.dataset.copyState = \"error\";\n button.setAttribute(\"aria-label\", \"Could not copy code\");\n button.title = \"Could not copy code\";\n return;\n }\n\n button.dataset.copyState = \"copied\";\n button.setAttribute(\"aria-label\", \"Code copied\");\n button.title = \"Code copied\";\n\n if (this.#resetTimer !== undefined) {\n window.clearTimeout(this.#resetTimer);\n }\n this.#resetTimer = window.setTimeout(() => {\n delete button.dataset.copyState;\n button.setAttribute(\"aria-label\", \"Copy code\");\n button.title = \"Copy code\";\n this.#resetTimer = undefined;\n }, 2_000);\n };\n}\n\nasync function writeClipboard(code: HTMLElement): Promise<void> {\n const value = code.textContent ?? \"\";\n if (navigator.clipboard?.writeText) {\n await navigator.clipboard.writeText(value);\n return;\n }\n\n const selection = window.getSelection();\n const previousRanges = selection\n ? Array.from({ length: selection.rangeCount }, (_, index) =>\n selection.getRangeAt(index),\n )\n : [];\n const range = document.createRange();\n range.selectNodeContents(code);\n selection?.removeAllRanges();\n selection?.addRange(range);\n const copied = document.execCommand(\"copy\");\n selection?.removeAllRanges();\n for (const previousRange of previousRanges)\n selection?.addRange(previousRange);\n if (!copied) throw new Error(\"Clipboard unavailable\");\n}\n\nif (!customElements.get(\"cookbook-code-block\")) {\n customElements.define(\"cookbook-code-block\", CookbookCodeBlock);\n}\n"],"mappings":";AAEA,IAAM,oBAAN,cAAgC,YAAY;CAC1C;CAEA,oBAAoB;EAClB,KAAK,iBAAiB,SAAS,KAAK,KAAK;CAC3C;CAEA,uBAAuB;EACrB,KAAK,oBAAoB,SAAS,KAAK,KAAK;EAC5C,IAAI,KAAK,gBAAgB,KAAA,GACvB,OAAO,aAAa,KAAK,WAAW;CAExC;CAEA,QAAQ,OAAO,UAAsB;EACnC,MAAM,SAAS,MAAM;EACrB,MAAM,SACJ,kBAAkB,UACd,OAAO,QAA2B,kBAAkB,IACpD,KAAA;EACN,MAAM,OAAO,KAAK,cAA2B,UAAU;EACvD,IAAI,CAAC,UAAU,CAAC,KAAK,SAAS,MAAM,KAAK,CAAC,MAAM;EAEhD,IAAI;GACF,MAAM,eAAe,IAAI;EAC3B,QAAQ;GACN,OAAO,QAAQ,YAAY;GAC3B,OAAO,aAAa,cAAc,qBAAqB;GACvD,OAAO,QAAQ;GACf;EACF;EAEA,OAAO,QAAQ,YAAY;EAC3B,OAAO,aAAa,cAAc,aAAa;EAC/C,OAAO,QAAQ;EAEf,IAAI,KAAK,gBAAgB,KAAA,GACvB,OAAO,aAAa,KAAK,WAAW;EAEtC,KAAK,cAAc,OAAO,iBAAiB;GACzC,OAAO,OAAO,QAAQ;GACtB,OAAO,aAAa,cAAc,WAAW;GAC7C,OAAO,QAAQ;GACf,KAAK,cAAc,KAAA;EACrB,GAAG,GAAK;CACV;AACF;AAEA,eAAe,eAAe,MAAkC;CAC9D,MAAM,QAAQ,KAAK,eAAe;CAClC,IAAI,UAAU,WAAW,WAAW;EAClC,MAAM,UAAU,UAAU,UAAU,KAAK;EACzC;CACF;CAEA,MAAM,YAAY,OAAO,a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