mirror of
https://github.com/fastmail/Squire.git
synced 2024-12-22 15:23:29 -05:00
Allow custom cleaning on paste.
A willPaste event is now fired just before pasted content is inserted into the document, allowing custom, arbitrary modification of the pasted content, or prevention of the paste event altogether.
This commit is contained in:
parent
b6c659e295
commit
98ebe661b2
3 changed files with 22 additions and 13 deletions
|
@ -58,9 +58,10 @@ Attach an event listener to the editor. The handler can be either a function or
|
|||
* **keypress**: Standard DOM keypress event.
|
||||
* **keyup**: Standard DOM keyup event.
|
||||
* **input**: The user inserted, deleted or changed the style of some text; in other words, the result for `editor.getHTML()` will have changed.
|
||||
* **pathChange**: The path (see getPath documentation) to the cursor has changed. The new path is available as the `data` property on the event object.
|
||||
* **select**: The user selected some text
|
||||
* **pathChange**: The path (see getPath documentation) to the cursor has changed. The new path is available as the `path` property on the event object.
|
||||
* **select**: The user selected some text.
|
||||
* **undoStateChange**: The availability of undo and/or redo has changed. The event object has two boolean properties, `canUndo` and `canRedo` to let you know the new state.
|
||||
* **willPaste**: The user is pasting content into the document. The content that will be inserted is available as the `fragment` property on the event object. You can modify this fragment in your event handler to change what will be pasted. You can also call the `preventDefault` on the event object to cancel the paste operation.
|
||||
|
||||
#### Parameters ####
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -67,10 +67,8 @@
|
|||
var handlers = events[ type ],
|
||||
i, l, obj;
|
||||
if ( handlers ) {
|
||||
if ( typeof event !== 'object' ) {
|
||||
event = {
|
||||
data: event
|
||||
};
|
||||
if ( !event ) {
|
||||
event = {};
|
||||
}
|
||||
if ( event.type !== type ) {
|
||||
event.type = type;
|
||||
|
@ -231,7 +229,7 @@
|
|||
focus.getPath() : '(selection)' : '';
|
||||
if ( path !== newPath ) {
|
||||
path = newPath;
|
||||
fireEvent( 'pathChange', newPath );
|
||||
fireEvent( 'pathChange', { path: newPath } );
|
||||
}
|
||||
}
|
||||
if ( anchor !== focus ) {
|
||||
|
@ -1219,7 +1217,7 @@
|
|||
|
||||
// Was anything actually pasted?
|
||||
if ( first ) {
|
||||
// Safari likes putting extra divs around things.
|
||||
// Safari and IE like putting extra divs around things.
|
||||
if ( first === frag.lastChild && first.nodeName === 'DIV' ) {
|
||||
frag.replaceChild( first.empty(), first );
|
||||
}
|
||||
|
@ -1229,17 +1227,27 @@
|
|||
cleanTree( frag, false );
|
||||
cleanupBRs( frag );
|
||||
|
||||
var node = frag;
|
||||
var node = frag,
|
||||
doPaste = true;
|
||||
while ( node = node.getNextBlock() ) {
|
||||
node.fixCursor();
|
||||
}
|
||||
|
||||
fireEvent( 'willPaste', {
|
||||
fragment: frag,
|
||||
preventDefault: function () {
|
||||
doPaste = false;
|
||||
}
|
||||
});
|
||||
|
||||
// Insert pasted data
|
||||
if ( doPaste ) {
|
||||
range.insertTreeFragment( frag );
|
||||
docWasChanged();
|
||||
|
||||
range.collapse( false );
|
||||
}
|
||||
}
|
||||
|
||||
setSelection( range );
|
||||
updatePath( range, true );
|
||||
|
|
Loading…
Reference in a new issue