1
1
<script setup lang="ts">
2
- import { computed } from ' vue'
2
+ import { h , onMounted , ref , render } from ' vue'
3
3
import { marked } from ' marked'
4
4
5
5
import useI18n from ' @/lang'
6
+ import { APP_TITLE , APP_VERSION , sampleID } from ' @/utils'
7
+ import CodeViewer from ' @/components/CodeViewer/index.vue'
8
+ import Divider from ' @/components/Divider/index.vue'
6
9
7
10
export type Options = {
8
11
type: ' text' | ' markdown'
@@ -22,34 +25,97 @@ const props = withDefaults(defineProps<Props>(), {
22
25
23
26
const emits = defineEmits ([' confirm' , ' cancel' , ' finish' ])
24
27
28
+ const content = ref <string | Record <string , any >>(' ' )
29
+ const domContainers: (() => void )[] = []
30
+
25
31
const { t } = useI18n .global
26
32
33
+ marked .setOptions ({ async: true })
34
+
35
+ marked .use ({
36
+ renderer: {
37
+ image({ href , title , text }) {
38
+ return ` <img src="${href }" alt="${title || text }" style="max-width: 100%"> `
39
+ },
40
+ link({ href , title }) {
41
+ return ` <span onclick="Plugins.BrowserOpenURL('${href }')" style="color: var(--primary-color); cursor: pointer">${title || href }</span> `
42
+ },
43
+ blockquote({ tokens }) {
44
+ const text = this .parser .parse (tokens )
45
+ return ` <div style="border-left: 4px solid var(--primary-color); padding: 8px; margin: 8px 0; display: flex; flex-direction: column; border-radius: 4px; background: var(--card-bg)">${text }</div> `
46
+ },
47
+ paragraph({ text }) {
48
+ return ` <p style="margin: 0">${text }</p> `
49
+ },
50
+ list({ ordered , items }) {
51
+ const children = items
52
+ .map (({ tokens }) => {
53
+ const text = this .parser .parse (tokens )
54
+ return ` <li style="padding: 0">${text }</li> `
55
+ })
56
+ .join (' ' )
57
+ if (ordered ) {
58
+ return ` <ol style="margin: 0; padding: 8px 16px">${children }</ol> `
59
+ }
60
+ return ` <ul style="margin: 0; padding: 8px 16px">${children }</ul> `
61
+ },
62
+ hr() {
63
+ const containerId = ' Br_' + sampleID ()
64
+ const comp = h (Divider , () => APP_TITLE + ' /' + APP_VERSION )
65
+ setTimeout (() => {
66
+ const div = document .getElementById (containerId )
67
+ if (! div ) return
68
+ render (comp , div )
69
+ domContainers .push (() => render (null , div ))
70
+ })
71
+ return ` <div id="${containerId }"></div> `
72
+ },
73
+ code({ text , lang }) {
74
+ const containerId = ' CodeViewer_' + sampleID ()
75
+ const comp = h (CodeViewer , { editable: false , modelValue: text , lang: lang as any })
76
+ setTimeout (() => {
77
+ const div = document .getElementById (containerId )
78
+ if (! div ) return
79
+ render (comp , div )
80
+ domContainers .push (() => render (null , div ))
81
+ })
82
+ return ` <div id="${containerId }"></div> `
83
+ }
84
+ }
85
+ })
86
+
87
+ const renderContent = async () => {
88
+ if (typeof props .message !== ' string' ) {
89
+ content .value = props .message
90
+ return
91
+ }
92
+ if (props .options .type === ' text' ) {
93
+ content .value = t (props .message )
94
+ return
95
+ }
96
+ content .value = await marked .parse (props .message )
97
+ }
98
+
99
+ onMounted (renderContent )
100
+
27
101
const handleConfirm = () => {
28
102
emits (' confirm' , true )
29
103
emits (' finish' )
104
+ domContainers .forEach ((destroy ) => destroy ())
30
105
}
31
106
32
107
const handleCancel = () => {
33
108
emits (' cancel' )
34
109
emits (' finish' )
110
+ domContainers .forEach ((destroy ) => destroy ())
35
111
}
36
-
37
- const message = computed (() => {
38
- if (typeof props .message !== ' string' ) {
39
- return props .message
40
- }
41
- if (props .options .type === ' text' ) {
42
- return t (props .message )
43
- }
44
- return marked .use ().parse (props .message )
45
- })
46
112
</script >
47
113
48
114
<template >
49
115
<Transition name =" slide-down" appear >
50
116
<div class =" confirm" >
51
117
<div class =" title" >{{ t(title) }}</div >
52
- <div class =" message select-text" v-html =" message " ></div >
118
+ <div class =" message select-text" v-html =" content " ></div >
53
119
<div class =" form-action" >
54
120
<Button v-if =" cancel" @click =" handleCancel" size =" small" >{{ t('common.cancel') }}</Button >
55
121
<Button @click =" handleConfirm" size =" small" type =" primary" >
0 commit comments