Skip to content

Commit

Permalink
Added support for focusable code blocks on overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
squidfunk committed May 16, 2020
1 parent 900c113 commit c6e2e3a
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 6 deletions.
1 change: 0 additions & 1 deletion material/assets/javascripts/bundle.4e2bfc5d.min.js.map

This file was deleted.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions material/assets/javascripts/bundle.ff925e8b.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions material/assets/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"assets/javascripts/bundle.js": "assets/javascripts/bundle.4e2bfc5d.min.js",
"assets/javascripts/bundle.js.map": "assets/javascripts/bundle.4e2bfc5d.min.js.map",
"assets/javascripts/bundle.js": "assets/javascripts/bundle.ff925e8b.min.js",
"assets/javascripts/bundle.js.map": "assets/javascripts/bundle.ff925e8b.min.js.map",
"assets/javascripts/vendor.js": "assets/javascripts/vendor.809e24aa.min.js",
"assets/javascripts/vendor.js.map": "assets/javascripts/vendor.809e24aa.min.js.map",
"assets/javascripts/worker/search.js": "assets/javascripts/worker/search.f6ebf1dc.min.js",
Expand Down
2 changes: 1 addition & 1 deletion material/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ <h1>{{ page.title | default(config.site_name, true)}}</h1>
</div>
{% block scripts %}
<script src="{{ 'assets/javascripts/vendor.809e24aa.min.js' | url }}"></script>
<script src="{{ 'assets/javascripts/bundle.4e2bfc5d.min.js' | url }}"></script>
<script src="{{ 'assets/javascripts/bundle.ff925e8b.min.js' | url }}"></script>
{%- set translations = {} -%}
{%- for key in [
"clipboard.copy",
Expand Down
2 changes: 2 additions & 0 deletions src/assets/javascripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import {
SearchIndex
} from "integrations"
import {
patchCodeBlocks,
patchTables,
patchDetails,
patchScrollfix,
Expand Down Expand Up @@ -178,6 +179,7 @@ export function initialize(config: unknown) {

const keyboard$ = setupKeyboard()

patchCodeBlocks({ document$, viewport$ })
patchDetails({ document$, hash$ })
patchScripts({ document$ })
patchSource({ document$ })
Expand Down
76 changes: 76 additions & 0 deletions src/assets/javascripts/patches/code/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2016-2020 Martin Donath <martin.donath@squidfunk.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

import { Observable, combineLatest } from "rxjs"
import { distinctUntilKeyChanged, map } from "rxjs/operators"

import { Viewport, getElements } from "browser"

/* ----------------------------------------------------------------------------
* Helper types
* ------------------------------------------------------------------------- */

/**
* Mount options
*/
interface MountOptions {
document$: Observable<Document> /* Document observable */
viewport$: Observable<Viewport> /* Viewport observable */
}

/* ----------------------------------------------------------------------------
* Functions
* ------------------------------------------------------------------------- */

/**
* Patch all `code` elements
*
* This function will make overflowing code blocks focusable via keyboard, so
* they can be scrolled without a mouse.
*
* @param options - Options
*/
export function patchCodeBlocks(
{ document$, viewport$ }: MountOptions
): void {
const els$ = document$
.pipe(
map(() => getElements<HTMLTableElement>("pre > code"))
)

/* Observe viewport size only */
const size$ = viewport$
.pipe(
distinctUntilKeyChanged("size")
)

/* Make overflowing elements focusable */
combineLatest([els$, size$])
.subscribe(([els]) => {
for (const el of els) {
if (el.scrollWidth > el.clientWidth)
el.setAttribute("tabindex", "0")
else
el.removeAttribute("tabindex")
}
})
}
1 change: 1 addition & 0 deletions src/assets/javascripts/patches/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* IN THE SOFTWARE.
*/

export * from "./code"
export * from "./details"
export * from "./script"
export * from "./scrollfix"
Expand Down

0 comments on commit c6e2e3a

Please sign in to comment.