mirror of https://github.com/shadowfacts/type.git
Initial chunks work
This commit is contained in:
parent
9290f1579b
commit
219aec3cb2
76
js/main.js
76
js/main.js
|
@ -12,10 +12,17 @@ let filePath = hashBits.slice(3, hashBits.length).join("/");
|
||||||
$.get({
|
$.get({
|
||||||
url: `https://raw.githubusercontent.com/${repo}/${filePath}`,
|
url: `https://raw.githubusercontent.com/${repo}/${filePath}`,
|
||||||
success: (code) => {
|
success: (code) => {
|
||||||
let lang = getLanguageByExtension(getFileExtension());
|
getChunk(code)
|
||||||
$.getScript(`https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.19.0/mode/${lang.file}/${lang.file}.min.js`, () => {
|
.then((chunk) => {
|
||||||
setup(code, lang.mime);
|
let lang = getLanguageByExtension(getFileExtension());
|
||||||
});
|
console.log(`Detected language as ${lang.mime}`);
|
||||||
|
$.getScript(`https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.19.0/mode/${lang.file}/${lang.file}.min.js`, () => {
|
||||||
|
setup(chunk, lang.mime);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
throw e;
|
||||||
|
})
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -254,24 +261,64 @@ function getFileExtension() {
|
||||||
return parts[parts.length - 1];
|
return parts[parts.length - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getChunk(code) {
|
||||||
|
let lines = code.split("\n");
|
||||||
|
return localforage.getItem(repo)
|
||||||
|
.then((val) => {
|
||||||
|
if (val && val[filePath] && val[filePath].chunk) {
|
||||||
|
let chunk = val[filePath].chunk;
|
||||||
|
let totalChunks = Math.floor(lines.length / 50);
|
||||||
|
if (chunk == totalChunks - 1) {
|
||||||
|
return lines.slice(totalChunks - (lines.length % 50), lines.length);
|
||||||
|
} else {
|
||||||
|
return lines.slice(chunk * 50, (chunk + 1) * 50 + 1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!val) val = {};
|
||||||
|
if (!val[filePath]) val[filePath] = {};
|
||||||
|
val[filePath].chunk = 0;
|
||||||
|
localforage.setItem(repo, val)
|
||||||
|
.catch((e) => {
|
||||||
|
throw e;
|
||||||
|
});
|
||||||
|
return lines.slice(0, 51);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then((lines) => {
|
||||||
|
return lines.join("\n");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function load() {
|
function load() {
|
||||||
return localforage.getItem(repo)
|
return localforage.getItem(repo)
|
||||||
.then((val) => {
|
.then((val) => {
|
||||||
loadInvalids(val[filePath]);
|
if (val && val[filePath] && val[filePath].hasOwnProperty("chunk") && val[filePath].chunks) {
|
||||||
loadCursor(val[filePath]);
|
let chunk = val[filePath].chunks[val[filePath].chunk];
|
||||||
loadTheme(val[filePath]);
|
loadInvalids(chunk);
|
||||||
|
loadCursor(chunk);
|
||||||
|
loadTheme(val);
|
||||||
|
} else {
|
||||||
|
save();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function save() {
|
function save() {
|
||||||
let obj = {};
|
|
||||||
saveInvalids(obj);
|
|
||||||
saveCursor(obj);
|
|
||||||
saveTheme(obj);
|
|
||||||
localforage.getItem(repo)
|
localforage.getItem(repo)
|
||||||
.then((val) => {
|
.then((val) => {
|
||||||
if (!val) val = {};
|
if (!val) val = {};
|
||||||
val[filePath] = obj;
|
if (!val[filePath]) val[filePath] = {};
|
||||||
|
let file = val[filePath];
|
||||||
|
|
||||||
|
if (!file.chunk) file.chunk = 0;
|
||||||
|
if (!file.chunks) file.chunks = [];
|
||||||
|
if (!file.chunks[file.chunk]) file.chunks[file.chunk] = {};
|
||||||
|
|
||||||
|
let chunk = file.chunks[file.chunk];
|
||||||
|
saveInvalids(chunk);
|
||||||
|
saveCursor(chunk);
|
||||||
|
saveTheme(val);
|
||||||
|
|
||||||
localforage.setItem(repo, val)
|
localforage.setItem(repo, val)
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
throw e;
|
throw e;
|
||||||
|
@ -280,6 +327,7 @@ function save() {
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
throw e;
|
throw e;
|
||||||
});
|
});
|
||||||
|
setTimeout(save, 15000);
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadInvalids(obj) {
|
function loadInvalids(obj) {
|
||||||
|
@ -320,8 +368,8 @@ function saveTheme(obj) {
|
||||||
obj.theme = themeSelector.val();
|
obj.theme = themeSelector.val();
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadCursor(val) {
|
function loadCursor(obj) {
|
||||||
editor.setCursor(val && val.cursor ? val.cursor : { line: 0, ch: 0 });
|
editor.setCursor(obj && obj.cursor ? obj.cursor : { line: 0, ch: 0 });
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveCursor(obj) {
|
function saveCursor(obj) {
|
||||||
|
|
Loading…
Reference in New Issue