Added cursor_token_match util file
This commit is contained in:
		
							parent
							
								
									2044648179
								
							
						
					
					
						commit
						16a9bc6e93
					
				
							
								
								
									
										26
									
								
								app/javascript/gabsocial/utils/cursor_token_match.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								app/javascript/gabsocial/utils/cursor_token_match.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,26 @@
 | 
				
			|||||||
 | 
					'use strict';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function textAtCursorMatchesToken(str, caretPosition, searchTokens) {
 | 
				
			||||||
 | 
					  let word;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  let left = str.slice(0, caretPosition).search(/\S+$/);
 | 
				
			||||||
 | 
					  let right = str.slice(caretPosition).search(/\s/);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (right < 0) {
 | 
				
			||||||
 | 
					    word = str.slice(left);
 | 
				
			||||||
 | 
					  } else {
 | 
				
			||||||
 | 
					    word = str.slice(left, right + caretPosition);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (!word || word.trim().length < 3 || searchTokens.indexOf(word[0]) === -1) {
 | 
				
			||||||
 | 
					    return [null, null];
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  word = word.trim().toLowerCase();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (word.length > 0) {
 | 
				
			||||||
 | 
					    return [left + 1, word];
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return [null, null];
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user