<template>
	<view class="container">
		<view class="toolbar" @touchend.stop="toolBarClick">
			<view class="iconfont icon-charutupian" @touchend.stop="insertImage"></view>
			<view :class="'iconfont icon-format-header-1 ' + (formats.header === 1 ? 'ql-active' : '')" data-name="header" :data-value="1"></view>
			<view :class="'iconfont icon-format-header-2 ' + (formats.header === 2 ? 'ql-active' : '')" data-name="header" :data-value="2"></view>
			<view :class="'iconfont icon-format-header-3 ' + (formats.header === 3 ? 'ql-active' : '')" data-name="header" :data-value="3"></view>
			<view :class="'iconfont icon-format-header-4 ' + (formats.header === 4 ? 'ql-active' : '')" data-name="header" :data-value="4"></view>
			<view :class="'iconfont icon-format-header-5 ' + (formats.header === 5 ? 'ql-active' : '')" data-name="header" :data-value="5"></view>
			<view :class="'iconfont icon-format-header-6 ' + (formats.header === 6 ? 'ql-active' : '')" data-name="header" :data-value="6"></view>
			<view :class="'iconfont icon-zitijiacu ' + (formats.bold ? 'ql-active' : '')" data-name="bold"></view>
			<view :class="'iconfont icon-zitishanchuxian ' + (formats.strike ? 'ql-active' : '')" data-name="strike"></view>
			<view :class="'iconfont icon-zitixieti ' + (formats.italic ? 'ql-active' : '')" data-name="italic"></view>
			<view :class="'iconfont icon-zuoduiqi ' + (formats.align === 'left' ? 'ql-active' : '')" data-name="align" data-value="left"></view>
			<view :class="'iconfont icon-juzhongduiqi ' + (formats.align === 'center' ? 'ql-active' : '')" data-name="align" data-value="center"></view>
			<view :class="'iconfont icon-youduiqi ' + (formats.align === 'right' ? 'ql-active' : '')" data-name="align" data-value="right"></view>
			<view :class="'iconfont icon-zuoyouduiqi ' + (formats.align === 'justify' ? 'ql-active' : '')" data-name="align" data-value="justify"></view>
			<view :class="'iconfont icon-line-height ' + (formats.lineHeight ? 'ql-active' : '')" data-name="lineHeight" data-value="2"></view>
			<view :class="'iconfont icon-Character-Spacing ' + (formats.letterSpacing ? 'ql-active' : '')" data-name="letterSpacing" data-value="2em"></view>
			<view :class="'iconfont icon-722bianjiqi_duanqianju ' + (formats.marginTop ? 'ql-active' : '')" data-name="marginTop" data-value="20px"></view>
			<view class="iconfont icon-clearedformat" @tap="removeFormat"></view>
			<view :class="'iconfont icon-font ' + (formats.fontFamily ? 'ql-active' : '')" data-name="fontFamily" data-value="Pacifico"></view>
			<view :class="'iconfont icon-fontsize ' + (formats.fontSize === '24px' ? 'ql-active' : '')" data-name="fontSize" data-value="24px"></view>
			<view class="iconfont icon-date" @tap="insertDate"></view>
			<view class="iconfont icon-undo" @tap="undo"></view>
			<view class="iconfont icon-redo" @tap="redo"></view>
			<view :class="'iconfont icon-zitixiahuaxian ' + (formats.underline ? 'ql-active' : '')" data-name="underline"></view>
			<view class="iconfont icon--checklist" data-name="list" data-value="check"></view>
			<view :class="'iconfont icon-youxupailie ' + (formats.list === 'ordered' ? 'ql-active' : '')" data-name="list" data-value="ordered"></view>
			<view :class="'iconfont icon-wuxupailie ' + (formats.list === 'bullet' ? 'ql-active' : '')" data-name="list" data-value="bullet"></view>
			<view class="iconfont icon-outdent" data-name="indent" data-value="-1"></view>
			<view class="iconfont icon-indent" data-name="indent" data-value="+1"></view>
			<view class="iconfont icon-fengexian" @tap="insertDivider"></view>
			<view :class="'iconfont icon-zitixiabiao ' + (formats.script === 'sub' ? 'ql-active' : '')" data-name="script" data-value="sub"></view>
			<view :class="'iconfont icon-zitishangbiao ' + (formats.script === 'super' ? 'ql-active' : '')" data-name="script" data-value="super"></view>
			<!-- <view class="iconfont icon-quanping"></view> -->
			<view class="iconfont icon-shanchu" @tap="clear"></view>
			<!-- <view :class="'iconfont icon-direction-rtl ' + (formats.direction === 'rtl' ? 'ql-active' : '')" data-name="direction" data-value="rtl"></view> -->
		</view>
		<editor :id="editorId" show-img-size :read-only="isEdit" show-img-resize show-img-toolbar class="ql-container" :placeholder="placeholder" @statuschange="onStatusChange" @ready="onEditorReady" @input="eventhandle"></editor>
	</view>
