Ticket #6359: IESelection.js

File IESelection.js, 3.0 KB (added by Jude Allred, 13 years ago)

the moveToPoint workaround

Line 
1(function() {
2        var rangeOnMouseDown = null; // Stores the cursor position when the user first clicked.
3
4        function getRangeAtPoint(px, py) {
5                var range = ck.document.$.body.createTextRange();
6                try {
7                        range.moveToPoint(px, py);
8                } catch (e) {
9                        //If moveToPoint fails due to an IE bug ('Unknown Error'), just go to the end of the current range.
10                        range.collapse(false);
11                }
12                return range;
13        }
14
15        function getRangeAtEndOfDoc() {
16                var range = ck.document.$.body.createTextRange();
17                range.expand('textedit');
18                range.collapse(false); // false == move to end
19                return range;
20        }
21
22        function getRangeAtBeginOfDoc() {
23                var range = ck.document.$.body.createTextRange();
24                range.expand('textedit');
25                range.collapse(true); // true == move to start.
26                return range;
27        }
28
29        var fxnSelectFromMouseToEndOfDoc = function(event) {
30                event.preventDefault();
31                if (event.button !== 1) {
32                        // The mouse button has been released, but we didn't get a mouseup event, so lets unbind.
33                        jDocHtml.unbind('mousemove.ieselectionfix');
34                        return;
35                }
36
37                try {
38                        var rangeCurrent = getRangeAtPoint(event.pageX, event.pageY);
39                        // If the range has no parent element, the mouse is probably between paragraphs.
40                        // IE will move the range to the beginning of the document. This creates lots of
41                        // obnoxious flashing when selecting across paragraphs, so don't do anything when
42                        // this happens.
43                        if (!rangeCurrent.parentElement()) {
44                                return;
45                        }
46
47                        var rangeToSelect = ck.document.$.body.createTextRange();
48                        if (rangeCurrent.compareEndPoints('StartToStart', rangeOnMouseDown) > 0) {
49                                // rangeCurrent is at or right of rangeOnMouseDown.
50                                rangeToSelect.setEndPoint('StartToStart', rangeOnMouseDown);
51                                rangeToSelect.setEndPoint('EndToEnd', rangeCurrent);
52                        } else {
53                                // rangeCurrent is left of rangeOnMouseDown.
54                                rangeToSelect.setEndPoint('StartToStart', rangeCurrent);
55                                rangeToSelect.setEndPoint('EndToEnd', rangeOnMouseDown);
56                        }
57                        rangeToSelect.select();
58                } catch (exception) {
59                        ;
60                }
61        };
62
63        var jDocHtml = $('html', ck.document.$);
64        jDocHtml.mousedown(function(event) {
65                // We only want to move the cursor if nothing inside the document was clicked
66                if (event.target == this) {
67                        //alert(event.target.tagName);
68                        try {
69                                rangeOnMouseDown = getRangeAtPoint(event.pageX, event.pageY);
70                                rangeOnMouseDown.select();
71                                jDocHtml.bind('mousemove.ieselectionfix', fxnSelectFromMouseToEndOfDoc);
72                        } catch (exc) {
73                                //alert('error');
74                                jDocHtml.unbind('mousemove.ieselectionfix');
75                        }
76                        event.preventDefault();
77                }
78        });
79
80        jDocHtml.mouseup(function(event) {
81                jDocHtml.unbind('mousemove.ieselectionfix');
82        });
83
84        // When the user double-clicks, select the word underneath their cursor.
85        jDocHtml.dblclick(function(event) {
86                if (event.target == this) {
87                        try {
88                                var range = getRangeAtPoint(event.pageX, event.pageY);
89                                range.expand('word');
90                                range.select();
91                        } catch (exc) {
92                                ;
93                        }
94                }
95        });
96} ());
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy