Skip to content

Commit

Permalink
scroll to top on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-valim committed Sep 16, 2023
1 parent 97fbfe5 commit 0cd24cb
Showing 1 changed file with 41 additions and 44 deletions.
85 changes: 41 additions & 44 deletions assets/js/content.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,43 @@
import { qs, qsAll } from "./helpers";
import { settingsStore } from "./settings-store";
import { qs, qsAll } from './helpers'
import { settingsStore } from './settings-store'

const CONTENT_SELECTOR = ".content";
const CONTENT_INNER_SELECTOR = ".content-inner";
const LIVEBOOK_BADGE_ANCHOR_SELECTOR = ".livebook-badge";
const CONTENT_SELECTOR = '.content'
const CONTENT_INNER_SELECTOR = '.content-inner'
const LIVEBOOK_BADGE_ANCHOR_SELECTOR = '.livebook-badge'

/**
* Runs some general modifiers on the documentation content.
*/
export function initialize() {
fixLinks();
fixSpacebar();
setLivebookBadgeUrl();
fixBlockquotes();
export function initialize () {
fixLinks()
fixSpacebar()
setLivebookBadgeUrl()
fixBlockquotes()
}

/**
* Removes underline from links that have nested code or images.
*/
function fixLinks() {
qs(CONTENT_SELECTOR)
.querySelectorAll("a")
.forEach((anchor) => {
if (anchor.querySelector("code, img")) {
anchor.classList.add("no-underline");
}
});
function fixLinks () {
qs(CONTENT_SELECTOR).querySelectorAll('a').forEach(anchor => {
if (anchor.querySelector('code, img')) {
anchor.classList.add('no-underline')
}
})
}

/**
* Add CSS classes to `blockquote` elements when those are used to
* support admonition text blocks
*/
function fixBlockquotes() {
const classes = ["warning", "info", "error", "neutral", "tip"];
function fixBlockquotes () {
const classes = ['warning', 'info', 'error', 'neutral', 'tip']

classes.forEach((element) => {
qsAll(`blockquote h3.${element}, blockquote h4.${element}`).forEach(
(header) => {
header.closest("blockquote").classList.add(element);
}
);
});
classes.forEach(element => {
qsAll(`blockquote h3.${element}, blockquote h4.${element}`).forEach(header => {
header.closest('blockquote').classList.add(element)
})
})
}

/**
Expand All @@ -52,37 +48,38 @@ function fixBlockquotes() {
* the user would be forced to first click on the content element
* before these keybindings worked.
*/
function fixSpacebar() {
qs(CONTENT_INNER_SELECTOR).setAttribute("tabindex", -1);
qs(CONTENT_INNER_SELECTOR).focus();
function fixSpacebar () {
qs(CONTENT_INNER_SELECTOR).setAttribute('tabindex', -1)
qs(CONTENT_INNER_SELECTOR).focus()
}

/**
* Updates "Run in Livebook" badges to link to a notebook
* corresponding to the current documentation page.
*/
function setLivebookBadgeUrl() {
const path = window.location.pathname;
const notebookPath = path.replace(/(\.html)?$/, ".livemd");
const notebookUrl = new URL(notebookPath, window.location.href).toString();
function setLivebookBadgeUrl () {
const path = window.location.pathname
const notebookPath = path.replace(/(\.html)?$/, '.livemd')
const notebookUrl = new URL(notebookPath, window.location.href).toString()

settingsStore.getAndSubscribe((settings) => {
const targetUrl = settings.livebookUrl
? getLivebookImportUrl(settings.livebookUrl, notebookUrl)
: getLivebookDevRunUrl(notebookUrl);
settingsStore.getAndSubscribe(settings => {
const targetUrl =
settings.livebookUrl
? getLivebookImportUrl(settings.livebookUrl, notebookUrl)
: getLivebookDevRunUrl(notebookUrl)

for (const anchor of qsAll(LIVEBOOK_BADGE_ANCHOR_SELECTOR)) {
anchor.href = targetUrl;
anchor.href = targetUrl
}
});
})
}

function getLivebookDevRunUrl(notebookUrl) {
return `https://livebook.dev/run?url=${encodeURIComponent(notebookUrl)}`;
function getLivebookDevRunUrl (notebookUrl) {
return `https://livebook.dev/run?url=${encodeURIComponent(notebookUrl)}`
}

function getLivebookImportUrl(livebookUrl, notebookUrl) {
return `${livebookUrl}/import?url=${encodeURIComponent(notebookUrl)}`;
function getLivebookImportUrl (livebookUrl, notebookUrl) {
return `${livebookUrl}/import?url=${encodeURIComponent(notebookUrl)}`
}

// Check if the device width is below a certain threshold (e.g., 768px for mobile)
Expand Down

0 comments on commit 0cd24cb

Please sign in to comment.