cleaning up draft related code
This commit is contained in:
@@ -14,10 +14,11 @@ import (
|
||||
)
|
||||
|
||||
type editSession struct {
|
||||
clients uint
|
||||
name string
|
||||
doc *acedoc.Document
|
||||
tmpdir string
|
||||
clients uint
|
||||
docname string
|
||||
filename string
|
||||
doc *acedoc.Document
|
||||
tmpdir string
|
||||
}
|
||||
|
||||
func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) {
|
||||
@@ -32,25 +33,32 @@ func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) {
|
||||
draftPrefix := fmt.Sprintf("draft-%s", base64.RawURLEncoding.EncodeToString([]byte(docName)))
|
||||
tmpdir := path.Join(os.TempDir(), draftPrefix)
|
||||
|
||||
ref := &editSession{
|
||||
name: docName,
|
||||
doc: acedoc.NewString(string(contents)),
|
||||
tmpdir: tmpdir,
|
||||
es := &editSession{
|
||||
docname: docName,
|
||||
filename: filename,
|
||||
doc: acedoc.NewString(string(contents)),
|
||||
tmpdir: tmpdir,
|
||||
}
|
||||
|
||||
err = ref.doc.LogToFile(path.Join(ch.Dir, "logs", docName))
|
||||
err = es.doc.LogToFile(path.Join(ch.Dir, "logs", docName))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ch.docs[filename] = ref
|
||||
ch.docs[filename] = es
|
||||
|
||||
hugoCmd := exec.Command("hugo", "--watch", "-D", "-d", ref.tmpdir)
|
||||
ch.renderDraft(es)
|
||||
|
||||
return es, nil
|
||||
}
|
||||
|
||||
func (ch *CaddyHugo) renderDraft(es *editSession) error {
|
||||
hugoCmd := exec.Command("hugo", "--watch", "-D", "-d", es.tmpdir)
|
||||
hugoCmd.Dir = ch.Dir
|
||||
err = hugoCmd.Start()
|
||||
err := hugoCmd.Start()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error starting hugo: %v", err)
|
||||
return fmt.Errorf("error starting hugo: %v", err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
@@ -72,12 +80,12 @@ func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) {
|
||||
<-ticker.C
|
||||
ch.mtx.Lock()
|
||||
|
||||
err := ioutil.WriteFile(filename, []byte(ref.doc.Contents()), 0644)
|
||||
err := ioutil.WriteFile(es.filename, []byte(es.doc.Contents()), 0644)
|
||||
if err != nil {
|
||||
fmt.Println("error saving document contents:", err)
|
||||
}
|
||||
|
||||
if ref.clients == 0 {
|
||||
if es.clients == 0 {
|
||||
idleTicks++
|
||||
idleTime := time.Duration(idleTicks) * WebsocketFileTicker
|
||||
if idleTime >= IdleWebsocketTimeout {
|
||||
@@ -87,11 +95,13 @@ func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) {
|
||||
fmt.Printf(", error publishing: %v\n", err)
|
||||
}
|
||||
|
||||
ref.doc.Close()
|
||||
os.RemoveAll(tmpdir)
|
||||
delete(ch.docs, filename)
|
||||
es.doc.Close()
|
||||
os.RemoveAll(es.tmpdir)
|
||||
delete(ch.docs, es.filename)
|
||||
ch.mtx.Unlock()
|
||||
return
|
||||
} else {
|
||||
fmt.Println("idle for", idleTime)
|
||||
}
|
||||
} else {
|
||||
idleTicks = 0
|
||||
@@ -100,7 +110,7 @@ func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) {
|
||||
}
|
||||
}()
|
||||
|
||||
return ref, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ch *CaddyHugo) hasEditSession(docName string) (*editSession, bool) {
|
||||
|
||||
+10
-5
@@ -80,12 +80,12 @@ func baseNoExt(name string) string {
|
||||
}
|
||||
|
||||
func (t *tmplData) IframeSource() string {
|
||||
name := baseNoExt(t.docref.name)
|
||||
ctype := baseNoExt(path.Dir(t.docref.name))
|
||||
name := baseNoExt(t.docref.docname)
|
||||
ctype := baseNoExt(path.Dir(t.docref.docname))
|
||||
if ctype == "content" {
|
||||
return fmt.Sprintf("/hugo/draft/%s/%s/", base64.RawURLEncoding.EncodeToString([]byte(t.docref.name)), strings.Replace(name, " ", "-", -1))
|
||||
return fmt.Sprintf("/hugo/draft/%s/%s/", base64.RawURLEncoding.EncodeToString([]byte(t.docref.docname)), strings.Replace(name, " ", "-", -1))
|
||||
}
|
||||
return fmt.Sprintf("/hugo/draft/%s/%s/%s/", base64.RawURLEncoding.EncodeToString([]byte(t.docref.name)), ctype, strings.Replace(name, " ", "-", -1))
|
||||
return fmt.Sprintf("/hugo/draft/%s/%s/%s/", base64.RawURLEncoding.EncodeToString([]byte(t.docref.docname)), ctype, strings.Replace(name, " ", "-", -1))
|
||||
}
|
||||
|
||||
var EditPage = `<html>
|
||||
@@ -218,12 +218,17 @@ a {
|
||||
|
||||
const sawChangesBumpsTo = 10;
|
||||
|
||||
var sentinelSrc = 'about:blank';
|
||||
var oldSrc = '';
|
||||
|
||||
var sawChanges = -1;
|
||||
window.setInterval(function () {
|
||||
if (sawChanges >= 0) {
|
||||
sawChanges--;
|
||||
if (sawChanges == 0) {
|
||||
iframe.contentWindow.location.reload();
|
||||
if (iframe.contentWindow) {
|
||||
iframe.contentWindow.location.reload();
|
||||
}
|
||||
}
|
||||
}
|
||||
uiBindings.now = moment();
|
||||
|
||||
Reference in New Issue
Block a user