mirror of
https://github.com/pure-admin/vue-pure-admin.git
synced 2025-11-15 14:03:36 +08:00
feat: 添加Markdown示例 (#1193)
This commit is contained in:
105
src/views/markdown/components/Vditor.vue
Normal file
105
src/views/markdown/components/Vditor.vue
Normal file
@@ -0,0 +1,105 @@
|
||||
<script setup lang="ts">
|
||||
import "vditor/dist/index.css";
|
||||
import Vditor from "vditor";
|
||||
import { useDark } from "@pureadmin/utils";
|
||||
import { useIntervalFn } from "@vueuse/core";
|
||||
import { onMounted, ref, watch, toRaw, onUnmounted } from "vue";
|
||||
|
||||
const emit = defineEmits([
|
||||
"update:modelValue",
|
||||
"after",
|
||||
"focus",
|
||||
"blur",
|
||||
"esc",
|
||||
"ctrlEnter",
|
||||
"select"
|
||||
]);
|
||||
|
||||
const props = defineProps({
|
||||
options: {
|
||||
type: Object,
|
||||
default() {
|
||||
return {};
|
||||
}
|
||||
},
|
||||
modelValue: {
|
||||
type: String,
|
||||
default: ""
|
||||
}
|
||||
});
|
||||
|
||||
const { isDark } = useDark();
|
||||
const editor = ref<Vditor | null>(null);
|
||||
const markdownRef = ref<HTMLElement | null>(null);
|
||||
|
||||
onMounted(() => {
|
||||
editor.value = new Vditor(markdownRef.value as HTMLElement, {
|
||||
...props.options,
|
||||
value: props.modelValue,
|
||||
cache: {
|
||||
enable: false
|
||||
},
|
||||
fullscreen: {
|
||||
index: 10000
|
||||
},
|
||||
after() {
|
||||
emit("after", toRaw(editor.value));
|
||||
},
|
||||
input(value: string) {
|
||||
emit("update:modelValue", value);
|
||||
},
|
||||
focus(value: string) {
|
||||
emit("focus", value);
|
||||
},
|
||||
blur(value: string) {
|
||||
emit("blur", value);
|
||||
},
|
||||
esc(value: string) {
|
||||
emit("esc", value);
|
||||
},
|
||||
ctrlEnter(value: string) {
|
||||
emit("ctrlEnter", value);
|
||||
},
|
||||
select(value: string) {
|
||||
emit("select", value);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
watch(
|
||||
() => props.modelValue,
|
||||
newVal => {
|
||||
if (newVal !== editor.value?.getValue()) {
|
||||
editor.value?.setValue(newVal);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
watch(
|
||||
() => isDark.value,
|
||||
newVal => {
|
||||
const { pause } = useIntervalFn(() => {
|
||||
if (editor.value.vditor) {
|
||||
newVal
|
||||
? editor.value.setTheme("dark", "dark", "rose-pine")
|
||||
: editor.value.setTheme("classic", "light", "github");
|
||||
pause();
|
||||
}
|
||||
}, 20);
|
||||
}
|
||||
);
|
||||
|
||||
onUnmounted(() => {
|
||||
const editorInstance = editor.value;
|
||||
if (!editorInstance) return;
|
||||
try {
|
||||
editorInstance?.destroy?.();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div ref="markdownRef" />
|
||||
</template>
|
||||
59
src/views/markdown/index.vue
Normal file
59
src/views/markdown/index.vue
Normal file
@@ -0,0 +1,59 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from "vue";
|
||||
import Vditor from "./components/Vditor.vue";
|
||||
|
||||
defineOptions({
|
||||
name: "Markdown"
|
||||
});
|
||||
|
||||
const text = ref(`
|
||||
\`\`\`ts
|
||||
function sayHello(): void {
|
||||
\tconsole.log("Hello, World!");
|
||||
}
|
||||
sayHello();
|
||||
\`\`\`
|
||||
# 一级标题
|
||||
## 二级标题
|
||||
### 三级标题
|
||||
#### 四级标题
|
||||
##### 五级标题
|
||||
###### 六级标题
|
||||
`);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<el-card shadow="never">
|
||||
<template #header>
|
||||
<div class="card-header">
|
||||
<span class="font-medium">
|
||||
Markdown组件,采用开源的
|
||||
<el-link
|
||||
href="https://b3log.org/vditor/"
|
||||
target="_blank"
|
||||
style="margin: 0 4px 5px; font-size: 16px"
|
||||
>
|
||||
Vditor
|
||||
</el-link>
|
||||
</span>
|
||||
</div>
|
||||
<el-link
|
||||
class="mt-2"
|
||||
href="https://github.com/pure-admin/vue-pure-admin/blob/main/src/views/markdown"
|
||||
target="_blank"
|
||||
>
|
||||
代码位置 src/views/markdown
|
||||
</el-link>
|
||||
</template>
|
||||
<h1 class="mb-2">
|
||||
双向绑定:<span class="text-red-500">{{ text }}</span>
|
||||
</h1>
|
||||
<Vditor
|
||||
v-model="text"
|
||||
:options="{
|
||||
height: 560, // 高度
|
||||
outline: { enable: true, position: 'right' } // 大纲
|
||||
}"
|
||||
/>
|
||||
</el-card>
|
||||
</template>
|
||||
Reference in New Issue
Block a user