123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- /*
- * ScriptTool.js
- *
- * @author realor
- */
- import { Tool } from './Tool.js'
- import { FileExplorer } from '../ui/FileExplorer.js'
- import { GeometryUtils } from '../utils/GeometryUtils.js'
- import { ObjectUtils } from '../utils/ObjectUtils.js'
- import { MessageDialog } from '../ui/MessageDialog.js'
- import { ConfirmDialog } from '../ui/ConfirmDialog.js'
- import { Toast } from '../ui/Toast.js'
- import { Solid } from '../core/Solid.js'
- import { SolidGeometry } from '../core/SolidGeometry.js'
- import { Cord } from '../core/Cord.js'
- import { CordGeometry } from '../core/CordGeometry.js'
- import { Profile } from '../core/Profile.js'
- import { ProfileGeometry } from '../core/ProfileGeometry.js'
- import { Metadata, Result } from '../io/FileService.js'
- import { ScriptDialog } from '../ui/ScriptDialog.js'
- import { I18N } from '../i18n/I18N.js'
- import * as THREE from '../lib/three.module.js'
- class ScriptTool extends Tool {
- constructor(application, options) {
- super(application)
- this.name = 'script'
- this.label = 'tool.script.label'
- this.className = 'script'
- this.setOptions(options)
- const panel = new FileExplorer(application, false)
- panel.showFileSize = false
- this.panel = panel
- this.edit = false
- panel.title = this.label
- panel.group = 'script'
- const dialog = new ScriptDialog(this.application, (name, code) => this.onSave(name, code))
- this.dialog = dialog
- panel.openFile = (url, code) => {
- this.openScript(() => this.setScript(url, code))
- }
- panel.addContextButton(
- 'open',
- 'button.open',
- () => panel.openEntry(),
- () => panel.isEntrySelected() && !panel.isFileEntrySelected()
- )
- panel.addContextButton(
- 'open_file',
- 'button.run',
- () => panel.openEntry(),
- () => panel.isDirectoryList() && panel.isFileEntrySelected()
- )
- panel.addContextButton(
- 'edit',
- 'button.edit',
- () => {
- this.edit = true
- panel.openEntry()
- },
- () => panel.isDirectoryList() && panel.isEntrySelected()
- )
- panel.addContextButton(
- 'new',
- 'button.new',
- () =>
- this.openScript(() => {
- this.edit = true
- this.setScript('', '')
- }),
- () => true
- )
- panel.addContextButton(
- 'editor',
- 'button.editor',
- () => dialog.show(),
- () => true
- )
- panel.addCommonContextButtons()
- panel.addServiceContextButtons()
- application.panelManager.addPanel(this.panel)
- }
- activate() {
- this.panel.visible = true
- if (this.panel.service === null) {
- this.panel.goHome()
- }
- }
- deactivate() {
- this.panel.visible = false
- }
- onSave(name, code) {
- const panel = this.panel
- panel.entryName = name
- panel.entryType = Metadata.FILE
- const path = panel.basePath + '/' + name
- if (panel.service) {
- panel.savePath(path, code, () => (this.dialog.saved = true))
- } else {
- MessageDialog.create('ERROR', 'message.select_directory')
- .setClassName('error')
- .setI18N(this.application.i18n)
- .show()
- }
- }
- openScript(action) {
- const dialog = this.dialog
- const application = this.application
- if (!dialog.saved) {
- ConfirmDialog.create('title.unsaved_changes', 'question.discard_changes', dialog.scriptName)
- .setAction(action)
- .setAcceptLabel('button.discard')
- .setCancelLabel('button.no')
- .setI18N(application.i18n)
- .show()
- } else {
- action()
- }
- }
- setScript(url, code) {
- const dialog = this.dialog
- const index = url.lastIndexOf('/')
- let name = url.substring(index + 1)
- dialog.scriptName = name
- dialog.scriptCode = code
- dialog.saved = true
- dialog.clearConsole()
- if (this.edit) {
- this.edit = false
- dialog.show()
- } else {
- let error = dialog.run()
- if (error) {
- dialog.show()
- }
- }
- }
- }
- export { ScriptTool }
|