mirror of https://github.com/shadowfacts/type.git
Save invalid marks
This commit is contained in:
parent
4a0315d927
commit
eb60d66020
84
main.js
84
main.js
|
@ -43,18 +43,9 @@ function setup(data, mime) {
|
||||||
|
|
||||||
editor.setSize("100%", "100%");
|
editor.setSize("100%", "100%");
|
||||||
|
|
||||||
localforage.getItem(repo)
|
load()
|
||||||
.then((val) => {
|
.then(save)
|
||||||
var startPos; // the initial position of the cursor
|
.then(() => {
|
||||||
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(), {
|
incompleteMark = editor.doc.markText(editor.getCursor(), getEndPos(), {
|
||||||
className: "incomplete"
|
className: "incomplete"
|
||||||
});
|
});
|
||||||
|
@ -81,11 +72,7 @@ function setup(data, mime) {
|
||||||
let line = editor.doc.getLine(pos.line);
|
let line = editor.doc.getLine(pos.line);
|
||||||
let char = line.charCodeAt(pos.ch);
|
let char = line.charCodeAt(pos.ch);
|
||||||
if (event.charCode != char) {
|
if (event.charCode != char) {
|
||||||
let mark = editor.doc.markText(pos, {line: pos.line, ch: pos.ch + 1}, {
|
markInvalid(pos);
|
||||||
className: "invalid"
|
|
||||||
});
|
|
||||||
if (!invalids[pos.line]) invalids[pos.line] = [];
|
|
||||||
invalids[pos.line][pos.ch] = mark;
|
|
||||||
}
|
}
|
||||||
editor.setCursor({ line: pos.line, ch: pos.ch + 1 });
|
editor.setCursor({ line: pos.line, ch: pos.ch + 1 });
|
||||||
updateIncompleteMark();
|
updateIncompleteMark();
|
||||||
|
@ -222,14 +209,30 @@ function updateIncompleteMark() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveCursor() {
|
function markInvalid(pos) {
|
||||||
|
let mark = editor.doc.markText(pos, {line: pos.line, ch: pos.ch + 1}, {
|
||||||
|
className: "invalid"
|
||||||
|
});
|
||||||
|
if (!invalids[pos.line]) invalids[pos.line] = [];
|
||||||
|
invalids[pos.line][pos.ch] = mark;
|
||||||
|
}
|
||||||
|
|
||||||
|
function load() {
|
||||||
|
return localforage.getItem(repo)
|
||||||
|
.then((val) => {
|
||||||
|
loadInvalids(val[filePath]);
|
||||||
|
loadCursor(val[filePath]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function save() {
|
||||||
|
let obj = {};
|
||||||
|
saveInvalids(obj);
|
||||||
|
saveCursor(obj);
|
||||||
localforage.getItem(repo)
|
localforage.getItem(repo)
|
||||||
.then((val) => {
|
.then((val) => {
|
||||||
if (!val) val = {};
|
if (!val) val = {};
|
||||||
|
val[filePath] = obj;
|
||||||
val[filePath] = {
|
|
||||||
pos: editor.getCursor()
|
|
||||||
};
|
|
||||||
localforage.setItem(repo, val)
|
localforage.setItem(repo, val)
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
throw e;
|
throw e;
|
||||||
|
@ -238,7 +241,42 @@ function saveCursor() {
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
throw e;
|
throw e;
|
||||||
});
|
});
|
||||||
setTimeout(saveCursor, 15000);
|
}
|
||||||
|
|
||||||
|
function loadInvalids(val) {
|
||||||
|
let serialized = val.invalids;
|
||||||
|
if (serialized) {
|
||||||
|
editor.operation(() => { // buffer all DOM changes together b/c performance
|
||||||
|
serialized.forEach(markInvalid);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveInvalids(obj) {
|
||||||
|
let serialized = [];
|
||||||
|
for (var i = 0; i < invalids.length; i++) {
|
||||||
|
let inner = invalids[i];
|
||||||
|
if (!inner) continue;
|
||||||
|
|
||||||
|
for (var j = 0; j < inner.length; j++) {
|
||||||
|
let mark = inner[j];
|
||||||
|
if (!mark) continue;
|
||||||
|
|
||||||
|
let pos = mark.find();
|
||||||
|
if (pos) {
|
||||||
|
serialized.push(pos.from);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
obj.invalids = serialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadCursor(val) {
|
||||||
|
editor.setCursor(val && val.cursor ? val.cursor : { line: 0, ch: 0 });
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveCursor(obj) {
|
||||||
|
obj.cursor = editor.getCursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
String.prototype.hasOnlyWhiteSpaceBeforeIndex = function(index) {
|
String.prototype.hasOnlyWhiteSpaceBeforeIndex = function(index) {
|
||||||
|
|
Loading…
Reference in New Issue