Skip to content

Commit

Permalink
Consolidate Apple OS detection
Browse files Browse the repository at this point in the history
There were previously two functions that checked for Apple OS.

Converts previous OS module to Styling and modernises the function in Helpers.
  • Loading branch information
DavidOliver committed Jun 6, 2024
1 parent c5f3632 commit 4a9a573
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions assets/js/entry/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { initialize as initTooltips } from '../tooltips/tooltips'
import { initialize as initHintsPage } from '../tooltips/hint-page'
import { initialize as initCopyButton } from '../copy-button'
import { initialize as initSettings } from '../settings'
import { initialize as initOs } from '../os'
import { initialize as initStyling } from '../styling'
import { initialize as initTabsets } from '../tabsets'
import { initialize as initPreview} from '../preview'

Expand All @@ -31,7 +31,7 @@ onDocumentReady(() => {
initTooltips()
initHintsPage()
initCopyButton()
initOs()
initStyling()
initTabsets()

if (isPreview) {
Expand Down
6 changes: 3 additions & 3 deletions assets/js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ export function getProjectNameAndVersion () {
}

/**
* Return `true` if the client's OS is MacOS.
* Return `true` if the client's OS is Apple.
*
* @return {Boolean}
*/
export function isMacOS () {
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform)
export function isAppleOS () {
return /(Macintosh|iPhone|iPad|iPod)/.test(window.navigator.userAgent)
}
4 changes: 2 additions & 2 deletions assets/js/keyboard-shortcuts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isMacOS, qs } from './helpers'
import { isAppleOS, qs } from './helpers'
import { toggleSidebar } from './sidebar/sidebar-drawer'
import { focusSearchInput } from './search-bar'
import { cycleTheme } from './theme'
Expand Down Expand Up @@ -72,7 +72,7 @@ function handleKeyDown (event) {

const matchingShortcut = keyboardShortcuts.find(shortcut => {
if (shortcut.hasModifier) {
if (isMacOS() && event.metaKey) { return shortcut.key === event.key }
if (isAppleOS() && event.metaKey) { return shortcut.key === event.key }
if (event.ctrlKey) { return shortcut.key === event.key }

return false
Expand Down
6 changes: 0 additions & 6 deletions assets/js/os.js

This file was deleted.

8 changes: 4 additions & 4 deletions assets/js/search-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
AUTOCOMPLETE_CONTAINER_SELECTOR,
AUTOCOMPLETE_SUGGESTION_LIST_SELECTOR
} from './autocomplete/autocomplete-list'
import { isMacOS, qs } from './helpers'
import { isAppleOS, qs } from './helpers'

const SEARCH_INPUT_SELECTOR = 'form.search-bar input'
const SEARCH_CLOSE_BUTTON_SELECTOR = 'form.search-bar .search-close-button'
Expand Down Expand Up @@ -67,17 +67,17 @@ function addEventListeners () {
}

searchInput.addEventListener('keydown', event => {
const macOS = isMacOS()
const appleOS = isAppleOS()

if (event.key === 'Escape') {
clearSearch()
searchInput.blur()
} else if (event.key === 'Enter') {
handleAutocompleteFormSubmission(event)
} else if (event.key === 'ArrowUp' || (macOS && event.ctrlKey && event.key === 'p')) {
} else if (event.key === 'ArrowUp' || (appleOS && event.ctrlKey && event.key === 'p')) {
moveAutocompleteSelection(-1)
event.preventDefault()
} else if (event.key === 'ArrowDown' || (macOS && event.ctrlKey && event.key === 'n')) {
} else if (event.key === 'ArrowDown' || (appleOS && event.ctrlKey && event.key === 'n')) {
moveAutocompleteSelection(1)
event.preventDefault()
} else if (event.key === 'Tab') {
Expand Down
6 changes: 6 additions & 0 deletions assets/js/styling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { isAppleOS } from "./helpers"

Check failure on line 1 in assets/js/styling.js

View workflow job for this annotation

GitHub Actions / Check JS

Strings must use singlequote

export function initialize () {
const osClass = isAppleOS() ? 'apple-os' : 'non-apple-os'
document.documentElement.classList.add(osClass)
}

0 comments on commit 4a9a573

Please sign in to comment.