uni-ai-msg.vue 3.2 KB
Newer Older
DCloud_JSON's avatar
1.0.0  
DCloud_JSON 已提交
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>
DCloud_JSON's avatar
1.0.0  
DCloud_JSON 已提交
4 5 6 7 8 9
		<!-- #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 已提交
10 11 12 13 14
	</view>
</template>

<script>
	import parseHtml from './html-parser.js';
15 16 17
	import MarkdownIt from "markdown-it";
	import hljs from 'highlight.js';

DCloud_JSON's avatar
DCloud_JSON 已提交
18
	const md = new MarkdownIt({
19
		html: true,
DCloud_JSON's avatar
DCloud_JSON 已提交
20 21 22
		highlight: function(str, lang) {
			if (lang && hljs.getLanguage(lang)) {
				try {
DCloud_JSON's avatar
1.0.0  
DCloud_JSON 已提交
23
					return '<pre class="hljs" style="padding: 5px 8px;margin: 5px 0;overflow: auto;"><code>' +
24 25
						hljs.highlight(lang, str, true).value +
						'</code></pre>';
DCloud_JSON's avatar
DCloud_JSON 已提交
26 27
				} catch (__) {}
			}
28

DCloud_JSON's avatar
1.0.0  
DCloud_JSON 已提交
29
			return '<pre class="hljs" style="padding: 5px 8px;margin: 5px 0;overflow: auto;"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
DCloud_JSON's avatar
DCloud_JSON 已提交
30 31 32 33 34
		}
	})
	export default {
		name: "msg",
		data() {
DCloud_JSON's avatar
1.0.0  
DCloud_JSON 已提交
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
			return {
				left:"-100px",
				top:"-100px"
			};
		},
		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 已提交
63 64 65 66 67
		},
		props: {
			md: {
				type: String,
				default () {
68
					return ''
DCloud_JSON's avatar
DCloud_JSON 已提交
69 70
				}
			},
71 72 73 74 75 76
			showCursor: {
				type: [Boolean, Number],
				default () {
					return false
				}
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
77 78
		},
		computed: {
DCloud_JSON's avatar
1.0.0  
DCloud_JSON 已提交
79 80 81
			html() {
				let html = md.render(this.md + '<span class="cursor">|</span>')
				return html
DCloud_JSON's avatar
DCloud_JSON 已提交
82
			},
83
			nodes() {
DCloud_JSON's avatar
DCloud_JSON 已提交
84
				return parseHtml(this.html)
85
			}
DCloud_JSON's avatar
1.0.0  
DCloud_JSON 已提交
86 87 88 89 90 91 92 93 94 95 96
		},
		methods:{
			// #ifdef H5
			copy(){
				uni.setClipboardData({
					data:this.md,
					showToast:false,
				})
				this.left = "-100px"
			}
			// #endif
97
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
98 99 100 101 102
	}
</script>

<style lang="scss">
	@import "highlight.js/styles/agate.css";
103
	@import "highlight.js/styles/a11y-dark.css";
DCloud_JSON's avatar
DCloud_JSON 已提交
104 105

	/* #ifndef APP-NVUE */
DCloud_JSON's avatar
1.0.0  
DCloud_JSON 已提交
106
	.rich-text-box ::v-deep pre.hljs{
107
		padding: 5px 8px;
DCloud_JSON's avatar
DCloud_JSON 已提交
108
		margin: 5px 0;
DCloud_JSON's avatar
1.0.0  
DCloud_JSON 已提交
109
		overflow: auto;
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	}
	
	.cursor{
		display: none;
	}
	.show-cursor .cursor {
		display: inline-block;
		color: blue;
		font-weight: bold;
		animation: blinking 1s infinite;
	}

	@keyframes blinking {
		from {
			opacity: 1.0;
		}

		to {
			opacity: 0.0;
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
130
	}
DCloud_JSON's avatar
1.0.0  
DCloud_JSON 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	/* #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 已提交
161
</style>