mirror of
https://github.com/sensebox/blockly-app
synced 2025-04-19 16:07:33 +02:00
implement communication between angular and blockly
via postMessage
This commit is contained in:
parent
4fba8d6900
commit
a4e143adf3
2 changed files with 31 additions and 11 deletions
|
@ -360,6 +360,21 @@
|
||||||
window.removeEventListener('load', load, false);
|
window.removeEventListener('load', load, false);
|
||||||
Ardublockly.init();
|
Ardublockly.init();
|
||||||
SenseboxExtension.init();
|
SenseboxExtension.init();
|
||||||
|
|
||||||
|
window.addEventListener('message', function load(event) {
|
||||||
|
switch (event.data) {
|
||||||
|
case 'getSketch':
|
||||||
|
var code = Ardublockly.generateArduino();
|
||||||
|
event.source.postMessage({
|
||||||
|
type: 'sketch',
|
||||||
|
data: code,
|
||||||
|
}, event.origin)
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.warn(`postMessage type ${event.data} not implemented`)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Component, OnInit } from '@angular/core';
|
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
|
||||||
import { IonicPage, NavController, NavParams } from 'ionic-angular';
|
import { IonicPage, NavController, NavParams } from 'ionic-angular';
|
||||||
import { OtaWizardPage } from '../ota-wizard/ota-wizard';
|
import { OtaWizardPage } from '../ota-wizard/ota-wizard';
|
||||||
|
|
||||||
|
@ -9,21 +9,26 @@ import { OtaWizardPage } from '../ota-wizard/ota-wizard';
|
||||||
templateUrl: 'blockly.html',
|
templateUrl: 'blockly.html',
|
||||||
})
|
})
|
||||||
export class BlocklyPage implements OnInit{
|
export class BlocklyPage implements OnInit{
|
||||||
|
@ViewChild('blocklyFrame') blocklyFrame: ElementRef
|
||||||
|
|
||||||
ngOnInit(){
|
ngOnInit() { }
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public navCtrl: NavController,
|
public navCtrl: NavController,
|
||||||
public navParams: NavParams) {
|
public navParams: NavParams
|
||||||
|
) {
|
||||||
|
window.addEventListener('message', ev => {
|
||||||
|
const { type, data } = ev.data;
|
||||||
|
switch (type) {
|
||||||
|
case 'sketch':
|
||||||
|
this.navCtrl.push(OtaWizardPage, { sketch: data })
|
||||||
|
break
|
||||||
|
default:
|
||||||
}
|
}
|
||||||
|
})
|
||||||
ionViewDidLoad() {
|
|
||||||
console.log('ionViewDidLoad SenseBoxPage');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
launchOtaWizard () {
|
launchOtaWizard () {
|
||||||
this.navCtrl.setRoot(OtaWizardPage)
|
this.blocklyFrame.nativeElement.contentWindow.postMessage('getSketch', '*')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue