You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
286 lines
6.5 KiB
286 lines
6.5 KiB
package caddyhugo
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
|
|
"git.stephensearles.com/stephen/acedoc"
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
)
|
|
|
|
func (t tmplData) Content() ([]string, error) {
|
|
var files []string
|
|
|
|
err := filepath.Walk(path.Join(t.Site.Root, "content"), func(name string, fi os.FileInfo, err error) error {
|
|
if fi.IsDir() {
|
|
return nil
|
|
}
|
|
|
|
files = append(files, name)
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return nil, err
|
|
}
|
|
|
|
return files, nil
|
|
}
|
|
|
|
func (t tmplData) ContentTypes() ([]string, error) {
|
|
layoutDir, err := os.Open(path.Join(t.Site.Root, "archetypes"))
|
|
if err != nil {
|
|
fmt.Println("opening layout dir", err)
|
|
return nil, err
|
|
}
|
|
defer layoutDir.Close()
|
|
|
|
names, err := layoutDir.Readdirnames(0)
|
|
if err != nil {
|
|
fmt.Println("reading dir", err)
|
|
return nil, err
|
|
}
|
|
|
|
out := []string{"default"}
|
|
for _, name := range names {
|
|
out = append(out, name[:len(name)-len(filepath.Ext(name))])
|
|
}
|
|
|
|
return out, nil
|
|
}
|
|
|
|
type tmplData struct {
|
|
Site *httpserver.SiteConfig
|
|
R *http.Request
|
|
CaddyHugo
|
|
Doc *acedoc.Document
|
|
docref *docref
|
|
}
|
|
|
|
func (t tmplData) LoadContent() (string, error) {
|
|
return t.Doc.Contents(), nil
|
|
}
|
|
|
|
func baseNoExt(name string) string {
|
|
base := path.Base(name)
|
|
return base[:len(base)-len(path.Ext(base))]
|
|
}
|
|
|
|
func (t tmplData) IframeSource() string {
|
|
name := baseNoExt(t.docref.name)
|
|
ctype := baseNoExt(path.Dir(t.docref.name))
|
|
return fmt.Sprintf("/hugo/draft/%s/%s/%s/", base64.RawURLEncoding.EncodeToString([]byte(t.docref.name)), ctype, name)
|
|
}
|
|
|
|
var EditPage = `<html>
|
|
<head>
|
|
<script src="/hugo/simplemde.js"></script>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js"></script>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-settings_menu.js"></script>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-keybinding_menu.js"></script>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/keybinding-vim.js"></script>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/mode-markdown.js"></script>
|
|
<style type="text/css" media="screen">
|
|
#editor {
|
|
position: absolute;
|
|
top: 50px;
|
|
right: 0;
|
|
bottom: 0;
|
|
left: 40%;
|
|
}
|
|
#draft {
|
|
position: absolute;
|
|
top: 50px;
|
|
right: 60%;
|
|
bottom: 0;
|
|
left: 0;
|
|
|
|
}
|
|
#draft > iframe {
|
|
height: 100%;
|
|
width: 100%;
|
|
border: none;
|
|
}
|
|
</style>
|
|
<link rel="stylesheet" href="/hugo/simplemde.css" />
|
|
|
|
<body>
|
|
<textarea id="editor">{{ .LoadContent }}</textarea>
|
|
<div id="draft"><iframe src="{{ .IframeSource }}">Loading draft...</iframe></div>
|
|
<script>
|
|
var ltime = 0;
|
|
function getLtime() {
|
|
ltime++
|
|
return ltime
|
|
}
|
|
|
|
function observe(l) {
|
|
if (l > ltime) {
|
|
ltime = l;
|
|
}
|
|
}
|
|
|
|
var editorElem = document.getElementById("editor");
|
|
var editor = new SimpleMDE({element: editorElem, forceSync: true});
|
|
|
|
// Create WebSocket connection.
|
|
const socket = new WebSocket('ws://' + location.host + location.pathname);
|
|
|
|
var iframe = document.querySelector("#draft > iframe");
|
|
|
|
const sawChangesBumpsTo = 10;
|
|
|
|
var sawChanges = -1;
|
|
window.setInterval(function () {
|
|
if (sawChanges >= 0) {
|
|
sawChanges--;
|
|
if (sawChanges == 0) {
|
|
iframe.contentWindow.location.reload();
|
|
}
|
|
}
|
|
}, 50);
|
|
|
|
// Listen for messages
|
|
socket.addEventListener('message', function (event) {
|
|
var message = JSON.parse(event.data);
|
|
observe(message.ltime);
|
|
|
|
var deltas = [];
|
|
deltas.push.apply(deltas, message.deltas);
|
|
|
|
deltas.forEach(function(aceDelta) {
|
|
var cmDelta = aceDeltaToCM(aceDelta)
|
|
console.log(cmDelta);
|
|
|
|
var content = ""
|
|
var to = {
|
|
line: aceDelta.start.row,
|
|
ch: aceDelta.start.column,
|
|
}
|
|
|
|
if (aceDelta.action == "insert") {
|
|
content = aceDelta.lines.join("\n");
|
|
to = null;
|
|
}
|
|
|
|
editor.codemirror.doc.replaceRange(content, cmDelta.from, to, "dontreflect");
|
|
sawChanges = sawChangesBumpsTo;
|
|
})
|
|
});
|
|
|
|
socket.addEventListener('error', function (err) {
|
|
console.log(err);
|
|
});
|
|
|
|
editor.codemirror.on("change", function (cm, cmDelta) {
|
|
if (cmDelta.origin == "dontreflect") {
|
|
return;
|
|
}
|
|
|
|
var aceDelta = cmDeltaToAce(cmDelta);
|
|
console.log(cmDelta, "=>", aceDelta)
|
|
|
|
sawChanges = sawChangesBumpsTo;
|
|
socket.send(JSON.stringify({
|
|
"deltas": [aceDelta],
|
|
"ltime": getLtime(),
|
|
}))
|
|
})
|
|
|
|
function cmDeltaToAce(cmDelta) {
|
|
var isRemove = (cmDelta.removed.length > 0 && cmDelta.removed[0].length > 0) || cmDelta.removed.length > 1;
|
|
var lines = isRemove ? cmDelta.removed : cmDelta.text;
|
|
var aceDelta = {
|
|
action: isRemove ? "remove" : "insert",
|
|
lines: lines,
|
|
start: {
|
|
row: cmDelta.from.line,
|
|
column: cmDelta.from.ch,
|
|
},
|
|
end: {
|
|
row: cmDelta.from.line + (isRemove ? lines.length - 1 : lines.length - 1 ),
|
|
column: lines[lines.length-1].length,
|
|
}
|
|
};
|
|
|
|
if (aceDelta.start.row == aceDelta.end.row) {
|
|
aceDelta.end.column += cmDelta.from.ch;
|
|
}
|
|
|
|
if (false && isRemove && aceDelta.start.row == aceDelta.end.row) {
|
|
var origStart = aceDelta.start;
|
|
aceDelta.start = aceDelta.end;
|
|
aceDelta.end = origStart;
|
|
aceDelta.start.column += cmDelta.from.ch;
|
|
}
|
|
|
|
|
|
return aceDelta;
|
|
}
|
|
|
|
function aceDeltaToCM(aceDelta) {
|
|
|
|
var cmDelta = {
|
|
text: [],
|
|
removed: [],
|
|
from: {
|
|
line: aceDelta.start.row,
|
|
ch: aceDelta.start.column,
|
|
},
|
|
to: {
|
|
// cm deltas are weird. to refers to the selection end, which
|
|
// with a simple blinking cursor with no selection, is always
|
|
// the same as from
|
|
line: aceDelta.start.row,
|
|
ch: aceDelta.start.column,
|
|
},
|
|
}
|
|
|
|
if (aceDelta.action == "remove") {
|
|
var origStart = aceDelta.start;
|
|
aceDelta.start = aceDelta.end;
|
|
aceDelta.end = origStart;
|
|
|
|
cmDelta.removed = aceDelta.lines
|
|
cmDelta.text = [""]
|
|
} else {
|
|
cmDelta.text = aceDelta.lines
|
|
cmDelta.removed = [""]
|
|
}
|
|
|
|
return cmDelta;
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>`
|
|
|
|
var AdminPage = `<html><body>not implemented</body></html>`
|
|
|
|
var AuthorPage = `<html>
|
|
<head>
|
|
</head>
|
|
<body>
|
|
<p>Create content:</p>
|
|
<form action="/hugo/edit/new" method="POST">
|
|
<label>Name: <input type="text" name="name" /></label>
|
|
<select name="type">
|
|
{{- range .ContentTypes }}
|
|
<option value="{{ . }}">{{ . }}</option>
|
|
{{- end }}
|
|
</select>
|
|
<input type="submit" />
|
|
</form>
|
|
|
|
<p>Edit content:</p>
|
|
<ul>{{ range .Content }}
|
|
<li><a href="/hugo/edit/{{ . }}">{{ . }}</a></li>
|
|
{{- end }}
|
|
</ul>
|
|
</body>
|
|
</html>`
|
|
|