Skip to content

Instantly share code, notes, and snippets.

@FWDekker
Last active September 2, 2021 10:28
Show Gist options
  • Save FWDekker/364585d7eee2cc5ac690a8276aaab62b to your computer and use it in GitHub Desktop.
Save FWDekker/364585d7eee2cc5ac690a8276aaab62b to your computer and use it in GitHub Desktop.
Saves the Ace editor's settings in a cookie
// ==UserScript==
// @name WebLab settings saver
// @namespace https://fwdekker.com/
// @version 0.4
// @description Saves Ace editor settings in Weblab. Use Ctrl+Shift+[ to save settings, use Ctrl+Shift+] to load settings.
// @author F.W. Dekker
// @match https://weblab.tudelft.nl/*
// @grant none
// ==/UserScript==
(function()
{
'use strict';
var cookieName = "weblab_settings";
//////
///
/// Editor settings
///
//////
// Initialises the editor settings part
function initEditorSettings()
{
loadOptions();
registerCommand({
name: "saveSettings",
bindKey: {win: "Ctrl-Shift-[", mac: "Command-Shift-["},
exec: function(editor)
{
if (confirm("Save options?"))
{
saveOptions();
}
}
});
registerCommand({
name: "loadSettings",
bindKey: {win: "Ctrl-Shift-]", mac: "Command-Shift-]"},
exec: function(editor)
{
if (confirm("Load options?"))
{
if (loadOptions() !== 0)
{
alert("Could not load settings: No settings were saved");
}
}
}
});
}
// Saves cookie data. Cookies will be valid for 25 years.
function writeCookie(name, data)
{
var date = new Date();
date.setTime(date.getTime() + (25 * 365 * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
document.cookie = name + "=" + data + expires + "; path=/";
}
// Loads and returns cookie data.
function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(";");
for (var i=0; i < ca.length; i++)
{
var c = ca[i];
while (c.charAt(0) == " ")
{
c = c.substring(1,c.length);
}
if (c.indexOf(nameEQ) === 0)
{
return c.substring(nameEQ.length, c.length);
}
}
return null;
}
// Saves editor options to a cookie.
function saveOptions()
{
var options = editorInstances[0].getOptions();
writeCookie(cookieName, JSON.stringify(options));
}
// Loads editor options from a cookie.
function loadOptions()
{
var rawOptions = readCookie(cookieName);
if (rawOptions === null)
{
return -1;
}
var options = JSON.parse(rawOptions);
editorInstances[0].setOptions(options);
editorInstances[1].setOptions(options);
return 0;
}
//////
///
/// Folding
///
//////
// Initialises the folding part
function initFolding()
{
registerCommand({
name: "foldall",
bindKey: {win: "Ctrl+Shift+-", mac: "Ctrl-Command-Option-0"},
exec: function(editor)
{
editor.session.foldAll();
}
});
registerCommand({
name: "unfoldall",
bindKey: {win: "Ctrl+Shift++", mac: "Command-Option-Shift-0"},
exec: function(editor)
{
foldToLevel(editor, undefined);
}
});
registerCommand({
name: "unfold-to-1",
bindKey: {win: "Ctrl+Shift+1", mac: "Command-Option-Shift-1"},
exec: function(editor)
{
foldToLevel(editor, 1);
}
});
registerCommand({
name: "unfold-to-2",
bindKey: {win: "Ctrl+Shift+2", mac: "Command-Option-Shift-2"},
exec: function(editor)
{
foldToLevel(editor, 2);
}
});
registerCommand({
name: "unfold-to-3",
bindKey: {win: "Ctrl+Shift+3", mac: "Command-Option-Shift-3"},
exec: function(editor)
{
foldToLevel(editor, 3);
}
});
registerCommand({
name: "unfold-to-4",
bindKey: {win: "Ctrl+Shift+4", mac: "Command-Option-Shift-4"},
exec: function(editor)
{
foldToLevel(editor, 4);
}
});
registerCommand({
name: "unfold-to-5",
bindKey: {win: "Ctrl+Shift+5", mac: "Command-Option-Shift-5"},
exec: function(editor)
{
foldToLevel(editor, 5);
}
});
}
// If level is 0, all widgets are folded. If level is 1, only the outermost widgets are unfolded. Et cetera.
// If level is undefined, all widgets will be unfolded.
function foldToLevel(editor, level)
{
if (level === undefined)
{
editor.session.unfold();
return;
}
editor.session.foldAll();
var folds;
for (var i = 0; i < level; i++)
{
folds = editor.session.getAllFolds();
if (folds.length === 0)
{
break;
}
editor.session.expandFolds(folds);
}
}
//////
///
/// Key bindings
///
//////
function registerCommand(command)
{
editorInstances[0].commands.addCommand(command);
editorInstances[1].commands.addCommand(command);
}
$(window).load(function()
{
initEditorSettings();
initFolding();
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment