Skip to content

Commit

Permalink
version 1.2.0
Browse files Browse the repository at this point in the history
replace instace specific event listener with a global one as demonstrated in fastboot docs https://ember-fastboot.com/docs/addon-author-guide
  • Loading branch information
st-h committed May 18, 2019
1 parent fbf2c93 commit 723795b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
28 changes: 23 additions & 5 deletions addon/mixins/resize-text.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import Mixin from '@ember/object/mixin';
import { inject as service } from '@ember/service';

let componentsToNotify = [];
let didSetupListener = false;

function setupListener() {
didSetupListener = true;
window.addEventListener('resize', () => {
componentsToNotify.forEach(c => {
c.scaleFont();
});
}, false);
}

export default Mixin.create({

classNames: ['resize-text'],
Expand All @@ -11,15 +23,21 @@ export default Mixin.create({
textMeasurer: service(),

didInsertElement() {
this._super(...arguments);
window.addEventListener('resize', this.set('_resizeHandler', this.scaleFont.bind(this)), false);
if (!didSetupListener) {
setupListener();
}
componentsToNotify.push(this);
this.element.style['white-space'] = 'nowrap';
this.scaleFont();
},

willDestroyElement: function() {
this._super(...arguments);
window.removeEventListener('resize', this.get('_resizeHandler'), false);
willDestroy() {
for (let i = 0; i < componentsToNotify.length; i++) {
if (componentsToNotify[i] === this) {
componentsToNotify.splice(i, 1);
break;
}
}
},

scaleFont() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ember-resize-text",
"version": "1.1.2",
"version": "1.2.0",
"description": "ember addon that makes use of ember-text-measurer to efficiently resize the font of a single line of text to make it fit",
"keywords": [
"ember-addon",
Expand Down

0 comments on commit 723795b

Please sign in to comment.