-
Notifications
You must be signed in to change notification settings - Fork 90
/
component.js
180 lines (143 loc) · 5.25 KB
/
component.js
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
import { computed } from '@ember/object';
import Component from '@ember/component';
import { wait } from 'ember-animated';
import move from 'ember-animated/motions/move';
import { fadeIn, fadeOut } from 'ember-animated/motions/opacity';
import { highlightCode } from 'ember-cli-addon-docs/utils/compile-markdown';
import { htmlSafe } from '@ember/string';
export default Component.extend({
init() {
this._super(...arguments);
this.codeTransition = this.codeTransition.bind(this);
},
onAnimationChange() {},
originalLines: linesFromDiff('diff', 'before'),
finalLines: linesFromDiff('diff', 'after'),
activeLines: computed('isShowingFinal', function() {
return this.isShowingFinal ? this.finalLines : this.originalLines;
}),
codeTransition: function*({ duration, insertedSprites, removedSprites, keptSprites }) {
this.incrementProperty('transitionsRunning');
this.set('isAnimatingInsertedLines', false);
if (this.isShowingFinal) {
removedSprites.forEach(fadeOut);
// Need to set inserted sprites to 0 opacity in case their animation is interrupted
insertedSprites.forEach(sprite => {
sprite.applyStyles({
opacity: '0'
});
});
keptSprites.map(sprite => {
fadeIn(sprite);
move(sprite);
});
yield wait(duration);
while (this.animationPaused) {
yield wait(100);
}
// this.set('isAnimatingInsertedLines', true);
this.onAnimationChange(true);
for (let sprite of insertedSprites) {
sprite.moveToFinalPosition();
sprite.applyStyles({
overflow: 'hidden',
opacity: '1',
display: 'inline-block',
width: 'auto'
});
let totalWidth = sprite.element.getBoundingClientRect().width;
let chars = sprite.element.textContent;
let characterWidth = totalWidth / chars.length;
sprite.reveal();
for (var i = 0; i < chars.length; i++) {
sprite.applyStyles({
width: characterWidth * (i + 1)
});
if (chars[i] !== " ") {
yield wait(15);
}
}
}
// this.set('isAnimatingInsertedLines', false);
this.onAnimationChange(false);
} else {
removedSprites.forEach(fadeOut);
keptSprites.map(sprite => {
fadeIn(sprite);
move(sprite);
});
insertedSprites.forEach(fadeIn);
}
this.decrementProperty('transitionsRunning');
},
});
function linesFromDiff(diffProperty, beforeOrAfter) {
return computed(function() {
let lineObjects = getLineObjectsFromDiff(this[diffProperty], beforeOrAfter);
let language = this.label.substr(this.label.lastIndexOf('.') + 1);
return highlightLineObjects(lineObjects, language);
});
}
function highlightLineObjects(lineObjects, language) {
let code = lineObjects.map(lineObject => lineObject.text).join('\n');
let highlightedCode = highlightCode(code, language);
return highlightedCode.split('\n').map((text, index) => ({
id: lineObjects[index].id,
highlighted: lineObjects[index].highlighted,
// htmlSafe is justified here because we generated the highlighting markup
// ourself in highlightCode
text: htmlSafe(text === "" ? "\n" : text),
}));
}
function getLineObjectsFromDiff(diff, beforeOrAfter) {
let diffLines = diff.split('\n');
let lineObjects = diffLines.map((diff, index) => {
return {
index,
text: diff
};
});
let { keptLines, addedLines, removedLines } = groupedLines(lineObjects);
let lines;
if (beforeOrAfter === 'before') {
lines = keptLines.concat(removedLines).sort((a, b) => a.index - b.index);
} else if (beforeOrAfter === 'after') {
lines = keptLines.concat(addedLines).sort((a, b) => a.index - b.index);
}
return lines;
}
function groupedLines(lineObjects) {
let isAddedLine = lineObject => lineObject.text.indexOf('+') === 0;
let isRemovedLine = lineObject => lineObject.text.indexOf('-') === 0;
let isModifiedLine = lineObject => isAddedLine(lineObject) || isRemovedLine(lineObject);
let hasAddedOrRemovedLines = lineObjects.filter(isModifiedLine).length > 0;
return lineObjects.map((lineObject, index) => {
if (isAddedLine(lineObject)) {
lineObject.id = `added-${index}`;
lineObject.text = lineObject.text.replace('+', ' ');
lineObject.highlighted = true;
} else if (isRemovedLine(lineObject)) {
lineObject.id = `removed-${index}`;
lineObject.text = lineObject.text.replace('-', ' ');
// .replace(/^\s\s/, ""); // remove the 2-space indent
} else {
lineObject.id = `kept-${index}`;
}
return lineObject;
}).map(lineObject => {
/*
If we have either addded or removed lines, all text has a 2-space indent
right now, so we remove it.
If we don't, we don't need to dedent anything, because all space was
dedented by the `dedent` function when the diff was originally passed in.
*/
if (hasAddedOrRemovedLines) {
lineObject.text = lineObject.text.replace(/^\s\s/, "");
}
return lineObject;
}).reduce((groupedLines, lineObject) => {
let type = lineObject.id.split('-')[0];
groupedLines[`${type}Lines`].push(lineObject);
return groupedLines;
}, { keptLines: [], removedLines: [], addedLines: []});
}