Browse Source

配置优化

mxd 3 years ago
parent
commit
1b421fb917
3 changed files with 42 additions and 8 deletions
  1. 13 0
      src/App.vue
  2. 21 1
      src/components/magic-editor.vue
  3. 8 7
      src/components/panel/header/magic-header.vue

+ 13 - 0
src/App.vue

@@ -14,6 +14,19 @@ if (window.MAGIC_EDITOR_CONFIG) {
 defaultConfig.baseURL = import.meta.env.MODE === 'development' ? 'http://127.0.0.1:9999/magic/web' : './';
 defaultConfig.serverURL = import.meta.env.MODE === 'development' ? 'http://127.0.0.1:9999/' : './';
 defaultConfig.inJar = true;
+const getQueryString = name => {
+	var r = window.location.search.substr(1).match(new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i'));
+	return r && unescape(r[2])
+}
+const headerName = getQueryString('headerName')
+const headerValue = getQueryString('headerValue')
+if(headerName && headerValue){
+	defaultConfig.request = defaultConfig.request || {}
+	defaultConfig.request.beforeSend = cfg => {
+		cfg.headers[headerName] = headerValue
+		return cfg
+	}
+}
 </script>
 <style>
 html,body,#app{

+ 21 - 1
src/components/magic-editor.vue

@@ -2,7 +2,7 @@
     <div class="magic-editor" :style="themeStyle" @contextmenu.prevent="" ref="root">
 		<magic-login v-show="showLogin" v-model:value="showLogin" v-model:error="errorMessage"/>
 		<!-- 顶部区域 -->
-		<magic-header :themeStyle="themeStyle" v-if="loadFinish"/>
+		<magic-header :themeStyle="themeStyle" v-if="loadFinish" :header="config.header"/>
 		<magic-main ref="componentMain" @onLoad="init" v-if="loadFinish"/>
 		<!-- 状态条 -->
 		<magic-status-bar :config="config" v-if="loadFinish"/>
@@ -70,7 +70,27 @@ if (constants.BASE_URL.startsWith('http')) { // http开头
 } else {
 	link = link + '/' + constants.BASE_URL
 }
+if(config.blockClose !== false) {
+	window.onbeforeunload = () => '系统可能不会保存您所做的更改。'
+}
+const requestConfig = config.request || {
+	beforeSend: config => config,
+	onError: err => Promise.reject(err)
+}
+request.getAxios().interceptors.request.use(
+	cfg => requestConfig.beforeSend && requestConfig.beforeSend(cfg) || cfg, 
+	err => requestConfig.onError && requestConfig.onError(err) || Promise.reject(err)
+)
+const responseConfig = config.response || {
+	onSuccess: resp => resp,
+	onError: err => Promise.reject(err)
+}
+request.getAxios().interceptors.response.use(
+	resp => responseConfig.onSuccess && responseConfig.onSuccess(resp) || resp, 
+	err => responseConfig.onError && responseConfig.onError(err) || Promise.reject(err)
+)
 request.setBaseURL(constants.BASE_URL)
+constants.AUTO_SAVE = config.autoSave !== false
 bus.status('message.loadClass')
 provide('i18n.format', $i)
 const installPlugin = () => {

+ 8 - 7
src/components/panel/header/magic-header.vue

@@ -44,12 +44,13 @@ for(let k in localeScripts){
         })
     })
 }
-const { themeStyle } = defineProps({
+const props = defineProps({
     title: {
         type: String,
         default: 'magic-api'
     },
-    themeStyle: Object
+    themeStyle: Object,
+    header: Object
 })
 const version = constants.MAGIC_API_VERSION_TEXT
 const skinVisible = ref(false)
@@ -69,26 +70,26 @@ bus.$on(Message.OPEN, item => {
         }
     })
 })
-const buttons = ref([
+const buttons = computed(() => [
     {name: `${$i('message.run')}(Ctrl + Q)`, icon: 'run', disabled: computed(() => opened.value.runnable !== true || opened.value.running === true), onClick: ()=> bus.$emit(Message.DO_TEST)},
     {name: `${$i('message.save')}(Ctrl + S)`, icon: 'save', onClick: () => bus.$emit(Message.DO_SAVE, true)},
     {name: `${$i('message.search')}(Ctrl + Shift + F)`, icon: 'search', onClick: () => bus.$emit(Message.DO_SEARCH)},
     {name: $i('message.upload'), icon: 'upload', onClick: () => bus.$emit(Message.DO_UPLOAD)},
     {name: $i('message.export'), icon: 'download', onClick: () => bus.$emit(Message.DO_DOWNLOAD)},
     {name: $i('message.push'), icon: 'push', onClick: () => bus.$emit(Message.DO_PUSH)},
-    {name: $i('message.skin'), icon: 'skin', onClick: () => {skinVisible.value = !skinVisible.value; localeVisible.value = false}},
+    {name: $i('message.skin'), displayKey: 'skin', icon: 'skin', onClick: () => {skinVisible.value = !skinVisible.value; localeVisible.value = false}},
     {name: $i('message.i18n'), icon: 'i18n', onClick: () => {localeVisible.value = !localeVisible.value; skinVisible.value = false}},
     {name: $i('message.reload'), icon: 'refresh', onClick: () => bus.$emit(Message.RELOAD_RESOURCES)}
-])
+].filter(it => props.header[it.displayKey] !== false))
 const switchTheme = $theme => {
     constants.THEME = $theme
     bus.$emit(Message.SWITCH_THEME, $theme)
     bus.status('message.switchSkin',true, $theme)
     monaco.editor.setTheme($theme)
-    Object.keys(themeStyle).forEach(key => themeStyle[key] = undefined)
+    Object.keys(props.themeStyle).forEach(key => props.themeStyle[key] = undefined)
     let theme = Themes[$theme]
     store.set(constants.STORE.theme, $theme)
-    Object.keys(theme).forEach((key) => themeStyle[`--${key}`] = theme[key])
+    Object.keys(theme).forEach((key) => props.themeStyle[`--${key}`] = theme[key])
     skinVisible.value = false
 }
 const storeTheme = store.get(constants.STORE.theme)