1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package main

import (
	"html/template"
	"log"
	"net/http"
)

// pageCSS is the stylesheet shared by the editor and the preview.
//
// The preview half deliberately mirrors the typography of profpatsch.de
// (Quattrocento body serif, Open Sans headings, a ~60ch measure, the same
// light/dark palette) so that what you see while writing is what the published
// page will look like. The editor half is a plain card list with no
// resemblance to the site, because it is a tool, not a page.
const pageCSS = `
:root {
  --bg: #ffffff;
  --fg: #111111;
  --muted: #777777;
  --link: #0b5fff;
  --border: #dddddd;
  --card: #f7f7f8;
  --accent: #2f6fb0;
  --danger: #c62828;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a1a;
    --fg: #dddddd;
    --muted: #888888;
    --link: #aaddff;
    --border: #3a3a3a;
    --card: #242427;
    --accent: #5aa3dd;
    --danger: #e57373;
  }
}
*, *::before, *::after { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, sans-serif;
  line-height: 1.5;
}
a { color: var(--link); }

/* ── shell ───────────────────────────────────────────────────────────── */
header.top {
  position: sticky; top: 0; z-index: 20;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding: .6rem 1rem;
  display: flex; align-items: center; gap: .75rem;
}
header.top h1 { font-size: 1.05rem; margin: 0; flex: 1; font-weight: 600; }
header.top a { text-decoration: none; }
.wrap { max-width: 62ch; margin: 0 auto; padding: 1.5rem 1rem 6rem; }
button, .btn {
  font: inherit; font-size: .85rem;
  padding: .3rem .7rem;
  border: 1px solid var(--border); border-radius: 5px;
  background: var(--card); color: var(--fg);
  cursor: pointer;
}
button:hover, .btn:hover { border-color: var(--accent); }
button.danger { color: var(--danger); }
.muted { color: var(--muted); font-size: .85rem; }

/* ── post list ───────────────────────────────────────────────────────── */
ul.posts { list-style: none; padding: 0; }
ul.posts li {
  border-bottom: 1px solid var(--border);
  padding: .6rem 0;
  display: flex; align-items: baseline; gap: .6rem;
}
ul.posts li a { font-weight: 600; text-decoration: none; flex: 1; }
form.newpost { display: flex; gap: .5rem; margin: 1rem 0 2rem; }
form.newpost input {
  flex: 1; font: inherit; padding: .4rem .6rem;
  border: 1px solid var(--border); border-radius: 5px;
  background: var(--bg); color: var(--fg);
}

/* ── editor ──────────────────────────────────────────────────────────── */
.post-meta { display: flex; flex-direction: column; gap: .5rem; margin-bottom: 1.5rem; }
.post-meta input {
  font: inherit; padding: .4rem .6rem;
  border: 1px solid var(--border); border-radius: 5px;
  background: var(--bg); color: var(--fg);
}
.post-meta input.title { font-size: 1.3rem; font-weight: 600; }

.block {
  border: 1px solid var(--border); border-radius: 7px;
  background: var(--card);
  margin: .5rem 0;
  padding: .5rem;
}
/* The block being dragged, and the gap that opens where it would land. */
.block.dragging { opacity: .4; }
.block.drop-target { border-color: var(--accent); border-style: dashed; }
.block-head {
  display: flex; align-items: center; gap: .4rem;
  font-size: .75rem; color: var(--muted);
  margin-bottom: .4rem;
}
.block-head .kind { text-transform: uppercase; letter-spacing: .05em; flex: 1; }
.block-head .handle { cursor: grab; user-select: none; padding: 0 .3rem; }
.block textarea {
  width: 100%; font: inherit;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: .85rem; line-height: 1.45;
  border: 1px solid var(--border); border-radius: 5px;
  background: var(--bg); color: var(--fg);
  padding: .5rem; resize: vertical;
}
.block input.meta {
  width: 100%; font: inherit; font-size: .8rem; margin-top: .3rem;
  border: 1px solid var(--border); border-radius: 5px;
  background: var(--bg); color: var(--fg); padding: .3rem .5rem;
}
.block img.thumb { max-width: 100%; border-radius: 5px; display: block; }
.block .saving { color: var(--accent); }

.add-bar { display: flex; gap: .4rem; margin: 1rem 0; flex-wrap: wrap; }
/* Full-window overlay shown while a file is dragged over the page. */
.dropzone {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(47,111,176,.15);
  border: 3px dashed var(--accent);
  display: none; align-items: center; justify-content: center;
  font-size: 1.3rem; color: var(--accent); font-weight: 600;
  pointer-events: none;
}
body.dragging-file .dropzone { display: flex; }
.errors { color: var(--danger); font-size: .85rem; }

/* ── preview: matches profpatsch.de ──────────────────────────────────── */
article.post {
  max-width: 62ch; margin: 0 auto; padding: 2rem 1rem 5rem;
  font-size: 20px;
}
article.post h1 { font-weight: 300; font-size: 2.2rem; margin-bottom: .1em; }
article.post .subtitle { font-weight: 300; font-size: 1.2rem; color: var(--muted); margin-top: 0; }
article.post h2, article.post h3 { font-weight: 400; margin-top: 2em; }
article.post p, article.post li {
  font-family: Quattrocento, Georgia, serif;
  line-height: 1.5em;
}
article.post p { text-align: justify; }
article.post pre {
  overflow-x: auto; padding: .8rem; border-radius: 6px;
  font-size: .8rem; line-height: 1.45;
  background: var(--card);
}
article.post code { font-size: .85em; }
article.post pre code { font-size: inherit; }
article.post blockquote {
  margin: 1.5em 0; padding-left: 1em;
  border-left: 3px solid var(--border); color: var(--muted);
}
article.post table { border-collapse: collapse; width: 100%; font-size: .9rem; }
article.post th, article.post td { border: 1px solid var(--border); padding: .3rem .5rem; }
figure.block-image, figure.block-stl { margin: 2em 0; }
figure.block-image img { max-width: 100%; height: auto; display: block; border-radius: 4px; }
figcaption {
  font-size: .8rem; color: var(--muted); margin-top: .5em; text-align: center;
  font-family: system-ui, sans-serif;
}
.block-error {
  border: 1px solid var(--danger); color: var(--danger);
  border-radius: 5px; padding: .5rem; font-size: .85rem; margin: 1em 0;
}

/* ── stl viewer ──────────────────────────────────────────────────────── */
/* The viewer is inert until activated. Crucially it does NOT set
   touch-action here: while inactive a finger must be able to scroll the page
   past the model, exactly as it would past an image. Only .is-active takes
   the gestures over. */
.stl-viewer {
  position: relative;
  width: 100%; aspect-ratio: 4 / 3;
  background: var(--card);
  border: 1px solid var(--border); border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
  /* Dragging the model must never begin a text selection or a native drag of
     the canvas bitmap; either would be delivered as a drag-and-drop instead of
     an orbit. The JS also calls preventDefault, but this holds even before a
     listener runs. */
  user-select: none; -webkit-user-select: none;
  -webkit-user-drag: none;
}
.stl-viewer.is-active { touch-action: none; }
.stl-viewer canvas {
  display: block; width: 100%; height: 100%;
  cursor: default;
  -webkit-user-drag: none;
}
.stl-viewer.is-active canvas { cursor: grab; }
.stl-viewer.is-active canvas:active { cursor: grabbing; }
.stl-viewer canvas:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.stl-viewer .stl-noscript { font-size: .9rem; color: var(--muted); padding: 1rem; text-align: center; }

/* The activation affordance, centred over a slightly dimmed model. */
.stl-activate {
  position: absolute; z-index: 2;
  font-size: .85rem; padding: .45rem .9rem;
  border-radius: 999px;
  background: var(--bg); color: var(--fg);
  border: 1px solid var(--border);
  box-shadow: 0 1px 4px rgba(0,0,0,.15);
  cursor: pointer;
}
.stl-viewer.is-active .stl-activate { display: none; }
/* Dim only while inert, so it is visible that the model is not yet live. */
.stl-viewer:not(.is-active)::after {
  content: ""; position: absolute; inset: 0; z-index: 1;
  background: rgba(127,127,127,.12);
  pointer-events: none;
}

/* Shown only while active: the way out must be visible, not guessed. */
.stl-hint {
  display: none;
  position: absolute; z-index: 2;
  left: .5rem; bottom: .5rem;
  font-size: .7rem; color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--border); border-radius: 4px;
  padding: .15rem .4rem;
  pointer-events: none;
}
.stl-viewer.is-active .stl-hint { display: block; }

.stl-download { font-size: .8rem; margin: .4rem 0 0; text-align: center; }
`

