Add scroll-wheel file browsing.

Improve backtick comments (works on non-diff pages)
Other small bits of cleanup.
This commit is contained in:
Sage Vaillancourt 2023-02-07 14:01:47 -05:00
parent bee9f4e2b2
commit 22079471bc
5 changed files with 50 additions and 17 deletions

View File

@ -44,24 +44,24 @@ const keywordMap = {
const bookendedWith = (text, bookend) => text.startsWith(bookend) && text.endsWith(bookend) const bookendedWith = (text, bookend) => text.startsWith(bookend) && text.endsWith(bookend)
const getClass = (text, keywords) => { const getClass = (text, prefix, keywords) => {
if (keywords.has(text)) { if (keywords.has(text)) {
return 'hl-keyword' return prefix + 'keyword'
} }
if (bookendedWith(text, '"')) { if (bookendedWith(text, '"')) {
return 'hl-string' return prefix + 'string'
} }
return 'hl-variable' return prefix + 'variable'
} }
const getKeywords = () => keywordMap[getFileName()?.replace(/.*\./g, '')] || keywordMap.none const getKeywords = () => keywordMap[getFileName()?.replace(/.*\./g, '')] || keywordMap.none
const commentSpan = innerText => { const commentSpan = (innerText, prefix) => {
const span = document.createElement('span') const span = document.createElement('span')
span.innerText = innerText span.innerText = innerText
span.classList.add('hl-comment') span.classList.add(prefix + 'comment')
return span return span
} }
@ -69,24 +69,29 @@ const colorCodeInComment = (codeStart = '`', codeEnd) => {
codeEnd ??= codeStart codeEnd ??= codeStart
const processed = new Set() const processed = new Set()
const keywords = getKeywords() const keywords = getKeywords()
getClassNameElementsArray('hl-comment').forEach(element => { const comments = [...getClassNameElementsArray('hl-comment'), ...getClassNameElementsArray('cm-comment')]
if (!element.innerText?.startsWith(codeStart) || processed.has(element)) { comments.forEach(element => {
const codeStartIndex = element.innerText?.indexOf(codeStart)
if (codeStartIndex === -1 || processed.has(element)) {
return 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.replaceWith(codeStartElement, element)
element.innerText = element.innerText.replace(codeStart, '')
while (element) { while (element) {
if (!element.classList.contains('hl-comment')) { if (!element.classList.contains('hl-comment') && !element.classList.contains('cm-comment')) {
break break
} }
processed.add(element) processed.add(element)
element.classList.remove('hl-comment') element.classList.remove(`${prefix}comment`)
const text = element.innerText.replaceAll(codeStart, '').replaceAll(codeEnd, '') 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) const codeEndIndex = element.innerText.indexOf(codeEnd)
if (codeEndIndex > -1) { 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.innerText = element.innerText.substring(0, codeEndIndex)
element.replaceWith(element, codeEndElement) element.replaceWith(element, codeEndElement)
break break

24
file-scroll.js Normal file
View File

@ -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
})

View File

@ -1,7 +1,7 @@
{ {
"name": "Sage's BitBucket Addon", "name": "Sage's BitBucket Addon",
"description": "Ensure searches don't include forks (and other enhancements)", "description": "Ensure searches don't include forks (and other enhancements)",
"version": "1.0.3", "version": "1.0.4",
"manifest_version": 2, "manifest_version": 2,
"browser_specific_settings": { "browser_specific_settings": {
"gecko": { "gecko": {
@ -27,6 +27,7 @@
"utils.js", "utils.js",
"backtick-comments.js", "backtick-comments.js",
"csharp-quotes.js", "csharp-quotes.js",
"file-scroll.js",
"repo-search-sorter.js", "repo-search-sorter.js",
"var-highlighter.js" "var-highlighter.js"
], ],

View File

@ -16,11 +16,14 @@ let claim = 0
const sortSearchResult = () => { const sortSearchResult = () => {
const searchBox = Array.from(document.getElementsByTagName('input')) const searchBox = Array.from(document.getElementsByTagName('input'))
.filter(input => input.name === 'repository-search')[0] .filter(input => input.name === 'repository-search')[0]
if (!searchBox) {
return
}
searchBox.onkeyup = e => { searchBox.onkeyup = e => {
claim++ claim++
const myClaim = claim const myClaim = claim
setTimeout(() => { setTimeout(() => {
if (claim != myClaim) { if (claim !== myClaim) {
return return
} }
const ol = getClassNameElementsArray('search-results')[0]?.firstChild const ol = getClassNameElementsArray('search-results')[0]?.firstChild

View File

@ -4,7 +4,7 @@ const CURRENT_PAGE_DELAY = 300
const getClassNameElementsArray = className => const getClassNameElementsArray = className =>
Array.from(document.getElementsByClassName(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 => { const addFix = fixFunc => {
window.addEventListener('load', () => setTimeout(fixFunc, NEW_PAGE_DELAY)) window.addEventListener('load', () => setTimeout(fixFunc, NEW_PAGE_DELAY))
addEventListener('hashchange', () => setTimeout(fixFunc, CURRENT_PAGE_DELAY)) addEventListener('hashchange', () => setTimeout(fixFunc, CURRENT_PAGE_DELAY))