table_walker.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.TableWalker = void 0;
  4. const DomUtil = require("../common/dom_util.js");
  5. const event_util_js_1 = require("../common/event_util.js");
  6. const semantic_meaning_js_1 = require("../semantic_tree/semantic_meaning.js");
  7. const syntax_walker_js_1 = require("./syntax_walker.js");
  8. const walker_js_1 = require("./walker.js");
  9. class TableWalker extends syntax_walker_js_1.SyntaxWalker {
  10. constructor(node, generator, highlighter, xml) {
  11. super(node, generator, highlighter, xml);
  12. this.node = node;
  13. this.generator = generator;
  14. this.highlighter = highlighter;
  15. this.firstJump = null;
  16. this.key_ = null;
  17. this.row_ = 0;
  18. this.currentTable_ = null;
  19. this.keyMapping.set(event_util_js_1.KeyCode.ZERO, this.jumpCell.bind(this));
  20. this.keyMapping.set(event_util_js_1.KeyCode.ONE, this.jumpCell.bind(this));
  21. this.keyMapping.set(event_util_js_1.KeyCode.TWO, this.jumpCell.bind(this));
  22. this.keyMapping.set(event_util_js_1.KeyCode.THREE, this.jumpCell.bind(this));
  23. this.keyMapping.set(event_util_js_1.KeyCode.FOUR, this.jumpCell.bind(this));
  24. this.keyMapping.set(event_util_js_1.KeyCode.FIVE, this.jumpCell.bind(this));
  25. this.keyMapping.set(event_util_js_1.KeyCode.SIX, this.jumpCell.bind(this));
  26. this.keyMapping.set(event_util_js_1.KeyCode.SEVEN, this.jumpCell.bind(this));
  27. this.keyMapping.set(event_util_js_1.KeyCode.EIGHT, this.jumpCell.bind(this));
  28. this.keyMapping.set(event_util_js_1.KeyCode.NINE, this.jumpCell.bind(this));
  29. }
  30. move(key) {
  31. this.key_ = key;
  32. const result = super.move(key);
  33. this.modifier = false;
  34. return result;
  35. }
  36. up() {
  37. this.moved = walker_js_1.WalkerMoves.UP;
  38. return this.eligibleCell_() ? this.verticalMove_(false) : super.up();
  39. }
  40. down() {
  41. this.moved = walker_js_1.WalkerMoves.DOWN;
  42. return this.eligibleCell_() ? this.verticalMove_(true) : super.down();
  43. }
  44. jumpCell() {
  45. if (!this.isInTable_() || this.key_ === null) {
  46. return this.getFocus();
  47. }
  48. if (this.moved === walker_js_1.WalkerMoves.ROW) {
  49. this.moved = walker_js_1.WalkerMoves.CELL;
  50. const column = this.key_ - event_util_js_1.KeyCode.ZERO;
  51. if (!this.isLegalJump_(this.row_, column)) {
  52. return this.getFocus();
  53. }
  54. return this.jumpCell_(this.row_, column);
  55. }
  56. const row = this.key_ - event_util_js_1.KeyCode.ZERO;
  57. if (row > this.currentTable_.childNodes.length) {
  58. return this.getFocus();
  59. }
  60. this.row_ = row;
  61. this.moved = walker_js_1.WalkerMoves.ROW;
  62. return this.getFocus().clone();
  63. }
  64. undo() {
  65. const focus = super.undo();
  66. if (focus === this.firstJump) {
  67. this.firstJump = null;
  68. }
  69. return focus;
  70. }
  71. eligibleCell_() {
  72. const primary = this.getFocus().getSemanticPrimary();
  73. return (this.modifier &&
  74. primary.type === semantic_meaning_js_1.SemanticType.CELL &&
  75. TableWalker.ELIGIBLE_CELL_ROLES.indexOf(primary.role) !== -1);
  76. }
  77. verticalMove_(direction) {
  78. const parent = this.previousLevel();
  79. if (!parent) {
  80. return null;
  81. }
  82. const origFocus = this.getFocus();
  83. const origIndex = this.levels.indexOf(this.primaryId());
  84. const origLevel = this.levels.pop();
  85. const parentIndex = this.levels.indexOf(parent);
  86. const row = this.levels.get(direction ? parentIndex + 1 : parentIndex - 1);
  87. if (!row) {
  88. this.levels.push(origLevel);
  89. return null;
  90. }
  91. this.setFocus(this.singletonFocus(row));
  92. const children = this.nextLevel();
  93. const newNode = children[origIndex];
  94. if (!newNode) {
  95. this.setFocus(origFocus);
  96. this.levels.push(origLevel);
  97. return null;
  98. }
  99. this.levels.push(children);
  100. return this.singletonFocus(children[origIndex]);
  101. }
  102. jumpCell_(row, column) {
  103. if (!this.firstJump) {
  104. this.firstJump = this.getFocus();
  105. this.virtualize(true);
  106. }
  107. else {
  108. this.virtualize(false);
  109. }
  110. const id = this.currentTable_.id.toString();
  111. let level;
  112. do {
  113. level = this.levels.pop();
  114. } while (level.indexOf(id) === -1);
  115. this.levels.push(level);
  116. this.setFocus(this.singletonFocus(id));
  117. this.levels.push(this.nextLevel());
  118. const semRow = this.currentTable_.childNodes[row - 1];
  119. this.setFocus(this.singletonFocus(semRow.id.toString()));
  120. this.levels.push(this.nextLevel());
  121. return this.singletonFocus(semRow.childNodes[column - 1].id.toString());
  122. }
  123. isLegalJump_(row, column) {
  124. const xmlTable = DomUtil.querySelectorAllByAttrValue(this.getRebuilt().xml, 'id', this.currentTable_.id.toString())[0];
  125. if (!xmlTable || xmlTable.hasAttribute('alternative')) {
  126. return false;
  127. }
  128. const rowNode = this.currentTable_.childNodes[row - 1];
  129. if (!rowNode) {
  130. return false;
  131. }
  132. const xmlRow = DomUtil.querySelectorAllByAttrValue(xmlTable, 'id', rowNode.id.toString())[0];
  133. if (!xmlRow || xmlRow.hasAttribute('alternative')) {
  134. return false;
  135. }
  136. return !!(rowNode && rowNode.childNodes[column - 1]);
  137. }
  138. isInTable_() {
  139. let snode = this.getFocus().getSemanticPrimary();
  140. while (snode) {
  141. if (TableWalker.ELIGIBLE_TABLE_TYPES.indexOf(snode.type) !== -1) {
  142. this.currentTable_ = snode;
  143. return true;
  144. }
  145. snode = snode.parent;
  146. }
  147. return false;
  148. }
  149. }
  150. exports.TableWalker = TableWalker;
  151. TableWalker.ELIGIBLE_CELL_ROLES = [
  152. semantic_meaning_js_1.SemanticRole.DETERMINANT,
  153. semantic_meaning_js_1.SemanticRole.ROWVECTOR,
  154. semantic_meaning_js_1.SemanticRole.BINOMIAL,
  155. semantic_meaning_js_1.SemanticRole.SQUAREMATRIX,
  156. semantic_meaning_js_1.SemanticRole.MULTILINE,
  157. semantic_meaning_js_1.SemanticRole.MATRIX,
  158. semantic_meaning_js_1.SemanticRole.VECTOR,
  159. semantic_meaning_js_1.SemanticRole.CASES,
  160. semantic_meaning_js_1.SemanticRole.TABLE
  161. ];
  162. TableWalker.ELIGIBLE_TABLE_TYPES = [
  163. semantic_meaning_js_1.SemanticType.MULTILINE,
  164. semantic_meaning_js_1.SemanticType.MATRIX,
  165. semantic_meaning_js_1.SemanticType.VECTOR,
  166. semantic_meaning_js_1.SemanticType.CASES,
  167. semantic_meaning_js_1.SemanticType.TABLE
  168. ];