Skip to content

Commit

Permalink
Update some keybindings to make more vim-like
Browse files Browse the repository at this point in the history
  • Loading branch information
dit7ya committed Sep 2, 2023
1 parent e974057 commit 2d083e4
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 116 deletions.
159 changes: 82 additions & 77 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ actions.vimEditURL = () =>
(url) => {
actions.openLink(url)
},
"url"
"url",
)

actions.getOrgLink = () => `[[${window.location.href}][${document.title}]]`
Expand Down Expand Up @@ -143,11 +143,14 @@ actions.getDiscussionsUrl = ({ href = window.location.href } = {}) =>

// Surfingkeys-specific actions
// ----------------------------
actions.openAnchor = ({
newTab = false,
active = true,
prop = "href",
} = {}) => (a) => actions.openLink(a[prop], { newTab, active })

actions.closeTab = () => RUNTIME("closeTab")
actions.openLast = () => RUNTIME("openLast")

actions.openAnchor =
({ newTab = false, active = true, prop = "href" } = {}) =>
(a) =>
actions.openLink(a[prop], { newTab, active })

actions.openLink = (url, { newTab = false, active = true } = {}) => {
if (newTab) {
Expand Down Expand Up @@ -178,7 +181,7 @@ actions.togglePdfViewer = () =>

actions.previewLink = () =>
util.createHints("a[href]", (a) =>
Front.showEditor(a.href, (url) => actions.openLink(url), "url")
Front.showEditor(a.href, (url) => actions.openLink(url), "url"),
)

actions.scrollElement = (el, dir) => {
Expand Down Expand Up @@ -236,7 +239,7 @@ actions.az.viewProduct = () => {
actions.viewGodoc = () =>
actions.openLink(
`https://godoc.org/${util.getURLPath({ count: 2, domain: true })}`,
{ newTab: true }
{ newTab: true },
)

// Google
Expand Down Expand Up @@ -379,61 +382,63 @@ actions.dg.siteSearch = (site) => {
// GitHub
// ------
actions.gh = {}
actions.gh.star = ({ toggle = false } = {}) => async () => {
const hasDisplayNoneParent = (e) =>
window.getComputedStyle(e).display === "none" ||
(e.parentElement ? hasDisplayNoneParent(e.parentElement) : false)

const starContainers = Array.from(
document.querySelectorAll("div.starring-container")
).filter((e) => !hasDisplayNoneParent(e))

let container
switch (starContainers.length) {
case 0:
return
case 1:
;[container] = starContainers
break
default:
try {
container = await util.createHints(starContainers, { action: null })
} catch (_) {
actions.gh.star =
({ toggle = false } = {}) =>
async () => {
const hasDisplayNoneParent = (e) =>
window.getComputedStyle(e).display === "none" ||
(e.parentElement ? hasDisplayNoneParent(e.parentElement) : false)

const starContainers = Array.from(
document.querySelectorAll("div.starring-container"),
).filter((e) => !hasDisplayNoneParent(e))

let container
switch (starContainers.length) {
case 0:
return
}
}
case 1:
;[container] = starContainers
break
default:
try {
container = await util.createHints(starContainers, { action: null })
} catch (_) {
return
}
}

const repoUrl = container.parentElement.parentElement?.matches(
"ul.pagehead-actions"
)
? window.location.pathname
: new URL(container.parentElement.querySelector("form").action).pathname
const repoUrl = container.parentElement.parentElement?.matches(
"ul.pagehead-actions",
)
? window.location.pathname
: new URL(container.parentElement.querySelector("form").action).pathname

const status = container.classList.contains("on")
const repo = repoUrl.slice(1).split("/").slice(0, 2).join("/")
const status = container.classList.contains("on")
const repo = repoUrl.slice(1).split("/").slice(0, 2).join("/")

let star = "★"
let statusMsg = "starred"
let copula = "is"
let star = "★"
let statusMsg = "starred"
let copula = "is"

if ((status && toggle) || (!status && !toggle)) {
statusMsg = `un${statusMsg}`
star = "☆"
}
if ((status && toggle) || (!status && !toggle)) {
statusMsg = `un${statusMsg}`
star = "☆"
}

if (toggle) {
copula = "has been"
container
.querySelector(
status
? ".starred button, button.starred"
: ".unstarred button, button.unstarred"
)
.click()
}
if (toggle) {
copula = "has been"
container
.querySelector(
status
? ".starred button, button.starred"
: ".unstarred button, button.unstarred",
)
.click()
}

Front.showBanner(`${star} Repository ${repo} ${copula} ${statusMsg}!`)
}
Front.showBanner(`${star} Repository ${repo} ${copula} ${statusMsg}!`)
}

actions.gh.parseRepo = (url = window.location.href, rootOnly = false) => {
let u
Expand All @@ -446,7 +451,7 @@ actions.gh.parseRepo = (url = window.location.href, rootOnly = false) => {
const isRoot = rest.length === 0
const cond =
["github.com", "gist.github.com", "raw.githubusercontent.com"].includes(
u.hostname
u.hostname,
) &&
typeof user === "string" &&
user.length > 0 &&
Expand Down Expand Up @@ -683,7 +688,7 @@ actions.gh.openSourceFile = () => {

actions.gh.openProfile = () =>
actions.gh.openPage(
`${document.querySelector("meta[name='user-login']").content}`
`${document.querySelector("meta[name='user-login']").content}`,
)

actions.gh.toggleLangStats = () =>
Expand Down Expand Up @@ -811,7 +816,7 @@ actions.gh.openFileFromClipboard = async ({ newTab = true } = {}) => {

actions.openLink(
`https://github.com/${dest.user}/${dest.repo}/tree/${dest.commitHash}/${clip}`,
{ newTab }
{ newTab },
)
}

Expand Down Expand Up @@ -842,21 +847,21 @@ actions.tw.openUser = () =>
[].concat(
[
...document.querySelectorAll(
"a[role='link'] img[src^='https://pbs.twimg.com/profile_images']"
"a[role='link'] img[src^='https://pbs.twimg.com/profile_images']",
),
].map((e) => e.closest("a")),
[...document.querySelectorAll("a[role='link']")].filter((e) =>
e.text.match(/^@/)
)
)
e.text.match(/^@/),
),
),
)

// Reddit
// ------
actions.re = {}
actions.re.collapseNextComment = () => {
const vis = Array.from(
document.querySelectorAll(".noncollapsed.comment")
document.querySelectorAll(".noncollapsed.comment"),
).filter((e) => util.isElementInViewport(e))
if (vis.length > 0) {
vis[0].querySelector(".expand").click()
Expand All @@ -876,7 +881,7 @@ actions.hn.goParent = () => {

actions.hn.collapseNextComment = () => {
const vis = Array.from(document.querySelectorAll("a.togg")).filter(
(e) => e.innerText === "[–]" && util.isElementInViewport(e)
(e) => e.innerText === "[–]" && util.isElementInViewport(e),
)
if (vis.length > 0) {
vis[0].click()
Expand Down Expand Up @@ -909,7 +914,7 @@ actions.hn.goPage = (dist = 1) => {
actions.hn.openLinkAndComments = (e) => {
const linkUrl = e.querySelector(".titleline>a").href
const commentsUrl = e.nextElementSibling.querySelector(
"a[href^='item']:not(.titlelink)"
"a[href^='item']:not(.titlelink)",
).href
actions.openLink(commentsUrl, { newTab: true })
actions.openLink(linkUrl, { newTab: true })
Expand All @@ -922,10 +927,10 @@ actions.ph.openExternal = () => {
Hints.create("ul[class^='postsList_'] > li > div[class^='item_']", (p) =>
actions.openLink(
p.querySelector(
"div[class^='meta_'] > div[class^='actions_'] > div[class^='minorActions_'] > a:nth-child(1)"
"div[class^='meta_'] > div[class^='actions_'] > div[class^='minorActions_'] > a:nth-child(1)",
).href,
{ newTab: true }
)
{ newTab: true },
),
)
}

Expand Down Expand Up @@ -974,7 +979,7 @@ actions.wp.markdownSummary = () =>
(acc, f) => (f(acc) && false) || acc,
document
.querySelector("#mw-content-text p:not([class]):not([id])")
.cloneNode(true)
.cloneNode(true),
)
.innerText.trim()}
Expand All @@ -988,7 +993,7 @@ actions.nt.adjustTemp = (dir) =>
.querySelector(
`button[data-test='thermozilla-controller-controls-${
dir > 0 ? "in" : "de"
}crement-button']`
}crement-button']`,
)
.click()

Expand All @@ -999,7 +1004,7 @@ actions.nt.setMode = async (mode) => {
if (q) return q
popover
.querySelector(
`button[data-test='thermozilla-mode-switcher-${mode}-button']`
`button[data-test='thermozilla-mode-switcher-${mode}-button']`,
)
.click()
return util.until(query)
Expand Down Expand Up @@ -1027,7 +1032,7 @@ actions.nt.setFan = async (desiredState) => {
if (q) return q
popover
.querySelector(
`div[data-test='thermozilla-fan-timer-${startStop}-button']`
`div[data-test='thermozilla-fan-timer-${startStop}-button']`,
)
.click()
return util.until(query)
Expand All @@ -1038,7 +1043,7 @@ actions.nt.setFan = async (desiredState) => {
const q = query()
if (q) return q
Hints.dispatchMouseClick(
listbox.querySelector("div[role='option']:last-child")
listbox.querySelector("div[role='option']:last-child"),
)
return util.until(query)
}
Expand All @@ -1062,7 +1067,7 @@ actions.nt.setFan = async (desiredState) => {

const fanRunning = () =>
document.querySelector(
"div[data-test='thermozilla-aag-fan-listcell-title']"
"div[data-test='thermozilla-aag-fan-listcell-title']",
)

const startFan = async () => {
Expand Down Expand Up @@ -1093,7 +1098,7 @@ actions.re.focusSearch = () =>
actions.dispatchMouseEvents(
document.getElementById("docsearch"),
"mousedown",
"click"
"click",
)

actions.re.scrollSidebar = (dir) =>
Expand All @@ -1118,7 +1123,7 @@ actions.ik.toggleProductDetails = async () => {
document.querySelector(".range-revamp-expander__btn")
const productDetailsButtonQuery = () =>
document.querySelector(
".range-revamp-product-information-section__button button"
".range-revamp-product-information-section__button button",
)

const openProductDetailsModal = async () => {
Expand Down
Loading

0 comments on commit 2d083e4

Please sign in to comment.