feat(editor): add lossless bounded CSV table editing
This commit is contained in:
1 parent
bd80ff6a0c
commit
2d6ceac0d7
19 files changed
+520
-40
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"pageswiki/internal/grammaratur"
|
||||
)
|
||||
@@ -73,7 +74,20 @@ func (s *Server) handleEditorState(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
out := map[string]any{"revision_uuid": rev.UUID, "head_revision_uuid": head, "lang": rev.Language, "content_type": rev.ContentType, "filename": rev.Filename, "principal_uuid": owner}
|
||||
if r.URL.Query().Get("metadata") != "1" {
|
||||
if rev.UUID != "" {
|
||||
csv := selectProjectionAdapter(contentDescriptor{MediaType: strings.ToLower(strings.TrimSpace(strings.Split(rev.ContentType, ";")[0])), Filename: rev.Filename}).Kind == "csv"
|
||||
if csv {
|
||||
size, e := s.store.DocumentSizeContext(r.Context(), rev.DocumentHash)
|
||||
if e != nil || size > maxCSVMacroBytes {
|
||||
http.Error(w, "CSV editor size limit exceeded", 413)
|
||||
return
|
||||
}
|
||||
window, e := s.store.ReadDocumentWindowContext(r.Context(), rev.DocumentHash, 0, maxCSVMacroBytes)
|
||||
if e != nil || window.Truncated || !utf8.Valid(window.Data) || strings.ContainsRune(string(window.Data), 0) {
|
||||
http.Error(w, "CSV source unavailable or not UTF-8", 415)
|
||||
return
|
||||
}
|
||||
rev.Body = string(window.Data)
|
||||
} else if rev.UUID != "" {
|
||||
rev, err = s.store.GetRevision(rev.UUID)
|
||||
}
|
||||
if err != nil {
|
||||
@@ -90,7 +104,7 @@ func (s *Server) handleEditorState(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
out["body"], out["markdown"] = rev.Body, markdown
|
||||
kind := revisionContentView(rev, "").Kind
|
||||
out["editable"] = rev.UUID == "" || kind == "html" || kind == "code"
|
||||
out["editable"] = rev.UUID == "" || kind == "html" || kind == "code" || (csv && utf8.ValidString(rev.Body) && !strings.ContainsRune(rev.Body, 0))
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(out)
|
||||
}
|
||||
Reference in new issue
Block a user