2018-03-25 11:09:17 -04:00
|
|
|
import server from './server.js';
|
2019-08-26 20:21:43 +02:00
|
|
|
import ws from "./ws.js";
|
2018-03-25 23:25:17 -04:00
|
|
|
import treeCache from "./tree_cache.js";
|
|
|
|
|
import NoteFull from "../entities/note_full.js";
|
2020-01-12 11:15:23 +01:00
|
|
|
import appContext from "./app_context.js";
|
2019-05-05 10:59:34 +02:00
|
|
|
|
2019-05-19 09:13:13 +02:00
|
|
|
function getActiveEditor() {
|
2020-01-12 12:37:44 +01:00
|
|
|
const activeTabContext = appContext.getActiveTabContext();
|
2019-05-19 09:13:13 +02:00
|
|
|
|
|
|
|
|
if (activeTabContext && activeTabContext.note && activeTabContext.note.type === 'text') {
|
|
|
|
|
return activeTabContext.getComponent().getEditor();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 22:28:58 +02:00
|
|
|
async function loadNote(noteId) {
|
|
|
|
|
const row = await server.get('notes/' + noteId);
|
|
|
|
|
|
2019-10-26 09:51:08 +02:00
|
|
|
const noteShort = await treeCache.getNote(noteId);
|
|
|
|
|
|
|
|
|
|
return new NoteFull(treeCache, row, noteShort);
|
2018-08-29 22:28:58 +02:00
|
|
|
}
|
|
|
|
|
|
2018-09-07 10:50:05 +02:00
|
|
|
function focusOnTitle() {
|
2020-01-24 17:54:47 +01:00
|
|
|
appContext.trigger('focusOnTitle');
|
2018-08-29 22:28:58 +02:00
|
|
|
}
|
|
|
|
|
|
2019-01-10 22:46:08 +01:00
|
|
|
function focusAndSelectTitle() {
|
2020-01-24 17:54:47 +01:00
|
|
|
appContext.trigger('focusAndSelectTitle');
|
2019-01-01 19:32:34 +01:00
|
|
|
}
|
|
|
|
|
|
2019-08-26 20:21:43 +02:00
|
|
|
ws.subscribeToOutsideSyncMessages(syncData => {
|
2019-06-01 12:14:09 +02:00
|
|
|
const noteIdsToRefresh = new Set();
|
|
|
|
|
|
|
|
|
|
syncData
|
|
|
|
|
.filter(sync => sync.entityName === 'notes')
|
|
|
|
|
.forEach(sync => noteIdsToRefresh.add(sync.entityId));
|
|
|
|
|
|
2019-08-06 22:39:27 +02:00
|
|
|
// we need to reload because of promoted attributes
|
2019-06-01 12:14:09 +02:00
|
|
|
syncData
|
|
|
|
|
.filter(sync => sync.entityName === 'attributes')
|
|
|
|
|
.forEach(sync => noteIdsToRefresh.add(sync.noteId));
|
|
|
|
|
|
2020-01-19 21:24:14 +01:00
|
|
|
// FIXME
|
2019-06-01 12:14:09 +02:00
|
|
|
for (const noteId of noteIdsToRefresh) {
|
2020-01-12 12:30:30 +01:00
|
|
|
appContext.refreshTabs(null, noteId);
|
2018-08-29 22:28:58 +02:00
|
|
|
}
|
2018-08-06 14:43:42 +02:00
|
|
|
});
|
|
|
|
|
|
2019-06-12 19:59:52 +02:00
|
|
|
function noteChanged() {
|
2020-01-12 12:37:44 +01:00
|
|
|
const activeTabContext = appContext.getActiveTabContext();
|
2019-06-12 19:59:52 +02:00
|
|
|
|
|
|
|
|
if (activeTabContext) {
|
|
|
|
|
activeTabContext.noteChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-26 22:29:14 -04:00
|
|
|
// this makes sure that when user e.g. reloads the page or navigates away from the page, the note's content is saved
|
|
|
|
|
// this sends the request asynchronously and doesn't wait for result
|
2020-01-19 21:40:23 +01:00
|
|
|
// FIXME
|
2020-01-19 22:05:45 +01:00
|
|
|
$(window).on('beforeunload', () => {
|
|
|
|
|
//saveNotesIfChanged();
|
|
|
|
|
});
|
2018-03-26 22:29:14 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
export default {
|
|
|
|
|
loadNote,
|
2018-09-07 10:50:05 +02:00
|
|
|
focusOnTitle,
|
2019-01-10 22:46:08 +01:00
|
|
|
focusAndSelectTitle,
|
2019-05-19 09:13:13 +02:00
|
|
|
getActiveEditor,
|
2020-01-24 17:54:47 +01:00
|
|
|
noteChanged
|
2018-03-25 11:09:17 -04:00
|
|
|
};
|