SaveLocalTool.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * SaveLocalTool.js
  3. *
  4. * @author realor
  5. */
  6. import { Tool } from './Tool.js'
  7. import { SaveDialog } from '../ui/SaveDialog.js'
  8. import { MessageDialog } from '../ui/MessageDialog.js'
  9. import { IOManager } from '../io/IOManager.js'
  10. class SaveLocalTool extends Tool {
  11. constructor(application, options) {
  12. super(application)
  13. this.name = 'savelocal'
  14. this.label = 'tool.savelocal.label'
  15. this.help = 'tool.savelocal.help'
  16. this.className = 'savelocal'
  17. this.url = null
  18. this.defaultFileName = 'scene.brf'
  19. this.setOptions(options)
  20. }
  21. activate() {
  22. let dialog = new SaveDialog(this.label, this.defaultFileName)
  23. dialog.setI18N(this.application.i18n)
  24. dialog.onSave = (name, format, onlySelection) => {
  25. this.onSave(name, format, onlySelection)
  26. }
  27. dialog.onCancel = () => {
  28. dialog.hide()
  29. this.application.useTool(null)
  30. }
  31. dialog.show()
  32. }
  33. deactivate() {}
  34. onSave(name, formatName, onlySelection) {
  35. const application = this.application
  36. if (this.url) {
  37. window.URL.revokeObjectURL(this.url)
  38. }
  39. const object = application.getModelRoot(onlySelection)
  40. const onCompleted = data => {
  41. try {
  42. this.url = window.URL.createObjectURL(data)
  43. let linkElem = document.createElement('a')
  44. linkElem.download = intent.name
  45. linkElem.target = '_blank'
  46. linkElem.href = this.url
  47. linkElem.style.display = 'block'
  48. linkElem.click()
  49. } catch (ex) {
  50. MessageDialog.create('ERROR', ex)
  51. .setClassName('error')
  52. .setI18N(application.i18n)
  53. .show()
  54. }
  55. this.application.useTool(null)
  56. }
  57. const onError = error => {
  58. MessageDialog.create('ERROR', error)
  59. .setClassName('error')
  60. .setI18N(application.i18n)
  61. .show()
  62. this.application.useTool(null)
  63. }
  64. let intent = {
  65. object: object,
  66. name: name || this.defaultFileName,
  67. onCompleted: onCompleted,
  68. onError: onError
  69. }
  70. IOManager.export(intent)
  71. }
  72. }
  73. export { SaveLocalTool }