Embed the preact code directly (#1177)

* Embed the preact code directly

* Do not transform object rest spread

* Lint

* Ignore preact from lint
This commit is contained in:
Paulus Schoutsen 2018-05-15 17:56:32 -04:00 committed by GitHub
parent 47642957c8
commit 96d7ec7cda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
30 changed files with 162 additions and 151 deletions

View File

@ -80,7 +80,6 @@ gulp.task('run_rollup_es5', () => gulp.src([
gulp.task('run_rollup', () => gulp.src([ gulp.task('run_rollup', () => gulp.src([
'js/core.js', 'js/core.js',
'js/panel-config/panel-config.js',
'js/util.js', 'js/util.js',
'demo_data/demo_data.js', 'demo_data/demo_data.js',
]) ])

View File

@ -5,7 +5,7 @@ export function onChangeEvent(prop, ev) {
return; return;
} }
const data = { ...origData }; const data = Object.assign({}, origData);
if (ev.target.value) { if (ev.target.value) {
data[ev.target.name] = ev.target.value; data[ev.target.name] = ev.target.value;

View File

@ -0,0 +1,5 @@
import { render } from 'preact';
export default function unmount(mountEl) {
render(() => null, mountEl);
}

View File

@ -15,31 +15,31 @@ export default class Automation extends Component {
} }
onChange(ev) { onChange(ev) {
this.props.onChange({ this.props.onChange(Object.assign(
...this.props.automation, {}, this.props.automation,
[ev.target.name]: ev.target.value, { [ev.target.name]: ev.target.value },
}); ));
} }
triggerChanged(trigger) { triggerChanged(trigger) {
this.props.onChange({ this.props.onChange(Object.assign(
...this.props.automation, {}, this.props.automation,
trigger, { trigger },
}); ));
} }
conditionChanged(condition) { conditionChanged(condition) {
this.props.onChange({ this.props.onChange(Object.assign(
...this.props.automation, {}, this.props.automation,
condition, { condition },
}); ));
} }
actionChanged(action) { actionChanged(action) {
this.props.onChange({ this.props.onChange(Object.assign(
...this.props.automation, {}, this.props.automation,
action, { action },
}); ));
} }
render({ automation, isWide, hass, localize }) { render({ automation, isWide, hass, localize }) {

View File

@ -29,10 +29,10 @@ export default class ConditionRow extends Component {
const type = ev.target.selectedItem.attributes.condition.value; const type = ev.target.selectedItem.attributes.condition.value;
if (type !== this.props.condition.condition) { if (type !== this.props.condition.condition) {
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, Object.assign(
condition: type, { condition: type },
...TYPES[type].defaultConfig TYPES[type].defaultConfig
}); ));
} }
} }

View File

@ -11,10 +11,10 @@ export default class NumericStateCondition extends Component {
} }
entityPicked(ev) { entityPicked(ev) {
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, Object.assign(
...this.props.condition, {}, this.props.condition,
entity_id: ev.target.value, { entity_id: ev.target.value },
}); ));
} }
/* eslint-disable camelcase */ /* eslint-disable camelcase */

View File

@ -11,10 +11,10 @@ export default class StateCondition extends Component {
} }
entityPicked(ev) { entityPicked(ev) {
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, Object.assign(
...this.props.condition, {}, this.props.condition,
entity_id: ev.target.value, { entity_id: ev.target.value },
}); ));
} }
/* eslint-disable camelcase */ /* eslint-disable camelcase */

View File

@ -12,7 +12,7 @@ export default class SunCondition extends Component {
} }
radioGroupPicked(key, ev) { radioGroupPicked(key, ev) {
const condition = { ...this.props.condition }; const condition = Object.assign({}, this.props.condition);
if (ev.target.selected) { if (ev.target.selected) {
condition[key] = ev.target.selected; condition[key] = ev.target.selected;

View File

@ -18,17 +18,17 @@ export default class ZoneCondition extends Component {
} }
entityPicked(ev) { entityPicked(ev) {
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, Object.assign(
...this.props.condition, {}, this.props.condition,
entity_id: ev.target.value, { entity_id: ev.target.value },
}); ));
} }
zonePicked(ev) { zonePicked(ev) {
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, Object.assign(
...this.props.condition, {}, this.props.condition,
zone: ev.target.value, { zone: ev.target.value },
}); ));
} }
/* eslint-disable camelcase */ /* eslint-disable camelcase */

