| @@ -0,0 +1,71 @@ |
| 1 | +/// The markup of a streamed page (ADR 0066's `async`, toboggan's `htms`): |
| 2 | +/// a page goes out with a placeholder where each slow part will go, then |
| 3 | +/// each part as it finishes, as a `<template>` and a `<swap-in>` marker |
| 4 | +/// that moves the template's content into its placeholder, then the end |
| 5 | +/// of the document. One inline script in the head defines the marker. |
| 6 | + |
| 7 | +import std/html.{type Html, Attribute, Node} |
| 8 | +import std/list |
| 9 | +import std/string |
| 10 | + |
| 11 | +/// `skeleton` shown until the part `id` arrives: its root element gets the |
| 12 | +/// id and `aria-busy`; anything else is wrapped in a `<div>` that has them. |
| 13 | +pub fn placeholder :: id: String skeleton: Html -> Html |
| 14 | +{ |
| 15 | + let marks = [Attribute("id", id), Attribute("aria-busy", "true")] |
| 16 | + match skeleton |
| 17 | + { Node(tag, attributes, children) -> |
| 18 | + Node(tag, list.append attributes marks, children) |
| 19 | + , _ -> Node("div", marks, [skeleton]) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +/// The part `id`, rendered: its content in a template, then the marker |
| 24 | +/// that swaps it in when the browser reads it. |
| 25 | +pub fn part :: id: String content: String -> String |
| 26 | +{ |
| 27 | + "<template>{content}</template><swap-in for=\"{html.escape id}\"></swap-in>" |
| 28 | +} |
| 29 | + |
| 30 | +/// The script that defines `<swap-in>`, allowed by `nonce`. It replaces |
| 31 | +/// the placeholder with the template just before the marker, removes |
| 32 | +/// both, and announces each new element with a `swap-in:swapped` event |
| 33 | +/// (htmx can process it there). |
| 34 | +pub fn script :: nonce: String -> String |
| 35 | +{ |
| 36 | + "<script nonce=\"{html.escape nonce}\">{behaviour ()}</script>" |
| 37 | +} |
| 38 | + |
| 39 | +fn behaviour :: -> String |
| 40 | +{ |
| 41 | + string.join |
| 42 | + [ "customElements.define(\"swap-in\",class extends HTMLElement\{connectedCallback()\{" |
| 43 | + , "const t=this.previousElementSibling,s=document.getElementById(this.getAttribute(\"for\"));" |
| 44 | + , "if(s&&t instanceof HTMLTemplateElement)\{const n=[...t.content.children];s.replaceWith(t.content);" |
| 45 | + , "for(const e of n)e.dispatchEvent(new CustomEvent(\"swap-in:swapped\",\{bubbles:true\}))\}" |
| 46 | + , "if(t instanceof HTMLTemplateElement)t.remove();this.remove()\}\})" |
| 47 | + ] |
| 48 | + "" |
| 49 | +} |
| 50 | + |
| 51 | +/// A whole page opened for streaming: up to its last `</body>`, with the |
| 52 | +/// script at the end of its head, and the rest, to send last. `None` when |
| 53 | +/// the page has no head or no body end to split at. |
| 54 | +pub fn opened :: page: String nonce: String -> Option<(String, String)> |
| 55 | +{ |
| 56 | + match string.split page "</head>" |
| 57 | + { [before, after, ..more] -> |
| 58 | + { |
| 59 | + let tail = string.join [after, ..more] "</head>" |
| 60 | + match list.reverse (string.split tail "</body>") |
| 61 | + { [last, kept, ..earlier] -> |
| 62 | + { |
| 63 | + let body = string.join (list.reverse [kept, ..earlier]) "</body>" |
| 64 | + Some((before <> script nonce <> "</head>" <> body, "</body>" <> last)) |
| 65 | + } |
| 66 | + , _ -> None |
| 67 | + } |
| 68 | + } |
| 69 | + , _ -> None |
| 70 | + } |
| 71 | +} |