js2fml static method
Using the js package we can capture calls from javascript within flutter.
The js2fml(json) function is called from js and held in the context map
for use within flutter, passing its json as a map and allowing us in
dart/flutter to access it. This enables fml to be used through an iframe.
index.html loads the script from local.js file then listens to the .js postMessages() calls
//
window.addEventListener('message', function(event) {
var data;
try {
//
data = JSON.parse(event.data);
} catch(e) {}
try {
if (event.origin.startsWith('https://pad.fml.dev') && data && data.data && data.to) {
window.parent.postMessage({'data': data.data, 'from': event.origin, 'to': data.to});
}
else if (!event.origin.startsWith('vscode-webview://')) {
//
return;
}
else {
js2fml({'data': ${event.data}, 'from': ${event.origin}, 'to': 'fml'});
}
} catch(err) {
//
}
});
Basic implementation to show a template sent from js for vscode extension.
Next step: non-breaking refactor to expand the protocol and enhance fml2js
Implementation
/// Basic implementation to show a template sent from js for vscode extension.
/// Next step: non-breaking refactor to expand the protocol and enhance fml2js
static void js2fml() {
context['js2fml'] = (json) async {
// The script in index.html sets the data value that we assign to doc:
// `js2fml({'data': `${event.data}`, 'from': `${event.origin}`, 'to': 'fml'});`
String doc = json['data'];
WidgetModel? model = System();
EventManager.of(model)?.broadcastEvent(
model, Event(EventTypes.openjstemplate, parameters: {'templ8': doc}));
};
}