threepipe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DropzonePlugin.ts 5.0KB

2 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import {type ThreeViewer} from '../../viewer/'
  2. // noinspection ES6PreferShortImport
  3. import {AViewerPluginSync} from '../../viewer/AViewerPlugin'
  4. import {Dropzone} from '../../utils'
  5. import {uiButton, uiConfig, uiFolderContainer, UiObjectConfig, uiToggle} from 'uiconfig.js'
  6. import type {AddAssetOptions, ImportFilesOptions, ImportResult} from '../../assetmanager'
  7. import {serialize} from 'ts-browser-helpers'
  8. export interface DropzonePluginOptions {
  9. domElement?: HTMLElement
  10. allowedExtensions?: string[]
  11. autoImport?: boolean
  12. autoAdd?: boolean
  13. importOptions?: ImportFilesOptions
  14. addOptions?: AddAssetOptions
  15. }
  16. /**
  17. * Dropzone Plugin
  18. *
  19. * Adds a dropzone to the viewer for importing assets.
  20. *
  21. * Automatically imports and adds assets to the scene, the behavior can be configured.
  22. * @category Plugins
  23. */
  24. @uiFolderContainer('Dropzone')
  25. export class DropzonePlugin extends AViewerPluginSync<'drop'> {
  26. static readonly PluginType = 'Dropzone'
  27. uiConfig!: UiObjectConfig
  28. @uiToggle() @serialize() enabled = true
  29. private _inputEl?: HTMLInputElement
  30. private _dropzone?: Dropzone
  31. private _allowedExtensions: string[]|undefined = undefined // undefined and empty array is different.
  32. /**
  33. * Automatically import assets when dropped.
  34. */
  35. @serialize() autoImport = true
  36. /**
  37. * Automatically add dropped and imported assets to the scene.
  38. Works only if {@link autoImport} is true.
  39. */
  40. @uiToggle() @serialize() autoAdd = true
  41. /**
  42. * Import options for the {@link AssetImporter.importFiles}
  43. */
  44. @uiConfig() @serialize() importOptions: ImportFilesOptions = {
  45. autoImportZipContents: true,
  46. forceImporterReprocess: false,
  47. }
  48. /**
  49. * Add options for the {@link RootScene.addObject}
  50. */
  51. @uiConfig() @serialize() addOptions: AddAssetOptions = {
  52. autoCenter: true,
  53. importConfig: true,
  54. autoScale: true,
  55. autoScaleRadius: 2,
  56. license: '',
  57. clearSceneObjects: false,
  58. disposeSceneObjects: false,
  59. autoSetBackground: false,
  60. autoSetEnvironment: true,
  61. }
  62. /**
  63. * Allowed file extensions. If undefined, all files are allowed.
  64. */
  65. get allowedExtensions(): string[] | undefined {
  66. return this._allowedExtensions
  67. }
  68. set allowedExtensions(value: string[] | undefined) {
  69. this._allowedExtensions = value
  70. if (this._inputEl) this._inputEl.accept = value ? value.map(v=>'.' + v).join(', ') : ''
  71. }
  72. /**
  73. * Prompt for file selection using the browser file dialog.
  74. */
  75. @uiButton('Select files')
  76. public promptForFile(): void {
  77. if (!this.enabled) return
  78. this.allowedExtensions = this._allowedExtensions
  79. this._inputEl?.click()
  80. }
  81. private _domElement?: HTMLElement
  82. constructor(options?: DropzonePluginOptions) {
  83. super()
  84. if (!options) return
  85. this._domElement = options.domElement
  86. this.allowedExtensions = options.allowedExtensions
  87. this.autoImport = options.autoImport ?? this.autoImport
  88. this.autoAdd = options.autoAdd ?? this.autoAdd
  89. this.importOptions = {...this.importOptions, ...options.importOptions}
  90. this.addOptions = {...this.addOptions, ...options.addOptions}
  91. }
  92. onAdded(viewer: ThreeViewer) {
  93. super.onAdded(viewer)
  94. this._inputEl = document.createElement('input')!
  95. this._inputEl.type = 'file'
  96. if (!this._domElement) this._domElement = viewer.canvas
  97. this._dropzone = new Dropzone(this._domElement, this._inputEl, {
  98. drop: this._onFileDrop.bind(this),
  99. })
  100. this.allowedExtensions = this._allowedExtensions
  101. }
  102. onRemove(viewer: ThreeViewer) {
  103. super.onRemove(viewer)
  104. this._dropzone?.destroy()
  105. this._dropzone = undefined
  106. this._inputEl = undefined
  107. }
  108. private async _onFileDrop({files, nativeEvent}: {files: Map<string, File>, nativeEvent: DragEvent}) {
  109. if (!files) return
  110. if (!this.enabled) return
  111. const viewer = this._viewer
  112. if (!viewer) return
  113. if (this._allowedExtensions !== undefined) {
  114. for (const file of files.keys()) {
  115. if (!this._allowedExtensions.includes(file.split('.').pop()?.toLowerCase() ?? '')) {
  116. files.delete(file)
  117. }
  118. }
  119. }
  120. if (files.size < 1) return
  121. const manager = viewer.assetManager
  122. let imported: Map<string, (ImportResult | undefined)[]>|undefined
  123. let assets: (ImportResult | undefined)[]|undefined
  124. if (this.autoImport) {
  125. imported = await manager.importer.importFiles(files, {
  126. allowedExtensions: this.allowedExtensions, ...this.importOptions,
  127. })
  128. if (this.autoAdd) {
  129. const toAdd = [...imported?.values() ?? []].flat(2).filter(v=>!!v) ?? []
  130. assets = await manager.loadImported(toAdd, {...this.addOptions})
  131. }
  132. }
  133. this.dispatchEvent({type: 'drop', files, imported, assets, nativeEvent})
  134. }
  135. }