This repository was archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff-highlighter.html
More file actions
155 lines (132 loc) · 3.74 KB
/
diff-highlighter.html
File metadata and controls
155 lines (132 loc) · 3.74 KB
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
<!--
@license
Copyright (c) 2017 Preview-Code. All rights reserved.
This code may only be used under the BSD style license found in LICENSE.txt
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="highlight-import.html">
<link rel="import" href="highlight-styles.html">
<!--
Syntax highlighting for textual diffs.
@demo demo/index.html
-->
<dom-module id="diff-highlighter">
<template>
<style include="highlight-styles">
:host {
display: flex;
}
#beforeColumn, #afterColumn {
padding: 0 4px;
color: #888;
background-color: #eee;
font-family: monospace;
@apply(--line-number-columns);
}
#beforeColumn > span,
#afterColumn > span {
@apply(--line-numbers);
display: block;
text-align: center;
}
#codeColumn {
overflow-x: auto;
position: relative;
}
span {
line-height: var(--line-height, 24px);
height: var(--line-height, 24px);
}
pre {
overflow-x: auto;
line-height: var(--line-height, 24px);
margin: 0;
}
#diffColors {
width: 100%;
height: 100%;
float: right;
position: absolute;
top: 0;
pointer-events: none;
z-index: 0;
right: 0;
opacity: 0.2;
}
#diffColors div {
height: var(--line-height, 24px);
}
#diffColors .add {
background-color: var(--add-color, green);
}
#diffColors .del {
background-color: var(--add-color, red);
}
</style>
<div id="beforeColumn"></div>
<div id="afterColumn"></div>
<div id="codeColumn">
<pre><code id="code" class="javascript"></code></pre>
<div id="diffColors"></div>
</div>
</template>
<script>
Polymer({
is: 'diff-highlighter',
observers: [
'_onDiffChange(code, beforeStart, afterStart)'
],
properties: {
code: String,
language: String,
beforeStart: Number,
afterStart: Number,
},
_onDiffChange: function(code, beforeStart, afterStart) {
var lines = code.split('\n');
// The first and last line may be empty.
if (code.charAt(0) === '\n') {
lines.shift();
}
if (code.charAt(code.length-1) === '\n') {
lines.pop();
}
var before = this.beforeStart;
var after = this.afterStart;
var createSpan = function(content) {
return '<span>' + content + '</span>';
}
var beforeContent = '';
var afterContent = '';
var strippedCode = [];
var diffMarks = [];
lines.forEach(function(line) {
beforeContent += createSpan(line.startsWith('+') ? '' : before++);
afterContent += createSpan(line.startsWith('-') ? '' : after++);
strippedCode.push(line.substring(1));
diffMarks.push(line.charAt(0));
});
strippedCode = strippedCode.reduce(function(acc, line) {
return acc + '\n' + line;
});
Polymer.dom(this.$.beforeColumn).innerHTML = beforeContent;
Polymer.dom(this.$.afterColumn).innerHTML = afterContent;
Polymer.dom(this.$.code).innerHTML = hljs.highlightAuto(strippedCode).value;
this._updateDiffLines(diffMarks);
},
_updateDiffLines: function(marks) {
var createDiv = function(cls, content) {
return '<div class="' + cls + '">' + content + '</div>';
}
var markClass = function(mark) {
return mark === '+' ? 'add' : mark === '-' ? 'del' : 'normal';
}
var content = ''
marks.forEach(function(mark) {
content += createDiv(markClass(mark), mark);
});
Polymer.dom(this.$.diffColors).innerHTML = content;
}
});
</script>
</dom-module>