mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 13:01:31 +08:00 
			
		
		
		
	chore(code/find): reimplement single replace
This commit is contained in:
		
							parent
							
								
									690337ee40
								
							
						
					
					
						commit
						b646475018
					
				| @ -54,43 +54,12 @@ export default class FindInCode { | |||||||
|         codeEditor?.cleanSearch(); |         codeEditor?.cleanSearch(); | ||||||
|         codeEditor?.focus(); |         codeEditor?.focus(); | ||||||
|     } |     } | ||||||
|     async replace(replaceText: string) { |  | ||||||
|         // this.findResult may be undefined and null
 |  | ||||||
|         if (!this.findResult || this.findResult.length === 0) { |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
|         let currentFound = -1; |  | ||||||
|         this.findResult.forEach((marker, index) => { |  | ||||||
|             const pos = marker.find(); |  | ||||||
|             if (pos) { |  | ||||||
|                 if (marker.className === FIND_RESULT_SELECTED_CSS_CLASSNAME) { |  | ||||||
|                     currentFound = index; |  | ||||||
|                     return; |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         }); |  | ||||||
|         if (currentFound >= 0) { |  | ||||||
|             let marker = this.findResult[currentFound]; |  | ||||||
|             let pos = marker.find(); |  | ||||||
|             const codeEditor = await this.getCodeEditor(); |  | ||||||
|             const doc = codeEditor?.doc; |  | ||||||
|             if (doc) { |  | ||||||
|                 doc.replaceRange(replaceText, pos.from, pos.to); |  | ||||||
|             } |  | ||||||
|             marker.clear(); |  | ||||||
| 
 | 
 | ||||||
|             let nextFound; |     async replace(replaceText: string) { | ||||||
|             if (currentFound === this.findResult.length - 1) { |         const codeEditor = await this.getCodeEditor(); | ||||||
|                 nextFound = 0; |         codeEditor?.replace(replaceText); | ||||||
|             } else { |  | ||||||
|                 nextFound = currentFound; |  | ||||||
|             } |  | ||||||
|             this.findResult.splice(currentFound, 1); |  | ||||||
|             if (this.findResult.length > 0) { |  | ||||||
|                 this.findNext(0, nextFound, nextFound); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|     async replaceAll(replaceText: string) { |     async replaceAll(replaceText: string) { | ||||||
|         if (!this.findResult || this.findResult.length === 0) { |         if (!this.findResult || this.findResult.length === 0) { | ||||||
|             return; |             return; | ||||||
|  | |||||||
| @ -46,6 +46,17 @@ export class SearchHighlighter { | |||||||
|         this.#scrollToMatchNearestSelection(); |         this.#scrollToMatchNearestSelection(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     replaceActiveMatch(replacementText: string) { | ||||||
|  |         if (!this.parsedMatches.length || this.currentFound === 0) return; | ||||||
|  | 
 | ||||||
|  |         const matchIndex = this.currentFound - 1; | ||||||
|  |         const match = this.parsedMatches[matchIndex]; | ||||||
|  | 
 | ||||||
|  |         this.view.dispatch({ | ||||||
|  |             changes: { from: match.from, to: match.to, insert: replacementText } | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     scrollToMatch(matchIndex: number) { |     scrollToMatch(matchIndex: number) { | ||||||
|         if (this.parsedMatches.length <= matchIndex) { |         if (this.parsedMatches.length <= matchIndex) { | ||||||
|             return; |             return; | ||||||
|  | |||||||
| @ -194,6 +194,10 @@ export default class CodeMirror extends EditorView { | |||||||
|         this.searchPlugin?.scrollToMatch(nextFound); |         this.searchPlugin?.scrollToMatch(nextFound); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     async replace(replaceText: string) { | ||||||
|  |         this.searchPlugin?.replaceActiveMatch(replaceText); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     cleanSearch() { |     cleanSearch() { | ||||||
|         if (this.searchPlugin) { |         if (this.searchPlugin) { | ||||||
|             this.dispatch({ |             this.dispatch({ | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Elian Doran
						Elian Doran