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 {
}
}
export async function fetchApi(uri, {params, method = 'get', auth = true, judgeSuccess = true, contentType = 'json', errMessage} = {
method: 'get',
contentType: 'json'
}) {
let url = uri.startsWith('http') || uri.startsWith('//') ? uri : API_HOST + uri;
export async function fetchApi(uri, {host = '', params, method = 'get', auth = true, judgeSuccess = true, contentType = 'json', dataField = 'data', errMessage}) {
let url = host + (uri.startsWith('http') || uri.startsWith('//') ? uri : API_HOST + uri);
const options = {
method,
......@@ -69,24 +66,19 @@ export async function fetchApi(uri, {params, method = 'get', auth = true, judgeS
}
const response = await fetch(url, options);
const respText = await response.text();
if (response.status === 310) { //客户端重定向,用于跨域重定向
location.href = respText;
if (response.status === 401) {
location.href = '/admin/permission';
}
const jsonObj = await response.json();
//console.log(jsonObj);
try {
let jsonObj = JSON.parse(respText);
if (judgeSuccess) {
if (jsonObj.success) {
return jsonObj.data;
return jsonObj[dataField];
}
} else {
return jsonObj;
}
return Promise.reject(new ApiError('call api failed', jsonObj.code, errMessage, jsonObj.details));
}catch (e) {
return Promise.reject(e);
}
throw new ApiError('call api failed', jsonObj.code, errMessage, jsonObj.details);
}
......@@ -23,5 +23,10 @@ export default new Router({
name: 'preview',
component: () => import('./views/Preview.vue')
},
{
path: '/diff',
name: 'diff',
component: () => import('./views/ProjectDiff.vue')
},
]
})
......@@ -7,7 +7,15 @@ import {editorApi, projectApi} from "../../api"
import path from "path"
import generateUUID from "uuid/v4"
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 events from "@/global-events"
import {packImages} from "../../utils/sheet-pack"
......@@ -20,7 +28,6 @@ import {fetchApi} from "../../api/common"
import {toZeroing} from "psd-parse-web"
import {arrayFind} from "element-ui/src/utils/util";
const storeName = 'project';
const psStoreName = 'pack-history';
......@@ -47,47 +54,6 @@ const defaultOptions = {
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 = {
modules: {
package: packageStore,
......@@ -383,8 +349,7 @@ export const projectStore = {
parentChildren.splice(index, 1);
},
copyNode(state, {node, parentNode,copyState}) {
copyNode(state, {node, parentNode, copyState}) {
let _node1 = node;
console.log(_node1)
copyNodeCatch =clonePureObj(_node1)
......@@ -506,9 +471,12 @@ export const projectStore = {
getters: {
project(state) {
const {id, name, creator, data, update_time} = state;
let newData = clonePureObj(data);
deleteDesignConfig(newData.processes);
deleteAssetsDepConfig(newData);
return {
id, name, creator, update_time,
data: JSON.stringify(data),
data: JSON.stringify(newData),
};
},
menuBadge: (state) => (key) => {
......@@ -626,7 +594,7 @@ export const projectStore = {
},
actions: {
async saveToLocal({getters, commit}) {
const {project} = getters;
const project = getters.project;
await db.set(storeName, {id: project.id, data: JSON.stringify(project)});
//localStorage.setItem('project-' + project.id, JSON.stringify(project));
......@@ -674,9 +642,7 @@ export const projectStore = {
}
},
async saveToRemote({state, dispatch, getters}, {remark}) {
console.log(getters.project);
let result = await projectApi.saveOne(getters.project, remark);
console.log(result);
await projectApi.saveOne(getters.project, remark);
dispatch('deleteLocalVersion', state.id);
},
async updateProject({commit}, projectID) {
......@@ -867,7 +833,7 @@ export const projectStore = {
await db.remove(psStoreName, id);
},
savePreview({state, rootState, getters}) {
const {project} = getters;
const project = getters.project;
const packages = state.package.packages;
/*const data = {
......@@ -877,7 +843,7 @@ export const projectStore = {
localStorage.setItem('preview-project-' + project.id, JSON.stringify(data));
localStorage.setItem('preview-ts', Date.now().toString());*/
preprocess(project, state.data, clonePureObj(packages));
preprocess(project, clonePureObj(packages));
},
addBehaviorDirect({state}, {behaviors, alias}) {
......@@ -967,3 +933,45 @@ export async function updateMock(mocks) {
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() {
* @param parent
*/
export function traverseViewNode(nodes, callback, parent) {
if (!nodes) {
return;
}
for (let node of nodes) {
callback(node, parent);
if (node.children && node.children.length > 0) {
......@@ -400,3 +403,24 @@ export function getViewNodePath(node, sep = '/') {
export function timeFormat(time, format = 'YYYY-MM-DD HH:mm:ss') {
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) {
}
}
export async function preprocess(project, data, packages) {
let newData = clonePureObj(data);
const codes = await divideCode(newData, {
export async function preprocess(project, packages) {
let data = JSON.parse(project.data);
const codes = await divideCode(data, {
debug: true,
getPackages: getPackages(packages),
dependencies: data.dependencies,
......@@ -50,7 +50,7 @@ export async function preprocess(project, data, packages) {
scripts.push(newScriptContent('var proxy_mode=true;'));
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/');
......@@ -65,7 +65,7 @@ export async function preprocess(project, data, packages) {
db.set(storeName, {
id: project.id,
data: newData,
data,
tpl,
});
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