feat: 添加代码编辑器示例 (#1194)

This commit is contained in:
xiaoming
2024-12-24 09:14:43 +08:00
committed by GitHub
parent 0dd6665b2a
commit 6d26300181
19 changed files with 189 additions and 30 deletions

View File

@@ -170,7 +170,7 @@ onBeforeUnmount(() => {
</el-card>
</template>
<style scoped lang="scss">
<style lang="scss" scoped>
::v-deep(.el-upload-dragger) {
display: flex;
align-items: center;

View File

@@ -0,0 +1,96 @@
<script setup lang="ts">
import "codemirror/theme/material-darker.css";
import "codemirror/addon/hint/show-hint.css";
import "codemirror/addon/hint/show-hint";
import "codemirror/addon/hint/javascript-hint.js";
import "codemirror/mode/javascript/javascript.js";
import { useDark } from "@pureadmin/utils";
import Codemirror from "codemirror-editor-vue3";
import { ref, reactive, watch, nextTick } from "vue";
import type { Editor, EditorConfiguration } from "codemirror";
const { isDark } = useDark();
const cminstance = ref<Editor | null>(null);
const cmOptions: EditorConfiguration = reactive({
mode: "javascript",
theme: isDark.value ? "material-darker" : "default",
tabSize: 4,
readOnly: false,
autofocus: true,
autoRefresh: true,
lineNumbers: true,
lineWiseCopyCut: true,
gutters: ["CodeMirror-lint-markers"],
lint: true,
extraKeys: {
Ctrl: "autocomplete",
Tab: "autocomplete"
},
hintOptions: {
completeSingle: false
}
});
const code = ref(`function sayHello() {
console.log("Hello, World!");
}
sayHello();`);
const onReady = (cm: Editor) => {
cminstance.value = cm;
cm.on("keypress", () => cm.showHint());
// console.log(cm.getValue());
};
watch(
() => isDark.value,
async newVal => {
await nextTick();
newVal
? cminstance.value.setOption("theme", "material-darker")
: cminstance.value.setOption("theme", "default");
}
);
</script>
<template>
<el-card shadow="never">
<template #header>
<div class="card-header">
<span class="font-medium">
代码编辑器组件采用开源的
<el-link
href="https://rennzhang.github.io/codemirror-editor-vue3/zh-CN/guide/getting-started"
target="_blank"
style="margin: 0 4px 5px; font-size: 16px"
>
codemirror-editor-vue3
</el-link>
</span>
</div>
<el-link
class="mt-2"
href="https://github.com/pure-admin/vue-pure-admin/blob/main/src/views/codemirror/index.vue"
target="_blank"
>
代码位置 src/views/codemirror/index.vue
</el-link>
</template>
<Codemirror
v-model:value="code"
width="100%"
height="400px"
:options="cmOptions"
:border="true"
@ready="onReady"
/>
</el-card>
</template>
<style lang="scss" scoped>
.codemirror-container.bordered {
border: 1px solid var(--pure-border-color);
}
</style>

View File

@@ -84,17 +84,7 @@ watch(
>
vue-json-pretty
</el-link>
支持大数据量
</span>
<span class="font-medium">
当然还有一款代码编辑器推荐这里就不做演示了采用开源的
<el-link
href="https://github.com/surmon-china/vue-codemirror"
target="_blank"
style="margin: 0 4px 5px; font-size: 16px"
>
codemirror6
</el-link>
支持大数据量
</span>
</div>
<el-link

View File

@@ -121,7 +121,7 @@ const swiperExample: any[] = [
</el-card>
</template>
<style scoped lang="scss">
<style lang="scss" scoped>
:deep(.el-card__body) {
padding-top: 0;
}

View File

@@ -89,7 +89,7 @@ const cardLogoClass = computed(() => [
</div>
</template>
<style scoped lang="scss">
<style lang="scss" scoped>
.list-card-item {
display: flex;
flex-direction: column;

View File

@@ -147,7 +147,7 @@ const {
</div>
</template>
<style scoped lang="scss">
<style lang="scss" scoped>
:deep(.el-dropdown-menu__item i) {
margin: 0;
}

View File

@@ -147,7 +147,7 @@ const {
</div>
</template>
<style scoped lang="scss">
<style lang="scss" scoped>
:deep(.el-dropdown-menu__item i) {
margin: 0;
}

View File

@@ -153,7 +153,7 @@ const {
</div>
</template>
<style scoped lang="scss">
<style lang="scss" scoped>
:deep(.el-dropdown-menu__item i) {
margin: 0;
}

View File

@@ -107,7 +107,7 @@ const {
</div>
</template>
<style scoped lang="scss">
<style lang="scss" scoped>
:deep(.el-dropdown-menu__item i) {
margin: 0;
}

View File

@@ -327,7 +327,7 @@ onMounted(() => {
</div>
</template>
<style scoped lang="scss">
<style lang="scss" scoped>
:deep(.el-dropdown-menu__item i) {
margin: 0;
}

View File

@@ -254,7 +254,7 @@ const {
</div>
</template>
<style scoped lang="scss">
<style lang="scss" scoped>
:deep(.el-dropdown-menu__item i) {
margin: 0;
}