Skip to content

Commit

Permalink
Dev: Support auto scale liriliri#32
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Oct 6, 2017
1 parent c1550c0 commit 37dcb1b
Show file tree
Hide file tree
Showing 19 changed files with 301 additions and 73 deletions.
52 changes: 40 additions & 12 deletions eustia/evalCss.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,51 @@
var mark = [];
_('toStr each filter');

var styleList = [],
scale = 1;

function exports(css)
{
for (var i = 0, len = mark.length; i < len; i++)
css = toStr(css);

for (var i = 0, len = styleList.length; i < len; i++)
{
if (mark[i] === css) return;
if (styleList[i].css === css) return;
}
mark.push(css);

var container = exports.container || document.head,
style = document.createElement('style');
let container = exports.container || document.head,
el = document.createElement('style');

style.type = 'text/css';
style.textContent = css;
el.type = 'text/css';
container.appendChild(el);

let style = {css, el, container};
resetStyle(style);
styleList.push(style);

container.appendChild(style);
return style;
}

exports.reset = function ()
exports.setScale = function (s)
{
scale = s;
each(styleList, style => resetStyle(style));
};

exports.clear = function ()
{
each(styleList, ({container, el}) => container.removeChild(el));
styleList = [];
};

exports.remove = function (style)
{
mark = [];
};
console.log(style);
styleList = filter(styleList, s => s !== style);

style.container.removeChild(style.el);
};

function resetStyle({css, el})
{
el.innerText = css.replace(/(\d+)px/g, ($0, $1) => (+$1 * scale) + 'px');
}
23 changes: 20 additions & 3 deletions src/Console/Console.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Logger from './Logger'
import Tool from '../DevTools/Tool'
import util from '../lib/util'
import emitter from '../lib/emitter'

