BaseNodeEditor.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { Node, ButtonInput, TitleElement, ContextMenu } from 'flow';
  2. import { exportJSON, onValidNode } from './NodeEditorUtils.js';
  3. import { setOutputAestheticsFromNode, getColorFromNode, getLengthFromNode } from './DataTypeLib.js';
  4. export class BaseNodeEditor extends Node {
  5. constructor( name, value = null, width = 300 ) {
  6. super();
  7. const getObjectCallback = ( /*output = null*/ ) => {
  8. return this.value;
  9. };
  10. this.setWidth( width );
  11. const title = new TitleElement( name )
  12. .setObjectCallback( getObjectCallback )
  13. .setSerializable( false );
  14. setOutputAestheticsFromNode( title, value );
  15. const contextButton = new ButtonInput().onClick( () => {
  16. context.open();
  17. } ).setIcon( 'ti ti-dots' );
  18. const onAddButtons = () => {
  19. context.removeEventListener( 'show', onAddButtons );
  20. context.add( new ButtonInput( 'Remove' ).setIcon( 'ti ti-trash' ).onClick( () => {
  21. this.dispose();
  22. } ) );
  23. if ( this.hasJSON() ) {
  24. this.context.add( new ButtonInput( 'Export' ).setIcon( 'ti ti-download' ).onClick( () => {
  25. exportJSON( this.exportJSON(), this.constructor.name );
  26. } ) );
  27. }
  28. context.add( new ButtonInput( 'Isolate' ).setIcon( 'ti ti-3d-cube-sphere' ).onClick( () => {
  29. this.context.hide();
  30. this.title.dom.dispatchEvent( new MouseEvent( 'dblclick' ) );
  31. } ) );
  32. };
  33. const context = new ContextMenu( this.dom );
  34. context.addEventListener( 'show', onAddButtons );
  35. this.title = title;
  36. if ( this.icon ) this.setIcon( 'ti ti-' + this.icon );
  37. this.contextButton = contextButton;
  38. this.context = context;
  39. title.addButton( contextButton );
  40. this.add( title );
  41. this.editor = null;
  42. this.value = value;
  43. this.onValidElement = onValidNode;
  44. this.outputLength = getLengthFromNode( value );
  45. }
  46. getColor() {
  47. const color = getColorFromNode( this.value );
  48. return color ? color + 'BB' : null;
  49. }
  50. hasJSON() {
  51. return this.value && typeof this.value.toJSON === 'function';
  52. }
  53. exportJSON() {
  54. return this.value.toJSON();
  55. }
  56. serialize( data ) {
  57. super.serialize( data );
  58. delete data.width;
  59. }
  60. deserialize( data ) {
  61. delete data.width;
  62. super.deserialize( data );
  63. }
  64. setEditor( value ) {
  65. this.editor = value;
  66. this.dispatchEvent( new Event( 'editor' ) );
  67. return this;
  68. }
  69. add( element ) {
  70. element.onValid( ( source, target ) => this.onValidElement( source, target ) );
  71. return super.add( element );
  72. }
  73. setName( value ) {
  74. this.title.setTitle( value );
  75. return this;
  76. }
  77. setIcon( value ) {
  78. this.title.setIcon( 'ti ti-' + value );
  79. return this;
  80. }
  81. getName() {
  82. return this.title.getTitle();
  83. }
  84. setObjectCallback( callback ) {
  85. this.title.setObjectCallback( callback );
  86. return this;
  87. }
  88. getObject( callback ) {
  89. return this.title.getObject( callback );
  90. }
  91. setColor( color ) {
  92. this.title.setColor( color );
  93. return this;
  94. }
  95. invalidate() {
  96. this.title.dispatchEvent( new Event( 'connect' ) );
  97. }
  98. dispose() {
  99. this.setEditor( null );
  100. this.context.hide();
  101. super.dispose();
  102. }
  103. }