Former-commit-id: fbbf79d757
This commit is contained in:
Henrique Dias 2017-01-15 11:47:52 +00:00
parent 8e0b3b17df
commit ee279c4233
5 changed files with 94 additions and 55 deletions

View File

@ -266,7 +266,7 @@ function openEvent(event) {
if(selectedItems.length) { if(selectedItems.length) {
link = document.getElementById(selectedItems[0]).dataset.url + link; link = document.getElementById(selectedItems[0]).dataset.url + link;
} else { } else {
link = window.location + link; link = window.location.pathname + link;
} }
window.open(link); window.open(link);

View File

@ -22,6 +22,17 @@ editor.textareaAutoGrow = function () {
window.addEventListener('resize', addAutoGrow) window.addEventListener('resize', addAutoGrow)
} }
editor.toggleSourceEditor = function (event) {
event.preventDefault();
if(document.querySelector('[data-kind="content-only"]')) {
window.location = window.location.pathname + "?visual=true"
return;
}
window.location = window.location.pathname + "?visual=false"
}
function deleteFrontMatterItem(event) { function deleteFrontMatterItem(event) {
event.preventDefault(); event.preventDefault();
document.getElementById(this.dataset.delete).remove(); document.getElementById(this.dataset.delete).remove();
@ -163,11 +174,18 @@ document.addEventListener("DOMContentLoaded", (event) => {
if(!document.getElementById('editor')) return; if(!document.getElementById('editor')) return;
editor.textareaAutoGrow(); editor.textareaAutoGrow();
templates.arrayItem = document.getElementById("array-item-template"); templates.arrayItem = document.getElementById("array-item-template");
templates.base = document.getElementById('base-template'); templates.base = document.getElementById('base-template');
templates.objectItem = document.getElementById("object-item-template"); templates.objectItem = document.getElementById("object-item-template");
templates.temporary = document.getElementById('temporary-template'); templates.temporary = document.getElementById('temporary-template');
buttons.save = document.querySelector('#save'); buttons.save = document.querySelector('#save');
buttons.editSource = document.querySelector('#edit-source');
if(buttons.editSource) {
buttons.editSource.addEventListener('click', editor.toggleSourceEditor)
}
let container = document.getElementById('editor'), let container = document.getElementById('editor'),
kind = container.dataset.kind; kind = container.dataset.kind;
@ -255,4 +273,4 @@ document.addEventListener("DOMContentLoaded", (event) => {
}); });
return false; return false;
}); });

View File

@ -67,9 +67,16 @@
<div class="actions{{ if .IsDir }} disabled{{ end }}" id="file-only"> <div class="actions{{ if .IsDir }} disabled{{ end }}" id="file-only">
{{- if and (not .IsDir) (.User.AllowEdit) }} {{- if and (not .IsDir) (.User.AllowEdit) }}
{{- if .Editor}} {{- if .Editor}}
{{- if eq .Data.Mode "markdown" }} {{- if eq .Data.Mode "markdown" }}
<div class="action" id="preview" onclick="notImplemented(event);"> <div class="action" id="preview" onclick="notImplemented(event);">
<i class="material-icons">remove_red_eye</i> <i class="material-icons" title="Preview">remove_red_eye</i>
</div>
{{- end }}
{{- if eq .Data.Visual true }}
<div class="action" id="edit-source">
<i class="material-icons" title="Toggle edit source">code</i>
</div> </div>
{{- end }} {{- end }}
{{- end }} {{- end }}

View File

