FileEditor.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { StringInput, Element } from 'flow';
  2. import { BaseNodeEditor } from '../BaseNodeEditor.js';
  3. import { NodeUtils } from 'three/webgpu';
  4. import { arrayBuffer } from 'three/tsl';
  5. export class FileEditor extends BaseNodeEditor {
  6. constructor( buffer = null, name = 'File' ) {
  7. super( 'File', arrayBuffer( buffer ), 250 );
  8. this.nameInput = new StringInput( name ).setReadOnly( true );
  9. this.add( new Element().add( this.nameInput ) );
  10. this.url = null;
  11. }
  12. set buffer( arrayBuffer ) {
  13. if ( this.url !== null ) {
  14. URL.revokeObjectUR( this.url );
  15. }
  16. this.value.value = arrayBuffer;
  17. this.url = null;
  18. }
  19. get buffer() {
  20. return this.value.value;
  21. }
  22. getURL() {
  23. if ( this.url === null ) {
  24. const blob = new Blob( [ this.buffer ], { type: 'application/octet-stream' } );
  25. this.url = URL.createObjectURL( blob );
  26. }
  27. return this.url;
  28. }
  29. serialize( data ) {
  30. super.serialize( data );
  31. data.buffer = NodeUtils.arrayBufferToBase64( this.buffer );
  32. data.name = this.nameInput.getValue();
  33. }
  34. deserialize( data ) {
  35. super.deserialize( data );
  36. this.buffer = NodeUtils.base64ToArrayBuffer( data.buffer );
  37. this.nameInput.setValue( data.name );
  38. }
  39. }
粤ICP备19079148号