View File

@ -1,15 +0,0 @@
import { h, render } from 'preact';
import Automation from './automation.js';
import Script from './script.js';
window.ScriptEditor = function (mountEl, props, mergeEl) {
return render(h(Script, props), mountEl, mergeEl);
};
window.AutomationEditor = function (mountEl, props, mergeEl) {
return render(h(Automation, props), mountEl, mergeEl);
};
window.unmountPreact = function (mountEl, mergeEl) {
render(() => null, mountEl, mergeEl);
};

View File

@ -11,17 +11,17 @@ export default class ScriptEditor extends Component {
} }
onChange(ev) { onChange(ev) {
this.props.onChange({ this.props.onChange(Object.assign(
...this.props.script, {}, this.props.script,
[ev.target.name]: ev.target.value, { [ev.target.name]: ev.target.value }
}); ));
} }
sequenceChanged(sequence) { sequenceChanged(sequence) {
this.props.onChange({ this.props.onChange(Object.assign(
...this.props.script, {}, this.props.script,
sequence, { sequence },
}); ));
} }
render({ script, isWide, hass, localize }) { render({ script, isWide, hass, localize }) {

View File

@ -11,17 +11,17 @@ export default class CallServiceAction extends Component {
} }
serviceChanged(ev) { serviceChanged(ev) {
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, Object.assign(
...this.props.action, {}, this.props.action,
service: ev.target.value, { service: ev.target.value },
}); ));
} }
serviceDataChanged(data) { serviceDataChanged(data) {
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, Object.assign(
...this.props.action, {}, this.props.action,
data, { data },
}); ));
} }
render({ action, hass, localize }) { render({ action, hass, localize }) {

View File

@ -18,7 +18,7 @@ export default class ConditionAction extends Component {
} }
} }
ConditionAction.defaultConfig = { ConditionAction.defaultConfig = Object.assign(
condition: 'state', { condition: 'state' },
...StateCondition.defaultConfig, StateCondition.defaultConfig,
}; );

View File

@ -12,10 +12,10 @@ export default class EventAction extends Component {
} }
serviceDataChanged(data) { serviceDataChanged(data) {
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, Object.assign(
...this.props.action, {}, this.props.action,
data, { data },
}); ));
} }
render({ action, localize }) { render({ action, localize }) {

View File

@ -12,10 +12,10 @@ export default class WaitAction extends Component {
// Gets fired on mount. If empty, onChangeEvent removes attribute. // Gets fired on mount. If empty, onChangeEvent removes attribute.
// Without the attribute this action is no longer matched to this component. // Without the attribute this action is no longer matched to this component.
onTemplateChange(ev) { onTemplateChange(ev) {
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, Object.assign(
...this.props.trigger, {}, this.props.trigger,
[ev.target.name]: ev.target.value, { [ev.target.name]: ev.target.value },
}); ));
} }
render({ action, localize }) { render({ action, localize }) {

View File

@ -13,10 +13,10 @@ export default class EventTrigger extends Component {
/* eslint-disable camelcase */ /* eslint-disable camelcase */
eventDataChanged(event_data) { eventDataChanged(event_data) {
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, Object.assign(
...this.props.trigger, {}, this.props.trigger,
event_data, { event_data },
}); ));
} }
render({ trigger, localize }) { render({ trigger, localize }) {

View File

@ -8,10 +8,10 @@ export default class HassTrigger extends Component {
} }
radioGroupPicked(ev) { radioGroupPicked(ev) {
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, Object.assign(
...this.props.trigger, {}, this.props.trigger,
event: ev.target.selected, { event: ev.target.selected },
}); ));
} }
/* eslint-disable camelcase */ /* eslint-disable camelcase */

View File

@ -12,10 +12,10 @@ export default class Trigger extends Component {
} }
addTrigger() { addTrigger() {
const trigger = this.props.trigger.concat({ const trigger = this.props.trigger.concat(Object.assign(
platform: 'state', { platform: 'state' },
...StateTrigger.defaultConfig, StateTrigger.defaultConfig,
}); ));
this.props.onChange(trigger); this.props.onChange(trigger);
} }

