forked from camsong/chrome-github-mate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
panel.js
191 lines (160 loc) · 5.81 KB
/
panel.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
/*
* @file Right panel of github mate, including favorites and markdown outline
* @thanks-to originaly implemented by github.com/zbycz
*/
function gm_get_favorites() {
return JSON.parse(localStorage.getItem('github-mate-panel-favorites')) || [{
loc: 'https://github.com/rubyerme/chrome-github-mate',
text: 'Default by OctoMate',
title: 'default entry'
}];
}
function gm_set_favorites(items) {
try {
localStorage.setItem('github-mate-panel-favorites', JSON.stringify(items));
} catch (e) {
if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
alert("Local storage is full, can't add more favorite");
}
}
}
// use lastString to avoid rerendering
var lastString = '';
function gm_refresh_favorites() {
var ul = '';
var items = gm_get_favorites();
for (var i in items) {
var item = items[i];
//var text = decodeURIComponent(item.loc.replace(/https?:\/\/[^\/]+\//, '')).replace('+', ' ');
ul += '<li><span data-i="' + i + '" class="edit">✎</span> <a href="' + item.loc + '" title="' + item.title + '">' + item.text + '</a></li>'
}
if (ul != lastString) {
lastString = ul;
panelNode.find('ul.github-mate-favorites').html(ul);
}
}
// put panelNode in global
var panelNode = $('div.github-mate-panel');
function initPanelNode() {
if (panelNode.length === 0) {
let isCollapse = localStorage.getItem('github-mate-panel-collapse') === 'true';
panelNode = $('<div class="github-mate-panel' + (isCollapse ? ' collapse' : '') + '"></div>').html(
"<a href='javascript:;' class='add btn'>➕</a>" +
"<a href='javascript:;' class='toggle btn collapse'></a>" +
"<b>Favorites</b>" +
"<ul class='github-mate-favorites'></ul>" +
"<div class='github-mate-outline'></div>"
);
$('body').append(panelNode);
setInterval(gm_refresh_favorites, 5000);
gm_refresh_favorites();
panelNode.on('click', 'a.toggle', function (e) {
$('.github-mate-panel').toggleClass('collapse');
localStorage.setItem('github-mate-panel-collapse', $('.github-mate-panel').hasClass('collapse'));
$(e.target).blur();
});
panelNode.on('click', 'a.add', function (e) {
var path = location.pathname.split('/'); // /winsite/iprace_fe/issues/779
var text = prompt('Add this page, edit title:', path[2] + ' ' + document.title);
if (!text) return;
var items = gm_get_favorites();
items.push({
loc: location.toString(),
title: document.title,
text: text
});
gm_set_favorites(items);
gm_refresh_favorites();
e.preventDefault();
});
panelNode.on('click', 'span.edit', function () {
var items = gm_get_favorites();
var i = $(this).attr('data-i');
var pr = window.prompt('Set new title OR set blank to delete', items[i].text);
if (pr === null) return;
else if (pr === '') items.splice(i, 1);
else items[i].text = pr;
gm_set_favorites(items);
gm_refresh_favorites();
});
}
}
function updateOutlinesInPanel() {
var $container = document.createElement('div')
$container.classList.add('github-mate-outline')
Array.from(document.querySelectorAll('.markdown-body:not(.comment-body):not(.message)')).forEach($md => {
var $headers = Array.from($md.querySelectorAll(headerSel))
var $b = document.createElement('b')
$b.innerText = "Outline"
$container.appendChild($b)
var $outline = document.createElement('ul')
$container.appendChild($outline)
// generate outline from headers
$headers.forEach($h => {
var level = getHeaderLevel($h)
if (!level) return
var $ul = $outline,
$li, $child
for (var l = 1; l < level; l++) {
$li = $ul.lastChild || $ul.appendChild(document.createElement('li'))
$child = $li.lastChild || {}
$ul = $child.tagName === 'UL' ? $child : $li.appendChild(document.createElement('ul'))
}
var $topic = $ul
.appendChild(document.createElement('li'))
.appendChild(document.createElement('a'))
$topic.innerText = $h.innerText
$topic.href = `#${$h.querySelector(anchorSel).id.replace(/^user-content-/, '')}`
})
// find all sublists with one item and replace with contents
Array.from($container.querySelectorAll('ul')).forEach($ul => {
try {
var $parent = $ul.parentNode
var $li = $ul.firstChild
var $child = $li.firstChild
if ($li !== $ul.lastChild || $child.tagName !== 'UL') return
while ($child) {
$parent.insertBefore($child, $ul.nextSibling) // inserts to end of list if $ul.nextSibling is null
$child = $child.nextSibling
}
$parent.removeChild($ul)
} catch (e) {
console.log("Error setting up GitHub Mate panel:", e)
}
})
})
panelNode.find('.github-mate-outline').replaceWith($container);
}
// Copyright (c) 2015 Dan Kaplun
// The MIT License (MIT)
// from https://github.com/dbkaplun/github-markdown-outline-extension/blob/master/index.js
// very hackily edited by zbycz to add it in panel
getHeaderLevel.REGEXP = /h(\d)/i
function getHeaderLevel($h) {
var level = Number(((($h || {}).tagName || '').match(getHeaderLevel.REGEXP) || [])[1])
return isNaN(level) ? undefined : level
}
var headerSels = []
for (var l = 1; l <= 6; l++) headerSels.push('h' + l)
var headerSel = headerSels.join(', ')
var anchorSel = 'a[id]'
var Panel = {
init: function () {
chrome.runtime.sendMessage({
key: "feature-4-enable"
}, function (response) {
if (typeof (response.result) === 'undefined' || response.result === true) {
Panel.constructor();
}
});
},
constructor: function () {
// update after changed page with pjax
$(document).on('pjax:end', function () {
updateOutlinesInPanel();
});
initPanelNode();
updateOutlinesInPanel();
},
};
Panel.init();