Changeset 28170

Show
Ignore:
Timestamp:
01/08/09 21:28:21 (6 months ago)
Author:
suVene
Message:

resizable current textarea using a keymap

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/vimperator-plugins/trunk/resizable_textarea.js

    r28169 r28170  
    1212  <description lang="ja">テキストエリアをリサイズ可能にする。</description> 
    1313  <author mail="suvene@zeromemory.info" homepage="http://zeromemory.sblo.jp/">suVene</author> 
    14   <version>0.1.0</version> 
     14  <version>0.2.0</version> 
    1515  <license>MIT</license> 
    1616  <minVersion>2.0pre</minVersion> 
    1717  <maxVersion>2.0α2</maxVersion> 
    18   <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/resizeble_textarea.js</updateURL> 
     18  <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/resizable_textarea.js</updateURL> 
    1919  <detail><![CDATA[ 
    2020== Usage == 
     21=== NORMAL MODE or VISUAL MODE or CARET MODE === 
    2122>|| 
    22 :textareaResize 
    23   or 
    24 :tr 
     23:textareaResize or :tr 
    2524||< 
     25you can resize textareas by using a mouse. 
     26 
     27=== INSERT MODE or TEXTAREA MODE === 
     28>|| 
     29"<A-r>" or "<M-r>" 
     30||< 
     31you can resize current component by using a keyboad. 
     32=== keymap === 
     33"k" or "up": 
     34  height more small. 
     35"j" or "down": 
     36  height more large. 
     37"h" or "left": 
     38  width more small. 
     39"l" or "right": 
     40  width more large. 
     41"escape" or "enter": 
     42  end of resize. 
    2643  ]]></detail> 
    2744</VimperatorPlugin>; 
     
    3855      initResize: this._bind(this, this.initResize), 
    3956      resize: this._bind(this, this.resize), 
    40       stopResize: this._bind(this, this.stopResize) 
     57      stopResize: this._bind(this, this.stopResize), 
     58      currentResize: this._bind(this, this.currentResize) 
    4159    }; 
    4260  }, 
    43   resizable: function(args) { 
     61  resizable: function(focused) { 
     62    if (focused) { 
     63      this.initResize({ target: focused }, focused); 
     64      return; 
     65    } 
    4466    this.changeCursor(false); 
    4567    this.doc.addEventListener("mousedown", this.handler.initResize, false); 
    4668  }, 
    47   initResize: function(event) { 
     69  initResize: function(event, focused) { 
    4870    var target = this.target = event.target; 
    4971    if (!target || 
     
    5274      return; 
    5375 
     76    if (focused) { 
     77      this.target.startBgColor = this.target.style.backgroundColor; 
     78      this.target.style.backgroundColor = "#F4BC14"; 
     79      this.doc.addEventListener("keydown", this.handler.currentResize, false); 
     80      return; 
     81    } 
     82 
    5483    this.target.startWidth = this.target.clientWidth; 
    5584    this.target.startHeight = this.target.clientHeight; 
    56     this.target.startX = event.clientX; 
    57     this.target.startY = event.clientY; 
     85    this.target.startX = event.clientX || event.target.offsetLeft; 
     86    this.target.startY = event.clientY || event.target.offsetTop; 
    5887 
    5988    this.doc.addEventListener("mousemove", this.handler.resize, false); 
     
    87116    } 
    88117  }, 
     118  currentResize: function(event) { 
     119    var nodeName = this.target.nodeName.toLowerCase(); 
     120    switch (event.keyCode) { 
     121    case KeyEvent.DOM_VK_UP: case KeyEvent.DOM_VK_K: 
     122      if (nodeName == "textarea") 
     123        this.target.style.height = this.target.clientHeight - 10 + "px"; 
     124      break; 
     125    case KeyEvent.DOM_VK_DOWN: case KeyEvent.DOM_VK_J: 
     126      if (nodeName == "textarea") 
     127        this.target.style.height = this.target.clientHeight + 15 + "px"; 
     128      break; 
     129    case KeyEvent.DOM_VK_LEFT: case KeyEvent.DOM_VK_H: 
     130      this.target.style.width = this.target.clientWidth - 10 + "px"; 
     131      break; 
     132    case KeyEvent.DOM_VK_RIGHT: case KeyEvent.DOM_VK_L: 
     133      this.target.style.width = this.target.clientWidth + 15 + "px"; 
     134      break; 
     135    case KeyEvent.DOM_VK_RETURN: case KeyEvent.DOM_VK_ENTER: case KeyEvent.DOM_VK_ESCAPE: 
     136      this.target.style.background = this.target.startBgColor; 
     137      setTimeout(function(self) { 
     138          self.doc.removeEventListener("keydown", self.handler.currentResize, false); 
     139          self.target.focus(); 
     140        }, 10, this 
     141      ); 
     142      break; 
     143    } 
     144    try{ 
     145      event.preventDefault(); 
     146    } catch (e) {} 
     147  }, 
    89148  _bind: function() { 
    90149      var obj = Array.prototype.shift.apply(arguments); 
     
    99158 
    100159commands.addUserCommand( 
    101   [ "textareaResize", "tr" ], "resizable textarea.", 
     160  [ "textareaResize", "tr" ], "Allows you to resize textareas.", 
    102161  function() { 
    103162    var instance = new TextResizer(window.content.document); 
    104     instance.resizable.apply(instance, arguments); 
     163    instance.resizable.apply(instance); 
    105164  }, 
    106165  null, 
     
    108167); 
    109168 
     169mappings.add( 
     170  [ modes.INSERT, modes.TEXTAREA ], 
     171  [ "<M-r>", "<A-r>" ], 
     172  "Allows you to resize current textarea.", 
     173  function() { 
     174    var instance = new TextResizer(window.content.document); 
     175    instance.resizable.apply(instance, [ document.commandDispatcher.focusedElement ]); 
     176  } 
     177); 
     178 
    110179})(); 
    111180// vim: set fdm=marker sw=2 ts=2 sts=0 et: