-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (60 loc) · 1.62 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
var _uuidCounter = 0;
function uuid() {
return "plugin-chart-".concat(++_uuidCounter);
}
function c3(id, bodyString) {
// bind to element
// body.bindto = '#' + id;
bodyString = bodyString.replace(/^\{/, "{\"bindto\":\"#".concat(id, "\","));
return "c3.generate(".concat(bodyString, ");");
}
function highcharts(id, bodyString) {
try {
var body = JSON.parse(bodyString); // http://www.highcharts.com/docs/getting-started/your-first-chart
body.chart = body.chart || {};
body.chart.renderTo = id;
return "new Highcharts.Chart(".concat(JSON.stringify(body), ");");
} catch (e) {
console.error(e);
}
}
function echarts (id, bodyString) {
try {
var body = bodyString || "{}";
return `echarts.init(document.getElementById(${JSON.stringify(id)})).setOption(${body})`;
} catch (e) {
console.error(e);
}
};
var chartFns = /*#__PURE__*/Object.freeze({
c3: c3,
highcharts: highcharts,
echarts: echarts
});
var chartScriptFn = function chartScriptFn() {};
module.exports = {
book: {
assets: './assets'
},
hooks: {
init: function init() {
var _this$config$get = this.config.get('pluginsConfig.charts'),
type = _this$config$get.type;
chartScriptFn = chartFns[type];
}
},
blocks: {
chart: {
process: function process(blk) {
var id = uuid();
var bodyString = blk.body.trim();
var scripts = chartScriptFn(id, bodyString);
return `<div>
<div class="plugin-chart" id="${id}"></div>
<script>${scripts}</script>
</div>`;
}
}
}
};