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