ScriptTool.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * ScriptTool.js
  3. *
  4. * @author realor
  5. */
  6. import { Tool } from './Tool.js'
  7. import { FileExplorer } from '../ui/FileExplorer.js'
  8. import { GeometryUtils } from '../utils/GeometryUtils.js'
  9. import { ObjectUtils } from '../utils/ObjectUtils.js'
  10. import { MessageDialog } from '../ui/MessageDialog.js'
  11. import { ConfirmDialog } from '../ui/ConfirmDialog.js'
  12. import { Toast } from '../ui/Toast.js'
  13. import { Solid } from '../core/Solid.js'
  14. import { SolidGeometry } from '../core/SolidGeometry.js'
  15. import { Cord } from '../core/Cord.js'
  16. import { CordGeometry } from '../core/CordGeometry.js'
  17. import { Profile } from '../core/Profile.js'
  18. import { ProfileGeometry } from '../core/ProfileGeometry.js'
  19. import { Metadata, Result } from '../io/FileService.js'
  20. import { ScriptDialog } from '../ui/ScriptDialog.js'
  21. import { I18N } from '../i18n/I18N.js'
  22. import * as THREE from '../lib/three.module.js'
  23. class ScriptTool extends Tool {
  24. constructor(application, options) {
  25. super(application)
  26. this.name = 'script'
  27. this.label = 'tool.script.label'
  28. this.className = 'script'
  29. this.setOptions(options)
  30. const panel = new FileExplorer(application, false)
  31. panel.showFileSize = false
  32. this.panel = panel
  33. this.edit = false
  34. panel.title = this.label
  35. panel.group = 'script'
  36. const dialog = new ScriptDialog(this.application, (name, code) => this.onSave(name, code))
  37. this.dialog = dialog
  38. panel.openFile = (url, code) => {
  39. this.openScript(() => this.setScript(url, code))
  40. }
  41. panel.addContextButton(
  42. 'open',
  43. 'button.open',
  44. () => panel.openEntry(),
  45. () => panel.isEntrySelected() && !panel.isFileEntrySelected()
  46. )
  47. panel.addContextButton(
  48. 'open_file',
  49. 'button.run',
  50. () => panel.openEntry(),
  51. () => panel.isDirectoryList() && panel.isFileEntrySelected()
  52. )
  53. panel.addContextButton(
  54. 'edit',
  55. 'button.edit',
  56. () => {
  57. this.edit = true
  58. panel.openEntry()
  59. },
  60. () => panel.isDirectoryList() && panel.isEntrySelected()
  61. )
  62. panel.addContextButton(
  63. 'new',
  64. 'button.new',
  65. () =>
  66. this.openScript(() => {
  67. this.edit = true
  68. this.setScript('', '')
  69. }),
  70. () => true
  71. )
  72. panel.addContextButton(
  73. 'editor',
  74. 'button.editor',
  75. () => dialog.show(),
  76. () => true
  77. )
  78. panel.addCommonContextButtons()
  79. panel.addServiceContextButtons()
  80. application.panelManager.addPanel(this.panel)
  81. }
  82. activate() {
  83. this.panel.visible = true
  84. if (this.panel.service === null) {
  85. this.panel.goHome()
  86. }
  87. }
  88. deactivate() {
  89. this.panel.visible = false
  90. }
  91. onSave(name, code) {
  92. const panel = this.panel
  93. panel.entryName = name
  94. panel.entryType = Metadata.FILE
  95. const path = panel.basePath + '/' + name
  96. if (panel.service) {
  97. panel.savePath(path, code, () => (this.dialog.saved = true))
  98. } else {
  99. MessageDialog.create('ERROR', 'message.select_directory')
  100. .setClassName('error')
  101. .setI18N(this.application.i18n)
  102. .show()
  103. }
  104. }
  105. openScript(action) {
  106. const dialog = this.dialog
  107. const application = this.application
  108. if (!dialog.saved) {
  109. ConfirmDialog.create('title.unsaved_changes', 'question.discard_changes', dialog.scriptName)
  110. .setAction(action)
  111. .setAcceptLabel('button.discard')
  112. .setCancelLabel('button.no')
  113. .setI18N(application.i18n)
  114. .show()
  115. } else {
  116. action()
  117. }
  118. }
  119. setScript(url, code) {
  120. const dialog = this.dialog
  121. const index = url.lastIndexOf('/')
  122. let name = url.substring(index + 1)
  123. dialog.scriptName = name
  124. dialog.scriptCode = code
  125. dialog.saved = true
  126. dialog.clearConsole()
  127. if (this.edit) {
  128. this.edit = false
  129. dialog.show()
  130. } else {
  131. let error = dialog.run()
  132. if (error) {
  133. dialog.show()
  134. }
  135. }
  136. }
  137. }
  138. export { ScriptTool }