<template>
	<div>
		<p style="color: red;margin: 10px;">* 将您从word复制的试题直接粘贴在下面编辑器并进行打点后即可一键生成试题</p>
		<div class="exersices-content">
			<div style="margin:  0 10px;">
				<div ref="editor" style="text-align:left"></div>
			</div>
		</div>
	</div>
</template>
<script>
	import E from 'wangeditor'
	export default {
		data() {
			return {
				stemContent: '',
				stemEditor: null,
				answerContent: '',
				answerEditor: null

			}
		},
		methods: {
			
		},
		mounted() {
			let stemEditor = new E(this.$refs.editor)
			stemEditor.config.onchange = (html) => {
				this.stemContent = html
			}
			stemEditor.config.uploadImgShowBase64 = true;
			this.$editorTools.initSimpleEditor(stemEditor,this)
			stemEditor.config.pasteFilterStyle = true
			stemEditor.config.height = 800
			stemEditor.create()
			

			function hexToBase64(str) {
			    var bString = "";
			    for( var i = 0; i < str.length; i +=2) {
			         bString += String.fromCharCode( parseInt( str.substr( i, 2), 16));
			    }
			    return "data:image/png; base64," + btoa(bString);
			}

			document.addEventListener('paste', function(e) {
				for (var i = 0, len = e.clipboardData.items.length; i < len; i++) {
					var item = e.clipboardData.items[i];
					console.log(item);
					if (item.kind === "string" && item.type === 'text/rtf') {
						item.getAsString(function(str) {
							console.log(str)
                            str = str.replace(/[\r]/igm, '').replace(/[\n]/igm, '').replace(/[\r\n]/igm, '')
							console.log(str)
							var pattern = /\\pngblip.*?\}\{/g;
							// var pattern = /(?:\\pngblip[\s\S].+\})([\s\S].[\s\S])+(?=\})/g;
							var srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/ig;
							let result = str.match(pattern)
							console.log(result)
							if(!result) return
							let base64Arr = result.map(i => hexToBase64(i.split('}')[1]))
							let html = stemEditor.txt.html()
							console.log(html.match(srcReg))
							if(html.match(srcReg).length){
								html.match(srcReg).filter(j => j.includes('file://')).forEach((i,index) => {
										html = html.replace(i,`src="${base64Arr[index]}"`)
								})
								stemEditor.txt.clear()
								stemEditor.txt.html(html)
							}
						})
					}
				}
			})
		},

	}
</script>

