@hearthscale/app-ui 0.2.4 → 0.4.0-rc.1

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.
package/dist/markdown.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  CopyGlyph
3
- } from "./chunks/chunk-UDXTR7UW.js";
3
+ } from "./chunks/chunk-K3W7YUMY.js";
4
4
 
5
5
  // ../ui/dist/chat/markdown.js
6
6
  import { jsx as _jsx5, Fragment as _Fragment } from "react/jsx-runtime";
@@ -31,7 +31,7 @@ function useCopy() {
31
31
  useEffect(() => () => clearTimeout(timer.current), []);
32
32
  const copy = (what, press) => {
33
33
  const hold = tokenMs(press, "--dur-copied");
34
- const written = typeof what === "string" ? navigator.clipboard.writeText(what) : what();
34
+ const written = typeof what === "string" ? writeText(what) : what();
35
35
  void written.then(() => {
36
36
  clearTimeout(timer.current);
37
37
  setCopied(true);
@@ -41,6 +41,28 @@ function useCopy() {
41
41
  };
42
42
  return { copied, copy };
43
43
  }
44
+ function writeText(text) {
45
+ if (window.isSecureContext)
46
+ return navigator.clipboard.writeText(text);
47
+ const focused = document.activeElement;
48
+ const selection = document.getSelection();
49
+ const ranges = selection ? Array.from({ length: selection.rangeCount }, (_, at) => selection.getRangeAt(at)) : [];
50
+ const field = document.createElement("textarea");
51
+ field.value = text;
52
+ field.style.position = "fixed";
53
+ field.style.opacity = "0";
54
+ document.body.append(field);
55
+ field.select();
56
+ field.setSelectionRange(0, text.length);
57
+ const copied = document.execCommand("copy");
58
+ field.remove();
59
+ selection?.removeAllRanges();
60
+ for (const range of ranges)
61
+ selection?.addRange(range);
62
+ if (focused instanceof HTMLElement)
63
+ focused.focus();
64
+ return copied ? Promise.resolve() : Promise.reject(new Error("The browser refused to copy."));
65
+ }
44
66
 
45
67
  // ../ui/dist/CodeBlock.js
46
68
  function CodeBlock({ code, language, reason }) {