Skip to content

Commit

Permalink
removed editors dependency on firebase for init
Browse files Browse the repository at this point in the history
  • Loading branch information
vickeykumar committed Jun 16, 2024
1 parent 8ff02c7 commit 7fad387
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/resources/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,8 @@ <h3 class="section__title">Share/Collaborate</h3>
<script src="https://cdnjs.cloudflare.com/ajax/libs/firebase/9.17.1/firebase-auth-compat.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/firebase/9.17.1/firebase-database-compat.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.11/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.11/ext-language_tools.min.js" type="text/javascript" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.9.6/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.9.6/ext-language_tools.min.js" type="text/javascript" charset="utf-8"></script>
<script src="./js/filesaver.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.15/jstree.min.js"></script>
<script src="./js/scribbler.js"></script>
Expand Down
73 changes: 41 additions & 32 deletions src/resources/js/scribbler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1151,9 +1151,11 @@ $(function() {
// apply the changes in the future only
var openPageTimestamp = Date.now();

// Take the editor value on start and set it in the editor
currentEditorValue.child("content").once("value", function (contentRef) {

var editorInitialized = false;
const initializeEditorApp = (initalcontent="/* Welcome to openrepl! */") => {
if (!editorInitialized) {
//all init for editor goes here
editorInitialized = true;
// Somebody changed the lang. Hey, we have to update it in our editor too!
currentEditorValue.child("lang").on("value", function (r) {
let langdata = r.val();
Expand All @@ -1180,29 +1182,29 @@ $(function() {
// Initialize the ACE editor
editor = ace.edit("editor");
editor.setTheme(getTheme());
editor.setFontSize("14px");
editor.setFontSize("14px");
editor.$blockScrolling = Infinity;
editor.setOptions({
enableBasicAutocompletion: true,
enableSnippets: true,
enableLiveAutocompletion: true
});
// drag and drop feature
editor.container.addEventListener("dragover", function(e) {
e.preventDefault(); // prevent default behaviour given by browser
});

editor.container.addEventListener("drop", function(e) {
e.preventDefault();
var file = e.dataTransfer.files[0];
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
editor.setValue(contents);
};
reader.readAsText(file);
});
// drag and drop feature
editor.container.addEventListener("dragover", function(e) {
e.preventDefault(); // prevent default behaviour given by browser
});

editor.container.addEventListener("drop", function(e) {
e.preventDefault();
var file = e.dataTransfer.files[0];
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
editor.setValue(contents);
};
reader.readAsText(file);
});

// key binding for file save
editor.commands.addCommand({
Expand Down Expand Up @@ -1288,33 +1290,30 @@ $(function() {
// 3. Turn off the applyingDeltas
applyingDeltas = false;
});

// Get the current content
var val = contentRef.val();

// If the editor doesn't exist already....
if (val === null) {
if (initalcontent === null) {
// ...we will initialize a new one.
// ...with this content:
if (window[CONTENT_KEY]) {
val = window[CONTENT_KEY];
initalcontent = window[CONTENT_KEY];
} else {
val = "/* Welcome to openrepl! */\n/* Editor underdevelopment! */";
initalcontent = "/* Welcome to openrepl! */\n/* Editor underdevelopment! */";
}

//get language from optionmenu
//get language from optionmenu
var optionlang = $('#optionMenu > select option:selected').data('editor');
if (optionlang==null) {
optionlang="c_cpp"
}
if (optionlang==null) {
optionlang="c_cpp"
}
// Here's where we set the initial content of the editor
editorValues.child(editorId).set({
lang: {
data: optionlang,
silent: true // trigger event only when told
},
queue: {},
content: val
content: initalcontent
});
}

Expand All @@ -1324,15 +1323,25 @@ $(function() {
// ...then set the value
// -1 will move the cursor at the begining of the editor, preventing
// selecting all the code in the editor (which is happening by default)
editor.setValue(val, -1);
editor.setValue(initalcontent, -1);

// ...then set applyingDeltas to false
applyingDeltas = false;

// And finally, focus the editor!
editor.focus();
$("#select-lang").trigger('change');
}
}; // end of editor init App

// Take the editor value on start and set it in the editor
currentEditorValue.child("content").once("value", function (contentRef) {
// Get the current content
var val = contentRef.val();
initializeEditorApp(val);
});
// initalize the editor App after 10s delay if above fails to
setTimeout(initializeEditorApp, 10000);
});


Expand Down

0 comments on commit 7fad387

Please sign in to comment.