2021-02-17 01:24:20 +05:00
|
|
|
import StorageInterface from "../interfaces/storage";
|
|
|
|
|
import EventSource from "eventsource";
|
2021-04-06 11:55:23 +05:00
|
|
|
import Config from "../utils/config";
|
|
|
|
|
import http from "notes-core/utils/http";
|
2021-04-07 20:54:20 +05:00
|
|
|
import { EV, EVENTS } from "notes-core/common";
|
2021-02-17 01:24:20 +05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @type {import("notes-core/api").default}
|
|
|
|
|
*/
|
|
|
|
|
var db;
|
|
|
|
|
function initializeDatabase() {
|
2021-04-06 11:55:23 +05:00
|
|
|
return import("notes-core/api").then(async ({ default: Database }) => {
|
2021-02-17 01:24:20 +05:00
|
|
|
db = new Database(StorageInterface, EventSource);
|
2021-02-26 18:21:49 +05:00
|
|
|
// db.host({
|
|
|
|
|
// API_HOST: "https://api.notesnook.com",
|
|
|
|
|
// AUTH_HOST: "https://auth.streetwriters.co",
|
|
|
|
|
// SSE_HOST: "https://events.streetwriters.co",
|
|
|
|
|
// });
|
|
|
|
|
|
2021-02-20 18:27:15 +05:00
|
|
|
db.host({
|
2021-02-26 18:21:49 +05:00
|
|
|
API_HOST: "http://localhost:5264",
|
|
|
|
|
AUTH_HOST: "http://localhost:8264",
|
|
|
|
|
SSE_HOST: "http://localhost:7264",
|
2021-02-20 18:27:15 +05:00
|
|
|
});
|
|
|
|
|
|
2021-04-06 11:55:23 +05:00
|
|
|
await db.init();
|
|
|
|
|
|
|
|
|
|
if (!isAppHydrated()) {
|
2021-04-06 12:47:21 +05:00
|
|
|
try {
|
2021-04-07 20:54:20 +05:00
|
|
|
loadDefaultNotes(db);
|
2021-04-06 12:47:21 +05:00
|
|
|
} catch (e) {}
|
2021-04-06 11:55:23 +05:00
|
|
|
}
|
2021-02-17 01:24:20 +05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { db, initializeDatabase };
|
|
|
|
|
|
2021-04-06 11:55:23 +05:00
|
|
|
function isAppHydrated() {
|
|
|
|
|
return Config.get("hydrated", false);
|
|
|
|
|
}
|
2021-02-17 01:24:20 +05:00
|
|
|
|
2021-04-06 11:55:23 +05:00
|
|
|
function setAppHydrated() {
|
|
|
|
|
return Config.set("hydrated", true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function loadDefaultNotes(db) {
|
|
|
|
|
const notes = await http.get("/notes/index.json");
|
|
|
|
|
if (!notes) return;
|
|
|
|
|
let autoOpenId;
|
|
|
|
|
for (let note of notes) {
|
|
|
|
|
const content = await http.get(note.webContent);
|
|
|
|
|
let id = await db.notes.add({
|
|
|
|
|
title: note.title,
|
|
|
|
|
headline: note.headline,
|
|
|
|
|
localOnly: true,
|
|
|
|
|
content: { type: "tiny", data: content },
|
|
|
|
|
});
|
|
|
|
|
if (note.autoOpen) autoOpenId = id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (autoOpenId) quickNavigate(`/notes/${autoOpenId}/edit`);
|
2021-04-07 20:54:20 +05:00
|
|
|
setAppHydrated();
|
2021-04-17 11:03:38 +05:00
|
|
|
EV.publish(EVENTS.appRefreshRequested);
|
2021-04-06 11:55:23 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function quickNavigate(url) {
|
|
|
|
|
window.history.replaceState(null, null, `#${url}`);
|
|
|
|
|
dispatchEvent(new HashChangeEvent("hashchange"));
|
|
|
|
|
}
|