/* global CodeMirror plausible */ import { Cookies } from './modules/cookies.js' import { Fontsize } from './modules/fontsize.js' import { Filters } from './modules/filters.js' import { Language } from './modules/language.js' import { Spacing } from './modules/spacing.js' import { Theme } from './modules/theme.js' let fontData const fontsize = new Fontsize() window.CMeditor = CodeMirror.fromTextArea(document.getElementById('code'), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, theme: 'pastel-on-dark', lineWrapping: true }) /** * Get the font from the #, the cookie, or a default */ function getFont () { let font = window.location.hash.substring(1) if (!font) { font = Cookies.get('font') } if (!font) { font = 'source-code-pro' } return font } // ProgrammingFonts font selector const selectFont = () => { const msg = document.querySelector('footer .subtitle') const codeMirror = document.querySelector('.CodeMirror') const font = getFont() if (typeof fontData !== 'undefined') { msg.innerHTML = `Test drive ${fontData[font].name}!` } if (typeof fontData !== 'undefined' && fontData[font].rendering === 'bitmap') { codeMirror.classList.add('no-smooth') if (fontData[font]['bitmap size']) { fontsize.forceSize(fontData[font]['bitmap size']) } } else { codeMirror.classList.remove('no-smooth') fontsize.reset() } if (font === 'input') { codeMirror.style.fontFamily = 'Input Mono, monospace' codeMirror.querySelectorAll('pre, textarea').forEach((element) => { element.style.fontFamily = 'Input Mono, monospace' }) } else { codeMirror.style.fontFamily = `${font}, monospace` codeMirror.querySelectorAll('pre, textarea').forEach((element) => { element.style.fontFamily = `${font}, monospace` }) } document.querySelectorAll('#select-font [data-alias]').forEach((element) => { element.classList.remove('active') }) document.querySelectorAll(`#select-font [data-alias='${font}']`).forEach((element) => { element.classList.add('active') }) Cookies.set('font', font) } function renderSelectList () { const icon = '' const pinIcon = '' let favoritesMap = {} let favorites = [] const ajax = new XMLHttpRequest() document.getElementById('select-font').innerHTML = '' try { favorites = JSON.parse(localStorage.getItem('favorites')) || [] favoritesMap = favorites.reduce((acc, alias) => { acc[alias] = true return acc }, {}) } catch (err) { // eslint-disable-next-line no-console console.error('could not render favorites', err) } ajax.onreadystatechange = () => { const fonts = [] const authors = [] if (ajax.readyState === 4 && ajax.status === 200) { fontData = ajax.response Object.keys(fontData).forEach((key) => { const v = fontData[key] v.alias = key fonts.push(v) if (authors.indexOf(v.author) < 0) { authors.push(v.author) } }) } authors.sort() fonts.sort((a, b) => { if (favoritesMap[a.alias] && !favoritesMap[b.alias]) { return -1 } if (!favoritesMap[a.alias] && favoritesMap[b.alias]) { return 1 } if (a.name.toLowerCase() < b.name.toLowerCase()) { return -1 } if (a.name.toLowerCase() > b.name.toLowerCase()) { return 1 } return 0 }) authors.forEach((author) => { const option = document.createElement('option') option.innerHTML = author document.getElementById('authors-list').querySelector('.other').appendChild(option) }) fonts.forEach((v) => { const option = document.createElement('div') option.classList.add('entry') if (favoritesMap[v.alias]) { option.classList.add('pinned') } option.setAttribute('data-alias', v.alias) option.innerHTML = `${v.name}${v.year} — ${v.author}${pinIcon} Website${icon}` document.getElementById('select-font').appendChild(option) }) selectFont() new Filters(fontData).init() } ajax.responseType = 'json' ajax.open('GET', 'fonts.json', true) ajax.send() } window.toggleFavorite = (alias) => { try { let favorites = JSON.parse(localStorage.getItem('favorites')) || [] if (favorites.indexOf(alias) > -1) { favorites = favorites.filter((v) => { return v !== alias }) } else { favorites.push(alias) } localStorage.setItem('favorites', JSON.stringify(Array.from(new Set(favorites)))) } catch (err) { // eslint-disable-next-line no-console console.error('could not save favorite', err) } renderSelectList() return false } function walk (direction) { const activeEntry = document.querySelector('.entry.active') let target = null let next = direction === 'up' ? activeEntry.previousElementSibling : activeEntry.nextElementSibling while (target === null) { if (next) { if (next.matches('.entry:not(.filtered-out)')) { target = next } else { next = direction === 'up' ? next.previousElementSibling : next.nextElementSibling } } else { target = false } } function isVisible (el) { const offset = document.getElementById('filters').getBoundingClientRect().height const container = document.querySelector('section.select-list').getBoundingClientRect().height if (direction === 'up') { if (el.getBoundingClientRect().top < offset) { return false } } else { if (el.getBoundingClientRect().bottom > offset + container) { return false } } return true } if (target) { target.querySelector('a').click() if (!isVisible(target)) { target.scrollIntoView() } } } window.onhashchange = () => { plausible('Font Selected') selectFont() } window.addEventListener('DOMContentLoaded', () => { renderSelectList() new Theme().init() fontsize.init() new Spacing().init() new Language().init() document.querySelector('.select-list').onkeyup = (event) => { if (event.ctrlKey || event.altKey || event.metaKey || event.shiftKey) { return } if (event.key === 'ArrowUp') { event.preventDefault() event.stopPropagation() walk('up') } else if (event.key === 'ArrowDown') { event.preventDefault() event.stopPropagation() walk('down') } } document.body.addEventListener('keydown', (event) => { if (event.ctrlKey || event.metaKey) { if (event.key === '-') { event.preventDefault() event.stopPropagation() fontsize.down() } else if (event.key === '=') { event.preventDefault() event.stopPropagation() fontsize.up() } } }) })