Skip to content

Commit

Permalink
maint(frontend): catch some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wang0618 committed May 22, 2021
1 parent 32f6feb commit 6f715ae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
20 changes: 14 additions & 6 deletions webiojs/src/handlers/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export class OutputHandler implements CommandHandler {
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) {
let output_to_root = false;
if (msg.command === 'output') {
Expand All @@ -37,7 +45,7 @@ export class OutputHandler implements CommandHandler {

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)
return console.error(`Scope '${msg.spec.scope}' not found`);
Expand All @@ -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)
elem.fadeIn({
complete: () => {
Expand Down Expand Up @@ -108,9 +116,9 @@ export class OutputHandler implements CommandHandler {
container_elem.append(html);
else {
if (spec.position >= 0)
$(`${spec.container}>*`).eq(spec.position).insertBefore(html);
$(`${spec.container} > *`).eq(spec.position).insertBefore(html);
else
$(`${spec.container}>*`).eq(spec.position).insertAfter(html);
$(`${spec.container} > *`).eq(spec.position).insertAfter(html);
}
}
if (msg.spec.clear !== undefined) {
Expand All @@ -119,7 +127,7 @@ export class OutputHandler implements CommandHandler {
if (msg.spec.clear_before !== undefined)
$(`${msg.spec.clear_before}`).prevAll().remove();
if (msg.spec.clear_after !== undefined)
$(`${msg.spec.clear_after}~*`).remove();
$(`${msg.spec.clear_after} ~ *`).remove();
if (msg.spec.scroll_to !== undefined) {
let target = $(`${msg.spec.scroll_to}`);
if (!target.length) {
Expand All @@ -133,7 +141,7 @@ export class OutputHandler implements CommandHandler {
$(`${msg.spec.clear_range[1]}`).length) {
let removed: HTMLElement[] = [];
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]) {
valid = true;
return false;
Expand Down
6 changes: 5 additions & 1 deletion webiojs/src/models/input/textarea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ export class Textarea extends InputItem {
after_show(first_show: boolean): any {
if (first_show && this.spec.code) {
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)
this.code_mirror.on('change', (instance: object, changeObj: object) => {
this.send_value_listener(this, null, 'change');
Expand Down

0 comments on commit 6f715ae

Please sign in to comment.