1 | import Vue from 'vue/dist/vue.esm';
|
2 | import Vuex from 'vuex';
|
3 | import userConfig from 'manifest';
|
4 | import clonedeep from 'lodash.clonedeep';
|
5 | import demoList from '.demoList.json';
|
6 | import router from '@/js/router.js';
|
7 | import bus from '@/js/eventbus.js';
|
8 |
|
9 | let config = userConfig;
|
10 | if(typeof config === 'function') {
|
11 | config = config(process.env.NODE_ENV);
|
12 | }
|
13 |
|
14 | config = Object.assign({
|
15 | name: 'DEMOSIFY',
|
16 | version: 'v1',
|
17 | homePage: 'https://github.com/betseyliu/demo-ground',
|
18 | logo: '',
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | boxTheme: 'monokai',
|
29 | globalPackages: {
|
30 | js: [],
|
31 | css: [],
|
32 | },
|
33 |
|
34 | editorViewMode: 'tab',
|
35 | }, config);
|
36 |
|
37 | import progress from 'nprogress';
|
38 | progress.configure({
|
39 | showSpinner: false,
|
40 | });
|
41 |
|
42 | const path = require('path');
|
43 |
|
44 | Vue.use(Vuex);
|
45 |
|
46 | const demoBoxes = {};
|
47 |
|
48 | function importAllDemo(r) {
|
49 | r.keys().forEach(key => {
|
50 | const name = /^\.\/(.+)\//.exec(key)[1];
|
51 | demoBoxes[name] = r(key).default;
|
52 | })
|
53 | }
|
54 | importAllDemo(require.context('demos', true, /config.js$/));
|
55 |
|
56 | const links = demoList.map((link) => {
|
57 | if(typeof link === 'string') {
|
58 | return {label: link, src: `/${link}`};
|
59 | }
|
60 | return link;
|
61 | });
|
62 |
|
63 | const state = {
|
64 | config,
|
65 | boxes: {},
|
66 | foldBoxes: [],
|
67 | links,
|
68 | iframeStatus: null,
|
69 | transforming: false,
|
70 | isSidebarShown: true,
|
71 | autoRun: true,
|
72 | logs: [],
|
73 | currentBox: undefined,
|
74 | dependencies: {
|
75 | js: [],
|
76 | css: [],
|
77 | },
|
78 | };
|
79 |
|
80 | const mutations = {
|
81 | CLEAR_BOXES(state) {
|
82 | state.boxes = {};
|
83 | },
|
84 | UPDATE_KEY(state, {type, key}) {
|
85 | if(!state.boxes[type]) state.boxes[type] = {};
|
86 | state.boxes[type].key = key;
|
87 | },
|
88 | UPDATE_CODE(state, {type, code}) {
|
89 | if(!state.boxes[type]) state.boxes[type] = {};
|
90 | state.boxes[type].code = code;
|
91 | if(state.autoRun) bus.$emit('run');
|
92 | },
|
93 | UPDATE_TRANSFORM(state, {type, transform}) {
|
94 | if(!state.boxes[type]) state.boxes[type] = {};
|
95 | state.boxes[type].transform = transform;
|
96 | },
|
97 | UPDATE_TRANSFORMER(state, {type, transformer}) {
|
98 | if(!state.boxes[type]) state.boxes[type] = {};
|
99 | state.boxes[type].transformer = transformer;
|
100 | },
|
101 | UPDATE_EDITOR_HOOK(state, {type, editorHook}) {
|
102 | if(!state.boxes[type]) state.boxes[type] = {};
|
103 | state.boxes[type].editorHook = editorHook;
|
104 | },
|
105 | UPDATE_FOLD_BOXES(state, foldBoxes) {
|
106 | state.foldBoxes = foldBoxes;
|
107 | },
|
108 | UPDATE_VISIBLE(state, {type, visible}) {
|
109 | if(!state.boxes[type]) state.boxes[type] = {};
|
110 | state.boxes[type].visible = visible;
|
111 | },
|
112 | TOGGLE_BOX_FOLD(state, boxName) {
|
113 | const boxIndex = state.foldBoxes.indexOf(boxName);
|
114 | if(boxIndex > -1) {
|
115 | state.foldBoxes.splice(boxIndex, 1);
|
116 | } else {
|
117 | state.foldBoxes.push(boxName);
|
118 | }
|
119 | },
|
120 | SET_IFRAME_STATUS(state, status) {
|
121 | state.iframeStatus = status
|
122 | },
|
123 | SET_TRANSFORM(state, status) {
|
124 | state.transforming = status
|
125 | },
|
126 | TOGGLE_AUTO_RUN(state) {
|
127 | state.autoRun = !state.autoRun;
|
128 | },
|
129 | CLEAR_LOGS(state) {
|
130 | state.logs = [];
|
131 | },
|
132 | ADD_LOG(state, log) {
|
133 | state.logs.push(log);
|
134 | },
|
135 | UPDATE_DEPENDENCIES(state, dependencies = {js: [], css: []}) {
|
136 | state.dependencies = dependencies;
|
137 | },
|
138 | TOGGLE_SIDEBAR(state) {
|
139 | state.isSidebarShown = !state.isSidebarShown;
|
140 | },
|
141 | UPDATE_CURRENT_BOX(state, box) {
|
142 | state.currentBox = box;
|
143 | },
|
144 | };
|
145 |
|
146 | const actions = {
|
147 | clearBoxes({commit}) {
|
148 | commit('CLEAR_BOXES');
|
149 | },
|
150 | updateKey({commit}, pl) {
|
151 | commit('UPDATE_KEY', pl);
|
152 | },
|
153 | updateCode({commit}, pl) {
|
154 | commit('UPDATE_CODE', pl);
|
155 | },
|
156 | updateTransformer({commit}, pl) {
|
157 | commit('UPDATE_TRANSFORMER', pl);
|
158 | },
|
159 | updateTransform({commit}, pl) {
|
160 | commit('UPDATE_TRANSFORM', pl);
|
161 | },
|
162 | updateEditorHook({commit}, pl) {
|
163 | commit('UPDATE_EDITOR_HOOK', pl);
|
164 | },
|
165 | updateFoldBoxes({commit}, pl) {
|
166 | commit('UPDATE_FOLD_BOXES', pl);
|
167 | },
|
168 | updateVisible({commit}, pl) {
|
169 | commit('UPDATE_VISIBLE', pl)
|
170 | },
|
171 | toggleBoxFold({commit}, pl) {
|
172 | commit('TOGGLE_BOX_FOLD', pl);
|
173 | },
|
174 | toogleAutoRun({commit}) {
|
175 | commit('TOGGLE_AUTO_RUN');
|
176 | },
|
177 | updateDependencies({commit}, pl) {
|
178 | commit('UPDATE_DEPENDENCIES', pl);
|
179 | },
|
180 | updateCurrentBox({commit}, pl) {
|
181 | commit('UPDATE_CURRENT_BOX', pl);
|
182 | },
|
183 | async setBoxes({dispatch}, demo) {
|
184 | progress.start();
|
185 |
|
186 | let demoID;
|
187 | if(!demoBoxes[demo]) {
|
188 | router.push({path : '/404'});
|
189 | progress.done();
|
190 | return;
|
191 | }
|
192 | if(typeof demo === 'string') {
|
193 | demoID = demo;
|
194 | demo = await demoBoxes[demo]();
|
195 | } else {
|
196 | demoID = 'demo-' + Math.random().toString(16).slice(2, 14);
|
197 | }
|
198 |
|
199 |
|
200 | const{foldBoxes, visibleBoxes, packages, ...boxes} = demo;
|
201 |
|
202 | const ac = [];
|
203 |
|
204 | dispatch('clearBoxes');
|
205 |
|
206 | Object.entries(boxes).forEach(([type, {code, transformer, visible, transform, editorHook}]) => {
|
207 | transform = transform || function(code) {return code};
|
208 | ac.push(
|
209 | dispatch('updateKey', {type, key: demoID}),
|
210 | dispatch('updateCode', { type, code: code.default }),
|
211 | dispatch('updateTransformer', { type, transformer }),
|
212 | dispatch('updateTransform', { type, transform }),
|
213 | dispatch('updateVisible', {type, visible: Boolean(visible)}),
|
214 | dispatch('updateEditorHook', {type, editorHook}),
|
215 | );
|
216 | })
|
217 |
|
218 |
|
219 | const dependencies = {
|
220 | js: [
|
221 | ...(config.globalPackages.js || []) ,
|
222 | ...(packages.js || []),
|
223 | ],
|
224 | css: [
|
225 | ...(config.globalPackages.css || []) ,
|
226 | ...(packages.css || []),
|
227 | ],
|
228 | };
|
229 |
|
230 | ac.push(dispatch('updateFoldBoxes', foldBoxes || []));
|
231 | ac.push(dispatch('updateDependencies', dependencies));
|
232 | ac.push(dispatch('updateCurrentBox', Object.entries(boxes).find(([key, value]) => value.visible)[0]|| undefined));
|
233 | await Promise.all(ac);
|
234 | progress.done();
|
235 | },
|
236 | setIframeStatus({ commit }, status) {
|
237 | commit('SET_IFRAME_STATUS', status)
|
238 | },
|
239 | transform({ commit }, status) {
|
240 | commit('SET_TRANSFORM', status)
|
241 | },
|
242 | clearLogs({commit}) {
|
243 | commit('CLEAR_LOGS');
|
244 | },
|
245 | addLog({commit}, pl) {
|
246 | commit('ADD_LOG', pl)
|
247 | },
|
248 | }
|
249 |
|
250 | const store = new Vuex.Store({
|
251 | state,
|
252 | mutations,
|
253 | actions,
|
254 | });
|
255 |
|
256 | bus.$on('setBoxes', (demo) => {store.dispatch('setBoxes', demo)})
|
257 |
|
258 |
|
259 | export default store;
|