1
0
Fork 0
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:
Norwin 2018-12-05 13:57:21 +01:00
parent 4fba8d6900
commit a4e143adf3
2 changed files with 31 additions and 11 deletions

View file

@ -26,7 +26,7 @@
<script src="blockly/blockly/blockly_compressed.js"></script> <script src="blockly/blockly/blockly_compressed.js"></script>
<script src="blockly/blockly/blocks_compressed.js"></script> <script src="blockly/blockly/blocks_compressed.js"></script>
<script src="blockly/blockly/arduino_compressed.js"></script> <script src="blockly/blockly/arduino_compressed.js"></script>
<!-- To use the uncompressed version comment out the above and comment in the ones below <!-- To use the uncompressed version comment out the above and comment in the ones below
<script src="../blockly/blockly_uncompressed.js"></script> <script src="../blockly/blockly_uncompressed.js"></script>
<script src="../blockly/blocks/logic.js"></script> <script src="../blockly/blocks/logic.js"></script>
<script src="../blockly/blocks/loops.js"></script> <script src="../blockly/blocks/loops.js"></script>
@ -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>

View file

@ -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 => {
ionViewDidLoad() { const { type, data } = ev.data;
console.log('ionViewDidLoad SenseBoxPage'); switch (type) {
case 'sketch':
this.navCtrl.push(OtaWizardPage, { sketch: data })
break
default:
}
})
} }
launchOtaWizard () { launchOtaWizard () {
this.navCtrl.setRoot(OtaWizardPage) this.blocklyFrame.nativeElement.contentWindow.postMessage('getSketch', '*')
} }
} }