| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- import Node from './Node.js';
- import { addMethodChaining, nodeProxy } from '../tsl/TSLCore.js';
- /**
- * This node can be used as a context management component for another node.
- * {@link NodeBuilder} performs its node building process in a specific context and
- * this node allows the modify the context. A typical use case is to overwrite `getUV()` e.g.:
- *
- * ```js
- *node.context( { getUV: () => customCoord } );
- *```
- * @augments Node
- */
- class ContextNode extends Node {
- static get type() {
- return 'ContextNode';
- }
- /**
- * Constructs a new context node.
- *
- * @param {Node} node - The node whose context should be modified.
- * @param {Object} [value={}] - The modified context data.
- */
- constructor( node, value = {} ) {
- super();
- /**
- * This flag can be used for type testing.
- *
- * @type {Boolean}
- * @readonly
- * @default true
- */
- this.isContextNode = true;
- /**
- * The node whose context should be modified.
- *
- * @type {Node}
- */
- this.node = node;
- /**
- * The modified context data.
- *
- * @type {Object}
- * @default {}
- */
- this.value = value;
- }
- /**
- * This method is overwritten to ensure it returns the reference to {@link ContextNode#node}.
- *
- * @return {Node} A reference to {@link ContextNode#node}.
- */
- getScope() {
- return this.node.getScope();
- }
- /**
- * This method is overwritten to ensure it returns the type to {@link ContextNode#node}.
- *
- * @param {NodeBuilder} builder - The current node builder.
- * @return {String} The type of {@link ContextNode#node}.
- */
- getNodeType( builder ) {
- return this.node.getNodeType( builder );
- }
- analyze( builder ) {
- this.node.build( builder );
- }
- setup( builder ) {
- const previousContext = builder.getContext();
- builder.setContext( { ...builder.context, ...this.value } );
- const node = this.node.build( builder );
- builder.setContext( previousContext );
- return node;
- }
- generate( builder, output ) {
- const previousContext = builder.getContext();
- builder.setContext( { ...builder.context, ...this.value } );
- const snippet = this.node.build( builder, output );
- builder.setContext( previousContext );
- return snippet;
- }
- }
- export default ContextNode;
- export const context = /*@__PURE__*/ nodeProxy( ContextNode );
- export const label = ( node, name ) => context( node, { label: name } );
- addMethodChaining( 'context', context );
- addMethodChaining( 'label', label );
|