vue3uniapps使用富文本mp-html插件

news/2024/10/12 10:26:16

1. 实现效果

具体需求:顶部是搜索栏,包括搜索结果个数,目前跳到第几个,包含上一个、下一个按钮。富文本区域关键词高亮黄色,当前关键词为高亮橙色。如图

2. 版本号

用到vue3 和 uniapp , mp-html 插件版本是v2.5.0,

插件地址:https://ext.dcloud.net.cn/plugin?id=805

用npm方式打包放到compenonts文件,如图,打包步骤详见官方文档。

3. 页面具体代码

<template><view class="htmlparsePage"><view class="fixedTop"><topbar :config="barConfig"></topbar><view class="search-bar-container"><view class="weui-search-bar__form"><view class="weui-search-bar__box"><icon class="weui-icon-search_in-box" type="search" size="14"></icon><inputclass="weui-search-bar__input"placeholder-style="color:#b9babd;"placeholder="请输入需要搜索的关键词"v-model="keyWord"confirm-type="search"@confirm="searchSelf(keyWord)"/><viewclass="weui-icon-clear"v-if="keyWord?.length > 0"@click="() => {keyWord = ''searchSelf('')}"><icon type="clear" size="14"></icon></view></view></view><view class="a-search" style="margin-left: 10rpx; margin-right: 10rpx" v-if="searchRes?.num"><view class="searchNum">{{ searchIndex }}/{{ searchRes?.num }}</view></view><view class="a-search" style="margin-right: 10rpx" v-if="searchRes?.num" @click="preSearch"><image mode="widthFix" src="../../static/img/up.svg"></image></view><view class="a-search" @click="nextSearch" v-if="searchRes?.num"><image mode="widthFix" src="../../static/img/down.svg"></image></view></view></view><view v-if="show && data.content" class="mpHtmlRef"><mp-htmlref="mp"use-anchorsearchcontainer-style="padding:10px":content="data.content"@linktap="navigate"@preview-img="false"@show-img-menu="false":selectable="true":tag-style="{table: 'border-collapse: collapse;border: solid black 1px',tr: 'border: solid black 1px',td: 'border: solid black 1px',}"@load="onMpLoaded"></mp-html></view><view v-else class="emptyBox"><empty /></view></view>
</template><script setup>
import empty from '../../components/empty/empty'
import mpHtml from '../../components/mp-html/mp-html'   // 引入
import { getDocDetail } from '../../api/filterList'
import topbar from '../../components/topbar/topbar.vue'
import { onLoad, onUnload, onShareAppMessage } from '@dcloudio/uni-app'
import { ref, reactive, onMounted, nextTick } from 'vue'
const mp = ref(null) //富文本对象
const id = ref('')
const show = ref(true)
const keyWord = ref('')
const data = reactive({content: '',
})
const barConfig = reactive({bg_color: 'transparent',color: '#000',flag: 1,name: '法规详情',
})
const searchRes = ref(null)
const searchIndex = ref(1)function navigate(e) {this.$nextTick(() => {uni.navigateTo({url: '../detail/analysis?id=' + e.href,success: function (res) {},fail: function (res) {},complete: function (res) {},})})
}onUnload(() => {uni.hideLoading()
})onLoad((options) => {id.value = options.idkeyWord.value = options.searchInput || '' // 关键词高亮
  getData()
})

