MediaWiki:Gadget-FDCodeEditor.js: Difference between revisions
FDCodeEditor: debounce the synthetic preview keyup (upstream DOT preview stacks async renders) |
FDCodeEditor: catch throws from attach() (then()s 2nd arg never saw them) + guard against double-attach |
||
| Line 57: | Line 57: | ||
var cm6 = require( 'ext.CodeMirror.v6.lib' ); | var cm6 = require( 'ext.CodeMirror.v6.lib' ); | ||
var $textarea = $( textarea ); | var $textarea = $( textarea ); | ||
var previewTimer = null; | var previewTimer = null; | ||
var cm; | |||
// Never attach twice to the same textarea. CodeMirror's initialize() calls | |||
// $.fn.textSelection( 'register', ... ), which THROWS "Another textSelection | |||
// API was already registered" the second time - and because that throw | |||
// happens inside a promise callback it is easy for it to disappear | |||
// silently, leaving a plain textarea and no explanation. | |||
if ( $textarea.data( 'fdCodeEditorAttached' ) ) { | |||
return; | |||
} | |||
$textarea.data( 'fdCodeEditorAttached', true ); | |||
cm = new CodeMirror( textarea ); | |||
cm.initialize( [ | cm.initialize( [ | ||
| Line 112: | Line 124: | ||
attach( require, textarea ); | attach( require, textarea ); | ||
} ); | } ); | ||
} | } ).catch( function ( err ) { | ||
// | // .catch(), NOT then()'s second argument: a rejection handler passed to | ||
// | // then() does not see errors THROWN INSIDE its own success callback, so | ||
mw.log.warn( '[FDCodeEditor] CodeMirror | // anything attach() threw used to vanish without trace - the editor just | ||
// stayed a plain textarea with every module reporting "ready". | |||
// Never fail silently. | |||
mw.log.warn( '[FDCodeEditor] could not attach CodeMirror; the diagram ' + | |||
'editor stays a plain textarea.', err ); | 'editor stays a plain textarea.', err ); | ||
} ); | } ); | ||
} ); | } ); | ||
}() ); | }() ); | ||