-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
executable file
·259 lines (214 loc) · 8.39 KB
/
index.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
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
(function(){
var mask;
var maskRect;
var info;
var infoRect;
var code;
var currentTemplatePath;
var currentLineIndex;
var offsetCount = 5;
var port = 8493;
var protocol = window.location.protocol;
var timeId;
function request(url,cb){
var xmlHttpReq = new XMLHttpRequest();
xmlHttpReq.open('GET', url);
xmlHttpReq.send(null);
xmlHttpReq.onreadystatechange = function () {
if(xmlHttpReq.readyState === 4){
cb && cb(xmlHttpReq.responseText);
}
}
}
function fectchContent(path,type){
var url = chrome.extension.getURL(path);
request(url,function(content){
var s = document.createElement(type);
s.innerHTML = content;
document.getElementsByTagName('head')[0].appendChild(s);
});
}
fectchContent('./css/index.css','style');
fectchContent('./css/highlight.css','style');
function getCodeContent (contentList , viewLineCount, viewLineLastCount) {
if (!contentList) return '';
var dCount = 0;
var startCount, endCount;
startCount = Math.max(1, viewLineCount - offsetCount);
endCount = startCount + offsetCount * 2 - 1;
var codeArr = [];
for (var i = 0; i < contentList.length; i++) {
var lineCount = i + 1;
var newCodeLine;
var lineText;
var lineElement = document.createElement('p');
if (lineCount >= startCount && lineCount <= endCount){
lineText = '<span class="vfinder-line-count">' + lineCount + ':</span>' + contentList[i];
// 当前行
if (lineCount >= viewLineCount && (viewLineLastCount == null || lineCount <= viewLineLastCount)) {
lineElement.className = 'vfinder-current-code-line';
}
lineElement.innerHTML = lineText;
codeArr.push('<pre><code>' + lineElement.outerHTML + '</code></pre>');
}
}
return codeArr.join('');
}
function setCode (path, linkElement) {
if (!code) {
code = document.createElement('div');
code.className = 'vfinder-code';
document.body.appendChild(code);
}
code.style.display = 'block';
request(protocol + '//localhost:' + port + '?view=' + encodeURIComponent(path) + '&count=' + (offsetCount * 2), function(data) {
var contentList;
var codeRect;
var viewLineCount;
var codeContent;
var lastIndex = linkElement.getAttribute('data-last-index');
var codeElement = document.createElement('div');
codeElement.className = 'vfinder-code-content';
var dataType = linkElement.getAttribute('data-type');
if (data) {
data = JSON.parse(data);
contentList = data.contentList;
viewLineCount = data.viewLineCount;
}
infoRect = info.getBoundingClientRect();
// 显示的代码片段内容
codeContent = getCodeContent(contentList , viewLineCount, lastIndex === '' ? null : Number(lastIndex) + 1);
codeElement.innerHTML = codeContent;
code.innerHTML = `<div class="vfinder-path"><span>${path}</span></div>`;
code.appendChild(codeElement);
codeRect = code.getBoundingClientRect();
var linkRect = linkElement.getBoundingClientRect();
code.style.left = infoRect.left + (infoRect.width - codeRect.width) / 2 + 'px';
if (infoRect.right + codeRect.width > window.innerWidth) {
code.style.left = infoRect.left - codeRect.width + 20 + 'px';
}
else {
code.style.left = infoRect.left + infoRect.width - 20 + 'px';
}
if (infoRect.top + codeRect.height > window.innerHeight) {
code.style.top = window.innerHeight - codeRect.height - 20 + 'px';
}
else {
code.style.top = linkRect.top - 30 + 'px';
}
});
}
function setMask (target) {
var windowWidth = window.innerWidth;
var windowHeight = window.innerHeight;
var top;
var left;
if (!mask) {
mask = document.createElement('div');
mask.className = 'vfinder-mask';
document.body.appendChild(mask);
}
maskRect = target.getBoundingClientRect();
mask.style.display = 'block';
mask.style.left = maskRect.left + 'px';
mask.style.top = maskRect.top + 'px';
mask.style.width = maskRect.width + 'px';
mask.style.height = maskRect.height + 'px';
if (!info) {
info = document.createElement('div');
info.className = 'vfinder-info';
bindInfoClick(info);
bindInfoHover(info);
document.body.appendChild(info);
}
info.style.display = 'block';
info.innerHTML = createSingleLink('template', target)
+ createSingleLink('script', target)
+ createSingleLink('style', target)
+ createSingleLink('created', target, currentLineIndex);
infoRect = info.getBoundingClientRect();
// 超出右边界
if (maskRect.right + infoRect.width > windowWidth) {
info.style.right = 20 + 'px';
info.style.left = 'auto';
}
else {
info.style.left = maskRect.right - Math.min(30, (maskRect.width * 0.1)) + 'px';
info.style.right = 'auto';
}
if (infoRect.height > maskRect.height) {
top = maskRect.top + ((maskRect.height - infoRect.height) / 2);
}
else {
top = maskRect.top + (Math.min(30, maskRect.height * 0.1));
}
if (top + infoRect.height > windowHeight) {
top = windowHeight - infoRect.height - 10;
}
else if (top < 0) {
top = 10;
}
info.style.top = top + 'px';
}
function bindInfoHover (info) {
info.addEventListener('mouseover', function(e){
var target = e.target;
if (target.tagName === 'A') {
setCode(target.getAttribute("data-path"), target);
}
});
}
function createSingleLink (name, target) {
var path = target.getAttribute('data-' + name + '-path');
var lineIndex;
if (!path) {
return '';
}
if (name === 'created') {
lineIndex = target.getAttribute('data-created-index');
}
var lastLineIndex = target.getAttribute('data-' + name + '-last-index');
path = lineIndex == null ? path : path + ':' + (Number(lineIndex) + 1);
var fileName = path.replace(/^.*[\\\/]/, '');
return `<div><span class="vfinder-info-name-${name}">${name}:</span><a href="javascript:" data-type="${name}" data-last-index="${lastLineIndex}" data-path="${path}">${fileName}</a></div>`;
}
function bindInfoClick (info) {
info.addEventListener('mousedown', function(e){
var target = e.target;
var path = target.getAttribute('data-path');
if (path) {
request(protocol + '//localhost:8493?open=' + encodeURIComponent(path));
}
});
}
function getComponentElement (target) {
if (!target) return;
while(target && target.getAttribute) {
var templatePath = target.getAttribute('data-template-path');
var parentTemplatePath = target.getAttribute('data-parent-template-path');
if (templatePath || parentTemplatePath) {
currentTemplatePath = templatePath;
return target;
}
target = target.parentNode;
}
}
document.addEventListener('mouseover',function(e){
var target = e.target;
if ((info && info.contains(target)) || (code && code.contains(target))) {
return;
}
if (code) {
code.style.display = 'none';
}
target = getComponentElement(target);
if (!target) {
if (mask) {
currentTemplatePath = null;
mask.style.display = info.style.display = 'none';
}
return;
}
setMask(target);
});
})();