Lock the scroll of the div and textarea to keep the highlighting accurate.
This commit is contained in:
		
							parent
							
								
									cc48040f11
								
							
						
					
					
						commit
						49905f1273
					
				| @ -49,6 +49,8 @@ | |||||||
|   pointer-events: none; |   pointer-events: none; | ||||||
|   color: transparent; |   color: transparent; | ||||||
|   white-space: pre-wrap; |   white-space: pre-wrap; | ||||||
|  |   word-wrap: break-word; | ||||||
|  |   overflow-y: scroll; | ||||||
| 
 | 
 | ||||||
|   .highlighted { |   .highlighted { | ||||||
|     background: #ffff00; |     background: #ffff00; | ||||||
|  | |||||||
| @ -1,4 +1,4 @@ | |||||||
| import React, { ReactNode, useMemo, useState } from "react"; | import React, { ReactNode, useMemo, useRef, useState } from "react"; | ||||||
| import "./Editor.css"; | import "./Editor.css"; | ||||||
| import { Highlight } from "./highlight"; | import { Highlight } from "./highlight"; | ||||||
| import { buildShadow } from "./shadow"; | import { buildShadow } from "./shadow"; | ||||||
| @ -63,15 +63,29 @@ function Editor({ defaultValue = default_org_source }) { | |||||||
|     setHighlights([]); |     setHighlights([]); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   const textAreaRef = useRef<HTMLTextAreaElement>(null); | ||||||
|  |   const shadowRef = useRef<HTMLDivElement>(null); | ||||||
|  |   function onTextAreaScroll() { | ||||||
|  |     if (shadowRef.current !== null && textAreaRef.current !== null) { | ||||||
|  |       const textAreaScrollTop = textAreaRef.current.scrollTop; | ||||||
|  |       shadowRef.current.scrollTop = textAreaScrollTop; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   return ( |   return ( | ||||||
|     <div className="Editor"> |     <div className="Editor"> | ||||||
|       <div className="Editor-textwrapper"> |       <div className="Editor-textwrapper"> | ||||||
|         <textarea |         <textarea | ||||||
|  |           ref={textAreaRef} | ||||||
|           onChange={handleChange} |           onChange={handleChange} | ||||||
|           className="Editor-textarea" |           className="Editor-textarea" | ||||||
|           value={value} |           value={value} | ||||||
|  |           onScroll={onTextAreaScroll} | ||||||
|         /> |         /> | ||||||
|         <div className="Editor-underlay">{buildShadow(highlights, value)}</div> |         <div ref={shadowRef} className="Editor-underlay"> | ||||||
|  |           {buildShadow(highlights, value)} | ||||||
|  |           <br/> | ||||||
|  |         </div> | ||||||
|       </div> |       </div> | ||||||
|       <OrgAst |       <OrgAst | ||||||
|         setHighlight={setHighlight} |         setHighlight={setHighlight} | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ | |||||||
|   flex: 1; |   flex: 1; | ||||||
|   background: #eeeeee; |   background: #eeeeee; | ||||||
|   padding: 5px; |   padding: 5px; | ||||||
|   overflow: scroll; |   overflow: auto; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| .OrgAstNode { | .OrgAstNode { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Tom Alexander
						Tom Alexander