diff --git a/CHANGELOG.md b/CHANGELOG.md index b18854ee50c8a0b2b1899f6b98850bfd6d4d739b..a29bb18b398a665c2c45ebe45968faf2d782703d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ ## [Unreleased] +### Added + +- 代码编辑器新增空白提示 + ## [0.7.38-alpha.63] - 2025-03-03 ### Added diff --git a/src/editor/code/monaco-editor/monaco-editor.scss b/src/editor/code/monaco-editor/monaco-editor.scss index 6904d70253136c168f895f2441a2679f71593c01..05b5b603b4c888c8794b1c371782198244bce731 100644 --- a/src/editor/code/monaco-editor/monaco-editor.scss +++ b/src/editor/code/monaco-editor/monaco-editor.scss @@ -9,6 +9,13 @@ min-height: 200px; overflow: hidden; } + + @include e('first-prompt') { + &::before { + padding-right: getCssVar(spacing, base); + content: #{getCssVar('code', 'placeholder')}; + } + } } @include b('code-toolbar') { @@ -135,4 +142,4 @@ @include b('code-footer-dialog') { min-height: 68px; margin-top: 0; -} +} \ No newline at end of file diff --git a/src/editor/code/monaco-editor/monaco-editor.tsx b/src/editor/code/monaco-editor/monaco-editor.tsx index 072e01e369752dfbdcdd8699c88c89f7d267c77c..33eb00e9899a99249cba69c5c12b69368d49b26b 100644 --- a/src/editor/code/monaco-editor/monaco-editor.tsx +++ b/src/editor/code/monaco-editor/monaco-editor.tsx @@ -66,6 +66,9 @@ export const IBizCode = defineComponent({ let editor: monaco.editor.IStandaloneCodeEditor | null; let monacoEditor: typeof monaco.editor; + let decorationsCollection: + | monaco.editor.IEditorDecorationsCollection + | undefined; const { UIStore } = useUIStore(); // 编辑器主题 @@ -157,6 +160,41 @@ export const IBizCode = defineComponent({ editor!.setValue(currentVal.value); }); + // 创建空白提示 + const createDecorationsCollection = (): void => { + if (decorationsCollection) decorationsCollection.clear(); + if (c.placeHolder && !editor?.getValue()) { + decorationsCollection = editor?.createDecorationsCollection([ + { + range: new loaderMonaco.Range(1, 1, 1, 1), // 影响第1行的开始位置 + options: { + isWholeLine: true, // 是否作用于整行 + beforeContentClassName: `${ns.e('first-prompt')} mtk8`, // 在内容前加修饰 mtk8为注释样式类名 + }, + }, + ]); + } + }; + + // 保存原始的 setValue 方法 + const originalSetValue = editor.setValue.bind(editor); + // 重写 setValue 方法 + editor.setValue = (newValue: string): void => { + // 调用原始的 setValue 方法 + originalSetValue(newValue); + createDecorationsCollection(); + }; + + // 获取焦点 + editor.onDidFocusEditorText(() => { + decorationsCollection?.clear(); + }); + + // 失去焦点 + editor.onDidBlurEditorText(() => { + createDecorationsCollection(); + }); + // 监听值的变化 editor.onDidChangeModelContent(() => { if (!hasEnableEdit.value) { @@ -314,7 +352,13 @@ export const IBizCode = defineComponent({ // 绘制代码框内容 const renderCodeContent = () => { - return
; + return ( + + ); }; onMounted(() => { @@ -323,6 +367,7 @@ export const IBizCode = defineComponent({ onUnmounted(() => { editor?.dispose(); + decorationsCollection?.clear(); }); return {