</template>

<script>
	import Emitter from '@/uview-ui/libs/util/emitter.js';
	export default {
		name: 'fa-editor',
		mixins: [Emitter],
		props: {
			value: {
				type: String,
				default: ''
			},
			type: {
				type: String,
				default: 'html'
			},
			html: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				isEdit: false,
				placeholder: '开始输入...',
				editorId: this.$u.guid(10),
				formats: {}
			};
		},
		methods: {
			onStatusChange(e) {
				this.formats = e.detail;
			},
			onEditorReady() {
				let that = this;
				uni.createSelectorQuery()
					.in(this)
					.select(`#${that.editorId}`)
					.context(function(res) {
						that.editorCtx = res.context;
						//初始化数据
						if (that.html) {
							res.context.setContents({
								html: that.html,
								complete: function(e) {
									console.log(e);
								}
							});
						}
					})
					.exec();
			},
			toolBarClick(e) {
				let { name, value } = e.target.dataset;
				if (!name) return;
				this.editorCtx.format(name, value);
			},
			undo() {
				this.editorCtx.undo();
			},
			redo() {
				this.editorCtx.redo();
			},
			blur() {
				this.editorCtx.blur();
			},
			removeFormat() {
				this.editorCtx.removeFormat();
			},
			//插入横线
			insertDivider() {
				this.editorCtx.insertDivider({
					success: function() {
						console.log('insert divider success');
					}
				});
			},
			//插入时间
			insertDate() {
				const date = new Date();
				const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
				this.editorCtx.insertText({
					text: formatDate
				});
			},
			clear() {
				this.editorCtx.clear({
					success: function(res) {
						console.log('clear success');
					}
				});
			},
			insertImage() {
				uni.chooseImage({
					count: 9,
					success: chooseImageRes => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						tempFilePaths.map(async item => {
							try {
								let res = await this.$api.goUpload({ filePath: item });
								this.editorCtx.insertImage({
									src: res.data.fullurl,
									data: {
										id: 'abcd',
										role: 'god'
									},
									width: '80%',
									success: function() {
										console.log('insert image success');
									}
								});
							} catch (e) {
								this.$u.toast('上传失败！');
							}
						});
					}
				});
			},
			getData() {
				return new Promise((resolve, reject) => {
					this.editorCtx.getContents({
						success: function(res) {
							res.html = res.html.replace(/wx:nodeid="\d+"/g,'');
							resolve(res);
						},
						fail: function(err) {
							reject(err);
						}
					});
				});
			},
			eventhandle(e) {
				console.log(e);
				let value = this.type == 'html' ? e.detail.html : e.detail.text;
				this.$emit('input', value);
				setTimeout(() => {
					this.dispatch('u-form-item', 'on-form-blur', value);
				}, 50);
			}
		}
	};
</script>

