@@ -63,40 +63,6 @@ export function setUp() {
6363 }
6464 } ) ;
6565
66- // Delegate relative links to native file opening
67- previewPane . addEventListener ( 'click' , async event => {
68- const target = event . target ;
69- if ( ! ( target instanceof HTMLElement ) ) {
70- return ;
71- }
72-
73- const anchor = target . closest ( 'a' ) ;
74- if ( anchor === null ) {
75- return ;
76- }
77-
78- // Use getAttribute to get the raw href, not the resolved absolute URL
79- const href = anchor . getAttribute ( 'href' ) ;
80- if ( ! href ?. startsWith ( '../' ) ) {
81- return ;
82- }
83-
84- if ( typeof MarkEdit . getFileInfo !== 'function' ) {
85- return ;
86- }
87-
88- const basePath = ( await MarkEdit . getFileInfo ( ) ) ?. parentPath ;
89- if ( basePath === undefined ) {
90- return ;
91- }
92-
93- event . preventDefault ( ) ;
94- event . stopPropagation ( ) ;
95-
96- const absolutePath = joinPaths ( basePath , decodeURIComponent ( href ) ) ;
97- await MarkEdit . openFile ( absolutePath ) ;
98- } ) ;
99-
10066 const mutationObserver = new MutationObserver ( updateGutterStyle ) ;
10167 mutationObserver . observe ( previewPane , { attributes : true , attributeFilter : [ 'style' , 'class' ] } ) ;
10268
@@ -109,6 +75,11 @@ export function setUp() {
10975 renderHtmlPreview ( ) ;
11076 }
11177 } ) ;
78+
79+ // Delegate relative links to native file opening
80+ if ( typeof MarkEdit . getFileInfo === 'function' ) {
81+ previewPane . addEventListener ( 'click' , handleExternalFiles ) ;
82+ }
11283}
11384
11485export function setViewMode ( mode : ViewMode , needsDisplay = true ) {
@@ -325,6 +296,38 @@ async function saveGeneratedHtml(styled: boolean) {
325296 MarkEdit . showSavePanel ( { fileName, string } ) ;
326297}
327298
299+ async function handleExternalFiles ( event : MouseEvent ) {
300+ if ( ! ( event . target instanceof Element ) ) {
301+ return ;
302+ }
303+
304+ const anchor = event . target . closest ( 'a' ) ;
305+ if ( anchor === null ) {
306+ return ;
307+ }
308+
309+ // We need to handle this because it is outside of the webpage root
310+ const href = anchor . getAttribute ( 'href' ) ;
311+ if ( ! href ?. startsWith ( '../' ) ) {
312+ return ;
313+ }
314+
315+ const basePath = ( await MarkEdit . getFileInfo ( ) ) ?. parentPath ;
316+ if ( basePath === undefined ) {
317+ return ;
318+ }
319+
320+ try {
321+ const absolutePath = joinPaths ( basePath , decodeURIComponent ( href ) ) ;
322+ await MarkEdit . openFile ( absolutePath ) ;
323+
324+ event . preventDefault ( ) ;
325+ event . stopPropagation ( ) ;
326+ } catch ( error ) {
327+ console . error ( `Failed to open file: ${ error } ` ) ;
328+ }
329+ }
330+
328331const states : {
329332 viewMode : ViewMode ;
330333 splitter : Splitter | undefined ;
0 commit comments