-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d35c447
commit fc1725c
Showing
6 changed files
with
236 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () { | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |