OpenFileDialog.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import path from 'node:path'
  2. import {Button, Form, TextInput} from 'tui-lib/ui/controls'
  3. import {Label} from 'tui-lib/ui/presentation'
  4. import Dialog from './Dialog.js'
  5. import FilePickerForm from './FilePickerForm.js'
  6. export default class OpenFileDialog extends Dialog {
  7. constructor() {
  8. super()
  9. this.visible = false
  10. this.form = new Form()
  11. this.pane.addChild(this.form)
  12. this.filePathLabel = new Label('Enter file path:')
  13. this.filePathInput = new TextInput()
  14. this.openButton = new Button('Open')
  15. this.cancelButton = new Button('Cancel')
  16. this.filePickerForm = new FilePickerForm()
  17. this.filePickerForm.captureTab = false
  18. this.form.addChild(this.filePathLabel)
  19. this.form.addInput(this.filePathInput)
  20. this.form.addInput(this.filePickerForm)
  21. this.form.addInput(this.openButton)
  22. this.form.addInput(this.cancelButton)
  23. this._resolve = null
  24. this.openButton.on('pressed', () => {
  25. this._resolve(this.filePathInput.value)
  26. })
  27. this.filePathInput.on('value', () => {
  28. this._resolve(this.filePathInput.value)
  29. })
  30. {
  31. const cb = append => p => {
  32. this.filePathInput.setValue((path.relative(process.cwd(), p) || '.') + append)
  33. }
  34. this.filePickerForm.on('selected', cb(''))
  35. this.filePickerForm.on('browsingDirectory', cb('/'))
  36. }
  37. this.cancelButton.on('pressed', () => {
  38. this._resolve(null)
  39. })
  40. const dir = (this.lastFilePath
  41. ? path.relative(process.cwd(), path.dirname(this.lastFilePath)) + '/'
  42. : './')
  43. this.filePathInput.setValue(dir)
  44. this.filePickerForm.fillItems(dir)
  45. }
  46. fixLayout() {
  47. super.fixLayout()
  48. this.pane.w = Math.min(this.contentW, 40)
  49. this.pane.h = Math.min(this.contentH, 20)
  50. this.pane.centerInParent()
  51. this.form.w = this.pane.contentW
  52. this.form.h = this.pane.contentH
  53. this.filePathLabel.x = 0
  54. this.filePathLabel.y = 0
  55. this.filePathInput.x = this.filePathLabel.right + 2
  56. this.filePathInput.y = this.filePathLabel.y
  57. this.filePathInput.w = this.form.contentW - this.filePathInput.x
  58. this.filePickerForm.x = 0
  59. this.filePickerForm.y = this.filePathInput.y + 2
  60. this.filePickerForm.w = this.form.contentW
  61. this.filePickerForm.h = this.form.contentH - this.filePickerForm.y - 2
  62. this.openButton.x = 0
  63. this.openButton.y = this.form.contentH - 1
  64. this.cancelButton.x = this.openButton.right + 2
  65. this.cancelButton.y = this.openButton.y
  66. }
  67. selected() {
  68. this.form.firstInput()
  69. }
  70. go() {
  71. this.visible = true
  72. this.root.select(this)
  73. return new Promise(resolve => {
  74. this._resolve = resolve
  75. }).then(filePath => {
  76. this.visible = false
  77. this.lastFilePath = filePath
  78. return filePath
  79. })
  80. }
  81. }