[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/webroot/rsrc/js/core/ -> behavior-oncopy.js (source)

   1  /**
   2   * @provides javelin-behavior-phabricator-oncopy
   3   * @requires javelin-behavior
   4   *           javelin-dom
   5   */
   6  
   7  /**
   8   * Tools like Paste and Differential don't normally respond to the clipboard
   9   * 'copy' operation well, because when a user copies text they'll get line
  10   * numbers and other metadata.
  11   *
  12   * To improve this behavior, applications can embed markers that delimit
  13   * metadata (left of the marker) from content (right of the marker). When
  14   * we get a copy event, we strip out all the metadata and just copy the
  15   * actual text.
  16   */
  17  JX.behavior('phabricator-oncopy', function() {
  18  
  19    var zws = '\u200B'; // Unicode Zero-Width Space
  20  
  21    JX.enableDispatch(document.body, 'copy');
  22    JX.Stratcom.listen(
  23      ['copy'],
  24      null,
  25      function(e) {
  26  
  27        var selection;
  28        var text;
  29        if (window.getSelection) {
  30          selection = window.getSelection();
  31          text = selection.toString();
  32        } else {
  33          selection = document.selection;
  34          text = selection.createRange().text;
  35        }
  36  
  37        if (text.indexOf(zws) == -1) {
  38          // If there's no marker in the text, just let it copy normally.
  39          return;
  40        }
  41  
  42        var result = [];
  43  
  44        // Strip everything before the marker (and the marker itself) out of the
  45        // text. If a line doesn't have the marker, throw it away (the assumption
  46        // is that it's a line number or part of some other meta-text).
  47        var lines = text.split('\n');
  48        var pos;
  49        for (var ii = 0; ii < lines.length; ii++) {
  50          pos = lines[ii].indexOf(zws);
  51          if (pos == -1 && ii !== 0) {
  52            continue;
  53          }
  54          result.push(lines[ii].substring(pos + 1));
  55        }
  56        result = result.join('\n');
  57  
  58        var rawEvent = e.getRawEvent();
  59        var clipboardData = 'clipboardData' in rawEvent ?
  60          rawEvent.clipboardData :
  61          window.clipboardData;
  62        clipboardData.setData('Text', result);
  63        e.prevent();
  64      });
  65  });


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1