Ben Lin
2025-03-05 043701d5a28df6a0ea596ed7e4198676480bcc60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<template>
  <PageWrapper title="MarkDown组件示例">
    <div>
      <a-button @click="toggleTheme" class="mb-2" type="primary"> 黑暗主题 </a-button>
      <a-button @click="clearValue" class="mb-2" type="default"> 清空内容 </a-button>
      <MarkDown
        v-model:value="valueRef"
        @change="handleChange"
        ref="markDownRef"
        placeholder="这是占位文本"
      />
    </div>
    <div class="mt-2">
      <Card title="Markdown Viewer 组件演示">
        <MarkdownViewer :value="valueRef" />
      </Card>
    </div>
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { ref, unref } from 'vue';
  import { MarkDown, MarkDownActionType, MarkdownViewer } from '@/components/Markdown';
  import { PageWrapper } from '@/components/Page';
  import { Card } from 'ant-design-vue';
  import { type Nullable } from '@vben/types';
 
  const markDownRef = ref<Nullable<MarkDownActionType>>(null);
  const valueRef = ref(`
# 标题h1
 
##### 标题h5
 
**加粗**
*斜体*
~~删除线~~
[链接](https://github.com/vbenjs/vue-vben-admin)
↓分割线↓
 
---
 
 
* 无序列表1
  * 无序列表1.1
 
1. 有序列表1
2. 有序列表2
 
* [ ] 任务列表1
* [x] 任务列表2
 
> 引用示例
 
\`\`\`js
// 代码块:
(() => {
  var htmlRoot = document.getElementById('htmlRoot');
  var theme = window.localStorage.getItem('__APP__DARK__MODE__');
  if (htmlRoot && theme) {
    htmlRoot.setAttribute('data-theme', theme);
    theme = htmlRoot = null;
  }
})();
\`\`\`
 
| 表格 | 示例 | 🎉️ |
| --- | --- | --- |
| 1 | 2 | 3 |
| 4 | 5 | 6 |
`);
 
  function toggleTheme() {
    const markDown = unref(markDownRef);
    if (!markDown) return;
    const vditor = markDown.getVditor();
    vditor.setTheme('dark', 'dark', 'dracula');
  }
 
  function handleChange(v: string) {
    valueRef.value = v;
  }
 
  function clearValue() {
    valueRef.value = '';
  }
</script>