Add scroll-wheel file browsing.
Improve backtick comments (works on non-diff pages) Other small bits of cleanup.
This commit is contained in:
parent
bee9f4e2b2
commit
22079471bc
|
@ -44,24 +44,24 @@ const keywordMap = {
|
|||
|
||||
const bookendedWith = (text, bookend) => text.startsWith(bookend) && text.endsWith(bookend)
|
||||
|
||||
const getClass = (text, keywords) => {
|
||||
const getClass = (text, prefix, keywords) => {
|
||||
if (keywords.has(text)) {
|
||||
return 'hl-keyword'
|
||||
return prefix + 'keyword'
|
||||
}
|
||||
|
||||
if (bookendedWith(text, '"')) {
|
||||
return 'hl-string'
|
||||
return prefix + 'string'
|
||||
}
|
||||
|
||||
return 'hl-variable'
|
||||
return prefix + 'variable'
|
||||
}
|
||||
|
||||
const getKeywords = () => keywordMap[getFileName()?.replace(/.*\./g, '')] || keywordMap.none
|
||||
|
||||
const commentSpan = innerText => {
|
||||
const commentSpan = (innerText, prefix) => {
|
||||
const span = document.createElement('span')
|
||||
span.innerText = innerText
|
||||
span.classList.add('hl-comment')
|
||||
span.classList.add(prefix + 'comment')
|
||||
return span
|
||||
}
|
||||
|
||||
|
@ -69,24 +69,29 @@ const colorCodeInComment = (codeStart = '`', codeEnd) => {
|
|||
codeEnd ??= codeStart
|
||||
const processed = new Set()
|
||||
const keywords = getKeywords()
|
||||
getClassNameElementsArray('hl-comment').forEach(element => {
|
||||
if (!element.innerText?.startsWith(codeStart) || processed.has(element)) {
|
||||
const comments = [...getClassNameElementsArray('hl-comment'), ...getClassNameElementsArray('cm-comment')]
|
||||
comments.forEach(element => {
|
||||
const codeStartIndex = element.innerText?.indexOf(codeStart)
|
||||
if (codeStartIndex === -1 || processed.has(element)) {
|
||||
return
|
||||
}
|
||||
const codeStartElement = commentSpan(codeStart)
|
||||
const prefix = element.classList.contains('hl-comment') ? 'hl-' : 'cm-'
|
||||
const endOfCodeStart = codeStartIndex + codeStart.length
|
||||
const codeStartText = element.innerText.substring(0, endOfCodeStart)
|
||||
const codeStartElement = commentSpan(codeStartText, prefix)
|
||||
element.innerText = element.innerText.substring(endOfCodeStart, element.innerText.length)
|
||||
element.replaceWith(codeStartElement, element)
|
||||
element.innerText = element.innerText.replace(codeStart, '')
|
||||
while (element) {
|
||||
if (!element.classList.contains('hl-comment')) {
|
||||
if (!element.classList.contains('hl-comment') && !element.classList.contains('cm-comment')) {
|
||||
break
|
||||
}
|
||||
processed.add(element)
|
||||
element.classList.remove('hl-comment')
|
||||
element.classList.remove(`${prefix}comment`)
|
||||
const text = element.innerText.replaceAll(codeStart, '').replaceAll(codeEnd, '')
|
||||
element.classList.add(getClass(text, keywords))
|
||||
element.classList.add(getClass(text, prefix, keywords))
|
||||
const codeEndIndex = element.innerText.indexOf(codeEnd)
|
||||
if (codeEndIndex > -1) {
|
||||
const codeEndElement = commentSpan(element.innerText.substring(codeEndIndex))
|
||||
const codeEndElement = commentSpan(element.innerText.substring(codeEndIndex), prefix)
|
||||
element.innerText = element.innerText.substring(0, codeEndIndex)
|
||||
element.replaceWith(element, codeEndElement)
|
||||
break
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
const getChangeHeader = () => getClassNameElementsArray('change-header')[0]
|
||||
|
||||
const getFileElements = () => getClassNameElementsArray('file')
|
||||
|
||||
const getSelectedFile = () => getClassNameElementsArray('file-selected')[0]
|
||||
|
||||
const changeFile = e => {
|
||||
const up = e.wheelDelta ? e.wheelDelta > 0 : e.deltaY < 0
|
||||
const selectedFile = getSelectedFile()
|
||||
const fileElements = getFileElements()
|
||||
const index = fileElements.indexOf(selectedFile)
|
||||
const nextElement =
|
||||
(up && index > 0) ? fileElements[index - 1] :
|
||||
(!up && index < (fileElements.length - 1)) ? fileElements[index + 1] :
|
||||
null
|
||||
nextElement?.focus()
|
||||
nextElement?.click()
|
||||
}
|
||||
|
||||
addFix(() => {
|
||||
const header = getChangeHeader()
|
||||
console.log({ header })
|
||||
header.onwheel = changeFile
|
||||
})
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "Sage's BitBucket Addon",
|
||||
"description": "Ensure searches don't include forks (and other enhancements)",
|
||||
"version": "1.0.3",
|
||||
"version": "1.0.4",
|
||||
"manifest_version": 2,
|
||||
"browser_specific_settings": {
|
||||
"gecko": {
|
||||
|
@ -27,6 +27,7 @@
|
|||
"utils.js",
|
||||
"backtick-comments.js",
|
||||
"csharp-quotes.js",
|
||||
"file-scroll.js",
|
||||
"repo-search-sorter.js",
|
||||
"var-highlighter.js"
|
||||
],
|
||||
|
|
|
@ -16,11 +16,14 @@ let claim = 0
|
|||
const sortSearchResult = () => {
|
||||
const searchBox = Array.from(document.getElementsByTagName('input'))
|
||||
.filter(input => input.name === 'repository-search')[0]
|
||||
if (!searchBox) {
|
||||
return
|
||||
}
|
||||
searchBox.onkeyup = e => {
|
||||
claim++
|
||||
const myClaim = claim
|
||||
setTimeout(() => {
|
||||
if (claim != myClaim) {
|
||||
if (claim !== myClaim) {
|
||||
return
|
||||
}
|
||||
const ol = getClassNameElementsArray('search-results')[0]?.firstChild
|
||||
|
|
2
utils.js
2
utils.js
|
@ -4,7 +4,7 @@ const CURRENT_PAGE_DELAY = 300
|
|||
const getClassNameElementsArray = className =>
|
||||
Array.from(document.getElementsByClassName(className))
|
||||
|
||||
// Runs on "complete" load and when https://urlurl.url#this-hash-value changes
|
||||
/// Runs on "complete" load and when `https://website.tld#this-hash-value` changes
|
||||
const addFix = fixFunc => {
|
||||
window.addEventListener('load', () => setTimeout(fixFunc, NEW_PAGE_DELAY))
|
||||
addEventListener('hashchange', () => setTimeout(fixFunc, CURRENT_PAGE_DELAY))
|
||||
|
|
Loading…
Reference in New Issue