Commit 7e600024 authored by rockyl's avatar rockyl

Merge branch 'mvvm' into code-diff

# Conflicts:
#	src/api/common.js
#	src/config.js
#	src/store/modules/project.js
parents 7cb7b6c1 f05589e1
...@@ -24,11 +24,8 @@ class ApiError extends Error { ...@@ -24,11 +24,8 @@ class ApiError extends Error {
} }
} }
export async function fetchApi(uri, {params, method = 'get', auth = true, judgeSuccess = true, contentType = 'json', errMessage} = { export async function fetchApi(uri, {host = '', params, method = 'get', auth = true, judgeSuccess = true, contentType = 'json', dataField = 'data', errMessage}) {
method: 'get', let url = host + (uri.startsWith('http') || uri.startsWith('//') ? uri : API_HOST + uri);
contentType: 'json'
}) {
let url = uri.startsWith('http') || uri.startsWith('//') ? uri : API_HOST + uri;
const options = { const options = {
method, method,
...@@ -69,24 +66,19 @@ export async function fetchApi(uri, {params, method = 'get', auth = true, judgeS ...@@ -69,24 +66,19 @@ export async function fetchApi(uri, {params, method = 'get', auth = true, judgeS
} }
const response = await fetch(url, options); const response = await fetch(url, options);
const respText = await response.text(); if (response.status === 401) {
location.href = '/admin/permission';
if (response.status === 310) { //客户端重定向,用于跨域重定向
location.href = respText;
} }
const jsonObj = await response.json();
//console.log(jsonObj);
try {
let jsonObj = JSON.parse(respText);
if (judgeSuccess) { if (judgeSuccess) {
if (jsonObj.success) { if (jsonObj.success) {
return jsonObj.data; return jsonObj[dataField];
} }
} else { } else {
return jsonObj; return jsonObj;
} }
return Promise.reject(new ApiError('call api failed', jsonObj.code, errMessage, jsonObj.details)); throw new ApiError('call api failed', jsonObj.code, errMessage, jsonObj.details);
}catch (e) {
return Promise.reject(e);
}
} }
...@@ -23,5 +23,10 @@ export default new Router({ ...@@ -23,5 +23,10 @@ export default new Router({
name: 'preview', name: 'preview',
component: () => import('./views/Preview.vue') component: () => import('./views/Preview.vue')
}, },
{
path: '/diff',
name: 'diff',
component: () => import('./views/ProjectDiff.vue')
},
] ]
}) })
...@@ -7,7 +7,15 @@ import {editorApi, projectApi} from "../../api" ...@@ -7,7 +7,15 @@ import {editorApi, projectApi} from "../../api"
import path from "path" import path from "path"
import generateUUID from "uuid/v4" import generateUUID from "uuid/v4"
import {flattenViews, getCmpByUUID, getCmpProps} from '../../utils/common' import {flattenViews, getCmpByUUID, getCmpProps} from '../../utils/common'
import {assetScheme, clonePureObj, findProcess, getMockServeEnabled, saveAs, traverseViewNode} from "../../utils" import {
assetScheme,
clonePureObj, deleteAssetsDepConfig,
deleteDesignConfig,
findProcess,
getMockServeEnabled,
saveAs,
traverseViewNode
} from "../../utils"
import {template} from "../../template" import {template} from "../../template"
import events from "@/global-events" import events from "@/global-events"
import {packImages} from "../../utils/sheet-pack" import {packImages} from "../../utils/sheet-pack"
...@@ -20,7 +28,6 @@ import {fetchApi} from "../../api/common" ...@@ -20,7 +28,6 @@ import {fetchApi} from "../../api/common"
import {toZeroing} from "psd-parse-web" import {toZeroing} from "psd-parse-web"
import {arrayFind} from "element-ui/src/utils/util"; import {arrayFind} from "element-ui/src/utils/util";
const storeName = 'project'; const storeName = 'project';
const psStoreName = 'pack-history'; const psStoreName = 'pack-history';
...@@ -47,47 +54,6 @@ const defaultOptions = { ...@@ -47,47 +54,6 @@ const defaultOptions = {
const OPERATE_MAX_LENGTH = 200; // 撤销重做栈最大值 const OPERATE_MAX_LENGTH = 200; // 撤销重做栈最大值
function getDefaultOptions() {
return clonePureObj(defaultOptions)
}
function setUUIDForAllChildren(node) {
if (node.children && node.children.length > 0) {
for (let i = 0; i < node.children.length; i++) {
node.children[i] = copyBaseRoot(node.children[i]);
setUUIDForAllChildren(node.children[i])
}
}
}
let copyNodeCatch = null;
function copyBaseRoot(node) {
let _node = JSON.parse(JSON.stringify(node));
let data;
if (_node.children && _node.children.length > 0) {
data = {
name: _node.name,
type: _node.type,
properties: _node.properties,
events: _node.events,
uuid: generateUUID(),
children: _node.children
};
} else {
data = {
name: _node.name,
type: _node.type,
properties: _node.properties,
events: _node.events,
uuid: generateUUID(),
};
}
return data
}
export const projectStore = { export const projectStore = {
modules: { modules: {
package: packageStore, package: packageStore,
...@@ -383,8 +349,7 @@ export const projectStore = { ...@@ -383,8 +349,7 @@ export const projectStore = {
parentChildren.splice(index, 1); parentChildren.splice(index, 1);
}, },
copyNode(state, {node, parentNode, copyState}) {
copyNode(state, {node, parentNode,copyState}) {
let _node1 = node; let _node1 = node;
console.log(_node1) console.log(_node1)
copyNodeCatch =clonePureObj(_node1) copyNodeCatch =clonePureObj(_node1)
...@@ -506,9 +471,12 @@ export const projectStore = { ...@@ -506,9 +471,12 @@ export const projectStore = {
getters: { getters: {
project(state) { project(state) {
const {id, name, creator, data, update_time} = state; const {id, name, creator, data, update_time} = state;
let newData = clonePureObj(data);
deleteDesignConfig(newData.processes);
deleteAssetsDepConfig(newData);
return { return {
id, name, creator, update_time, id, name, creator, update_time,
data: JSON.stringify(data), data: JSON.stringify(newData),
}; };
}, },
menuBadge: (state) => (key) => { menuBadge: (state) => (key) => {
...@@ -626,7 +594,7 @@ export const projectStore = { ...@@ -626,7 +594,7 @@ export const projectStore = {
}, },
actions: { actions: {
async saveToLocal({getters, commit}) { async saveToLocal({getters, commit}) {
const {project} = getters; const project = getters.project;
await db.set(storeName, {id: project.id, data: JSON.stringify(project)}); await db.set(storeName, {id: project.id, data: JSON.stringify(project)});
//localStorage.setItem('project-' + project.id, JSON.stringify(project)); //localStorage.setItem('project-' + project.id, JSON.stringify(project));
...@@ -674,9 +642,7 @@ export const projectStore = { ...@@ -674,9 +642,7 @@ export const projectStore = {
} }
}, },
async saveToRemote({state, dispatch, getters}, {remark}) { async saveToRemote({state, dispatch, getters}, {remark}) {
console.log(getters.project); await projectApi.saveOne(getters.project, remark);
let result = await projectApi.saveOne(getters.project, remark);
console.log(result);
dispatch('deleteLocalVersion', state.id); dispatch('deleteLocalVersion', state.id);
}, },
async updateProject({commit}, projectID) { async updateProject({commit}, projectID) {
...@@ -867,7 +833,7 @@ export const projectStore = { ...@@ -867,7 +833,7 @@ export const projectStore = {
await db.remove(psStoreName, id); await db.remove(psStoreName, id);
}, },
savePreview({state, rootState, getters}) { savePreview({state, rootState, getters}) {
const {project} = getters; const project = getters.project;
const packages = state.package.packages; const packages = state.package.packages;
/*const data = { /*const data = {
...@@ -877,7 +843,7 @@ export const projectStore = { ...@@ -877,7 +843,7 @@ export const projectStore = {
localStorage.setItem('preview-project-' + project.id, JSON.stringify(data)); localStorage.setItem('preview-project-' + project.id, JSON.stringify(data));
localStorage.setItem('preview-ts', Date.now().toString());*/ localStorage.setItem('preview-ts', Date.now().toString());*/
preprocess(project, state.data, clonePureObj(packages)); preprocess(project, clonePureObj(packages));
}, },
addBehaviorDirect({state}, {behaviors, alias}) { addBehaviorDirect({state}, {behaviors, alias}) {
...@@ -967,3 +933,45 @@ export async function updateMock(mocks) { ...@@ -967,3 +933,45 @@ export async function updateMock(mocks) {
db.set('mock', mock); db.set('mock', mock);
} }
} }
function getDefaultOptions() {
return clonePureObj(defaultOptions)
}
function setUUIDForAllChildren(node) {
if (node.children && node.children.length > 0) {
for (let i = 0; i < node.children.length; i++) {
node.children[i] = copyBaseRoot(node.children[i]);
setUUIDForAllChildren(node.children[i])
}
}
}
let copyNodeCatch = null;
function copyBaseRoot(node) {
let _node = JSON.parse(JSON.stringify(node));
let data;
if (_node.children && _node.children.length > 0) {
data = {
name: _node.name,
type: _node.type,
properties: _node.properties,
events: _node.events,
uuid: generateUUID(),
children: _node.children
};
} else {
data = {
name: _node.name,
type: _node.type,
properties: _node.properties,
events: _node.events,
uuid: generateUUID(),
};
}
return data
}
...@@ -93,6 +93,9 @@ export function guid() { ...@@ -93,6 +93,9 @@ export function guid() {
* @param parent * @param parent
*/ */
export function traverseViewNode(nodes, callback, parent) { export function traverseViewNode(nodes, callback, parent) {
if (!nodes) {
return;
}
for (let node of nodes) { for (let node of nodes) {
callback(node, parent); callback(node, parent);
if (node.children && node.children.length > 0) { if (node.children && node.children.length > 0) {
...@@ -400,3 +403,24 @@ export function getViewNodePath(node, sep = '/') { ...@@ -400,3 +403,24 @@ export function getViewNodePath(node, sep = '/') {
export function timeFormat(time, format = 'YYYY-MM-DD HH:mm:ss') { export function timeFormat(time, format = 'YYYY-MM-DD HH:mm:ss') {
return moment(time).format(format) return moment(time).format(format)
} }
export function deleteDesignConfig(processes) {
for (let process of processes) {
if (process.sub) {
for (let key in process.sub) {
let p = process.sub[key];
delete p.design['input'];
delete p.design['output'];
}
}
if (process.metas) {
deleteDesignConfig(process.metas);
}
}
}
export function deleteAssetsDepConfig(data) {
for (let asset of data.assets) {
delete asset['dep'];
}
}
...@@ -24,9 +24,9 @@ function getPackages(packages) { ...@@ -24,9 +24,9 @@ function getPackages(packages) {
} }
} }
export async function preprocess(project, data, packages) { export async function preprocess(project, packages) {
let newData = clonePureObj(data); let data = JSON.parse(project.data);
const codes = await divideCode(newData, { const codes = await divideCode(data, {
debug: true, debug: true,
getPackages: getPackages(packages), getPackages: getPackages(packages),
dependencies: data.dependencies, dependencies: data.dependencies,
...@@ -50,7 +50,7 @@ export async function preprocess(project, data, packages) { ...@@ -50,7 +50,7 @@ export async function preprocess(project, data, packages) {
scripts.push(newScriptContent('var proxy_mode=true;')); scripts.push(newScriptContent('var proxy_mode=true;'));
codesCache = codes; codesCache = codes;
const dataUrl = URL.createObjectURL(new Blob([JSON.stringify(newData)])); const dataUrl = URL.createObjectURL(new Blob([JSON.stringify(data)]));
const {libraryScriptElMap, analyseResult} = await generateLibraryScriptEl(tpl, dependencies, getPackages(packages), '//yun.duiba.com.cn/editor/zeroing/libs/'); const {libraryScriptElMap, analyseResult} = await generateLibraryScriptEl(tpl, dependencies, getPackages(packages), '//yun.duiba.com.cn/editor/zeroing/libs/');
...@@ -65,7 +65,7 @@ export async function preprocess(project, data, packages) { ...@@ -65,7 +65,7 @@ export async function preprocess(project, data, packages) {
db.set(storeName, { db.set(storeName, {
id: project.id, id: project.id,
data: newData, data,
tpl, tpl,
}); });
localStorage.setItem('preview-ts', Date.now().toString()); localStorage.setItem('preview-ts', Date.now().toString());
......
<template>
<monaco-editor
class="script-editor"
ref="editor"
v-model="code"
language="json"
:options="monacoEditorOptions"
diff-editor
:original="originalCode"
/>
</template>
<script>
import MonacoEditor from "vue-monaco";
import {monacoEditorOptions} from "../utils";
//import old from './old.json'
//let originalCode = JSON.stringify(old, null, '\t');
let originalCode = `hello`;
console.log(originalCode);
export default {
name: "ProjectDiff",
components: {MonacoEditor},
data() {
return {
monacoEditorOptions,
code: '',
originalCode,
}
},
mounted() {
let editor = this.$refs.editor.getEditor();
editor.onDidUpdateDiff(()=>{
let m = editor.getLineChanges();
console.log(m);
})
}
}
</script>
<style scoped>
.script-editor{
width: 100%;
}
</style>
\ No newline at end of file
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