BVB Source Codes

Squire Show Clipboard.js Source code

Return Download Squire: download Clipboard.js Source code - Download Squire Source code - Type:.js
  1. /*jshint strict:false, undef:false, unused:false */
  2.  
  3. // The (non-standard but supported enough) innerText property is based on the
  4. // render tree in Firefox and possibly other browsers, so we must insert the
  5. // DOM node into the document to ensure the text part is correct.
  6. var setClipboardData = function ( clipboardData, node, root ) {
  7.     var body = node.ownerDocument.body;
  8.     var html, text;
  9.  
  10.     // Firefox will add an extra new line for BRs at the end of block when
  11.     // calculating innerText, even though they don't actually affect display.
  12.     // So we need to remove them first.
  13.     cleanupBRs( node, root );
  14.  
  15.     node.setAttribute( 'style',
  16.         'position:fixed;overflow:hidden;bottom:100%;right:100%;' );
  17.     body.appendChild( node );
  18.     html = node.innerHTML;
  19.     text = node.innerText || node.textContent;
  20.  
  21.     // Firefox (and others?) returns unix line endings (\n) even on Windows.
  22.     // If on Windows, normalise to \r\n, since Notepad and some other crappy
  23.     // apps do not understand just \n.
  24.     if ( isWin ) {
  25.         text = text.replace( /\r?\n/g, '\r\n' );
  26.     }
  27.  
  28.     clipboardData.setData( 'text/html', html );
  29.     clipboardData.setData( 'text/plain', text );
  30.  
  31.     body.removeChild( node );
  32. };
  33.  
  34. var onCut = function ( event ) {
  35.     var clipboardData = event.clipboardData;
  36.     var range = this.getSelection();
  37.     var node = this.createElement( 'div' );
  38.     var root = this._root;
  39.     var self = this;
  40.  
  41.     // Save undo checkpoint
  42.     this.saveUndoState( range );
  43.  
  44.     // Edge only seems to support setting plain text as of 2016-03-11.
  45.     // Mobile Safari flat out doesn't work:
  46.     // https://bugs.webkit.org/show_bug.cgi?id=143776
  47.     if ( !isEdge && !isIOS && clipboardData ) {
  48.         moveRangeBoundariesUpTree( range, root );
  49.         node.appendChild( deleteContentsOfRange( range, root ) );
  50.         setClipboardData( clipboardData, node, root );
  51.         event.preventDefault();
  52.     } else {
  53.         setTimeout( function () {
  54.             try {
  55.                 // If all content removed, ensure div at start of root.
  56.                 self._ensureBottomLine();
  57.             } catch ( error ) {
  58.                 self.didError( error );
  59.             }
  60.         }, 0 );
  61.     }
  62.  
  63.     this.setSelection( range );
  64. };
  65.  
  66. var onCopy = function ( event ) {
  67.     var clipboardData = event.clipboardData;
  68.     var range = this.getSelection();
  69.     var node = this.createElement( 'div' );
  70.     var root = this._root;
  71.     var startBlock, endBlock, copyRoot, contents, parent, newContents;
  72.  
  73.     // Edge only seems to support setting plain text as of 2016-03-11.
  74.     // Mobile Safari flat out doesn't work:
  75.     // https://bugs.webkit.org/show_bug.cgi?id=143776
  76.     if ( !isEdge && !isIOS && clipboardData ) {
  77.         range = range.cloneRange();
  78.         startBlock = getStartBlockOfRange( range, root );
  79.         endBlock = getEndBlockOfRange( range, root );
  80.         copyRoot = ( ( startBlock === endBlock ) && startBlock ) || root;
  81.         moveRangeBoundariesDownTree( range );
  82.         moveRangeBoundariesUpTree( range, copyRoot );
  83.         contents = range.cloneContents();
  84.         parent = range.commonAncestorContainer;
  85.         if ( parent.nodeType === TEXT_NODE ) {
  86.             parent = parent.parentNode;
  87.         }
  88.         while ( parent && parent !== copyRoot ) {
  89.             newContents = parent.cloneNode( false );
  90.             newContents.appendChild( contents );
  91.             contents = newContents;
  92.             parent = parent.parentNode;
  93.         }
  94.         node.appendChild( contents );
  95.  
  96.         setClipboardData( clipboardData, node, root );
  97.         event.preventDefault();
  98.     }
  99. };
  100.  
  101. // Need to monitor for shift key like this, as event.shiftKey is not available
  102. // in paste event.
  103. function monitorShiftKey ( event ) {
  104.     this.isShiftDown = event.shiftKey;
  105. }
  106.  
  107. var onPaste = function ( event ) {
  108.     var clipboardData = event.clipboardData;
  109.     var items = clipboardData && clipboardData.items;
  110.     var choosePlain = this.isShiftDown;
  111.     var fireDrop = false;
  112.     var hasImage = false;
  113.     var plainItem = null;
  114.     var self = this;
  115.     var l, item, type, types, data;
  116.  
  117.     // Current HTML5 Clipboard interface
  118.     // ---------------------------------
  119.     // https://html.spec.whatwg.org/multipage/interaction.html
  120.  
  121.     // Edge only provides access to plain text as of 2016-03-11.
  122.     if ( !isEdge && items ) {
  123.         event.preventDefault();
  124.         l = items.length;
  125.         while ( l-- ) {
  126.             item = items[l];
  127.             type = item.type;
  128.             if ( !choosePlain && type === 'text/html' ) {
  129.                 /*jshint loopfunc: true */
  130.                 item.getAsString( function ( html ) {
  131.                     self.insertHTML( html, true );
  132.                 });
  133.                 /*jshint loopfunc: false */
  134.                 return;
  135.             }
  136.             if ( type === 'text/plain' ) {
  137.                 plainItem = item;
  138.             }
  139.             if ( !choosePlain && /^image\/.*/.test( type ) ) {
  140.                 hasImage = true;
  141.             }
  142.         }
  143.         // Treat image paste as a drop of an image file.
  144.         if ( hasImage ) {
  145.             this.fireEvent( 'dragover', {
  146.                 dataTransfer: clipboardData,
  147.                 /*jshint loopfunc: true */
  148.                 preventDefault: function () {
  149.                     fireDrop = true;
  150.                 }
  151.                 /*jshint loopfunc: false */
  152.             });
  153.             if ( fireDrop ) {
  154.                 this.fireEvent( 'drop', {
  155.                     dataTransfer: clipboardData
  156.                 });
  157.             }
  158.         } else if ( plainItem ) {
  159.             plainItem.getAsString( function ( text ) {
  160.                 self.insertPlainText( text, true );
  161.             });
  162.         }
  163.         return;
  164.     }
  165.  
  166.     // Old interface
  167.     // -------------
  168.  
  169.     // Safari (and indeed many other OS X apps) copies stuff as text/rtf
  170.     // rather than text/html; even from a webpage in Safari. The only way
  171.     // to get an HTML version is to fallback to letting the browser insert
  172.     // the content. Same for getting image data. *Sigh*.
  173.     //
  174.     // Firefox is even worse: it doesn't even let you know that there might be
  175.     // an RTF version on the clipboard, but it will also convert to HTML if you
  176.     // let the browser insert the content. I've filed
  177.     // https://bugzilla.mozilla.org/show_bug.cgi?id=1254028
  178.     types = clipboardData && clipboardData.types;
  179.     if ( !isEdge && types && (
  180.             indexOf.call( types, 'text/html' ) > -1 || (
  181.                 !isGecko &&
  182.                 indexOf.call( types, 'text/plain' ) > -1 &&
  183.                 indexOf.call( types, 'text/rtf' ) < 0 )
  184.             )) {
  185.         event.preventDefault();
  186.         // Abiword on Linux copies a plain text and html version, but the HTML
  187.         // version is the empty string! So always try to get HTML, but if none,
  188.         // insert plain text instead. On iOS, Facebook (and possibly other
  189.         // apps?) copy links as type text/uri-list, but also insert a **blank**
  190.         // text/plain item onto the clipboard. Why? Who knows.
  191.         if ( !choosePlain && ( data = clipboardData.getData( 'text/html' ) ) ) {
  192.             this.insertHTML( data, true );
  193.         } else if (
  194.                 ( data = clipboardData.getData( 'text/plain' ) ) ||
  195.                 ( data = clipboardData.getData( 'text/uri-list' ) ) ) {
  196.             this.insertPlainText( data, true );
  197.         }
  198.         return;
  199.     }
  200.  
  201.     // No interface. Includes all versions of IE :(
  202.     // --------------------------------------------
  203.  
  204.     this._awaitingPaste = true;
  205.  
  206.     var body = this._doc.body,
  207.         range = this.getSelection(),
  208.         startContainer = range.startContainer,
  209.         startOffset = range.startOffset,
  210.         endContainer = range.endContainer,
  211.         endOffset = range.endOffset;
  212.  
  213.     // We need to position the pasteArea in the visible portion of the screen
  214.     // to stop the browser auto-scrolling.
  215.     var pasteArea = this.createElement( 'DIV', {
  216.         contenteditable: 'true',
  217.         style: 'position:fixed; overflow:hidden; top:0; right:100%; width:1px; height:1px;'
  218.     });
  219.     body.appendChild( pasteArea );
  220.     range.selectNodeContents( pasteArea );
  221.     this.setSelection( range );
  222.  
  223.     // A setTimeout of 0 means this is added to the back of the
  224.     // single javascript thread, so it will be executed after the
  225.     // paste event.
  226.     setTimeout( function () {
  227.         try {
  228.             // IE sometimes fires the beforepaste event twice; make sure it is
  229.             // not run again before our after paste function is called.
  230.             self._awaitingPaste = false;
  231.  
  232.             // Get the pasted content and clean
  233.             var html = '',
  234.                 next = pasteArea,
  235.                 first, range;
  236.  
  237.             // #88: Chrome can apparently split the paste area if certain
  238.             // content is inserted; gather them all up.
  239.             while ( pasteArea = next ) {
  240.                 next = pasteArea.nextSibling;
  241.                 detach( pasteArea );
  242.                 // Safari and IE like putting extra divs around things.
  243.                 first = pasteArea.firstChild;
  244.                 if ( first && first === pasteArea.lastChild &&
  245.                         first.nodeName === 'DIV' ) {
  246.                     pasteArea = first;
  247.                 }
  248.                 html += pasteArea.innerHTML;
  249.             }
  250.  
  251.             range = self._createRange(
  252.                 startContainer, startOffset, endContainer, endOffset );
  253.             self.setSelection( range );
  254.  
  255.             if ( html ) {
  256.                 self.insertHTML( html, true );
  257.             }
  258.         } catch ( error ) {
  259.             self.didError( error );
  260.         }
  261.     }, 0 );
  262. };
  263.  
  264. // On Windows you can drag an drop text. We can't handle this ourselves, because
  265. // as far as I can see, there's no way to get the drop insertion point. So just
  266. // save an undo state and hope for the best.
  267. var onDrop = function ( event ) {
  268.     var types = event.dataTransfer.types;
  269.     var l = types.length;
  270.     var hasPlain = false;
  271.     var hasHTML = false;
  272.     while ( l-- ) {
  273.         switch ( types[l] ) {
  274.         case 'text/plain':
  275.             hasPlain = true;
  276.             break;
  277.         case 'text/html':
  278.             hasHTML = true;
  279.             break;
  280.         default:
  281.             return;
  282.         }
  283.     }
  284.     if ( hasHTML || hasPlain ) {
  285.         this.saveUndoState();
  286.     }
  287. };
  288.  