// templates are parsed once at startup. A parse failure is a programming error
// in an embedded constant, so it panics rather than being handled.
var templates = template.Must(template.New("").Parse(indexTmpl + editTmpl + previewTmpl))

// render writes a named template, reporting a failure as a 500.
//
// Note that a template error after the first byte has been written cannot
// produce a clean error page; the log line is what makes such a truncated
// response diagnosable.
func (s *server) render(w http.ResponseWriter, name string, data map[string]any) {
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	if err := templates.ExecuteTemplate(w, name, data); err != nil {
		log.Printf("render %s: %v", name, err)
	}
}

const indexTmpl = `
{{define "index"}}<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>blocks</title>
<link rel="stylesheet" href="/style.css">
</head>
<body>
<header class="top"><h1>blocks</h1></header>
<div class="wrap">
  <form class="newpost" method="post" action="/posts">
    <input name="title" placeholder="New post title" aria-label="New post title" required>
    <button type="submit">Create</button>
  </form>
  {{if .Posts}}
  <ul class="posts">
    {{range .Posts}}
    <li>
      <a href="/edit/{{.Slug}}">{{if .Title}}{{.Title}}{{else}}(untitled){{end}}</a>
      <span class="muted">{{.Status}}</span>
      <a class="muted" href="/posts/{{.Slug}}">preview</a>
    </li>
    {{end}}
  </ul>
  {{else}}
  <p class="muted">No posts yet.</p>
  {{end}}
</div>
</body>
</html>{{end}}
`

