File size: 3,615 Bytes
07bbbbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
 * Setup click handler for links
 */
import { scrollToAnchor } from './utils';
import { useSystemStore } from '../../store/useSystemStore';
// Type definitions
interface NavigationMessage {
  type: 'navigate';
  url: string;
}

function mdxLinkHandler(anchor: HTMLAnchorElement, win: Window, event: Event): boolean {
    console.debug("MDX link element found:", anchor.href);
    event.preventDefault();
    
    // Check if it's an audio link
    let service_path = anchor.href.substring(useSystemStore.getState().baseUrl.length);
    if (service_path.startsWith('sound')) {
        console.debug("Audio link detected, playing audio:", anchor.href);
        // Create and play audio element
        const audio = new Audio(anchor.href);
        audio.play().catch(function(error: Error) {
            console.debug("Audio play failed:", error);
        });
    } else {
        // Send navigation message for non-audio links
        console.debug("Sending navigation message:", anchor.href);
        const message: NavigationMessage = {
            type: 'navigate',
            url: anchor.href
        };
        win.parent.postMessage(message, '*');
        console.debug("Navigation message sent:", message);
    }
    return true;
}

export function clickHandler(event: Event, cur_doc:Document, cur_win:Window): boolean {
    console.debug("Click event detected", event.target);
    // Find the closest anchor element
    let element = event.target as Element | null;
    while (element && element.tagName !== 'A') {
        element = element.parentElement;
    }

    if (element && element.tagName.toLowerCase() === 'a') {
        const anchor = element as HTMLAnchorElement;
        
        if (anchor.href) {
            // Check if it's a local fragment link (starts with #)
            const href = anchor.getAttribute('href');
            if (href && href.startsWith('#')) {
                event.preventDefault();
                console.debug("Local fragment link detected, allowing default behavior:", href);
                scrollToAnchor(cur_doc, href.substring(1));
                return true;
            }
            
            // Check if it's a same-page fragment link (same URL with #fragment)
            const currentUrl = cur_win.location.href;
            const linkUrl = anchor.href;
            
            // Remove fragment from both URLs for comparison
            const currentUrlWithoutFragment = currentUrl.split('#')[0];
            const linkUrlWithoutFragment = linkUrl.split('#')[0];
            
            // If base URLs are the same and link has a fragment, it's a same-page fragment link
            if (currentUrlWithoutFragment === linkUrlWithoutFragment && linkUrl.indexOf('#') !== -1) {
                console.debug("Same-page fragment link detected, allowing default behavior:", linkUrl);
                return true;
            }
            
            // Check if it's a link with http://mdict.cn/service/ href
            if (anchor.href.startsWith(useSystemStore.getState().baseUrl)) {
                 return mdxLinkHandler(anchor, cur_win, event);
            }
        }
    }
    return false;
}

// // Setup link click handler
// export function clickHandlerSetup(iframe: HTMLIFrameElement): void {
//     console.debug("Setting up click handler");
//     const doc = iframe.contentDocument;
//     if (!doc) {
//         console.error("Failed to get document");
//         return;
//     }
//     // Add event listener for all link clicks
//     doc.addEventListener('click', (event: MouseEvent) => clickHandler(event, iframe), true); 
// }