| 
									
										
										
										
											2023-09-27 15:05:34 +03:00
										 |  |  | /********************************************************************** | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | **********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-23 14:37:14 +03:00
										 |  |  | Element.prototype.visibleInViewport = function(partial=false){ | 
					
						
							|  |  |  |   var { top, left, bottom, right } = this.getBoundingClientRect() | 
					
						
							|  |  |  |   var { innerHeight, innerWidth } = window | 
					
						
							|  |  |  |   return partial | 
					
						
							|  |  |  |     ? ((top > 0  | 
					
						
							|  |  |  | 				&& top < innerHeight)  | 
					
						
							|  |  |  | 			|| (bottom > 0  | 
					
						
							|  |  |  | 				&& bottom < innerHeight)) | 
					
						
							|  |  |  | 		&& ((left > 0  | 
					
						
							|  |  |  | 				&& left < innerWidth)  | 
					
						
							|  |  |  | 			|| (right > 0  | 
					
						
							|  |  |  | 				&& right < innerWidth)) | 
					
						
							|  |  |  |     : (top >= 0  | 
					
						
							|  |  |  | 		&& left >= 0  | 
					
						
							|  |  |  | 		&& bottom <= innerHeight  | 
					
						
							|  |  |  | 		&& right <= innerWidth) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-30 03:51:33 +03:00
										 |  |  | //---------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // XXX should these be here???
 | 
					
						
							|  |  |  | HTMLElement.encode = function(str){ | 
					
						
							|  |  |  | 	var span = document.createElement('span') | 
					
						
							| 
									
										
										
										
											2023-10-31 17:38:55 +03:00
										 |  |  | 	// XXX
 | 
					
						
							|  |  |  | 	return str | 
					
						
							|  |  |  | 		.replace(/&/g, '&')  | 
					
						
							|  |  |  | 		.replace(/</g, '<') | 
					
						
							|  |  |  | 		.replace(/>/g, '>') } | 
					
						
							|  |  |  | 	/*/ | 
					
						
							| 
									
										
										
										
											2023-10-30 03:51:33 +03:00
										 |  |  | 	span.innerText = str | 
					
						
							|  |  |  | 	return span.innerHTML } | 
					
						
							| 
									
										
										
										
											2023-10-31 17:38:55 +03:00
										 |  |  | 	//*/
 | 
					
						
							|  |  |  | // XXX this does not convert <br> back to \n...
 | 
					
						
							| 
									
										
										
										
											2023-10-30 03:51:33 +03:00
										 |  |  | HTMLElement.decode = function(str){ | 
					
						
							|  |  |  | 	var span = document.createElement('span') | 
					
						
							| 
									
										
										
										
											2023-10-31 17:38:55 +03:00
										 |  |  | 	// XXX
 | 
					
						
							|  |  |  | 	return str | 
					
						
							|  |  |  | 		.replace(/%lt;/g, '<') | 
					
						
							|  |  |  | 		.replace(/>/g, '>')  | 
					
						
							|  |  |  | 		.replace(/&/g, '&') } | 
					
						
							|  |  |  | 	/*/ | 
					
						
							| 
									
										
										
										
											2023-10-30 03:51:33 +03:00
										 |  |  | 	span.innerHTML = str | 
					
						
							|  |  |  | 	return span.innerText } | 
					
						
							| 
									
										
										
										
											2023-10-31 17:38:55 +03:00
										 |  |  | 	//*/
 | 
					
						
							| 
									
										
										
										
											2023-10-30 03:51:33 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-23 14:37:14 +03:00
										 |  |  | //---------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-27 15:05:34 +03:00
										 |  |  | HTMLTextAreaElement.prototype.updateSize = function(){ | 
					
						
							|  |  |  | 	this.style.height = '' | 
					
						
							|  |  |  | 	this.style.height = this.scrollHeight + 'px'  | 
					
						
							|  |  |  | 	return this } | 
					
						
							|  |  |  | HTMLTextAreaElement.prototype.autoUpdateSize = function(){ | 
					
						
							|  |  |  | 	var that = this | 
					
						
							|  |  |  | 	this.addEventListener('input',  | 
					
						
							|  |  |  | 		function(evt){ | 
					
						
							|  |  |  | 			that.updateSize() })  | 
					
						
							|  |  |  | 	return this } | 
					
						
							| 
									
										
										
										
											2023-10-14 22:49:02 +03:00
										 |  |  | HTMLTextAreaElement.prototype.getTextGeometry = function(){ | 
					
						
							|  |  |  | 	var offset = this.selectionStart | 
					
						
							|  |  |  | 	var text = this.value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// get the relevant styles...
 | 
					
						
							|  |  |  | 	var style = getComputedStyle(this) | 
					
						
							| 
									
										
										
										
											2023-10-23 17:37:53 +03:00
										 |  |  | 	var paddingV = parseFloat(style.paddingTop) + parseFloat(style.paddingBottom) | 
					
						
							| 
									
										
										
										
											2023-10-14 22:49:02 +03:00
										 |  |  | 	var s = {} | 
					
						
							|  |  |  | 	for(var i=0; i < style.length; i++){ | 
					
						
							|  |  |  | 		var k = style[i] | 
					
						
							|  |  |  | 		if(k.startsWith('font') | 
					
						
							|  |  |  | 				|| k.startsWith('line') | 
					
						
							|  |  |  | 				|| k.startsWith('white-space')){ | 
					
						
							|  |  |  | 			s[k] = style[k] } } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var carret = document.createElement('span') | 
					
						
							| 
									
										
										
										
											2023-10-23 17:37:53 +03:00
										 |  |  | 	carret.innerText = '|'  | 
					
						
							| 
									
										
										
										
											2023-10-14 22:49:02 +03:00
										 |  |  | 	carret.style.margin = '0px' | 
					
						
							|  |  |  | 	carret.style.padding = '0px' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var span = document.createElement('span') | 
					
						
							|  |  |  | 	Object.assign(span.style, { | 
					
						
							|  |  |  | 		...s, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		position: 'fixed', | 
					
						
							|  |  |  | 		display: 'block', | 
					
						
							| 
									
										
										
										
											2023-10-30 03:46:07 +03:00
										 |  |  | 		/* DEBUG... | 
					
						
							|  |  |  | 		top: '0px', | 
					
						
							|  |  |  | 		left: '0px', | 
					
						
							|  |  |  | 		/*/ | 
					
						
							| 
									
										
										
										
											2023-10-14 22:49:02 +03:00
										 |  |  | 		top: '-100%', | 
					
						
							|  |  |  | 		left: '-100%', | 
					
						
							| 
									
										
										
										
											2023-10-30 03:46:07 +03:00
										 |  |  | 		//*/
 | 
					
						
							| 
									
										
										
										
											2023-10-23 17:37:53 +03:00
										 |  |  | 		width: style.width, | 
					
						
							|  |  |  | 		height: style.height, | 
					
						
							| 
									
										
										
										
											2023-10-14 22:49:02 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		padding: style.padding, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-23 17:37:53 +03:00
										 |  |  | 		boxSizing: style.boxSizing, | 
					
						
							| 
									
										
										
										
											2023-10-30 03:46:07 +03:00
										 |  |  | 		whiteSpace: style.whiteSpace, | 
					
						
							| 
									
										
										
										
											2023-10-23 17:37:53 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-14 22:49:02 +03:00
										 |  |  | 		outline: 'solid 1px red', | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		pointerEvents: 'none', | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2023-10-23 17:37:53 +03:00
										 |  |  | 	span.append( | 
					
						
							|  |  |  | 		text.slice(0, offset), | 
					
						
							|  |  |  | 		carret, | 
					
						
							| 
									
										
										
										
											2023-10-23 17:43:25 +03:00
										 |  |  | 		// NOTE: wee need the rest of the text for the carret to be typeset
 | 
					
						
							|  |  |  | 		// 		to the correct line...
 | 
					
						
							| 
									
										
										
										
											2023-10-23 17:37:53 +03:00
										 |  |  | 		text.slice(offset)) | 
					
						
							| 
									
										
										
										
											2023-10-14 22:49:02 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	document.body.append(span) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var res = { | 
					
						
							|  |  |  | 		length: text.length, | 
					
						
							| 
									
										
										
										
											2023-10-23 16:47:19 +03:00
										 |  |  | 		lines: Math.floor( | 
					
						
							| 
									
										
										
										
											2023-10-23 17:37:53 +03:00
										 |  |  | 			(this.offsetHeight - paddingV)  | 
					
						
							| 
									
										
										
										
											2023-10-23 16:47:19 +03:00
										 |  |  | 				/ carret.offsetHeight), | 
					
						
							| 
									
										
										
										
											2023-10-14 22:49:02 +03:00
										 |  |  | 		line: Math.floor(carret.offsetTop / carret.offsetHeight), | 
					
						
							|  |  |  | 		offset: offset, | 
					
						
							|  |  |  | 		offsetLeft: carret.offsetLeft, | 
					
						
							|  |  |  | 		offsetTop: carret.offsetTop, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	span.remove() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return res } | 
					
						
							| 
									
										
										
										
											2023-09-27 15:05:34 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-07 07:04:58 +03:00
										 |  |  | // calculate number of lines in text area (both wrapped and actual lines)
 | 
					
						
							|  |  |  | Object.defineProperty(HTMLTextAreaElement.prototype, 'heightLines', { | 
					
						
							|  |  |  | 	enumerable: false, | 
					
						
							|  |  |  | 	get: function(){ | 
					
						
							|  |  |  | 		var style = getComputedStyle(this) | 
					
						
							|  |  |  | 		return Math.floor( | 
					
						
							|  |  |  | 			(this.scrollHeight  | 
					
						
							|  |  |  | 				- parseFloat(style.paddingTop) | 
					
						
							|  |  |  | 				- parseFloat(style.paddingBottom))  | 
					
						
							|  |  |  | 			/ (parseFloat(style.lineHeight)  | 
					
						
							|  |  |  | 				|| parseFloat(style.fontSize))) }, }) | 
					
						
							|  |  |  | Object.defineProperty(HTMLTextAreaElement.prototype, 'lines', { | 
					
						
							|  |  |  | 	enumerable: false, | 
					
						
							|  |  |  | 	get: function(){ | 
					
						
							|  |  |  | 		return this.value | 
					
						
							|  |  |  | 			.split(/\n/g) | 
					
						
							|  |  |  | 			.length }, }) | 
					
						
							|  |  |  | // XXX this does not account for wrapping...
 | 
					
						
							| 
									
										
										
										
											2023-09-28 20:49:06 +03:00
										 |  |  | Object.defineProperty(HTMLTextAreaElement.prototype, 'caretLine', { | 
					
						
							|  |  |  | 	enumerable: false, | 
					
						
							|  |  |  | 	get: function(){ | 
					
						
							|  |  |  | 		var offset = this.selectionStart | 
					
						
							|  |  |  | 		return offset != null ? | 
					
						
							|  |  |  | 			this.value | 
					
						
							|  |  |  | 				.slice(0, offset) | 
					
						
							|  |  |  | 				.split(/\n/g) | 
					
						
							|  |  |  | 				.length | 
					
						
							| 
									
										
										
										
											2023-10-07 07:04:58 +03:00
										 |  |  | 			: undefined }, }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Object.defineProperty(HTMLTextAreaElement.prototype, 'caretOffset', { | 
					
						
							|  |  |  | 	enumerable: false, | 
					
						
							|  |  |  | 	get: function(){ | 
					
						
							|  |  |  | 		var offset = this.selectionStart | 
					
						
							|  |  |  | 		var r = document.createRange() | 
					
						
							|  |  |  | 		r.setStart(this, offset) | 
					
						
							|  |  |  | 		r.setEnd(this, offset) | 
					
						
							|  |  |  | 		var rect = r.getBoundingClientRect() | 
					
						
							|  |  |  | 		return { | 
					
						
							|  |  |  | 			top: rect.top,  | 
					
						
							|  |  |  | 			left: rect.left, | 
					
						
							|  |  |  | 		} }, | 
					
						
							| 
									
										
										
										
											2023-09-28 20:49:06 +03:00
										 |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-27 15:05:34 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-27 15:41:29 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-07 07:04:58 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-27 15:05:34 +03:00
										 |  |  | /********************************************************************** | 
					
						
							|  |  |  | * vim:set ts=4 sw=4 :                                                */ |