~mort/coffeepaste

ref: e586900f830a9374dd465c4c34086e5234843ca8 coffeepaste/web/view.html -rw-r--r-- 7.8 KiB
e586900fMartin Dørum fix line nums 1 year, 1 month ago
                                                                                
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
<html>
<head>
    <meta charset="utf-8">
    <title>Coffeepaste</title>
    <link rel="stylesheet" href="/vendor/highlight.css">
<style>
html, body, #container, #container pre {
    height: 100%;
}
#line-nums {
    min-height: 100%;
}
body, .hljs {
    background: #121212;
    color: #f5f2e0;
    font-size: 1.05em;
}
body {
    margin: 0px;
}
pre {
    margin: 0px;
    vertical-align: top;
    display: inline-block;
    white-space: pre;
}
#container {
    white-space: nowrap;
}
#line-nums, body {
    position: sticky;
    left: 0px;
}
#code, #line-nums {
    -webkit-text-size-adjust: none;
    text-size-adjust: none;
    line-height: 1.5;
    padding-top: 6px;
    overflow-x: initial;
    display: inline-block;
}
#code {
    position: relative;
    z-index: 20;
    background: none;
    padding-left: 5px;
}
#line-nums {
    z-index: 30;
    border-right: 1px solid #171717;
    background-color: #212121;
    color: #fffff0;
    padding-left: 0px;
    padding-right: 0px;
    box-sizing: border-box;
    user-select: none;
}
#line-nums .line-number {
    position: relative;
    z-index: 20;
    cursor: pointer;
    padding-left: 6px;
    padding-right: 7px;
    padding-top: 3px;
    padding-bottom: 3px;
}
#selection-box {
    z-index: 10;
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100%;
    background: rgba(16, 120, 255, 0.2);
    width: 100%;
    pointer-events: none;
}
#selection-box.hidden {
    display: none;
}
</style>
</head>
<body>
    <noscript>
        Sorry, code view currently requires JavaScript.
        Please remove the file extension from the URL to see a plain-text version.
    </noscript>

    <div class="hidden" id="selection-box"></div>
    <div id="container">
        <pre><code class="hljs" id="line-nums"></code></pre>
        <pre><code class="hljs" id="code"></code></pre>
    </div>

<script>
var code = document.getElementById("code");
var lineNums = document.getElementById("line-nums");
let selectionBox = document.getElementById("selection-box");

var parts = location.pathname.substr(1).split("/")[0].split(".");
var name = parts[0];
var ext = parts.length > 1 ? parts[1] : null;
let loaded = false;
let loadSuccess = false;
let highlighted = false;

document.title += " - " + name;

let selection = {
    start: null,
    startEl: null,
    end: null,
    endEl: null,
};

selectionBox.addEventListener("click", clearSelection);

if (location.hash.length > 1 && location.hash != "#_") {
    let hash = location.hash.substr(1);
    if (hash.indexOf("-") == -1) {
        selection.start = parseInt(hash);
    } else {
        let parts = hash.split("-");
        selection.start = parseInt(parts[0]);
        selection.end = parseInt(parts[1]);
    }
}

if (!name) {
    loaded = true;
    code.innerText = "Invalid URL.";
} else {
    doLoad(name);
}

// Show "loading" after a sec
setTimeout(function() {
    if (!loaded) {
        code.innerText = "Loading...";
    }
}, 1000);

// Highlight later if the source code content wins the loading race
document.addEventListener("DOMContentLoaded", function() {
    if (loaded && loadSuccess && !highlighted) {
        doHighlight(code.innerText);
        highlighted = true;
    }
});

function doLoad(name) {
    loaded = true;
    var xhr = new XMLHttpRequest();
    xhr.addEventListener("error", onError);
    xhr.addEventListener("abort", onAbort);
    xhr.addEventListener("load", onLoad);
    xhr.overrideMimeType("text/plain");
    xhr.open("GET", "/" + name);
    xhr.send();
}