downloadClipboard.js Source code - Download Squire Source code
Related Source Codes/Software:
thor - Thor is a toolkit for building powerful command-li... 2017-01-08
glide - Package Management for Golang h... 2017-01-08
TextFieldEffects - Custom UITextFields effects inspired by Codrops, b... 2017-01-08
flowchart.js - Draws simple SVG flow chart diagrams from textual ... 2017-01-08
RoundedImageView - A fast ImageView that supports rounded corners, ov... 2017-01-07
webpack-demos - a collection of simple demos of Webpack 2017-01-08
amazon-dsstne - Deep Scalable Sparse Tensor Network Engine (DSSTNE... 2017-01-08
rq - Simple job queues for Python ht... 2017-01-08
emmet-vim - emmet for vim: http://emmet.io/ ... 2017-01-08
prose - A Content Editor for GitHub. ht... 2017-01-08
csvkit - A suite of utilities for converting to and working... 2017-02-18
Messenger - This is a native iOS Messenger app, making realtim... 2017-02-18
meteor-up - Production Quality Meteor Deployment 2017-02-18
book-of-modern-frontend-tooling - The Front-end Tooling Book 2017-02-17
sorcery - Magical authentication for Rails 3 & 4 2017-02-17
iScript - Xiami.com script--about shrimp, Baidu's Web site, ... 2017-02-17
AndroidViewHover - An elegant way to show your menu or messages. 2017-02-17
viper - Go configuration with fang 2017-02-17
bypy - The Python client for Baidu Yun (Personal Cloud St... 2017-02-17
ZLSwipeableView - A simple view for building card like interface ins... 2017-02-17

 Back to top