Bläddra i källkod

423math重写

tangjunhao 4 månader sedan
förälder
incheckning
2be0e9c84e

+ 14 - 0
package-lock.json

@@ -11,6 +11,7 @@
         "@element-plus/icons": "^0.0.11",
         "@element-plus/icons-vue": "^2.3.1",
         "@kitware/vtk.js": "^29.7.3",
+        "@monaco-editor/loader": "^1.5.0",
         "@vue-flow/background": "^1.3.0",
         "@vue-flow/controls": "^1.1.2",
         "@vue-flow/core": "^1.37.1",
@@ -2588,6 +2589,14 @@
         "node": "^12.20.0 || >=14"
       }
     },
+    "node_modules/@monaco-editor/loader": {
+      "version": "1.5.0",
+      "resolved": "https://registry.npmmirror.com/@monaco-editor/loader/-/loader-1.5.0.tgz",
+      "integrity": "sha512-hKoGSM+7aAc7eRTRjpqAZucPmoNOC4UUbknb/VNoTkEIkCPhqV8LfbsgM1webRM7S/z21eHEx9Fkwx8Z/C/+Xw==",
+      "dependencies": {
+        "state-local": "^1.0.6"
+      }
+    },
     "node_modules/@napi-rs/canvas": {
       "version": "0.1.68",
       "resolved": "https://registry.npmmirror.com/@napi-rs/canvas/-/canvas-0.1.68.tgz",
@@ -7085,6 +7094,11 @@
         "node": ">=0.1.14"
       }
     },
