SplitNode.js 722 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import Node from '../core/Node.js';
  2. class SplitNode extends Node {
  3. constructor( node, components = 'x' ) {
  4. super();
  5. this.node = node;
  6. this.components = components;
  7. }
  8. getNodeType( builder ) {
  9. return builder.getTypeFromLength( this.components.length );
  10. }
  11. generate( builder ) {
  12. const node = this.node;
  13. const nodeTypeLength = builder.getTypeLength( node.getNodeType( builder ) );
  14. const components = this.components;
  15. let type = null;
  16. if ( components.length >= nodeTypeLength ) {
  17. // need expand the input node
  18. type = this.getNodeType( builder );
  19. }
  20. const nodeSnippet = node.build( builder, type );
  21. return `${nodeSnippet}.${this.components}`;
  22. }
  23. }
  24. export default SplitNode;