1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-04 18:19:54 +00:00

Editor: Fix handling empty input

This commit is contained in:
Timur Ismagilov 2022-06-11 12:36:45 +03:00
parent 9713c18b6b
commit 7a98d29c74
2 changed files with 10 additions and 18 deletions

View File

@ -1,7 +1,6 @@
package hyphae
import (
"errors"
"os"
)
@ -11,7 +10,7 @@ import (
func FetchMycomarkupFile(h Hypha) (string, error) {
switch h := h.(type) {
case *EmptyHypha:
return "", errors.New("empty hyphae have no text")
return "", nil
case *MediaHypha:
if !h.HasTextFile() {
return "", nil

View File

@ -62,17 +62,16 @@ func UploadText(h hyphae.Hypha, data []byte, userMessage string, u *user.User) e
return errors.New("invalid hypha name")
}
oldText, err := hyphae.FetchMycomarkupFile(h)
if err != nil {
hop.Abort()
return err
}
// Empty data check
if len(bytes.TrimSpace(data)) == 0 { // if nothing but whitespace
switch h.(type) {
case *hyphae.EmptyHypha, *hyphae.MediaHypha:
// Writing no description, it's ok, just like cancel button.
hop.Abort()
return nil
case *hyphae.TextualHypha:
// What do you want passing nothing for a textual hypha?
return errors.New("No data passed")
}
if len(bytes.TrimSpace(data)) == 0 && len(oldText) == 0 { // if nothing but whitespace
hop.Abort()
return nil
}
// At this point, we have a savable user-generated Mycomarkup document. Gotta save it.
@ -90,12 +89,6 @@ func UploadText(h hyphae.Hypha, data []byte, userMessage string, u *user.User) e
hyphae.Insert(H)
backlinks.UpdateBacklinksAfterEdit(H, "")
case *hyphae.MediaHypha:
oldText, err := hyphae.FetchMycomarkupFile(h)
if err != nil {
hop.Abort()
return err
}
// TODO: that []byte(...) part should be removed
if bytes.Equal(data, []byte(oldText)) {
// No changes! Just like cancel button