| 1 | package docs |
| 2 | |
| 3 | // The menu bar's model and copy — File / Edit / View / Format / Tools, |
| 4 | // rendered by views/show.html on estilo's [estilo-menubar] and |
| 5 | // [estilo-menu] (the one menu style the suite shares) and given its |
| 6 | // keyboard behaviour by fixtures/menu.mjs. |
| 7 | // |
| 8 | // It replaces the two export links that used to be pinned to the |
| 9 | // trailing end of the formatting bar. Those were the "random buttons" |
| 10 | // of Paul's brief, and the objection was structural rather than |
| 11 | // aesthetic: export is a document-level verb sitting inside a group of |
| 12 | // run-level formatting controls, which is the one place a person will |
| 13 | // not look for it. |
| 14 | // |
| 15 | // Three properties this file exists to hold, all of them easy to lose |
| 16 | // later: |
| 17 | // |
| 18 | // 1. THE MENU WORKS WITHOUT JAVASCRIPT. Each menu is a <details>, so |
| 19 | // it opens with no script at all, and every item that can be a |
| 20 | // plain <a> or a <form> is one. That is not a nicety here: the |
| 21 | // export links worked scriptless before this change |
| 22 | // (TestShowRendersContentWithoutScript is built on the same |
| 23 | // guarantee), and moving them behind a control that only opens |
| 24 | // under JavaScript would have quietly removed the only route to a |
| 25 | // person's own document. Items that genuinely need the editor — |
| 26 | // Bold, Undo — are Kind "command" and are disabled until the |
| 27 | // bundle enables them, which is honest: without scripts there is |
| 28 | // no editor for them to act on. |
| 29 | // |
| 30 | // 2. THE MENU AND THE TOOLBAR ARE ONE COMMAND TABLE. A "command" item |
| 31 | // carries the same data-docs-command name the toolbar's buttons |
| 32 | // carry, resolved against the same table in fixtures/toolbar.mjs. |
| 33 | // TestMenuCommandsExistInTheBundle holds every name in this file to |
| 34 | // that file's source, the same way |
| 35 | // TestToolbarOptionValuesMatchTheBundle already holds the |
| 36 | // toolbar's. A menu item naming a command nobody implements is a |
| 37 | // test failure here, not a silent no-op in front of a person. |
| 38 | // |
| 39 | // 3. NO SUBMENUS. rastrillo's rst-menus has a trap where a nested |
| 40 | // <details> inside a name= exclusive group collapses its parent, |
| 41 | // and the Sheets session hit it first. The response is not to work |
| 42 | // around it but to have no second level: "Download as PDF" and |
| 43 | // "Download as Markdown" are siblings rather than children of a |
| 44 | // "Download" submenu. Fewer levels is the better menu anyway — the |
| 45 | // submenus were Google Docs' shape copied rather than chosen. |
| 46 | // |
| 47 | // Every string is a catalog key resolved through rastrillo.T at render |
| 48 | // time. A literal here is a bug (AGENTS.md). |
| 49 | |
| 50 | import ( |
| 51 | "net/http" |
| 52 | |
| 53 | estilomenu "amadan.net/oficina/estilo.git/menu" |
| 54 | "amadan.net/rastrillo/rastrillo" |
| 55 | ) |
| 56 | |
| 57 | // Menu item kinds. The kind decides what show.html renders, and each |
| 58 | // one is a different HTML element rather than a <button> styled to look |
| 59 | // like the others — which is what keeps the scriptless half working. |
| 60 | const ( |
| 61 | // menuCommand dispatches Command into the editor's command table. |
| 62 | // Rendered as a <button type="button">, disabled until the bundle |
| 63 | // enables it. |
| 64 | menuCommand = "command" |
| 65 | // menuLink is an ordinary <a href>. Works with no script. |
| 66 | menuLink = "link" |
| 67 | // menuForm is a one-button <form method="post">. Works with no |
| 68 | // script. Used for the two state-changing verbs that are not |
| 69 | // editor commands: creating a document and signing out. |
| 70 | menuForm = "form" |
| 71 | // menuDivider is a horizontal rule between groups of items. It is |
| 72 | // a real <hr> rather than a border on the next item, so it |
| 73 | // announces as a separator rather than as nothing. |
| 74 | menuDivider = "divider" |
| 75 | ) |
| 76 | |
| 77 | // menuItem is one row in one menu. |
| 78 | // |
| 79 | // Every item works in this build. An item whose feature is not built is |
| 80 | // not in the menu at all (spec §7.3: hidden, not disabled): the Insert |
| 81 | // menu, Find and replace and Show ruler used to be rendered disabled |
| 82 | // with the spec section each was waiting on, and a bar of greyed-out |
| 83 | // promises is the main thing that made the editor read as a prototype. |
| 84 | // Disabled is now only ever contextual — Redo with nothing to redo — |
| 85 | // and that is decided by the command itself in fixtures/menu.mjs. |
| 86 | type menuItem struct { |
| 87 | Kind string |
| 88 | Command string // menuCommand: the data-docs-command name |
| 89 | Href string // menuLink: the target |
| 90 | Action string // menuForm: the POST target |
| 91 | Download bool // menuLink: adds the download attribute |
| 92 | // JumpTo marks the File menu's "Open a document…": a link to the |
| 93 | // list that fixtures/jump-to.mjs turns into the ⌘K palette. |
| 94 | JumpTo bool |
| 95 | Label string |
| 96 | // Shortcut is the chord, already formatted for the request's |
| 97 | // platform by estilo's menu.Shortcut (⇧⌘Z on a Mac, Ctrl+Y |
| 98 | // elsewhere), so nothing flips after paint. The binding itself lives |
| 99 | // in the bundle's keymap. |
| 100 | Shortcut string |
| 101 | // Checkable items report a state with estilo's check column: the View |
| 102 | // menu's paged/infinite pair, the Format menu's paragraph styles and |
| 103 | // alignments. Each is a one-of group, so exactly one is pressed. |
| 104 | // |
| 105 | // Pressed is rendered server-side where the server knows the state |
| 106 | // (the view mode, from its cookie), so the menu is right before any |
| 107 | // script runs; fixtures/menu.mjs keeps every checkable item in step |
| 108 | // with the editor afterwards, on every state update and after a view |
| 109 | // switch. |
| 110 | Checkable bool |
| 111 | Pressed bool |
| 112 | } |
| 113 | |
| 114 | // menu is one top-level <details> in the bar. |
| 115 | type menu struct { |
| 116 | // ID is the DOM id stem, used to tie the <summary> to its item |
| 117 | // list. Not a catalog key — it never reaches a person. |
| 118 | ID string |
| 119 | Title string |
| 120 | Items []menuItem |
| 121 | } |
| 122 | |
| 123 | // Checks reports whether the menu has any checkable item. Such a menu |
| 124 | // renders the check column on every row, so labels stay aligned; a plain |
| 125 | // menu has no empty gutter. |
| 126 | func (m menu) Checks() bool { |
| 127 | for _, it := range m.Items { |
| 128 | if it.Checkable { |
| 129 | return true |
| 130 | } |
| 131 | } |
| 132 | return false |
| 133 | } |
| 134 | |
| 135 | // menuView is what views/show.html renders. |
| 136 | type menuView struct { |
| 137 | // Label is the bar's own accessible name. |
| 138 | Label string |
| 139 | Menus []menu |
| 140 | } |
| 141 | |
| 142 | // newMenuView builds the bar's copy for one request. |
| 143 | // |
| 144 | // sqid is needed because two items are per-document links (the two |
| 145 | // exports). Everything else is the same on every document, but the |
| 146 | // whole structure is built per request regardless, because every label |
| 147 | // is resolved through rastrillo.T against the request's locale — a |
| 148 | // package-level var built once would be built in whichever locale |
| 149 | // happened to serve first. |
| 150 | // The two page-view modes, and the cookie that carries a person's |
| 151 | // choice between them. |
| 152 | // |
| 153 | // A COOKIE, not localStorage, and the reason is the first paint. The |
| 154 | // server has to know the mode to render [docs-canvas]'s data-view, or a |
| 155 | // person who prefers paged watches an infinite document reflow into |
| 156 | // pages the moment the deferred bundle runs. localStorage is invisible |
| 157 | // here; a cookie is not. |
| 158 | // |
| 159 | // PER PERSON, not per document. How you like to read is not something |
| 160 | // to impose on a collaborator, and nothing about it belongs in the CRDT |
| 161 | // — see fixtures/pages.mjs's head comment for why a persisted page |
| 162 | // break would be an edit two people could fight over. |
| 163 | const ( |
| 164 | ViewPaged = "paged" |
| 165 | ViewInfinite = "infinite" |
| 166 | ViewCookie = "docs-view" |
| 167 | ) |
| 168 | |
| 169 | // viewMode reads the mode from the request, defaulting to infinite. |
| 170 | // |
| 171 | // Closed in the strong sense, like every other value this app takes off |
| 172 | // the wire: anything that is not exactly "paged" is infinite. The |
| 173 | // cookie is written by fixtures/pages.mjs and is therefore a value a |
| 174 | // person can set to anything at all, and it lands in an HTML attribute |
| 175 | // — so it is a key into this comparison and never a string that gets |
| 176 | // printed. Same discipline as fragment.rs's ALIGNMENTS, for the same |
| 177 | // reason. |
| 178 | func viewMode(r *http.Request) string { |
| 179 | c, err := r.Cookie(ViewCookie) |
| 180 | if err != nil || c.Value != ViewPaged { |
| 181 | return ViewInfinite |
| 182 | } |
| 183 | return ViewPaged |
| 184 | } |
| 185 | |
| 186 | // The inspector's open or closed state (spec I2: the Format toggle). A |
| 187 | // cookie for the same reason docs-view is one: the server renders the |
| 188 | // state, so the first paint is already right (no flash, CLS 0), which |
| 189 | // localStorage could not give. Written by brand/inspector.js. |
| 190 | const ( |
| 191 | InspectorOpen = "open" |
| 192 | InspectorClosed = "closed" |
| 193 | InspectorCookie = "docs-inspector" |
| 194 | ) |
| 195 | |
| 196 | // inspectorState reads it, defaulting to open. Closed in the strong sense |
| 197 | // viewMode is: only exactly "closed" is closed, and the value is a key |
| 198 | // into this comparison, never a string that gets printed. |
| 199 | func inspectorState(r *http.Request) string { |
| 200 | c, err := r.Cookie(InspectorCookie) |
| 201 | if err != nil || c.Value != InspectorClosed { |
| 202 | return InspectorOpen |
| 203 | } |
| 204 | return InspectorClosed |
| 205 | } |
| 206 | |
| 207 | // fileItems is the File menu, whose first entry is the one thing in this |
| 208 | // bar a guest may not do. |
| 209 | // |
| 210 | // "New document" is a plain form posting /documents, which a guest is |
| 211 | // answered 403 for. A control that exists only to fail is the same defect |
| 212 | // as the editor a reader could type into, in a smaller place — so it is |
| 213 | // absent for them rather than present and refused. |
| 214 | func fileItems(r *http.Request, sqid string, canCreate, canDelete bool) []menuItem { |
| 215 | items := make([]menuItem, 0, 8) |
| 216 | if canCreate { |
| 217 | items = append(items, menuItem{Kind: menuForm, Action: "/documents", Label: rastrillo.T(r, "docs.editor.menu.file.new")}) |
| 218 | } |
| 219 | // Sign out used to be the last item here too. It is in the account |
| 220 | // menu and nowhere else now (spec §8.2): once, in the same corner, in |
| 221 | // every app. |
| 222 | items = append(items, |
| 223 | menuItem{Kind: menuLink, Href: "/", JumpTo: true, Label: rastrillo.T(r, "docs.editor.menu.file.open"), Shortcut: estilomenu.Shortcut(r, "Mod+K")}, |
| 224 | menuItem{Kind: menuDivider}, |
| 225 | // The two exports, moved here from [docs-doc-actions]. Still plain |
| 226 | // links with the download attribute, so they behave identically to |
| 227 | // before for anyone who had learned where they were — the route |
| 228 | // did not change, only where it is named. |
| 229 | menuItem{Kind: menuLink, Href: "/d/" + sqid + "/export.pdf", Download: true, Label: rastrillo.T(r, "docs.export.pdf.label")}, |
| 230 | menuItem{Kind: menuLink, Href: "/d/" + sqid + "/export.md", Download: true, Label: rastrillo.T(r, "docs.export.markdown.label")}, |
| 231 | menuItem{Kind: menuCommand, Command: "print", Label: rastrillo.T(r, "docs.editor.menu.file.print"), Shortcut: estilomenu.Shortcut(r, "Mod+P")}, |
| 232 | ) |
| 233 | // Delete… is the owner's, last and set apart, and a link to its own |
| 234 | // confirmation page (delete.go) rather than a command: it works with |
| 235 | // no script, and nothing is deleted by the menu itself. |
| 236 | if canDelete { |
| 237 | items = append(items, |
| 238 | menuItem{Kind: menuDivider}, |
| 239 | menuItem{Kind: menuLink, Href: "/d/" + sqid + "/delete", Label: rastrillo.T(r, "docs.editor.menu.file.delete")}, |
| 240 | ) |
| 241 | } |
| 242 | return items |
| 243 | } |
| 244 | |
| 245 | // redoChord is the redo binding a person on this platform expects. The |
| 246 | // keymap binds both (editor-entry.js: Mod-y and Mod-Shift-z); the label |
| 247 | // names the one each platform's own editors use. |
| 248 | func redoChord(r *http.Request) string { |
| 249 | if estilomenu.Apple(r) { |
| 250 | return "Mod+Shift+Z" |
| 251 | } |
| 252 | return "Mod+Y" |
| 253 | } |
| 254 | |
| 255 | func newMenuView(r *http.Request, sqid string, canCreate, canDelete bool) menuView { |
| 256 | mode := viewMode(r) |
| 257 | key := func(chord string) string { return estilomenu.Shortcut(r, chord) } |
| 258 | return menuView{ |
| 259 | Label: rastrillo.T(r, "docs.editor.menu.label"), |
| 260 | Menus: []menu{ |
| 261 | { |
| 262 | ID: "file", |
| 263 | Title: rastrillo.T(r, "docs.editor.menu.file"), |
| 264 | Items: fileItems(r, sqid, canCreate, canDelete), |
| 265 | }, |
| 266 | { |
| 267 | ID: "edit", |
| 268 | Title: rastrillo.T(r, "docs.editor.menu.edit"), |
| 269 | Items: []menuItem{ |
| 270 | {Kind: menuCommand, Command: "undo", Label: rastrillo.T(r, "docs.editor.toolbar.undo"), Shortcut: key("Mod+Z")}, |
| 271 | {Kind: menuCommand, Command: "redo", Label: rastrillo.T(r, "docs.editor.toolbar.redo"), Shortcut: key(redoChord(r))}, |
| 272 | {Kind: menuDivider}, |
| 273 | {Kind: menuCommand, Command: "select_all", Label: rastrillo.T(r, "docs.editor.menu.edit.selectall"), Shortcut: key("Mod+A")}, |
| 274 | }, |
| 275 | }, |
| 276 | { |
| 277 | ID: "view", |
| 278 | Title: rastrillo.T(r, "docs.editor.menu.view"), |
| 279 | Items: []menuItem{ |
| 280 | // A pair rather than one toggle, so the current mode |
| 281 | // is readable without interpreting a checkmark's |
| 282 | // absence. Exactly one is pressed. |
| 283 | // |
| 284 | // aria-pressed on a <button>, NOT aria-checked: |
| 285 | // aria-checked belongs to menuitemcheckbox and |
| 286 | // menuitemradio, and these menus are disclosures |
| 287 | // whose items are ordinary buttons (see the markup's |
| 288 | // own comment for why). Using the attribute that |
| 289 | // does not apply to the element is worse than using |
| 290 | // none. |
| 291 | {Kind: menuCommand, Command: "view_paged", Label: rastrillo.T(r, "docs.editor.menu.view.paged"), Checkable: true, Pressed: mode == ViewPaged}, |
| 292 | {Kind: menuCommand, Command: "view_infinite", Label: rastrillo.T(r, "docs.editor.menu.view.infinite"), Checkable: true, Pressed: mode != ViewPaged}, |
| 293 | }, |
| 294 | }, |
| 295 | { |
| 296 | ID: "format", |
| 297 | Title: rastrillo.T(r, "docs.editor.menu.format"), |
| 298 | Items: []menuItem{ |
| 299 | // Every one of these already exists in |
| 300 | // fixtures/toolbar.mjs's command table. The menu |
| 301 | // adds no behaviour here at all — it is a second |
| 302 | // surface onto the same names, which is the whole |
| 303 | // reason property 2 above is worth a test. |
| 304 | {Kind: menuCommand, Command: "strong", Label: rastrillo.T(r, "docs.editor.toolbar.bold"), Shortcut: key("Mod+B")}, |
| 305 | {Kind: menuCommand, Command: "em", Label: rastrillo.T(r, "docs.editor.toolbar.italic"), Shortcut: key("Mod+I")}, |
| 306 | {Kind: menuCommand, Command: "code", Label: rastrillo.T(r, "docs.editor.toolbar.code")}, |
| 307 | {Kind: menuDivider}, |
| 308 | // The paragraph styles, checked for the one the caret |
| 309 | // is in. "Body text" used to show as DISABLED there, |
| 310 | // because setting the style it already has does |
| 311 | // nothing: disabled standing in for current. |
| 312 | // |
| 313 | // The command names match the block select's own |
| 314 | // option values (export.go's Blocks), so the two |
| 315 | // controls set the same thing by the same name. |
| 316 | {Kind: menuCommand, Command: "block_paragraph", Label: rastrillo.T(r, "docs.editor.toolbar.block.paragraph"), Checkable: true}, |
| 317 | {Kind: menuCommand, Command: "block_heading-1", Label: rastrillo.T(r, "docs.editor.toolbar.block.heading1"), Checkable: true}, |
| 318 | {Kind: menuCommand, Command: "block_heading-2", Label: rastrillo.T(r, "docs.editor.toolbar.block.heading2"), Checkable: true}, |
| 319 | {Kind: menuCommand, Command: "block_heading-3", Label: rastrillo.T(r, "docs.editor.toolbar.block.heading3"), Checkable: true}, |
| 320 | {Kind: menuDivider}, |
| 321 | {Kind: menuCommand, Command: "align_left", Label: rastrillo.T(r, "docs.editor.toolbar.align.left"), Checkable: true}, |
| 322 | {Kind: menuCommand, Command: "align_center", Label: rastrillo.T(r, "docs.editor.toolbar.align.center"), Checkable: true}, |
| 323 | {Kind: menuCommand, Command: "align_right", Label: rastrillo.T(r, "docs.editor.toolbar.align.right"), Checkable: true}, |
| 324 | {Kind: menuDivider}, |
| 325 | {Kind: menuCommand, Command: "bullet_list", Label: rastrillo.T(r, "docs.editor.toolbar.bulletlist")}, |
| 326 | {Kind: menuCommand, Command: "ordered_list", Label: rastrillo.T(r, "docs.editor.toolbar.orderedlist")}, |
| 327 | {Kind: menuCommand, Command: "blockquote", Label: rastrillo.T(r, "docs.editor.toolbar.quote")}, |
| 328 | {Kind: menuDivider}, |
| 329 | // The same two commands the Tab key runs, by the |
| 330 | // same names — see fixtures/indent.mjs and the |
| 331 | // keymap in editor-entry.js. The shortcut shown |
| 332 | // here is therefore a statement of fact rather than |
| 333 | // a second binding. |
| 334 | {Kind: menuCommand, Command: "indent", Label: rastrillo.T(r, "docs.editor.menu.format.indent"), Shortcut: key("Tab")}, |
| 335 | {Kind: menuCommand, Command: "outdent", Label: rastrillo.T(r, "docs.editor.menu.format.outdent"), Shortcut: key("Shift+Tab")}, |
| 336 | {Kind: menuDivider}, |
| 337 | {Kind: menuCommand, Command: "clear_formatting", Label: rastrillo.T(r, "docs.editor.menu.format.clear")}, |
| 338 | }, |
| 339 | }, |
| 340 | { |
| 341 | ID: "tools", |
| 342 | Title: rastrillo.T(r, "docs.editor.menu.tools"), |
| 343 | Items: []menuItem{ |
| 344 | {Kind: menuCommand, Command: "word_count", Label: rastrillo.T(r, "docs.editor.menu.tools.wordcount")}, |
| 345 | }, |
| 346 | }, |
| 347 | { |
| 348 | // Help is where a person looks for the shortcuts, and |
| 349 | // last in the bar, as on every desktop. Fonts and the |
| 350 | // Colophon are the same two pages the account menu's |
| 351 | // Help group links (chrome.go). |
| 352 | ID: "help", |
| 353 | Title: rastrillo.T(r, "docs.editor.menu.help"), |
| 354 | Items: []menuItem{ |
| 355 | {Kind: menuCommand, Command: "shortcuts", Label: rastrillo.T(r, "docs.editor.menu.tools.shortcuts")}, |
| 356 | {Kind: menuDivider}, |
| 357 | {Kind: menuLink, Href: fontsPath, Label: rastrillo.T(r, "docs.help.fonts")}, |
| 358 | {Kind: menuLink, Href: colophonPath, Label: rastrillo.T(r, "docs.help.colophon")}, |
| 359 | }, |
| 360 | }, |
| 361 | }, |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // menuCommandNames is every command name the bar names, in order, with |
| 366 | // duplicates kept — internal/docs's TestMenuCommandsExistInTheBundle |
| 367 | // resolves each against fixtures/toolbar.mjs's source, and |
| 368 | // TestMenuStructureIsWellFormed checks the shape of the rest. |
| 369 | // |
| 370 | // It reads the built view rather than a second list, which is the point: |
| 371 | // a list maintained beside newMenuView is a list that stops matching it, |
| 372 | // and the failure mode is a test that passes by checking a name nobody |
| 373 | // renders. |
| 374 | func menuCommandNames(v menuView) []string { |
| 375 | var out []string |
| 376 | for _, m := range v.Menus { |
| 377 | for _, it := range m.Items { |
| 378 | if it.Kind == menuCommand { |
| 379 | out = append(out, it.Command) |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | return out |
| 384 | } |
| 385 | |
| 386 | // readerMenu is the menu bar a reader gets: what they can use and nothing |
| 387 | // else (the phone web audit's finding 8; spec §7.3, hidden not disabled). |
| 388 | // A reader has no editor, so every command the editor runs would ship |
| 389 | // disabled for good; they are absent instead, and so is a menu left with |
| 390 | // nothing in it (Edit, View, Format, Tools). A divider never starts or |
| 391 | // ends a menu, or follows another. |
| 392 | func readerMenu(v menuView) menuView { |
| 393 | out := menuView{Label: v.Label} |
| 394 | for _, m := range v.Menus { |
| 395 | var items []menuItem |
| 396 | for _, it := range m.Items { |
| 397 | switch { |
| 398 | case it.Kind == menuCommand: |
| 399 | case it.Kind == menuDivider && (len(items) == 0 || items[len(items)-1].Kind == menuDivider): |
| 400 | default: |
| 401 | items = append(items, it) |
| 402 | } |
| 403 | } |
| 404 | for len(items) > 0 && items[len(items)-1].Kind == menuDivider { |
| 405 | items = items[:len(items)-1] |
| 406 | } |
| 407 | if len(items) > 0 { |
| 408 | m.Items = items |
| 409 | out.Menus = append(out.Menus, m) |
| 410 | } |
| 411 | } |
| 412 | return out |
| 413 | } |
| 414 | |
| 415 | // ••• on a phone (design/phone-web, Paul 2026-11-28: a drop-down from the |
| 416 | // ••• button, not a sheet). Below 704px the menu bar is not drawn, so |
| 417 | // ••• carries its verbs, in the native design's order: Share… and Rename; |
| 418 | // the exports and Print…; Word count…, Open a document… and the |
| 419 | // paged/infinite pair; Help (a disclosure inside the panel, since a |
| 420 | // <details> menu cannot nest in the bar's group); Delete…, the owner's, |
| 421 | // last and set apart. Filtered by capability as the menu bar is: a row a |
| 422 | // person cannot use is absent. |
| 423 | // |
| 424 | // A row the editor runs is a proxy for the menu bar's item of the same |
| 425 | // command (data-docs-proxy, brand/phone.js), so the bar stays the one |
| 426 | // command table (menu.go's property 2) and its state (disabled until the |
| 427 | // editor mounts, pressed for the current view) is mirrored, not kept |
| 428 | // twice. Shortcut hints are left out: this is the touch idiom. |
| 429 | const ( |
| 430 | moreLink = "link" |
| 431 | moreProxy = "proxy" |
| 432 | moreDivider = "divider" |
| 433 | moreHelp = "help" |
| 434 | ) |
| 435 | |
| 436 | type moreRow struct { |
| 437 | Kind string |
| 438 | Href string |
| 439 | Download bool |
| 440 | JumpTo bool |
| 441 | // Rename focuses the large title in place (brand/phone.js); without |
| 442 | // script it is a link to it. |
| 443 | Rename bool |
| 444 | Danger bool |
| 445 | // Proxy is the menu bar command a proxy row runs. |
| 446 | Proxy string |
| 447 | Checkable bool |
| 448 | Pressed bool |
| 449 | Label string |
| 450 | // Icon names the row's glyph (views/partials/editor_bar.html's |
| 451 | // docs-more-icon); empty for none. |
| 452 | Icon string |
| 453 | // Help is the Help disclosure's rows. |
| 454 | Help []moreRow |
| 455 | } |
| 456 | |
| 457 | // moreRowView is one row as docs-more-row draws it: the row, and |
| 458 | // whether its panel draws the check column (so an icon-less row keeps |
| 459 | // its label aligned). |
| 460 | type moreRowView struct { |
| 461 | Row moreRow |
| 462 | Checks bool |
| 463 | } |
| 464 | |
| 465 | // Checks reports whether any row has a check column. |
| 466 | func (v moreView) Checks() bool { |
| 467 | for _, r := range v.Rows { |
| 468 | if r.Checkable { |
| 469 | return true |
| 470 | } |
| 471 | } |
| 472 | return false |
| 473 | } |
| 474 | |
| 475 | type moreView struct { |
| 476 | // Label is the ••• button's accessible name; Aa and AaLabel the |
| 477 | // Format button beside it, a writer's. |
| 478 | Label string |
| 479 | Aa bool |
| 480 | AaLabel string |
| 481 | Rows []moreRow |
| 482 | } |
| 483 | |
| 484 | // moreCaps is what the person may do with this document. |
| 485 | type moreCaps struct { |
| 486 | Share, Write, Delete, ImportReport bool |
| 487 | } |
| 488 | |
| 489 | func newMoreView(r *http.Request, sqid string, c moreCaps) moreView { |
| 490 | mode := viewMode(r) |
| 491 | var rows []moreRow |
| 492 | divide := func() { |
| 493 | if len(rows) > 0 && rows[len(rows)-1].Kind != moreDivider { |
| 494 | rows = append(rows, moreRow{Kind: moreDivider}) |
| 495 | } |
| 496 | } |
| 497 | if c.Share { |
| 498 | rows = append(rows, moreRow{Kind: moreLink, Href: "/d/" + sqid + "/share", Label: rastrillo.T(r, "docs.share.action"), Icon: "share"}) |
| 499 | } |
| 500 | if c.Write { |
| 501 | rows = append(rows, moreRow{Kind: moreLink, Href: "#docs-title", Rename: true, Label: rastrillo.T(r, "docs.chrome.title.rename"), Icon: "rename"}) |
| 502 | } |
| 503 | divide() |
| 504 | rows = append(rows, |
| 505 | moreRow{Kind: moreLink, Href: "/d/" + sqid + "/export.pdf", Download: true, Label: rastrillo.T(r, "docs.export.pdf.label"), Icon: "download"}, |
| 506 | moreRow{Kind: moreLink, Href: "/d/" + sqid + "/export.md", Download: true, Label: rastrillo.T(r, "docs.export.markdown.label"), Icon: "download"}, |
| 507 | ) |
| 508 | if c.Write { |
| 509 | rows = append(rows, moreRow{Kind: moreProxy, Proxy: "print", Label: rastrillo.T(r, "docs.editor.menu.file.print"), Icon: "print"}) |
| 510 | } |
| 511 | divide() |
| 512 | if c.Write { |
| 513 | rows = append(rows, moreRow{Kind: moreProxy, Proxy: "word_count", Label: rastrillo.T(r, "docs.editor.menu.tools.wordcount"), Icon: "count"}) |
| 514 | } |
| 515 | rows = append(rows, moreRow{Kind: moreLink, Href: "/", JumpTo: true, Label: rastrillo.T(r, "docs.editor.menu.file.open"), Icon: "open"}) |
| 516 | if c.ImportReport { |
| 517 | rows = append(rows, moreRow{Kind: moreLink, Href: "/d/" + sqid + "/import-report", Label: rastrillo.T(r, "docs.import.report.open"), Icon: "report"}) |
| 518 | } |
| 519 | if c.Write { |
| 520 | rows = append(rows, |
| 521 | moreRow{Kind: moreProxy, Proxy: "view_paged", Label: rastrillo.T(r, "docs.editor.menu.view.paged"), Checkable: true, Pressed: mode == ViewPaged}, |
| 522 | moreRow{Kind: moreProxy, Proxy: "view_infinite", Label: rastrillo.T(r, "docs.editor.menu.view.infinite"), Checkable: true, Pressed: mode != ViewPaged}, |
| 523 | ) |
| 524 | } |
| 525 | divide() |
| 526 | help := moreRow{Kind: moreHelp, Label: rastrillo.T(r, "docs.editor.menu.help"), Icon: "help"} |
| 527 | if c.Write { |
| 528 | help.Help = append(help.Help, moreRow{Kind: moreProxy, Proxy: "shortcuts", Label: rastrillo.T(r, "docs.editor.menu.tools.shortcuts")}) |
| 529 | } |
| 530 | help.Help = append(help.Help, |
| 531 | moreRow{Kind: moreLink, Href: fontsPath, Label: rastrillo.T(r, "docs.help.fonts")}, |
| 532 | moreRow{Kind: moreLink, Href: colophonPath, Label: rastrillo.T(r, "docs.help.colophon")}, |
| 533 | ) |
| 534 | rows = append(rows, help) |
| 535 | if c.Delete { |
| 536 | divide() |
| 537 | rows = append(rows, moreRow{Kind: moreLink, Href: "/d/" + sqid + "/delete", Danger: true, Label: rastrillo.T(r, "docs.editor.menu.file.delete"), Icon: "delete"}) |
| 538 | } |
| 539 | return moreView{ |
| 540 | Label: rastrillo.T(r, "docs.editor.more"), |
| 541 | Aa: c.Write, |
| 542 | AaLabel: rastrillo.T(r, "docs.editor.inspector.toggle"), |
| 543 | Rows: rows, |
| 544 | } |
| 545 | } |
| 546 | |