Skip to content

Commit

Permalink
Slash switches editor v8.2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
arnoudkooi committed Oct 28, 2024
1 parent d35c447 commit fc1725c
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 9 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
# CHANGELOG.md
## 8.2.3.0 (2024-10-28)
Features:
- Moving the advanced slashcommand and slasswitches editing to Monaco editor in a new browser tab

Fixes / changes:
- Improve handling of "link" type switches autocomplete (Slack)
- Changing setting to default on: Prioritize CTRL-/ and CMD-/ above OOB shortcut.




## 8.2.2.1 (2024-10-24)
Fixes / changes:
Expand Down
2 changes: 1 addition & 1 deletion inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ function snuSlashCommandAddListener() {
switchValue = snuslashswitchesvalueoverwrites[`${prop}.${tableName}`];
}
query = query.replace(val, "");
if (snuslashswitches[prop].type == "link") {
if (snuslashswitches[prop].type == "link" && (snufilter + thisKeyWithSpace).includes("-" + prop + " ")) {
targeturl = switchValue.replace(/\$0/g, tableName);
targeturl = targeturl.replace(/\$sysid/, mySysId);
targeturl = snuResolveVariables(targeturl).variableString;
Expand Down
167 changes: 167 additions & 0 deletions js/monaco/settingeditor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
let data;
let editor;
let versionid;
let language = 'json';

let monacoUrl = chrome.runtime.getURL('/') + 'js/monaco/vs';
require.config({
paths: {
'vs': monacoUrl
}
});

const params = new URLSearchParams(window.location.search);
let setting = params.get('setting') || "slashsswitches";
setting = ["slashsswitches","slashcommands","monacooptions"].includes(setting) ? setting : "slashsswitches";

let theme = "vs-dark";
let title = "Settings Editor - " + setting;

document.title = title;
document.getElementById('title').innerText = title;
require(['vs/editor/editor.main'], () => {

editor = monaco.editor.create(document.getElementById('container'), {
automaticLayout: true,
language: language,
theme: theme,
wordWrap: "on",
colorDecorators: true,
"bracketPairColorization.enabled": true
});

addActions(editor);

getFromSyncStorageGlobal("snusettings", function (data) {
let snusettings = data;
editor.setValue(snusettings[setting]);
setTimeout(() => {
editor.getAction('editor.action.formatDocument').run();
editor.focus();
}, 100);
versionid = getEditor().getModel().getAlternativeVersionId();
})

});


document.querySelector('button#save').addEventListener('click', e => {
saveSettings();
});



function addActions(editor) {

const blockContext = "editorTextFocus && !suggestWidgetVisible && !renameInputVisible && !inSnippetMode && !quickFixWidgetVisible";
editor.addAction({
id: "updateRecord",
label: "Save",
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS],
contextMenuGroupId: "2_execution",
precondition: blockContext,
run: () => {
saveSettings();
},
});

editor.addAction({
id: "google",
label: "Search Google",
contextMenuGroupId: "2_execution",
precondition: "editorHasSelection",
run: (editor) => {
let selection = getEditor().getModel().getValueInRange(editor.getSelection());
window.open('https://www.google.com/search?q=' + selection);
}
})

}


function getEditor() {
return (typeof editor.getValue !== 'undefined') ?
editor : editor.getModifiedEditor();
}



window.onbeforeunload = function (e) {
if (versionid == getEditor().getModel().getAlternativeVersionId()) return null
e = e || window.event;
return 'Closing tab will loose unsaved work, continue?';
};




//get an instance independent sync parameter
function getFromSyncStorageGlobal(theName, callback) {
chrome.storage.sync.get(theName, function (resSync) {
var dataSync = resSync[theName];

if (typeof dataSync !== 'object'){ //only objects can become large and merged.
callback(dataSync);
return;
}

getFromChromeStorageGlobal(theName,function (resLocal) {
var objLocal = resLocal || {};
var objMerged = { ...dataSync, ...objLocal};
callback(objMerged);
});
});
}

//get an instance independent global parameter
function getFromChromeStorageGlobal(theName, callback) {
chrome.storage.local.get(theName, function (result) {
callback(result[theName]);
});
}

function saveSettings() {

const jsonSetting = JSON.parse(editor.getValue());
const minifiedSetting = JSON.stringify(jsonSetting);

getFromSyncStorageGlobal("snusettings", function (data) { //get the current settings in case it was edited in the popup
snusettingsSync = data;
snusettingsSync[setting] = minifiedSetting;

snusettings = {};

Object.keys(snusettingsSync).forEach(key => {
if (key.length >= 5000) { //overflow to local storage #204
snusettings[key] = '' + snusettingsSync[key];
delete snusettingsSync[key];
}
});

setToChromeSyncStorageGlobal("snusettings", snusettingsSync);
setToChromeStorageGlobal("snusettings", snusettings);

document.querySelector('#response').innerHTML = `Saved: ${new Date().toLocaleTimeString()}`;
versionid = getEditor().getModel().getAlternativeVersionId();

})

}

//set an instance independent sync parameter
function setToChromeSyncStorageGlobal(theName, theValue) {
var myobj = {};
myobj[theName] = theValue;
chrome.storage.sync.set(myobj, function () {

});
}

//set an instance independent parameter
function setToChromeStorageGlobal(theName, theValue) {
//console.log(theName, theValue)
var myobj = {};
myobj[theName] = theValue;
chrome.storage.local.set(myobj, function () {
});
}
6 changes: 3 additions & 3 deletions popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ <h5>Settings <i>(Reload browser after changing settings to take affect)</i></h5>
Slashcommands:
</label>
<div class="form-check" title="">
<input class="form-check-input snu-setting" type="checkbox" id="slashpopuppriority"
<input class="form-check-input snu-setting" type="checkbox" checked="checked" id="slashpopuppriority"
name="slashpopuppriority" >
<label class="form-check-label" for="slashpopuppriority" title="Prioritize the slashcommand popup above shortcuts, hit shortcut again to show shortcuts">
Prioritize CTRL-/ and CMD-/ above OOB shortcut.
Expand Down Expand Up @@ -488,11 +488,11 @@ <h5>Settings <i>(Reload browser after changing settings to take affect)</i></h5>
</div>
<hr />
<h5>Slash Commands</h5>
Advanced, please use the <a id="helperslashcommands" href='#tabslashcommands'>Slashcommands</a> tab to manage custom commands, or doubleclick textarea to remove readonly.
Advanced, please use the <a id="helperslashcommands" href='#tabslashcommands'>Slashcommands</a> tab to manage custom commands, or doubleclick textarea to open editor.
<textarea class="form-control form-control-sm snu-setting" readonly="readonly" autocomplete="off" autocapitalize="off"
spellcheck="false" id="slashcommands"
placeholder="Go to slashcommands tab to create custom commands"></textarea>
Advanced, <a href="https://arnoudkooi.com/switch" target="_blank">custom switches</a> . doubleclick textarea to remove readonly.
Advanced, <a href="https://arnoudkooi.com/switch" target="_blank">custom switches</a> . doubleclick textarea to open editor.
<textarea class="form-control form-control-sm snu-setting" readonly="readonly" autocomplete="off" autocapitalize="off"
spellcheck="false" id="slashsswitches"
placeholder="Advanced, pro's only ;-)"></textarea>
Expand Down
17 changes: 12 additions & 5 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ function setBrowserVariables(obj) {
});

