Browse Source

Implement a simple log event system

pull/1446/head
Artur Arseniev 8 years ago
parent
commit
217fbc1355
  1. 20
      src/editor/index.js
  2. 12
      src/editor/model/Editor.js

20
src/editor/index.js

@ -597,6 +597,26 @@ module.exports = config => {
return this;
},
/**
* Trigger event log message
* @param {String} msg String to log
* @param {Object} [opts={}] Custom options
* @param {String} [opts.ns=''] Namespace of the log (eg. to use in plugins)
* @param {String} [opts.level='debug'] Level of the log, `debug`, `info`, `warning`, `error`
* @return {this}
* @example
* editor.log('Something done!', { ns: 'from-plugin-x', level: 'info' });
* // This will trigger following events
* // `log`, `log:info`, `log-from-plugin-x`, `log-from-plugin-x:info`
* // Callbacks of those events will always receive the message and
* // options, as arguments, eg:
* // editor.on('log:info', (msg, opts) => console.info(msg, opts))
*/
log(msg, opts = {}) {
em.log(msg, opts);
return this;
},
/**
* Attach event
* @param {string} event Event name

12
src/editor/model/Editor.js

@ -621,6 +621,18 @@ module.exports = Backbone.Model.extend({
return !!this.get('editing');
},
log(msg, opts = {}) {
const { ns, level = 'debug' } = opts;
this.trigger('log', msg, opts);
level && this.trigger(`log:${level}`, msg, opts);
if (ns) {
const logNs = `log-${ns}`;
this.trigger(logNs, msg, opts);
level && this.trigger(`${logNs}:${level}`, msg, opts);
}
},
/**
* Set/get data from the HTMLElement
* @param {HTMLElement} el

Loading…
Cancel
Save