Skip to content

Commit

Permalink
feat: default settings liriliri#141
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Jun 27, 2020
1 parent 74d1ade commit 49db104
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 19 deletions.
18 changes: 16 additions & 2 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,30 @@ Initialize eruda.
|tool |string array|Choose which default tools you want, by default all will be added |
|autoScale=true |boolean |Auto scale eruda for different viewport settings |
|useShadowDom=true|boolean |Use shadow dom for css encapsulation |
|defaults |object |Default settings |

Available default settings:

|Name |Type |Desc |
|------------|------|---------------------------------------------|
|transparency|number|Transparency, 0 to 1 |
|displaySize |number|Display size, 0 to 100 |
|theme |string|Theme, defaults to Light or Dark in dark mode|

```javascript
let el = document.createElement('div');
const el = document.createElement('div');
document.body.appendChild(el);

eruda.init({
container: el,
tool: ['console', 'elements'],
useShadowDom: true,
autoScale: true
autoScale: true,
defaults: {
displaySize: 50,
transparency: 0.9,
theme: 'Monokai Pro'
}
});
```

Expand Down
20 changes: 13 additions & 7 deletions src/DevTools/DevTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@ import {
isNum,
$,
throttle,
isDarkMode
isDarkMode,
extend
} from '../lib/util'
import evalCss from '../lib/evalCss'
import LunaNotification from 'luna-notification'

export default class DevTools extends Emitter {
constructor($container) {
constructor($container, { defaults = {} } = {}) {
super()

this._defCfg = extend(
{
transparency: 1,
displaySize: 80,
theme: isDarkMode() ? 'Dark' : 'Light'
},
defaults
)

this._style = evalCss(require('./DevTools.scss'))

this.$container = $container
Expand Down Expand Up @@ -142,11 +152,7 @@ export default class DevTools extends Emitter {
return this
}
initCfg(settings) {
const cfg = (this.config = Settings.createCfg('dev-tools', {
transparency: 1,
displaySize: 80,
theme: isDarkMode() ? 'Dark' : 'Light'
}))
const cfg = (this.config = Settings.createCfg('dev-tools', this._defCfg))

this._setTransparency(cfg.get('transparency'))
this._setDisplaySize(cfg.get('displaySize'))
Expand Down
10 changes: 6 additions & 4 deletions src/eruda.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ import {
import evalCss from './lib/evalCss'

export default {
init({ container, tool, autoScale = true, useShadowDom = true } = {}) {
init({ container, tool, autoScale = true, useShadowDom = true, defaults = {} } = {}) {
if (this._isInit) return

this._isInit = true
this._scale = 1

this._initContainer(container, useShadowDom)
this._initStyle()
this._initDevTools()
this._initDevTools(defaults)
this._initEntryBtn()
this._initSettings()
this._initTools(tool)
Expand Down Expand Up @@ -182,8 +182,10 @@ export default {

this._$el = $(el)
},
_initDevTools() {
this._devTools = new DevTools(this._$el)
_initDevTools(defaults) {
this._devTools = new DevTools(this._$el, {
defaults
})
},
_initStyle() {
const className = 'eruda-style-container'
Expand Down
12 changes: 7 additions & 5 deletions test/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ function boot(name, cb) {
// Need a little delay to make sure width and height of webpack dev server iframe are initialized.
setTimeout(function() {
let options = {
useShadowDom: false
useShadowDom: false,
defaults: {
displaySize: 50,
transparency: 0.9,
theme: 'Monokai Pro'
}
}
if (name) {
options.tool = name === 'settings' ? [] : name
Expand All @@ -13,10 +18,7 @@ function boot(name, cb) {
} catch (e) {
alert(e)
}
eruda
.show()
.get()
.config.set('displaySize', 50)
eruda.show()

cb && cb()

Expand Down
7 changes: 6 additions & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@
try {
eruda.init({
container: el,
useShadowDom: false
useShadowDom: false,
defaults: {
displaySize: 50,
transparency: 0.9,
theme: 'Monokai Pro'
}
})
} catch (e) {
alert(e)
Expand Down

0 comments on commit 49db104

Please sign in to comment.