oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "html/template"
5 "slices"
6
7 "amadan.net/oficina/mapa/internal/mapa/icons"
8)
9
10// The menu bar (level-up spec §3.4): File, Edit, Insert, View and Help.
11// File is first, as in Docs and Slides (build note 87).
12// Each menu is a <details> (Docs' reason: it opens with no script), and
13// an entry that does not apply to the screen is not drawn.
14
15// menuBar is templates/frame.html's mapa-menubar data.
16type menuBar struct {
17 Label string
18 Menus []menuGroup
19}
20
21// menuGroup is one menu: its id, title and entries.
22type menuGroup struct {
23 ID, Title string
24 Items []menuEntry
25}
26
27// menuEntry is one row. Kind is "link", "form" (a POST with Fields),
28// "command" (a button mapa.js binds; disabled until it does), "dialog"
29// (opens the <dialog> with id Dialog) or "divider".
30type menuEntry struct {
31 Kind, Label, Href, Action, Command, Dialog, Key string
32 Fields map[string]string
33 Danger bool
34 // Here is a verb for the page on screen only, which a sidebar row's
35 // menu leaves out (build note 88).
36 Here bool
37}
38
39func link(label, href string) menuEntry {
40 return menuEntry{Kind: "link", Label: label, Href: href}
41}
42
43func command(label, name, key string) menuEntry {
44 return menuEntry{Kind: "command", Label: label, Command: name, Key: key}
45}
46
47var divider = menuEntry{Kind: "divider"}
48
49// menusFor is the bar's menus for one screen. importer is whether the
50// viewer is in MAPA_IMPORTERS: to anyone else the import is not there
51// (Notion import spec §7).
52func menusFor(page *view, importer bool) menuBar {
53 groups := []menuGroup{{ID: "file", Title: "File",
54 Items: fileMenu(page.Chrome, page.Frame.Side.pinned(), importer)}}
55 if page.Chrome != nil && (page.Chrome.Editor || page.Chrome.Writable) {
56 groups = append(groups, menuGroup{ID: "edit", Title: "Edit",
57 Items: editMenu()}, menuGroup{ID: "insert", Title: "Insert",
58 Items: insertMenu(page.Chrome.Writable)})
59 }
60 groups = append(groups, menuGroup{ID: "view", Title: "View",
61 Items: viewMenu(page)}, menuGroup{ID: "help", Title: "Help",
62 Items: helpMenu()})
63 return menuBar{Label: "Mapa", Menus: groups}
64}
65
66// fileMenu is ordered the Mac way (build note 87): New first, then the
67// import, then what this page allows, then the lists, and Delete… last,
68// set apart, as in Docs.
69func fileMenu(page *pageChrome, pinned, importer bool) []menuEntry {
70 items := []menuEntry{link("New team page", "/new"),
71 link("New scratch page", "/scratch/new")}
72 if importer {
73 items = append(items, divider,
74 link("Import from Notion…", importPath))
75 }
76 var remove []menuEntry
77 if page != nil && page.Page != nil && page.Page.ID != 0 {
78 verbs, deleteEntry := pageVerbs(*page.Page, pinned)
79 items = append(items, divider)
80 items = append(items, verbs...)
81 remove = []menuEntry{divider, deleteEntry}
82 }
83 items = append(items, divider, link("Deleted pages", "/deleted"))
84 return append(items, remove...)
85}
86
87// pageVerbs is what a page allows, Pin or Unpin among them, and its
88// Delete…, which each menu sets apart. It is the one source for the
89// bar's File menu and a sidebar row's ⋯ (build note 88), so the two
90// cannot drift.
91func pageVerbs(page PageView, pinned bool) ([]menuEntry, menuEntry) {
92 base := "/p/" + page.Public
93 var items []menuEntry
94 if page.Kind == KindPage {
95 items = append(items, link("Rename…", base+"/rename"),
96 menuEntry{Kind: "link", Label: "Icon and banner…",
97 Href: base + "/look", Here: true},
98 menuEntry{Kind: "link", Label: "Link to a block…",
99 Href: base + "/blocks", Here: true})
100 }
101 if page.Rung == RungPrivate {
102 items = append(items, link("Make team page…", base+"/share"))
103 }
104 return append(items, link("History", base+"/history"),
105 pinEntry(&page, pinned)),
106 menuEntry{Kind: "link", Label: "Delete…", Href: base + "/delete",
107 Danger: true}
108}
109
110// rowMenu is a sidebar row's ⋯ (build note 88): pageVerbs, less what
111// needs the page on screen, then Delete… set apart. Pin and Unpin come
112// back to back, the screen the row was on, not the page it names.
113func rowMenu(page PageView, pinned bool, back string) []menuEntry {
114 verbs, deleteEntry := pageVerbs(page, pinned)
115 var items []menuEntry
116 for _, entry := range verbs {
117 if entry.Here {
118 continue
119 }
120 if entry.Kind == "form" {
121 entry.Fields["return"] = back
122 }
123 items = append(items, entry)
124 }
125 return append(items, divider, deleteEntry)
126}
127
128// editMenu acts on the textarea; mapa.js binds each command.
129func editMenu() []menuEntry {
130 return []menuEntry{command("Undo", "undo", "⌘Z"),
131 command("Redo", "redo", "⇧⌘Z"), divider,
132 command("Select all", "select-all", "⌘A")}
133}
134
135// insertMenu puts Markdown in at the caret, its shortcut shown, so the
136// menu quietly teaches that the page is Markdown (§3.4, §6.6). Link to
137// a page shows ⌘L, its key: over a selection it links the words, and
138// with none it types [[ (build note 81). Today's date goes in at once;
139// beside it, where the page editor runs, Date… opens its date picker
140// (build note 83), and Image… opens the file picker (images §4.1).
141// The Markdown screen has neither: its form has Upload and insert.
142func insertMenu(picker bool) []menuEntry {
143 items := []menuEntry{
144 command("Heading 1", "insert-h1", "#"),
145 command("Heading 2", "insert-h2", "##"),
146 command("Heading 3", "insert-h3", "###"), divider,
147 command("List", "insert-list", "-"),
148 command("Numbered list", "insert-numbered", "1."),
149 command("Checklist", "insert-check", "- [ ]"), divider,
150 command("Quote", "insert-quote", ">"),
151 command("Code", "insert-code", "```"),
152 command("Divider", "insert-rule", "---"), divider,
153 command("Link to a page", "insert-link", "⌘L"),
154 command("Today's date", "insert-date", "[[date]]"),
155 }
156 if picker {
157 items = append(items, command("Date…", "insert-date-pick", ""),
158 command("Image…", "insert-image", ""))
159 // The layouts go after Divider, the blocks' group; dates and
160 // images stay last.
161 at := slices.IndexFunc(items, func(entry menuEntry) bool {
162 return entry.Label == "Link to a page"
163 })
164 items = slices.Insert(items, at-1, columnEntries()...)
165 }
166 return items
167}
168
169// viewMenu folds the sidebar and toggles the panel. Each is a POST that
170// sets its cookie, so it works with scripts off; mapa.js does both in
171// place.
172func viewMenu(page *view) []menuEntry {
173 back := page.Return
174 items := []menuEntry{{Kind: "form", Label: "Hide sidebar",
175 Action: "/view/sidebar", Command: "toggle-sidebar", Key: "⌘\\",
176 Fields: map[string]string{"return": back}}}
177 if page.Chrome != nil && page.Chrome.Links {
178 items = append(items, menuEntry{Kind: "form", Label: "Links panel",
179 Action: "/view/links", Command: "toggle-links",
180 Fields: map[string]string{"return": back}})
181 }
182 if href := markdownHref(page.Chrome); href != "" {
183 items = append(items, divider, link("Edit as Markdown", href))
184 }
185 return items
186}
187
188// markdownHref is the textarea screen for the page on show, if any.
189func markdownHref(page *pageChrome) string {
190 if page == nil || page.Page == nil || page.Editor {
191 return ""
192 }
193 if page.Page.Kind == KindDaily {
194 return "/d/" + page.Page.Date + "/edit"
195 }
196 if page.Page.ID == 0 && page.Writable {
197 return newScratchMarkdown
198 }
199 if page.Page.ID == 0 {
200 return ""
201 }
202 return "/p/" + page.Page.Public + "/edit"
203}
204
205func helpMenu() []menuEntry {
206 return []menuEntry{
207 {Kind: "dialog", Label: "Keyboard shortcuts", Dialog: "mapa-keys"},
208 link("Markdown in Mapa", "/help/markdown"),
209 link(colophonTitle, colophonPath),
210 }
211}
212
213// iconHTML is one of Mapa's icons for a bar action (estilo draws it
214// before the label, decoratively).
215func iconHTML(slug string) template.HTML { return icons.Icon(slug) }
216
217// pinEntry is Pin or Unpin, a POST (the busy rule holds it 650ms).
218func pinEntry(page *PageView, pinned bool) menuEntry {
219 entry := menuEntry{Kind: "form", Label: "Pin to sidebar",
220 Action: "/p/" + page.Public + "/pin",
221 Fields: map[string]string{"pin": "1"}}
222 if pinned {
223 entry.Label, entry.Fields["pin"] = "Unpin from sidebar", "0"
224 }
225 return entry
226}
227
228// columnEntries are Insert's column layout entries (columns and icons
229// spec §1.7), the page editor's only: two to four columns, and, with the
230// caret in a layout, adding, removing and undoing columns. Outside a
231// layout those three do nothing.
232func columnEntries() []menuEntry {
233 return []menuEntry{divider,
234 command("2 columns", "insert-columns-2", ""),
235 command("3 columns", "insert-columns-3", ""),
236 command("4 columns", "insert-columns-4", ""),
237 command("Add a column", "add-column", ""),
238 command("Remove this column", "remove-column", ""),
239 command("Undo columns", "undo-columns", "")}
240}
241