Sources/OrgLive/App.swift

5b84d21562a93074b3eb63a63a26491eec85696a
org-live/Sources/OrgLive/App.swift history · blame · raw

115 lines · 4199 bytes

8 symbols in this file
  1import ElementaryUI
  2import JavaScriptKit
  3import OrgHighlight
  4import OrgSwift
  5
  6@main
  7struct App {
  8    static func main() {
  9        let app = Application(Editor())
 10        app.mount(in: .body)
 11    }
 12}
 13
 14@View
 15struct Editor {
 16    var body: some View {
 17        form(.autocomplete(.on)) {
 18            div(.class("toolbar")) {
 19                label(.custom(name: "for", value: "editor")) { "org-live" }
 20                span(.class("spacer")) {}
 21                button(.type(.button)) { "Save HTML" }
 22                    .onClick { save(renderHTML(editorValue()), extension: "html") }
 23                button(.type(.button)) { "Save Org" }
 24                    .onClick { save(editorValue(), extension: "org") }
 25            }
 26            div(.id("source")) {
 27                div(.class("layers")) {
 28                    div(.id("highlight"), .custom(name: "aria-hidden", value: "true")) {}
 29                    textarea(
 30                        .id("editor"),
 31                        .name("editor"),
 32                        .custom(name: "autocapitalize", value: "on"),
 33                        .custom(name: "autocorrect", value: "on"),
 34                        .custom(name: "spellcheck", value: "true"),
 35                        .placeholder("* Lorem Ipsum"),
 36                        .cols(80),
 37                        .wrap(.hard),
 38                        .autofocus,
 39                        .required
 40                    ) {}
 41                    .onInput { event in update(event.targetValue ?? "") }
 42                    .onAppear {
 43                        let editor = document.getElementById!("editor").object!
 44                        editor.value = .string(sample)
 45                        _ = editor.focus!()
 46                        _ = editor.setSelectionRange!(0, 0)
 47                        update(sample)
 48                        document.getElementById!("source").object!.scrollTop = 0
 49                    }
 50                }
 51            }
 52        }
 53        div(.id("preview")) {}
 54    }
 55}
 56
 57private let document = JSObject.global.document.object!
 58private var preview: JSObject { document.getElementById!("preview").object! }
 59private var highlight: JSObject { document.getElementById!("highlight").object! }
 60
 61private func editorValue() -> String {
 62    document.getElementById!("editor").value.string ?? ""
 63}
 64
 65/// Lines currently in #highlight, so an edit only replaces the lines that changed.
 66private var shownLines: [String] = []
 67
 68private func update(_ org: String) {
 69    updateHighlight(OrgHighlighter.lines(org))
 70    preview.innerHTML = .string(renderHTML(org))
 71}
 72
 73private func updateHighlight(_ lines: [String]) {
 74    var start = 0
 75    while start < lines.count && start < shownLines.count && lines[start] == shownLines[start] { start += 1 }
 76    var newEnd = lines.count
 77    var oldEnd = shownLines.count
 78    while newEnd > start && oldEnd > start && lines[newEnd - 1] == shownLines[oldEnd - 1] {
 79        newEnd -= 1
 80        oldEnd -= 1
 81    }
 82    shownLines = lines
 83    let element = highlight
 84    let children = element.children.object!
 85    if oldEnd > start {
 86        let range = document.createRange!().object!
 87        _ = range.setStartBefore!(children[start])
 88        _ = range.setEndAfter!(children[oldEnd - 1])
 89        _ = range.deleteContents!()
 90    }
 91    guard newEnd > start else { return }
 92    let html = JSValue.string(lines[start..<newEnd].joined())
 93    if start < Int(children.length.number!) {
 94        _ = children[start].object!.insertAdjacentHTML!("beforebegin", html)
 95    } else {
 96        _ = element.insertAdjacentHTML!("beforeend", html)
 97    }
 98}
 99
100private func renderHTML(_ org: String) -> String {
101    var options = OrgRenderOptions()
102    options.headingLevelOffset = 1
103    return OrgRenderer.renderToHTML(org, options: options)
104}
105
106private func save(_ data: String, extension ext: String) {
107    let filename = JSObject.global.Date.object!.new().toISOString!().string!
108    let blob = JSObject.global.Blob.object!.new([data])
109    let url = JSObject.global.URL.object!.createObjectURL!(blob)
110    let a = document.createElement!("a").object!
111    a.href = url
112    a.download = .string("\(filename).\(ext)")
113    _ = a.click!()
114    _ = JSObject.global.URL.object!.revokeObjectURL!(url)
115}