uni-app使用 editor 富文本

演示地址
github地址

<template>
	<view 
		class="editor-container flex" 
		:class="[position === 'top' ? 'flex-direction' : 'flex-reverse']"
	>
	
		<view class="toolbar" :style="barStyle" @click="format">
			<view :class="formats.bold ? 'text-blue' : ''" class="iconfont icon-zitijiacu" data-name="bold">
			</view>
			<view :class="formats.italic ? 'text-blue' : ''" class="iconfont icon-zitixieti" data-name="italic"></view>
			
			<view :class="formats.underline ? 'text-blue' : ''" class="iconfont icon-zitixiahuaxian" data-name="underline"></view>
			
			<view :class="formats.align === 'left' ? 'text-blue' : ''" class="iconfont icon-zuoduiqi" data-name="align" data-value="left"></view>
			
			<view :class="formats.align === 'center' ? 'text-blue' : ''" class="iconfont icon-juzhongduiqi" data-name="align" data-value="center"></view>
			 
			<view :class="formats.align === 'right' ? 'text-blue' : ''" class="iconfont icon-youduiqi" data-name="align" data-value="right"></view>
			 
			<view :class="formats.align === 'justify' ? 'text-blue' : ''" class="iconfont icon-zuoyouduiqi" data-name="align" data-value="justify"></view>
			
			<view class="iconfont icon-ClearFormatting-1" @tap="removeFormat"></view>
			
			<!-- <view :class="formats.backgroundColor === '#00ff00' ? 'text-blue' : ''" class="iconfont icon-fontbgcolor" data-name="backgroundColor" data-value="#00ff00"></view> -->
			
			<view :class="formats.list === 'ordered' ? 'text-blue' : ''" class="iconfont icon-youxupailie" data-name="list" data-value="ordered"></view>
			
			<view :class="formats.list === 'bullet' ? 'text-blue' : ''" class="iconfont icon-wuxupailie" data-name="list" data-value="bullet"></view>
			
			<view class="iconfont icon-Undo" @tap="undo"></view>
			<view class="iconfont icon-Redo" @tap="redo"></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-charutupian" @tap="insertImage"></view>
			
		</view>
		
		<view class="editor-wrapper solid padding-xs" :style="{ 'min-height': minHeight, 'height': height }">
			<editor
				id="editor"
				class="ql-container"
				:placeholder="placeholder"
				:show-img-size="showImgSize"
				:show-img-toolbar="showImgToolBar"
				:show-img-resize="showImgResize"
				@statuschange="onStatusChange"
				@ready="ready"
				@input="input"
			></editor>
		</view>
	</view>
</template>

<script>
  import { uploadImage } from '@/api/public.js'
	export default {
		props: {
			placeholder: {
				type: String,
				default: '说点什么吧...'
			},
			// 点击图片时显示图片大小控件
			showImgSize: {
				type: Boolean,
				default: false
			},
			// 点击图片时显示工具栏控件
			showImgToolBar: {
				type: Boolean,
				default: false
			},
			// 点击图片时显示修改尺寸控件
			showImgResize: {
				type: Boolean,
				default: false
			},
			// 工具栏位置 top bottom
			position: {
				type: String,
				default: 'top'
			},
			minHeight: {
				type: String,
				default: '400rpx'
			},
			height: {
				type: String,
				default: '400rpx'
			},
			barStyle: {
				type: String | Object,
				default: 'background-color: #D1F0E6'
			},
			// 图片上传地址
			uploadUrl: {
				type: String,
				default: ''
			},
			// 是否自动上传图片
			autoUpload: {
				type: Boolean,
				default: true
			},
			// 初始化 HTML
			html: {
				type: String,
				default: ''
			}
		},
		data () {
			return {
				formats: {},
        content: ''
			}
		},
		methods: {
			ready () {
				uni.createSelectorQuery().in(this).select('#editor').fields({
						size: true,
						context: true
					}, res => {
						this.editorCtx = res.context;
						this.editorCtx.setContents({
							html: this.html
						})
					})
					.exec();
				// uni.createSelectorQuery().in(this).select('#editor').context((res) => {
				// 	this.editorCtx = res.context
				// 	this.editorCtx.setContents({
				// 		html: this.html
				// 	})
				// }).exec()
			},
			format (e) {
				let {
					name,
					value
				} = e.target.dataset
				this.editorCtx.format(name, value)
			},
			onStatusChange(e) {
				const formats = e.detail
				this.formats = formats
			},
			undo() {
				this.editorCtx.undo()
			},
			redo() {
				this.editorCtx.redo()
			},
			removeFormat () {
				this.editorCtx.removeFormat()
			},
			insertDivider() {
				this.editorCtx.insertDivider()
			},
			insertImage() {
				uni.chooseImage({
					count: 1,
					success: (res) => {
						this.editorCtx.insertImage({
							src: res.tempFilePaths[0],
							alt: '图片',
							success: (r) => {
								if (!this.autoUpload) return
								if (!this.uploadUrl) return
								// 自动上传
								this.handleUploadFile(res.tempFilePaths[0])
							}
						})
					}
				})
			},
      // 依次上传图片
      handleUploadFile(url) {
        this.$http
          .upload(`${uploadImage}`, { filePath: url, name: 'file' })
          .then(res => {
            if (res.success) {
              let regAll = /<img.*?(?:>|\/>)/g
              this.editorCtx.getContents({
              	success: r => {
                  // console.log(r);
                  const group = r.html.match(regAll)
                  // console.log('group', group);
                  group.map(item => {
                    this.content = r.html.replace(item, `<img src="${res.message}" />`)
                  })
                  // console.log(this.content);
                  this.$emit('input', this.content)
              	}
              })
              
            }
          })
      },
			input () {
				this.editorCtx.getContents({
					success: res => {
            this.content = res.html
						this.$emit('input', this.content)
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.editor-container {
		width: 100%;
	}
	
	.toolbar {
		display: flex;
		flex-wrap: wrap;
		font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
		.iconfont {
			display: inline-flex;
			align-items: center;
			width: 48rpx;
			height: 48rpx;
			box-sizing: content-box;
			padding: 16rpx 16rpx;
			font-size: 40rpx;
		}
	}
	
	.editor-wrapper {
		background: #fff;
	}
	
	.ql-container {
		width: 100%;
		min-height: 0;
		height: 100%;
	}
</style>
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值