mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 04:51:31 +08:00 
			
		
		
		
	more impl for search actions
This commit is contained in:
		
							parent
							
								
									ce09e4a1eb
								
							
						
					
					
						commit
						d67e1552ee
					
				
							
								
								
									
										39
									
								
								src/public/app/widgets/search_actions/execute_script.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								src/public/app/widgets/search_actions/execute_script.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,39 @@ | ||||
| import SpacedUpdate from "../../services/spaced_update.js"; | ||||
| import AbstractAction from "./abstract_action.js"; | ||||
| 
 | ||||
| const TPL = ` | ||||
| <tr> | ||||
|     <td> | ||||
|         Execute script: | ||||
|     </td> | ||||
|     <td> | ||||
|         <div style="display: flex; align-items: center"> | ||||
|             <div style="margin-right: 15px;" class="text-nowrap">Script: </div>  | ||||
|              | ||||
|             <input type="text"  | ||||
|                class="form-control script" | ||||
|                placeholder="note.title = note.title + '- suffix';"/> | ||||
|         </div> | ||||
|     </td> | ||||
|     <td> | ||||
|         <span class="bx bx-x icon-action" data-action-conf-del></span> | ||||
|     </td> | ||||
| </tr>`; | ||||
| 
 | ||||
| export default class ExecuteScriptSearchAction extends AbstractAction { | ||||
|     static get actionName() { return "executeScript"; } | ||||
| 
 | ||||
|     doRender() { | ||||
|         const $action = $(TPL); | ||||
|         const $script = $action.find('.script'); | ||||
|         $script.val(this.actionDef.script || ""); | ||||
| 
 | ||||
|         const spacedUpdate = new SpacedUpdate(async () => { | ||||
|             await this.saveAction({ script: $script.val() }); | ||||
|         }, 1000) | ||||
| 
 | ||||
|         $script.on('input', () => spacedUpdate.scheduleUpdate()); | ||||
| 
 | ||||
|         return $action; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										59
									
								
								src/public/app/widgets/search_actions/rename_relation.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								src/public/app/widgets/search_actions/rename_relation.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,59 @@ | ||||
| import SpacedUpdate from "../../services/spaced_update.js"; | ||||
| import AbstractAction from "./abstract_action.js"; | ||||
| 
 | ||||
| const TPL = ` | ||||
| <tr> | ||||
|     <td> | ||||
|         Rename relation: | ||||
|     </td> | ||||
|     <td> | ||||
|         <div style="display: flex; align-items: center"> | ||||
|             <div style="display: flex; align-items: center"> | ||||
|                 <div style="margin-right: 15px;">From:</div>  | ||||
|                  | ||||
|                 <input type="text"  | ||||
|                     class="form-control old-relation-name"  | ||||
|                     placeholder="old name"  | ||||
|                     pattern="[\\p{L}\\p{N}_:]+" | ||||
|                     title="Alphanumeric characters, underscore and colon are allowed characters."/> | ||||
|                  | ||||
|                 <div style="margin-right: 15px; margin-left: 15px;">To:</div>  | ||||
|                  | ||||
|                 <input type="text"  | ||||
|                     class="form-control new-relation-name"  | ||||
|                     placeholder="new name" | ||||
|                     pattern="[\\p{L}\\p{N}_:]+" | ||||
|                     title="Alphanumeric characters, underscore and colon are allowed characters."/> | ||||
|             </div> | ||||
|         </div> | ||||
|     </td> | ||||
|     <td> | ||||
|         <span class="bx bx-x icon-action" data-action-conf-del></span> | ||||
|     </td> | ||||
| </tr>`; | ||||
| 
 | ||||
| export default class RenameRelationSearchAction extends AbstractAction { | ||||
|     static get actionName() { return "renameRelation"; } | ||||
| 
 | ||||
|     doRender() { | ||||
|         const $action = $(TPL); | ||||
| 
 | ||||
|         const $oldRelationName = $action.find('.old-relation-name'); | ||||
|         $oldRelationName.val(this.actionDef.oldRelationName || ""); | ||||
| 
 | ||||
|         const $newRelationName = $action.find('.new-relation-name'); | ||||
|         $newRelationName.val(this.actionDef.newRelationName || ""); | ||||
| 
 | ||||
|         const spacedUpdate = new SpacedUpdate(async () => { | ||||
|             await this.saveAction({ | ||||
|                 oldRelationName: $oldRelationName.val(), | ||||
|                 newRelationName: $newRelationName.val() | ||||
|             }); | ||||
|         }, 1000) | ||||
| 
 | ||||
|         $oldRelationName.on('input', () => spacedUpdate.scheduleUpdate()); | ||||
|         $newRelationName.on('input', () => spacedUpdate.scheduleUpdate()); | ||||
| 
 | ||||
|         return $action; | ||||
|     } | ||||
| } | ||||
| @ -12,6 +12,8 @@ import DeleteRelationSearchAction from "./search_actions/delete_relation.js"; | ||||
| import RenameLabelSearchAction from "./search_actions/rename_label.js"; | ||||
| import SetLabelValueSearchAction from "./search_actions/set_label_value.js"; | ||||
| import SetRelationTargetSearchAction from "./search_actions/set_relation_target.js"; | ||||
| import RenameRelationSearchAction from "./search_actions/rename_relation.js"; | ||||
| import ExecuteScriptSearchAction from "./search_actions/execute_script.js"; | ||||
| 
 | ||||
| const TPL = ` | ||||
| <div class="search-definition-widget"> | ||||
| @ -205,8 +207,10 @@ for (const clazz of [ | ||||
|     DeleteLabelSearchAction, | ||||
|     DeleteRelationSearchAction, | ||||
|     RenameLabelSearchAction, | ||||
|     RenameRelationSearchAction, | ||||
|     SetLabelValueSearchAction, | ||||
|     SetRelationTargetSearchAction | ||||
|     SetRelationTargetSearchAction, | ||||
|     ExecuteScriptSearchAction | ||||
| ]) { | ||||
|     ACTION_CLASSES[clazz.actionName] = clazz; | ||||
| } | ||||
|  | ||||
| @ -71,14 +71,46 @@ const ACTION_HANDLERS = { | ||||
|         note.isDeleted; | ||||
|         note.save(); | ||||
|     }, | ||||
|     deleteLabel: (action, note) => { | ||||
|         for (const label of note.getOwnedLabels(action.labelName)) { | ||||
|             label.isDeleted = true; | ||||
|             label.save(); | ||||
|         } | ||||
|     }, | ||||
|     deleteRelation: (action, note) => { | ||||
|         for (const relation of note.getOwnedRelations(action.relationName)) { | ||||
|             relation.isDeleted = true; | ||||
|             relation.save(); | ||||
|         } | ||||
|     }, | ||||
|     renameLabel: (action, note) => { | ||||
|         for (const label of note.getOwnedLabels(action.oldLabelName)) { | ||||
|             label.name = action.newLabelName; | ||||
|             label.save(); | ||||
|         } | ||||
|     }, | ||||
|     renameRelation: (action, note) => { | ||||
|         for (const relation of note.getOwnedRelations(action.oldRelationName)) { | ||||
|             relation.name = action.newRelationName; | ||||
|             relation.save(); | ||||
|         } | ||||
|     }, | ||||
|     setLabelValue: (action, note) => { | ||||
|         note.setLabel(action.labelName, action.labelValue); | ||||
|     }, | ||||
|     setRelationTarget: (action, note) => { | ||||
|         note.setRelation(action.relationName, action.targetNoteId); | ||||
|     }, | ||||
|     executeScript: (action, note) => { | ||||
|         if (!action.script || !action.script.trim()) { | ||||
|             log.info("Ignoring executeScript since the script is empty.") | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         const scriptFunc = new Function("note", action.script); | ||||
|         scriptFunc(note); | ||||
| 
 | ||||
|         note.save(); | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| @ -132,9 +164,14 @@ async function searchAndExecute(req) { | ||||
|         } | ||||
| 
 | ||||
|         for (const action of actions) { | ||||
|             ACTION_HANDLERS[action.name](action, resultNote); | ||||
| 
 | ||||
|             try { | ||||
|                 log.info(`Applying action handler to note ${resultNote.noteId}: ${JSON.stringify(action)}`); | ||||
| 
 | ||||
|                 ACTION_HANDLERS[action.name](action, resultNote); | ||||
|             } | ||||
|             catch (e) { | ||||
|                 log.error(`ExecuteScript search action failed with ${e.message}`); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 zadam
						zadam