From 0cd24cb0710805463768a1dac3e8867f4d59d329 Mon Sep 17 00:00:00 2001 From: paulo-valim Date: Sat, 16 Sep 2023 10:35:15 +0200 Subject: [PATCH] scroll to top on iOS --- assets/js/content.js | 85 +++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 44 deletions(-) diff --git a/assets/js/content.js b/assets/js/content.js index 767a7b375..17123cdc0 100644 --- a/assets/js/content.js +++ b/assets/js/content.js @@ -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) + }) + }) } /** @@ -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)