修改文件内容代码显示及修改
This commit is contained in:
parent
15b30093e9
commit
a4c83bbec1
|
|
@ -5,22 +5,27 @@
|
||||||
<el-button type="primary" @click="UpdateFileCOntent">保存修改</el-button>
|
<el-button type="primary" @click="UpdateFileCOntent">保存修改</el-button>
|
||||||
<div class="content">
|
<div class="content">
|
||||||
<h2>{{ fileName }}</h2>
|
<h2>{{ fileName }}</h2>
|
||||||
|
<div class="editor-container">
|
||||||
<textarea
|
<textarea
|
||||||
ref="textareaRef"
|
ref="textareaRef"
|
||||||
v-model="localContent"
|
v-model="localContent"
|
||||||
cols="40"
|
@input="handleInput"
|
||||||
rows="10"
|
@scroll="handleScroll"
|
||||||
spellcheck="false"
|
spellcheck="false"
|
||||||
></textarea>
|
></textarea>
|
||||||
|
<pre class="highlight-container" ref="highlightRef"><code v-html="highlightedCode"></code></pre>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</transition>
|
</transition>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, watch, nextTick } from "vue";
|
import { ref, watch, nextTick, onMounted } from "vue";
|
||||||
import { UpdateFileContentService } from "@/api/file";
|
import { UpdateFileContentService } from "@/api/file";
|
||||||
import { ElMessage } from "element-plus";
|
import { ElMessage } from "element-plus";
|
||||||
|
import hljs from 'highlight.js';
|
||||||
|
import 'highlight.js/styles/atom-one-dark.css';
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
content: {
|
content: {
|
||||||
|
|
@ -46,13 +51,101 @@ const emit = defineEmits<{
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
const textareaRef = ref<HTMLTextAreaElement | null>(null);
|
const textareaRef = ref<HTMLTextAreaElement | null>(null);
|
||||||
|
const highlightRef = ref<HTMLElement | null>(null);
|
||||||
const localContent = ref(props.content);
|
const localContent = ref(props.content);
|
||||||
|
const highlightedCode = ref('');
|
||||||
|
|
||||||
|
// 根据文件名判断语言
|
||||||
|
const detectLanguage = (fileName: string) => {
|
||||||
|
const extension = fileName.split('.').pop()?.toLowerCase();
|
||||||
|
|
||||||
|
const languageMap: Record<string, string> = {
|
||||||
|
'js': 'javascript',
|
||||||
|
'jsx': 'javascript',
|
||||||
|
'ts': 'typescript',
|
||||||
|
'tsx': 'typescript',
|
||||||
|
'html': 'html',
|
||||||
|
'css': 'css',
|
||||||
|
'scss': 'scss',
|
||||||
|
'less': 'less',
|
||||||
|
'py': 'python',
|
||||||
|
'java': 'java',
|
||||||
|
'php': 'php',
|
||||||
|
'go': 'go',
|
||||||
|
'rb': 'ruby',
|
||||||
|
'rs': 'rust',
|
||||||
|
'c': 'c',
|
||||||
|
'cpp': 'cpp',
|
||||||
|
'cs': 'csharp',
|
||||||
|
'json': 'json',
|
||||||
|
'md': 'markdown',
|
||||||
|
'xml': 'xml',
|
||||||
|
'yaml': 'yaml',
|
||||||
|
'yml': 'yaml',
|
||||||
|
'sh': 'bash',
|
||||||
|
'bash': 'bash',
|
||||||
|
'sql': 'sql',
|
||||||
|
};
|
||||||
|
|
||||||
|
return extension && languageMap[extension] ? languageMap[extension] : 'plaintext';
|
||||||
|
};
|
||||||
|
|
||||||
|
// 高亮代码
|
||||||
|
const highlightCode = () => {
|
||||||
|
if (!localContent.value) {
|
||||||
|
highlightedCode.value = '';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const language = detectLanguage(props.fileName);
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (language !== 'plaintext') {
|
||||||
|
const highlighted = hljs.highlight(localContent.value, { language });
|
||||||
|
highlightedCode.value = highlighted.value;
|
||||||
|
} else {
|
||||||
|
// 如果无法检测语言,尝试自动检测
|
||||||
|
const highlighted = hljs.highlightAuto(localContent.value);
|
||||||
|
highlightedCode.value = highlighted.value;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Highlight error:', error);
|
||||||
|
// 降级处理:转义HTML并保留换行
|
||||||
|
highlightedCode.value = localContent.value
|
||||||
|
.replace(/&/g, '&')
|
||||||
|
.replace(/</g, '<')
|
||||||
|
.replace(/>/g, '>')
|
||||||
|
.replace(/"/g, '"')
|
||||||
|
.replace(/'/g, ''');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 处理输入事件
|
||||||
|
const handleInput = () => {
|
||||||
|
highlightCode();
|
||||||
|
nextTick(() => {
|
||||||
|
if (textareaRef.value && highlightRef.value) {
|
||||||
|
// 同步滚动位置
|
||||||
|
highlightRef.value.scrollTop = textareaRef.value.scrollTop;
|
||||||
|
highlightRef.value.scrollLeft = textareaRef.value.scrollLeft;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// 处理滚动事件
|
||||||
|
const handleScroll = () => {
|
||||||
|
if (textareaRef.value && highlightRef.value) {
|
||||||
|
highlightRef.value.scrollTop = textareaRef.value.scrollTop;
|
||||||
|
highlightRef.value.scrollLeft = textareaRef.value.scrollLeft;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// 当props.content变化时同步到本地
|
// 当props.content变化时同步到本地
|
||||||
watch(
|
watch(
|
||||||
() => props.content,
|
() => props.content,
|
||||||
(newVal) => {
|
(newVal) => {
|
||||||
localContent.value = newVal;
|
localContent.value = newVal;
|
||||||
|
highlightCode();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -63,10 +156,16 @@ watch(
|
||||||
if (newVal) {
|
if (newVal) {
|
||||||
await nextTick();
|
await nextTick();
|
||||||
textareaRef.value?.focus();
|
textareaRef.value?.focus();
|
||||||
|
highlightCode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 初始化
|
||||||
|
onMounted(() => {
|
||||||
|
highlightCode();
|
||||||
|
});
|
||||||
|
|
||||||
const handleClose = () => {
|
const handleClose = () => {
|
||||||
emit("close");
|
emit("close");
|
||||||
};
|
};
|
||||||
|
|
@ -97,28 +196,50 @@ const UpdateFileCOntent = async () => {
|
||||||
right: 0;
|
right: 0;
|
||||||
width: 50%;
|
width: 50%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
background: white;
|
background-color: #282c34;
|
||||||
z-index: 1001;
|
z-index: 1001;
|
||||||
box-shadow: -2px 0 8px rgba(0, 0, 0, 0.15);
|
box-shadow: -2px 0 8px rgba(0, 0, 0, 0.3);
|
||||||
padding: 20px;
|
padding: 20px;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
|
color: #abb2bf;
|
||||||
}
|
}
|
||||||
|
|
||||||
.close-btn {
|
.close-btn {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
top: 10px;
|
top: 10px;
|
||||||
right: 10px;
|
right: 10px;
|
||||||
font-size: 24px;
|
width: 30px;
|
||||||
|
height: 30px;
|
||||||
background: none;
|
background: none;
|
||||||
border: none;
|
border: none;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
transition: color 0.2s;
|
|
||||||
z-index: 1;
|
z-index: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
.close-btn:hover {
|
.close-btn::before,
|
||||||
color: #666;
|
.close-btn::after {
|
||||||
|
content: '';
|
||||||
|
position: absolute;
|
||||||
|
width: 20px;
|
||||||
|
height: 2px;
|
||||||
|
background-color: #abb2bf;
|
||||||
|
top: 50%;
|
||||||
|
left: 50%;
|
||||||
|
transition: background-color 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.close-btn::before {
|
||||||
|
transform: translate(-50%, -50%) rotate(45deg);
|
||||||
|
}
|
||||||
|
|
||||||
|
.close-btn::after {
|
||||||
|
transform: translate(-50%, -50%) rotate(-45deg);
|
||||||
|
}
|
||||||
|
|
||||||
|
.close-btn:hover::before,
|
||||||
|
.close-btn:hover::after {
|
||||||
|
background-color: #61afef;
|
||||||
}
|
}
|
||||||
|
|
||||||
.content {
|
.content {
|
||||||
|
|
@ -128,23 +249,108 @@ const UpdateFileCOntent = async () => {
|
||||||
margin-top: 20px;
|
margin-top: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
textarea {
|
.content h2 {
|
||||||
|
color: #e5e5e5;
|
||||||
|
margin-bottom: 15px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.editor-container {
|
||||||
|
position: relative;
|
||||||
flex: 1;
|
flex: 1;
|
||||||
|
border-radius: 4px;
|
||||||
|
background-color: #282c34;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.editor-container textarea {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
padding: 10px;
|
height: 100%;
|
||||||
border: 1px solid #ddd;
|
padding: 12px;
|
||||||
|
border: 1px solid #3e4451;
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
resize: none;
|
resize: none;
|
||||||
font-family: monospace;
|
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
line-height: 1.5;
|
line-height: 1.5;
|
||||||
|
color: transparent;
|
||||||
|
background-color: transparent;
|
||||||
|
caret-color: #61afef;
|
||||||
|
z-index: 2;
|
||||||
|
white-space: pre;
|
||||||
|
overflow: auto;
|
||||||
tab-size: 2;
|
tab-size: 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.editor-container textarea:focus {
|
||||||
|
outline: none;
|
||||||
|
border-color: #61afef;
|
||||||
|
}
|
||||||
|
|
||||||
|
.highlight-container {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
padding: 12px;
|
||||||
|
margin: 0;
|
||||||
|
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
|
||||||
|
font-size: 14px;
|
||||||
|
line-height: 1.5;
|
||||||
|
pointer-events: none;
|
||||||
|
overflow: auto;
|
||||||
|
z-index: 1;
|
||||||
|
white-space: pre;
|
||||||
|
tab-size: 2;
|
||||||
|
background-color: #282c34;
|
||||||
|
}
|
||||||
|
|
||||||
|
.highlight-container code {
|
||||||
|
font-family: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 滚动条样式 */
|
||||||
|
.editor-container textarea::-webkit-scrollbar,
|
||||||
|
.highlight-container::-webkit-scrollbar {
|
||||||
|
width: 8px;
|
||||||
|
height: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.editor-container textarea::-webkit-scrollbar-track,
|
||||||
|
.highlight-container::-webkit-scrollbar-track {
|
||||||
|
background: #21252b;
|
||||||
|
}
|
||||||
|
|
||||||
|
.editor-container textarea::-webkit-scrollbar-thumb,
|
||||||
|
.highlight-container::-webkit-scrollbar-thumb {
|
||||||
|
background: #3e4451;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.editor-container textarea::-webkit-scrollbar-thumb:hover,
|
||||||
|
.highlight-container::-webkit-scrollbar-thumb:hover {
|
||||||
|
background: #4b5363;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 保存按钮样式 */
|
||||||
|
:deep(.el-button--primary) {
|
||||||
|
background-color: #61afef;
|
||||||
|
border-color: #61afef;
|
||||||
|
}
|
||||||
|
|
||||||
|
:deep(.el-button--primary:hover) {
|
||||||
|
background-color: #528bbc;
|
||||||
|
border-color: #528bbc;
|
||||||
|
}
|
||||||
|
|
||||||
.slide-enter-active,
|
.slide-enter-active,
|
||||||
.slide-leave-active {
|
.slide-leave-active {
|
||||||
transition: transform 0.3s ease;
|
transition: transform 0.3s ease;
|
||||||
}
|
}
|
||||||
|
|
||||||
.slide-enter-from,
|
.slide-enter-from,
|
||||||
.slide-leave-to {
|
.slide-leave-to {
|
||||||
transform: translateX(100%);
|
transform: translateX(100%);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue