Save cursor position

This commit is contained in:
Shadowfacts 2016-09-30 19:48:42 -04:00
parent b62ac18656
commit 4a0315d927
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 128 additions and 88 deletions

View File

@ -12,6 +12,7 @@
<script src="codemirror/codemirror.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/localforage/1.4.2/localforage.min.js"></script>
<script src="languages.js"></script>
<script src="main.js"></script>
</body>

47
main.js
View File

@ -5,11 +5,14 @@ var focused = false;
let invalids = [];
// fetch file and setup
let file = window.location.hash.substring(1);
let hash = window.location.hash.substring(1);
let hashBits = hash.split("/");
let repo = hashBits.slice(0, 3).join("/");
let filePath = hashBits.slice(3, hashBits.length).join("/");
$.get({
url: `https://raw.githubusercontent.com/${file}`,
url: `https://raw.githubusercontent.com/${repo}/${filePath}`,
success: (code) => {
let parts = file.split(".");
let parts = filePath.split(".");
let fileExtension = parts[parts.length - 1];
let lang = getLanguageByExtension(fileExtension);
$.get({
@ -40,7 +43,19 @@ function setup(data, mime) {
editor.setSize("100%", "100%");
incompleteMark = editor.doc.markText({ line: 0, ch: 0 }, getEndPos(), {
localforage.getItem(repo)
.then((val) => {
var startPos; // the initial position of the cursor
if (val && val[filePath] && val[filePath].pos) {
startPos = val[filePath].pos;
} else {
startPos = { line: 0, ch: 0 };
}
saveCursor();
editor.setCursor(startPos);
incompleteMark = editor.doc.markText(editor.getCursor(), getEndPos(), {
className: "incomplete"
});
@ -141,6 +156,11 @@ function setup(data, mime) {
}
}
});
})
.catch((e) => {
throw e;
});
}
function moveToEndOfPreviousLine() {
@ -202,6 +222,25 @@ function updateIncompleteMark() {
});
}
function saveCursor() {
localforage.getItem(repo)
.then((val) => {
if (!val) val = {};
val[filePath] = {
pos: editor.getCursor()
};
localforage.setItem(repo, val)
.catch((e) => {
throw e;
});
})
.catch((e) => {
throw e;
});
setTimeout(saveCursor, 15000);
}
String.prototype.hasOnlyWhiteSpaceBeforeIndex = function(index) {
return this.substring(index) == this.trim();
};