-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab-board-descriptions.user.js
403 lines (342 loc) · 13.1 KB
/
gitlab-board-descriptions.user.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// ==UserScript==
// @name GitLab board descriptions
// @namespace https://github.com/Danamir/gitlab-userscripts/
// @version 1.3
// @description Display issues description in GiLab issues board
// @author Danamir
// @match http*://*/*/boards
// @match http*://*/*/boards?*
// @match http*://*/*/boards/*
// @require https://code.jquery.com/jquery-3.4.1.min.js
// ==/UserScript==
/**
* Userscript to display issues description preview in GitLab issues board.
*
* Usage:
* - Click the descriptions button to toggle the display.
*
* Notes:
* - Limited to the dislayed issues on first load.
*/
// configuration
var description_font_size = ".90em";
var description_height = "12em";
var description_markdown_quick_render = true;
var description_ignores = [
/^\s*(discussion|cf\.?)\s*:\s*#.*$/i
];
var description_stoppers = [
/^.{0,3}\(from redmine: .*\).{0,3}$/
];
// local variables
var project_id;
/**
* Get URL parameters.
* @param param Parameter name.
* @returns {*}
*/
function $_GET(param) {
var vars = {};
window.location.href.replace( location.hash, '' ).replace(
/[?&]+([^=&]+)=?([^&]*)?/gi, // regexp
function( m, key, value ) { // callback
vars[key] = value !== undefined ? value : '';
}
);
if ( param ) {
return vars[param] ? vars[param] : null;
}
return vars;
}
/**
* Get project id from the page.
* @returns {*|jQuery}
*/
function get_project_id() {
return $('#search_project_id').prop('value');
}
/**
* Get all displayed issues ids.
* @param board The board to scan. (default = main board)
* @returns {Array}
*/
function get_issue_ids(board) {
if (!board || board.length === 0) {
board = $('#main-list'); // gitlab-swimlanes compatibility
if (board.length === 0) {
board = $('.boards-list');
}
}
var iids = [];
$('.board-card-number', board).each(function () {
var id = $(this).text().trim().replace(/^#/, '');
if ($.inArray(id, iids) === -1) {
iids.push(id);
}
});
return iids;
}
/**
* Fetch descriptions
* @param iids The issues ids. (default = all visible ids)
*/
function refresh_descriptions(iids) {
if (!project_id) {
console.log("Project id not found.");
return;
}
if (!iids) {
iids = get_issue_ids();
}
var page_iids = iids.splice(0, 100);
while(page_iids.length > 0) {
$.ajax({
url: "/api/v4/projects/" + project_id + "/issues",
data: {
per_page: 100,
iids: page_iids
},
type: "GET",
success: function (data) {
var issues = {};
$.each(data, function () {
var issue = this;
if (!issue || !issue.description) {
return true; // continue
}
var lines = [];
var lines_full = [];
var stop = false;
var skip = false;
$.each(issue.description.split("\n"), function () {
var line = this;
// ignore lines
$.each(description_ignores, function () {
var ignore = this;
if (ignore.exec(line.trim())) {
skip = true;
return false; // break
}
});
// stop description scanning
$.each(description_stoppers, function () {
var stopper = this;
if (stopper.exec(line.trim())) {
stop = true;
return false; // break
}
});
lines_full.push(line);
if (skip) {
return true; // continue
} else if (stop) {
return false; // break
}
lines.push(line);
});
if (lines_full.length > 0 && lines.length === 0) {
lines.push(" ");
}
if (lines.length > 0) {
issue.description = lines.join("\n");
issue.description_full = lines_full.join("\n");
issues[issue['iid']] = issue;
}
});
$('.btn-display-board-descriptions').removeClass("disabled");
display_descriptions(issues);
}
});
page_iids = iids.splice(0, 100);
}
}
/**
* Display descriptions in board(s).
* @param issues
*/
function display_descriptions(issues) {
$('.boards-list').each(function () {
var board = $(this);
$('.board-card', board).each(function () {
var card = $(this);
var header = $('.board-card-header', card);
var id = $('.board-card-number', card).text().trim().replace(/^#/, '');
if(issues[id] && issues[id]['description']) {
var description = issues[id]['description'].trim();
var description_full = issues[id]['description_full'].trim();
if ($('.board-card-body', card).length === 0) {
header.after('<div class="board-card-body"><div class="board-card-description"></div><div class="board-card-description-full"></div></div>');
}
if (description_markdown_quick_render) {
if (description_full === description) {
description = markdown_quick_render(description);
description_full = description;
} else {
description = markdown_quick_render(description);
description_full = markdown_quick_render(description_full);
}
}
$('.board-card-description', card).html(description);
$('.board-card-description-full', card).html(description_full);
}
});
});
$('.board-card').on("click", function (e) {
var card = $(this);
var card_description = $('.board-card-description-full', card);
var has_description = card_description.length > 0;
setTimeout(function () {
var block_description = $('.js-issuable-update .block.description');
if (!has_description) {
if (block_description.length > 0) {
block_description.remove();
}
} else {
if (block_description.length === 0) {
block_description = '<div class="block description"><div class="title">Description</div><div class="value">' + card_description.html() + '</div></div>';
var block_subscriptions = $('.js-issuable-update .block.subscriptions');
if (block_subscriptions.length >= 0) {
block_subscriptions.before(block_description);
} else {
$('.js-issuable-update div').last().after(block_description);
}
}
$('.value', block_description).html(card_description.html());
}
}, 10);
});
show_descriptions();
}
/**
* Show descriptions.
*/
function show_descriptions() {
$('.board-card-description').each(function () {
$(this).css({display: ""});
});
}
/**
* Display descriptions.
*/
function hide_descriptions() {
$('.board-card-description').each(function () {
$(this).css({display: "none"});
});
}
/**
* Minimal markdown renderer.
* @param text
* @return {string}
*/
function markdown_quick_render(text) {
if (!text) {
return text;
}
var markdown = text;
// html
markdown = markdown.replace(/</g, '<'); // <
markdown = markdown.replace(/>/g, '>'); // >
// code
markdown = markdown.replace(/```(\S*)\s*\n([\s\S]*)\n\s*```/g, '<pre class="code code-$1">$2</pre>'); // ```
markdown = markdown.replace(/`([^`]*)`/g, '<code class="code">$1</code>'); // `
// styles
markdown = markdown.replace(/(^|[\s,.:;]+)\*{3}([^*\n]+)\*{3}([\s,.:;]+|$)/g, '$1<i><b>$2</b></i>$3'); // ***
markdown = markdown.replace(/(^|[\s,.:;]+)\*{2}([^*\n]+)\*{2}([\s,.:;]+|$)/g, '$1<b>$2</b>$3'); // **
markdown = markdown.replace(/(^|[\s,.:;]+)\*{1}([^*\n]+)\*{1}([\s,.:;]+|$)/g, '$1<i>$2</i>$3'); // *
// markdown = markdown.replace(/(^|\s+)_{1}([^_\n]+)_{1}(\s+|$)/g, '$1<span style="text-decoration: underline;">$2</span>$3'); // underline markdown is not supported in GitLab
markdown = markdown.replace(/(^|[\s,.:;]+)_{3}([^_\n]+)_{3}([\s,.:;]+|$)/g, '$1<i><b>$2</b></i>$3'); // ___
markdown = markdown.replace(/(^|[\s,.:;]+)_{2}([^_\n]+)_{2}([\s,.:;]+|$)/g, '$1<b>$2</b>$3'); // __
markdown = markdown.replace(/(^|[\s,.:;]+)_{1}([^_\n]+)_{1}([\s,.:;]+|$)/g, '$1<i>$2</i>$3'); // _
markdown = markdown.replace(/(^|[\s,.:;]+)~{2}([^~\n]+)~{2}([\s,.:;]+|$)/g, '$1<del>$2</del>$3'); // ~~
// title
markdown = markdown.replace(/##/g, '▪▪');
markdown = markdown.replace(/(^▪|[\n▪])#/g, '$1▪');
// markdown = markdown.replace(/(▪+ .*)/g, '<strong>$1</strong>');
// list
markdown = markdown.replace(/(^|\n) {2,10}[*\-]/g, '\n ▫');
markdown = markdown.replace(/(^|\n) [*\-]/g, '\n •');
markdown = markdown.replace(/(^|\n)[*\-]/g, '\n•');
markdown = markdown.replace(/(( )?[▫•]?\s*)(https?:\/\/\S+)/g, '<span style="white-space: nowrap; overflow-y: hidden;">$1$2 $3</span>');
return markdown;
}
$(document).ready(function() {
console.log('Loading GitLab board descriptions...');
setTimeout(function () {
project_id = get_project_id();
// styles
$('.boards-list').css("overflow-x", "auto");
$('head').append('\
<style type="text/css">\
.board-card-body {\
position: relative;\
}\
.board-card-description {\
font-size: '+description_font_size+';\
color: #909090;\
white-space: pre-wrap;\
overflow: hidden;\
max-height: '+description_height+';\
\
}\
.board-card-description:after {\
content:"";\
position:absolute;\
bottom:0;\
left:0;\
top:calc('+description_height+' - 2em);\
width:100%;\
background: linear-gradient(rgba(255,255,255,0), #FFF);\
}\
.board-card-description code, .block.description code {\
color: inherit;\
background: #f0f0f0;\
padding: 1px 2px;\
}\
.board-card-description pre.code, .block.description pre.code {\
color: inherit;\
padding: 1px 2px;\
border-color: #f4f4f4;\
}\
.board-card-description-full {\
display: none;\
}\
.block.description .value {\
font-size: '+description_font_size+';\
color: #707070;\
white-space: pre-wrap;\
}\
</style>');
// descriptions button
var btn = $('<button type="button" class="btn btn-default gl-ml-3"></button>');
var tooltip = '<span style="white-space: nowrap">Toggle descriptions</span>';
tooltip += '<br><span style="font-size: 0.85em; white-space: nowrap">Ctrl : Refresh descriptions</span>';
btn.addClass("btn-display-board-descriptions has-tooltip active");
btn.attr("data-toggle", "button");
btn.attr("data-html", "true");
btn.attr("title", tooltip);
btn.attr("description-active", "true");
btn.text("");
btn.append('<svg class="s16" data-testid="doc-text-icon"><use xlink:href="/assets/icons-81bca028cfa382a852fa2c8a6dfb4fb2b7467093d38f9fe9a07a519ca785299c.svg#doc-text"></use></svg>');
$('.board-extra-actions').css("white-space", "nowrap");
$('.board-extra-actions').first().append(btn);
// first load
refresh_descriptions();
$('.btn-display-board-descriptions').on("click", function (e) {
if (e && e.ctrlKey) {
// always toggle
btn.attr("description-active", "false");
btn.removeClass('active');
}
if(btn.attr("description-active") === "false") {
// $('.btn-display-board-descriptions').addClass("disabled");
btn.attr("description-active", "true");
btn.removeClass('active');
refresh_descriptions();
} else {
btn.attr("description-active", "false");
btn.addClass('active');
hide_descriptions();
}
});
}, 1000);
});