mirror of
https://github.com/fastmail/Squire.git
synced 2025-01-03 05:00:13 -05:00
Catch all errors to allow logging.
* All errors will be caught and passed to the editor.didError fn. This can be overridden to do something useful, like logging them to the server.
This commit is contained in:
parent
f08fe04bb2
commit
52e517b376
4 changed files with 99 additions and 77 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -1,6 +1,6 @@
|
|||
/* Copyright © 2011-2012 by Neil Jenkins. Licensed under the MIT license. */
|
||||
|
||||
/*global UA, DOMTreeWalker, Range, top, document, setTimeout */
|
||||
/*global UA, DOMTreeWalker, Range, top, document, setTimeout, console */
|
||||
|
||||
( function ( doc, UA, TreeWalker ) {
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
|||
|
||||
var win = doc.defaultView;
|
||||
var body = doc.body;
|
||||
var editor;
|
||||
|
||||
var isOpera = UA.isOpera;
|
||||
var isGecko = UA.isGecko;
|
||||
|
@ -56,7 +57,7 @@
|
|||
// document node, since these events are fired in a custom manner by the
|
||||
// editor code.
|
||||
var customEvents = {
|
||||
cut: 1, paste: 1, focus: 1, blur: 1,
|
||||
focus: 1, blur: 1,
|
||||
pathChange: 1, select: 1, input: 1, undoStateChange: 1
|
||||
};
|
||||
|
||||
|
@ -74,11 +75,15 @@
|
|||
}
|
||||
for ( i = 0, l = handlers.length; i < l; i += 1 ) {
|
||||
obj = handlers[i];
|
||||
try {
|
||||
if ( obj.handleEvent ) {
|
||||
obj.handleEvent( event );
|
||||
} else {
|
||||
obj( event );
|
||||
}
|
||||
} catch ( error ) {
|
||||
editor.didError( error );
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -1184,24 +1189,28 @@
|
|||
// --- Cut and Paste ---
|
||||
|
||||
var afterCut = function () {
|
||||
try {
|
||||
// If all content removed, ensure div at start of body.
|
||||
body.fixCursor();
|
||||
} catch ( error ) {
|
||||
editor.didError( error );
|
||||
}
|
||||
};
|
||||
|
||||
doc.addEventListener( isIE ? 'beforecut' : 'cut', function () {
|
||||
addEventListener( isIE ? 'beforecut' : 'cut', function () {
|
||||
// Save undo checkpoint
|
||||
var range = getSelection();
|
||||
recordUndoState( range );
|
||||
getRangeAndRemoveBookmark( range );
|
||||
setSelection( range );
|
||||
setTimeout( afterCut, 0 );
|
||||
}, false );
|
||||
});
|
||||
|
||||
// IE sometimes fires the beforepaste event twice; make sure it is not run
|
||||
// again before our after paste function is called.
|
||||
var awaitingPaste = false;
|
||||
|
||||
doc.addEventListener( isIE ? 'beforepaste' : 'paste', function ( event ) {
|
||||
addEventListener( isIE ? 'beforepaste' : 'paste', function ( event ) {
|
||||
if ( awaitingPaste ) { return; }
|
||||
|
||||
// Treat image paste as a drop of an image file.
|
||||
|
@ -1252,6 +1261,7 @@
|
|||
// single javascript thread, so it will be executed after the
|
||||
// paste event.
|
||||
setTimeout( function () {
|
||||
try {
|
||||
// Get the pasted content and clean
|
||||
var frag = pasteArea.detach().empty(),
|
||||
first = frag.firstChild,
|
||||
|
@ -1261,7 +1271,8 @@
|
|||
// Was anything actually pasted?
|
||||
if ( first ) {
|
||||
// Safari and IE like putting extra divs around things.
|
||||
if ( first === frag.lastChild && first.nodeName === 'DIV' ) {
|
||||
if ( first === frag.lastChild &&
|
||||
first.nodeName === 'DIV' ) {
|
||||
frag.replaceChild( first.empty(), first );
|
||||
}
|
||||
|
||||
|
@ -1296,8 +1307,11 @@
|
|||
updatePath( range, true );
|
||||
|
||||
awaitingPaste = false;
|
||||
} catch ( error ) {
|
||||
editor.didError( error );
|
||||
}
|
||||
}, 0 );
|
||||
}, false );
|
||||
});
|
||||
|
||||
// --- Keyboard interaction ---
|
||||
|
||||
|
@ -1333,6 +1347,7 @@
|
|||
// link in Opera, it won't delete the link. Let's make things consistent. If
|
||||
// you delete all text inside an inline tag, remove the inline tag.
|
||||
var afterDelete = function () {
|
||||
try {
|
||||
var range = getSelection(),
|
||||
node = range.startContainer,
|
||||
parent;
|
||||
|
@ -1357,6 +1372,9 @@
|
|||
setSelection( range );
|
||||
updatePath( range );
|
||||
}
|
||||
} catch ( error ) {
|
||||
editor.didError( error );
|
||||
}
|
||||
};
|
||||
|
||||
// If you select all in IE8 then type, it makes a P; replace it with
|
||||
|
@ -1672,7 +1690,11 @@
|
|||
};
|
||||
};
|
||||
|
||||
win.editor = {
|
||||
win.editor = editor = {
|
||||
|
||||
didError: function ( error ) {
|
||||
console.log( error );
|
||||
},
|
||||
|
||||
_setPlaceholderTextNode: setPlaceholderTextNode,
|
||||
|
||||
|
@ -1957,7 +1979,7 @@
|
|||
// --- Initialise ---
|
||||
|
||||
body.setAttribute( 'contenteditable', 'true' );
|
||||
win.editor.setHTML( '' );
|
||||
editor.setHTML( '' );
|
||||
|
||||
if ( win.onEditorLoad ) {
|
||||
win.onEditorLoad( win.editor );
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/* Copyright © 2011-2012 by Neil Jenkins. Licensed under the MIT license. */
|
||||
|
||||
( function ( undefined ) {
|
||||
( function () {
|
||||
|
||||
/*jshint strict: false */
|
||||
|
||||
|
|
Loading…
Reference in a new issue