$('.snu-setting').change(function () {
setSettings();
saveSettings();
});

$('.snu-instance-setting').change(function () {
Expand Down Expand Up @@ -441,7 +441,14 @@ function setBrowserVariables(obj) {
});

$('#slashcommands, #slashsswitches').on('dblclick',function(){
$(this).prop('readonly','');

var extensionUrl = chrome.runtime.getURL("settingeditor.html");
var createObj = {
'url': extensionUrl + "?setting=" + this.id,
'active': true
}
chrome.tabs.create(createObj);

})

$('#iconallowbadge').on('change',function(){
Expand Down Expand Up @@ -608,7 +615,7 @@ function getSettings(callback) {
})
}

function setSettings() {
function saveSettings() {
var snusettingsSync = {};
var snusettings = {}
$('.snu-setting').each(function (index, item) {
Expand Down Expand Up @@ -1347,7 +1354,7 @@ function getSlashcommands() {
if (!confirm("Delete command " + cmd + "?")) return;
delete objCustomCommands[cmd];
$('#slashcommands').val(JSON.stringify(objCustomCommands));
setSettings();
saveSettings();
getSlashcommands();

});
Expand Down Expand Up @@ -1401,7 +1408,7 @@ function getSlashcommands() {
response => { });
});

setSettings();
saveSettings();
getSlashcommands();

chrome.runtime.sendMessage({ "event" : "initializecontextmenus"});
Expand Down
43 changes: 43 additions & 0 deletions settingeditor.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code Editor</title>

<link rel="stylesheet" href="css/codeeditor.css">
<script src="js/monaco/vs/loader.js"></script>

</head>

<body>
<section id="snUtilsMonocoEditor">
<header id="header">
<div class="title">
<span class="title" id="title">
Settings editor
</span>
</div>
<div id="state">


<!-- <div class="btn-group btns" title="Swith the autocomplete library">
<button id="sercerglobal" type="button" class="btn">Server<div>Global</div></button>
<button id="sercerscoped" class="btn">Server<div>Scoped</div></button>
<button id="client" class="btn">Client<div></div></button>
</div> -->
<button id="save">Save</button>
</div>
</header>
<section id="container"></section>
<footer id="snutils-monoco-footer">
<div class="record-meta">Do not modify settings in popup while editing here.</div>
<span id="response"></span>
</footer>
</section>
<script src="js/monaco/settingeditor.js"></script>

</body>

</html>

0 comments on commit fc1725c

Please sign in to comment.