瀏覽代碼

fix: copy to clipboard in doc demo missing line break

and add `hold()` to clipboard when necessary
wangweimin 4 年之前
父節點
當前提交
e42dfab4cc
共有 1 個文件被更改,包括 5 次插入2 次删除
  1. 5 2
      demos/doc_demo.py

+ 5 - 2
demos/doc_demo.py

@@ -37,6 +37,9 @@ from pywebio.session import *
 
 
 def copytoclipboard(code):
+    code = IMPORT_CODE + code
+    if 'put_buttons(' in code or 'put_file(' in code:
+        code += '\n\nhold()  # keep session alive'
     run_js("writeText(text)", text=code)
     toast('已复制')
 
@@ -44,7 +47,7 @@ def copytoclipboard(code):
 def handle_code(code, title):
     run_js("""
     window.writeText = function(text) {
-        const input = document.createElement('INPUT');
+        const input = document.createElement('textarea');
         input.style.opacity  = 0;
         input.style.position = 'absolute';
         input.style.left = '-100000px';
@@ -68,7 +71,7 @@ def handle_code(code, title):
 
             put_buttons(['运行', '复制代码'], onclick=[
                 partial(run_code, code=p, scope=scope, locals=locals),
-                partial(copytoclipboard, code=IMPORT_CODE + p)
+                partial(copytoclipboard, code=p)
             ])
 
         put_markdown('----')