2018-03-26 01:41:29 +08:00
|
|
|
import treeService from './tree.js';
|
2019-05-09 01:55:24 +08:00
|
|
|
import TabContext from './note_context.js';
|
2018-03-25 23:09:17 +08:00
|
|
|
import server from './server.js';
|
2018-03-26 09:16:57 +08:00
|
|
|
import messagingService from "./messaging.js";
|
2018-03-26 09:29:35 +08:00
|
|
|
import infoService from "./info.js";
|
2018-03-26 11:25:17 +08:00
|
|
|
import treeCache from "./tree_cache.js";
|
|
|
|
import NoteFull from "../entities/note_full.js";
|
2018-07-29 22:06:13 +08:00
|
|
|
import bundleService from "./bundle.js";
|
2018-12-24 17:10:36 +08:00
|
|
|
import utils from "./utils.js";
|
2019-02-27 04:37:15 +08:00
|
|
|
import importDialog from "../dialogs/import.js";
|
2019-05-08 03:34:01 +08:00
|
|
|
import contextMenuService from "./context_menu.js";
|
2019-05-09 02:14:41 +08:00
|
|
|
import treeUtils from "./tree_utils.js";
|
2018-03-25 23:09:17 +08:00
|
|
|
|
2019-05-05 16:59:34 +08:00
|
|
|
const chromeTabsEl = document.querySelector('.chrome-tabs');
|
|
|
|
const chromeTabs = new ChromeTabs();
|
|
|
|
chromeTabs.init(chromeTabsEl);
|
|
|
|
|
2019-05-09 01:55:24 +08:00
|
|
|
const $tabContentsContainer = $("#note-tab-container");
|
2019-05-04 03:50:14 +08:00
|
|
|
const $savedIndicator = $(".saved-indicator");
|
2018-03-25 23:09:17 +08:00
|
|
|
|
2019-01-02 02:32:34 +08:00
|
|
|
let detailLoadedListeners = [];
|
|
|
|
|
2019-05-06 02:45:07 +08:00
|
|
|
/** @return {NoteFull} */
|
2019-03-15 03:21:27 +08:00
|
|
|
function getActiveNote() {
|
2019-05-02 04:19:29 +08:00
|
|
|
const activeContext = getActiveContext();
|
|
|
|
return activeContext ? activeContext.note : null;
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
|
|
|
|
2019-03-15 03:21:27 +08:00
|
|
|
function getActiveNoteId() {
|
2019-05-02 04:19:29 +08:00
|
|
|
const activeNote = getActiveNote();
|
|
|
|
|
2019-03-15 03:21:27 +08:00
|
|
|
return activeNote ? activeNote.noteId : null;
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
|
|
|
|
2019-03-15 03:21:27 +08:00
|
|
|
function getActiveNoteType() {
|
|
|
|
const activeNote = getActiveNote();
|
2018-03-27 12:22:02 +08:00
|
|
|
|
2019-03-15 03:21:27 +08:00
|
|
|
return activeNote ? activeNote.type : null;
|
2018-03-27 12:22:02 +08:00
|
|
|
}
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function reload() {
|
|
|
|
// no saving here
|
2017-11-05 05:54:27 +08:00
|
|
|
|
2019-03-15 03:21:27 +08:00
|
|
|
await loadNoteDetail(getActiveNoteId());
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-05 05:54:27 +08:00
|
|
|
|
2019-05-06 02:45:07 +08:00
|
|
|
async function reloadAllTabs() {
|
2019-05-09 01:55:24 +08:00
|
|
|
for (const tabContext of tabContexts) {
|
|
|
|
const note = await loadNote(tabContext.note.noteId);
|
2019-05-06 02:45:07 +08:00
|
|
|
|
2019-05-09 02:14:41 +08:00
|
|
|
await loadNoteDetailToContext(tabContext, note, tabContext.notePath);
|
2019-05-08 03:04:07 +08:00
|
|
|
|
2019-05-06 02:45:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 04:24:43 +08:00
|
|
|
async function openInTab(noteId) {
|
|
|
|
await loadNoteDetail(noteId, true);
|
|
|
|
}
|
2019-05-02 05:06:18 +08:00
|
|
|
|
2019-05-09 02:14:41 +08:00
|
|
|
async function switchToNote(notePath) {
|
|
|
|
await saveNotesIfChanged();
|
2017-11-05 05:54:27 +08:00
|
|
|
|
2019-05-09 02:14:41 +08:00
|
|
|
await loadNoteDetail(notePath);
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-05 05:54:27 +08:00
|
|
|
|
2019-03-15 03:21:27 +08:00
|
|
|
function getActiveNoteContent() {
|
2019-05-02 04:19:29 +08:00
|
|
|
return getActiveContext().getComponent().getContent();
|
2018-09-03 22:05:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function onNoteChange(func) {
|
2019-05-02 04:19:29 +08:00
|
|
|
return getActiveContext().getComponent().onNoteChange(func);
|
2018-09-03 22:05:28 +08:00
|
|
|
}
|
|
|
|
|
2019-05-02 04:19:29 +08:00
|
|
|
async function saveNotesIfChanged() {
|
2019-05-09 01:55:24 +08:00
|
|
|
for (const ctx of tabContexts) {
|
2019-05-02 04:19:29 +08:00
|
|
|
await ctx.saveNoteIfChanged();
|
2018-12-28 03:22:33 +08:00
|
|
|
}
|
|
|
|
|
2019-05-02 04:19:29 +08:00
|
|
|
// make sure indicator is visible in a case there was some race condition.
|
|
|
|
$savedIndicator.fadeIn();
|
|
|
|
}
|
2017-11-05 05:54:27 +08:00
|
|
|
|
2019-05-09 01:55:24 +08:00
|
|
|
/** @type {TabContext[]} */
|
|
|
|
let tabContexts = [];
|
2018-04-08 20:21:49 +08:00
|
|
|
|
2019-05-08 04:33:53 +08:00
|
|
|
function getActiveComponent() {
|
|
|
|
return getActiveContext().getComponent();
|
|
|
|
}
|
|
|
|
|
2019-05-09 01:55:24 +08:00
|
|
|
/** @returns {TabContext} */
|
2019-05-02 04:19:29 +08:00
|
|
|
function getActiveContext() {
|
2019-05-09 01:55:24 +08:00
|
|
|
for (const ctx of tabContexts) {
|
|
|
|
if (ctx.$tabContent.is(":visible")) {
|
2019-05-03 04:24:43 +08:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-05 05:54:27 +08:00
|
|
|
|
2019-05-05 16:59:34 +08:00
|
|
|
function showTab(tabId) {
|
|
|
|
tabId = parseInt(tabId);
|
|
|
|
|
2019-05-09 01:55:24 +08:00
|
|
|
for (const ctx of tabContexts) {
|
|
|
|
ctx.$tabContent.toggle(ctx.tabId === tabId);
|
2018-03-27 12:22:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-06 02:45:07 +08:00
|
|
|
/**
|
2019-05-09 01:55:24 +08:00
|
|
|
* @param {TabContext} ctx
|
2019-05-06 02:45:07 +08:00
|
|
|
* @param {NoteFull} note
|
|
|
|
*/
|
2019-05-09 02:14:41 +08:00
|
|
|
async function loadNoteDetailToContext(ctx, note, notePath) {
|
|
|
|
ctx.setNote(note, notePath);
|
2018-11-01 02:08:31 +08:00
|
|
|
|
2018-12-24 17:10:36 +08:00
|
|
|
if (utils.isDesktop()) {
|
2019-03-21 05:28:54 +08:00
|
|
|
// needs to happen after loading the note itself because it references active noteId
|
2019-05-06 00:24:59 +08:00
|
|
|
ctx.attributes.refreshAttributes();
|
2019-05-06 02:45:07 +08:00
|
|
|
} else {
|
2018-12-25 06:08:43 +08:00
|
|
|
// mobile usually doesn't need attributes so we just invalidate
|
2019-05-06 00:24:59 +08:00
|
|
|
ctx.attributes.invalidateAttributes();
|
2018-12-25 06:08:43 +08:00
|
|
|
}
|
2018-02-13 12:53:00 +08:00
|
|
|
|
2019-05-04 20:34:03 +08:00
|
|
|
ctx.noteChangeDisabled = true;
|
2017-11-05 05:54:27 +08:00
|
|
|
|
2018-03-27 12:22:02 +08:00
|
|
|
try {
|
2019-05-02 04:19:29 +08:00
|
|
|
ctx.$noteTitle.val(ctx.note.title);
|
2017-12-25 22:46:11 +08:00
|
|
|
|
2018-12-24 17:10:36 +08:00
|
|
|
if (utils.isDesktop()) {
|
2019-05-05 04:44:25 +08:00
|
|
|
ctx.noteType.type(ctx.note.type);
|
|
|
|
ctx.noteType.mime(ctx.note.mime);
|
2018-12-24 17:10:36 +08:00
|
|
|
}
|
2017-11-05 05:54:27 +08:00
|
|
|
|
2019-05-02 04:19:29 +08:00
|
|
|
for (const componentType in ctx.components) {
|
|
|
|
if (componentType !== ctx.note.type) {
|
|
|
|
ctx.components[componentType].cleanup();
|
2018-10-31 19:29:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 04:19:29 +08:00
|
|
|
ctx.$noteDetailComponents.hide();
|
2017-11-15 11:50:56 +08:00
|
|
|
|
2019-05-02 04:19:29 +08:00
|
|
|
ctx.$noteTitle.removeAttr("readonly"); // this can be set by protected session service
|
2018-12-28 03:22:33 +08:00
|
|
|
|
2019-05-06 01:48:30 +08:00
|
|
|
await ctx.getComponent().show(ctx);
|
2019-05-06 02:45:07 +08:00
|
|
|
} finally {
|
2019-05-04 20:34:03 +08:00
|
|
|
ctx.noteChangeDisabled = false;
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2018-01-22 12:36:09 +08:00
|
|
|
|
2019-05-06 02:45:07 +08:00
|
|
|
treeService.setBranchBackgroundBasedOnProtectedStatus(note.noteId);
|
2018-01-24 12:41:22 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
// after loading new note make sure editor is scrolled to the top
|
2019-05-06 01:48:30 +08:00
|
|
|
ctx.getComponent().scrollToTop();
|
2018-01-24 12:41:22 +08:00
|
|
|
|
2019-01-02 02:32:34 +08:00
|
|
|
fireDetailLoaded();
|
|
|
|
|
2019-05-02 04:19:29 +08:00
|
|
|
ctx.$scriptArea.empty();
|
2018-07-30 02:51:28 +08:00
|
|
|
|
2019-05-04 03:50:14 +08:00
|
|
|
await bundleService.executeRelationBundles(ctx.note, 'runOnNoteView');
|
2018-08-07 18:48:11 +08:00
|
|
|
|
2019-05-05 04:44:25 +08:00
|
|
|
if (utils.isDesktop()) {
|
|
|
|
await ctx.attributes.showAttributes();
|
|
|
|
|
|
|
|
await ctx.showChildrenOverview();
|
|
|
|
}
|
2018-04-09 10:38:52 +08:00
|
|
|
}
|
|
|
|
|
2019-05-09 02:14:41 +08:00
|
|
|
async function loadNoteDetail(notePath, newTab = false) {
|
|
|
|
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
|
2019-05-06 02:45:07 +08:00
|
|
|
const loadedNote = await loadNote(noteId);
|
|
|
|
let ctx;
|
|
|
|
|
2019-05-09 01:55:24 +08:00
|
|
|
if (tabContexts.length === 0 || newTab) {
|
2019-05-06 02:45:07 +08:00
|
|
|
// if it's a new tab explicitly by user then it's in background
|
2019-05-09 01:55:24 +08:00
|
|
|
ctx = new TabContext(chromeTabs, newTab);
|
|
|
|
tabContexts.push(ctx);
|
2019-05-06 02:45:07 +08:00
|
|
|
|
|
|
|
if (!newTab) {
|
|
|
|
showTab(ctx.tabId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ctx = getActiveContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
// we will try to render the new note only if it's still the active one in the tree
|
|
|
|
// this is useful when user quickly switches notes (by e.g. holding down arrow) so that we don't
|
|
|
|
// try to render all those loaded notes one after each other. This only guarantees that correct note
|
|
|
|
// will be displayed independent of timing
|
|
|
|
const currentTreeNode = treeService.getActiveNode();
|
|
|
|
if (!newTab && currentTreeNode && currentTreeNode.data.noteId !== loadedNote.noteId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-09 02:14:41 +08:00
|
|
|
await loadNoteDetailToContext(ctx, loadedNote, notePath);
|
2019-05-06 02:45:07 +08:00
|
|
|
}
|
|
|
|
|
2018-08-30 04:28:58 +08:00
|
|
|
async function loadNote(noteId) {
|
|
|
|
const row = await server.get('notes/' + noteId);
|
|
|
|
|
|
|
|
return new NoteFull(treeCache, row);
|
|
|
|
}
|
|
|
|
|
2018-09-07 16:50:05 +08:00
|
|
|
function focusOnTitle() {
|
2019-05-02 04:19:29 +08:00
|
|
|
getActiveContext().$noteTitle.focus();
|
2018-08-30 04:28:58 +08:00
|
|
|
}
|
|
|
|
|
2019-01-11 05:46:08 +08:00
|
|
|
function focusAndSelectTitle() {
|
2019-05-02 04:19:29 +08:00
|
|
|
getActiveContext().$noteTitle.focus().select();
|
2019-01-11 05:46:08 +08:00
|
|
|
}
|
|
|
|
|
2019-01-02 02:32:34 +08:00
|
|
|
/**
|
|
|
|
* Since detail loading may take some time and user might just browse through the notes using UP-DOWN keys,
|
|
|
|
* we intentionally decouple activation of the note in the tree and full load of the note so just avaiting on
|
|
|
|
* fancytree's activate() won't wait for the full load.
|
|
|
|
*
|
|
|
|
* This causes an issue where in some cases you want to do some action after detail is loaded. For this reason
|
|
|
|
* we provide the listeners here which will be triggered after the detail is loaded and if the loaded note
|
|
|
|
* is the one registered in the listener.
|
|
|
|
*/
|
|
|
|
function addDetailLoadedListener(noteId, callback) {
|
|
|
|
detailLoadedListeners.push({ noteId, callback });
|
|
|
|
}
|
|
|
|
|
|
|
|
function fireDetailLoaded() {
|
|
|
|
for (const {noteId, callback} of detailLoadedListeners) {
|
2019-05-02 04:19:29 +08:00
|
|
|
if (noteId === getActiveNoteId()) {
|
2019-01-02 02:32:34 +08:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// all the listeners are one time only
|
|
|
|
detailLoadedListeners = [];
|
|
|
|
}
|
|
|
|
|
2018-08-30 04:28:58 +08:00
|
|
|
messagingService.subscribeToSyncMessages(syncData => {
|
2019-03-15 03:21:27 +08:00
|
|
|
if (syncData.some(sync => sync.entityName === 'notes' && sync.entityId === getActiveNoteId())) {
|
2018-08-30 04:28:58 +08:00
|
|
|
infoService.showMessage('Reloading note because of background changes');
|
|
|
|
|
|
|
|
reload();
|
|
|
|
}
|
2018-08-06 20:43:42 +08:00
|
|
|
});
|
|
|
|
|
2019-05-09 01:55:24 +08:00
|
|
|
$tabContentsContainer.on("dragover", e => e.preventDefault());
|
2019-02-27 04:37:15 +08:00
|
|
|
|
2019-05-09 01:55:24 +08:00
|
|
|
$tabContentsContainer.on("dragleave", e => e.preventDefault());
|
2019-02-27 04:37:15 +08:00
|
|
|
|
2019-05-09 01:55:24 +08:00
|
|
|
$tabContentsContainer.on("drop", e => {
|
2019-03-15 03:21:27 +08:00
|
|
|
importDialog.uploadFiles(getActiveNoteId(), e.originalEvent.dataTransfer.files, {
|
2019-02-27 04:37:15 +08:00
|
|
|
safeImport: true,
|
|
|
|
shrinkImages: true,
|
|
|
|
textImportedAsText: true,
|
|
|
|
codeImportedAsCode: true,
|
|
|
|
explodeArchives: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-05 16:59:34 +08:00
|
|
|
chromeTabsEl.addEventListener('activeTabChange', ({ detail }) => {
|
|
|
|
const tabId = detail.tabEl.getAttribute('data-tab-id');
|
|
|
|
|
|
|
|
showTab(tabId);
|
|
|
|
|
|
|
|
console.log(`Activated tab ${tabId}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
chromeTabsEl.addEventListener('tabRemove', ({ detail }) => {
|
|
|
|
const tabId = parseInt(detail.tabEl.getAttribute('data-tab-id'));
|
|
|
|
|
2019-05-09 01:55:24 +08:00
|
|
|
tabContexts = tabContexts.filter(nc => nc.tabId !== tabId);
|
2019-05-05 16:59:34 +08:00
|
|
|
|
|
|
|
console.log(`Removed tab ${tabId}`);
|
|
|
|
});
|
|
|
|
|
2019-05-08 03:34:01 +08:00
|
|
|
$(chromeTabsEl).on('contextmenu', '.chrome-tab', e => {
|
|
|
|
const tab = $(e.target).closest(".chrome-tab");
|
|
|
|
|
|
|
|
contextMenuService.initContextMenu(e, {
|
|
|
|
getContextMenuItems: () => {
|
|
|
|
return [
|
|
|
|
{title: "Close all tabs except for this", cmd: "removeAllTabsExceptForThis", uiIcon: "empty"}
|
|
|
|
];
|
|
|
|
},
|
|
|
|
selectContextMenuItem: (e, cmd) => {
|
|
|
|
if (cmd === 'removeAllTabsExceptForThis') {
|
|
|
|
chromeTabs.removeAllTabsExceptForThis(tab[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-05 16:59:34 +08:00
|
|
|
if (utils.isElectron()) {
|
|
|
|
utils.bindShortcut('ctrl+w', () => {
|
2019-05-09 01:55:24 +08:00
|
|
|
if (tabContexts.length === 1) {
|
2019-05-05 16:59:34 +08:00
|
|
|
// at least one tab must be present
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
chromeTabs.removeTab(chromeTabs.activeTabEl);
|
|
|
|
});
|
2019-05-08 03:04:07 +08:00
|
|
|
|
|
|
|
utils.bindShortcut('ctrl+tab', () => {
|
|
|
|
const nextTab = chromeTabs.nextTabEl;
|
|
|
|
|
|
|
|
if (nextTab) {
|
|
|
|
chromeTabs.setCurrentTab(nextTab);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
utils.bindShortcut('ctrl+shift+tab', () => {
|
|
|
|
const prevTab = chromeTabs.previousTabEl;
|
|
|
|
|
|
|
|
if (prevTab) {
|
|
|
|
chromeTabs.setCurrentTab(prevTab);
|
|
|
|
}
|
|
|
|
});
|
2019-05-05 16:59:34 +08:00
|
|
|
}
|
2019-05-04 03:50:14 +08:00
|
|
|
|
2018-03-27 10:29:14 +08: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
|
2019-05-02 04:19:29 +08:00
|
|
|
$(window).on('beforeunload', () => { saveNotesIfChanged(); }); // don't convert to short form, handler doesn't like returned promise
|
2018-03-27 10:29:14 +08:00
|
|
|
|
2019-05-02 04:19:29 +08:00
|
|
|
setInterval(saveNotesIfChanged, 3000);
|
2018-03-25 23:09:17 +08:00
|
|
|
|
|
|
|
export default {
|
|
|
|
reload,
|
2019-05-06 02:45:07 +08:00
|
|
|
reloadAllTabs,
|
2019-05-03 04:24:43 +08:00
|
|
|
openInTab,
|
2018-03-25 23:09:17 +08:00
|
|
|
switchToNote,
|
|
|
|
loadNote,
|
2019-05-08 04:33:53 +08:00
|
|
|
loadNoteDetail,
|
2019-03-15 03:21:27 +08:00
|
|
|
getActiveNote,
|
|
|
|
getActiveNoteContent,
|
|
|
|
getActiveNoteType,
|
|
|
|
getActiveNoteId,
|
2018-09-07 16:50:05 +08:00
|
|
|
focusOnTitle,
|
2019-01-11 05:46:08 +08:00
|
|
|
focusAndSelectTitle,
|
2019-05-02 04:19:29 +08:00
|
|
|
saveNotesIfChanged,
|
2019-01-02 02:32:34 +08:00
|
|
|
onNoteChange,
|
2019-05-05 04:44:25 +08:00
|
|
|
addDetailLoadedListener,
|
2019-05-08 04:33:53 +08:00
|
|
|
getActiveContext,
|
|
|
|
getActiveComponent
|
2018-03-25 23:09:17 +08:00
|
|
|
};
|