@ -2,7 +2,8 @@ package handlers
import ( import (
"bytes" "bytes"
"fmt" "errors"
"net/http"
"path/filepath" "path/filepath"
"strings" "strings"
@ -15,78 +16,91 @@ import (
type Editor struct { type Editor struct {
Class string Class string
Mode string Mode string
Visual bool
Content string Content string
FrontMatter *frontmatter.Content FrontMatter *frontmatter.Content
} }
// GetEditor gets the editor based on a FileInfo struct // GetEditor gets the editor based on a FileInfo struct
func GetEditor(i *file.Info) (*Editor, error) { func GetEditor(r *http.Request, i *file.Info) (*Editor, error) {
// Create a new editor variable and set the mode
editor := new(Editor)
editor.Mode = strings.TrimPrefix(filepath.Ext(i.Name), ".")
switch editor.Mode {
case "md", "markdown", "mdown", "mmark":
editor.Mode = "markdown"
case "asciidoc", "adoc", "ad":
editor.Mode = "asciidoc"
case "rst":
editor.Mode = "rst"
case "html", "htm":
editor.Mode = "html"
case "js":
editor.Mode = "javascript"
case "go":
editor.Mode = "golang"
}
var page parser.Page
var err error var err error
// Handle the content depending on the file extension // Create a new editor variable and set the mode
switch editor.Mode { editor := new(Editor)
case "json", "toml", "yaml": editor.Mode = editorMode(i.Name)
// Defines the class and declares an error editor.Class = editorClass(editor.Mode)
editor.Class = "frontmatter-only"
if editor.Class == "frontmatter-only" || editor.Class == "complete" {
editor.Visual = true
}
if r.URL.Query().Get("visual") == "false" {
editor.Class = "content-only"
}
if editor.Class == "frontmatter-only" {
// Checks if the file already has the frontmatter rune and parses it // Checks if the file already has the frontmatter rune and parses it
if frontmatter.HasRune(i.Content) { if frontmatter.HasRune(i.Content) {
editor.FrontMatter, _, err = frontmatter.Pretty(i.Content) editor.FrontMatter, _, err = frontmatter.Pretty(i.Content)
} else { } else {
editor.FrontMatter, _, err = frontmatter.Pretty(frontmatter.AppendRune(i.Content, editor.Mode)) editor.FrontMatter, _, err = frontmatter.Pretty(frontmatter.AppendRune(i.Content, editor.Mode))
} }
}
if editor.Class == "complete" && frontmatter.HasRune(i.Content) {
var page parser.Page
// Starts a new buffer and parses the file using Hugo's functions
buffer := bytes.NewBuffer(i.Content)
page, err = parser.ReadFrom(buffer)
editor.Class = "complete"
// Check if there were any errors
if err == nil { if err == nil {
break // Parses the page content and the frontmatter
editor.Content = strings.TrimSpace(string(page.Content()))
editor.FrontMatter, _, err = frontmatter.Pretty(page.FrontMatter())
} }
}
fmt.Println("Hey") if editor.Class == "complete" && !frontmatter.HasRune(i.Content) {
err = errors.New("Complete but without rune")
}
fallthrough if editor.Class == "content-only" || err != nil {
case "markdown", "asciidoc", "rst":
if frontmatter.HasRune(i.Content) {
// Starts a new buffer and parses the file using Hugo's functions
buffer := bytes.NewBuffer(i.Content)
page, err = parser.ReadFrom(buffer)
editor.Class = "complete"
if err == nil {
// Parses the page content and the frontmatter
editor.Content = strings.TrimSpace(string(page.Content()))
editor.FrontMatter, _, err = frontmatter.Pretty(page.FrontMatter())
if err == nil {
break
}
}
}
fallthrough
default:
editor.Class = "content-only" editor.Class = "content-only"
editor.Content = i.StringifyContent() editor.Content = i.StringifyContent()
} }
return editor, nil return editor, nil
} }
func editorClass(mode string) string {
switch mode {
case "json", "toml", "yaml":
return "frontmatter-only"
case "markdown", "asciidoc", "rst":
return "complete"
}
return "content-only"
}
func editorMode(filename string) string {
mode := strings.TrimPrefix(filepath.Ext(filename), ".")
switch mode {
case "md", "markdown", "mdown", "mmark":
mode = "markdown"
case "asciidoc", "adoc", "ad":
mode = "asciidoc"
case "rst":
mode = "rst"
case "html", "htm":
mode = "html"
case "js":
mode = "javascript"
case "go":
mode = "golang"
}
return mode
}

View File

@ -36,7 +36,7 @@ func ServeSingle(w http.ResponseWriter, r *http.Request, c *config.Config, u *co
} }
if i.CanBeEdited() && u.AllowEdit { if i.CanBeEdited() && u.AllowEdit {
p.Data, err = GetEditor(i) p.Data, err = GetEditor(r, i)
p.Editor = true p.Editor = true
if err != nil { if err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err