uni-ai-msg.vue 3.1 KB
Newer Older
1 2
<template>
	<view class="rich-text-box" :class="{'show-cursor':showCursor}" ref="rich-text-box">
DCloud_JSON's avatar
DCloud_JSON 已提交
3
		<rich-text v-if="nodes&&nodes.length" space="nbsp" :nodes="nodes"></rich-text>
4 5 6 7 8 9 10

		<!-- #ifdef H5 -->
		<view class="copy-box" :style="{left,top}">
			<text class="copy" @click="copy">复制</text>
			<!-- <view v-if="left != '-100px'" class="copy-mask" @click="left = '-100px'"></view> -->
		</view>
		<!-- #endif -->
DCloud_JSON's avatar
DCloud_JSON 已提交
11 12 13 14
	</view>
</template>

<script>
DCloud_JSON's avatar
1.0.3  
DCloud_JSON 已提交
15
	import MarkdownIt from '@/lib/markdown-it.min.js';
DCloud_JSON's avatar
DCloud_JSON 已提交
16 17 18
	
	// hljs是由 Highlight.js 经兼容性修改后的文件,请勿直接升级。否则会造成uni-app-vue3-Android下有兼容问题
	import hljs from "@/lib/highlight/highlight-uni.min.js";
DCloud_JSON's avatar
1.0.3  
DCloud_JSON 已提交
19
	
DCloud_JSON's avatar
DCloud_JSON 已提交
20
	import parseHtml from '@/lib/html-parser.js';
21 22 23

	// console.log('hljs--',hljs);
	// console.log('hljs--',hljs.getLanguage('js'));
24

25
	const markdownIt = MarkdownIt({
26
		html: true,
DCloud_JSON's avatar
DCloud_JSON 已提交
27
		highlight: function(str, lang) {
28 29 30 31 32 33 34 35 36 37 38 39 40 41
			// if (lang && hljs.getLanguage(lang)) {
			// 	console.error('lang', lang)
			// 	try {
			// 		return '<pre class="hljs" style="padding: 5px 8px;margin: 5px 0;overflow: auto;"><code>' +
			// 			hljs.highlight('lang', str, true).value +
			// 			'</code></pre>';
			// 	} catch (__) {}
			// }

			try {
				return '<pre class="hljs" style="padding: 5px 8px;margin: 5px 0;overflow: auto;"><code>' +
					hljs.highlightAuto(str).value +
					'</code></pre>';
			} catch (__) {}
42

43 44
			return '<pre class="hljs" style="padding: 5px 8px;margin: 5px 0;overflow: auto;"><code>' +
				markdownIt.utils.escapeHtml(str) + '</code></pre>';
DCloud_JSON's avatar
DCloud_JSON 已提交
45 46
		}
	})
47

DCloud_JSON's avatar
DCloud_JSON 已提交
48 49 50
	export default {
		name: "msg",
		data() {
51 52 53
			return {
				left: "-100px",
				top: "-100px"
DCloud_JSON's avatar
1.0.0  
DCloud_JSON 已提交
54
			};
55 56 57 58 59 60 61 62
		},
		mounted() {
			// #ifdef H5
			// web端限制不选中文字时出现系统右键菜单
			let richTextBox = this.$refs['rich-text-box']
			if (richTextBox) {
				richTextBox.$el.addEventListener('contextmenu', (e) => {
					if (!document.getSelection().toString()) {
63
						// console.log(e);
64 65 66
						this.top = e.y + 'px'
						this.left = e.x + 'px'

67 68
						// console.log(e.x);
						// console.log(e.y);
69 70 71 72 73 74 75 76 77 78
						e.preventDefault()
					}
				})
			}

			document.addEventListener('click', () => {
				this.left = "-100px"
			})

			// #endif
DCloud_JSON's avatar
DCloud_JSON 已提交
79 80 81 82 83
		},
		props: {
			md: {
				type: String,
				default () {
84
					return ''
DCloud_JSON's avatar
DCloud_JSON 已提交
85 86
				}
			},
87 88 89 90 91 92
			showCursor: {
				type: [Boolean, Number],
				default () {
					return false
				}
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
93 94
		},
		computed: {
95
			html() {
96
				if(this.md.split("```").length%2){
DCloud_JSON's avatar
DCloud_JSON 已提交
97
					return markdownIt.render(this.md + ' \n <span class="cursor">|</span>');
98
				}else{
DCloud_JSON's avatar
1.0.3  
DCloud_JSON 已提交
99
					return markdownIt.render(this.md) + ' \n <span class="cursor">|</span>';
100
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
101
			},
102
			nodes() {
DCloud_JSON's avatar
DCloud_JSON 已提交
103 104 105
				// return this.html
				// console.log('parseHtml(this.html)',parseHtml(this.html));
				return parseHtml(this.html)
106 107 108 109 110 111 112 113 114 115 116 117
			}
		},
		methods: {
			// #ifdef H5
			copy() {
				uni.setClipboardData({
					data: this.md,
					showToast: false,
				})
				this.left = "-100px"
			}
			// #endif
118
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
119 120 121 122
	}
</script>

<style lang="scss">
DCloud_JSON's avatar
DCloud_JSON 已提交
123 124
	/* #ifndef VUE3 && APP-PLUS */
	@import "@/components/uni-ai-msg/uni-ai-msg.scss";
125
	/* #endif */
DCloud_JSON's avatar
DCloud_JSON 已提交
126
</style>