refactorings for add link and include note

This commit is contained in:
zadam
2020-02-16 10:50:48 +01:00
parent 7e41a2750c
commit e06f3ef97e
11 changed files with 65 additions and 42 deletions

View File

@@ -36,36 +36,10 @@ window.glob.PROFILING_LOG = false;
window.glob.isDesktop = utils.isDesktop;
window.glob.isMobile = utils.isMobile;
// required for CKEditor image upload plugin
// FIXME
window.glob.getActiveNode = () => appContext.getMainNoteTree().getActiveNode();
window.glob.getComponentByEl = el => appContext.getComponentByEl(el);
window.glob.getHeaders = server.getHeaders;
window.glob.showAddLinkDialog = () => import('./dialogs/add_link.js').then(d => d.showDialog());
window.glob.showIncludeNoteDialog = cb => import('./dialogs/include_note.js').then(d => d.showDialog(cb));
window.glob.loadIncludedNote = async (noteId, el) => {
const note = await treeCache.getNote(noteId);
if (note) {
$(el).empty().append($("<h3>").append(await linkService.createNoteLink(note.noteId, {
showTooltip: false
})));
const {renderedContent} = await noteContentRenderer.getRenderedContent(note);
$(el).append(renderedContent);
}
};
// this is required by CKEditor when uploading images
window.glob.noteChanged = () => {
const activeTabContext = appContext.tabManager.getActiveTabContext();
if (activeTabContext) {
activeTabContext.noteChanged();
}
};
window.glob.refreshTree = treeService.reload;
// required for ESLint plugin
// required for ESLint plugin and CKEditor
window.glob.getActiveTabNote = () => appContext.tabManager.getActiveTabNote();
window.glob.requireLibrary = libraryLoader.requireLibrary;
window.glob.ESLINT = libraryLoader.ESLINT;

View File

@@ -5,10 +5,12 @@ import utils from "../services/utils.js";
const $dialog = $("#include-note-dialog");
const $form = $("#include-note-form");
const $autoComplete = $("#include-note-autocomplete");
let callback = null;
export async function showDialog(cb) {
callback = cb;
/** @var TextTypeWidget */
let textTypeWidget;
export async function showDialog(widget) {
textTypeWidget = widget;
$autoComplete.val('');
@@ -24,9 +26,9 @@ $form.on('submit', () => {
if (notePath) {
$dialog.modal('hide');
if (callback) {
callback(treeService.getNoteIdFromNotePath(notePath));
}
const includedNoteId = treeService.getNoteIdFromNotePath(notePath);
textTypeWidget.addIncludeNote(includedNoteId);
}
else {
console.error("No noteId to include.");

View File

@@ -16,6 +16,7 @@ class AppContext {
this.tabManager = new TabManager(this);
this.components = [];
this.executors = [];
this.idToComponent = {};
}
async start() {
@@ -59,6 +60,18 @@ class AppContext {
this.trigger('initialRenderComplete');
}
registerComponent(componentId, component) {
this.idToComponent[componentId] = component;
}
findComponentById(componentId) {
return this.idToComponent[componentId];
}
getComponentByEl(el) {
return $(el).closest(".component").prop('component');
}
async trigger(name, data) {
this.eventReceived(name, data);

View File

@@ -5,6 +5,9 @@ class BasicWidget extends Component {
render() {
const $widget = this.doRender();
$widget.addClass('component')
.prop('component', this);
keyboardActionsService.updateDisplayedShortcuts($widget);
this.toggle(this.isEnabled());

View File

@@ -5,6 +5,9 @@ import TypeWidget from "./type_widget.js";
import utils from "../../services/utils.js";
import appContext from "../../services/app_context.js";
import keyboardActionService from "../../services/keyboard_actions.js";
import treeCache from "../../services/tree_cache.js";
import linkService from "../../services/link.js";
import noteContentRenderer from "../../services/note_content_renderer.js";
const ENABLE_INSPECTOR = false;
@@ -254,4 +257,32 @@ export default class TextTypeWidget extends TypeWidget {
addLinkToTextCommand() {
import("../../dialogs/add_link.js").then(d => d.showDialog(this));
}
addIncludeNoteToTextCommand() {
import("../../dialogs/include_note.js").then(d => d.showDialog(this));
}
async loadIncludedNote(noteId, el) {
const note = await treeCache.getNote(noteId);
if (note) {
$(el).empty().append($("<h3>").append(await linkService.createNoteLink(note.noteId, {
showTooltip: false
})));
const {renderedContent} = await noteContentRenderer.getRenderedContent(note);
$(el).append(renderedContent);
}
}
addIncludeNote(noteId) {
this.textEditor.model.change( writer => {
// Insert <includeNote>*</includeNote> at the current selection position
// in a way that will result in creating a valid model structure
this.textEditor.model.insertContent(writer.createElement('includeNote', {
noteId: noteId
}));
} );
}
}