富文本rich-text

<template>
	<div class="rich-text">
		<quill-editor ref="text" v-model="htmlString" class="myQuillEditor" :options="editorOption" />
	</div>
</template>

<script>
	import 'quill/dist/quill.core.css' // import styles
	import 'quill/dist/quill.snow.css' // for snow theme
	import 'quill/dist/quill.bubble.css' // for bubble theme
	import {
		quillEditor,
		Quill
	} from 'vue-quill-editor';
	import {
		container,
		addQuillTitle
	} from 'quill-video-image-module';
	import {
		ImageExtend,
		QuillWatch
	} from 'quill-video-image-module';
	import {
		VideoExtend,
		QuillVideoWatch
	} from 'quill-video-image-module'
	// import ImageResize from 'quill-image-resize-module'
	// 引入video模块并注册
	import video from 'quill-video-image-module/video'
	Quill.register(video, true)
	// Quill.register('modules/ImageResize', ImageResize)
	Quill.register('modules/ImageExtend', ImageExtend)
	Quill.register('modules/VideoExtend', VideoExtend)

	const toolbarOptions = [
		['bold', 'italic', 'underline', 'strike'], // 加粗 斜体 下划线 删除线 -----['bold', 'italic', 'underline', 'strike']
		['blockquote', 'code-block'], // 引用  代码块-----['blockquote', 'code-block']
		[{
			header: 1
		}, {
			header: 2
		}], // 1、2 级标题-----[{ header: 1 }, { header: 2 }]
		[{
			list: 'ordered'
		}, {
			list: 'bullet'
		}], // 有序、无序列表-----[{ list: 'ordered' }, { list: 'bullet' }]
		[{
			script: 'sub'
		}, {
			script: 'super'
		}], // 上标/下标-----[{ script: 'sub' }, { script: 'super' }]
		[{
			indent: '-1'
		}, {
			indent: '+1'
		}], // 缩进-----[{ indent: '-1' }, { indent: '+1' }]
		// [{'direction': 'rtl'}], // 文本方向-----[{'direction': 'rtl'}]
		[{
			size: ['small', false, 'large', 'huge']
		}], // 字体大小-----[{ size: ['small', false, 'large', 'huge'] }]
		// [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题-----[{ header: [1, 2, 3, 4, 5, 6, false] }]
		[{
			color: []
		}, {
			background: []
		}], // 字体颜色、字体背景颜色-----[{ color: [] }, { background: [] }]
		// [{ font: [] }], // 字体种类-----[{ font: [] }]
		[{
			align: []
		}], // 对齐方式-----[{ align: [] }]
		['clean'], // 清除文本格式-----['clean']
		['image'], // 链接、图片、视频-----['link', 'image', 'video']
		['link'],
		['video'],
	];
	export default {
		name: 'richText',
		components: {
			quillEditor
		},
		props: {
			htmlStr: { // 富文本回显
				type: String
			}
		},
		// 禁调:富文本复制功能

		data() {
			// 测试数据
			let token =
				// 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJtb2JpbGUiOiI0ODEiLCJleHAiOjE2MTE4MTI4MzR9.X5zyf6jvuSbt6gBUkkkcQ2zbmQDZ3jL_4MS5InnTOyc'
				'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJtb2JpbGUiOiI0ODEiLCJleHAiOjE2MTE5MDI2OTB9.TEJNKgWUb23ZI2xyyxTsAOcIruJo30SkOlcwGHVjagw'
			return {
				htmlString: '',
				textPicSuc: true, // 是否有正在上传的信息
				editorOption: {
					theme: "snow", // or 'bubble'
					placeholder: "请输入",
					modules: {
						// ImageResize: {},
						ImageExtend: {
							loading: true,
							name: 'file',
							size: 15, // M
							action: '',
							response: (res) => {
								return res.data.path
							},
							sizeError: (res) => {
								this.$message({
									message: '上传图片不能超过10M',
									type: 'error'
								});
							},
							start: () => {
								this.textPicSuc = false
							},
							error: () => {
								this.$message({
									message: '图片上传失败,请重新上传',
									type: 'error'
								});
							},
							success: () => {
								this.textPicSuc = true
							},
						},
						VideoExtend: {
							loading: true,
							name: 'file',
							action: '',
							// size: 100, // M
							headers: (xhr) => {
								// set custom token(optional)
								// xhr.setRequestHeader('token', this.token)
							},
							response: (res) => {
								return res.data.path
							},
							start: () => {
								this.textPicSuc = false
							},
							error: () => {
								this.$message({
									message: '视频上传失败,请重新上传',
									type: 'error'
								});
							},
							success: () => {
								this.textPicSuc = true
							},
						},
						toolbar: {
							container: toolbarOptions,
							handlers: {
								'image': function(val, a, b) {
									QuillWatch.emit(this.quill.id)
								},
								'video': function() {
									QuillVideoWatch.emit(this.quill.id)
								}
							}
						}
					}
				}
			}
		},
		mounted: function() {
			if (this.htmlStr) {
				this.htmlString = this.htmlStr
			}
		},
		methods: {}
	}
</script>

<style scoped>
	.myQuillEditor {
		height: 500px;

		>>>.ql-container {
			height: 85%;
		}
	}
</style>
<!-- https://github.com/lw1995/quill-video-image-module -->


### 如何在 UniApp 中使用 `rich-text` 组件插入和显示富文本内容 #### 解决富文本显示问题 当遇到接口返回的数据带有转义字符,导致 `rich-text` 组件无法正确解析HTML标签的情况时,可以通过解码函数处理字符串来解决这个问题。对于含有转义符的HTML代码,在将其传递给 `rich-text` 前应先转换成可被识别的形式[^1]。 #### 数据绑定与节点属性修改 为了确保 HTML 文件能够被适当地呈现出来,可以采用如下方式通过 `nodes` 属性向 `<rich-text>` 传入要渲染的内容: ```html <template> <view> <!-- 使用 v-html 或者 :nodes 来绑定数据 --> <rich-text :nodes="formattedHtml"></rich-text> </view> </template> <script> export default { data() { return { formattedHtml: '' } }, onLoad(option) { let rawHtml = '<p>这是一个测试。</p><img src="example.jpg"/>'; // 对于来自服务器带转义字符的数据需做unescape操作 const unescapedData = this.escape2Html(rawHtml); // 可选:调整 img 标签样式以适应不同屏幕尺寸 this.formattedHtml = unescapedData.replace(/<img /g, '<img style="max-width:100%;height:auto;" '); }, methods:{ escape2Html(str){ var arrayEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'}; return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrayEntities[t];}); } } } </script> ``` 上述代码展示了如何接收原始HTML字符串,并对其进行必要的替换以便更好地控制图像和其他元素的表现形式[^2]。 #### 图片点击事件支持 如果希望实现点击图片放大查看的功能,则需要额外编写逻辑监听用户的交互行为,并调用微信内置API完成大图浏览效果。不过需要注意的是,标准版 `rich-text` 并不直接提供这样的功能,可能需要借助其他插件或是自定义组件来增强用户体验[^3]。 #### 自定义样式应用 针对特定需求定制化样式的场景下,可以在服务端预先对某些标签添加CSS类名,之后再由前端统一管理这些类对应的视觉表现。例如下面的例子说明了怎样为所有的段落和图片指定不同的外观风格[^4]: ```php // 后端PHP示例 $detail['content'] = str_replace('<img', '<img class="custom-img"', $detail['content']); $detail['content'] = str_replace('<p', '<p class="custom-p"', $detail['content']); ``` 随后就可以依据 `.custom-img`,`.custom-p` 类名设定相应的 CSS 规则了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值