maintain scroll position

Hunter
Monday, July 7th, 2025 at 7:37 PM

index.html+28-6

index.html
125  	</div>
126  	
127  	<div class="preview-pane">
    -		<iframe id="preview" sandbox="allow-scripts"></iframe>
128 +		<iframe id="preview"></iframe>
129  	</div>
130  
131  	<script type="module">

136  
137  		function updatePreview() {
138  			const code = editorView.state.doc.toString();
    -			const blob = new Blob([code], { type: 'text/html' });
    -			const url = URL.createObjectURL(blob);
    -			preview.src = url;
139  			
    -			// Clean up the previous URL to prevent memory leaks
    -			setTimeout(() => URL.revokeObjectURL(url), 1000);
140 +			// Store scroll position before updating
141 +			let scrollX = 0, scrollY = 0;
142 +			try {
143 +				if (preview.contentWindow && preview.contentDocument) {
144 +					scrollX = preview.contentWindow.scrollX;
145 +					scrollY = preview.contentWindow.scrollY;
146 +				}
147 +			} catch (e) {
148 +				// Ignore cross-origin errors
149 +			}
150 +			
151 +			// Write HTML directly to iframe document
152 +			const doc = preview.contentDocument || preview.contentWindow.document;
153 +			doc.open();
154 +			doc.write(code);
155 +			doc.close();
156 +			
157 +			// Restore scroll position after a short delay to ensure content is rendered
158 +			setTimeout(() => {
159 +				try {
160 +					if (preview.contentWindow) {
161 +						preview.contentWindow.scrollTo(scrollX, scrollY);
162 +					}
163 +				} catch (e) {
164 +					// Ignore cross-origin errors
165 +				}
166 +			}, 10);
167  		}
168  
169  		function saveToStorage() {