File size: 12,217 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
 * Word Picker Module for Dictionary Content
 * 
 * Provides touch/click word selection functionality for dictionary entries.
 * Allows users to tap or click on words to look them up in the dictionary.
 */

// Type definitions
import { getIframeGlobals } from './iframeSetup';
import { clickHandler } from './clickHandler';

interface WordPickerContext {
    touchStartPoint: TouchPoint | null;
    TouchTimerID: number;
    lastTouchUpTimeStamp: number;
}
// let iOS = ( navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false );
// let MacOS =  ( navigator.userAgent.match(/(Macintosh)/g) ? true : false );

function getContext(cur_win: Window): WordPickerContext {
    return (cur_win as any).wordPickerContext as WordPickerContext;
}

interface TouchPoint {
    screenX: number;
    screenY: number;
    target: Element | null;
}

// For touch word pickup
// Don't encodeURI(word) when calling this function, because mdx_url_func will encode it.
function mdx_click(word: string, posX: number, posY: number, cur_doc:Document, cur_win: Window): void {
    const global=getIframeGlobals(cur_win);
    const factor = (cur_doc.body.getBoundingClientRect().width + 16) / cur_win.innerWidth; // top.window.devicePixelRatio;
    const x = parseInt(String(posX * factor));
    const y = parseInt(String(posY * factor));
    const message: any=
    {
        type: 'lookup',
        word: word, 
        anchor: {x:x, y:y}, 
        fromEntry: {profile_id:global.profileId, entry_no:global.entryNo }
    };
    cur_win.parent.postMessage( message, "*");
}

const specialAlpha = "ùçœæàâïéèêëîôöüûäß''ñáíúóÙÇŒÆÀÂÏÉÈÊËÎÔÖÜÛÄßÑÁÍÚÓ";

function mdx_isAlpha(c: string): boolean {
    const charCode = c.charCodeAt(0);
    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c === '\'') || (charCode > 200 && charCode < 300)) {
        return true;
    } else {
        return specialAlpha.indexOf(c) >= 0;
    }
}


function mdx_processNode(cur_doc: Document, node: Text): void {
    const parentNode = node.parentNode;
    const nodeValue = node.nodeValue || '';
    const str = nodeValue.toLowerCase();
    if (str && str.length > 0 && parentNode) {
        let headPos = 0;
        while (headPos < str.length) {
            let tailPos = headPos;
            while (tailPos < str.length && !mdx_isAlpha(str.charAt(tailPos)))
                tailPos++;

            if (tailPos > headPos) {
                parentNode.insertBefore(cur_doc.createTextNode(nodeValue.substr(headPos, tailPos - headPos)), node);
                headPos = tailPos;
            }

            while (tailPos < str.length && mdx_isAlpha(str.charAt(tailPos)))
                tailPos++;

            if (tailPos > headPos) {
                const word = nodeValue.substr(headPos, tailPos - headPos);
                const link = cur_doc.createElement('MDICT');
                link.className = 'mdx_word';
                // link.setAttribute('onclick', null);
                link.appendChild(cur_doc.createTextNode(word));
                parentNode.insertBefore(link, node);
                headPos = tailPos;
            }
        }

        parentNode.removeChild(node);
    }
}

function mdx_walk(cur_doc: Document, node: Node): void {
    if (node.nodeType === Node.TEXT_NODE) {
        if (node.parentNode?.nodeName && 
            (node.parentNode.nodeName === 'HEAD' || 
             node.parentNode.nodeName === 'SCRIPT' || 
             node.parentNode.nodeName === 'STYLE'))
            return;
        if ((node as any).visited || (node as any).className === 'mdx_word')
            return;
        (node as any).visited = true;
    }

    if (!node.childNodes || node.childNodes.length === 0) {
        if (node.nodeType === Node.TEXT_NODE) mdx_processNode(cur_doc, node as Text);
    } else if ((node as HTMLElement).onclick) {
        // Skip nodes with onclick handlers
    } else {
        let currentNode: ChildNode | null = node.firstChild;
        while (currentNode) {
            const nextSibling = currentNode.nextSibling;
            const anchor = currentNode as HTMLAnchorElement;
            if (!(currentNode.nodeName && currentNode.nodeName === 'A' && anchor.href && anchor.href !== ''))
                mdx_walk(cur_doc, currentNode);
            currentNode = nextSibling;
        }
    }
}

