import treeService from './tree.js'; import server from './server.js'; import utils from './utils.js'; import toastService from './toast.js'; import linkService from './link.js'; import treeCache from './tree_cache.js'; import noteTooltipService from './note_tooltip.js'; import protectedSessionService from './protected_session.js'; import dateNotesService from './date_notes.js'; import CollapsibleWidget from '../widgets/collapsible_widget.js'; import ws from "./ws.js"; import hoistedNoteService from "./hoisted_note.js"; import appContext from "./app_context.js"; import TabAwareWidget from "../widgets/tab_aware_widget.js"; import TabCachingWidget from "../widgets/tab_caching_widget.js"; import BasicWidget from "../widgets/basic_widget.js"; /** * This is the main frontend API interface for scripts. It's published in the local "api" object. * * @constructor * @hideconstructor */ function FrontendScriptApi(startNote, currentNote, originEntity = null, $container = null) { const $pluginButtons = $("#plugin-buttons"); /** @property {jQuery} container of all the rendered script content */ this.$container = $container; /** @property {object} note where script started executing */ this.startNote = startNote; /** @property {object} note where script is currently executing */ this.currentNote = currentNote; /** @property {object|null} entity whose event triggered this execution */ this.originEntity = originEntity; // to keep consistency with backend API this.dayjs = dayjs; /** @property {CollapsibleWidget} */ this.CollapsibleWidget = CollapsibleWidget; /** @property {TabAwareWidget} */ this.TabAwareWidget = TabAwareWidget; /** @property {TabCachingWidget} */ this.TabCachingWidget = TabCachingWidget; /** @property {BasicWidget} */ this.BasicWidget = BasicWidget; /** * Activates note in the tree and in the note detail. * * @method * @param {string} notePath (or noteId) * @returns {Promise} */ this.activateNote = async notePath => { await appContext.tabManager.getActiveTabContext().setNote(notePath); }; /** * Activates newly created note. Compared to this.activateNote() also makes sure that frontend has been fully synced. * * @param {string} notePath (or noteId) * @return {Promise} */ this.activateNewNote = async notePath => { await ws.waitForMaxKnownSyncId(); await appContext.tabManager.getActiveTabContext().setNote(notePath); appContext.triggerEvent('focusAndSelectTitle'); }; /** * @typedef {Object} ToolbarButtonOptions * @property {string} title * @property {string} [icon] - name of the boxicon to be used (e.g. "time" for "bx-time" icon) * @property {function} action - callback handling the click on the button * @property {string} [shortcut] - keyboard shortcut for the button, e.g. "alt+t" */ /** * Adds new button the the plugin area. * * @param {ToolbarButtonOptions} opts */ this.addButtonToToolbar = opts => { const buttonId = "toolbar-button-" + opts.title.replace(/[^a-zA-Z0-9]/g, "-"); const button = $('