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

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

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

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

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

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

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

	.cursor {
		display: none;
	}

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

	@keyframes blinking {
		from {
			opacity: 1.0;
		}

		to {
			opacity: 0.0;
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
148
	}
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

	/* #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 已提交
183
</style>