// @ts-ignore - Reserved for future use
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function _GetHighlightedElement(cur_win: Window): Node | null {
    const sel = cur_win.getSelection();
    if (sel && sel.rangeCount > 0)
        return sel.getRangeAt(0).startContainer;
    else
        return null;
}

function HasSelectionContent(cur_win: Window): boolean {
    const a = cur_win.getSelection();
    return !!(a && !a.isCollapsed);
}

// Call by App to lookup selected content
// @ts-ignore - Reserved for future use
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function _LookupSelection (cur_doc:Document, cur_win: Window, x: number, y: number): string {
    console.debug("Has valid window, try get selection");
    const sel = cur_win.getSelection();
    const txt = sel?.toString() || "";
    console.debug("Get selection:" + txt);
    if (sel && txt) {
        let posX: number | undefined;
        let posY: number | undefined;
        if (x && y) {
            posX = x;
            posY = y;
        } else {
            if (sel.rangeCount > 0) {
                const rect = sel.getRangeAt(0).getBoundingClientRect();
                posX = rect.left + rect.width / 2;
                posY = rect.top + rect.height / 2;
                console.debug("X:" + posX + " Y:" + posY);
            }
        }
        if (posX !== undefined && posY !== undefined){
            mdx_click(txt, CalTouchX(cur_win, posX), CalTouchY(cur_win, posY), cur_doc, cur_win);
        }
    }
    return txt;
};

function shouldSkipCurrentElement(cur_win: Window, obj: HTMLElement | null): boolean {
    for (; obj != null; obj = obj.parentNode as HTMLElement | null) {
        if ((obj.tagName === 'a' || obj.tagName === 'A') && (obj as HTMLAnchorElement).href) {
            return true;
        }
        if ((obj as any).name && (obj as any).name.indexOf('__mdx_name') === 0)
            return true;

        if (obj.onclick != null) {
            return true;
        }

        if (typeof (cur_win as any).jQuery !== 'undefined') {
            if (typeof (cur_win as any).$ !== 'undefined' && (cur_win as any).$._data !== 'undefined') {
                const tempE = (cur_win as any).$._data((cur_win as any).$(obj)[0], "events");
                if (tempE && tempE["click"]) {
                    console.debug("Skip element for jQuery event");
                    return true;
                }
            }
        }
    }   
    return false;
}

function CalTouchY(cur_win: Window, posY: number): number {
    if ((cur_win as any).iframe_id) {
        const iframe = cur_win.parent.document.getElementById((cur_win as any).iframe_id) as HTMLIFrameElement;
        if (iframe) {
            const clientRect = iframe.getBoundingClientRect();
            const top = clientRect.top;
            return posY + top;
        }
    }
    return posY;
}

function CalTouchX(cur_win: Window, posX: number): number {
    if ((cur_win as any).iframe_id) {
        const iframe = cur_win.parent.document.getElementById((cur_win as any).iframe_id) as HTMLIFrameElement;
        if (iframe) {
            const clientRect = iframe.getBoundingClientRect();
            const left = clientRect.left;
            return posX + left;
        }
    }
    return posX;
}

