Skip to content

Commit 71e792d

Browse files
committed
Initial support for keybindings
1 parent 0566339 commit 71e792d

File tree

5 files changed

+258
-96
lines changed

5 files changed

+258
-96
lines changed

assets.go

Lines changed: 92 additions & 92 deletions
Large diffs are not rendered by default.

assets/templates/settings-edit.tmpl

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,42 @@
2929
event.preventDefault();
3030
}
3131
}
32+
{{ else }}
33+
function confirm_default(event) {
34+
if (!window.confirm("Are you sure you want to restore to the default value?")) {
35+
event.preventDefault();
36+
}
37+
}
38+
function update_pressed_key(event) {
39+
event.preventDefault();
40+
var keys = [];
41+
if (event.ctrlKey) {
42+
keys.push("Control");
43+
}
44+
if (event.shiftKey) {
45+
keys.push("Shift");
46+
}
47+
if (['control', 'shift'].indexOf(event.key.toLowerCase()) < 0) {
48+
keys.push(event.key.toUpperCase());
49+
}
50+
document.getElementById('value').value = keys.join(',');
51+
document.getElementById('value-human-readable').textContent = keys.join(' + ');
52+
}
53+
function set_onkeypress(elem) {
54+
event.preventDefault();
55+
if (document.body.onkeydown) {
56+
document.body.onkeydown = null;
57+
elem.textContent = "Edit";
58+
} else {
59+
elem.textContent = "Accept";
60+
document.body.onkeydown = update_pressed_key;
61+
}
62+
elem.disabled = false;
63+
}
3264
{{ end }}
3365
</script>
3466
</head>
35-
<body>
67+
<body{{ if eq .action "keybindings" }} onload="init()"{{ end }}>
3668
<section class="container grid-960 mt-10">
3769
<header class="navbar navbar-fixed-height">
3870
<section class="navbar-section">
@@ -56,6 +88,36 @@
5688
<input type="text" class="form-input" name="key" id="key" value="{{ .editkey }}" placeholder="Environment Variable Name"/>
5789
<label class="form-label" for="value">Value</label>
5890
<input type="text" class="form-input" name="value" id="value" value="{{ if gt $eklen 0 }}{{ index .data .editkey }}{{ end }}" placeholder="Insert a variable value (or leave it empty)"/>
91+
{{ else }}{{ $value := index .data .editkey }}
92+
<input type="hidden" name="value" id="value" value="{{ $value }}"/>
93+
<input type="hidden" name="section" value="{{ .section }}" />
94+
<input type="hidden" name="key" id="key" value="{{ .editkey }}"/>
95+
96+
<div class="columns col-gapless">
97+
<div class="column col-3 spacing-left">
98+
<div class="input-group">
99+
<span class="input-group-addon detail-size spacing-top height-32px">Section</span>
100+
</div>
101+
<div class="input-group">
102+
<span class="input-group-addon detail-size spacing-top height-32px">Key Function</span>
103+
</div>
104+
<div class="input-group">
105+
<span class="input-group-addon detail-size spacing-top height-32px">Key Binding</span>
106+
</div>
107+
</div>
108+
<div class="column col-8">
109+
<div class="input-group spacing-left">
110+
<span class="input-group-addon detail-size spacing-top height-32px">{{ .section }}</span>
111+
</div>
112+
<div class="input-group">
113+
<span class="input-group-addon detail-size spacing-top height-32px">{{ .editkey }}</span>
114+
</div>
115+
<div class="input-group ">
116+
<span class="input-group-addon detail-size spacing-top height-32px" style="width: 250px;" id="value-human-readable">{{ keycombo $value }}</span>
117+
<button class="btn btn-primary spacing-top height-32px" style="width: 100px; padding-right: 3px" onclick="set_onkeypress(this)">Edit</button>
118+
</div>
119+
</div>
120+
</div>
59121
{{ end }}
60122
</div>
61123
<div class="form-group">

assets/templates/settings.tmpl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,35 @@
7070
New Environment Variable
7171
</a>
7272
</p>
73+
<p>
74+
<h3 class="s-title">KeyBindings</h3>
75+
{{ range $section, $map := .keybindings }}
76+
<h5 class="s-title" style="margin-bottom: 0px;">{{ $section }}</h5>
77+
<div class="columns col-gapless">
78+
<div class="column col-1 col-btn spacing-left">
79+
{{ range $name, $keynum := $map }}
80+
<a class="btn spacing-top" href="{{ $root }}settings/keybindings/{{ $section }}/{{ $name }}">
81+
<i class="icon icon-edit">Edit</i>
82+
</a>
83+
{{ end }}
84+
</div>
85+
<div class="column col-3 spacing-left">
86+
{{ range $name, $keynum := $map }}
87+
<div class="input-group">
88+
<span class="input-group-addon detail-size spacing-top height-32px">{{ $name }}</span>
89+
</div>
90+
{{ end }}
91+
</div>
92+
<div class="column col-8">
93+
{{ range $name, $keynum := $map }}
94+
<div class="input-group spacing-left">
95+
<span class="input-group-addon height-32px spacing-top">{{ keycombo $keynum }}</span>
96+
</div>
97+
{{ end }}
98+
</div>
99+
</div>
100+
{{ end }}
101+
</p>
73102
</section>
74103
</section>
75104
</body>

config.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,28 @@ const (
1616
KB_PAGE = "Page"
1717
)
1818

