Changeset 530

Show
Ignore:
Timestamp:
08/30/08 21:52:54 (3 months ago)
Author:
jf.hovinne
Message:

Removed SAPI, planned for 0.6

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/wymeditor/jquery.wymeditor.explorer.js

    r485 r530  
    7676      this._wym.bindEvents(); 
    7777       
    78       // NOTE v.mische this part will be changed on event-hacking 
    79       jQuery(this._doc).bind("keydown", this.handleKeydown); 
    80        
    8178      //post-init functions 
    8279      if(jQuery.isFunction(this._options.postInit)) this._options.postInit(this); 
     
    150147}; 
    151148 
    152 WYMeditor.WymClassExplorer.prototype.handleKeydown = function(evt) { 
    153    
    154   //'this' is the doc 
    155   var wym = WYMeditor.INSTANCES[this.title]; 
    156    
    157   // "start" Selection API 
    158   var sel = wym.selection.getSelection(); 
    159  
    160 /* 
    161     // some small tests for the Selection API 
    162     var containers = WYMeditor.MAIN_CONTAINERS.join(","); 
    163     if (sel.isAtStart(containers)) 
    164         alert("isAtStart: "+sel.startNode.parentNode.nodeName); 
    165     if (sel.isAtEnd(containers)) 
    166         alert("isAtEnd: "+sel.endNode.parentNode.nodeName); 
    167     if (evt.keyCode==WYMeditor.KEY.DELETE) { 
    168         // if deleteIfExpanded wouldn't work, no selected text would be 
    169         // deleted if you press del-key 
    170         if (sel.deleteIfExpanded()) 
    171             return false; 
    172     } 
    173     if (evt.keyCode==WYMeditor.KEY.HOME) { 
    174         // if cursorToStart won't work, the cursor won't be set to start 
    175         // if you press home-key 
    176         sel.cursorToStart(sel.container); 
    177         return false; 
    178     } 
    179     if (evt.keyCode==WYMeditor.KEY.END) 
    180     { 
    181         // if cursorToEnd won't work, the cursor won't be set to the end 
    182         // if you press end-key 
    183         sel.cursorToEnd(sel.container); 
    184         return false; 
    185     } 
    186 */ 
    187 }; 
    188  
    189 /********** SELECTION API **********/ 
    190 /* 
    191 Class: WymSelExplorer 
    192     Implementation of the Selection API for Microsoft Internet Explorer 
    193  
    194 Properties: 
    195     original    - (object) Selection Object 
    196     isCollapsed - (boolean) True if selection is collapsed to single position 
    197     startNode   - (DOM node) Node where the selection starts 
    198     startOffset - (integer) Offset of the position in startNode 
    199     endNode     - (DOM node) Node where the selection ends 
    200     endOffset   - (integer) Offset of the position in endNode 
    201     container   - (DOM node) Node which includes the whole selection 
    202  
    203 Example: 
    204     "|" marks the start and the end of the selection. 
    205     (start code) 
    206     <p>A <b>sm|all</b> examp|le.</p> 
    207     (end code) 
    208  
    209     isCollapsed - false 
    210     startNode   - "small" 
    211     startOffset - 2 
    212     endNode     - " example" 
    213     endOffset   - 6 
    214     container   - <p>...</p> 
    215 */ 
    216 WYMeditor.WymSelExplorer = function(wym) { 
    217     this._wym = wym; 
    218 }; 
    219  
    220 WYMeditor.WymSelExplorer.prototype = { 
    221     /* 
    222     Property: getSelection 
    223         Return a Selection API object 
    224  
    225     Example: 
    226         (start code) 
    227         var sel = wym.selection.getSelection(); 
    228         (end code) 
    229  
    230     Returns: 
    231         (object) Selection API object 
    232     */ 
    233     getSelection: function() { 
    234         var sel = this._wym._iframe.contentWindow.document.selection; 
    235         var range = sel.createRange(); 
    236  
    237         this.original = sel; 
    238  
    239         if (sel.type == "None") 
    240             this.isCollapsed = true; 
    241         else 
    242             this.isCollapsed = false; 
    243  
    244         // get start of the selection (resp. cursor position if collapsed) 
    245         var selStart = this._getNodeAndOffset(range.duplicate(), true); 
    246         this.startNode = selStart.node; 
    247         this.startOffset = selStart.offset; 
    248  
    249         if (this.isCollapsed) { 
    250             this.endNode = this.startNode; 
    251             this.endOffset = this.startOffset; 
    252         } 
    253         else { 
    254             var selEnd = this._getNodeAndOffset(range.duplicate(), false); 
    255             this.endNode = selEnd.node; 
    256             this.endOffset = selEnd.offset; 
    257         } 
    258  
    259         this.container = jQuery(range.parentElement()).parentsOrSelf( 
    260                 WYMeditor.MAIN_CONTAINERS.join(","))[0]; 
    261  
    262         return this; 
    263     }, 
    264  
    265  
    266     _getNodeAndOffset: function(range, collapseToStart) { 
    267         var parentElement = range.parentElement(); 
    268  
    269         // range from the beginning of parentElement to cursor position 
    270         var parentRange = range.duplicate(); 
    271  
    272         // length of parentElement's text 
    273         var parentLength; 
    274  
    275         // offset from the beginning of parentElements to the cursor position 
    276         var parentOffset = 0; 
    277  
    278         // offset of the position to its node 
    279         var offset = 0; 
    280  
    281         // collapse to start or end 
    282         range.collapse(collapseToStart); 
    283         // select parent node and set range from the beginning of that node to 
    284         // the cursor position 
    285         parentRange.moveToElementText(parentElement); 
    286         parentLength = parentRange.text.length; 
    287         // XXX v.mische fix it 
    288         var backwardRange = parentRange.duplicate(); 
    289         var searchRange = parentRange.duplicate(); 
    290  
    291  
    292         parentRange.setEndPoint("EndToStart", range); 
    293         parentOffset = parentRange.text.length; 
    294  
    295         // direction to search 
    296         //  1 = forward 
    297         // -1 = backward 
    298         var direction = 1; 
    299  
    300         // where to start to find the position 
    301         var childPosition = 0; 
    302  
    303         // ratio of the cursor position to the total parentElement text length 
    304         var offsetRatio = parentOffset/parentLength; 
    305  
    306         // offset from start (if search direction==-1: end) to the child node 
    307         // we are currently at while searching the position 
    308         // finally: position of the cursor within its node 
    309         var offset = 0; 
    310  
    311         var childNodes = parentElement.childNodes; 
    312  
    313         // try to find a better start position for searching 
    314         //if (childNodes.length>20 && offsetRatio>0.1) { 
    315         if (childNodes.length>20 && offsetRatio>0.1) { 
    316             // range around the appropriate node  
    317             var childRange = searchRange.duplicate(); 
    318             // length from start to the beginning of a node near the node 
    319             // of the position we are looking for 
    320             //var childOffsetRange = parentRange.duplicate(); 
    321  
    322             childPosition = Math.round(offsetRatio * childNodes.length-1); 
    323  
    324             if (childPosition <= 0) 
    325                 childPosition = 1; 
    326  
    327             // moveToElementText doesn't work with text nodes 
    328             while ((childNodes[childPosition].nodeType == WYMeditor.NODE.TEXT 
    329                     || childNodes[childPosition].nodeName == "BR") 
    330                     && childPosition > 0) { 
    331                 childPosition--; 
    332             } 
    333  
    334             if (childPosition > 0) { 
    335                 childRange.moveToElementText(childNodes[childPosition]); 
    336                 // Range from start of parent node to the start of the proposed 
    337                 // child node 
    338                 childRange.setEndPoint("EndToStart", searchRange); 
    339  
    340                 // search forward 
    341                 if (parentOffset > childRange.text.length) { 
    342                     direction = 1; 
    343                     searchRange.setEndPoint("StartToEnd", childRange); 
    344                     searchRange.setEndPoint("EndToEnd", parentRange); 
    345                 } 
    346                 // search backwards 
    347                 else { 
    348                     direction = -1; 
    349                     searchRange.setEndPoint("StartToEnd", parentRange); 
    350                     searchRange.setEndPoint("EndToEnd", childRange); 
    351  
    352                     // caused by ranges 
    353                     childPosition--; 
    354                 } 
    355  
    356             } 
    357             // start at the beginning 
    358             else { 
    359                 direction = 1; 
    360                 searchRange = parentRange.duplicate(); 
    361             } 
    362         } 
    363         else { 
    364             if (direction == 1) 
    365                 searchRange = parentRange.duplicate(); 
    366             else { 
    367                 searchRange = backwardRange.duplicate(); 
    368                 searchRange.setEndPoint("StartToEnd", parentRange); 
    369             } 
    370         } 
    371  
    372         var node=parentElement.childNodes[childPosition]; 
    373  
    374  
    375         var currentOffsetRange = searchRange.duplicate(); 
    376         if (direction==1) { 
    377             // text length of the current node 
    378             var nodeLength = 0; 
    379             var br = 0; 
    380  
    381             while (node) { 
    382                 if (node.nodeName == 'BR') 
    383                     br++; 
    384                 nodeLength = this._getTextLength(node); 
    385                 if (currentOffsetRange.text.length > nodeLength) { 
    386                     currentOffsetRange.moveStart('character', nodeLength + br); 
    387                     br = 0; 
    388                 } 
    389                 else 
    390                     break; 
    391  
    392                 node = node.nextSibling; 
    393             } 
    394             offset = currentOffsetRange.text.length; 
    395         } 
    396         // go backwards 
    397         else { 
    398             var nodeLength = 0; 
    399             var br = 0; 
    400  
    401             while (node) { 
    402                 if (node.nodeName == 'BR') 
    403                     br++; 
    404                 nodeLength = this._getTextLength(node); 
    405  
    406                 if (currentOffsetRange.text.length > nodeLength) { 
    407                     var length = currentOffsetRange.text.length; 
    408                     currentOffsetRange.moveEnd('character', -(nodeLength+br)); 
    409                     // NOTE v.mische Sometimes the range isn't move as far as 
    410                     // expected. I don't know whenwhy it happens, but it does 
    411                     if (br == 0) { 
    412                         var diff = (length-nodeLength) - currentOffsetRange.text.length; 
    413                         currentOffsetRange.moveEnd('character', diff); 
    414                     } 
    415                     br = 0; 
    416                 } 
    417                 else 
    418                     break; 
    419  
    420                 if (node.previousSibling) 
    421                     node = node.previousSibling; 
    422                 else 
    423                     break; 
    424             } 
    425  
    426             offset = nodeLength - currentOffsetRange.text.length; 
    427         } 
    428  
    429         return {'node': node, 'offset': offset}; 
    430     }, 
    431  
    432     /* 
    433     Property: _getTextLength 
    434         Get the length of the text of a DOM node. 
    435  
    436     Arguments: 
    437         node - (DOM node) [required] The node you what to know the length of. 
    438  
    439     Example: 
    440         (start code) 
    441         var node = document.createTextNode("foo"); 
    442         _getTextLength(node) // returns 3 
    443         (end code) 
    444  
    445     Returns: 
    446         (int) Length of a DOM node. 
    447     */ 
    448     _getTextLength: function(node) { 
    449         if (node.nodeType == WYMeditor.NODE.ELEMENT) 
    450             return node.innerText.length; 
    451         else if (node.nodeType == WYMeditor.NODE.TEXT) 
    452             return node.data.length; 
    453     }, 
    454  
    455     /* 
    456     Property: deleteIfExpanded 
    457         Delete contents of a selection 
    458  
    459     Example: 
    460         (start code) 
    461         var sel = wym.selection.getSelection(); 
    462         sel.deleteIfExpanded(); 
    463         (end code) 
    464  
    465     Returns: 
    466         (boolean) True if it was expanded and thus deleted 
    467     */ 
    468     deleteIfExpanded: function() { 
    469         if(!this.isCollapsed) { 
    470             this.original.clear(); 
    471             return true; 
    472         } 
    473         return false; 
    474     } 
    475 }; 
    476  
  • trunk/src/wymeditor/jquery.wymeditor.js

    r529 r530  
    104104*/ 
    105105 
    106     VERSION             : "0.5-a1", 
     106    VERSION             : "0.5-a2", 
    107107    INSTANCES           : [], 
    108108    STRINGS             : [], 
     
    270270        this._options.jQueryPath = this._options.jQueryPath 
    271271        || this.computeJqueryPath(); 
    272  
    273         //instanciate a WYMeditor Selection API class 
    274         this.selection = new WYMeditor.WymSelection(); 
    275272 
    276273        //initialize the editor instance 
     
    688685  if (jQuery.browser.msie) { 
    689686    var WymClass = new WYMeditor.WymClassExplorer(this); 
    690     var WymSel   = new WYMeditor.WymSelExplorer(this); 
    691687  } 
    692688  else if (jQuery.browser.mozilla) { 
    693689    var WymClass = new WYMeditor.WymClassMozilla(this); 
    694     var WymSel   = new WYMeditor.WymSelMozilla(this); 
    695690  } 
    696691  else if (jQuery.browser.opera) { 
    697692    var WymClass = new WYMeditor.WymClassOpera(this); 
    698     var WymSel   = new WYMeditor.WymSelOpera(this); 
    699693  } 
    700694  else if (jQuery.browser.safari) { 
    701695    var WymClass = new WYMeditor.WymClassSafari(this); 
    702     var WymSel   = new WYMeditor.WymSelSafari(this); 
    703696  } 
    704697   
     
    810803      this.loadSkin(); 
    811804       
    812     } 
    813      
    814     if(WymSel) { 
    815      
    816       //extend the selection object 
    817       //don't use jQuery.extend since 1.1.4 
    818       //jQuery.extend(this.selection, WymSel); 
    819       for (prop in WymSel) { this.selection[prop] = WymSel[prop]; } 
    820805    } 
    821806}; 
     
    15211506    wym._options.postInitDialog(wym,window); 
    15221507 
    1523 }; 
    1524  
    1525  
    1526  
    1527 /********** SELECTION API **********/ 
    1528  
    1529 WYMeditor.WymSelection = function() { 
    1530     this.test = "test from WymSelection"; 
    1531 }; 
    1532  
    1533  
    1534 WYMeditor.WymSelection.prototype = { 
    1535     /* The following properties are set in the browser specific file (in 
    1536      * getSelection()): 
    1537      * this.original 
    1538      * this.startNode 
    1539      * this.endNode 
    1540      * this.startOffset 
    1541      * this.endOffset 
    1542      * this.iscollapsed 
    1543      * this.container 
    1544      */ 
    1545  
    1546     /* The following methods are implemented in browser specific file: 
    1547      *  - deleteIfExpanded() 
    1548      *  - cursorToStart() 
    1549      *  - cursorToEnd() 
    1550      */ 
    1551  
    1552  
    1553     isAtStart: function(jqexpr) { 
    1554         var parent = jQuery(this.startNode).parentsOrSelf(jqexpr); 
    1555  
    1556         // jqexpr isn't a parent of the current cursor position 
    1557         if (parent.length==0) 
    1558             return false; 
    1559  
    1560         var startNode = this.startNode; 
    1561         if (startNode.nodeType == WYMeditor.NODE.TEXT) { 
    1562             // 1. startNode ist first child 
    1563             // 2. offset needs to be 0 to be at the start (or the previous 
    1564             //    characters are whitespaces) 
    1565             if ((startNode.previousSibling 
    1566                     && !WYMeditor.isPhantomNode(startNode.previousSibling)) 
    1567                         || (this.startOffset != 0 && !WYMeditor.isPhantomString( 
    1568                             startNode.data.substring(0, this.startOffset)))) 
    1569                 return false; 
    1570             else 
    1571                 startNode = startNode.parentNode; 
    1572         } 
    1573         // cursor can be at the start of a text node and have a startOffset > 0 
    1574         // (if the node contains trailign whitespaces) 
    1575         else if (this.startOffset != 0) 
    1576             return false; 
    1577  
    1578  
    1579         for (var n=jQuery(startNode); n[0]!=parent[0]; n=n.parent()) { 
    1580             var firstChild = n.parent().children(':first'); 
    1581  
    1582             // node isn't first child => cursor can't be at the beginning 
    1583             if (firstChild[0] != n[0] 
    1584                     || (firstChild[0].previousSibling 
    1585                         && !WYMeditor.isPhantomNode(firstChild[0].previousSibling))) 
    1586                 return false; 
    1587         } 
    1588  
    1589         return true; 
    1590     }, 
    1591  
    1592     isAtEnd: function(jqexpr) { 
    1593         var parent = jQuery(this.endNode).parentsOrSelf(jqexpr); 
    1594  
    1595         // jqexpr isn't a parent of the current cursor position 
    1596         if (parent.length==0) 
    1597             return false; 
    1598         else 
    1599             parent = parent[0]; 
    1600  
    1601  
    1602         // This is the case if, e.g ("|" = cursor): <p>textnode|<br/></p>, 
    1603         // there the offset of endNode (endOffset) is 1 (behind the first node 
    1604         // of <p>) 
    1605         if (this.endNode == parent) { 
    1606             // NOTE I don't know if it is a good idea to delete the <br> 
    1607             // here, as "atEnd()" probably shouldn't change the dom tree, 
    1608             // but only searching it 
    1609             if (this.endNode.lastChild.nodeName == "BR") 
    1610                 this.endNode.removeChild(endNode.lastChild); 
    1611  
    1612             // if cursor is really at the end 
    1613             if (this.endOffset == 0) 
    1614                 return false; 
    1615             else { 
    1616                 for (var nNext=this.endNode.childNodes[this.endOffset-1].nextSibling; 
    1617                         nNext==null || nNext.nodeName == "BR"; 
    1618                         nNext=nNext.nextSibling) 
    1619  
    1620                 if (nNext==null) 
    1621                     return true; 
    1622             } 
    1623  
    1624         } 
    1625         else { 
    1626             var endNode = this.endNode; 
    1627             if (endNode.nodeType == WYMeditor.NODE.TEXT) { 
    1628                 if ((endNode.nextSibling 
    1629                         && !WYMeditor.isPhantomNode(endNode.nextSibling)) 
    1630                             || (this.endOffset != endNode.data.length)) 
    1631                     return false; 
    1632                 else 
    1633                     endNode = endNode.parentNode; 
    1634             } 
    1635  
    1636             for (var n=endNode; n!=parent; n=n.parentNode) { 
    1637                 var lastChild = n.parentNode.lastChild; 
    1638                 // node isn't last child => cursor can't be at the end 
    1639                 // (is this true?) in gecko there the last child could be a 
    1640                 //     phantom node 
    1641  
    1642                 // sometimes also whitespacenodes which aren't phatom nodes 
    1643                 // get stripped, but this is ok, as this is a wysiwym editor 
    1644                 if ((lastChild != n) || 
    1645                         (WYMeditor.isPhantomNode(lastChild) 
    1646                         && lastChild.previousSibling != n)) { 
    1647                     return false; 
    1648                 } 
    1649             } 
    1650         } 
    1651  
    1652         if (this.endOffset == this.endNode.length) 
    1653             return true; 
    1654         else 
    1655             return false; 
    1656     } 
    16571508}; 
    16581509 
  • trunk/src/wymeditor/jquery.wymeditor.mozilla.js

    r503 r530  
    166166  var wym = WYMeditor.INSTANCES[this.title]; 
    167167   
    168   // "start" Selection API 
    169   var sel = wym.selection.getSelection(); 
    170  
    171 /* 
    172     // some small tests for the Selection API 
    173     var containers = WYMeditor.MAIN_CONTAINERS.join(","); 
    174     if (sel.isAtStart(containers)) 
    175         alert("isAtStart: "+sel.startNode.parentNode.nodeName); 
    176     if (sel.isAtEnd(containers)) 
    177         alert("isAtEnd: "+sel.endNode.parentNode.nodeName); 
    178     if (evt.keyCode==WYMeditor.KEY.DELETE) { 
    179         // if deleteIfExpanded wouldn't work, no selected text would be 
    180         // deleted if you press del-key 
    181         if (sel.deleteIfExpanded()) 
    182             return false; 
    183     } 
    184     if (evt.keyCode==WYMeditor.KEY.HOME) { 
    185         // if cursorToStart won't work, the cursor won't be set to start 
    186         // if you press home-key 
    187         sel.cursorToStart(sel.container); 
    188         return false; 
    189     } 
    190     if (evt.keyCode==WYMeditor.KEY.END) 
    191     { 
    192         // if cursorToEnd won't work, the cursor won't be set to the end 
    193         // if you press end-key 
    194         sel.cursorToEnd(sel.container); 
    195         return false; 
    196     } 
    197 */ 
    198    
    199168  if(evt.ctrlKey){ 
    200169    if(evt.keyCode == 66){ 
     
    308277}; 
    309278 
    310 /********** SELECTION API **********/ 
    311  
    312 WYMeditor.WymSelMozilla = function(wym) { 
    313     this._wym = wym; 
    314 }; 
    315  
    316 WYMeditor.WymSelMozilla.prototype = { 
    317     getSelection: function() { 
    318         var _sel = this._wym._iframe.contentWindow.getSelection(); 
    319         // NOTE v.mische can startNode/endNote be phantom nodes? 
    320         this.startNode = _sel.getRangeAt(0).startContainer; 
    321         this.endNode = _sel.getRangeAt(0).endContainer; 
    322         this.startOffset = _sel.getRangeAt(0).startOffset; 
    323         this.endOffset = _sel.getRangeAt(0).endOffset; 
    324         this.isCollapsed = _sel.isCollapsed; 
    325         this.original = _sel; 
    326         this.container = jQuery(this.startNode).parentsOrSelf( 
    327                 WYMeditor.MAIN_CONTAINERS.join(","))[0]; 
    328  
    329         return this; 
    330     }, 
    331  
    332     cursorToStart: function(jqexpr) { 
    333         if (jqexpr.nodeType == WYMeditor.NODE.TEXT) 
    334             jqexpr = jqexpr.parentNode; 
    335  
    336         var firstTextNode = jQuery(jqexpr)[0]; 
    337  
    338         while (firstTextNode.nodeType!=WYMeditor.NODE.TEXT) { 
    339             if (!firstTextNode.hasChildNodes()) 
    340                 break; 
    341             firstTextNode = firstTextNode.firstChild; 
    342         } 
    343  
    344         if (WYMeditor.isPhantomNode(firstTextNode)) 
    345             firstTextNode = firstTextNode.nextSibling; 
    346  
    347         // e.g. an <img/> 
    348         if (firstTextNode.nodeType == WYMeditor.NODE.ELEMENT) 
    349             this.original.collapse(firstTextNode.parentNode, 0); 
    350         else 
    351             this.original.collapse(firstTextNode, 0); 
    352     }, 
    353  
    354     cursorToEnd: function(jqexpr) { 
    355         if (jqexpr.nodeType == WYMeditor.NODE.TEXT) 
    356             jqexpr = jqexpr.parentNode; 
    357  
    358         var lastTextNode = jQuery(jqexpr)[0]; 
    359  
    360         while (lastTextNode.nodeType!=WYMeditor.NODE.TEXT) { 
    361             if (!lastTextNode.hasChildNodes()) 
    362                 break; 
    363             lastTextNode = lastTextNode.lastChild; 
    364         } 
    365  
    366         if (WYMeditor.isPhantomNode(lastTextNode)) 
    367             lastTextNode = lastTextNode.previousSibling; 
    368  
    369         // e.g. an <img/> 
    370         if (lastTextNode.nodeType == WYMeditor.NODE.ELEMENT) 
    371             this.original.collapse(lastTextNode.parentNode, 
    372                 lastTextNode.parentNode.childNodes.length); 
    373         else 
    374             this.original.collapse(lastTextNode, lastTextNode.length); 
    375     }, 
    376  
    377     deleteIfExpanded: function() { 
    378         if(!this.original.isCollapsed) { 
    379             this.original.deleteFromDocument(); 
    380             return true; 
    381         } 
    382         return false; 
    383     } 
    384 }; 
    385  
    386  
  • trunk/src/wymeditor/jquery.wymeditor.opera.js

    r485 r530  
    107107  //'this' is the doc 
    108108  var wym = WYMeditor.INSTANCES[this.title]; 
    109    
    110   // "start" Selection API 
    111   var sel = wym.selection.getSelection(); 
    112  
    113 /* 
    114     // some small tests for the Selection API 
    115     var containers = WYMeditor.MAIN_CONTAINERS.join(","); 
    116     if (sel.isAtStart(containers)) 
    117         alert("isAtStart: "+sel.startNode.parentNode.nodeName); 
    118     if (sel.isAtEnd(containers)) 
    119         alert("isAtEnd: "+sel.endNode.parentNode.nodeName); 
    120     if (evt.keyCode==WYMeditor.KEY.DELETE) { 
    121         // if deleteIfExpanded wouldn't work, no selected text would be 
    122         // deleted if you press del-key 
    123         if (sel.deleteIfExpanded()) 
    124             return false; 
    125     } 
    126     if (evt.keyCode==WYMeditor.KEY.HOME) { 
    127         // if cursorToStart won't work, the cursor won't be set to start 
    128         // if you press home-key 
    129         sel.cursorToStart(sel.container); 
    130         return false; 
    131     } 
    132     if (evt.keyCode==WYMeditor.KEY.END) 
    133     { 
    134         // if cursorToEnd won't work, the cursor won't be set to the end 
    135         // if you press end-key 
    136         sel.cursorToEnd(sel.container); 
    137         return false; 
    138     } 
    139 */ 
    140  
     109  var sel = wym._iframe.contentWindow.getSelection(); 
     110  startNode = sel.getRangeAt(0).startContainer; 
    141111 
    142112  //Get a P instead of no container 
    143   if(!sel.container 
     113  if(!jQuery(startNode).parentsOrSelf( 
     114                WYMeditor.MAIN_CONTAINERS.join(","))[0] 
    144115      && evt.keyCode != WYMeditor.KEY.ENTER 
    145116      && evt.keyCode != WYMeditor.KEY.LEFT 
     
    166137}; 
    167138 
    168 /********** SELECTION API **********/ 
    169  
    170 WYMeditor.WymSelOpera = function(wym) { 
    171     this._wym = wym; 
    172 }; 
    173  
    174 WYMeditor.WymSelOpera.prototype = { 
    175     getSelection: function() { 
    176         var _sel = this._wym._iframe.contentWindow.getSelection(); 
    177         // NOTE v.mische can startNode/endNote be phantom nodes? 
    178         this.startNode = _sel.getRangeAt(0).startContainer; 
    179         this.endNode = _sel.getRangeAt(0).endContainer; 
    180         this.startOffset = _sel.getRangeAt(0).startOffset; 
    181         this.endOffset = _sel.getRangeAt(0).endOffset; 
    182         this.isCollapsed = _sel.isCollapsed; 
    183         this.original = _sel; 
    184         this.container = jQuery(this.startNode).parentsOrSelf( 
    185                 WYMeditor.MAIN_CONTAINERS.join(","))[0]; 
    186  
    187         return this; 
    188     }, 
    189  
    190     cursorToStart: function(jqexpr) { 
    191         if (jqexpr.nodeType == WYMeditor.NODE.TEXT) 
    192             jqexpr = jqexpr.parentNode; 
    193  
    194         var firstTextNode = jQuery(jqexpr)[0]; 
    195  
    196         while (firstTextNode.nodeType!=WYMeditor.NODE.TEXT) { 
    197             if (!firstTextNode.hasChildNodes()) 
    198                 break; 
    199             firstTextNode = firstTextNode.firstChild; 
    200         } 
    201  
    202         if (WYMeditor.isPhantomNode(firstTextNode)) 
    203             firstTextNode = firstTextNode.nextSibling; 
    204  
    205         // e.g. an <img/> 
    206         if (firstTextNode.nodeType == WYMeditor.NODE.ELEMENT) 
    207             this.original.collapse(firstTextNode.parentNode, 0); 
    208         else 
    209             this.original.collapse(firstTextNode, 0); 
    210     }, 
    211  
    212     cursorToEnd: function(jqexpr) { 
    213         if (jqexpr.nodeType == WYMeditor.NODE.TEXT) 
    214             jqexpr = jqexpr.parentNode; 
    215  
    216         var lastTextNode = jQuery(jqexpr)[0]; 
    217  
    218         while (lastTextNode.nodeType!=WYMeditor.NODE.TEXT) { 
    219             if (!lastTextNode.hasChildNodes()) 
    220                 break; 
    221             lastTextNode = lastTextNode.lastChild; 
    222         } 
    223  
    224         if (WYMeditor.isPhantomNode(lastTextNode)) 
    225             lastTextNode = lastTextNode.previousSibling; 
    226  
    227         // e.g. an <img/> 
    228         if (lastTextNode.nodeType == WYMeditor.NODE.ELEMENT) 
    229             this.original.collapse(lastTextNode.parentNode, 
    230                 lastTextNode.parentNode.childNodes.length); 
    231         else 
    232             this.original.collapse(lastTextNode, lastTextNode.length); 
    233     }, 
    234  
    235     deleteIfExpanded: function() { 
    236         if(!this.original.isCollapsed) { 
    237             this.original.deleteFromDocument(); 
    238             return true; 
    239         } 
    240         return false; 
    241     } 
    242 }; 
    243  
    244  
  • trunk/src/wymeditor/jquery.wymeditor.safari.js

    r518 r530  
    146146  var wym = WYMeditor.INSTANCES[this.title]; 
    147147   
    148   // "start" Selection API 
    149   //var sel = wym.selection.getSelection(); 
    150  
    151 /* 
    152     // some small tests for the Selection API 
    153     var containers = WYMeditor.MAIN_CONTAINERS.join(","); 
    154     if (sel.isAtStart(containers)) 
    155         alert("isAtStart: "+sel.startNode.parentNode.nodeName); 
    156     if (sel.isAtEnd(containers)) 
    157         alert("isAtEnd: "+sel.endNode.parentNode.nodeName); 
    158     if (evt.keyCode==WYMeditor.KEY.DELETE) { 
    159         // if deleteIfExpanded wouldn't work, no selected text would be 
    160         // deleted if you press del-key 
    161         if (sel.deleteIfExpanded()) 
    162             return false; 
    163     } 
    164     if (evt.keyCode==WYMeditor.KEY.HOME) { 
    165         // if cursorToStart won't work, the cursor won't be set to start 
    166         // if you press home-key 
    167         sel.cursorToStart(sel.container); 
    168         return false; 
    169     } 
    170     if (evt.keyCode==WYMeditor.KEY.END) 
    171     { 
    172         // if cursorToEnd won't work, the cursor won't be set to the end 
    173         // if you press end-key 
    174         sel.cursorToEnd(sel.container); 
    175         return false; 
    176     } 
    177 */ 
    178    
    179148  if(evt.ctrlKey){ 
    180149    if(evt.keyCode == 66){ 
     
    294263}; 
    295264 
    296 /********** SELECTION API **********/ 
    297  
    298 WYMeditor.WymSelSafari = function(wym) { 
    299     this._wym = wym; 
    300 }; 
    301  
    302 WYMeditor.WymSelSafari.prototype = { 
    303     getSelection: function() { 
    304         var _sel = this._wym._iframe.contentWindow.getSelection(); 
    305         // NOTE v.mische can startNode/endNote be phantom nodes? 
    306         this.startNode = _sel.getRangeAt(0).startContainer; 
    307         this.endNode = _sel.getRangeAt(0).endContainer; 
    308         this.startOffset = _sel.getRangeAt(0).startOffset; 
    309         this.endOffset = _sel.getRangeAt(0).endOffset; 
    310         this.isCollapsed = _sel.isCollapsed; 
    311         this.original = _sel; 
    312         this.container = jQuery(this.startNode).parentsOrSelf( 
    313                 WYMeditor.MAIN_CONTAINERS.join(","))[0]; 
    314  
    315         return this; 
    316     }, 
    317  
    318     cursorToStart: function(jqexpr) { 
    319         if (jqexpr.nodeType == WYMeditor.NODE.TEXT) 
    320             jqexpr = jqexpr.parentNode; 
    321  
    322         var firstTextNode = jQuery(jqexpr)[0]; 
    323  
    324         while (firstTextNode.nodeType!=WYMeditor.NODE.TEXT) { 
    325             if (!firstTextNode.hasChildNodes()) 
    326                 break; 
    327             firstTextNode = firstTextNode.firstChild; 
    328         } 
    329  
    330         if (WYMeditor.isPhantomNode(firstTextNode)) 
    331             firstTextNode = firstTextNode.nextSibling; 
    332  
    333         // e.g. an <img/> 
    334         if (firstTextNode.nodeType == WYMeditor.NODE.ELEMENT) 
    335             this.original.collapse(firstTextNode.parentNode, 0); 
    336         else 
    337             this.original.collapse(firstTextNode, 0); 
    338     }, 
    339  
    340     cursorToEnd: function(jqexpr) { 
    341         if (jqexpr.nodeType == WYMeditor.NODE.TEXT) 
    342             jqexpr = jqexpr.parentNode; 
    343  
    344         var lastTextNode = jQuery(jqexpr)[0]; 
    345  
    346         while (lastTextNode.nodeType!=WYMeditor.NODE.TEXT) { 
    347             if (!lastTextNode.hasChildNodes()) 
    348                 break; 
    349             lastTextNode = lastTextNode.lastChild; 
    350         } 
    351  
    352         if (WYMeditor.isPhantomNode(lastTextNode)) 
    353             lastTextNode = lastTextNode.previousSibling; 
    354  
    355         // e.g. an <img/> 
    356         if (lastTextNode.nodeType == WYMeditor.NODE.ELEMENT) 
    357             this.original.collapse(lastTextNode.parentNode, 
    358                 lastTextNode.parentNode.childNodes.length); 
    359         else 
    360             this.original.collapse(lastTextNode, lastTextNode.length); 
    361     }, 
    362  
    363     deleteIfExpanded: function() { 
    364         if(!this.original.isCollapsed) { 
    365             this.original.deleteFromDocument(); 
    366             return true; 
    367         } 
    368         return false; 
    369     } 
    370 }; 
    371  
    372