function OnTouchWord(event: Event, wordNode: HTMLElement, posX: number, posY: number, cur_doc:Document, cur_win: Window): boolean {
    if (shouldSkipCurrentElement(cur_win, wordNode)) {
        return false;
    }
    let context = getContext(cur_win);
    // @ts-ignore - Reserved for future use
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    (event as any).cancelBubble = true;
    event.preventDefault();

    const word = wordNode.innerText;
    console.debug("Touch word:" + word + ", X=" + posX + ", Y=" + posY);

    context.TouchTimerID = cur_win.setTimeout(function () { 
        context.TouchTimerID = 0; 
        mdx_click(word, CalTouchX(cur_win, posX), CalTouchY(cur_win, posY), cur_doc, cur_win); 
    }, 250);
    return true;
}

function OnClickImpl(event: MouseEvent, cur_doc: Document, cur_win: Window): boolean {
    if (!HasSelectionContent(cur_win) && !clickHandler(event, cur_doc, cur_win)) {
        const clientX = event.clientX;
        const clientY = event.clientY;
        const element = cur_doc.elementFromPoint(clientX, clientY);
        if (element != null) {
            console.debug("Check target element:", element);
            console.debug("Element class name:", element.className);
            if (element.className != null && element.className === 'mdx_word') {
                // var Word=WordNode.innerHTML.replace('/<.+?>/gim','');
                return OnTouchWord(event, element as HTMLElement, clientX, clientY, cur_doc, cur_win);
            } else {
                mdx_walk(cur_doc, element);
                const NewElement = cur_doc.elementFromPoint(clientX, clientY);
                if (NewElement != null && NewElement.className != null && NewElement.className === 'mdx_word')
                    return OnTouchWord(event, NewElement as HTMLElement, clientX, clientY, cur_doc, cur_win);
                else
                    return handleNoneMdxEle(event.target as HTMLElement, event, cur_doc, cur_win);
            }
        }
    }
    return false;
}

function handleNoneMdxEle(o: HTMLElement, _event: Event, _cur_doc: Document, _cur_win: Window): boolean {
    // Show big picture
    console.debug("Handle none mdx element:" + o.nodeName.toLowerCase());
    if (o.nodeName.toLowerCase() === 'img') {
        try {
            const img = o as HTMLImageElement;
            if ((typeof (img.onmousedown) === 'undefined' || img.onmousedown === null)
                && (typeof (img.onclick) === 'undefined' || img.onclick === null)) {
                if (img.clientWidth > 120 && img.clientHeight > 120){
                    //showBigMddPicture?.(img.src);
                    //TODO: should show big picture in iframe
                    return true;
                }
            }
        } catch (e: any) {
            console.warn(e.message);
        }
    }
    return false;
}

function wordPickerSetup(iframe: HTMLIFrameElement): void {
    let wordPickerContext: WordPickerContext = {
        touchStartPoint: null,
        TouchTimerID: 0,
        lastTouchUpTimeStamp: 0
    };
    (iframe.contentWindow as any).wordPickerContext = wordPickerContext;

    let cur_doc = iframe.contentDocument as Document;
    let cur_win = iframe.contentWindow as Window;

    const clickCheck = function (event: MouseEvent) {
        console.debug("Got click:" , event);
        OnClickImpl(event, cur_doc, cur_win);
    };

    iframe.contentDocument?.addEventListener('mouseup', () => {
        // 获取当前选中的文本
        const selection = cur_win.getSelection();
        if (selection){
            const selectedText = selection?.toString().trim();
            if (selectedText?.trim()) {
                const range = selection.getRangeAt(0); // 获取第一个范围
                const rect = range.getBoundingClientRect(); // 获取边界框 
                const global=getIframeGlobals(cur_win);
                console.debug("Selected text:" + selectedText + ", X:" + rect.left + ", Y:" + rect.top);
                const message: any=
                {
                    type: 'has_selection',
                    word: selectedText.trim(), 
                    anchor: {x:rect.left, y:rect.top}, 
                    fromEntry: {profile_id:global.profileId, entry_no:global.entryNo }
                };
                cur_win.parent.postMessage( message, "*");
            }
        }
    });
    iframe.contentDocument?.body.addEventListener('click', clickCheck, false);
    console.debug("Touch event listener setuped");
}

export {
    wordPickerSetup
}