export default class Console extends Tool
{
Expand All @@ -9,6 +10,9 @@ export default class Console extends Tool
super();

this.name = 'console';
this._scale = 1;

this._registerListener();
}
init($el, parent)
{
Expand Down Expand Up @@ -80,16 +84,29 @@ export default class Console extends Tool
}
destroy()
{
this._logger.destroy();
super.destroy();

util.evalCss.remove(this._style);
this.ignoreGlobalErr();
this.restoreConsole();
this._unregisterListener();
}
_registerListener()
{
this._scaleListener = scale => this._scale = scale;

emitter.on(emitter.SCALE, this._scaleListener);
}
_unregisterListener()
{
emitter.off(emitter.SCALE, this._scaleListener);
}
_appendTpl()
{
let $el = this._$el;

util.evalCss(require('./Console.scss'));
this._style = util.evalCss(require('./Console.scss'));
$el.append(require('./Console.hbs')());

let _$inputContainer = $el.find('.eruda-js-input'),
Expand Down Expand Up @@ -174,15 +191,15 @@ export default class Console extends Tool
{
this._$inputContainer.css({
paddingTop: 0,
height: 40
height: 40 * this._scale
});

this._$inputBtns.hide();
}
_showInput()
{
this._$inputContainer.css({
paddingTop: 40,
paddingTop: 40 * this._scale,
height: '100%'
});

Expand Down
6 changes: 5 additions & 1 deletion src/Console/Logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class Logger extends util.Emitter
constructor($el, parent)
{
super();
util.evalCss(require('./Logger.scss'));
this._style = util.evalCss(require('./Logger.scss'));

this._$el = $el;
this._parent = parent;
Expand Down Expand Up @@ -47,6 +47,10 @@ export default class Logger extends util.Emitter
{
Log.showSrcInSources = flag;
}
destroy()
{
util.evalCss.remove(this._style);
}
filter(val)
{
this._filter = val;
Expand Down
26 changes: 23 additions & 3 deletions src/DevTools/DevTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import NavBar from './NavBar'
import util from '../lib/util'
import logger from '../lib/logger'
import Tool from './Tool'
import emitter from '../lib/emitter'

export default class DevTools extends util.Emitter
{
Expand All @@ -10,15 +11,17 @@ export default class DevTools extends util.Emitter
super();

if (!util.isMobile()) util.evalCss(require('../style/scrollbar.css'));
util.evalCss(require('./DevTools.scss'));
this._style = util.evalCss(require('./DevTools.scss'));

this.$parent = $parent;
this._isShow = false;
this._opacity = 1;
this._scale = 1;
this._tools = {};

this._appendTpl();
this._initNavBar();
this._registerListener();
}
show()
{
Expand Down Expand Up @@ -168,15 +171,32 @@ export default class DevTools extends util.Emitter
}
setNavBarHeight(height)
{
this._$el.css('paddingTop', height);
this._navBar.setHeight(height);
this._navBarHeight = height;
this._$el.css('paddingTop', height * this._scale);
this._navBar.setHeight(height * this._scale);
}
destroy()
{
util.evalCss.remove(this._style);
this._unregisterListener();
this.removeAll();
this._navBar.destroy();
this._$el.remove();
}
_registerListener()
{
this._scaleListener = scale =>
{
this._scale = scale;
this.setNavBarHeight(this._navBarHeight);
}

emitter.on(emitter.SCALE, this._scaleListener);
}
_unregisterListener()
{
emitter.off(emitter.SCALE, this._scaleListener);
}
_setTransparency(opacity)
{
if (!util.isNum(opacity)) return;
Expand Down
6 changes: 4 additions & 2 deletions src/DevTools/NavBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class NavBar extends util.Emitter
{
super();

util.evalCss(require('./NavBar.scss'));
this._style = util.evalCss(require('./NavBar.scss'));

this._$el = $el;
$el.html('<div class="eruda-bottom-bar"></div>');
Expand Down Expand Up @@ -60,6 +60,7 @@ export default class NavBar extends util.Emitter
}
destroy()
{
util.evalCss.remove(this._style);
this._$el.remove();
}
_resetBottomBar()
Expand All @@ -82,7 +83,6 @@ export default class NavBar extends util.Emitter
this._$el.css('height', height);

let $el = this._$el;
this._resetBottomBar();

$el.css({
height: height
Expand All @@ -91,6 +91,8 @@ export default class NavBar extends util.Emitter
'height': height,
'lineHeight': height
});

this._resetBottomBar();
}
_bindEvent()
{
Expand Down
4 changes: 3 additions & 1 deletion src/Elements/Elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class Elements extends Tool
{
super();

util.evalCss(require('./Elements.scss'));
this._style = util.evalCss(require('./Elements.scss'));

this.name = 'elements';
this._tpl = require('./Elements.hbs');
Expand Down Expand Up @@ -87,7 +87,9 @@ export default class Elements extends Tool
{
super.destroy();

util.evalCss.remove(this._style);
this._select.disable();
this._highlight.destroy();
this.restoreEventTarget();
}
_back()
Expand Down
6 changes: 5 additions & 1 deletion src/Elements/Highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default class Highlight
{
constructor($parent)
{
util.evalCss(require('./Highlight.scss'));
this._style = util.evalCss(require('./Highlight.scss'));

this._isShow = false;

Expand All @@ -22,6 +22,10 @@ export default class Highlight
this.render();
this._$el.show();
}
destroy()
{
util.evalCss.remove(this._style);
}
hide()
{
this._isShow = false;
Expand Down
36 changes: 29 additions & 7 deletions src/EntryBtn/EntryBtn.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import util from '../lib/util'
import Draggabilly from 'draggabilly'
import emitter from '../lib/emitter'

export default class EntryBtn extends util.Emitter
{
constructor($parent)
{
super();

util.evalCss(require('./EntryBtn.scss'));
this._style = util.evalCss(require('./EntryBtn.scss'));

this._$parent = $parent;
this._appendTpl();
this._makeDraggable();
this._bindEvent();
this._registerListener();
}
hide()
{
Expand All @@ -24,8 +26,33 @@ export default class EntryBtn extends util.Emitter
}
destroy()
{
util.evalCss.remove(this._style);
this._unregisterListener();
this._$el.remove();
}
_isOutOfRange()
{
let cfg = this.config,
pos = cfg.get('pos'),
defPos = this._getDefPos();

return pos.x > defPos.x + 10 ||
pos.x < 0 ||
pos.y < 0 ||
pos.y > defPos.y + 10;
}
_registerListener()
{
this._scaleListener = () => util.nextTick(() =>
{
if (this._isOutOfRange()) this._setPos();
});
emitter.on(emitter.SCALE, this._scaleListener)
}
_unregisterListener()
{
emitter.off(emitter.SCALE, this._scaleListener);
}
_appendTpl()
{
let $parent = this._$parent;
Expand All @@ -39,12 +66,7 @@ export default class EntryBtn extends util.Emitter
pos = cfg.get('pos'),
defPos = this._getDefPos();

let outOfRange = pos.x > defPos.x + 10 ||
pos.x < 0 ||
pos.y < 0 ||
pos.y > defPos.y + 10;

if (outOfRange ||
if (this._isOutOfRange() ||
!cfg.get('rememberPos') ||
orientationChanged) pos = defPos;

Expand Down
8 changes: 7 additions & 1 deletion src/Features/Features.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class Features extends Tool
{
super();

util.evalCss(require('./Features.scss'));
this._style = util.evalCss(require('./Features.scss'));

this.name = 'features';
this._tpl = require('./Features.hbs');
Expand All @@ -26,6 +26,12 @@ export default class Features extends Tool

if (!this._isInit) this._initFeatures();
}
destroy()
{
super.destroy();

util.evalCss.remove(this._style);
}
_initFeatures()
{
this._isInit = true;
Expand Down
Loading

0 comments on commit 37dcb1b

Please sign in to comment.