notes-server/notes/edit.go

76 lines
2.4 KiB
Go
Executable File

package notes
import (
"errors"
"fmt"
"io/ioutil"
"gitea.inhome.blapointe.com/local/notes-server/filetree"
"gitea.inhome.blapointe.com/local/notes-server/notes/editor"
"strings"
)
func (n *Notes) Edit(urlPath string) (string, error) {
p := filetree.NewPathFromURL(urlPath)
if p.IsDir() {
return "", errors.New("path is dir")
}
return editFile(p), nil
}
func editFile(p filetree.Path) string {
href := p.HREF
href = strings.TrimPrefix(href, "/")
hrefs := strings.SplitN(href, "/", 2)
href = hrefs[0]
if len(hrefs) > 1 {
href = hrefs[1]
}
b, _ := ioutil.ReadFile(p.Local)
return fmt.Sprintf(`
<div class="form">
<form action="/submit/%s" method="post">
<textarea id="mytext" name="content" style="cursor:crosshair;">%s</textarea>
<button type="submit">Submit</button>
</form>
</div>
<!--
https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.59.4/codemirror.min.js
https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.59.4/codemirror.min.css
https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.59.4/keymap/vim.min.js
https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.59.4/theme/dracula.min.css
-->
<script>%s</script>
<style>%s</style>
<script>%s</script>
<style>%s</style>
<script>
if( ! /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
CodeMirror.fromTextArea(document.getElementById("mytext"), {
lineNumbers: true,
mode: "md",
theme: "dracula",
keyMap: "vim",
smartIndent: true,
indentUnit: 3,
tabSize: 3,
indentWithTabs: false,
lineWrapping: true,
autofocus: true,
dragDrop: false,
spellcheck: true,
autocorrect: false,
autocapitalize: false,
extraKeys: {
Tab: (cm) => {
var spaces = Array(cm.getOption("indentUnit") + 1).join(" ");
cm.replaceSelection(spaces);
},
},
})
} else {
console.log(navigator.userAgent)
}
</script>
`, href, b, editor.CodeMirrorJS, editor.CodeMirrorCSS, editor.CodeMirrorVIM, editor.CodeMirrorTheme)
}