-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathlib.js
More file actions
239 lines (206 loc) · 6.44 KB
/
lib.js
File metadata and controls
239 lines (206 loc) · 6.44 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
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
export const createRootElement = () => {
const injectionElement = document.querySelector('.pr-toolbar')
if (!injectionElement) {
return
}
const rootId = '__better_github_pr'
let element = document.querySelector('.' + rootId)
if (!element) {
element = document.createElement('div')
element.className = rootId
injectionElement.appendChild(element)
}
return element
}
const sorter = (a, b) => {
const isFileA = Boolean(a.href)
const isFileB = Boolean(b.href)
if (isFileA === isFileB) {
return (b.nodeLabel > a.nodeLabel) ? -1 : ((a.nodeLabel > b.nodeLabel) ? 1 : 0)
} else if (isFileA && !isFileB) {
return 1
} else {
return -1
}
}
const parseChangeNumber = (n) => {
if (!n.replace) return 0
const number = parseInt(n.replace(',', ''), 10)
return isNaN(number) ? 0 : number
}
const getDiffStatsForDiffElement = (diffElement) => {
const diffStatSpan = diffElement.getElementsByClassName('diffstat')[0]
const changesTxt = diffStatSpan && diffStatSpan.getAttribute('aria-label')
const changes = changesTxt &&
changesTxt.match(/([\d,]*) additions? & ([\d,]*) deletions?/)
return changes && {
additions: parseChangeNumber(changes[1]),
deletions: parseChangeNumber(changes[2])
}
}
const getDiffElement = (fileId) => {
const el = document.getElementById(fileId)
if (el) {
return el
}
const gitHubEnterpriseEl = document.querySelector(`[data-anchor="${fileId}"]`)
if (gitHubEnterpriseEl) {
return gitHubEnterpriseEl.parentElement
}
return null
}
const countCommentsForFileId = (fileId) => {
const diffTable = getDiffElement(fileId)
if (!diffTable) {
return 0
}
return diffTable.querySelectorAll('.inline-comments').length
}
const isDeletedForFileId = (fileId) => {
const diffTable = getDiffElement(fileId)
if (!diffTable) {
return false
}
const hiddenDiffReason = diffTable.querySelector('.hidden-diff-reason')
return hiddenDiffReason && (hiddenDiffReason.innerText.includes('file was deleted'))
}
const filterItem = (item, filter) => {
if (filter === null || filter.trim() === EMPTY_FILTER) {
return true
}
if (filter[0] === '!') {
return item && item.toLowerCase().indexOf(filter.substr(1).toLowerCase()) < 0
}
return item && item.toLowerCase().indexOf(filter.toLowerCase()) > -1
}
const getCurrentFileLocation = (title) => {
if (title.split(' → ').length > 1) {
return title.split(' → ')[1]
}
return title.split(' → ')[0]
}
export const folderConcat = (node) => {
const isFileOrEmpty = (node.list === undefined || node.list.length === 0 || (node.href !== null && node.href !== undefined))
if (isFileOrEmpty) {
return node
}
const hasSingleChild = (node.list.length === 1)
if (hasSingleChild) {
const collapsed = folderConcat(node.list[0])
const isLastCollapsedIsFolder = node.nodeLabel !== '/' && (collapsed.href === null || collapsed.href === undefined)
if (isLastCollapsedIsFolder) {
node.nodeLabel = node.nodeLabel + '/' + collapsed.nodeLabel
node.hasComments = collapsed.hasComments || node.hasComments
node.isDeleted = collapsed.isDeleted || node.isDeleted
node.list = collapsed.list
}
return node
}
node.list.map(x => folderConcat(x))
return node
}
export const createFileTree = (filter = EMPTY_FILTER) => {
const fileInfo = [...document.querySelectorAll('.file-info > a')]
const files = fileInfo.map(({ title, href }) => {
title = getCurrentFileLocation(title)
return { title, href, parts: title.split('/') }
})
const count = fileInfo.filter(({ href }) => href && href.includes('#diff')).length
const tree = {
nodeLabel: '/',
list: [],
diffElements: []
}
files.forEach(({ parts, href }) => {
let location = tree
if (filterItem(parts[parts.length - 1], filter)) {
parts.forEach((part, index) => {
let node = location.list.find(node => node.nodeLabel === part)
if (!node) {
const hrefSplit = href.split('#')
const fileId = hrefSplit[hrefSplit.length - 1]
const diffElement = getDiffElement(fileId)
if (diffElement) {
const hasComments = (countCommentsForFileId(fileId) > 0)
const isDeleted = isDeletedForFileId(fileId)
tree.diffElements.push(diffElement)
node = {
nodeLabel: part,
list: [],
href: (index === parts.length - 1) ? href : null,
hasComments,
isDeleted,
diffElement,
diffStats: getDiffStatsForDiffElement(diffElement)
}
location.list.push(node)
}
}
location.list = location.list.sort(sorter)
location = node
})
}
})
return {
tree: folderConcat(tree),
count
}
}
export const isElementVisible = (el) => {
if (!el) {
return false
}
const GITHUB_HEADER_HEIGHT = 60
const rect = el.getBoundingClientRect()
const windowHeight = (window.innerHeight || document.documentElement.clientHeight)
const windowWidth = (window.innerWidth || document.documentElement.clientWidth)
const vertInView = (rect.top <= windowHeight) && ((rect.top + rect.height) >= GITHUB_HEADER_HEIGHT)
const horInView = (rect.left <= windowWidth) && ((rect.left + rect.width) >= 0)
return (vertInView && horInView)
}
export const isElementTarget = (el) => {
if (!el) {
return false
}
return el.matches(':target')
}
export const isElementTargetAndVisible = (el) => isElementTarget(el) && isElementVisible(el)
const EMPTY_FILTER = ''
export const getBrowserApi = () => {
let browserApi = window.chrome
if (typeof browser !== 'undefined') {
browserApi = browser
}
return browserApi
}
export const StorageSync = {
save () {
return new Promise(resolve => {
const diffStats = document.getElementById('diffStats').checked
const options = {
diffStats
}
if (window.chrome) {
window.chrome.storage.sync.set(options, resolve)
} else {
browser.storage.sync.set(options).then(resolve)
}
})
},
get () {
return new Promise(resolve => {
const defaults = {
diffStats: false
}
if (window.chrome) {
window.chrome.storage.sync.get(defaults, resolve)
} else {
browser.storage.sync.get(defaults).then(resolve)
}
})
}
}
export const isFileViewed = diffElement => {
const checkbox = diffElement.querySelector('.js-reviewed-checkbox')
return checkbox && checkbox.checked
}