fix refactor codemirror
This commit is contained in:
@@ -1,97 +1,111 @@
|
|||||||
|
/* globals CodeMirror, challenge_Name */
|
||||||
|
// codeStorage
|
||||||
|
var codeStorageFactory = (function($, localStorage) {
|
||||||
|
|
||||||
|
var CodeStorageProps = {
|
||||||
|
version: 0.01,
|
||||||
|
keyVersion: 'saveVersion',
|
||||||
|
keyValue: null,
|
||||||
|
updateWait: 2000,
|
||||||
|
updateTimeoutId: null
|
||||||
|
};
|
||||||
|
|
||||||
|
var CodeStorage = {
|
||||||
|
hasSaved: function() {
|
||||||
|
return this.updateTimeoutId === null;
|
||||||
|
},
|
||||||
|
|
||||||
|
onSave: function(func) {
|
||||||
|
this.eventArray.push(func);
|
||||||
|
},
|
||||||
|
|
||||||
|
setSaveKey: function(key) {
|
||||||
|
this.keyValue = key + 'Val';
|
||||||
|
},
|
||||||
|
|
||||||
|
getStoredValue: function() {
|
||||||
|
return '' + localStorage.getItem(this.keyValue);
|
||||||
|
},
|
||||||
|
|
||||||
|
setEditor: function(editor) {
|
||||||
|
this.editor = editor;
|
||||||
|
},
|
||||||
|
|
||||||
|
isAlive: function() {
|
||||||
|
var val = this.getStoredValue();
|
||||||
|
return val !== 'null' &&
|
||||||
|
val !== 'undefined' &&
|
||||||
|
(val && val.length > 0);
|
||||||
|
},
|
||||||
|
|
||||||
|
updateStorage: function() {
|
||||||
|
if (typeof localStorage !== 'undefined') {
|
||||||
|
var value = this.editor.getValue();
|
||||||
|
localStorage.setItem(this.keyValue, value);
|
||||||
|
} else {
|
||||||
|
console.log('no web storage');
|
||||||
|
}
|
||||||
|
this.updateTimeoutId = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function codeStorageFactory(editor, challengeName) {
|
||||||
|
var codeStorage = Object.create(CodeStorage);
|
||||||
|
$.extend(codeStorage, CodeStorageProps);
|
||||||
|
codeStorage.setEditor(editor);
|
||||||
|
codeStorage.setSaveKey(challengeName);
|
||||||
|
return codeStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
var savedVersion = localStorage.getItem(CodeStorageProps.keyVersion);
|
||||||
|
if (savedVersion === null) {
|
||||||
|
localStorage.setItem(
|
||||||
|
CodeStorageProps.keyVersion,
|
||||||
|
CodeStorageProps.version
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return codeStorageFactory;
|
||||||
|
}($, localStorage));
|
||||||
|
|
||||||
var isInitRun = false;
|
var isInitRun = false;
|
||||||
var initPreview = true;
|
var initPreview = true;
|
||||||
var editor;
|
var editor;
|
||||||
var widgets = [];
|
|
||||||
editor = CodeMirror.fromTextArea(document.getElementById("codeEditor"), {
|
editor = CodeMirror.fromTextArea(document.getElementById('codeEditor'), {
|
||||||
lineNumbers: true,
|
lineNumbers: true,
|
||||||
mode: "text",
|
mode: 'text',
|
||||||
theme: 'monokai',
|
theme: 'monokai',
|
||||||
runnable: true,
|
runnable: true,
|
||||||
matchBrackets: true,
|
matchBrackets: true,
|
||||||
autoCloseBrackets: true,
|
autoCloseBrackets: true,
|
||||||
scrollbarStyle: 'null',
|
scrollbarStyle: 'null',
|
||||||
lineWrapping: true,
|
lineWrapping: true,
|
||||||
gutters: ["CodeMirror-lint-markers"]
|
gutters: ['CodeMirror-lint-markers']
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var codeStorage = codeStorageFactory(editor, challenge_Name);
|
||||||
var myCodeMirror = editor;
|
var myCodeMirror = editor;
|
||||||
|
|
||||||
var codeStorage = {
|
editor.on('keyup', function() {
|
||||||
version: 0.01,
|
clearTimeout(codeStorage.updateTimeoutId);
|
||||||
keyVersion:"saveVersion",
|
codeStorage.updateTimeoutId = setTimeout(
|
||||||
keyValue: null,//where the value of the editor is saved
|
codeStorage.updateStorage,
|
||||||
updateWait: 2000,// 2 seconds
|
codeStorage.updateWait
|
||||||
updateTimeoutId: null,
|
);
|
||||||
eventArray: []//for firing saves
|
|
||||||
};
|
|
||||||
codeStorage.hasSaved = function(){
|
|
||||||
return ( updateTimeoutId === null );
|
|
||||||
};
|
|
||||||
codeStorage.onSave = function(func){
|
|
||||||
codeStorage.eventArray.push(func);
|
|
||||||
};
|
|
||||||
codeStorage.setSaveKey = function(key){
|
|
||||||
codeStorage.keyValue = key + 'Val';
|
|
||||||
};
|
|
||||||
codeStorage.getEditorValue = function(){
|
|
||||||
return ('' + localStorage.getItem(codeStorage.keyValue));
|
|
||||||
};
|
|
||||||
|
|
||||||
codeStorage.isAlive = function() {
|
|
||||||
var val = this.getEditorValue();
|
|
||||||
return val !== 'null' &&
|
|
||||||
val !== 'undefined' &&
|
|
||||||
(val && val.length > 0);
|
|
||||||
};
|
|
||||||
codeStorage.updateStorage = function(){
|
|
||||||
if(typeof(Storage) !== undefined) {
|
|
||||||
var value = editor.getValue();
|
|
||||||
localStorage.setItem(codeStorage.keyValue, value);
|
|
||||||
} else {
|
|
||||||
var debugging = false;
|
|
||||||
if( debugging ){
|
|
||||||
console.log('no web storage');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
codeStorage.updateTimeoutId = null;
|
|
||||||
codeStorage.eventArray.forEach(function(func){
|
|
||||||
func();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
(function(){
|
|
||||||
var savedVersion = localStorage.getItem('saveVersion');
|
|
||||||
if( savedVersion === null ){
|
|
||||||
localStorage.setItem(codeStorage.keyVersion, codeStorage.version);//just write current version
|
|
||||||
}else{
|
|
||||||
if( savedVersion !== codeStorage.version ){
|
|
||||||
//Update version
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
codeStorage.setSaveKey(challenge_Name);
|
|
||||||
editor.on('keyup', function(){
|
|
||||||
window.clearTimeout(codeStorage.updateTimeoutId);
|
|
||||||
codeStorage.updateTimeoutId = window.setTimeout(codeStorage.updateStorage, codeStorage.updateWait);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
var editorValue;
|
var editorValue;
|
||||||
var challengeSeed = challengeSeed || null;
|
var challengeSeed = challengeSeed || null;
|
||||||
var tests = tests || [];
|
var tests = tests || [];
|
||||||
var allSeeds = '';
|
var allSeeds = '';
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
challengeSeed.forEach(function(elem) {
|
challengeSeed.forEach(function(elem) {
|
||||||
allSeeds += elem + '\n';
|
allSeeds += elem + '\n';
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
|
|
||||||
/*var defaultKeymap = {
|
|
||||||
'Cmd-E': 'emmet.expand_abbreviation',
|
|
||||||
'Tab': 'emmet.expand_abbreviation_with_tab',
|
|
||||||
'Enter': 'emmet.insert_formatted_line_break_only'
|
|
||||||
};
|
|
||||||
|
|
||||||
emmetCodeMirror(editor, defaultKeymap);*/
|
|
||||||
|
|
||||||
// Hijack tab key to insert two spaces instead
|
|
||||||
editor.setOption("extraKeys", {
|
editor.setOption("extraKeys", {
|
||||||
Tab: function(cm) {
|
Tab: function(cm) {
|
||||||
if (cm.somethingSelected()){
|
if (cm.somethingSelected()){
|
||||||
@@ -569,10 +583,14 @@ $('#submitButton').on('click', function() {
|
|||||||
bonfireExecute(true);
|
bonfireExecute(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
$(document).ready(function(){
|
$(document).ready(function() {
|
||||||
var $preview = $('#preview');
|
var $preview = $('#preview');
|
||||||
isInitRun = true;
|
isInitRun = true;
|
||||||
editorValue = (codeStorage.isAlive())? codeStorage.getEditorValue() : allSeeds;
|
|
||||||
|
editorValue = codeStorage.isAlive() ?
|
||||||
|
codeStorage.getStoredValue() :
|
||||||
|
allSeeds;
|
||||||
|
|
||||||
myCodeMirror.setValue(editorValue.replace(/fccss/gi, '<script>').replace(/fcces/gi, "</script>"));
|
myCodeMirror.setValue(editorValue.replace(/fccss/gi, '<script>').replace(/fcces/gi, "</script>"));
|
||||||
if (typeof $preview.html() !== 'undefined') {
|
if (typeof $preview.html() !== 'undefined') {
|
||||||
$preview.load(function(){
|
$preview.load(function(){
|
||||||
|
Reference in New Issue
Block a user