function onError(evt) {
    console.error(evt);
    code.innerText = "Load error.";
}

function onAbort(evt) {
    loaded = true;
    console.error(evt);
    code.innerText = "Load aborted.";
}

function onLoad(evt) {
    loaded = true;
    var xhr = evt.target;
    if (xhr.status != 200) {
        code.innerText = xhr.status + " " + xhr.statusText;
        return;
    }

    loadSuccess = true;

    if (window.hljs) {
        doHighlight(xhr.responseText)
    } else {
        code.innerText = xhr.responseText;
    }

    addLineNumbers(xhr.responseText);
    scrollToSelection();
}

function doHighlight(text) {
    try {
        if (ext && ext != "auto") {
            let res = hljs.highlight(ext, text);
            if (res.illegal) {
                console.error("Illegal matches found for " + ext + ":", res.illegalBy.msg);
                console.error("Context:\n" + res.illegalBy.context);
            } else {
                code.innerHTML = res.value;
                highlighted = true;
            }
        }

        if (!highlighted) {
            let res = hljs.highlightAuto(text);
            console.info("Guessed language", res.language);
            if (res.illegal) {
                console.error("Illegal matches found for guessed language:", res.illegalBy.msg);
                console.error("Context:");
                console.error(res.illegalBy.context);
                code.innerText = text;
            } else {
                code.innerHTML = res.value;
                highlighted = true;
            }
        }
    } catch (err) {
        console.error("Failed to highlight for some reason:", err);
        code.innerText = text;
    }

    highlighted = true;
}

function onLineNumClick(evt) {
    let target = evt.target;
    let line = parseInt(target.getAttribute("data-number"));

    // Deselect
    if (selection.start == line && selection.end == null) {
        // Just setting location.hash to "" scrolls to the top of the screen
        if (window.history) {
            window.history.replaceState("", document.title, location.href.split("#")[0]);
        } else {
            location.hash = "_";
        }
        clearSelection();

    // Range select
    } else if (evt.shiftKey && selection.start != null && selection.startEl != null) {
        if (line < selection.start) {
            selection.start = line;
            selection.startEl = target;
        } else {
            selection.end = line;
            selection.endEl = target;
        }

        location.hash = selection.start + "-" + selection.end;
        updateSelection();

    // Line select
    } else {
        selection.start = line;
        selection.startEl = target;
        selection.end = null;
        selection.endEl = null;
        location.hash = line;
        updateSelection();
    }
}

function addLineNumbers(content) {
    lineNums.innerHTML = "";
    let num = 1;
    for (i in content.split("\n")) {
        let el = document.createElement("span");
        el.className = "line-number";
        el.innerText = num;
        el.setAttribute("data-number", num);
        lineNums.appendChild(el);
        lineNums.appendChild(document.createTextNode("\n"));

        el.addEventListener("click", onLineNumClick);

        if (num == selection.start) {
            selection.startEl = el;
        } else if (num == selection.end) {
            selection.endEl = el;
        }

        num += 1;
    }

    updateSelection();
}

function clearSelection() {
    selection.start = selection.startEl = selection.end = selection.endEl = null;
    selectionBox.className = "hidden";
}

function updateSelection() {
    if (selection.startEl == null) {
        clearSelection();
        return;
    }

    selectionBox.className = "";
    selectionBox.style.top = selection.startEl.offsetTop + "px";
    selectionBox.style.minWidth = code.offsetWidth + "px";

    if (selection.endEl == null) {
        selectionBox.style.height = selection.startEl.offsetHeight + "px";
    } else {
        selectionBox.style.height = (
            selection.endEl.offsetTop -
            selection.startEl.offsetTop +
            selection.endEl.offsetHeight) + "px";
    }
}

function scrollToSelection() {
    if (selection.startEl != null) {
        window.scroll(0, selection.startEl.offsetTop - selection.startEl.offsetHeight * 3);
    }
}
</script>

    <script src="/vendor/highlight.js"></script>
</body>
</html>