19+
type KBindings map[string]map[string]string
20+
1921
type NotebookConfig struct {
2022
Environment map[string]string `json:"environment"`
23+
KeyBindings KBindings `json:"keybindings"`
2124
filename string
2225
mutex sync.Mutex
2326
}
2427

28+
var availableKeyBindings = KBindings{
29+
KB_INDEX: {
30+
"New Page": "Control,N",
31+
"About": "Control,H",
32+
"Settings": "Control,S",
33+
},
34+
KB_PAGE: {
35+
"Open/Close Pipe": "Control,O",
36+
"New Markdown": "Control,M",
37+
"Execute Command": "Control,E",
38+
},
39+
}
40+
2541
func getValue(kmap map[string]string, action, defkey string) string {
2642
if kmap == nil {
2743
return defkey
@@ -32,6 +48,19 @@ func getValue(kmap map[string]string, action, defkey string) string {
3248
return defkey
3349
}
3450

51+
func sanitizeKeyBindings(keyBindings KBindings) KBindings {
52+
sanitized := KBindings{}
53+
for section, kb := range availableKeyBindings {
54+
msection := map[string]string{}
55+
fsection := keyBindings[section]
56+
for action, key := range kb {
57+
msection[action] = getValue(fsection, action, key)
58+
}
59+
sanitized[section] = msection
60+
}
61+
return sanitized
62+
}
63+
3564
func NewNotebookConfig(folder string) *NotebookConfig {
3665
var config = &NotebookConfig{}
3766
config.filename = path.Join(folder, CONFIG_FILE)
@@ -42,6 +71,12 @@ func NewNotebookConfig(folder string) *NotebookConfig {
4271
if config.Environment == nil {
4372
config.Environment = map[string]string{}
4473
}
74+
if config.KeyBindings == nil {
75+
config.KeyBindings = availableKeyBindings
76+
config.Save()
77+
} else {
78+
config.KeyBindings = sanitizeKeyBindings(config.KeyBindings)
79+
}
4580

4681
if value, ok := config.Environment["RIZIN_PATH"]; !ok || len(value) < 1 {
4782
config.Environment["RIZIN_PATH"] = os.Getenv("RIZIN_PATH")
@@ -74,6 +109,21 @@ func (nc *NotebookConfig) SetEnvironment(key, value string) {
74109
nc.Environment[key] = value
75110
}
76111

112+
func (nc *NotebookConfig) SetKeyBindings(section, key, value string) bool {
113+
nc.mutex.Lock()
114+
defer nc.mutex.Unlock()
115+
value = strings.TrimSpace(value)
116+
key = strings.TrimSpace(key)
117+
if _, ok := nc.KeyBindings[section]; !ok {
118+
return false
119+
}
120+
if _, ok := nc.KeyBindings[section][key]; !ok {
121+
return false
122+
}
123+
nc.KeyBindings[section][key] = value
124+
return true
125+
}
126+
77127
func (nc *NotebookConfig) Save() {
78128
nc.mutex.Lock()
79129
defer nc.mutex.Unlock()

server_settings.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,36 @@ func serverAddSettings(root *gin.RouterGroup) {
1010
c.HTML(200, "settings.tmpl", gin.H{
1111
"root": webroot,
1212
"environment": config.Environment,
13+
"keybindings": config.KeyBindings,
1314
})
1415
})
1516
root.GET("/settings/:action/:section/:editkey", func(c *gin.Context) {
1617
action := c.Param("action")
1718
section := c.Param("section")
1819
editkey := c.Param("editkey")
19-
if action != "environment" {
20+
if action != "environment" && action != "keybindings" {
2021
c.HTML(404, "error.tmpl", gin.H{
2122
"root": webroot,
2223
"error": "cannot find page",
2324
})
2425
return
26+
} else if action == "keybindings" {
27+
if _, ok := config.KeyBindings[section]; !ok {
28+
c.HTML(404, "error.tmpl", gin.H{
29+
"root": webroot,
30+
"error": "cannot find page",
31+
})
32+
return
33+
}
2534
}
2635
if editkey == "new" {
2736
editkey = ""
2837
}
29-
var data map[string]string = nil
38+
var data map[string]string
3039
if action == "environment" {
3140
data = config.Environment
41+
} else {
42+
data = config.KeyBindings[section]
3243
}
3344
c.HTML(200, "settings-edit.tmpl", gin.H{
3445
"root": webroot,
@@ -43,7 +54,7 @@ func serverAddSettings(root *gin.RouterGroup) {
4354
value := c.DefaultPostForm("value", "")
4455
action := c.DefaultPostForm("action", "")
4556

46-
if action != "environment" {
57+
if action != "environment" && action != "keybindings" {
4758
c.HTML(404, "error.tmpl", gin.H{
4859
"root": webroot,
4960
"location": webroot + "settings",
@@ -70,6 +81,16 @@ func serverAddSettings(root *gin.RouterGroup) {
7081
}
7182
config.SetEnvironment(key, value)
7283
}
84+
} else if action == "keybindings" {
85+
section := c.DefaultPostForm("section", "")
86+
if !config.SetKeyBindings(section, key, value) {
87+
c.HTML(404, "error.tmpl", gin.H{
88+
"root": webroot,
89+
"location": webroot + "settings",
90+
"error": "invalid settings (Key Bindings)",
91+
})
92+
return
93+
}
7394
}
7495
config.Save()
7596
c.Redirect(302, webroot+"settings")

0 commit comments

Comments
 (0)