| 
									
										
										
										
											2018-03-27 22:42:46 -04:00
										 |  |  | import libraryLoader from "./library_loader.js"; | 
					
						
							| 
									
										
										
										
											2018-03-27 00:22:02 -04:00
										 |  |  | import bundleService from "./bundle.js"; | 
					
						
							|  |  |  | import infoService from "./info.js"; | 
					
						
							|  |  |  | import server from "./server.js"; | 
					
						
							|  |  |  | import noteDetailService from "./note_detail.js"; | 
					
						
							| 
									
										
										
										
											2018-12-29 10:04:59 +01:00
										 |  |  | import utils from "./utils.js"; | 
					
						
							| 
									
										
										
										
											2018-03-27 00:22:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  | class NoteDetailCode { | 
					
						
							| 
									
										
										
										
											2018-03-27 00:22:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-05-08 19:55:24 +02:00
										 |  |  |      * @param {TabContext} ctx | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     constructor(ctx) { | 
					
						
							|  |  |  |         this.ctx = ctx; | 
					
						
							|  |  |  |         this.codeEditor = null; | 
					
						
							| 
									
										
										
										
											2019-05-08 19:55:24 +02:00
										 |  |  |         this.$component = ctx.$tabContent.find('.note-detail-code'); | 
					
						
							| 
									
										
										
										
											2019-07-03 20:37:59 +02:00
										 |  |  |         this.$editorEl = this.$component.find('.note-detail-code-editor'); | 
					
						
							| 
									
										
										
										
											2019-05-08 19:55:24 +02:00
										 |  |  |         this.$executeScriptButton = ctx.$tabContent.find(".execute-script-button"); | 
					
						
							| 
									
										
										
										
											2018-03-27 00:22:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-19 16:56:16 +02:00
										 |  |  |         utils.bindElShortcut(ctx.$tabContent, "ctrl+return", () => this.executeCurrentNote()); | 
					
						
							| 
									
										
										
										
											2018-03-27 00:22:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 14:34:03 +02:00
										 |  |  |         this.$executeScriptButton.click(() => this.executeCurrentNote()); | 
					
						
							| 
									
										
										
										
											2018-03-27 00:22:02 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-12 12:58:55 +02:00
										 |  |  |     async render() { | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  |         await libraryLoader.requireLibrary(libraryLoader.CODE_MIRROR); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!this.codeEditor) { | 
					
						
							|  |  |  |             CodeMirror.keyMap.default["Shift-Tab"] = "indentLess"; | 
					
						
							|  |  |  |             CodeMirror.keyMap.default["Tab"] = "indentMore"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // these conflict with backward/forward navigation shortcuts
 | 
					
						
							|  |  |  |             delete CodeMirror.keyMap.default["Alt-Left"]; | 
					
						
							|  |  |  |             delete CodeMirror.keyMap.default["Alt-Right"]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             CodeMirror.modeURL = 'libraries/codemirror/mode/%N/%N.js'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 20:37:59 +02:00
										 |  |  |             this.codeEditor = CodeMirror(this.$editorEl[0], { | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  |                 value: "", | 
					
						
							|  |  |  |                 viewportMargin: Infinity, | 
					
						
							|  |  |  |                 indentUnit: 4, | 
					
						
							|  |  |  |                 matchBrackets: true, | 
					
						
							|  |  |  |                 matchTags: {bothTags: true}, | 
					
						
							|  |  |  |                 highlightSelectionMatches: {showToken: /\w/, annotateScrollbar: false}, | 
					
						
							|  |  |  |                 lint: true, | 
					
						
							|  |  |  |                 gutters: ["CodeMirror-lint-markers"], | 
					
						
							|  |  |  |                 lineNumbers: true, | 
					
						
							|  |  |  |                 tabindex: 100, | 
					
						
							|  |  |  |                 // we linewrap partly also because without it horizontal scrollbar displays only when you scroll
 | 
					
						
							|  |  |  |                 // all the way to the bottom of the note. With line wrap there's no horizontal scrollbar so no problem
 | 
					
						
							|  |  |  |                 lineWrapping: true | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 14:34:03 +02:00
										 |  |  |             this.onNoteChange(() => this.ctx.noteChanged()); | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-27 00:22:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  |         // CodeMirror breaks pretty badly on null so even though it shouldn't happen (guarded by consistency check)
 | 
					
						
							|  |  |  |         // we provide fallback
 | 
					
						
							|  |  |  |         this.codeEditor.setValue(this.ctx.note.content || ""); | 
					
						
							| 
									
										
										
										
											2018-03-27 00:22:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  |         const info = CodeMirror.findModeByMIME(this.ctx.note.mime); | 
					
						
							| 
									
										
										
										
											2018-03-27 00:22:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  |         if (info) { | 
					
						
							|  |  |  |             this.codeEditor.setOption("mode", info.mime); | 
					
						
							|  |  |  |             CodeMirror.autoLoadMode(this.codeEditor, info.mode); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-27 00:22:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 19:11:42 +02:00
										 |  |  |         this.show(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     show() { | 
					
						
							|  |  |  |         this.$component.show(); | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  |         this.codeEditor.refresh(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-27 00:22:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  |     getContent() { | 
					
						
							|  |  |  |         return this.codeEditor.getValue(); | 
					
						
							| 
									
										
										
										
											2018-05-26 19:27:47 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-27 00:22:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  |     focus() { | 
					
						
							|  |  |  |         this.codeEditor.focus(); | 
					
						
							| 
									
										
										
										
											2018-03-27 00:22:02 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-26 19:27:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  |     async executeCurrentNote() { | 
					
						
							|  |  |  |         // ctrl+enter is also used elsewhere so make sure we're running only when appropriate
 | 
					
						
							|  |  |  |         if (this.ctx.note.type !== 'code') { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-27 00:22:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  |         // make sure note is saved so we load latest changes
 | 
					
						
							|  |  |  |         await noteDetailService.saveNotesIfChanged(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (this.ctx.note.mime.endsWith("env=frontend")) { | 
					
						
							|  |  |  |             await bundleService.getAndExecuteBundle(this.ctx.note.noteId); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-09-03 16:05:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  |         if (this.ctx.note.mime.endsWith("env=backend")) { | 
					
						
							|  |  |  |             await server.post('script/run/' + this.ctx.note.noteId); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-27 00:22:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  |         infoService.showMessage("Note executed"); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-27 00:22:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  |     onNoteChange(func) { | 
					
						
							|  |  |  |         this.codeEditor.on('change', func); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     cleanup() { | 
					
						
							|  |  |  |         if (this.codeEditor) { | 
					
						
							|  |  |  |             this.codeEditor.setValue(''); | 
					
						
							| 
									
										
										
										
											2018-10-31 12:29:01 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     scrollToTop() { | 
					
						
							|  |  |  |         this.$component.scrollTop(0); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-10-31 12:29:01 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-04 00:16:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default NoteDetailCode; |