FileEditor.js 1.1 KB

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