mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 21:11:30 +08:00 
			
		
		
		
	fix "Render note" and "Execute script" buttons + refactoring around data-trigger-command handling
This commit is contained in:
		
							parent
							
								
									6dfe335707
								
							
						
					
					
						commit
						edb9bc9229
					
				| @ -45,10 +45,12 @@ class AppContext extends Component { | |||||||
| 
 | 
 | ||||||
|         $("body").append($renderedWidget); |         $("body").append($renderedWidget); | ||||||
| 
 | 
 | ||||||
|         $renderedWidget.on('click', "[data-trigger-command]", e => { |         $renderedWidget.on('click', "[data-trigger-command]", function() { | ||||||
|             const commandName = $(e.target).attr('data-trigger-command'); |             const commandName = $(this).attr('data-trigger-command'); | ||||||
|  |             const $component = $(this).closest(".component"); | ||||||
|  |             const component = $component.prop("component"); | ||||||
| 
 | 
 | ||||||
|             this.triggerCommand(commandName); |             component.triggerCommand(commandName); | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|         this.tabManager = new TabManager(); |         this.tabManager = new TabManager(); | ||||||
| @ -92,6 +94,8 @@ class AppContext extends Component { | |||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |         // this might hint at error but sometimes this is used by components which are at different places
 | ||||||
|  |         // in the component tree to communicate with each other
 | ||||||
|         console.debug(`Unhandled command ${name}, converting to event.`); |         console.debug(`Unhandled command ${name}, converting to event.`); | ||||||
| 
 | 
 | ||||||
|         return this.triggerEvent(name, data); |         return this.triggerEvent(name, data); | ||||||
|  | |||||||
| @ -7,6 +7,7 @@ import Component from "../widgets/component.js"; | |||||||
| import toastService from "./toast.js"; | import toastService from "./toast.js"; | ||||||
| import noteCreateService from "./note_create.js"; | import noteCreateService from "./note_create.js"; | ||||||
| import ws from "./ws.js"; | import ws from "./ws.js"; | ||||||
|  | import bundleService from "./bundle.js"; | ||||||
| 
 | 
 | ||||||
| export default class Entrypoints extends Component { | export default class Entrypoints extends Component { | ||||||
|     constructor() { |     constructor() { | ||||||
| @ -199,4 +200,23 @@ export default class Entrypoints extends Component { | |||||||
|     async openNewWindowCommand() { |     async openNewWindowCommand() { | ||||||
|         this.openInWindowCommand({notePath: ''}); |         this.openInWindowCommand({notePath: ''}); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     async runActiveNoteCommand() { | ||||||
|  |         const note = appContext.tabManager.getActiveTabNote(); | ||||||
|  | 
 | ||||||
|  |         // ctrl+enter is also used elsewhere so make sure we're running only when appropriate
 | ||||||
|  |         if (!note || note.type !== 'code') { | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         if (note.mime.endsWith("env=frontend")) { | ||||||
|  |             await bundleService.getAndExecuteBundle(note.noteId); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         if (note.mime.endsWith("env=backend")) { | ||||||
|  |             await server.post('script/run/' + note.noteId); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         toastService.showMessage("Note executed"); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -93,8 +93,8 @@ function updateDisplayedShortcuts($container) { | |||||||
| 		} | 		} | ||||||
| 	}); | 	}); | ||||||
| 
 | 
 | ||||||
| 	$container.find('button[data-command],a.icon-action[data-command],.kb-in-title').each(async (i, el) => { | 	$container.find('[data-trigger-command]').each(async (i, el) => { | ||||||
| 		const actionName = $(el).attr('data-command'); | 		const actionName = $(el).attr('data-trigger-command'); | ||||||
| 		const action = await getAction(actionName, true); | 		const action = await getAction(actionName, true); | ||||||
| 
 | 
 | ||||||
| 		if (action) { | 		if (action) { | ||||||
|  | |||||||
| @ -18,17 +18,14 @@ const WIDGET_TPL = ` | |||||||
| 
 | 
 | ||||||
|     <a data-trigger-command="collapseTree" |     <a data-trigger-command="collapseTree" | ||||||
|        title="Collapse note tree"  |        title="Collapse note tree"  | ||||||
|        data-command="collapseTree"  |  | ||||||
|        class="icon-action bx bx-layer-minus"></a> |        class="icon-action bx bx-layer-minus"></a> | ||||||
| 
 | 
 | ||||||
|     <a data-trigger-command="scrollToActiveNote" |     <a data-trigger-command="scrollToActiveNote" | ||||||
|        title="Scroll to active note"   |        title="Scroll to active note"   | ||||||
|        data-command="scrollToActiveNote"  |  | ||||||
|        class="icon-action bx bx-crosshair"></a> |        class="icon-action bx bx-crosshair"></a> | ||||||
| 
 | 
 | ||||||
|     <a data-trigger-command="searchNotes" |     <a data-trigger-command="searchNotes" | ||||||
|        title="Search in notes" |        title="Search in notes" | ||||||
|        data-command="searchNotes" |  | ||||||
|        class="icon-action bx bx-search"></a> |        class="icon-action bx bx-search"></a> | ||||||
| </div> | </div> | ||||||
| `;
 | `;
 | ||||||
|  | |||||||
| @ -3,10 +3,12 @@ import TabAwareWidget from "./tab_aware_widget.js"; | |||||||
| const TPL = ` | const TPL = ` | ||||||
| <div style="display: inline-flex;"> | <div style="display: inline-flex;"> | ||||||
|     <button class="btn btn-sm icon-button bx bx-play-circle render-button" |     <button class="btn btn-sm icon-button bx bx-play-circle render-button" | ||||||
|  |             data-trigger-command="renderActiveNote" | ||||||
|             title="Render"></button> |             title="Render"></button> | ||||||
|      |      | ||||||
|     <button class="btn btn-sm icon-button bx bx-play-circle execute-script-button" |     <button class="btn btn-sm icon-button bx bx-play-circle execute-script-button" | ||||||
|             title="Execute (Ctrl+Enter)"></button> |             data-trigger-command="runActiveNote" | ||||||
|  |             title="Execute"></button> | ||||||
| </div>`; | </div>`; | ||||||
| 
 | 
 | ||||||
| export default class RunScriptButtonsWidget extends TabAwareWidget { | export default class RunScriptButtonsWidget extends TabAwareWidget { | ||||||
| @ -21,6 +23,6 @@ export default class RunScriptButtonsWidget extends TabAwareWidget { | |||||||
| 
 | 
 | ||||||
|     refreshWithNote(note) { |     refreshWithNote(note) { | ||||||
|         this.$renderButton.toggle(note.type === 'render'); |         this.$renderButton.toggle(note.type === 'render'); | ||||||
|         this.$executeScriptButton.toggle(note.mime.startsWith('application/javascript')); |         this.$executeScriptButton.toggle(note.type === 'code' && note.mime.startsWith('application/javascript')); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -29,11 +29,11 @@ const TAB_TPL = ` | |||||||
|   <div class="note-tab-wrapper"> |   <div class="note-tab-wrapper"> | ||||||
|     <div class="note-tab-title"></div> |     <div class="note-tab-title"></div> | ||||||
|     <div class="note-tab-drag-handle"></div> |     <div class="note-tab-drag-handle"></div> | ||||||
|     <div class="note-tab-close kb-in-title" title="Close tab" data-command="closeActiveTab"><span>×</span></div> |     <div class="note-tab-close" title="Close tab" data-trigger-command="closeActiveTab"><span>×</span></div> | ||||||
|   </div> |   </div> | ||||||
| </div>`; | </div>`; | ||||||
| 
 | 
 | ||||||
| const NEW_TAB_BUTTON_TPL = `<div class="note-new-tab kb-in-title" data-command="openNewTab" title="Add new tab">+</div>`; | const NEW_TAB_BUTTON_TPL = `<div class="note-new-tab" data-trigger-command="openNewTab" title="Add new tab">+</div>`; | ||||||
| const FILLER_TPL = `<div class="tab-row-filler">
 | const FILLER_TPL = `<div class="tab-row-filler">
 | ||||||
|     <div class="tab-row-border"></div> |     <div class="tab-row-border"></div> | ||||||
| </div>`; | </div>`; | ||||||
| @ -394,10 +394,13 @@ export default class TabRowWidget extends BasicWidget { | |||||||
|         this.setupDraggabilly(); |         this.setupDraggabilly(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     setTabCloseEvent($tab) { |     closeActiveTabCommand({$el}) { | ||||||
|         $tab.find('.note-tab-close') |         const tabId = $el.closest(".note-tab").attr('data-tab-id'); | ||||||
|             .on('click', _ => appContext.tabManager.removeTab($tab.attr('data-tab-id'))); |  | ||||||
| 
 | 
 | ||||||
|  |         appContext.tabManager.removeTab(tabId); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     setTabCloseEvent($tab) { | ||||||
|         $tab.on('mousedown', e => { |         $tab.on('mousedown', e => { | ||||||
|             if (e.which === 2) { |             if (e.which === 2) { | ||||||
|                 appContext.tabManager.removeTab($tab.attr('data-tab-id')); |                 appContext.tabManager.removeTab($tab.attr('data-tab-id')); | ||||||
| @ -558,8 +561,6 @@ export default class TabRowWidget extends BasicWidget { | |||||||
|         this.$newTab = $(NEW_TAB_BUTTON_TPL); |         this.$newTab = $(NEW_TAB_BUTTON_TPL); | ||||||
| 
 | 
 | ||||||
|         this.$tabContainer.append(this.$newTab); |         this.$tabContainer.append(this.$newTab); | ||||||
| 
 |  | ||||||
|         this.$newTab.on('click', _ => this.triggerCommand('openNewTab')); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     setupFiller() { |     setupFiller() { | ||||||
|  | |||||||
| @ -1,9 +1,6 @@ | |||||||
| import libraryLoader from "../../services/library_loader.js"; | import libraryLoader from "../../services/library_loader.js"; | ||||||
| import bundleService from "../../services/bundle.js"; |  | ||||||
| import toastService from "../../services/toast.js"; |  | ||||||
| import server from "../../services/server.js"; |  | ||||||
| import keyboardActionService from "../../services/keyboard_actions.js"; |  | ||||||
| import TypeWidget from "./type_widget.js"; | import TypeWidget from "./type_widget.js"; | ||||||
|  | import keyboardActionService from "../../services/keyboard_actions.js"; | ||||||
| 
 | 
 | ||||||
| const TPL = ` | const TPL = ` | ||||||
| <div class="note-detail-code note-detail-printable"> | <div class="note-detail-code note-detail-printable"> | ||||||
| @ -27,11 +24,8 @@ export default class EditableCodeTypeWidget extends TypeWidget { | |||||||
|     doRender() { |     doRender() { | ||||||
|         this.$widget = $(TPL); |         this.$widget = $(TPL); | ||||||
|         this.$editor = this.$widget.find('.note-detail-code-editor'); |         this.$editor = this.$widget.find('.note-detail-code-editor'); | ||||||
|         this.$executeScriptButton = this.$widget.find(".execute-script-button"); |  | ||||||
| 
 | 
 | ||||||
|         keyboardActionService.setElementActionHandler(this.$widget, 'runActiveNote', () => this.executeCurrentNote()); |         keyboardActionService.setupActionsForElement('code-detail', this.$widget, this); | ||||||
| 
 |  | ||||||
|         this.$executeScriptButton.on('click', () => this.executeCurrentNote()); |  | ||||||
| 
 | 
 | ||||||
|         this.initialized = this.initEditor(); |         this.initialized = this.initEditor(); | ||||||
| 
 | 
 | ||||||
| @ -106,26 +100,6 @@ export default class EditableCodeTypeWidget extends TypeWidget { | |||||||
|         this.codeEditor.focus(); |         this.codeEditor.focus(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     async executeCurrentNote() { |  | ||||||
|         // ctrl+enter is also used elsewhere so make sure we're running only when appropriate
 |  | ||||||
|         if (this.note.type !== 'code') { |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         // make sure note is saved so we load latest changes
 |  | ||||||
|         await this.spacedUpdate.updateNowIfNecessary(); |  | ||||||
| 
 |  | ||||||
|         if (this.note.mime.endsWith("env=frontend")) { |  | ||||||
|             await bundleService.getAndExecuteBundle(this.noteId); |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         if (this.note.mime.endsWith("env=backend")) { |  | ||||||
|             await server.post('script/run/' + this.noteId); |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         toastService.showMessage("Note executed"); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     cleanup() { |     cleanup() { | ||||||
|         if (this.codeEditor) { |         if (this.codeEditor) { | ||||||
|             this.spacedUpdate.allowUpdateWithoutChange(() => { |             this.spacedUpdate.allowUpdateWithoutChange(() => { | ||||||
|  | |||||||
| @ -25,9 +25,6 @@ export default class RenderTypeWidget extends TypeWidget { | |||||||
|         this.$widget = $(TPL); |         this.$widget = $(TPL); | ||||||
|         this.$noteDetailRenderHelp = this.$widget.find('.note-detail-render-help'); |         this.$noteDetailRenderHelp = this.$widget.find('.note-detail-render-help'); | ||||||
|         this.$noteDetailRenderContent = this.$widget.find('.note-detail-render-content'); |         this.$noteDetailRenderContent = this.$widget.find('.note-detail-render-content'); | ||||||
|         this.$renderButton = this.$widget.find('.render-button'); |  | ||||||
| 
 |  | ||||||
|         this.$renderButton.on('click', () => this.refresh()); |  | ||||||
| 
 | 
 | ||||||
|         return this.$widget; |         return this.$widget; | ||||||
|     } |     } | ||||||
| @ -46,4 +43,10 @@ export default class RenderTypeWidget extends TypeWidget { | |||||||
|     cleanup() { |     cleanup() { | ||||||
|         this.$noteDetailRenderContent.empty(); |         this.$noteDetailRenderContent.empty(); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     renderActiveNoteEvent() { | ||||||
|  |         if (this.tabContext.isActive()) { | ||||||
|  |             this.refresh(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 zadam
						zadam