| | 1 | /* |
| | 2 | Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. |
| | 3 | For licensing, see LICENSE.html or http://ckeditor.com/license |
| | 4 | */ |
| | 5 | |
| | 6 | CKEDITOR.plugins.add( 'dropfile', |
| | 7 | { |
| | 8 | init : function( editor ) |
| | 9 | { |
| | 10 | // Only works for browers that support dropping desktop file into RTE. |
| | 11 | if ( !( CKEDITOR.env.gecko || CKEDITOR.env.webkit ) ) |
| | 12 | return; |
| | 13 | |
| | 14 | var localImageFileRegexp = /^file:\/\/.*?\.(jpg|jpeg|png|gif)$/; |
| | 15 | function handleDrop( evt ) |
| | 16 | { |
| | 17 | var target = evt.data.getTarget(), |
| | 18 | candidateUrl, |
| | 19 | imageFileUrl; |
| | 20 | |
| | 21 | candidateUrl = target.type == CKEDITOR.NODE_ELEMENT ? |
| | 22 | ( target.is( 'img' ) ? target.getAttribute( 'src' ) |
| | 23 | : target.is( 'a' )? target.getAttribute( 'href' ) : null ) : null; |
| | 24 | |
| | 25 | if ( candidateUrl && candidateUrl.match( localImageFileRegexp ) ) |
| | 26 | imageFileUrl = candidateUrl; |
| | 27 | |
| | 28 | imageFileUrl && setTimeout( function () |
| | 29 | { |
| | 30 | editor.fire( 'dropDesktopImage', { url : imageFileUrl, node : target } ); |
| | 31 | }, 0 ); |
| | 32 | } |
| | 33 | |
| | 34 | editor.on( 'contentDom', function ( evt ) |
| | 35 | { |
| | 36 | var isDragging; |
| | 37 | function removeDropListener() |
| | 38 | { |
| | 39 | if ( isDragging ) |
| | 40 | { |
| | 41 | isDragging = 0; |
| | 42 | doc.removeListener( 'DOMNodeInserted', handleDrop ); |
| | 43 | } |
| | 44 | } |
| | 45 | |
| | 46 | function addDropListener() |
| | 47 | { |
| | 48 | if ( !isDragging ) |
| | 49 | { |
| | 50 | isDragging = 1; |
| | 51 | doc.on( 'DOMNodeInserted', handleDrop ); |
| | 52 | } |
| | 53 | } |
| | 54 | |
| | 55 | var doc = editor.document, |
| | 56 | outerDoc = CKEDITOR.document; |
| | 57 | doc.on( 'dragenter', addDropListener ); |
| | 58 | outerDoc.on( 'mousemove', removeDropListener ); |
| | 59 | outerDoc.on( 'dragenter', removeDropListener ); |
| | 60 | }); |
| | 61 | } |
| | 62 | }); |