semantic_node_factory.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { SemanticType } from './semantic_meaning.js';
  2. import { SemanticDefault } from './semantic_default.js';
  3. import { SemanticNodeCollator } from './semantic_default.js';
  4. import { SemanticNode } from './semantic_node.js';
  5. export class SemanticNodeFactory {
  6. constructor() {
  7. this.leafMap = new SemanticNodeCollator();
  8. this.defaultMap = new SemanticDefault();
  9. this.idCounter_ = -1;
  10. }
  11. makeNode(id) {
  12. return this.createNode_(id);
  13. }
  14. makeUnprocessed(mml) {
  15. const node = this.createNode_();
  16. node.mathml = [mml];
  17. node.mathmlTree = mml;
  18. return node;
  19. }
  20. makeEmptyNode() {
  21. const node = this.createNode_();
  22. node.type = SemanticType.EMPTY;
  23. return node;
  24. }
  25. makeContentNode(content) {
  26. const node = this.createNode_();
  27. node.updateContent(content);
  28. return node;
  29. }
  30. makeMultipleContentNodes(num, content) {
  31. const nodes = [];
  32. for (let i = 0; i < num; i++) {
  33. nodes.push(this.makeContentNode(content));
  34. }
  35. return nodes;
  36. }
  37. makeLeafNode(content, font) {
  38. if (!content) {
  39. return this.makeEmptyNode();
  40. }
  41. const node = this.makeContentNode(content);
  42. node.font = font || node.font;
  43. const meaning = this.defaultMap.getNode(node);
  44. if (meaning) {
  45. node.type = meaning.type;
  46. node.role = meaning.role;
  47. node.font = meaning.font;
  48. }
  49. this.leafMap.addNode(node);
  50. return node;
  51. }
  52. makeBranchNode(type, children, contentNodes, opt_content) {
  53. const node = this.createNode_();
  54. if (opt_content) {
  55. node.updateContent(opt_content);
  56. }
  57. node.type = type;
  58. node.childNodes = children;
  59. node.contentNodes = contentNodes;
  60. children.concat(contentNodes).forEach(function (x) {
  61. x.parent = node;
  62. node.addMathmlNodes(x.mathml);
  63. });
  64. return node;
  65. }
  66. createNode_(id) {
  67. if (typeof id !== 'undefined') {
  68. this.idCounter_ = Math.max(this.idCounter_, id);
  69. }
  70. else {
  71. id = ++this.idCounter_;
  72. }
  73. return new SemanticNode(id);
  74. }
  75. }