Răsfoiți Sursa

demo: fix copy to clipboard in doc demos

WangWeimin 4 ani în urmă
părinte
comite
a72ca8ef93
1 a modificat fișierele cu 17 adăugiri și 1 ștergeri
  1. 17 1
      demos/doc_demo.py

+ 17 - 1
demos/doc_demo.py

@@ -34,11 +34,27 @@ from pywebio.session import *
 
 
 def copytoclipboard(code):
-    run_js("navigator.clipboard.writeText(text)", text=code)
+    run_js("writeText(text)", text=code)
     toast('已复制')
 
 
 def handle_code(code, title):
+    run_js("""
+    window.writeText = function(text) {
+        const input = document.createElement('INPUT');
+        input.style.opacity  = 0;
+        input.style.position = 'absolute';
+        input.style.left = '-100000px';
+        document.body.appendChild(input);
+
+        input.value = text;
+        input.select();
+        input.setSelectionRange(0, text.length);
+        document.execCommand('copy');
+        document.body.removeChild(input);
+        return true;
+    }
+    """)
     if title:
         put_markdown('## %s' % title)