const editTmpl = `
{{define "edit"}}<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{.Post.Title}} — blocks</title>
<link rel="stylesheet" href="/style.css">
</head>
<body data-post-id="{{.Post.ID}}">
<header class="top">
  <h1><a href="/">blocks</a></h1>
  <span class="muted" id="status"></span>
  <a class="btn" href="/posts/{{.Post.Slug}}" target="_blank" rel="noopener">Preview</a>
</header>

<div class="dropzone">Drop to add a block</div>

<div class="wrap">
  <div class="post-meta">
    <input class="title" id="post-title" value="{{.Post.Title}}" aria-label="Title" placeholder="Title">
    <input id="post-subtitle" value="{{.Post.Subtitle}}" aria-label="Subtitle" placeholder="Subtitle">
  </div>

  <div id="blocks"></div>

  <div class="add-bar">
    <button data-add="markdown">+ Markdown</button>
    <button data-add="code">+ Code</button>
    <span class="muted">or drop an image / STL file anywhere</span>
  </div>
  <div class="errors" id="errors"></div>
</div>

<script type="application/json" id="initial-blocks">{{.BlocksJSON}}</script>
<script src="/static/editor.js"></script>
</body>
</html>{{end}}
`

const previewTmpl = `
{{define "preview"}}<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{.Post.Title}}</title>
<link rel="stylesheet" href="/style.css">
</head>
<body>
<article class="post">
  <h1>{{.Post.Title}}</h1>
  {{if .Post.Subtitle}}<p class="subtitle">{{.Post.Subtitle}}</p>{{end}}
  {{.Body}}
</article>
{{if .UsesSTL}}<script src="/static/stl-viewer.js"></script>{{end}}
</body>
</html>{{end}}
`