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 initPreview = true;
|
||||
var editor;
|
||||
var widgets = [];
|
||||
editor = CodeMirror.fromTextArea(document.getElementById("codeEditor"), {
|
||||
lineNumbers: true,
|
||||
mode: "text",
|
||||
theme: 'monokai',
|
||||
runnable: true,
|
||||
matchBrackets: true,
|
||||
autoCloseBrackets: true,
|
||||
scrollbarStyle: 'null',
|
||||
lineWrapping: true,
|
||||
gutters: ["CodeMirror-lint-markers"]
|
||||
|
||||
editor = CodeMirror.fromTextArea(document.getElementById('codeEditor'), {
|
||||
lineNumbers: true,
|
||||
mode: 'text',
|
||||
theme: 'monokai',
|
||||
runnable: true,
|
||||
matchBrackets: true,
|
||||
autoCloseBrackets: true,
|
||||
scrollbarStyle: 'null',
|
||||
lineWrapping: true,
|
||||
gutters: ['CodeMirror-lint-markers']
|
||||
});
|
||||
|
||||
var codeStorage = codeStorageFactory(editor, challenge_Name);
|
||||
var myCodeMirror = editor;
|
||||
|
||||
var codeStorage = {
|
||||
version: 0.01,
|
||||
keyVersion:"saveVersion",
|
||||
keyValue: null,//where the value of the editor is saved
|
||||
updateWait: 2000,// 2 seconds
|
||||
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);
|
||||
editor.on('keyup', function() {
|
||||
clearTimeout(codeStorage.updateTimeoutId);
|
||||
codeStorage.updateTimeoutId = setTimeout(
|
||||
codeStorage.updateStorage,
|
||||
codeStorage.updateWait
|
||||
);
|
||||
});
|
||||
|
||||
var editorValue;
|
||||
var challengeSeed = challengeSeed || null;
|
||||
var tests = tests || [];
|
||||
var allSeeds = '';
|
||||
|
||||
(function() {
|
||||
challengeSeed.forEach(function(elem) {
|
||||
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", {
|
||||
Tab: function(cm) {
|
||||
if (cm.somethingSelected()){
|
||||
@@ -569,10 +583,14 @@ $('#submitButton').on('click', function() {
|
||||
bonfireExecute(true);
|
||||
});
|
||||
|
||||
$(document).ready(function(){
|
||||
$(document).ready(function() {
|
||||
var $preview = $('#preview');
|
||||
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>"));
|
||||
if (typeof $preview.html() !== 'undefined') {
|
||||
$preview.load(function(){
|
||||
|
Reference in New Issue
Block a user