// 获取文本数据 const getData
= async () => {uni.showLoading()getDocDetail({ id: id.value }).then((res) => {if (res.code === 200) {data.content = res.data.contentuni.hideLoading()}}).catch((e) => {uni.hideLoading()uni.showToast({title: '文档详情获取失败',icon: 'none',})}) }// 处理 mp-html 加载完成 const onMpLoaded = async () => {if (keyWord.value) {searchSelf(keyWord.value)} }const searchSelf = (key) => {mp.value.search(key, true).then((res) => {searchRes.value = ressearchIndex.value = 1if (res.num === 0) {uni.showToast({title: '没有了',icon: 'none',duration: 2000,})} else {show.value = falseres.highlight(1)res.jump(1, -343) // 高亮第 1 个结果并跳转到该位置,偏移量show.value = true}}) } // 下一个关键词 const nextSearch = () => {show.value = falseif (searchIndex.value < searchRes.value?.num) {searchIndex.value++searchRes.value.highlight(searchIndex.value)searchRes.value.jump(searchIndex.value, -343)show.value = true} else {uni.showToast({title: '没有了',icon: 'none',duration: 2000,})show.value = true} }
// 上一个关键词 const preSearch
= () => {show.value = falseif (searchIndex.value > 1 && searchIndex.value <= searchRes.value.num) {searchIndex.value--searchRes.value.highlight(searchIndex.value)searchRes.value.jump(searchIndex.value, -343)show.value = true} else {uni.showToast({title: '没有了',icon: 'none',duration: 2000,})show.value = true} } </script><style> .htmlparsePage {height: 100%;background: #e5f7fe; } .fixedTop {background: #e5f7fe;padding-bottom: 10rpx;position: fixed;top: 0;left: 0;height: 266rpx;z-index: 999; } .mpHtmlRef {height: calc(100% - 273rpx);margin-top: 273rpx;overflow-y: auto;background: #ffffff;border-radius: 48rpx 48rpx 0px 0px; } .emptyBox {margin-top: 30vh; } .emptyBox .emptyCon image {width: 280rpx;height: 280rpx; }.search-bar-container {background: #e5f7fe;padding: 15rpx 20rpx 0;height: 60rpx;display: flex; } .weui-search-bar__input {height: 60rpx;line-height: 28px;font-size: 24rpx;color: #939699; } .weui-search-bar__form {border: unset;border-radius: 30rpx;height: 60rpx; }.weui-search-bar__box {padding-left: 70rpx;height: 60rpx; }.weui-icon-search_in-box {left: 30rpx;float: left;top: 20rpx; }.a-search {height: 60rpx;display: flex;align-items: center;background-color: #fff;border-radius: 30px;padding: 0 15rpx; }.a-search image {height: 28rpx;width: 34rpx; } .searchNum {color: #858687; } </style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/53364.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

西安电子科技大学2021级计算机科学与技术专业重要成绩排名参考

从个人经历分享,所在计算机科学与技术专业的大学期间的重要成绩排名参考【非官方】,仅供选择方向参考!Hello World本文来自博客园,作者:LZHMS,转载请注明原文链接:https://www.cnblogs.com/LZHMS/p/18382071

OKR 如何激励团队有目标地工作

最近,关于90后和Z一代对为目标驱动的公司工作的渴望已经写了很多。这很可能成为围绕工作场所将如何演变的决定性叙述之一,尤其是在人才争夺战持续的情况下。 “带着目的工作 “需要的不仅仅是一个鼓舞人心的公司使命,它是每个领导者必须积极培养团队的东西。在这里,我将介绍…

查壳工具之Exeinfo PE

简介 Exeinfo PE是一款免费、专业的程序查壳软件,可以查看exe、dll程序的编译信息,开发语言,是否加壳,壳的种类以及入口地址等信息。 Exeinfo PE下载地址:https://github.com/ExeinfoASL/ASL GitHub地址打开后,直接选择Code--Download ZIP下载压缩包即可。 Exeinfo PE使用…

One-for-All:上交大提出视觉推理的符号化与逻辑推理分离的新范式 | ECCV 2024

通过对多样化基准的严格评估,论文展示了现有特定方法在实现跨领域推理以及其偏向于数据偏差拟合方面的缺陷。从两阶段的视角重新审视视觉推理:(1)符号化和(2)基于符号或其表示的逻辑推理,发现推理阶段比符号化更擅长泛化。因此,更高效的做法是通过为不同数据领域使用分…

投标流程

电脑不能安装WPS 国家电网新一代电子商务平台 (sgcc.com.cn) https://sign.utcsoft.com/utcbpc/service/download.html?q=ecp20 下载安装“供应商投标工具”

在Windows上搭建自己的Git服务器的图文教程

一、简介 以前,在别家的公司,一般早就把源代码管理工具搭建好了,很少有机会自己搭建一套。最近,公司也许要把现在不少的源码进行管理,于是我打算自己搭建源代码管理服务器。说起源代码管理,当然有很多中解决方案,我个人偏向搭建一个 Git 服务器。毕竟这个自己用的比较多…

Datawhale AI+X 深度学习入门(一)

一.机器学习的定义和核心 1.机器学习就是让机器具备找一个函数的能力。机器具备找函数的能力以后,它可以做很多事。 2.在机器学习领域里面,除了回归跟分类以外,还有结构化学习(structured learning)。机器不只是要做选择题或输出一个数字,而是产生一个有结构的物体,比如…

浅谈二分算法

浅谈二分算法 二分 首先知道一下二分是什么。 二分,是一种快速处理大型数据的方法。基本逻辑是折半查找。 设有一个共有 \(n\) 个数字的数组,要从中查询某个元素,就可以用二分查找。 注:这里的数组默认其成员数值具有单调性。这个点十分重要。 还记得小时候(我现在才新初一…