mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 13:01:31 +08:00 
			
		
		
		
	implemented new way of adding extra languages to note type picker
This commit is contained in:
		
							parent
							
								
									0558c30b56
								
							
						
					
					
						commit
						cf5c1c1be1
					
				| @ -50,7 +50,26 @@ const DEFAULT_MIME_TYPES = [ | |||||||
|     { mime: 'text/x-yaml', title: 'YAML' } |     { mime: 'text/x-yaml', title: 'YAML' } | ||||||
| ]; | ]; | ||||||
| 
 | 
 | ||||||
| let mimeTypes = DEFAULT_MIME_TYPES; | let mimeTypes = null; | ||||||
|  | 
 | ||||||
|  | async function getMimeTypes() { | ||||||
|  |     if (!mimeTypes) { | ||||||
|  |         let customCodeMimeTypes = []; | ||||||
|  | 
 | ||||||
|  |         try { | ||||||
|  |             customCodeMimeTypes = await server.get('custom-code-mime-types'); | ||||||
|  |         } | ||||||
|  |         catch (e) { | ||||||
|  |             log.error(`Could not retrieve custom mime types: ${e.message}`); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         mimeTypes = DEFAULT_MIME_TYPES.concat(customCodeMimeTypes); | ||||||
|  | 
 | ||||||
|  |         mimeTypes.sort((a, b) => a.title < b.title ? -1 : 1); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return mimeTypes; | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| export default class NoteTypeContext { | export default class NoteTypeContext { | ||||||
|     /** |     /** | ||||||
| @ -68,15 +87,15 @@ export default class NoteTypeContext { | |||||||
|         this.$renderButton = ctx.$tabContent.find('.render-button'); |         this.$renderButton = ctx.$tabContent.find('.render-button'); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     update() { |     async update() { | ||||||
|         this.$noteTypeButton.prop("disabled", |         this.$noteTypeButton.prop("disabled", | ||||||
|             () => ["file", "image", "search"].includes(this.ctx.note.type)); |             () => ["file", "image", "search"].includes(this.ctx.note.type)); | ||||||
| 
 | 
 | ||||||
|         this.$noteTypeDesc.text(this.findTypeTitle(this.ctx.note.type)); |         this.$noteTypeDesc.text(await this.findTypeTitle(this.ctx.note.type, this.ctx.note.mime)); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** actual body is rendered lazily on note-type button click */ |     /** actual body is rendered lazily on note-type button click */ | ||||||
|     renderDropdown() { |     async renderDropdown() { | ||||||
|         this.$noteTypeDropdown.empty(); |         this.$noteTypeDropdown.empty(); | ||||||
| 
 | 
 | ||||||
|         for (const noteType of NOTE_TYPES.filter(nt => nt.selectable)) { |         for (const noteType of NOTE_TYPES.filter(nt => nt.selectable)) { | ||||||
| @ -102,7 +121,7 @@ export default class NoteTypeContext { | |||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         for (const mimeType of mimeTypes) { |         for (const mimeType of await getMimeTypes()) { | ||||||
|             const $mimeLink = $('<a class="dropdown-item">') |             const $mimeLink = $('<a class="dropdown-item">') | ||||||
|                 .attr("data-mime-type", mimeType.mime) |                 .attr("data-mime-type", mimeType.mime) | ||||||
|                 .append('<span class="check">✓</span> ') |                 .append('<span class="check">✓</span> ') | ||||||
| @ -122,11 +141,19 @@ export default class NoteTypeContext { | |||||||
|         this.$renderButton.toggle(this.ctx.note.type === 'render'); |         this.$renderButton.toggle(this.ctx.note.type === 'render'); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     findTypeTitle(type) { |     async findTypeTitle(type, mime) { | ||||||
|  |         if (type === 'code') { | ||||||
|  |             const mimeTypes = await getMimeTypes(); | ||||||
|  |             const found = mimeTypes.find(mt => mt.mime === mime); | ||||||
|  | 
 | ||||||
|  |             return found ? found.title : mime; | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|             const noteType = NOTE_TYPES.find(nt => nt.type === type); |             const noteType = NOTE_TYPES.find(nt => nt.type === type); | ||||||
| 
 | 
 | ||||||
|             return noteType ? noteType.title : type; |             return noteType ? noteType.title : type; | ||||||
|         } |         } | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|     async save(type, mime) { |     async save(type, mime) { | ||||||
|         if (type !== this.ctx.note.type && !await this.confirmChangeIfContent()) { |         if (type !== this.ctx.note.type && !await this.confirmChangeIfContent()) { | ||||||
|  | |||||||
							
								
								
									
										24
									
								
								src/routes/api/custom_code_mime_types.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								src/routes/api/custom_code_mime_types.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,24 @@ | |||||||
|  | "use strict"; | ||||||
|  | 
 | ||||||
|  | const attributesService = require('../../services/attributes'); | ||||||
|  | const log = require('../../services/log'); | ||||||
|  | 
 | ||||||
|  | async function get() { | ||||||
|  |     const notes = await attributesService.getNotesWithLabel('codeMimeTypes'); | ||||||
|  |     let merged = []; | ||||||
|  | 
 | ||||||
|  |     for (const note of notes) { | ||||||
|  |         try { | ||||||
|  |             merged = merged.concat(await note.getJsonContent()); | ||||||
|  |         } | ||||||
|  |         catch (e) { | ||||||
|  |             log.error(`Cannot merge mime types from note=${note.noteId}: ${e.message}`); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return merged; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | module.exports = { | ||||||
|  |     get | ||||||
|  | }; | ||||||
| @ -35,6 +35,7 @@ const dateNotesRoute = require('./api/date_notes'); | |||||||
| const linkMapRoute = require('./api/link_map'); | const linkMapRoute = require('./api/link_map'); | ||||||
| const linksRoute = require('./api/links'); | const linksRoute = require('./api/links'); | ||||||
| const clipperRoute = require('./api/clipper'); | const clipperRoute = require('./api/clipper'); | ||||||
|  | const customCodeMimeTypesRoute = require('./api/custom_code_mime_types.js'); | ||||||
| 
 | 
 | ||||||
| const log = require('../services/log'); | const log = require('../services/log'); | ||||||
| const express = require('express'); | const express = require('express'); | ||||||
| @ -238,6 +239,8 @@ function register(app) { | |||||||
|     route(POST, '/api/clipper/notes', clipperMiddleware, clipperRoute.createNote, apiResultHandler); |     route(POST, '/api/clipper/notes', clipperMiddleware, clipperRoute.createNote, apiResultHandler); | ||||||
|     route(POST, '/api/clipper/open/:noteId', clipperMiddleware, clipperRoute.openNote, apiResultHandler); |     route(POST, '/api/clipper/open/:noteId', clipperMiddleware, clipperRoute.openNote, apiResultHandler); | ||||||
| 
 | 
 | ||||||
|  |     apiRoute(GET, '/api/custom-code-mime-types', customCodeMimeTypesRoute.get); | ||||||
|  | 
 | ||||||
|     app.use('', router); |     app.use('', router); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 zadam
						zadam