+    "node_modules/state-local": {
+      "version": "1.0.7",
+      "resolved": "https://registry.npmmirror.com/state-local/-/state-local-1.0.7.tgz",
+      "integrity": "sha512-HTEHMNieakEnoe33shBYcZ7NX83ACUjCu8c40iOGEZsngj9zRnkqS9j1pqQPXwobB0ZcVTk27REb7COQ0UR59w=="
+    },
     "node_modules/stream-browserify": {
       "version": "3.0.0",
       "resolved": "https://registry.npmmirror.com/stream-browserify/-/stream-browserify-3.0.0.tgz",

+ 1 - 0
package.json

@@ -13,6 +13,7 @@
     "@element-plus/icons": "^0.0.11",
     "@element-plus/icons-vue": "^2.3.1",
     "@kitware/vtk.js": "^29.7.3",
+    "@monaco-editor/loader": "^1.5.0",
     "@vue-flow/background": "^1.3.0",
     "@vue-flow/controls": "^1.1.2",
     "@vue-flow/core": "^1.37.1",

+ 111 - 72
src/components/PythonEditor/index.vue

@@ -1,88 +1,127 @@
-<template>
-  <div :id="editorId" class="editorContainer"></div>
-</template>
-
 <script setup>
-import { ref, onMounted, onUnmounted, watch, computed, toRaw } from 'vue'
-import * as monaco from 'monaco-editor'
+import { ref, onMounted, onBeforeUnmount, watch, nextTick } from "vue";
+import loader from "@monaco-editor/loader";
 
 const props = defineProps({
   value: String,
-  language: String,
-})
-
-const emit = defineEmits(['change'])
-const editor = ref(null)
-const editorId = computed(() => `editor_${Math.random().toString(36).slice(2, 11)}`)
-
-const getEditorValue = () => toRaw(editor.value)?.getValue() || ''
-
-let resizeObserver = null
-
-onMounted(() => {
-  const el = document.getElementById(editorId.value)
-
-  if (!el) {
-    // console.warn('[MonacoEdit]找不到容器元素')
-    return
+  language: {
+    type: String,
+    default: "python",
+  },
+  theme: {
+    type: String,
+    default: "vs-light",
+  },
+});
+
+const emit = defineEmits(["update:value"]);
+
+const editorContainer = ref(null);
+let editorInstance = null;
+
+// 调试:打印容器尺寸
+const logContainerSize = () => {
+  if (!editorContainer.value) {
+    // console.log('Editor container not yet mounted');
+    return;
+  }
+  const rect = editorContainer.value.getBoundingClientRect();
+  // console.log('Container size:', rect.width, 'x', rect.height);
+};
+
+// 初始化 Monaco 编辑器
+const initEditor = async () => {
+  const monaco = await loader.init();
+  
+  // 等待容器渲染完成
+  await nextTick();
+  
+  // 添加额外延迟确保布局完成(仅开发时需要)
+  if (process.env.NODE_ENV === 'development') {
+    await new Promise(resolve => setTimeout(resolve, 50));
   }
 
-  const initEditor = () => {
-    if (editor.value || el.clientWidth === 0 || el.clientHeight === 0) return
-
-    // console.log('[MonacoEdit]尺寸可用,初始化编辑器')
-
-    monaco.editor.defineTheme('custom-light', {
-      base: 'vs',
-      inherit: true,
-      rules: [],
-      colors: {
-        'editorGutter.background': '#EEEEEE',
-      },
-    })
-
-    editor.value = monaco.editor.create(el, {
-      value: props.value || '',
-      language: props.language || 'python',
-      minimap: { enabled: true },
-      colorDecorators: true,
-      readOnly: false,
-      theme: 'custom-light',
-      automaticLayout: true,
-    })
-
-    editor.value.onDidChangeModelContent(() => {
-      emit('change', getEditorValue())
-    })
-
-    console.log('[MonacoEdit]初始化完成')
+  // 再次检查尺寸
+  const rect = editorContainer.value.getBoundingClientRect();
+  // console.log('Final container size:', rect.width, 'x', rect.height);
+
+  editorInstance = monaco.editor.create(editorContainer.value, {
+    value: props.value || "",
+    language: props.language,
+    theme: props.theme,
+  });
+
+  editorInstance.onDidChangeModelContent(() => {
+    const value = editorInstance.getValue();
+    emit("update:value", value);  // 触发v-model更新
+  });
+
+  // 强制立即布局
+  editorInstance.layout();
+};
+
+// 处理窗口大小变化
+const handleResize = () => {
+  if (editorInstance) {
+    // console.log('Window resized, updating editor layout');
+    editorInstance.layout();
   }
+};
 
-  // 使用 ResizeObserver 监听尺寸变化
-  resizeObserver = new ResizeObserver(() => {
-    if (el.clientWidth > 0 && el.clientHeight > 0 && !editor.value) {
-      initEditor()
+onMounted(() => {
+  // console.log('Component mounted, initializing editor...');
+  initEditor();
+});
+
+onBeforeUnmount(() => {
+  // console.log('Component unmounting, disposing editor...');
+  window.removeEventListener('resize', handleResize);
+  editorInstance?.dispose();
+});
+
+// 监听语言变化
+watch(
+  () => props.language,
+  (newLanguage) => {
+    if (editorInstance) {
+      console.log('Language changed to:', newLanguage);
+      loader.init().then((monaco) => {
+        monaco.editor.setModelLanguage(editorInstance.getModel(), newLanguage);
+      });
     }
-  })
-
-  resizeObserver.observe(el)
-})
-
-onUnmounted(() => {
-  if (editor.value) {
-    editor.value.dispose()
-    editor.value = null
   }
-
-  if (resizeObserver) {
-    resizeObserver.disconnect()
-    resizeObserver = null
+);
+
+// 监听值变化
+watch(
+  () => props.value,
+  (newValue) => {
+    if (editorInstance && editorInstance.getValue() !== newValue) {
+      console.log('Value updated programmatically');
+      editorInstance.setValue(newValue);
+    }
   }
-})
+);
 </script>
 
-<style>
+<template>
+  <div ref="editorContainer" class="editorContainer"></div>
+</template>
+
+<style scoped>
 .editorContainer {
-  height: 400px !important;
+  height: 400px;
+  width: 100%;
+  border: 1px solid #ccc; /* 调试边框 */
+  position: relative; /* 确保正确的定位上下文 */
+}
+
+/* 强制设置 Monaco 容器尺寸 */
+.editorContainer :deep(.monaco-editor) {
+  position: absolute;
+  top: 0;
+  left: 0;
+  right: 0;
+  bottom: 0;
 }
 </style>

+ 7 - 22
src/views/home.vue

@@ -1244,7 +1244,7 @@
           type="textarea"
           placeholder="Please input"
         /> -->
-              <PythonEdit v-show="pythoneditorshow" :value="textarea1" language="python" @change="handleEditorChange"/>
+              <PythonEdit v-show="pythoneditorshow" v-model:value="textarea1" language="python" @change="handleEditorChange"/>
               </div>
               <div class="pythfoter">
                 <div class="span active" >
@@ -1303,7 +1303,7 @@
             <template #header="{ titleId, titleClass }">
               <div class="my-header ">
                 <!-- <el-image :src="getImgPath('xuek0.png')" fit="contain"></el-image> -->
-                <h4 :id="titleId" :class="titleClass">FUM to FEM</h4>
+                <h4 :id="titleId" :class="titleClass">FUN to FEM</h4>
               </div>
             </template>
             <FSI ref="Fsiref"/>
@@ -2194,7 +2194,7 @@ const optiongroup =  ref([
   { label: 'HCFD', value: 'HCFD', img:'xuek5.png' },
   { label: 'Matlab', value: 'Matlab', img:'xuek6.png' },
   { label: 'Nastran', value: 'Nastran', img:'xuek7.png' },
-  { label: 'FUM to FEM', value: 'FSI', img:'fsi.png' },
+  { label: 'FUN to FEM', value: 'FSI', img:'fsi.png' },
   { label: 'Flight', value: 'Flight', img:'flight.png' },
 ])
 const eloptimize = ref([])
@@ -2654,7 +2654,7 @@ const pythonSubmit = () => {
     transCode:'MDO0037',
     pid: pid.value,
     wid: pythonwid.value,
-    python: newtextarea1.value,
+    python: textarea1.value,
   }
   request(param).then((res) => {
     ElMessage({
@@ -2666,23 +2666,7 @@ const pythonSubmit = () => {
   });
 }
 
-let newtextarea1 = ref(`import os
-import numpy as np
-from surromdao.solver import BaseSolver
 
-class Branin(BaseSolver):
-    def __init__(self, filename=os.path):
-        super().__init__(filename)
-    
-    def compute(self, xdict):
-        x = np.zeros(2)
-        # x[0] = xdict['x1']
-        # x[1] = xdict['x2']`);
-// python处理编辑器内容变化的方法
-const handleEditorChange = (value) => {
-  // console.log('Editor content changed in parent component:', value);
-  newtextarea1.value = value;
-};
 
 let MathFuncxinjian = ref(false)
 const MathFunctabchange = (val) => {
@@ -3108,7 +3092,7 @@ const curveLine = () => {
       ElMessage.error(err.returnMsg)
     })
 }
-
+// 过程监控1
 const curveLine2 = () => {
   const params = {
     transCode: 'MDO0021',
@@ -3127,6 +3111,7 @@ const curveLine2 = () => {
       ElMessage.error(err.returnMsg)
     })
 }
+// 过程监控2
 const curveLine3 = () => {
   const params = {
     transCode: 'MDO0021',
@@ -3148,7 +3133,7 @@ const curveLine3 = () => {
     })
 }
 
-
+// 可视化页数据
 const curveLine4 = () => {
   const params = {
     transCode: 'MDO0021',

+ 1 - 1
src/views/vuetree/Sidebar.vue

@@ -175,7 +175,7 @@ const datatree = ref([
         },
         {
           id:'3-13',
-          label: 'FUM to FEM',
+          label: 'FUN to FEM',
           img:'fsi.png',
           name:'FSI'
         },

+ 1 - 1
src/views/vuetree/useDnD.js

@@ -64,7 +64,7 @@ function imagefun(){
     }else if(nid=='3-12'){
       return datas = {label:'TACS', image:tacs,name:'TACS'}
     }else if(nid=='3-13'){
-      return datas = {label:'FUM to FEM', image:fsi,backgroud:fsibackground , name:'FSI'}
+      return datas = {label:'FUN to FEM', image:fsi,backgroud:fsibackground , name:'FSI'}
     }else if(nid=='3-14'){
       return datas = {label:'MathFunc', image:mathfunc,name:'MathFunc'}
     }else if(nid=='3-15'){