| 1 | package render |
| 2 | |
| 3 | import ( |
| 4 | "html/template" |
| 5 | "strings" |
| 6 | |
| 7 | "amadan.net/oficina/slides/internal/slides/deck" |
| 8 | ) |
| 9 | |
| 10 | // editing.go is the markup the editor's stage is served in |
| 11 | // (Options.Editing): the same slide, every text a block carries drawn the |
| 12 | // way ProseMirror will draw it once the editor binds its views to it |
| 13 | // (fixtures/stage.mjs). Paul, 2026-10-26: "No flashing, ever!" — and a |
| 14 | // layout that moves after the slide is shown counts. The font gate shows |
| 15 | // the editor's slide before the editor has mounted when the editor is |
| 16 | // late, and binding then replaces each text's markup with ProseMirror's. |
| 17 | // If the two differ by a line, the slide moves: a heading whose last |
| 18 | // paragraphs were empty grew by two lines when the editor arrived |
| 19 | // (Astra's finding 3 on #27), because a run of <br> drops its last line |
| 20 | // and ProseMirror gives every empty paragraph a line of its own. |
| 21 | // |
| 22 | // So this is ProseMirror's own output, from its schema and its DOM |
| 23 | // serialiser's rules, and not an approximation of it: |
| 24 | // |
| 25 | // - one element per paragraph, in the tag stage.mjs's paragraphTag |
| 26 | // gives it — <div> for a heading's lines, a code block's and a |
| 27 | // cell's, <li> for a list's, <p> otherwise — with nothing between |
| 28 | // them, because ProseMirror writes no whitespace and the editable |
| 29 | // node is white-space: pre-wrap; |
| 30 | // - an empty paragraph, and a text with no paragraphs at all (the |
| 31 | // schema's doc is paragraph+), is the element holding ProseMirror's |
| 32 | // trailing break; |
| 33 | // - marks nest in the schema's order (fixtures/schema.mjs: b, i, c, h, |
| 34 | // href — strong, em, code, mark, a), outermost first, and a mark |
| 35 | // stays open across runs that share it, as the serialiser keeps it; |
| 36 | // - a code block's lines carry their marks, as the editor shows them. |
| 37 | // |
| 38 | // The viewer, the projector and the API keep the renderer's own markup, |
| 39 | // which is what a reader is served; only the stage, which the editor is |
| 40 | // about to own, is drawn in the editor's. render_test.go holds this to |
| 41 | // what ProseMirror produces, and internal/slidestest's |
| 42 | // TestFontALateEditorMovesNothing to the pixels. |
| 43 | |
| 44 | // trailingBreak is what ProseMirror puts in an empty textblock so that it |
| 45 | // has a line. |
| 46 | const trailingBreak = `<br class="ProseMirror-trailingBreak">` |
| 47 | |
| 48 | // editingParagraphs is t as ProseMirror draws a doc of paragraphs, each |
| 49 | // in tag. |
| 50 | func editingParagraphs(tag string, t deck.Text) template.HTML { |
| 51 | if len(t) == 0 { |
| 52 | t = deck.Text{nil} |
| 53 | } |
| 54 | var b strings.Builder |
| 55 | for _, p := range t { |
| 56 | b.WriteString("<" + tag + ">") |
| 57 | body := editingInline(p) |
| 58 | if body == "" { |
| 59 | body = trailingBreak |
| 60 | } |
| 61 | b.WriteString(string(body)) |
| 62 | b.WriteString("</" + tag + ">") |
| 63 | } |
| 64 | return template.HTML(b.String()) |
| 65 | } |
| 66 | |
| 67 | // pmMark is one mark as ProseMirror's serialiser sees it: its rank in |
| 68 | // the schema, its tags, and what makes two of them the same mark. |
| 69 | type pmMark struct { |
| 70 | rank int |
| 71 | open, shut string |
| 72 | } |
| 73 | |
| 74 | // marksOf is r's marks in the schema's order. |
| 75 | func marksOf(r deck.Run) []pmMark { |
| 76 | var out []pmMark |
| 77 | if r.B { |
| 78 | out = append(out, pmMark{0, "<strong>", "</strong>"}) |
| 79 | } |
| 80 | if r.I { |
| 81 | out = append(out, pmMark{1, "<em>", "</em>"}) |
| 82 | } |
| 83 | if r.C { |
| 84 | out = append(out, pmMark{2, "<code>", "</code>"}) |
| 85 | } |
| 86 | if r.H { |
| 87 | out = append(out, pmMark{3, "<mark>", "</mark>"}) |
| 88 | } |
| 89 | if r.Href != "" { |
| 90 | // schema.mjs's href: a live anchor only for an allowed scheme, |
| 91 | // otherwise a span carrying the value. |
| 92 | if h := safeHref(r.Href); h != "" { |
| 93 | out = append(out, pmMark{4, `<a href="` + template.HTMLEscapeString(h) + `">`, "</a>"}) |
| 94 | } else { |
| 95 | out = append(out, pmMark{4, `<span data-href="` + template.HTMLEscapeString(r.Href) + `">`, "</span>"}) |
| 96 | } |
| 97 | } |
| 98 | return out |
| 99 | } |
| 100 | |
| 101 | // editingInline is one paragraph's runs the way ProseMirror's |
| 102 | // DOMSerializer writes them: a run reuses every open mark it shares, |
| 103 | // from the outside in, and closes the rest before opening its own. |
| 104 | func editingInline(runs []deck.Run) template.HTML { |
| 105 | var b strings.Builder |
| 106 | var open []pmMark |
| 107 | for _, r := range runs { |
| 108 | if r.T == "" { |
| 109 | continue |
| 110 | } |
| 111 | marks := marksOf(r) |
| 112 | keep := 0 |
| 113 | for keep < len(open) && keep < len(marks) && open[keep] == marks[keep] { |
| 114 | keep++ |
| 115 | } |
| 116 | for len(open) > keep { |
| 117 | b.WriteString(open[len(open)-1].shut) |
| 118 | open = open[:len(open)-1] |
| 119 | } |
| 120 | for _, m := range marks[keep:] { |
| 121 | b.WriteString(m.open) |
| 122 | open = append(open, m) |
| 123 | } |
| 124 | b.WriteString(template.HTMLEscapeString(r.T)) |
| 125 | } |
| 126 | for len(open) > 0 { |
| 127 | b.WriteString(open[len(open)-1].shut) |
| 128 | open = open[:len(open)-1] |
| 129 | } |
| 130 | return template.HTML(b.String()) |
| 131 | } |
| 132 | |