context menu on link to open in new tab

This commit is contained in:
zadam 2019-05-07 22:33:53 +02:00
parent bfc61f8b36
commit 8eaf44735a
3 changed files with 43 additions and 19 deletions

View File

@ -2,7 +2,6 @@ import cloningService from '../services/cloning.js';
import linkService from '../services/link.js'; import linkService from '../services/link.js';
import noteDetailService from '../services/note_detail.js'; import noteDetailService from '../services/note_detail.js';
import treeUtils from '../services/tree_utils.js'; import treeUtils from '../services/tree_utils.js';
import noteDetailText from "../services/note_detail_text.js";
import noteAutocompleteService from "../services/note_autocomplete.js"; import noteAutocompleteService from "../services/note_autocomplete.js";
const $dialog = $("#add-link-dialog"); const $dialog = $("#add-link-dialog");
@ -96,7 +95,7 @@ $form.submit(() => {
const linkHref = '#' + notePath; const linkHref = '#' + notePath;
if (hasSelection()) { if (hasSelection()) {
const editor = noteDetailText.getEditor(); const editor = noteDetailService.getActiveComponent().getEditor();
editor.execute('link', linkHref); editor.execute('link', linkHref);
} }
@ -128,7 +127,7 @@ $form.submit(() => {
// returns true if user selected some text, false if there's no selection // returns true if user selected some text, false if there's no selection
function hasSelection() { function hasSelection() {
const model = noteDetailText.getEditor().model; const model = noteDetailService.getActiveComponent().getEditor().model;
const selection = model.document.selection; const selection = model.document.selection;
return !selection.isCollapsed; return !selection.isCollapsed;
@ -147,7 +146,7 @@ $linkTypes.change(linkTypeChanged);
// return back focus to note text detail after quitting add link // return back focus to note text detail after quitting add link
// the problem is that cursor position is reset // the problem is that cursor position is reset
$dialog.on("hidden.bs.modal", () => noteDetailText.focus()); $dialog.on("hidden.bs.modal", () => noteDetailService.getActiveComponent().focus());
export default { export default {
showDialog, showDialog,

View File

@ -1,6 +1,7 @@
import treeService from './tree.js'; import treeService from './tree.js';
import noteDetailText from './note_detail_text.js';
import treeUtils from './tree_utils.js'; import treeUtils from './tree_utils.js';
import contextMenuService from "./context_menu.js";
import noteDetailService from "./note_detail.js";
function getNotePathFromUrl(url) { function getNotePathFromUrl(url) {
const notePathMatch = /#(root[A-Za-z0-9/]*)$/.exec(url); const notePathMatch = /#(root[A-Za-z0-9/]*)$/.exec(url);
@ -13,16 +14,6 @@ function getNotePathFromUrl(url) {
} }
} }
function getNotePathFromLabel(label) {
const notePathMatch = / \((root[A-Za-z0-9/]*)\)/.exec(label);
if (notePathMatch !== null) {
return notePathMatch[1];
}
return null;
}
async function createNoteLink(notePath, noteTitle = null) { async function createNoteLink(notePath, noteTitle = null) {
if (!noteTitle) { if (!noteTitle) {
const noteId = treeUtils.getNoteIdFromNotePath(notePath); const noteId = treeUtils.getNoteIdFromNotePath(notePath);
@ -71,7 +62,7 @@ function goToLink(e) {
} }
function addLinkToEditor(linkTitle, linkHref) { function addLinkToEditor(linkTitle, linkHref) {
const editor = noteDetailText.getEditor(); const editor = noteDetailService.getActiveComponent().getEditor();
editor.model.change( writer => { editor.model.change( writer => {
const insertPosition = editor.model.document.selection.getFirstPosition(); const insertPosition = editor.model.document.selection.getFirstPosition();
@ -80,7 +71,7 @@ function addLinkToEditor(linkTitle, linkHref) {
} }
function addTextToEditor(text) { function addTextToEditor(text) {
const editor = noteDetailText.getEditor(); const editor = noteDetailService.getActiveComponent().getEditor();
editor.model.change(writer => { editor.model.change(writer => {
const insertPosition = editor.model.document.selection.getFirstPosition(); const insertPosition = editor.model.document.selection.getFirstPosition();
@ -102,6 +93,35 @@ function init() {
}; };
} }
function noteContextMenu(e) {
const $link = $(e.target);
const notePath = getNotePathFromLink($link);
if (!notePath) {
return;
}
e.preventDefault();
contextMenuService.initContextMenu(e, {
getContextMenuItems: () => {
return [
{title: "Open note in new tab", cmd: "openNoteInNewTab", uiIcon: "empty"}
];
},
selectContextMenuItem: (e, cmd) => {
if (cmd === 'openNoteInNewTab') {
noteDetailService.loadNoteDetail(notePath.split("/").pop(), true);
}
}
});
}
$(document).on('contextmenu', '.note-detail-text a', noteContextMenu);
$(document).on('contextmenu', "a[data-action='note']", noteContextMenu);
$(document).on('contextmenu', ".note-detail-render a", noteContextMenu);
// when click on link popup, in case of internal link, just go the the referenced note instead of default behavior // when click on link popup, in case of internal link, just go the the referenced note instead of default behavior
// of opening the link in new window/tab // of opening the link in new window/tab
$(document).on('click', "a[data-action='note']", goToLink); $(document).on('click', "a[data-action='note']", goToLink);
@ -124,7 +144,6 @@ $(document).on('click', 'span.ck-button__label', e => {
}); });
export default { export default {
getNotePathFromLabel,
getNotePathFromUrl, getNotePathFromUrl,
createNoteLink, createNoteLink,
addLinkToEditor, addLinkToEditor,

View File

@ -84,6 +84,10 @@ async function saveNotesIfChanged() {
/** @type {NoteContext[]} */ /** @type {NoteContext[]} */
let noteContexts = []; let noteContexts = [];
function getActiveComponent() {
return getActiveContext().getComponent();
}
/** @returns {NoteContext} */ /** @returns {NoteContext} */
function getActiveContext() { function getActiveContext() {
for (const ctx of noteContexts) { for (const ctx of noteContexts) {
@ -320,6 +324,7 @@ export default {
openInTab, openInTab,
switchToNote, switchToNote,
loadNote, loadNote,
loadNoteDetail,
getActiveNote, getActiveNote,
getActiveNoteContent, getActiveNoteContent,
getActiveNoteType, getActiveNoteType,
@ -329,5 +334,6 @@ export default {
saveNotesIfChanged, saveNotesIfChanged,
onNoteChange, onNoteChange,
addDetailLoadedListener, addDetailLoadedListener,
getActiveContext getActiveContext,
getActiveComponent
}; };