diff --git a/dist/amd/froala-editor.js b/dist/amd/froala-editor.js index d0eb664..86772da 100644 --- a/dist/amd/froala-editor.js +++ b/dist/amd/froala-editor.js @@ -63,12 +63,10 @@ define(['exports', 'aurelia-framework', 'aurelia-binding', './froala-editor-conf throw new Error('Decorating class property failed. Please ensure that transform-class-properties is enabled.'); } - var _dec, _dec2, _class, _desc, _value, _class2, _descriptor, _descriptor2, _descriptor3; + var _dec, _dec2, _class, _desc, _value, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4; var FroalaEditor1 = exports.FroalaEditor1 = (_dec = (0, _aureliaFramework.customElement)('froala-editor'), _dec2 = (0, _aureliaFramework.inject)(Element, _froalaEditorConfig.Config, _aureliaBinding.ObserverLocator), _dec(_class = _dec2(_class = (_class2 = function () { function FroalaEditor1(element, config, observerLocator) { - var _this = this; - _classCallCheck(this, FroalaEditor1); _initDefineProp(this, 'value', _descriptor, this); @@ -77,69 +75,69 @@ define(['exports', 'aurelia-framework', 'aurelia-binding', './froala-editor-conf _initDefineProp(this, 'eventHandlers', _descriptor3, this); - this.element = element; + _initDefineProp(this, 'editor', _descriptor4, this); + this.element = element; this.config = config.options(); - - this.subscriptions = [observerLocator.getObserver(this, 'value').subscribe(function (newValue, oldValue) { - if (_this.instance && _this.instance.html.get() != newValue) { - _this.instance.html(newValue); - } - })]; + this.observerLocator = observerLocator; } - FroalaEditor1.prototype.tearUp = function tearUp() { - var _this2 = this; + FroalaEditor1.prototype.bind = function bind(bindingContext, overrideContext) { + this.parent = bindingContext; + }; - if (this.config.iframe) { - this.instance = this.element.getElementsByTagName('textarea')[0]; - } else { - this.instance = this.element.getElementsByTagName('div')[0]; - } + FroalaEditor1.prototype.attached = function attached() { + var _this = this; - if (this.instance['data-froala.editor']) { + var editorSelector = this.config.iframe ? 'textarea' : 'div'; + + if (this.editor != null) { return; } - this.instance.innerHTML = this.value; - - if (this.eventHandlers && this.eventHandlers.length != 0) { - var _loop = function _loop(eventHandlerName) { - var handler = _this2.eventHandlers[eventHandlerName]; - _this2.instance.addEventListener('' + eventHandlerName, function () { - var p = arguments; - return handler.apply(this, p); - }); - }; + this.subscriptions = [this.observerLocator.getObserver(this, 'value').subscribe(function (newValue, oldValue) { + if (_this.editor && _this.editor.html.get() != newValue) { + _this.editor.html.set(newValue); + } + })]; - for (var eventHandlerName in this.eventHandlers) { - _loop(eventHandlerName); + this.editor = new _froala_editorPkgdMin2.default(this.element.querySelector(editorSelector), Object.assign({}, this.config), function () { + _this.editor.html.set(_this.value); + + if (_this.eventHandlers && _this.eventHandlers.length != 0) { + var _loop = function _loop(eventHandlerName) { + var handler = _this.eventHandlers[eventHandlerName]; + if (eventHandlerName === 'initialized') { + handler.apply(_this.parent); + } else { + _this.editor.events.on('' + eventHandlerName, function () { + for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { + args[_key] = arguments[_key]; + } + + return handler.apply(_this.parent, args); + }); + } + }; + + for (var eventHandlerName in _this.eventHandlers) { + _loop(eventHandlerName); + } } - } - this.instance.addEventListener('contentChanged', function (e, editor) { - return _this2.value = editor.html.get(); + _this.editor.events.on('blur', function (e) { + return _this.value = _this.editor.html.get(); + }); + _this.editor.events.on('contentChanged', function (e) { + return _this.value = _this.editor.html.get(); + }); }); - this.instance.addEventListener('blur', function (e, editor) { - return _this2.value = editor.html.get(); - }); - - this.instance = new _froala_editorPkgdMin2.default(this.element, Object.assign({}, this.config)); - }; - - FroalaEditor1.prototype.tearDown = function tearDown() { - if (this.instance && this.instance['data-froala.editor']) { - this.instance.destroy(); - } - - this.instance = null; - }; - - FroalaEditor1.prototype.attached = function attached() { - this.tearUp(); }; FroalaEditor1.prototype.detached = function detached() { - this.tearDown(); + if (this.editor != null) { + this.editor.destroy(); + this.editor = null; + } }; return FroalaEditor1; @@ -156,5 +154,8 @@ define(['exports', 'aurelia-framework', 'aurelia-binding', './froala-editor-conf initializer: function initializer() { return {}; } + }), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, 'editor', [_aureliaFramework.bindable], { + enumerable: true, + initializer: null })), _class2)) || _class) || _class); }); \ No newline at end of file diff --git a/dist/commonjs/froala-editor.js b/dist/commonjs/froala-editor.js index 9f96f3a..3ee87b9 100644 --- a/dist/commonjs/froala-editor.js +++ b/dist/commonjs/froala-editor.js @@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", { }); exports.FroalaEditor1 = undefined; -var _dec, _dec2, _class, _desc, _value, _class2, _descriptor, _descriptor2, _descriptor3; +var _dec, _dec2, _class, _desc, _value, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4; var _aureliaFramework = require('aurelia-framework'); @@ -66,8 +66,6 @@ function _initializerWarningHelper(descriptor, context) { var FroalaEditor1 = exports.FroalaEditor1 = (_dec = (0, _aureliaFramework.customElement)('froala-editor'), _dec2 = (0, _aureliaFramework.inject)(Element, _froalaEditorConfig.Config, _aureliaBinding.ObserverLocator), _dec(_class = _dec2(_class = (_class2 = function () { function FroalaEditor1(element, config, observerLocator) { - var _this = this; - _classCallCheck(this, FroalaEditor1); _initDefineProp(this, 'value', _descriptor, this); @@ -76,69 +74,69 @@ var FroalaEditor1 = exports.FroalaEditor1 = (_dec = (0, _aureliaFramework.custom _initDefineProp(this, 'eventHandlers', _descriptor3, this); - this.element = element; + _initDefineProp(this, 'editor', _descriptor4, this); + this.element = element; this.config = config.options(); - - this.subscriptions = [observerLocator.getObserver(this, 'value').subscribe(function (newValue, oldValue) { - if (_this.instance && _this.instance.html.get() != newValue) { - _this.instance.html(newValue); - } - })]; + this.observerLocator = observerLocator; } - FroalaEditor1.prototype.tearUp = function tearUp() { - var _this2 = this; + FroalaEditor1.prototype.bind = function bind(bindingContext, overrideContext) { + this.parent = bindingContext; + }; - if (this.config.iframe) { - this.instance = this.element.getElementsByTagName('textarea')[0]; - } else { - this.instance = this.element.getElementsByTagName('div')[0]; - } + FroalaEditor1.prototype.attached = function attached() { + var _this = this; - if (this.instance['data-froala.editor']) { + var editorSelector = this.config.iframe ? 'textarea' : 'div'; + + if (this.editor != null) { return; } - this.instance.innerHTML = this.value; - - if (this.eventHandlers && this.eventHandlers.length != 0) { - var _loop = function _loop(eventHandlerName) { - var handler = _this2.eventHandlers[eventHandlerName]; - _this2.instance.addEventListener('' + eventHandlerName, function () { - var p = arguments; - return handler.apply(this, p); - }); - }; + this.subscriptions = [this.observerLocator.getObserver(this, 'value').subscribe(function (newValue, oldValue) { + if (_this.editor && _this.editor.html.get() != newValue) { + _this.editor.html.set(newValue); + } + })]; - for (var eventHandlerName in this.eventHandlers) { - _loop(eventHandlerName); + this.editor = new _froala_editorPkgdMin2.default(this.element.querySelector(editorSelector), Object.assign({}, this.config), function () { + _this.editor.html.set(_this.value); + + if (_this.eventHandlers && _this.eventHandlers.length != 0) { + var _loop = function _loop(eventHandlerName) { + var handler = _this.eventHandlers[eventHandlerName]; + if (eventHandlerName === 'initialized') { + handler.apply(_this.parent); + } else { + _this.editor.events.on('' + eventHandlerName, function () { + for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { + args[_key] = arguments[_key]; + } + + return handler.apply(_this.parent, args); + }); + } + }; + + for (var eventHandlerName in _this.eventHandlers) { + _loop(eventHandlerName); + } } - } - this.instance.addEventListener('contentChanged', function (e, editor) { - return _this2.value = editor.html.get(); + _this.editor.events.on('blur', function (e) { + return _this.value = _this.editor.html.get(); + }); + _this.editor.events.on('contentChanged', function (e) { + return _this.value = _this.editor.html.get(); + }); }); - this.instance.addEventListener('blur', function (e, editor) { - return _this2.value = editor.html.get(); - }); - - this.instance = new _froala_editorPkgdMin2.default(this.element, Object.assign({}, this.config)); - }; - - FroalaEditor1.prototype.tearDown = function tearDown() { - if (this.instance && this.instance['data-froala.editor']) { - this.instance.destroy(); - } - - this.instance = null; - }; - - FroalaEditor1.prototype.attached = function attached() { - this.tearUp(); }; FroalaEditor1.prototype.detached = function detached() { - this.tearDown(); + if (this.editor != null) { + this.editor.destroy(); + this.editor = null; + } }; return FroalaEditor1; @@ -155,4 +153,7 @@ var FroalaEditor1 = exports.FroalaEditor1 = (_dec = (0, _aureliaFramework.custom initializer: function initializer() { return {}; } +}), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, 'editor', [_aureliaFramework.bindable], { + enumerable: true, + initializer: null })), _class2)) || _class) || _class); \ No newline at end of file diff --git a/dist/es2015/froala-editor.js b/dist/es2015/froala-editor.js index 83044a8..22b5cd7 100644 --- a/dist/es2015/froala-editor.js +++ b/dist/es2015/froala-editor.js @@ -1,4 +1,4 @@ -var _dec, _dec2, _class, _desc, _value, _class2, _descriptor, _descriptor2, _descriptor3; +var _dec, _dec2, _class, _desc, _value, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4; function _initDefineProp(target, property, descriptor, context) { if (!descriptor) return; @@ -59,59 +59,56 @@ export let FroalaEditor1 = (_dec = customElement('froala-editor'), _dec2 = injec _initDefineProp(this, 'eventHandlers', _descriptor3, this); - this.element = element; + _initDefineProp(this, 'editor', _descriptor4, this); + this.element = element; this.config = config.options(); + this.observerLocator = observerLocator; + } - this.subscriptions = [observerLocator.getObserver(this, 'value').subscribe((newValue, oldValue) => { - if (this.instance && this.instance.html.get() != newValue) { - this.instance.html(newValue); - } - })]; + bind(bindingContext, overrideContext) { + this.parent = bindingContext; } - tearUp() { - if (this.config.iframe) { - this.instance = this.element.getElementsByTagName('textarea')[0]; - } else { - this.instance = this.element.getElementsByTagName('div')[0]; - } + attached() { + const editorSelector = this.config.iframe ? 'textarea' : 'div'; - if (this.instance['data-froala.editor']) { + if (this.editor != null) { return; } - this.instance.innerHTML = this.value; - - if (this.eventHandlers && this.eventHandlers.length != 0) { - for (let eventHandlerName in this.eventHandlers) { - let handler = this.eventHandlers[eventHandlerName]; - this.instance.addEventListener(`${eventHandlerName}`, function () { - let p = arguments; - return handler.apply(this, p); - }); + this.subscriptions = [this.observerLocator.getObserver(this, 'value').subscribe((newValue, oldValue) => { + if (this.editor && this.editor.html.get() != newValue) { + this.editor.html.set(newValue); } - } - this.instance.addEventListener('contentChanged', (e, editor) => this.value = editor.html.get()); - this.instance.addEventListener('blur', (e, editor) => this.value = editor.html.get()); - - this.instance = new FroalaEditor(this.element, Object.assign({}, this.config)); - } - - tearDown() { - if (this.instance && this.instance['data-froala.editor']) { - this.instance.destroy(); - } - this.instance = null; - } + })]; - attached() { - this.tearUp(); + this.editor = new FroalaEditor(this.element.querySelector(editorSelector), Object.assign({}, this.config), () => { + this.editor.html.set(this.value); + + if (this.eventHandlers && this.eventHandlers.length != 0) { + for (let eventHandlerName in this.eventHandlers) { + let handler = this.eventHandlers[eventHandlerName]; + if (eventHandlerName === 'initialized') { + handler.apply(this.parent); + } else { + this.editor.events.on(`${eventHandlerName}`, (...args) => { + return handler.apply(this.parent, args); + }); + } + } + } + this.editor.events.on('blur', e => this.value = this.editor.html.get()); + this.editor.events.on('contentChanged', e => this.value = this.editor.html.get()); + }); } detached() { - this.tearDown(); + if (this.editor != null) { + this.editor.destroy(); + this.editor = null; + } } }, (_descriptor = _applyDecoratedDescriptor(_class2.prototype, 'value', [bindable], { enumerable: true, @@ -126,4 +123,7 @@ export let FroalaEditor1 = (_dec = customElement('froala-editor'), _dec2 = injec initializer: function () { return {}; } +}), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, 'editor', [bindable], { + enumerable: true, + initializer: null })), _class2)) || _class) || _class); \ No newline at end of file diff --git a/dist/system/froala-editor.js b/dist/system/froala-editor.js index 81f1f96..37368bb 100644 --- a/dist/system/froala-editor.js +++ b/dist/system/froala-editor.js @@ -3,7 +3,7 @@ System.register(['aurelia-framework', 'aurelia-binding', './froala-editor-config', 'froala-editor/js/froala_editor.pkgd.min.js'], function (_export, _context) { "use strict"; - var inject, customElement, bindable, ObserverLocator, Config, FroalaEditor, _dec, _dec2, _class, _desc, _value, _class2, _descriptor, _descriptor2, _descriptor3, FroalaEditor1; + var inject, customElement, bindable, ObserverLocator, Config, FroalaEditor, _dec, _dec2, _class, _desc, _value, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4, FroalaEditor1; function _initDefineProp(target, property, descriptor, context) { if (!descriptor) return; @@ -69,8 +69,6 @@ System.register(['aurelia-framework', 'aurelia-binding', './froala-editor-config execute: function () { _export('FroalaEditor1', FroalaEditor1 = (_dec = customElement('froala-editor'), _dec2 = inject(Element, Config, ObserverLocator), _dec(_class = _dec2(_class = (_class2 = function () { function FroalaEditor1(element, config, observerLocator) { - var _this = this; - _classCallCheck(this, FroalaEditor1); _initDefineProp(this, 'value', _descriptor, this); @@ -79,69 +77,69 @@ System.register(['aurelia-framework', 'aurelia-binding', './froala-editor-config _initDefineProp(this, 'eventHandlers', _descriptor3, this); - this.element = element; + _initDefineProp(this, 'editor', _descriptor4, this); + this.element = element; this.config = config.options(); - - this.subscriptions = [observerLocator.getObserver(this, 'value').subscribe(function (newValue, oldValue) { - if (_this.instance && _this.instance.html.get() != newValue) { - _this.instance.html(newValue); - } - })]; + this.observerLocator = observerLocator; } - FroalaEditor1.prototype.tearUp = function tearUp() { - var _this2 = this; + FroalaEditor1.prototype.bind = function bind(bindingContext, overrideContext) { + this.parent = bindingContext; + }; - if (this.config.iframe) { - this.instance = this.element.getElementsByTagName('textarea')[0]; - } else { - this.instance = this.element.getElementsByTagName('div')[0]; - } + FroalaEditor1.prototype.attached = function attached() { + var _this = this; - if (this.instance['data-froala.editor']) { + var editorSelector = this.config.iframe ? 'textarea' : 'div'; + + if (this.editor != null) { return; } - this.instance.innerHTML = this.value; - - if (this.eventHandlers && this.eventHandlers.length != 0) { - var _loop = function _loop(eventHandlerName) { - var handler = _this2.eventHandlers[eventHandlerName]; - _this2.instance.addEventListener('' + eventHandlerName, function () { - var p = arguments; - return handler.apply(this, p); - }); - }; + this.subscriptions = [this.observerLocator.getObserver(this, 'value').subscribe(function (newValue, oldValue) { + if (_this.editor && _this.editor.html.get() != newValue) { + _this.editor.html.set(newValue); + } + })]; - for (var eventHandlerName in this.eventHandlers) { - _loop(eventHandlerName); + this.editor = new FroalaEditor(this.element.querySelector(editorSelector), Object.assign({}, this.config), function () { + _this.editor.html.set(_this.value); + + if (_this.eventHandlers && _this.eventHandlers.length != 0) { + var _loop = function _loop(eventHandlerName) { + var handler = _this.eventHandlers[eventHandlerName]; + if (eventHandlerName === 'initialized') { + handler.apply(_this.parent); + } else { + _this.editor.events.on('' + eventHandlerName, function () { + for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { + args[_key] = arguments[_key]; + } + + return handler.apply(_this.parent, args); + }); + } + }; + + for (var eventHandlerName in _this.eventHandlers) { + _loop(eventHandlerName); + } } - } - this.instance.addEventListener('contentChanged', function (e, editor) { - return _this2.value = editor.html.get(); + _this.editor.events.on('blur', function (e) { + return _this.value = _this.editor.html.get(); + }); + _this.editor.events.on('contentChanged', function (e) { + return _this.value = _this.editor.html.get(); + }); }); - this.instance.addEventListener('blur', function (e, editor) { - return _this2.value = editor.html.get(); - }); - - this.instance = new FroalaEditor(this.element, Object.assign({}, this.config)); - }; - - FroalaEditor1.prototype.tearDown = function tearDown() { - if (this.instance && this.instance['data-froala.editor']) { - this.instance.destroy(); - } - - this.instance = null; - }; - - FroalaEditor1.prototype.attached = function attached() { - this.tearUp(); }; FroalaEditor1.prototype.detached = function detached() { - this.tearDown(); + if (this.editor != null) { + this.editor.destroy(); + this.editor = null; + } }; return FroalaEditor1; @@ -158,6 +156,9 @@ System.register(['aurelia-framework', 'aurelia-binding', './froala-editor-config initializer: function initializer() { return {}; } + }), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, 'editor', [bindable], { + enumerable: true, + initializer: null })), _class2)) || _class) || _class)); _export('FroalaEditor1', FroalaEditor1); diff --git a/package.json b/package.json index 3b5f30b..a798cd1 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "babel-preset-stage-1": "^6.22.0", "conventional-changelog": "^2.0.3", "del": "^3.0.0", - "gulp": "^4.0.0", + "gulp": "^3.9.1", "gulp-babel": "^6.1.2", "gulp-bump": "^2.7.0", "gulp-eslint": "^3.0.1", diff --git a/src/froala-editor.js b/src/froala-editor.js index 9db8d54..9bee384 100644 --- a/src/froala-editor.js +++ b/src/froala-editor.js @@ -10,83 +10,72 @@ import FroalaEditor from 'froala-editor/js/froala_editor.pkgd.min.js' @inject(Element, Config, ObserverLocator) export class FroalaEditor1 { @bindable value; - @bindable config = {} - @bindable eventHandlers = {} + @bindable config = {}; + @bindable eventHandlers = {}; + @bindable editor; + parent; element; - instance; constructor (element, config, observerLocator) { - // Store element. this.element = element; + this.config = config.options(); + this.observerLocator = observerLocator; + } + + // Get parent context to use in eventhandlers + bind(bindingContext, overrideContext) { + this.parent = bindingContext; + } - // Read config. - this.config = config.options(); + // Setup + attached() { + // Get element. + const editorSelector = this.config.iframe ? 'textarea' : 'div'; - // Observe value. + // Check if editor isn't already initialized. + if (this.editor != null) { return; } + + // Observe value. this.subscriptions = [ - observerLocator + this.observerLocator .getObserver(this, 'value') .subscribe((newValue, oldValue) => { - if (this.instance && this.instance.html.get() != newValue) { - this.instance.html(newValue); + if (this.editor && this.editor.html.get() != newValue) { + this.editor.html.set(newValue); } }) - ]; - } - - // Starting poing. - tearUp () { - // Get element. - if (this.config.iframe) { - this.instance = this.element.getElementsByTagName('textarea')[0]; - } - else { - this.instance = this.element.getElementsByTagName('div')[0]; - } - - // Check if editor isn't already initialized. - if (this.instance['data-froala.editor']) { - return; - } - - // Set the HTML for the inner element. - this.instance.innerHTML = this.value; - - // Set events. - if (this.eventHandlers && this.eventHandlers.length != 0) { - for(let eventHandlerName in this.eventHandlers) { - let handler = this.eventHandlers[eventHandlerName]; - this.instance.addEventListener(`${eventHandlerName}`, function() { - let p = arguments; - return handler.apply(this, p) - }); - + ]; + + // Initialize editor. + this.editor = new FroalaEditor(this.element.querySelector(editorSelector), Object.assign({}, this.config), () => { + // Set initial HTML value. + this.editor.html.set(this.value); + + // Set Events + if (this.eventHandlers && this.eventHandlers.length != 0) { + for(let eventHandlerName in this.eventHandlers) { + let handler = this.eventHandlers[eventHandlerName]; + if (eventHandlerName === 'initialized') { + handler.apply(this.parent); + } else { + this.editor.events.on(`${eventHandlerName}`, (...args) => { + return handler.apply(this.parent, args); + }); + } + } } - } - this.instance.addEventListener('contentChanged', (e, editor) => this.value = editor.html.get()); - this.instance.addEventListener('blur', (e, editor) => this.value = editor.html.get()) - // Initialize editor. - this.instance = new FroalaEditor(this.element, Object.assign({}, this.config)); + this.editor.events.on('blur', (e) => this.value = this.editor.html.get()); + this.editor.events.on('contentChanged', (e) => this.value = this.editor.html.get()); + }); } - // Destroy. - tearDown () { - if (this.instance && this.instance['data-froala.editor']) { - this.instance.destroy(); - } - - this.instance = null; - } - - // Setup. - attached () { - this.tearUp(); - } - - // Destroy. + // Destroy detached () { - this.tearDown(); + if (this.editor != null) { + this.editor.destroy(); + this.editor = null; + } } }