From 22079471bc82da1016be6c5386ac701f61e2e038 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Tue, 7 Feb 2023 14:01:47 -0500 Subject: [PATCH] Add scroll-wheel file browsing. Improve backtick comments (works on non-diff pages) Other small bits of cleanup. --- backtick-comments.js | 33 +++++++++++++++++++-------------- file-scroll.js | 24 ++++++++++++++++++++++++ manifest.json | 3 ++- repo-search-sorter.js | 5 ++++- utils.js | 2 +- 5 files changed, 50 insertions(+), 17 deletions(-) create mode 100644 file-scroll.js diff --git a/backtick-comments.js b/backtick-comments.js index cfc66d6..ca5b47b 100644 --- a/backtick-comments.js +++ b/backtick-comments.js @@ -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 diff --git a/file-scroll.js b/file-scroll.js new file mode 100644 index 0000000..d7660c9 --- /dev/null +++ b/file-scroll.js @@ -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 +}) diff --git a/manifest.json b/manifest.json index a2235b4..dd70188 100644 --- a/manifest.json +++ b/manifest.json @@ -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" ], diff --git a/repo-search-sorter.js b/repo-search-sorter.js index dfda2d4..3293869 100644 --- a/repo-search-sorter.js +++ b/repo-search-sorter.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 diff --git a/utils.js b/utils.js index 5c1f43f..490ba6d 100644 --- a/utils.js +++ b/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))