首页 > 其他分享 >vue3uniapps使用富文本mp-html插件

vue3uniapps使用富文本mp-html插件

时间:2024-08-27 10:03:31浏览次数:4  
标签:插件 searchIndex const res value height html vue3uniapps uni

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>
            <input
              class="weui-search-bar__input"
              placeholder-style="color:#b9babd;"
              placeholder="请输入需要搜索的关键词"
              v-model="keyWord"
              confirm-type="search"
              @confirm="searchSelf(keyWord)"
            />

            <view
              class="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-html
        ref="mp"
        use-anchor
        search
        container-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 { onl oad, 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.id
  keyWord.value = options.searchInput || '' // 关键词高亮
  getData()
})

// 获取文本数据 const getData = async () => { uni.showLoading() getDocDetail({ id: id.value }) .then((res) => { if (res.code === 200) { data.content = res.data.content uni.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 = res searchIndex.value = 1 if (res.num === 0) { uni.showToast({ title: '没有了', icon: 'none', duration: 2000, }) } else { show.value = false res.highlight(1) res.jump(1, -343) // 高亮第 1 个结果并跳转到该位置,偏移量 show.value = true } }) } // 下一个关键词 const nextSearch = () => { show.value = false if (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 = false if (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>

标签:插件,searchIndex,const,res,value,height,html,vue3uniapps,uni
From: https://www.cnblogs.com/shyhuahua/p/18382111

相关文章

  • auto_size_text 自动调整文本大小以适应其容器的 Flutter 插件
    依赖auto_size_text:^3.0.0//自动调整文本大小例子classAutoSizeRichTextExampleextendsStatelessWidget{@overrideWidgetbuild(BuildContextcontext){returnCenter(child:Container(width:double.infinity,child:AutoSi......
  • java一键生成数据库说明文档html格式
    要验收项目了,要写数据库文档,一大堆表太费劲了,直接生成一个吧,本来想用个别人的轮子,网上看了几个,感觉效果不怎么好,自己动手写一个吧。抽空再把字典表补充进去就OK了先看效果:目录快速导航生成效果关键代码try{ StringprefixTables="sys_monitor_db_ha......
  • 一文掌握:HTML标签使用全攻略,打造完美网页布局!
    HTML(超文本标记语言)是构建网页的基石。通过不同的标签,HTML定义了网页的结构和内容。了解各种HTML标签的用法是前端开发的基础。本文将回顾HTML中常见标签的用法,并介绍它们在实际开发中的应用。一、结构性标签<!DOCTYPEhtml>这个标签声明文档类型,并指定HTML5标准。它应......
  • 前端模块自动导入的插件
    前言开发中通常会有很多导入语句,如何确保一些通用的api和hook无需每次手动导入即可使用。<scriptsetuplang="ts">import{ref,reactive}from"vue"import{useRoute,useRouter}from"vue-router"import{login}from"./api/user"constcount=r......
  • 利用kafka和kafka connect插件debezium实现oracle表同步
    1.kafka安装1.1.java安装openjdk下载,建议使用17,至少应该高于版本11#进入家目录,解压下载的java包,配置环境变量tarvxfopenjdk-20.0.1_linux-x64_bin.tar.gz-C/usr/local/vi.bash_profile#注意要把JAVA的目录放到$PATH之前exportJAVA_HOME=/usr/local/jdk-20exportP......
  • UE5蓝图 离线实时语音转文字插件 教程 c/c++插件 毫秒级响应 比http更节约资源
    UE5蓝图实现离线实时语音转文字插件教程如何用UE5蓝图实现离线实时语音转文字,实时接收麦克风音频并且快速的转换成文字。那么我来分享一下ez2txt这个插件。bilibili使用教程效果展示:蓝图:只要启动麦克风就可以了,其他的繁琐步骤插件都封装好了。参数说明Rule1_m......
  • HTML+CSS+JS 实现淘宝首页官网
    效果展示:完整代码:HTML部分:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport&q......
  • HTML+CSS 公司官网首页 浮动布局!!
    网页展示: HTML部分:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"conte......
  • WordPress插件存在严重缺陷,允许黑客获取管理员访问权限
    近日,网络安全研究人员披露了WordPress的LiteSpeedCache插件中的一个严重安全漏洞,该漏洞可能允许未经身份验证的用户获得管理员权限。国际知名网络黑客安全专家、东方联盟创始人郭盛华在周一的一份报告中表示:“该插件存在未经身份验证的权限提升漏洞,任何未经身份验证的访问者都......
  • notification ant插件 封装notification 防止多个相同的错误提示同时展示 message也
    import{notification}from'ant-design-vue'typeNoticeType='info'|'success'|'error'|'warning'//保证notification提示不重复constmessageSet=newSet();letclearTimer:number|undefined;interf......