|  | @@ -8,33 +8,42 @@ import * as monaco from 'monaco-editor';
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  const props = defineProps({
 | 
	
		
			
				|  |  |    modelValue: String, // v-model 绑定
 | 
	
		
			
				|  |  | -  value: String,      // 传统 value 绑定(兼容旧代码)
 | 
	
		
			
				|  |  | +  value: String,      // 兼容传统 value 绑定
 | 
	
		
			
				|  |  |    language: String,
 | 
	
		
			
				|  |  |  });
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -const emit = defineEmits([
 | 
	
		
			
				|  |  | -  'update:modelValue', // v-model 更新事件
 | 
	
		
			
				|  |  | -  'change',           // 传统 change 事件
 | 
	
		
			
				|  |  | -]);
 | 
	
		
			
				|  |  | +const emit = defineEmits(['update:modelValue', 'change']);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  const editor = ref(null);
 | 
	
		
			
				|  |  | +let isUpdatingFromParent = false; // 添加防止死循环的锁
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -// 获取当前绑定的值(优先使用 modelValue)
 | 
	
		
			
				|  |  | -const getCurrentValue = () => {
 | 
	
		
			
				|  |  | -  return props.modelValue ?? props.value;
 | 
	
		
			
				|  |  | -};
 | 
	
		
			
				|  |  | +// 获取当前值
 | 
	
		
			
				|  |  | +const getCurrentValue = () => props.modelValue ?? props.value;
 | 
	
		
			
				|  |  | +const getEditorValue = () => toRaw(editor.value)?.getValue() || '';
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -// 获取编辑器内容
 | 
	
		
			
				|  |  | -const getEditorValue = () => {
 | 
	
		
			
				|  |  | -  return toRaw(editor.value)?.getValue() || '';
 | 
	
		
			
				|  |  | -};
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -// 监听外部值变化(如果父组件修改了 value,同步到编辑器)
 | 
	
		
			
				|  |  | +// 监听父组件传来的值变化(用于外部更新编辑器)
 | 
	
		
			
				|  |  |  watch(
 | 
	
		
			
				|  |  |    () => getCurrentValue(),
 | 
	
		
			
				|  |  |    (newValue) => {
 | 
	
		
			
				|  |  | -    if (editor.value && newValue !== getEditorValue()) {
 | 
	
		
			
				|  |  | -      editor.value.setValue(newValue);
 | 
	
		
			
				|  |  | +    const editorVal = getEditorValue();
 | 
	
		
			
				|  |  | +    const normalize = (s) => (s || '').trim();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    if (editor.value && normalize(newValue) !== normalize(editorVal)) {
 | 
	
		
			
				|  |  | +      console.log('外部更新编辑器内容', newValue);
 | 
	
		
			
				|  |  | +      console.log('当前编辑器内容', editorVal);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +      isUpdatingFromParent = true;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +      // 设置值
 | 
	
		
			
				|  |  | +      editor.value.setValue(newValue || '');
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +      // 等当前宏任务结束后再解除锁
 | 
	
		
			
				|  |  | +      queueMicrotask(() => {
 | 
	
		
			
				|  |  | +        isUpdatingFromParent = false;
 | 
	
		
			
				|  |  | +        console.log('编辑器内容已更新', editor.value.getValue());
 | 
	
		
			
				|  |  | +      });
 | 
	
		
			
				|  |  | +    } else {
 | 
	
		
			
				|  |  | +      console.log('内容相同,跳过更新');
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |  );
 | 
	
	
		
			
				|  | @@ -59,17 +68,19 @@ onMounted(() => {
 | 
	
		
			
				|  |  |      automaticLayout: true,
 | 
	
		
			
				|  |  |    });
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -  // 监听编辑器内容变化
 | 
	
		
			
				|  |  | +  // 编辑器内容变化时触发
 | 
	
		
			
				|  |  |    editor.value.onDidChangeModelContent(() => {
 | 
	
		
			
				|  |  | +    if (isUpdatingFromParent) return; // 阻止外部设置引起的触发
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      const newValue = getEditorValue();
 | 
	
		
			
				|  |  | -    emit('update:modelValue', newValue); // 触发 v-model 更新
 | 
	
		
			
				|  |  | -    emit('change', newValue);            // 触发传统 change 事件
 | 
	
		
			
				|  |  | +    emit('update:modelValue', newValue); // 通知父组件更新
 | 
	
		
			
				|  |  | +    emit('change', newValue);
 | 
	
		
			
				|  |  |    });
 | 
	
		
			
				|  |  |  });
 | 
	
		
			
				|  |  |  </script>
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  <style>
 | 
	
		
			
				|  |  | -#editorContainer{
 | 
	
		
			
				|  |  | +#editorContainer {
 | 
	
		
			
				|  |  |    height: 400px !important;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  </style>
 |