Commit 2cfd1ee7 authored by yann300's avatar yann300

Update pluginManager.js

parent 728c260e
'use strict' 'use strict'
/** /**
* Register and Manage plugin: * Register and Manage plugin:
*
* Plugin registration is done in the settings tab, * Plugin registration is done in the settings tab,
* using the following format: * using the following format:
* { * {
...@@ -8,6 +9,39 @@ ...@@ -8,6 +9,39 @@
* "url": "<plugin url>" * "url": "<plugin url>"
* } * }
* *
* structure of messages:
*
* - Notification sent by Remix:
*{
* action: 'notification',
* key: <string>,
* type: <string>,
* value: <array>
*}
*
* - Request sent by the plugin:
*{
* id: <number>,
* action: 'request',
* key: <string>,
* type: <string>,
* value: <array>
*}
*
* - Response sent by Remix and receive by the plugin:
*{
* id: <number>,
* action: 'response',
* key: <string>,
* type: <string>,
* value: <array>,
* error: (see below)
*}
* => The `error` property is `undefined` if no error happened.
* => In case of error (due to permission, system error, API error, etc...):
* error: { code, msg (optional), data (optional), stack (optional)
* => possible error code are still to be defined, but the generic one would be 500.
*
* Plugin receive 4 types of message: * Plugin receive 4 types of message:
* - focus (when he get focus) * - focus (when he get focus)
* - unfocus (when he loose focus - is hidden) * - unfocus (when he loose focus - is hidden)
...@@ -19,16 +53,20 @@ ...@@ -19,16 +53,20 @@
* CONFIG: * CONFIG:
* - getConfig(filename). The data to send should be formatted like: * - getConfig(filename). The data to send should be formatted like:
* { * {
* id: <requestid>,
* action: 'request',
* key: 'config',
* type: 'getConfig', * type: 'getConfig',
* arguments: ['filename.ext'], * value: ['filename.ext']
* id: <requestid>
* } * }
* the plugin will reveice a response like: * the plugin will reveice a response like:
* { * {
* id: <requestid>,
* action: 'response',
* key: 'config',
* type: 'getConfig', * type: 'getConfig',
* id: <requestid>
* error, * error,
* result * value: ['content of filename.ext']
* } * }
* same apply for the other call * same apply for the other call
* - setConfig(filename, content) * - setConfig(filename, content)
...@@ -47,12 +85,14 @@ class PluginManager { ...@@ -47,12 +85,14 @@ class PluginManager {
if (this.inFocus) { if (this.inFocus) {
// trigger to the current focus // trigger to the current focus
this.post(this.inFocus, JSON.stringify({ this.post(this.inFocus, JSON.stringify({
action: 'notification',
key: 'compiler',
type: 'compilationFinished', type: 'compilationFinished',
value: { value: [
success: success, success,
data: data, data,
source: source source
} ]
})) }))
} }
}) })
...@@ -61,37 +101,47 @@ class PluginManager { ...@@ -61,37 +101,47 @@ class PluginManager {
if (this.inFocus && this.inFocus !== tabName) { if (this.inFocus && this.inFocus !== tabName) {
// trigger unfocus // trigger unfocus
this.post(this.inFocus, JSON.stringify({ this.post(this.inFocus, JSON.stringify({
type: 'unfocus' action: 'notification',
key: 'app',
type: 'unfocus',
value: []
})) }))
} }
if (this.plugins[tabName]) { if (this.plugins[tabName]) {
// trigger focus // trigger focus
this.post(tabName, JSON.stringify({ this.post(tabName, JSON.stringify({
type: 'focus' action: 'notification',
key: 'app',
type: 'focus',
value: []
})) }))
this.inFocus = tabName this.inFocus = tabName
this.post(tabName, JSON.stringify({ this.post(tabName, JSON.stringify({
action: 'notification',
key: 'compiler',
type: 'compilationData', type: 'compilationData',
value: api.compiler.getCompilationResult() value: [api.compiler.getCompilationResult()]
})) }))
} }
}) })
window.addEventListener('message', (event) => { window.addEventListener('message', (event) => {
function response (type, callid, error, result) { function response (key, type, callid, error, result) {
self.post(self.inFocus, JSON.stringify({ self.post(self.inFocus, JSON.stringify({
id: callid, id: callid,
action: 'response',
key: key,
type: type, type: type,
error: error, error: error,
result: result value: [ result ]
})) }))
} }
if (event.type === 'message' && this.inFocus && this.plugins[this.inFocus] && this.plugins[this.inFocus].origin === event.origin) { if (event.type === 'message' && this.inFocus && this.plugins[this.inFocus] && this.plugins[this.inFocus].origin === event.origin) {
var data = JSON.parse(event.data) var data = JSON.parse(event.data)
data.arguments.unshift(this.inFocus) data.value.unshift(this.inFocus)
if (allowedapi[data.type]) { if (allowedapi[data.type]) {
data.arguments.push((error, result) => { data.value.push((error, result) => {
response(data.type, data.id, error, result) response(data.key, data.type, data.id, error, result)
}) })
api[data.key][data.type].apply({}, data.arguments) api[data.key][data.type].apply({}, data.arguments)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment