From 217fbc1355a9a530a30ef885413e3f9f664848c0 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Fri, 14 Sep 2018 22:52:26 +0200 Subject: [PATCH] Implement a simple log event system --- src/editor/index.js | 20 ++++++++++++++++++++ src/editor/model/Editor.js | 12 ++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/editor/index.js b/src/editor/index.js index 5cc2c3b2f..dbeb84d6e 100644 --- a/src/editor/index.js +++ b/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 diff --git a/src/editor/model/Editor.js b/src/editor/model/Editor.js index 2c66604c0..e2bb71edb 100644 --- a/src/editor/model/Editor.js +++ b/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