Skip to content

Commit d5ce7a6

Browse files
authored
Merge pull request #34 from Odrin/feature/line-link
GitHub-like line links
2 parents ad56321 + 781190e commit d5ce7a6

File tree

6 files changed

+178
-108
lines changed

6 files changed

+178
-108
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ jspm_packages
4040
.vscode
4141
*.js
4242
!template/**/*.js
43+
!plugins/**/assets/*.js
4344
*.js.map
4445
*.d.ts
4546
gh-pages
46-
build/*
47+
build/*
48+
/.idea

lib/utility/html.ts

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,11 @@ import {
55

66
import { LIST, NON_NULL } from './introspection';
77

8-
const EM_SIZE = 14;
9-
10-
function htmlLines(html: string): number {
11-
12-
let count = 0;
13-
let position = 0;
14-
15-
while (position !== -1) {
16-
position = html.indexOf('</li><li>', position + 1);
17-
count++;
18-
}
19-
20-
return count;
21-
};
22-
23-
function padding(html: string): string {
24-
25-
const lines = htmlLines(html);
26-
const orderOfMagnitude = lines.toString().length;
27-
28-
return (orderOfMagnitude * EM_SIZE + EM_SIZE).toString() + 'px';
29-
};
30-
318
export class HTML {
9+
index = 1;
3210

3311
code(code: string): string {
34-
return `<code class="highlight"><ul class="code" style="padding-left:${padding(code)}">${code}</ul></code>`;
12+
return `<code class="highlight"><table class="code"><tbody>${code}</tbody></table></code>`;
3513
}
3614

3715
highlight(text: string): string {
@@ -42,8 +20,9 @@ export class HTML {
4220
return ` <sup>${text}</sup>`;
4321
}
4422

45-
line(code: string): string {
46-
return `<li>${code}</li>`;
23+
line(code?: string): string {
24+
const row = this.index++;
25+
return `<tr class="row"><td id="L${row}" class="td-index">${row}</td><td id="LC${row}" class="td-code">${code || ''}</td></tr>`;
4726
}
4827

4928
tab(code: string): string {
@@ -103,9 +82,6 @@ export class HTML {
10382
}
10483
}
10584

106-
107-
export const html = new HTML;
108-
10985
export function split(text: string, len: number): string[] {
11086

11187
return text

lib/utility/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export {
2-
html,
2+
HTML,
33
split
44
} from './html';
55

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
.code {
2-
background-color: #f6f6f6;
2+
background-color: #fff;
33
color: #4D4D4C;
44
border: 1px solid #4D4D4C;
55
font-size: 14px;
66
font-family: 'Ubuntu Mono';
7-
cursor: text;
87
list-style-type: decimal;
98
border-radius: 0.25rem;
109
}
@@ -19,13 +18,28 @@
1918
background: #f6f6f6
2019
}
2120

22-
.code li {
23-
min-height: 1em;
24-
background: #FFF;
21+
.code .row:hover {
22+
background: #EFEFEF;
23+
}
24+
25+
.code .td-index {
26+
color: rgba(27, 31, 35, .3);
27+
padding: 1px 8px 1px 16px;
28+
text-align: right;
29+
width: 1%;
30+
}
31+
32+
.code .td-index:hover {
33+
cursor: pointer;
34+
color: rgba(27, 31, 35, .6);
35+
}
36+
37+
.code .td-code {
2538
padding: 1px 8px;
2639
}
27-
.code li:hover {
28-
background: #EFEFEF;
40+
41+
.code .td-code.highlighted {
42+
background-color: #fffbdd;
2943
}
3044

3145
.code .tab {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
(function () {
2+
ready(function () {
3+
var tables = window.document.getElementsByClassName('code');
4+
5+
for (var i = 0; i < tables.length; i++) {
6+
var table = tables[i];
7+
8+
table.addEventListener('click', onClick);
9+
}
10+
11+
window.addEventListener("hashchange", onHashChange, false);
12+
13+
onHashChange();
14+
});
15+
16+
function onHashChange() {
17+
var id = window.location.href.split('#')[1];
18+
19+
if (!id) {
20+
return;
21+
}
22+
23+
var lcid = id.indexOf('C') === -1 ? id.replace('L', 'LC') : id;
24+
var lineCode = window.document.getElementById(lcid);
25+
26+
if (!lineCode) {
27+
return;
28+
}
29+
30+
var highlighted = lineCode.closest('.code').getElementsByClassName('highlighted');
31+
32+
for (var i = 0; i < highlighted.length; i++) {
33+
highlighted[i].classList.remove('highlighted');
34+
}
35+
36+
lineCode.classList.add('highlighted');
37+
}
38+
39+
function onClick(e) {
40+
var target = e.target;
41+
42+
if (!target.classList.contains('td-index')) {
43+
return;
44+
}
45+
46+
var href = window.location.href.split('#')[0];
47+
48+
window.location.href = href + '#' + target.id;
49+
}
50+
51+
function ready(fn) {
52+
if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading"){
53+
fn();
54+
} else {
55+
document.addEventListener('DOMContentLoaded', fn);
56+
}
57+
}
58+
})();
59+
60+
// https://developer.mozilla.org/ru/docs/Web/API/Element/closest
61+
(function(e){
62+
e.closest = e.closest || function(css){
63+
var node = this;
64+
65+
while (node) {
66+
if (node.matches(css)) return node;
67+
else node = node.parentElement;
68+
}
69+
return null;
70+
}
71+
})(Element.prototype);

0 commit comments

Comments
 (0)