output.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import {Command, Session} from "../session";
  2. import {config, state} from '../state'
  3. import {body_scroll_to, box_scroll_to} from "../utils";
  4. import {getWidgetElement} from "../models/output"
  5. import {CommandHandler} from "./base";
  6. const DISPLAY_NONE_TAGS = ['script', 'style'];
  7. export class OutputHandler implements CommandHandler {
  8. session: Session;
  9. accept_command = ['output', 'output_ctl'];
  10. private readonly container_parent: JQuery;
  11. private readonly container_elem: JQuery;
  12. constructor(session: Session, container_elem: JQuery) {
  13. this.session = session;
  14. this.container_elem = container_elem;
  15. this.container_parent = this.container_elem.parent();
  16. }
  17. scroll_bottom() {
  18. body_scroll_to($('.pywebio'), 'bottom', null,15);
  19. };
  20. handle_message(msg: Command) {
  21. let output_to_root = false;
  22. if (msg.command === 'output') {
  23. let elem;
  24. try {
  25. elem = getWidgetElement(msg.spec);
  26. } catch (e) {
  27. return console.error(`Handle command error, command: ${msg}, error:${e}`);
  28. }
  29. let container_elem = $(msg.spec.scope);
  30. if (config.outputAnimation && DISPLAY_NONE_TAGS.indexOf(elem[0].tagName.toLowerCase())==-1 && container_elem.length == 1) elem.hide();
  31. if (container_elem.length === 0)
  32. return console.error(`Scope '${msg.spec.scope}' not found`);
  33. if (!msg.spec.scope || msg.spec.scope === '#pywebio-scope-ROOT') output_to_root = true;
  34. if (msg.spec.position === 0)
  35. container_elem.prepend(elem);
  36. else if (msg.spec.position === -1)
  37. container_elem.append(elem);
  38. else {
  39. for (let con of container_elem) {
  40. let pos = $(con.children).eq(msg.spec.position);
  41. if (msg.spec.position >= 0)
  42. elem.insertBefore(pos);
  43. else
  44. elem.insertAfter(pos);
  45. }
  46. }
  47. if (config.outputAnimation && DISPLAY_NONE_TAGS.indexOf(elem[0].tagName.toLowerCase())==-1 && container_elem.length == 1) elem.fadeIn();
  48. } else if (msg.command === 'output_ctl') {
  49. this.handle_output_ctl(msg);
  50. }
  51. // 当设置了AutoScrollBottom、并且当前输出输出到页面末尾时,滚动到底部
  52. if (state.AutoScrollBottom && output_to_root)
  53. this.scroll_bottom();
  54. };
  55. handle_output_ctl(msg: Command) {
  56. if (msg.spec.set_scope !== undefined) {
  57. let spec = msg.spec as {
  58. set_scope: string, // scope名
  59. container: string, // 此scope的父scope
  60. position: number, // 在父scope中创建此scope的位置 0 -> 在父scope的顶部创建, -1 -> 在父scope的尾部创建
  61. if_exist: string // 已经存在 ``name`` scope 时如何操作: `'remove'` 表示先移除旧scope再创建新scope, `'clear'` 表示将旧scope的内容清除,不创建新scope,null/不指定时表示立即返回不进行任何操作
  62. };
  63. let container_elem = $(`${spec.container}`);
  64. if (container_elem.length === 0)
  65. return console.error(`Scope '${msg.spec.scope}' not found`);
  66. let old = $(`#${spec.set_scope}`);
  67. if (old.length) {
  68. if (spec.if_exist == 'remove')
  69. old.remove();
  70. else if (spec.if_exist == 'clear') {
  71. old.empty();
  72. return;
  73. } else {
  74. return;
  75. }
  76. }
  77. let html = `<div id="${spec.set_scope}"></div>`;
  78. if (spec.position === 0)
  79. container_elem.prepend(html);
  80. else if (spec.position === -1)
  81. container_elem.append(html);
  82. else {
  83. if (spec.position >= 0)
  84. $(`${spec.container}>*`).eq(spec.position).insertBefore(html);
  85. else
  86. $(`${spec.container}>*`).eq(spec.position).insertAfter(html);
  87. }
  88. }
  89. if (msg.spec.clear !== undefined) {
  90. $(msg.spec.clear).empty();
  91. }
  92. if (msg.spec.clear_before !== undefined)
  93. $(`${msg.spec.clear_before}`).prevAll().remove();
  94. if (msg.spec.clear_after !== undefined)
  95. $(`${msg.spec.clear_after}~*`).remove();
  96. if (msg.spec.scroll_to !== undefined) {
  97. let target = $(`${msg.spec.scroll_to}`);
  98. if (!target.length) {
  99. console.error(`Scope ${msg.spec.scroll_to} not found`);
  100. } else {
  101. body_scroll_to(target, msg.spec.position);
  102. }
  103. }
  104. if (msg.spec.clear_range !== undefined) {
  105. if ($(`${msg.spec.clear_range[0]}`).length &&
  106. $(`${msg.spec.clear_range[1]}`).length) {
  107. let removed: HTMLElement[] = [];
  108. let valid = false;
  109. $(`${msg.spec.clear_range[0]}~*`).each(function () {
  110. if (this.id === msg.spec.clear_range[1]) {
  111. valid = true;
  112. return false;
  113. }
  114. removed.push(this);
  115. // $(this).remove();
  116. });
  117. if (valid)
  118. $(removed).remove();
  119. else
  120. console.warn(`clear_range not valid: can't find ${msg.spec.clear_range[1]} after ${msg.spec.clear_range[0]}`);
  121. }
  122. }
  123. if (msg.spec.remove !== undefined)
  124. $(`${msg.spec.remove}`).remove();
  125. };
  126. }