uni-ai-msg.vue 3.8 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 16 17
	import MarkdownIt from '@/lib/markdown-it.min.js';
	import hljs from "@/lib/highlight/highlight.min.js";
	
18 19 20 21 22
	// import hljs from "highlight.js";
	// import parseHtml from '@/lib/html-parser.js';

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

24
	const markdownIt = MarkdownIt({
25
		html: true,
DCloud_JSON's avatar
DCloud_JSON 已提交
26
		highlight: function(str, lang) {
27 28 29 30 31 32 33 34 35 36 37 38 39 40
			// 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 (__) {}
41

42 43
			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 已提交
44 45
		}
	})
46

DCloud_JSON's avatar
DCloud_JSON 已提交
47 48 49
	export default {
		name: "msg",
		data() {
50 51 52
			return {
				left: "-100px",
				top: "-100px"
DCloud_JSON's avatar
1.0.0  
DCloud_JSON 已提交
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
		},
		mounted() {
			// #ifdef H5
			// web端限制不选中文字时出现系统右键菜单
			let richTextBox = this.$refs['rich-text-box']
			if (richTextBox) {
				richTextBox.$el.addEventListener('contextmenu', (e) => {
					if (!document.getSelection().toString()) {
						console.log(e);
						this.top = e.y + 'px'
						this.left = e.x + 'px'

						console.log(e.x);
						console.log(e.y);
						e.preventDefault()
					}
				})
			}

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

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

<style lang="scss">
121
	@import "@/lib/highlight/github-dark.min.css";
DCloud_JSON's avatar
DCloud_JSON 已提交
122 123

	/* #ifndef APP-NVUE */
124
	.rich-text-box ::v-deep pre.hljs {
125
		padding: 5px 8px;
DCloud_JSON's avatar
DCloud_JSON 已提交
126
		margin: 5px 0;
DCloud_JSON's avatar
1.0.0  
DCloud_JSON 已提交
127
		overflow: auto;
128 129 130 131 132 133 134 135 136 137 138
	}

	.cursor {
		display: none;
	}

	.show-cursor .cursor {
		display: inline-block;
		color: blue;
		font-weight: bold;
		animation: blinking 1s infinite;
139 140 141 142 143 144 145 146 147 148
	}

	@keyframes blinking {
		from {
			opacity: 1.0;
		}

		to {
			opacity: 0.0;
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
149
	}
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

	/* #endif */

	/* #ifdef H5 */
	.copy-box {
		position: fixed;
	}

	// .copy-mask{
	// 	background-color: rgba(255,255,255,0.5);
	// 	width: 100vw;
	// 	height: 100vh;
	// 	position: fixed;
	// 	top: 0;
	// 	left: 0;
	// 	z-index: 9;
	// }
	.copy {
		position: fixed;
		background-color: #fff;
		box-shadow: 0 0 3px #aaa;
		padding: 5px;
		border-radius: 5px;
		z-index: 999;
		cursor: pointer;
		font-size: 14px;
		color: #222;
	}

	.copy:hover {
		color: #00a953;
	}

	/* #endif */
DCloud_JSON's avatar
DCloud_JSON 已提交
184
</style>