Browse Source

maint(frontend): catch some errors

wangweimin 4 năm trước cách đây
mục cha
commit
6f715ae0dc

+ 14 - 6
webiojs/src/handlers/output.ts

@@ -25,6 +25,14 @@ export class OutputHandler implements CommandHandler {
         body_scroll_to($('.pywebio'), 'bottom', null, 15);
         body_scroll_to($('.pywebio'), 'bottom', null, 15);
     };
     };
 
 
+    is_elem_visible(elem: JQuery) {
+        try {
+            return DISPLAY_NONE_TAGS.indexOf(elem[0].tagName.toLowerCase()) == -1;
+        } catch (e) {
+        }
+        return false;
+    }
+
     handle_message(msg: Command) {
     handle_message(msg: Command) {
         let output_to_root = false;
         let output_to_root = false;
         if (msg.command === 'output') {
         if (msg.command === 'output') {
@@ -37,7 +45,7 @@ export class OutputHandler implements CommandHandler {
 
 
             let container_elem = $(msg.spec.scope);
             let container_elem = $(msg.spec.scope);
 
 
-            if (config.outputAnimation && DISPLAY_NONE_TAGS.indexOf(elem[0].tagName.toLowerCase()) == -1 && container_elem.length == 1) elem.hide();
+            if (config.outputAnimation && this.is_elem_visible(elem) && container_elem.length == 1) elem.hide();
 
 
             if (container_elem.length === 0)
             if (container_elem.length === 0)
                 return console.error(`Scope '${msg.spec.scope}' not found`);
                 return console.error(`Scope '${msg.spec.scope}' not found`);
@@ -58,7 +66,7 @@ export class OutputHandler implements CommandHandler {
                 }
                 }
             }
             }
 
 
-            if (DISPLAY_NONE_TAGS.indexOf(elem[0].tagName.toLowerCase()) == -1 && container_elem.length == 1) {  // 输出内容为可见标签且输出目的scope唯一
+            if (this.is_elem_visible(elem) && container_elem.length == 1) {  // 输出内容为可见标签且输出目的scope唯一
                 if (config.outputAnimation)
                 if (config.outputAnimation)
                     elem.fadeIn({
                     elem.fadeIn({
                         complete: () => {
                         complete: () => {
@@ -108,9 +116,9 @@ export class OutputHandler implements CommandHandler {
                 container_elem.append(html);
                 container_elem.append(html);
             else {
             else {
                 if (spec.position >= 0)
                 if (spec.position >= 0)
-                    $(`${spec.container}>*`).eq(spec.position).insertBefore(html);
+                    $(`${spec.container} > *`).eq(spec.position).insertBefore(html);
                 else
                 else
-                    $(`${spec.container}>*`).eq(spec.position).insertAfter(html);
+                    $(`${spec.container} > *`).eq(spec.position).insertAfter(html);
             }
             }
         }
         }
         if (msg.spec.clear !== undefined) {
         if (msg.spec.clear !== undefined) {
@@ -119,7 +127,7 @@ export class OutputHandler implements CommandHandler {
         if (msg.spec.clear_before !== undefined)
         if (msg.spec.clear_before !== undefined)
             $(`${msg.spec.clear_before}`).prevAll().remove();
             $(`${msg.spec.clear_before}`).prevAll().remove();
         if (msg.spec.clear_after !== undefined)
         if (msg.spec.clear_after !== undefined)
-            $(`${msg.spec.clear_after}~*`).remove();
+            $(`${msg.spec.clear_after} ~ *`).remove();
         if (msg.spec.scroll_to !== undefined) {
         if (msg.spec.scroll_to !== undefined) {
             let target = $(`${msg.spec.scroll_to}`);
             let target = $(`${msg.spec.scroll_to}`);
             if (!target.length) {
             if (!target.length) {
@@ -133,7 +141,7 @@ export class OutputHandler implements CommandHandler {
                 $(`${msg.spec.clear_range[1]}`).length) {
                 $(`${msg.spec.clear_range[1]}`).length) {
                 let removed: HTMLElement[] = [];
                 let removed: HTMLElement[] = [];
                 let valid = false;
                 let valid = false;
-                $(`${msg.spec.clear_range[0]}~*`).each(function () {
+                $(`${msg.spec.clear_range[0]} ~ *`).each(function () {
                     if (this.id === msg.spec.clear_range[1]) {
                     if (this.id === msg.spec.clear_range[1]) {
                         valid = true;
                         valid = true;
                         return false;
                         return false;

+ 5 - 1
webiojs/src/models/input/textarea.ts

@@ -91,7 +91,11 @@ export class Textarea extends InputItem {
     after_show(first_show: boolean): any {
     after_show(first_show: boolean): any {
         if (first_show && this.spec.code) {
         if (first_show && this.spec.code) {
             this.code_mirror = CodeMirror.fromTextArea(this.element.find('textarea')[0], this.code_mirror_config);
             this.code_mirror = CodeMirror.fromTextArea(this.element.find('textarea')[0], this.code_mirror_config);
-            CodeMirror.autoLoadMode(this.code_mirror, this.code_mirror_config.mode);
+            try {
+                CodeMirror.autoLoadMode(this.code_mirror, this.code_mirror_config.mode);
+            } catch (e) {
+                console.error('CodeMirror load mode `%s` error: %s', this.code_mirror_config.mode, e);
+            }
             if (this.spec.onchange)
             if (this.spec.onchange)
                 this.code_mirror.on('change', (instance: object, changeObj: object) => {
                 this.code_mirror.on('change', (instance: object, changeObj: object) => {
                     this.send_value_listener(this, null, 'change');
                     this.send_value_listener(this, null, 'change');