View File

@ -11,10 +11,10 @@ export default class NumericStateTrigger extends Component {
} }
entityPicked(ev) { entityPicked(ev) {
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, Object.assign(
...this.props.trigger, {}, this.props.trigger,
entity_id: ev.target.value, { entity_id: ev.target.value },
}); ));
} }
/* eslint-disable camelcase */ /* eslint-disable camelcase */

View File

@ -11,10 +11,10 @@ export default class StateTrigger extends Component {
} }
entityPicked(ev) { entityPicked(ev) {
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, Object.assign(
...this.props.trigger, {}, this.props.trigger,
entity_id: ev.target.value, { entity_id: ev.target.value },
}); ));
} }
/* eslint-disable camelcase */ /* eslint-disable camelcase */

View File

@ -11,10 +11,10 @@ export default class SunTrigger extends Component {
} }
radioGroupPicked(ev) { radioGroupPicked(ev) {
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, Object.assign(
...this.props.trigger, {}, this.props.trigger,
event: ev.target.selected, { event: ev.target.selected },
}); ));
} }
/* eslint-disable camelcase */ /* eslint-disable camelcase */

View File

@ -35,10 +35,10 @@ export default class TriggerEdit extends Component {
const type = ev.target.selectedItem.attributes.platform.value; const type = ev.target.selectedItem.attributes.platform.value;
if (type !== this.props.trigger.platform) { if (type !== this.props.trigger.platform) {
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, Object.assign(
platform: type, { platform: type },
...TYPES[type].defaultConfig TYPES[type].defaultConfig
}); ));
} }
} }

View File

@ -19,24 +19,24 @@ export default class ZoneTrigger extends Component {
} }
entityPicked(ev) { entityPicked(ev) {
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, Object.assign(
...this.props.trigger, {}, this.props.trigger,
entity_id: ev.target.value, { entity_id: ev.target.value },
}); ));
} }
zonePicked(ev) { zonePicked(ev) {
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, Object.assign(
...this.props.trigger, {}, this.props.trigger,
zone: ev.target.value, { zone: ev.target.value },
}); ));
} }
radioGroupPicked(ev) { radioGroupPicked(ev) {
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, Object.assign(
...this.props.trigger, {}, this.props.trigger,
event: ev.target.selected, { event: ev.target.selected },
}); ));
} }
/* eslint-disable camelcase */ /* eslint-disable camelcase */

View File

@ -91,7 +91,6 @@
"babel-loader": "^7.1.4", "babel-loader": "^7.1.4",
"babel-plugin-external-helpers": "^6.22.0", "babel-plugin-external-helpers": "^6.22.0",
"babel-plugin-syntax-dynamic-import": "^6.18.0", "babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-plugin-transform-react-jsx": "^6.24.1", "babel-plugin-transform-react-jsx": "^6.24.1",
"babel-preset-env": "^1.6.1", "babel-preset-env": "^1.6.1",
"chai": "^4.1.2", "chai": "^4.1.2",

View File

@ -15,6 +15,7 @@ import '@polymer/paper-radio-button/paper-radio-button.js';
import '@polymer/paper-radio-group/paper-radio-group.js'; import '@polymer/paper-radio-group/paper-radio-group.js';
import { html } from '@polymer/polymer/lib/utils/html-tag.js'; import { html } from '@polymer/polymer/lib/utils/html-tag.js';
import { PolymerElement } from '@polymer/polymer/polymer-element.js'; import { PolymerElement } from '@polymer/polymer/polymer-element.js';
import { h, render } from 'preact';
import '../../../src/components/entity/ha-entity-picker.js'; import '../../../src/components/entity/ha-entity-picker.js';
import '../../../src/components/ha-combo-box.js'; import '../../../src/components/ha-combo-box.js';
@ -24,13 +25,19 @@ import '../../../src/layouts/ha-app-layout.js';
import '../../../src/util/hass-mixins.js'; import '../../../src/util/hass-mixins.js';
import '../ha-config-js.js'; import '../ha-config-js.js';
import '../ha-config-section.js'; import '../ha-config-section.js';
import Automation from '../../../js/panel-config/automation.js';
import unmountPreact from '../../../js/common/preact/unmount.js';
function AutomationEditor(mountEl, props, mergeEl) {
return render(h(Automation, props), mountEl, mergeEl);
}
/* /*
* @appliesMixin window.hassMixins.LocalizeMixin * @appliesMixin window.hassMixins.LocalizeMixin
* @appliesMixin window.hassMixins.EventsMixin * @appliesMixin window.hassMixins.EventsMixin
*/ */
class HaAutomationEditor extends class HaAutomationEditor extends
window.hassMixins.LocalizeMixin(window.hassMixins.EventsMixin(PolymerElement)) { window.hassMixins.LocalizeMixin(window.hassMixins.NavigateMixin(PolymerElement)) {
static get template() { static get template() {
return html` return html`
<style include="ha-style"> <style include="ha-style">
@ -181,7 +188,7 @@ class HaAutomationEditor extends
disconnectedCallback() { disconnectedCallback() {
super.disconnectedCallback(); super.disconnectedCallback();
if (this._rendered) { if (this._rendered) {
window.unmountPreact(this._rendered); unmountPreact(this._rendered);
this._rendered = null; this._rendered = null;
} }
} }
@ -251,7 +258,7 @@ class HaAutomationEditor extends
if (this._renderScheduled || !this.hass || !this.config) return; if (this._renderScheduled || !this.hass || !this.config) return;
this._renderScheduled = true; this._renderScheduled = true;
Promise.resolve().then(() => { Promise.resolve().then(() => {
this._rendered = window.AutomationEditor(this.$.root, { this._rendered = AutomationEditor(this.$.root, {
automation: this.config, automation: this.config,
onChange: this.configChanged, onChange: this.configChanged,
isWide: this.isWide, isWide: this.isWide,
@ -268,8 +275,7 @@ class HaAutomationEditor extends
this.dirty = false; this.dirty = false;
if (this.creatingNew) { if (this.creatingNew) {
history.replaceState(null, null, '/config/automation/edit/' + id); this.navigate(`/config/automation/edit/${id}`, true);
this.fire('location-changed');
} }
}.bind(this), function (errors) { }.bind(this), function (errors) {
this.errors = errors.body.message; this.errors = errors.body.message;

View File

@ -3,7 +3,6 @@ import '@polymer/iron-media-query/iron-media-query.js';
import { html } from '@polymer/polymer/lib/utils/html-tag.js'; import { html } from '@polymer/polymer/lib/utils/html-tag.js';
import { PolymerElement } from '@polymer/polymer/polymer-element.js'; import { PolymerElement } from '@polymer/polymer/polymer-element.js';
import '../../build-temp/panel-config.js';
import '../../src/layouts/hass-error-screen.js'; import '../../src/layouts/hass-error-screen.js';
import '../../src/util/hass-mixins.js'; import '../../src/util/hass-mixins.js';
import './automation/ha-config-automation.js'; import './automation/ha-config-automation.js';

View File

@ -15,6 +15,7 @@ import '@polymer/paper-radio-button/paper-radio-button.js';
import '@polymer/paper-radio-group/paper-radio-group.js'; import '@polymer/paper-radio-group/paper-radio-group.js';
import { html } from '@polymer/polymer/lib/utils/html-tag.js'; import { html } from '@polymer/polymer/lib/utils/html-tag.js';
import { PolymerElement } from '@polymer/polymer/polymer-element.js'; import { PolymerElement } from '@polymer/polymer/polymer-element.js';
import { h, render } from 'preact';
import '../../../src/components/entity/ha-entity-picker.js'; import '../../../src/components/entity/ha-entity-picker.js';
import '../../../src/components/ha-combo-box.js'; import '../../../src/components/ha-combo-box.js';
@ -22,11 +23,18 @@ import '../../../src/layouts/ha-app-layout.js';
import '../../../src/util/hass-mixins.js'; import '../../../src/util/hass-mixins.js';
import '../ha-config-js.js'; import '../ha-config-js.js';
import '../ha-config-section.js'; import '../ha-config-section.js';
import Script from '../../../js/panel-config/script.js';
import unmountPreact from '../../../js/common/preact/unmount.js';
function ScriptEditor(mountEl, props, mergeEl) {
return render(h(Script, props), mountEl, mergeEl);
}
/* /*
* @appliesMixin window.hassMixins.LocalizeMixin * @appliesMixin window.hassMixins.LocalizeMixin
*/ */
class HaScriptEditor extends window.hassMixins.LocalizeMixin(PolymerElement) { class HaScriptEditor extends
window.hassMixins.LocalizeMixin(window.hassMixins.NavigateMixin(PolymerElement)) {
static get template() { static get template() {
return html` return html`
<style include="ha-style"> <style include="ha-style">
@ -174,7 +182,7 @@ class HaScriptEditor extends window.hassMixins.LocalizeMixin(PolymerElement) {
disconnectedCallback() { disconnectedCallback() {
super.disconnectedCallback(); super.disconnectedCallback();
if (this._rendered) { if (this._rendered) {
window.unmountPreact(this._rendered); unmountPreact(this._rendered);
this._rendered = null; this._rendered = null;
} }
} }
@ -242,7 +250,7 @@ class HaScriptEditor extends window.hassMixins.LocalizeMixin(PolymerElement) {
if (this._renderScheduled || !this.hass || !this.config) return; if (this._renderScheduled || !this.hass || !this.config) return;
this._renderScheduled = true; this._renderScheduled = true;
Promise.resolve().then(() => { Promise.resolve().then(() => {
this._rendered = window.ScriptEditor(this.$.root, { this._rendered = ScriptEditor(this.$.root, {
script: this.config, script: this.config,
onChange: this.configChanged, onChange: this.configChanged,
isWide: this.isWide, isWide: this.isWide,

View File

@ -29,7 +29,9 @@
"**/*.html", "**/*.html",
"**/ha-paper-slider.js", "**/ha-paper-slider.js",
"**/hass-mixins.js", "**/hass-mixins.js",
"**/ha-iconset-svg.js" "**/ha-iconset-svg.js",
"**/ha-script-editor.js",
"**/ha-automation-editor.js"
] ]
}, },
"builds": [ "builds": [

View File

@ -10,23 +10,23 @@ function createConfig(isProdBuild, latestBuild) {
publicPath = `/home-assistant-polymer/${buildPath}`; publicPath = `/home-assistant-polymer/${buildPath}`;
} }
const rules = []; const babelOptions = {
if (!latestBuild) { plugins: [
rules.push({ // Only support the syntax, Webpack will handle it.
test: /\.js$/, "syntax-dynamic-import",
use: { [
loader: 'babel-loader', 'transform-react-jsx',
options: { {
presets: [ pragma: 'h'
['es2015', { modules: false }]
],
plugins: [
// Only support the syntax, Webpack will handle it.
"syntax-dynamic-import"
],
} }
} ],
}); ],
};
if (!latestBuild) {
babelOptions.presets = [
['es2015', { modules: false }]
];
} }
const chunkFilename = isProdBuild ? const chunkFilename = isProdBuild ?
@ -39,7 +39,15 @@ function createConfig(isProdBuild, latestBuild) {
authorize: './src/auth/ha-authorize.js' authorize: './src/auth/ha-authorize.js'
}, },
module: { module: {
rules rules: [
{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: babelOptions,
},
}
]
}, },
output: { output: {
filename: '[name].js', filename: '[name].js',

View File

@ -2660,7 +2660,7 @@ babel-plugin-syntax-jsx@^6.8.0:
version "6.18.0" version "6.18.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-jsx/-/babel-plugin-syntax-jsx-6.18.0.tgz#0af32a9a6e13ca7a3fd5069e62d7b0f58d0d8946" resolved "https://registry.yarnpkg.com/babel-plugin-syntax-jsx/-/babel-plugin-syntax-jsx-6.18.0.tgz#0af32a9a6e13ca7a3fd5069e62d7b0f58d0d8946"
babel-plugin-syntax-object-rest-spread@^6.8.0: babel-plugin-syntax-object-rest-spread@^6.13.0, babel-plugin-syntax-object-rest-spread@^6.8.0:
version "6.13.0" version "6.13.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz#fd6536f2bce13836ffa3a5458c4903a597bb3bf5" resolved "https://registry.yarnpkg.com/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz#fd6536f2bce13836ffa3a5458c4903a597bb3bf5"