<style lang="scss" scoped>
	@font-face {
		font-family: 'iconfont';
		src: url('//at.alicdn.com/t/font_945958_zfsfjju1dim.eot?t=1547618146468');
		/* IE9 */
		src: url('//at.alicdn.com/t/font_945958_zfsfjju1dim.eot?t=1547618146468#iefix') format('embedded-opentype'),
			/* IE6-IE8 */
			url('data:application/x-font-woff2;charset=utf-8;base64, ') format('woff2'),
			url('//at.alicdn.com/t/font_945958_zfsfjju1dim.woff?t=1547618146468') format('woff'),
			url('//at.alicdn.com/t/font_945958_zfsfjju1dim.ttf?t=1547618146468') format('truetype'),
			/* chrome, firefox, opera, Safari, Android, iOS 4.2+ */
			url('//at.alicdn.com/t/font_945958_zfsfjju1dim.svg?t=1547618146468#iconfont') format('svg');
		/* iOS 4.1- */
	}

	.iconfont {
		font-family: 'iconfont' !important;
		font-size: 16px;
		font-style: normal;
		-webkit-font-smoothing: antialiased;
		-moz-osx-font-smoothing: grayscale;
	}

	.icon-redo:before {
		content: '\e627';
	}

	.icon-undo:before {
		content: '\e633';
	}

	.icon-indent:before {
		content: '\eb28';
	}

	.icon-outdent:before {
		content: '\e6e8';
	}

	.icon-fontsize:before {
		content: '\e6fd';
	}

	.icon-format-header-1:before {
		content: '\e860';
	}

	.icon-format-header-4:before {
		content: '\e863';
	}

	.icon-format-header-5:before {
		content: '\e864';
	}

	.icon-format-header-6:before {
		content: '\e865';
	}

	.icon-clearup:before {
		content: '\e64d';
	}

	.icon-preview:before {
		content: '\e631';
	}

	.icon-date:before {
		content: '\e63e';
	}

	.icon-fontbgcolor:before {
		content: '\e678';
	}

	.icon-clearedformat:before {
		content: '\e67e';
	}

	.icon-font:before {
		content: '\e684';
	}

	.icon-723bianjiqi_duanhouju:before {
		content: '\e65f';
	}

	.icon-722bianjiqi_duanqianju:before {
		content: '\e660';
	}

	.icon-text_color:before {
		content: '\e72c';
	}

	.icon-format-header-2:before {
		content: '\e75c';
	}

	.icon-format-header-3:before {
		content: '\e75d';
	}

	.icon--checklist:before {
		content: '\e664';
	}

	.icon-baocun:before {
		content: '\ec09';
	}

	.icon-line-height:before {
		content: '\e7f8';
	}

	.icon-quanping:before {
		content: '\ec13';
	}

	.icon-direction-rtl:before {
		content: '\e66e';
	}

	.icon-direction-ltr:before {
		content: '\e66d';
	}

	.icon-selectall:before {
		content: '\e62b';
	}

	.icon-fuzhi:before {
		content: '\ec7a';
	}

	.icon-shanchu:before {
		content: '\ec7b';
	}

	.icon-bianjisekuai:before {
		content: '\ec7c';
	}

	.icon-fengexian:before {
		content: '\ec7f';
	}

	.icon-dianzan:before {
		content: '\ec80';
	}

	.icon-charulianjie:before {
		content: '\ec81';
	}

	.icon-charutupian:before {
		content: '\ec82';
	}

	.icon-wuxupailie:before {
		content: '\ec83';
	}

	.icon-juzhongduiqi:before {
		content: '\ec84';
	}

	.icon-yinyong:before {
		content: '\ec85';
	}

	.icon-youxupailie:before {
		content: '\ec86';
	}

	.icon-youduiqi:before {
		content: '\ec87';
	}

	.icon-zitidaima:before {
		content: '\ec88';
	}

	.icon-xiaolian:before {
		content: '\ec89';
	}

	.icon-zitijiacu:before {
		content: '\ec8a';
	}

	.icon-zitishanchuxian:before {
		content: '\ec8b';
	}

	.icon-zitishangbiao:before {
		content: '\ec8c';
	}

	.icon-zitibiaoti:before {
		content: '\ec8d';
	}

	.icon-zitixiahuaxian:before {
		content: '\ec8e';
	}

	.icon-zitixieti:before {
		content: '\ec8f';
	}

	.icon-zitiyanse:before {
		content: '\ec90';
	}

	.icon-zuoduiqi:before {
		content: '\ec91';
	}

	.icon-zitiyulan:before {
		content: '\ec92';
	}

	.icon-zitixiabiao:before {
		content: '\ec93';
	}

	.icon-zuoyouduiqi:before {
		content: '\ec94';
	}

	.icon-duigoux:before {
		content: '\ec9e';
	}

	.icon-guanbi:before {
		content: '\eca0';
	}

	.icon-shengyin_shiti:before {
		content: '\eca5';
	}

	.icon-Character-Spacing:before {
		content: '\e964';
	}

	.input-content {
		width: 100%;
	}

	.input-content textarea {
		padding: 5%;
		font-size: 30rpx;
		line-height: 1.5;
		width: 90%;
		border: 1px solid #eee;
	}

	.container {
		min-height: 600rpx;
		width: 100%;
		border: 1px solid #e0e0e0;

		.ql-container {
			box-sizing: border-box;
			width: 100%;
			height: 100%;
			font-size: 14px;
			line-height: 1.5;
			overflow: auto;
			padding: 5rpx 20rpx;
			word-break: break-word;
		}

		.toolbar {
			width: 100%;
			border: none;

			.iconfont {
				display: inline-block;
				cursor: pointer;
				height: 60rpx;
				width: 60rpx;
				margin: 13rpx 0;
				font-size: 33rpx;
				color: #000;
				text-align: center;
				background: none;
				border: none;
				outline: none;
				line-height: 2.2;
				vertical-align: middle;
			}

			.ql-active {
				background-color: #ecf5ff;
				border: 1px solid #eee;
			}
		}
	}
</style>
