add i18n for system/home nav editor, improve link manager
This commit is contained in:
1 parent
e8e9b505af
commit
2b4e1dbc57
9 files changed
+517
-52
No files matched your search
@@ -61,6 +61,12 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = st.EnsurePathLinkColumns()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
srv := server.New(st)
|
||||
|
||||
err = srv.Run(":3000")
|
||||
|
||||
+56
-32
@@ -6,40 +6,64 @@ import (
|
||||
|
||||
var translations = map[string]map[string]string{
|
||||
"en": {
|
||||
"config.title": "Config",
|
||||
"config.home": "Home",
|
||||
"config.wiki_name": "Wiki name",
|
||||
"config.locale": "Language",
|
||||
"config.home_path": "Home semantic path",
|
||||
"config.save": "Save",
|
||||
"config.nav": "Navigation",
|
||||
"config.objects": "Recent Objects",
|
||||
"config.primary": "Primary Link",
|
||||
"config.size": "Size",
|
||||
"config.updated": "Updated",
|
||||
"config.object": "Object",
|
||||
"config.nav_label": "Label",
|
||||
"config.nav_href": "Href",
|
||||
"config.nav_order": "Order",
|
||||
"config.nav_save": "Save Navigation",
|
||||
"config.title": "Config",
|
||||
"config.home": "Home",
|
||||
"config.wiki_name": "Wiki name",
|
||||
"config.locale": "Language",
|
||||
"config.home_path": "Home semantic path",
|
||||
"config.save": "Save",
|
||||
"config.nav": "Navigation",
|
||||
"config.objects": "Recent Objects",
|
||||
"config.primary": "Primary Link",
|
||||
"config.size": "Size",
|
||||
"config.updated": "Updated",
|
||||
"config.object": "Object",
|
||||
"config.nav_label": "Label",
|
||||
"config.nav_href": "Href",
|
||||
"config.nav_order": "Order",
|
||||
"config.nav_save": "Save Navigation",
|
||||
"config.nav_home": "Home Navigation",
|
||||
"config.nav_system": "System Navigation",
|
||||
"link.title": "Link Manager",
|
||||
"link.object": "Object",
|
||||
"link.primary": "Primary",
|
||||
"link.path": "Path",
|
||||
"link.linked_at": "Linked At",
|
||||
"link.action": "Action",
|
||||
"link.delete": "Delete",
|
||||
"link.add": "Add",
|
||||
"link.add_path": "Add Link",
|
||||
"link.placeholder": "/pageswiki/example",
|
||||
},
|
||||
"ja": {
|
||||
"config.title": "設定",
|
||||
"config.home": "ホーム",
|
||||
"config.wiki_name": "Wiki名称",
|
||||
"config.locale": "言語",
|
||||
"config.home_path": "トップページ",
|
||||
"config.save": "保存",
|
||||
"config.nav": "ナビゲーション",
|
||||
"config.objects": "最近のオブジェクト",
|
||||
"config.primary": "主なリンク",
|
||||
"config.size": "サイズ",
|
||||
"config.updated": "最終更新",
|
||||
"config.object": "オブジェクト",
|
||||
"config.nav_label": "表示名",
|
||||
"config.nav_href": "リンク先",
|
||||
"config.nav_order": "順序",
|
||||
"config.nav_save": "ナビゲーションを保存",
|
||||
"config.title": "設定",
|
||||
"config.home": "ホーム",
|
||||
"config.wiki_name": "Wiki名称",
|
||||
"config.locale": "言語",
|
||||
"config.home_path": "トップページ",
|
||||
"config.save": "保存",
|
||||
"config.nav": "ナビゲーション",
|
||||
"config.nav_home": "ホームナビゲーション",
|
||||
"config.nav_system": "システムナビゲーション",
|
||||
"config.objects": "最近のオブジェクト",
|
||||
"link.title": "リンク管理",
|
||||
"link.object": "オブジェクト",
|
||||
"link.primary": "主リンク",
|
||||
"link.path": "パス",
|
||||
"link.linked_at": "リンクされた日時",
|
||||
"link.action": "操作",
|
||||
"link.delete": "削除",
|
||||
"link.add": "追加",
|
||||
"link.add_path": "リンクを追加",
|
||||
"link.placeholder": "/pageswiki/example",
|
||||
"config.primary": "主なリンク",
|
||||
"config.size": "サイズ",
|
||||
"config.updated": "最終更新",
|
||||
"config.object": "オブジェクト",
|
||||
"config.nav_label": "表示名",
|
||||
"config.nav_href": "リンク先",
|
||||
"config.nav_order": "順序",
|
||||
"config.nav_save": "ナビゲーションを保存",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Server) handleLinkManager(
|
||||
@@ -9,14 +10,123 @@ func (s *Server) handleLinkManager(
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
objects, _ := s.store.ListObjects()
|
||||
objectUUID := ""
|
||||
|
||||
if strings.HasPrefix(r.URL.Path, "/l/uuid:") {
|
||||
|
||||
objectUUID =
|
||||
strings.TrimPrefix(
|
||||
r.URL.Path,
|
||||
"/l/uuid:",
|
||||
)
|
||||
}
|
||||
|
||||
if objectUUID == "" {
|
||||
|
||||
http.NotFound(
|
||||
w,
|
||||
r,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method == http.MethodPost {
|
||||
|
||||
err := r.ParseForm()
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
|
||||
action :=
|
||||
r.FormValue(
|
||||
"action",
|
||||
)
|
||||
|
||||
path :=
|
||||
strings.TrimSpace(
|
||||
r.FormValue("path"),
|
||||
)
|
||||
|
||||
switch action {
|
||||
|
||||
case "add":
|
||||
|
||||
if path == "" {
|
||||
http.Error(w, "path is required", 400)
|
||||
return
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
path = "/" + path
|
||||
}
|
||||
|
||||
err = s.store.AddPathLink(
|
||||
objectUUID,
|
||||
path,
|
||||
)
|
||||
|
||||
case "delete":
|
||||
|
||||
err = s.store.DeletePathLink(
|
||||
objectUUID,
|
||||
path,
|
||||
)
|
||||
|
||||
case "primary":
|
||||
|
||||
err = s.store.SetPrimaryPathLink(
|
||||
objectUUID,
|
||||
path,
|
||||
)
|
||||
|
||||
default:
|
||||
|
||||
http.Error(w, "unknown action", 400)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(
|
||||
w,
|
||||
r,
|
||||
"/l/uuid:"+objectUUID,
|
||||
http.StatusFound,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
links, err :=
|
||||
s.store.ListPathLinks(
|
||||
objectUUID,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
s.render(
|
||||
w,
|
||||
"links.gohtml",
|
||||
"link_manager.gohtml",
|
||||
map[string]any{
|
||||
"Title": "Link Manager",
|
||||
"Objects": objects,
|
||||
"NavItems": resolveNavItems(
|
||||
s.getSystemNavItems(),
|
||||
NavContext{
|
||||
ObjectUUID: objectUUID,
|
||||
ObjectURI: "uuid:" + objectUUID,
|
||||
},
|
||||
),
|
||||
"ObjectUUID": objectUUID,
|
||||
"ObjectURI": "uuid:" + objectUUID,
|
||||
"Links": links,
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -58,9 +58,8 @@ func RegisterUIRoutes(
|
||||
"/raw/",
|
||||
h.RawView,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/l",
|
||||
"/l/",
|
||||
h.LinkManager,
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
package store
|
||||
|
||||
type PathLink struct {
|
||||
Path string
|
||||
CreatedAt int64
|
||||
IsPrimary bool
|
||||
}
|
||||
|
||||
func (s *Store) EnsurePathLinkColumns() error {
|
||||
|
||||
_, _ = s.DB.Exec(`
|
||||
ALTER TABLE paths
|
||||
ADD COLUMN is_primary INTEGER NOT NULL DEFAULT 0
|
||||
`)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) ListPathLinks(
|
||||
objectUUID string,
|
||||
) ([]PathLink, error) {
|
||||
|
||||
rows, err := s.DB.Query(`
|
||||
SELECT
|
||||
path,
|
||||
created_at,
|
||||
COALESCE(is_primary, 0)
|
||||
FROM paths
|
||||
WHERE object_uuid = ?
|
||||
ORDER BY
|
||||
is_primary DESC,
|
||||
path ASC
|
||||
`, objectUUID)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
var out []PathLink
|
||||
|
||||
for rows.Next() {
|
||||
|
||||
var p PathLink
|
||||
var primary int
|
||||
|
||||
err := rows.Scan(
|
||||
&p.Path,
|
||||
&p.CreatedAt,
|
||||
&primary,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p.IsPrimary = primary != 0
|
||||
|
||||
out = append(out, p)
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *Store) AddPathLink(
|
||||
objectUUID string,
|
||||
path string,
|
||||
) error {
|
||||
|
||||
_, err := s.DB.Exec(`
|
||||
INSERT INTO paths (
|
||||
path,
|
||||
object_uuid,
|
||||
created_at,
|
||||
is_primary
|
||||
)
|
||||
VALUES (
|
||||
?,
|
||||
?,
|
||||
strftime('%s','now'),
|
||||
0
|
||||
)
|
||||
`, path, objectUUID)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) DeletePathLink(
|
||||
objectUUID string,
|
||||
path string,
|
||||
) error {
|
||||
|
||||
_, err := s.DB.Exec(`
|
||||
DELETE FROM paths
|
||||
WHERE object_uuid = ?
|
||||
AND path = ?
|
||||
`, objectUUID, path)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) SetPrimaryPathLink(
|
||||
objectUUID string,
|
||||
path string,
|
||||
) error {
|
||||
|
||||
tx, err := s.DB.Begin()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = tx.Exec(`
|
||||
UPDATE paths
|
||||
SET is_primary = 0
|
||||
WHERE object_uuid = ?
|
||||
`, objectUUID)
|
||||
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = tx.Exec(`
|
||||
UPDATE paths
|
||||
SET is_primary = 1
|
||||
WHERE object_uuid = ?
|
||||
AND path = ?
|
||||
`, objectUUID, path)
|
||||
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
@@ -27,30 +27,34 @@ func (s *Store) ListRecentObjects(
|
||||
) AS rn
|
||||
FROM revisions r
|
||||
),
|
||||
primary_path AS (
|
||||
ranked_path AS (
|
||||
SELECT
|
||||
p.object_uuid,
|
||||
MIN(p.path) AS path
|
||||
p.path,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY p.object_uuid
|
||||
ORDER BY
|
||||
COALESCE(p.is_primary, 0) DESC,
|
||||
p.path ASC
|
||||
) AS rn
|
||||
FROM paths p
|
||||
GROUP BY p.object_uuid
|
||||
)
|
||||
SELECT
|
||||
o.uuid,
|
||||
COALESCE(primary_path.path, ''),
|
||||
COALESCE(ranked_path.path, ''),
|
||||
COALESCE(LENGTH(latest.body), 0),
|
||||
COALESCE(latest.created_at, o.created_at)
|
||||
FROM objects o
|
||||
LEFT JOIN latest
|
||||
ON latest.object_uuid = o.uuid
|
||||
AND latest.rn = 1
|
||||
LEFT JOIN primary_path
|
||||
ON primary_path.object_uuid = o.uuid
|
||||
LEFT JOIN ranked_path
|
||||
ON ranked_path.object_uuid = o.uuid
|
||||
AND ranked_path.rn = 1
|
||||
ORDER BY
|
||||
COALESCE(latest.created_at, o.created_at) DESC
|
||||
LIMIT ?
|
||||
`,
|
||||
limit,
|
||||
)
|
||||
`, limit)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -50,11 +50,11 @@
|
||||
</a>
|
||||
|
||||
<a href="#" data-panel="nav-home" onclick="showConfigPanel('nav-home'); return false;">
|
||||
Home Navigation
|
||||
{{ T "config.nav_home" }}
|
||||
</a>
|
||||
|
||||
<a href="#" data-panel="nav-system" onclick="showConfigPanel('nav-system'); return false;">
|
||||
System Navigation
|
||||
{{ T "config.nav_system" }}
|
||||
</a>
|
||||
|
||||
</nav>
|
||||
@@ -196,7 +196,7 @@ Save
|
||||
class="config-section config-panel"
|
||||
>
|
||||
|
||||
<h2>Home Navigation</h2>
|
||||
<h2>{{ T "config.nav_home" }}</h2>
|
||||
|
||||
<form
|
||||
method="post"
|
||||
@@ -288,7 +288,7 @@ Save
|
||||
class="config-section config-panel"
|
||||
>
|
||||
|
||||
<h2>System Navigation</h2>
|
||||
<h2>{{ T "config.nav_system" }}</h2>
|
||||
|
||||
<form
|
||||
method="post"
|
||||
|
||||
@@ -42,7 +42,7 @@ View
|
||||
History
|
||||
</a>
|
||||
|
||||
<a href="/l?object_uuid={{ .ObjectUUID }}">
|
||||
<a href="/l/uuid:{{ .ObjectUUID }}">
|
||||
Links
|
||||
</a>
|
||||
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
{{ define "link_manager.gohtml" }}
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>{{ T "link.title" }}</title>
|
||||
<link rel="stylesheet" href="/o/path:/system/static/css/main.css">
|
||||
<link rel="stylesheet" href="/o/path:/system/static/css/modes.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<header class="topbar">
|
||||
|
||||
<div class="brand">
|
||||
<a href="/">{{ .WikiName }}</a>
|
||||
</div>
|
||||
|
||||
<nav>
|
||||
{{ range .NavItems }}
|
||||
<a href="{{ .ResolvedHref }}">{{ .Label }}</a>
|
||||
{{ end }}
|
||||
</nav>
|
||||
|
||||
</header>
|
||||
|
||||
<main class="container">
|
||||
|
||||
<section>
|
||||
|
||||
<h1>{{ T "link.title" }}</h1>
|
||||
|
||||
<p class="muted">
|
||||
{{ T "link.object" }}:
|
||||
<code>{{ .ObjectURI }}</code>
|
||||
</p>
|
||||
|
||||
<table class="link-table">
|
||||
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{{ T "link.primary" }}</th>
|
||||
<th>{{ T "link.path" }}</th>
|
||||
<th>{{ T "link.linked_at" }}</th>
|
||||
<th>{{ T "link.action" }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
|
||||
{{ range .Links }}
|
||||
|
||||
<tr>
|
||||
|
||||
<td>
|
||||
<form method="post" action="/l/uuid:{{ $.ObjectUUID }}">
|
||||
<input type="hidden" name="action" value="primary">
|
||||
<input type="hidden" name="path" value="{{ .Path }}">
|
||||
<input
|
||||
type="radio"
|
||||
name="primary_display"
|
||||
{{ if .IsPrimary }}checked{{ end }}
|
||||
onchange="this.form.submit()"
|
||||
>
|
||||
</form>
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<a href="{{ .Path }}">
|
||||
{{ .Path }}
|
||||
</a>
|
||||
</td>
|
||||
|
||||
<td class="mono">
|
||||
{{ formatUnix .CreatedAt }}
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<form
|
||||
method="post"
|
||||
action="/l/uuid:{{ $.ObjectUUID }}"
|
||||
onsubmit="return confirm('Delete link?\n\n{{ .Path }}')"
|
||||
>
|
||||
<input type="hidden" name="action" value="delete">
|
||||
<input type="hidden" name="path" value="{{ .Path }}">
|
||||
<button type="submit">
|
||||
Delete
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
|
||||
</tr>
|
||||
|
||||
{{ end }}
|
||||
|
||||
</tbody>
|
||||
|
||||
</table>
|
||||
|
||||
<h2>{{ T "link.add_path" }}</h2>
|
||||
|
||||
<form
|
||||
method="post"
|
||||
action="/l/uuid:{{ .ObjectUUID }}"
|
||||
class="add-link-form"
|
||||
>
|
||||
|
||||
<input type="hidden" name="action" value="add">
|
||||
|
||||
<input
|
||||
name="path"
|
||||
type="text"
|
||||
placeholder="{{ T "link.placeholder" }}"
|
||||
>
|
||||
|
||||
<button type="submit">
|
||||
Add
|
||||
</button>
|
||||
|
||||
</form>
|
||||
|
||||
</section>
|
||||
|
||||
</main>
|
||||
|
||||
<style>
|
||||
|
||||
.link-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
.link-table th {
|
||||
text-align: left;
|
||||
font-weight: normal;
|
||||
color: #888;
|
||||
border-bottom: 1px solid rgba(255,255,255,0.08);
|
||||
padding: 0.45rem 0.35rem;
|
||||
}
|
||||
|
||||
.link-table td {
|
||||
border-bottom: 1px solid rgba(255,255,255,0.035);
|
||||
padding: 0.38rem 0.35rem;
|
||||
}
|
||||
|
||||
.link-table tr:hover {
|
||||
background: rgba(255,255,255,0.025);
|
||||
}
|
||||
|
||||
.add-link-form {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
margin-top: 0.75rem;
|
||||
}
|
||||
|
||||
.add-link-form input[type="text"] {
|
||||
flex: 1;
|
||||
background: #0b0b0b;
|
||||
color: #eee;
|
||||
border: 1px solid #222;
|
||||
padding: 0.6rem;
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
.mono {
|
||||
font-family:
|
||||
"JetBrains Mono",
|
||||
monospace;
|
||||
}
|
||||
|
||||
.muted {
|
||||
color: #777;
|
||||
}
|
||||
|
||||
button {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
</style>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
{{ end }}
|
||||
Reference in new issue
Block a user