Skip to content

Commit e3905c0

Browse files
committed
Move the samples context out of the Vuepress plugin
"Utils" methods and chart defaults are now directly accessible under the /docs/samples folder, making easier to figure out from where some part of the samples snippets come from.
1 parent d86926e commit e3905c0

10 files changed

Lines changed: 111 additions & 68 deletions

File tree

docs/.vuepress/config.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,11 @@ module.exports = {
1717
],
1818
}],
1919
[ChartEditorPlugin, {
20-
defaults: {
21-
global: {
22-
legend: {
23-
display: false
24-
},
25-
title: {
26-
display: false
27-
},
28-
tooltips: {
29-
enabled: false
30-
}
31-
}
32-
}
20+
imports: [
21+
['samples/register.js'],
22+
['samples/defaults.js'],
23+
['samples/utils.js', 'Utils'],
24+
]
3325
}],
3426
],
3527
themeConfig: {

docs/.vuepress/plugins/chart-editor/components/ChartEditor.vue

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@
1313
</template>
1414

1515
<script>
16-
import Chart from 'chart.js';
17-
import * as Utils from '../utils';
16+
import {Chart} from 'chart.js';
1817
1918
// Components
2019
import ChartActions from './ChartActions.vue';
2120
import ChartView from './ChartView.vue';
2221
import CodeEditor from './CodeEditor.vue';
2322
24-
const CHART_DEFAULTS = Chart.helpers.merge({}, Chart.defaults);
25-
2623
export default {
2724
components: {
2825
ChartActions,
@@ -63,11 +60,31 @@ export default {
6360
return;
6461
}
6562
63+
const logger = {
64+
log(...args) {
65+
me.messages = [...me.messages, args.join(' ')].slice(-50);
66+
},
67+
};
68+
69+
const me = this;
70+
const context = {
71+
...(this.$chart || {}).imports,
72+
console: {
73+
...console,
74+
...logger,
75+
},
76+
Chart,
77+
};
78+
79+
const keys = Object.keys(context);
80+
const lines = keys.map((key) => {
81+
return `const ${key} = arguments[0].${key}`;
82+
});
83+
6684
const script = `
6785
'use strict';
68-
const Chart = arguments[0].Chart;
69-
const Utils = arguments[0].Utils;
7086
const module = {exports: {}};
87+
${lines.join(';\n')};
7188
(function(){ ${code} })();
7289
return module.exports;
7390
`;

docs/.vuepress/plugins/chart-editor/components/ChartView.vue

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
</template>
66

77
<script>
8-
import Chart from 'chart.js';
9-
import plugin from '../../../../../dist/chartjs-plugin-datalabels.js';
10-
11-
Chart.plugins.register(plugin);
8+
import {Chart} from 'chart.js';
129
1310
export default {
1411
methods: {

docs/.vuepress/plugins/chart-editor/components/CodeEditor.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ export default {
103103
},
104104
105105
watch: {
106+
output() {
107+
this.rebuild();
108+
},
106109
value: {
107110
immediate: true,
108111
handler() {

docs/.vuepress/plugins/chart-editor/index.js

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,65 @@
11
const {resolve} = require('path');
22

3-
module.exports = ({defaults}) => {
4-
function render(md) {
5-
const fence = md.renderer.rules.fence;
6-
md.renderer.rules.fence = (...args) => {
7-
const [tokens, idx] = args;
8-
const token = tokens[idx];
9-
const lang = token.info.trim();
3+
function render({renderer}) {
4+
const fence = renderer.rules.fence;
5+
renderer.rules.fence = (...args) => {
6+
const [tokens, idx] = args;
7+
const token = tokens[idx];
8+
const lang = token.info.trim();
109

11-
if (!(/ chart-editor( |$)/).test(lang)) {
12-
return fence(...args);
13-
}
10+
return (/ chart-editor( |$)/).test(lang) ?
11+
`<chart-editor :code="\`${token.content}\`"/>` :
12+
fence(...args);
13+
};
14+
}
15+
16+
function importsScripts(imports) {
17+
const names = imports.map(([, name]) => name).filter((name) => !!name);
18+
const lines = imports.map(([file, name]) => {
19+
const path = `@docs/${file}`;
20+
return name ?
21+
`import * as ${name} from '${path}'` :
22+
`import '${path}'`;
23+
});
1424

15-
return `<chart-editor :code="\`${token.content}\`"/>`;
16-
};
17-
}
25+
return `
26+
import Vue from 'vue';
27+
${lines.join(';\n')};
1828
29+
const imports = {
30+
${names.join(',\n')}
31+
}
32+
33+
Vue.mixin({
34+
created() {
35+
this.$chart = this.$chart || {};
36+
this.$chart.imports = imports;
37+
}
38+
})
39+
`;
40+
}
41+
42+
module.exports = ({imports}) => {
1943
return {
2044
name: 'vuepress-plugin-chart-editor',
2145
enhanceAppFiles: [
22-
{
23-
name: 'chart-defaults',
24-
content: `
25-
import Chart from 'chart.js';
26-
Chart.helpers.merge(Chart.defaults, ${JSON.stringify(defaults)});
27-
`
28-
},
2946
resolve(__dirname, 'global.js'),
3047
resolve(__dirname, 'enhancer.js'),
48+
{
49+
content: importsScripts(imports),
50+
name: 'chart-imports',
51+
}
3152
],
3253
chainWebpack: (config) => {
3354
config.merge({
3455
externals: {
3556
moment: 'moment',
3657
},
58+
resolve: {
59+
alias: {
60+
'@docs': resolve(__dirname, '../../../'),
61+
}
62+
}
3763
});
3864
},
3965
chainMarkdown: (config) => {

docs/samples/defaults.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {defaults, helpers} from 'chart.js';
2+
3+
const {merge} = helpers;
4+
5+
merge(defaults.global, {
6+
legend: {
7+
display: false
8+
},
9+
title: {
10+
display: false
11+
},
12+
tooltips: {
13+
enabled: false
14+
}
15+
});

docs/samples/events/listeners.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function log(type, context) {
1818
var j = context.dataIndex;
1919
var v = context.dataset.data[j];
2020

21-
Utils.log(type + ': ' + i + '-' + j + ' (' + v + ')')
21+
console.log(type + ': ' + i + '-' + j + ' (' + v + ')')
2222
}
2323
// </block:setup>
2424

docs/samples/events/selection.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function isSelected(context) {
3434
}
3535

3636
function log(selected) {
37-
Utils.log('selection: ' + selected.map(function(item) {
37+
console.log('selection: ' + selected.map(function(item) {
3838
return item.value;
3939
}).join(', '));
4040
}

docs/samples/register.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {plugins} from 'chart.js';
2+
import plugin from '../../dist/chartjs-plugin-datalabels.js';
3+
4+
plugins.register(plugin);

docs/.vuepress/plugins/chart-editor/utils.js renamed to docs/samples/utils.js

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function fallback(/* values ... */) {
1313

1414
var Color = typeof window !== 'undefined' ? window.Color : {};
1515

16-
var COLORS = [
16+
export var COLORS = [
1717
'#FF3784',
1818
'#36A2EB',
1919
'#4BC0C0',
@@ -28,19 +28,20 @@ var COLORS = [
2828
];
2929

3030
// Adapted from http://indiegamr.com/generate-repeatable-random-numbers-in-js/
31-
function srand(seed) {
32-
this._seed = seed;
31+
var _seed = Date.now();
32+
33+
export function srand(seed) {
34+
_seed = seed;
3335
}
3436

35-
function rand(min, max) {
36-
var seed = this._seed;
37+
export function rand(min, max) {
3738
min = min === undefined ? 0 : min;
3839
max = max === undefined ? 1 : max;
39-
this._seed = (seed * 9301 + 49297) % 233280;
40-
return min + (this._seed / 233280) * (max - min);
40+
_seed = (_seed * 9301 + 49297) % 233280;
41+
return min + (_seed / 233280) * (max - min);
4142
}
4243

43-
function numbers(config) {
44+
export function numbers(config) {
4445
var cfg = config || {};
4546
var min = fallback(cfg.min, 0);
4647
var max = fallback(cfg.max, 1);
@@ -64,13 +65,13 @@ function numbers(config) {
6465
return data;
6566
}
6667

67-
function color(offset) {
68+
export function color(offset) {
6869
var count = COLORS.length;
6970
var index = offset === undefined ? ~~rand(0, count) : offset;
7071
return COLORS[index % count];
7172
}
7273

73-
function colors(config) {
74+
export function colors(config) {
7475
var cfg = config || {};
7576
var c = cfg.color || color(0);
7677
var count = cfg.count !== undefined ? cfg.count : 8;
@@ -93,19 +94,7 @@ function colors(config) {
9394
return values;
9495
}
9596

96-
function transparentize(c, opacity) {
97+
export function transparentize(c, opacity) {
9798
var alpha = opacity === undefined ? 0.5 : 1 - opacity;
9899
return Color(c).alpha(alpha).rgbString();
99100
}
100-
101-
srand(Date.now());
102-
103-
module.exports = {
104-
color: color,
105-
colors: colors,
106-
COLORS: COLORS,
107-
numbers: numbers,
108-
rand: rand,
109-
srand: srand,
110-
transparentize: transparentize,
111-
};

0 commit comments

Comments
 (0)