Skip to content

Commit

Permalink
Initial support for keybindings
Browse files Browse the repository at this point in the history
  • Loading branch information
wargio committed Nov 16, 2021
1 parent 0566339 commit 71e792d
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 96 deletions.
184 changes: 92 additions & 92 deletions assets.go

Large diffs are not rendered by default.

64 changes: 63 additions & 1 deletion assets/templates/settings-edit.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,42 @@
event.preventDefault();
}
}
{{ else }}
function confirm_default(event) {
if (!window.confirm("Are you sure you want to restore to the default value?")) {
event.preventDefault();
}
}
function update_pressed_key(event) {
event.preventDefault();
var keys = [];
if (event.ctrlKey) {
keys.push("Control");
}
if (event.shiftKey) {
keys.push("Shift");
}
if (['control', 'shift'].indexOf(event.key.toLowerCase()) < 0) {
keys.push(event.key.toUpperCase());
}
document.getElementById('value').value = keys.join(',');
document.getElementById('value-human-readable').textContent = keys.join(' + ');
}
function set_onkeypress(elem) {
event.preventDefault();
if (document.body.onkeydown) {
document.body.onkeydown = null;
elem.textContent = "Edit";
} else {
elem.textContent = "Accept";
document.body.onkeydown = update_pressed_key;
}
elem.disabled = false;
}
{{ end }}
</script>
</head>
<body>
<body{{ if eq .action "keybindings" }} onload="init()"{{ end }}>
<section class="container grid-960 mt-10">
<header class="navbar navbar-fixed-height">
<section class="navbar-section">
Expand All @@ -56,6 +88,36 @@
<input type="text" class="form-input" name="key" id="key" value="{{ .editkey }}" placeholder="Environment Variable Name"/>
<label class="form-label" for="value">Value</label>
<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)"/>
{{ else }}{{ $value := index .data .editkey }}
<input type="hidden" name="value" id="value" value="{{ $value }}"/>
<input type="hidden" name="section" value="{{ .section }}" />
<input type="hidden" name="key" id="key" value="{{ .editkey }}"/>

<div class="columns col-gapless">
<div class="column col-3 spacing-left">
<div class="input-group">
<span class="input-group-addon detail-size spacing-top height-32px">Section</span>
</div>
<div class="input-group">
<span class="input-group-addon detail-size spacing-top height-32px">Key Function</span>
</div>
<div class="input-group">
<span class="input-group-addon detail-size spacing-top height-32px">Key Binding</span>
</div>
</div>
<div class="column col-8">
<div class="input-group spacing-left">
<span class="input-group-addon detail-size spacing-top height-32px">{{ .section }}</span>
</div>
<div class="input-group">
<span class="input-group-addon detail-size spacing-top height-32px">{{ .editkey }}</span>
</div>
<div class="input-group ">
<span class="input-group-addon detail-size spacing-top height-32px" style="width: 250px;" id="value-human-readable">{{ keycombo $value }}</span>
<button class="btn btn-primary spacing-top height-32px" style="width: 100px; padding-right: 3px" onclick="set_onkeypress(this)">Edit</button>
</div>
</div>
</div>
{{ end }}
</div>
<div class="form-group">
Expand Down
29 changes: 29 additions & 0 deletions assets/templates/settings.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,35 @@
New Environment Variable
</a>
</p>
<p>
<h3 class="s-title">KeyBindings</h3>
{{ range $section, $map := .keybindings }}
<h5 class="s-title" style="margin-bottom: 0px;">{{ $section }}</h5>
<div class="columns col-gapless">
<div class="column col-1 col-btn spacing-left">
{{ range $name, $keynum := $map }}
<a class="btn spacing-top" href="{{ $root }}settings/keybindings/{{ $section }}/{{ $name }}">
<i class="icon icon-edit">Edit</i>
</a>
{{ end }}
</div>
<div class="column col-3 spacing-left">
{{ range $name, $keynum := $map }}
<div class="input-group">
<span class="input-group-addon detail-size spacing-top height-32px">{{ $name }}</span>
</div>
{{ end }}
</div>
<div class="column col-8">
{{ range $name, $keynum := $map }}
<div class="input-group spacing-left">
<span class="input-group-addon height-32px spacing-top">{{ keycombo $keynum }}</span>
</div>
{{ end }}
</div>
</div>
{{ end }}
</p>
</section>
</section>
</body>
Expand Down
50 changes: 50 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,28 @@ const (
KB_PAGE = "Page"
)

type KBindings map[string]map[string]string

type NotebookConfig struct {
Environment map[string]string `json:"environment"`
KeyBindings KBindings `json:"keybindings"`
filename string
mutex sync.Mutex
}

var availableKeyBindings = KBindings{
KB_INDEX: {
"New Page": "Control,N",
"About": "Control,H",
"Settings": "Control,S",
},
KB_PAGE: {
"Open/Close Pipe": "Control,O",
"New Markdown": "Control,M",
"Execute Command": "Control,E",
},
}

func getValue(kmap map[string]string, action, defkey string) string {
if kmap == nil {
return defkey
Expand All @@ -32,6 +48,19 @@ func getValue(kmap map[string]string, action, defkey string) string {
return defkey
}

func sanitizeKeyBindings(keyBindings KBindings) KBindings {
sanitized := KBindings{}
for section, kb := range availableKeyBindings {
msection := map[string]string{}
fsection := keyBindings[section]
for action, key := range kb {
msection[action] = getValue(fsection, action, key)
}
sanitized[section] = msection
}
return sanitized
}

func NewNotebookConfig(folder string) *NotebookConfig {
var config = &NotebookConfig{}
config.filename = path.Join(folder, CONFIG_FILE)
Expand All @@ -42,6 +71,12 @@ func NewNotebookConfig(folder string) *NotebookConfig {
if config.Environment == nil {
config.Environment = map[string]string{}
}
if config.KeyBindings == nil {
config.KeyBindings = availableKeyBindings
config.Save()
} else {
config.KeyBindings = sanitizeKeyBindings(config.KeyBindings)
}

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

func (nc *NotebookConfig) SetKeyBindings(section, key, value string) bool {
nc.mutex.Lock()
defer nc.mutex.Unlock()
value = strings.TrimSpace(value)
key = strings.TrimSpace(key)
if _, ok := nc.KeyBindings[section]; !ok {
return false
}
if _, ok := nc.KeyBindings[section][key]; !ok {
return false
}
nc.KeyBindings[section][key] = value
return true
}

func (nc *NotebookConfig) Save() {
nc.mutex.Lock()
defer nc.mutex.Unlock()
Expand Down
27 changes: 24 additions & 3 deletions server_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,36 @@ func serverAddSettings(root *gin.RouterGroup) {
c.HTML(200, "settings.tmpl", gin.H{
"root": webroot,
"environment": config.Environment,
"keybindings": config.KeyBindings,
})
})
root.GET("/settings/:action/:section/:editkey", func(c *gin.Context) {
action := c.Param("action")
section := c.Param("section")
editkey := c.Param("editkey")
if action != "environment" {
if action != "environment" && action != "keybindings" {
c.HTML(404, "error.tmpl", gin.H{
"root": webroot,
"error": "cannot find page",
})
return
} else if action == "keybindings" {
if _, ok := config.KeyBindings[section]; !ok {
c.HTML(404, "error.tmpl", gin.H{
"root": webroot,
"error": "cannot find page",
})
return
}
}
if editkey == "new" {
editkey = ""
}
var data map[string]string = nil
var data map[string]string
if action == "environment" {
data = config.Environment
} else {
data = config.KeyBindings[section]
}
c.HTML(200, "settings-edit.tmpl", gin.H{
"root": webroot,
Expand All @@ -43,7 +54,7 @@ func serverAddSettings(root *gin.RouterGroup) {
value := c.DefaultPostForm("value", "")
action := c.DefaultPostForm("action", "")

if action != "environment" {
if action != "environment" && action != "keybindings" {
c.HTML(404, "error.tmpl", gin.H{
"root": webroot,
"location": webroot + "settings",
Expand All @@ -70,6 +81,16 @@ func serverAddSettings(root *gin.RouterGroup) {
}
config.SetEnvironment(key, value)
}
} else if action == "keybindings" {
section := c.DefaultPostForm("section", "")
if !config.SetKeyBindings(section, key, value) {
c.HTML(404, "error.tmpl", gin.H{
"root": webroot,
"location": webroot + "settings",
"error": "invalid settings (Key Bindings)",
})
return
}
}
config.Save()
c.Redirect(302, webroot+"settings")
Expand Down

0 comments on commit 71e792d

Please sign in to comment.