programmingfonts/index.js

190 lines
6.0 KiB
JavaScript
Raw Normal View History

2017-09-02 21:26:30 +02:00
/* global CodeMirror window document $ */
/* eslint-disable no-implicit-globals */
2017-07-28 23:07:09 +02:00
2017-01-20 17:59:59 +01:00
// CodeMirror
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
theme: "pastel-on-dark",
lineWrapping: true
});
// CodeMirror theme selector
var input = document.getElementById("select-theme");
2017-09-02 21:26:30 +02:00
2017-01-20 17:59:59 +01:00
function selectTheme() {
2017-09-02 21:26:30 +02:00
var theme = "monokai";
2017-07-29 11:41:12 +02:00
if (input.selectedIndex > -1) {
2017-09-02 21:26:30 +02:00
theme = input.options[input.selectedIndex].innerHTML;
2017-07-29 11:41:12 +02:00
}
2017-01-20 17:59:59 +01:00
editor.setOption("theme", theme);
2017-07-29 11:36:07 +02:00
document.cookie = "theme=" + theme + ";max-age=172800";
2017-01-20 17:59:59 +01:00
}
// ProgrammingFonts font selector
function selectFont() {
2017-07-28 23:07:09 +02:00
var font = window.location.hash.substring(1);
2017-01-20 17:59:59 +01:00
2017-10-26 21:03:02 +02:00
if (font === "") {
font = "input";
}
2017-01-20 17:59:59 +01:00
if (font === "input") {
$("pre").css({ fontFamily: "Input Mono, monospace" });
$("textarea").css({ fontFamily: "Input Mono, monospace" });
$(".CodeMirror").css({ fontFamily: "Input Mono, monospace" });
} else {
$("pre").css({ fontFamily: font + ", monospace" });
$("textarea").css({ fontFamily: font + ", monospace" });
$(".CodeMirror").css({ fontFamily: font + ", monospace" });
}
2017-07-29 17:40:29 +02:00
$("#select-font a[data-value]").removeClass("active");
$("#select-font a[data-value='" + font + "']").addClass("active");
2017-07-28 23:07:09 +02:00
2017-07-29 11:36:07 +02:00
document.cookie = "font=" + font + ";max-age=172800";
2017-01-20 17:59:59 +01:00
}
2017-07-28 23:07:09 +02:00
window.onhashchange = selectFont;
2017-01-20 17:59:59 +01:00
function setSize() {
var size = $("#size").val();
2017-09-02 21:26:30 +02:00
$(".CodeMirror").css({ fontSize: size + "px" });
2017-07-29 11:36:07 +02:00
document.cookie = "size=" + size + ";max-age=172800";
2017-01-20 17:59:59 +01:00
}
function setSpacing() {
var spacing = $("#spacing").val();
2017-09-02 21:26:30 +02:00
$(".CodeMirror").css({ lineHeight: spacing });
2017-07-29 11:36:07 +02:00
document.cookie = "spacing=" + spacing + ";max-age=172800";
2017-01-20 17:59:59 +01:00
}
function setAntialiasing() {
if ($("#aliasing").is(":checked")) {
$(".CodeMirror").removeClass("no-smooth");
2017-09-02 21:26:30 +02:00
document.cookie = "antialiasing=smooth;max-age=172800";
2017-01-20 17:59:59 +01:00
} else {
$(".CodeMirror").addClass("no-smooth");
2017-09-02 21:26:30 +02:00
document.cookie = "antialiasing=no-smooth;max-age=172800";
2017-01-20 17:59:59 +01:00
}
}
function selectLanguage() {
2017-07-24 22:48:10 +02:00
var lang = $("#select-language").val();
2017-09-02 21:26:30 +02:00
2017-07-24 22:48:10 +02:00
editor.setOption("mode", lang.toLowerCase());
2017-07-29 11:36:07 +02:00
document.cookie = "language=" + lang + ";max-age=172800";
2017-01-20 17:59:59 +01:00
}
2019-01-22 19:45:16 +01:00
function renderSelectList() {
$("#select-font").empty();
var icon = '<svg class="octicon" viewBox="0 0 12 14" version="1.1" width="12" height="14" aria-hidden="true"><path fill-rule="evenodd" d="M11 10h1v3c0 .55-.45 1-1 1H1c-.55 0-1-.45-1-1V3c0-.55.45-1 1-1h3v1H1v10h10v-3zM6 2l2.25 2.25L5 7.5 6.5 9l3.25-3.25L12 8V2H6z"></path></svg>';
2019-01-22 21:29:02 +01:00
var pinIcon = '<svg class="octicon octicon-pin" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M10 1.2V2l.5 1L6 6H2.2c-.44 0-.67.53-.34.86L5 10l-4 5 5-4 3.14 3.14a.5.5 0 0 0 .86-.34V10l3-4.5 1 .5h.8c.44 0 .67-.53.34-.86L10.86.86a.5.5 0 0 0-.86.34z"></path></svg>';
2019-01-22 19:45:16 +01:00
var favoritesMap = {};
try {
var favorites = JSON.parse(localStorage.getItem('favorites')) || [];
favoritesMap = favorites.reduce(function(acc, alias) {
acc[alias] = true;
return acc;
}, {});
} catch (err) {
console.error('could not render favorites', err);
}
$.getJSON("fonts.json", function(data) {
data.sort(function(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;
});
$.each(data, function(k, v) {
var liga_info = "";
var render_info = "";
if (v.ligatures) {
liga_info = ", ligatures";
}
if (v.rendering === "bitmap") {
render_info = ", bitmap";
}
$("#select-font").append(
"<div class='entry'>" +
2019-01-23 20:38:17 +01:00
"<a href='#" + v.alias + "' data-value=\"" + v.alias + "\">" +
"<span class='name'>" + v.name + "</span>" +
"<span class='details'>" + v.author + " (" + v.year + ") — " + v.style + render_info + liga_info + "</span>" +
"</a>" +
"<a class=\"favoritelink" + (favoritesMap[v.alias] ? ' pinned' : '') + "\" onclick=\"toggleFavorite('" + v.alias + "')\">" +
pinIcon +
"</a>" +
2019-01-22 19:45:16 +01:00
"<a class='website' href='" + v.website + "' rel=external> <span>Info & Download</span>" + icon + "</a></div>"
);
});
selectFont();
});
}
function toggleFavorite(alias) {
try {
var favorites = JSON.parse(localStorage.getItem('favorites')) || [];
if (favorites.indexOf(alias) > -1) {
favorites = favorites.filter(function(v) {
return v !== alias;
})
} else {
favorites.push(alias);
}
localStorage.setItem('favorites', JSON.stringify(Array.from(new Set(favorites))));
} catch (err) {
console.error('could not save favorite', err);
}
renderSelectList();
2019-01-22 21:29:02 +01:00
return false;
2019-01-22 19:45:16 +01:00
}
2017-09-02 21:26:30 +02:00
$(document).ready(function() {
var cookieValueSpacing = document.cookie.replace(/(?:(?:^|.*;\s*)spacing\s*=\s*([^;]*).*$)|^.*$/, "$1");
var cookieValueSize = document.cookie.replace(/(?:(?:^|.*;\s*)size\s*=\s*([^;]*).*$)|^.*$/, "$1");
var cookieValueAntialiasing = document.cookie.replace(/(?:(?:^|.*;\s*)antialiasing\s*=\s*([^;]*).*$)|^.*$/, "$1");
var cookieValueTheme = document.cookie.replace(/(?:(?:^|.*;\s*)theme\s*=\s*([^;]*).*$)|^.*$/, "$1");
var cookieValueLanguage = document.cookie.replace(/(?:(?:^|.*;\s*)language\s*=\s*([^;]*).*$)|^.*$/, "$1");
if (cookieValueSpacing !== "") {
$("#spacing").val(cookieValueSpacing);
}
if (cookieValueSize !== "") {
$("#size").val(cookieValueSize);
}
if (cookieValueAntialiasing === "smooth") {
2017-09-02 21:26:30 +02:00
$("#aliasing").prop("checked", true);
} else if (cookieValueAntialiasing === "no-smooth") {
2017-09-02 21:26:30 +02:00
$("#aliasing").prop("checked", false);
}
2017-07-24 22:48:10 +02:00
if (cookieValueTheme !== "") {
$("#select-theme").val(cookieValueTheme);
}
if (cookieValueLanguage !== "") {
$("#select-language").val(cookieValueLanguage);
}
2017-07-24 22:48:10 +02:00
selectTheme();
2017-01-20 17:59:59 +01:00
setSize();
setSpacing();
setAntialiasing();
2017-07-24 22:48:10 +02:00
selectLanguage();
2019-01-22 19:45:16 +01:00
renderSelectList();
2017-01-20 17:59:59 +01:00
$("#theme-next").click(function() {
$("#select-theme :selected").next().prop("selected", true);
2017-01-20 17:59:59 +01:00
selectTheme();
});
$("#theme-previous").click(function() {
$("#select-theme :selected").prev().prop("selected", true);
2017-01-20 17:59:59 +01:00
selectTheme();
});
});