forked from atom-community/atom-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript-input-view.js
81 lines (66 loc) · 1.73 KB
/
script-input-view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"use babel"
import { Emitter, CompositeDisposable } from "atom"
import { View } from "atom-space-pen-views-plus"
export default class ScriptInputView extends View {
static content() {
this.div({ class: "script-input-view" }, () => {
this.div({ class: "caption" }, "")
this.tag("atom-text-editor", { mini: "", class: "editor mini" })
})
}
initialize(options) {
this.options = options
this.emitter = new Emitter()
this.panel = atom.workspace.addModalPanel({ item: this })
this.panel.hide()
this.editor = this.find("atom-text-editor").get(0).getModel()
// set default text
if (this.options.default) {
this.editor.setText(this.options.default)
this.editor.selectAll()
}
// caption text
if (this.options.caption) {
this.find(".caption").text(this.options.caption)
}
this.find("atom-text-editor").on("keydown", (e) => {
if (e.keyCode === 27) {
e.stopPropagation()
this.emitter.emit("on-cancel")
this.hide()
}
})
this.subscriptions = new CompositeDisposable()
this.subscriptions.add(
atom.commands.add("atom-workspace", {
"core:confirm": () => {
this.emitter.emit("on-confirm", this.editor.getText().trim())
this.hide()
},
})
)
}
onConfirm(callback) {
return this.emitter.on("on-confirm", callback)
}
onCancel(callback) {
return this.emitter.on("on-cancel", callback)
}
focus() {
this.find("atom-text-editor").focus()
}
show() {
this.panel.show()
this.focus()
}
hide() {
this.panel.hide()
this.destroy()
}
destroy() {
if (this.subscriptions) {
this.subscriptions.dispose()
}
this.panel.destroy()
}
}