1
0
mirror of https://github.com/gluster/glusterdocs.git synced 2026-02-06 18:47:08 +01:00
Files
glusterdocs/docs/js/custom-features.js
Niraj Kumar Yadav d79a838f8a [QOL] Extend keyboard shortcuts to copy current address (#733)
This patch also removes the obsolete fix-docs.js. That
file depends of jQuery and we no longer use that. That file was also
the cause of the console errors on current docs site.

After this patch, users will be able to press C or Y(ank) to copy
current address along with anchors to the clipboard. This feature is just
an extension of multiple exisiting shortcuts like F/P/N.

Another patch is in the works which would update current address so that
current anchor is in the addressbar. That would make this patch more useful as
you will be able to copy the adress of the location that you are currently reading
in the doc and resume later or maybe share to someone. Cool, right?

Signed-off-by: black-dragon74 <niryadav@redhat.com>
2022-06-20 11:46:45 +05:30

41 lines
1.2 KiB
JavaScript

// Add ability to copy the current URL using vim like shortcuts
// There already exists navigation related shortcuts like
// F/S -- For Searching
// P/N -- For navigating to previous/next pages
// This patch just extends those features
// Expose the internal notification API of mkdocs
// This API isn't exposed publically, IDK why
// They use it internally to show notifications when user copies a code block
// I reverse engineered it for ease of use, takes a string arg `msg`
const notifyDOM = (msg) => {
if (typeof alert$ === "undefined") {
console.error("Clipboard notification API not available");
return;
}
alert$.next(msg);
};
// Extend the keyboard shortcut features
keyboard$.subscribe((key) => {
// We want to allow the user to be able to type our modifiders in search
// Disallowing that would be hilarious
if (key.mode === "search") {
return;
}
const keyPressed = key.type.toLowerCase();
// Y is added to honor vim enthusiasts (yank)
if (keyPressed === "c" || keyPressed === "y") {
const currLocation = window.location.href;
if (currLocation) {
navigator.clipboard
.writeText(currLocation)
.then(() => notifyDOM("Address copied to clipboard"))
.catch((e) => console.error(e));
}
}
});