oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1// chips.mjs — how the editor draws a [[link]], a date link and an embed
2// (spec §4.2): the DOM output specs internal/mapa/markup/chips.go writes
3// as HTML. Gate L4 compares the two byte for byte. A chip's state (team,
4// alias, pending, gone) is never an attribute of the node: it comes from
5// a resolution, the same answer the server's resolver gives (§6.8).
6
7const SVG = 'http://www.w3.org/2000/svg'
8
9// glyphs are chips.go's: Lucide's strokes on a 24px grid.
10const glyphs = {
11 page: ['M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z',
12 'M14 2v4a2 2 0 0 0 2 2h4', 'M9 13h6', 'M9 17h4'],
13 'page-plus': ['M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z',
14 'M14 2v4a2 2 0 0 0 2 2h4', 'M9 15h6', 'M12 12v6'],
15 calendar: ['M8 2v4', 'M16 2v4',
16 'M5 4h14a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2Z',
17 'M3 10h18'],
18 block: ['M13 4v16', 'M17 4v16', 'M19 4H9.5a4.5 4.5 0 0 0 0 9H13'],
19 embed: ['M21 8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16Z',
20 'm3.3 7 8.7 5 8.7-5', 'M12 22V12']
21}
22
23export function glyph (name) {
24 return [`${SVG} svg`, { 'mapa-i': '', viewBox: '0 0 24 24', 'aria-hidden': 'true' },
25 ...glyphs[name].map(path => [`${SVG} path`, { d: path }])]
26}
27
28// pageIcon is a page's icon in a chip's glyph cell (columns and icons
29// spec §2.4), as chips.go's iconHTML draws it.
30export function pageIcon (look) {
31 if (look?.emoji) {
32 return ['span', { 'mapa-i': '', 'mapa-icon': '', 'aria-hidden': 'true' }, look.emoji]
33 }
34 if (look?.image && /^[0-9a-z]{1,13}$/u.test(look.image)) {
35 return ['img', {
36 'mapa-i': '', 'mapa-icon': '', src: `/i/${look.image}`, alt: '',
37 width: '16', height: '16', loading: 'lazy', decoding: 'async'
38 }]
39 }
40 return null
41}
42
43function chip ({ href, kind, title, label, icon, look, shown, note }) {
44 const attrs = { href, 'mapa-chip': kind }
45 if (title) attrs.title = title
46 attrs['aria-label'] = label
47 const spec = ['a', attrs, pageIcon(look) || glyph(icon), shown]
48 if (note) spec.push(['span', { 'mapa-chip-note': '' }, note])
49 return spec
50}
51
52// wikiChip is a [[link]]: target, anchor and shown text as typed, and
53// the resolution the server gave for them.
54export function wikiChip ({ target, anchor, shown: typed }, answer) {
55 const shown = typed || target
56 if (answer.broken) return ['span', { 'mapa-chip': 'broken' }, shown]
57 if (answer.day) return dateChip(typed, answer)
58 if (answer.pending) {
59 return chip({
60 href: answer.href, kind: 'pending', icon: 'page-plus', shown,
61 label: `${shown}, page not written yet`
62 })
63 }
64 const title = answer.title || target
65 const parts = {
66 href: answer.href, kind: 'team', icon: 'page', shown,
67 label: `${shown}, team page`, look: answer.icon
68 }
69 if (answer.alias) {
70 parts.title = `Now called ${title}`
71 parts.label = `${shown}, now called ${title}`
72 }
73 if (anchor) {
74 parts.kind = parts.icon = 'block'
75 parts.look = null
76 parts.label = `${shown}, a block in ${title}`
77 }
78 if (anchor && answer.blockGone) {
79 parts.kind = 'block gone'
80 parts.label = `${shown}, block removed from ${title}`
81 parts.note = 'block removed'
82 }
83 return chip(parts)
84}
85
86// dateChip is a date link: the day in words, never the ISO date.
87export function dateChip (shown, answer) {
88 if (shown) {
89 return chip({
90 href: answer.href, kind: 'date', icon: 'calendar', shown,
91 label: `${shown}, your note for ${answer.day}`
92 })
93 }
94 return chip({
95 href: answer.href, kind: 'date', icon: 'calendar', shown: answer.day,
96 label: `${answer.day}, your note`
97 })
98}
99
100// embedChip is slice 1's embed: a quiet chip showing the reference.
101export function embedChip (ref) {
102 return ['span', {
103 'mapa-chip': 'embed', role: 'img',
104 'aria-label': `${ref}, embedded item, not shown yet`
105 }, glyph('embed'), ref]
106}
107