Selaa lähdekoodia

improve output functions' `anchor` parameter

if given `anchor` is existed, then replace content
wangweimin 5 vuotta sitten
vanhempi
säilyke
7c44445da3
2 muutettua tiedostoa jossa 28 lisäystä ja 20 poistoa
  1. 18 12
      pywebio/html/js/pywebio.js
  2. 10 8
      pywebio/output.py

+ 18 - 12
pywebio/html/js/pywebio.js

@@ -144,6 +144,7 @@
     };
 
     OutputController.prototype.handle_message = function (msg) {
+        var scroll_bottom = false;
         if (msg.command === 'output') {
             const func_name = `get_${msg.spec.type}_element`;
             if (!(func_name in OutputController.prototype)) {
@@ -151,23 +152,28 @@
             }
 
             var elem = OutputController.prototype[func_name].call(this, msg.spec);
-            if (msg.spec.anchor !== undefined) {
-                this.container_elem.find(`#${msg.spec.anchor}`).attr('id', '');
-                elem.attr('id', msg.spec.anchor);
-            }
-
-            if (msg.spec.before !== undefined) {
-                this.container_elem.find('#' + msg.spec.before).before(elem);
-            } else if (msg.spec.after !== undefined) {
-                this.container_elem.find('#' + msg.spec.after).after(elem);
+            if (msg.spec.anchor !== undefined && this.container_elem.find(`#${msg.spec.anchor}`).length) {
+                var pos = this.container_elem.find(`#${msg.spec.anchor}`);
+                pos.empty().append(elem);
+                elem.unwrap().attr('id', msg.spec.anchor);
             } else {
-                this.container_elem.append(elem);
+                if (msg.spec.anchor !== undefined)
+                    elem.attr('id', msg.spec.anchor);
+
+                if (msg.spec.before !== undefined) {
+                    this.container_elem.find('#' + msg.spec.before).before(elem);
+                } else if (msg.spec.after !== undefined) {
+                    this.container_elem.find('#' + msg.spec.after).after(elem);
+                } else {
+                    this.container_elem.append(elem);
+                    scroll_bottom = true;
+                }
             }
         } else if (msg.command === 'output_ctl') {
             this.handle_output_ctl(msg);
         }
-        // 当设置了AutoScrollBottom、并且不指定锚点进行输出时,滚动到底部
-        if (AutoScrollBottom && msg.command !== 'output_ctl' && msg.spec.before === undefined && msg.spec.after === undefined)
+        // 当设置了AutoScrollBottom、并且当前输出输出到页面末尾时,滚动到底部
+        if (AutoScrollBottom && scroll_bottom)
             this.scroll_bottom();
     };
 

+ 10 - 8
pywebio/output.py

@@ -119,11 +119,11 @@ def _put_content(type, anchor=None, before=None, after=None, **other_spec):
     """
     向用户端发送 ``output`` 指令
 
-    :param type: 输出类型
+    :param str type: 输出类型
     :param content: 输出内容
-    :param anchor: 为当前的输出内容标记锚点。若锚点已经存在,则先将旧锚点删除
-    :param before: 在给定的锚点之前输出内容。若给定的锚点不存在,则不输出任何内容
-    :param after: 在给定的锚点之后输出内容。若给定的锚点不存在,则不输出任何内容
+    :param str anchor: 为当前的输出内容标记锚点,若锚点已经存在,则将锚点处的内容替换为当前内容。
+    :param str before: 在给定的锚点之前输出内容。若给定的锚点不存在,则不输出任何内容
+    :param str after: 在给定的锚点之后输出内容。若给定的锚点不存在,则不输出任何内容
         注意: ``before`` 和 ``after`` 参数不可以同时使用
     :param other_spec: 额外的输出参数
     """
@@ -147,10 +147,12 @@ def put_text(text, inline=False, anchor=None, before=None, after=None):
 
     :param str text: 文本内容
     :param bool inline: 文本行末不换行。默认换行
-    :param str anchor: 为当前的输出内容标记锚点
-    :param str before: 在给定的锚点之前输出内容
-    :param str after: 在给定的锚点之后输出内容
-        注意: ``before`` 和 ``after`` 参数不可以同时使用
+    :param str anchor: 为当前的输出内容标记锚点,若锚点已经存在,则将锚点处的内容替换为当前内容。
+    :param str before: 在给定的锚点之前输出内容。若给定的锚点不存在,则不输出任何内容
+    :param str after: 在给定的锚点之后输出内容。若给定的锚点不存在,则不输出任何内容。
+
+    注意: ``before`` 和 ``after`` 参数不可以同时使用。
+    当 ``anchor`` 指定的锚点已经在页面上存在时,``before`` 和 ``after`` 参数将被忽略。
     """
     _put_content('text', content=str(text), inline=inline, anchor=anchor, before=before, after=after)