|
| 1 | +/** |
| 2 | + * Calendar view pane — bespoke renderer for urn:solid:Event. |
| 3 | + * |
| 4 | + * Reads Event.name, summary, startTime, endTime, location, attributedTo |
| 5 | + * and renders a save-the-date card: month/day chip on the left, title + |
| 6 | + * time range + location on the right. Empty fields gracefully omitted. |
| 7 | + * |
| 8 | + * Same data, different lens. Drop in alongside ui-pane (Inline) and |
| 9 | + * schema-pane (Edit) — LOSOS shows them all as tabs. |
| 10 | + * |
| 11 | + * AGPL-3.0 — part of solid-apps |
| 12 | + */ |
| 13 | + |
| 14 | +import { html, render } from 'https://losos.org/losos/html.js' |
| 15 | + |
| 16 | +const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] |
| 17 | +const niceHost = (u) => { try { return new URL(u).host.replace(/^www\./, '') } catch { return u } } |
| 18 | + |
| 19 | +function partsOf(iso) { |
| 20 | + if (!iso) return null |
| 21 | + const d = new Date(iso) |
| 22 | + if (isNaN(d)) return null |
| 23 | + return { |
| 24 | + month: MONTHS[d.getMonth()], |
| 25 | + day: d.getDate(), |
| 26 | + weekday: d.toLocaleDateString(undefined, { weekday: 'long' }), |
| 27 | + time: d.toLocaleTimeString(undefined, { hour: 'numeric', minute: '2-digit' }), |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function timeRange(start, end) { |
| 32 | + if (!start) return null |
| 33 | + if (!end) return start.time |
| 34 | + const sameClock = start.time === end.time |
| 35 | + return sameClock ? start.time : `${start.time} – ${end.time}` |
| 36 | +} |
| 37 | + |
| 38 | +export default { |
| 39 | + label: 'Calendar', |
| 40 | + icon: '\ud83d\udcc5', |
| 41 | + |
| 42 | + canHandle(subject, store) { |
| 43 | + const node = store.get(subject.value) |
| 44 | + if (!node) return false |
| 45 | + const t = store.type(node) |
| 46 | + if (!t) return false |
| 47 | + return Array.isArray(t) |
| 48 | + ? t.some(x => /Event/i.test(x)) |
| 49 | + : /Event/i.test(t) |
| 50 | + }, |
| 51 | + |
| 52 | + render(subject, lionStore, container, rawData) { |
| 53 | + let data = rawData |
| 54 | + if (!data) { |
| 55 | + const dataEl = document.querySelector('script[type="application/ld+json"]') |
| 56 | + try { data = JSON.parse(dataEl.textContent) } catch { return } |
| 57 | + } |
| 58 | + |
| 59 | + const name = data.name || data.title || 'Untitled' |
| 60 | + const summary = data.summary || data.description |
| 61 | + const start = partsOf(data.startTime) |
| 62 | + const end = partsOf(data.endTime) |
| 63 | + const location = (typeof data.location === 'object' ? data.location['@id'] : data.location) || '' |
| 64 | + const attributedTo = data.attributedTo |
| 65 | + ? (typeof data.attributedTo === 'string' ? data.attributedTo : data.attributedTo['@id'] || data.attributedTo.name) |
| 66 | + : null |
| 67 | + |
| 68 | + const range = start ? timeRange(start, end) : null |
| 69 | + const dateLine = start ? `${start.weekday}${range ? ' \u00b7 ' + range : ''}` : null |
| 70 | + const isUrl = (s) => /^https?:\/\//.test(s) |
| 71 | + |
| 72 | + render(container, html` |
| 73 | + <style> |
| 74 | + .ev-bg { |
| 75 | + min-height: 100vh; |
| 76 | + background: |
| 77 | + radial-gradient(circle at 20% 0%, rgba(239,68,68,0.06) 0%, transparent 40%), |
| 78 | + radial-gradient(circle at 80% 100%, rgba(99,102,241,0.06) 0%, transparent 40%), |
| 79 | + linear-gradient(180deg, #fafaf8 0%, #f0efeb 100%); |
| 80 | + padding: 64px 24px 96px; |
| 81 | + } |
| 82 | + .ev-card { |
| 83 | + max-width: 640px; margin: 0 auto; |
| 84 | + background: #fff; |
| 85 | + border: 1px solid #e5e3de; |
| 86 | + border-radius: 16px; |
| 87 | + padding: 36px; |
| 88 | + display: flex; gap: 28px; align-items: flex-start; |
| 89 | + box-shadow: 0 8px 32px rgba(0,0,0,0.04); |
| 90 | + } |
| 91 | + .ev-date { |
| 92 | + flex: 0 0 auto; |
| 93 | + width: 96px; |
| 94 | + border: 1px solid #e5e3de; |
| 95 | + border-radius: 12px; |
| 96 | + overflow: hidden; |
| 97 | + text-align: center; |
| 98 | + } |
| 99 | + .ev-month { |
| 100 | + background: #ef4444; color: #fff; |
| 101 | + font: 600 12px/1 'Inter', -apple-system, sans-serif; |
| 102 | + padding: 8px; letter-spacing: 0.18em; text-transform: uppercase; |
| 103 | + } |
| 104 | + .ev-day { |
| 105 | + font: 500 44px/1 Georgia, serif; |
| 106 | + color: #1a1a1a; padding: 16px 0; |
| 107 | + background: #fafaf8; |
| 108 | + } |
| 109 | + .ev-no-date { |
| 110 | + flex: 0 0 auto; |
| 111 | + width: 96px; height: 96px; |
| 112 | + border-radius: 12px; |
| 113 | + background: #f5f4f0; |
| 114 | + color: #aaa; |
| 115 | + display: flex; align-items: center; justify-content: center; |
| 116 | + font: 500 24px/1 Georgia, serif; |
| 117 | + } |
| 118 | + .ev-meta { flex: 1; min-width: 0; } |
| 119 | + .ev-name { |
| 120 | + font: 500 26px/1.2 Georgia, serif; |
| 121 | + color: #1a1a1a; margin: 0 0 8px; |
| 122 | + letter-spacing: -0.4px; |
| 123 | + } |
| 124 | + .ev-when { |
| 125 | + font: 500 13px/1.4 'Inter', -apple-system, sans-serif; |
| 126 | + color: #666; margin-bottom: 18px; |
| 127 | + } |
| 128 | + .ev-summary { |
| 129 | + font: 400 15px/1.6 Georgia, serif; |
| 130 | + color: #444; margin: 0 0 20px; |
| 131 | + } |
| 132 | + .ev-row { |
| 133 | + display: flex; gap: 8px; align-items: center; |
| 134 | + padding-top: 14px; margin-top: 14px; |
| 135 | + border-top: 1px solid #e5e3de; |
| 136 | + font: 400 13px/1.4 'Inter', -apple-system, sans-serif; |
| 137 | + color: #666; |
| 138 | + } |
| 139 | + .ev-row + .ev-row { border-top: none; padding-top: 0; margin-top: 6px; } |
| 140 | + .ev-row .icon { opacity: 0.6; } |
| 141 | + .ev-row a { color: #1a5276; text-decoration: none; } |
| 142 | + .ev-row a:hover { color: #6366f1; } |
| 143 | + </style> |
| 144 | +
|
| 145 | + <div class="ev-bg"> |
| 146 | + <div class="ev-card"> |
| 147 | + ${start |
| 148 | + ? html` |
| 149 | + <div class="ev-date"> |
| 150 | + <div class="ev-month">${start.month}</div> |
| 151 | + <div class="ev-day">${start.day}</div> |
| 152 | + </div>` |
| 153 | + : html`<div class="ev-no-date">?</div>` |
| 154 | + } |
| 155 | +
|
| 156 | + <div class="ev-meta"> |
| 157 | + <h1 class="ev-name">${name}</h1> |
| 158 | + ${dateLine ? html`<div class="ev-when">${dateLine}</div>` : null} |
| 159 | + ${summary ? html`<p class="ev-summary">${summary}</p>` : null} |
| 160 | +
|
| 161 | + ${location ? html` |
| 162 | + <div class="ev-row"> |
| 163 | + <span class="icon">\ud83d\udccd</span> |
| 164 | + ${isUrl(location) |
| 165 | + ? html`<a href="${location}" target="_blank" rel="noopener">${niceHost(location)}</a>` |
| 166 | + : html`<span>${location}</span>`} |
| 167 | + </div>` : null} |
| 168 | +
|
| 169 | + ${attributedTo ? html` |
| 170 | + <div class="ev-row"> |
| 171 | + <span class="icon">\ud83d\udc64</span> |
| 172 | + ${isUrl(attributedTo) |
| 173 | + ? html`<a href="${attributedTo}" target="_blank" rel="noopener">${niceHost(attributedTo)}</a>` |
| 174 | + : html`<span>${attributedTo}</span>`} |
| 175 | + </div>` : null} |
| 176 | + </div> |
| 177 | + </div> |
| 178 | + </div> |
| 179 | + `) |
| 180 | + } |
| 181 | +} |
0 commit comments