├── .gitignore
├── README.md
├── babel.config.js
├── changeLog.md
├── package-lock.json
├── package.json
├── public
├── favicon.ico
└── index.html
├── src
├── App.vue
├── assets
│ └── logo.png
├── components
│ └── SearchHighlight.vue
├── index.js
└── main.js
└── vue.config.js
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | node_modules
3 | /dist
4 |
5 | # local env files
6 | .env.local
7 | .env.*.local
8 |
9 | # Log files
10 | npm-debug.log*
11 | yarn-debug.log*
12 | yarn-error.log*
13 |
14 | # Editor directories and files
15 | .idea
16 | .vscode
17 | *.suo
18 | *.ntvs*
19 | *.njsproj
20 | *.sln
21 | *.sw?
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # vue-search-highlight
2 | Vue文本关键字高亮组件,v1.1.0开始支持HTML内容跨标签匹配关键字。
3 |
4 | ## 安装
5 | ```bash
6 | # 使用yarn
7 | yarn add vue-search-highlight
8 |
9 | # 使用npm
10 | npm install vue-search-highlight
11 | ```
12 |
13 | ## 引入组件
14 | 像使用普通组件一样使用vue-search-highlight。
15 | ```js
16 | import SearchHighlight from 'vue-search-highlight'
17 | // 注册为子组件
18 | components: {
19 | 'search-highlight': SearchHighlight
20 | },
21 | ```
22 |
23 | ## 组件props
24 | | 名称 | 说明 |
25 | | --- | --- |
26 | | content | 需要展示的文本,搜索即在这个文本中进行。 |
27 | | keyword | 搜索关键字 |
28 | | highlightStyle | 所有关键词高亮的样式, 默认'background: #ffff00' |
29 | | currentStyle | 当前搜索到的关键词样式, 默认default: 'background: #ff9632' |
30 | | regExp | 启用正则匹配,会用关键词构建一个正则表达式来匹配。启用时请确保keyword能正确构造正则表达式,并且不会匹配空串,否则组件将不会匹配任何内容 |
31 |
32 | ## 组件事件
33 | | 事件名 | 说明 |
34 | | --- | --- |
35 | | @current-change | 返回值:当前关键词索引(从1开始)。关键词改变的时候,如果搜索到内容,会返回1,搜索不到则返回0。 |
36 | | @match-count-change | 返回值:文本匹配关键词总数。 |
37 |
38 | ## 组件方法,可以通过ref调用
39 | 主要提供了关键词滚动到可视区域的方法:
40 | | 方法名 | 参数 | 说明 |
41 | | --- | --- | --- |
42 | | searchNext | 无 | 下一个关键词滚动到可视区域 |
43 | | searchLast | 无 | 上一个关键词滚动到可视区域 |
44 | | scrollTo | index | 滚动到第index(从1开始)个关键词 |
45 |
46 | ## 使用示例:
47 | 效果在线预览:https://wintc.top/laboratory/#/search-highlight
48 |
49 | 代码示例
50 | ```html
51 |
58 |
59 |
60 |
118 | ```
--------------------------------------------------------------------------------
/babel.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | presets: [
3 | '@vue/app'
4 | ]
5 | }
6 |
--------------------------------------------------------------------------------
/changeLog.md:
--------------------------------------------------------------------------------
1 | v 1.1.3
2 | - 修改了匹配函数,调用RegExp.prototype.exec匹配关键字而不是String.prototype.matchAll,兼容性更好。
3 | - 支持关键词构造正则表达式匹配。
4 |
5 | v 1.1.0
6 | - 支持内容HTML字符串,支持关键词跨标签匹配
7 | - 更正了match-count-change事件的拼写错误
8 |
9 | v 1.0.0
10 | - 支持文本,关键词高亮
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "vue-search-highlight",
3 | "version": "1.1.7",
4 | "keywords": [
5 | "搜索高亮",
6 | "vue",
7 | "关键字高亮"
8 | ],
9 | "author": "Lushenggang ",
10 | "repository": {
11 | "type": "git",
12 | "url": "https://github.com/Lushenggang/vue-search-highlight"
13 | },
14 | "license": "MIT",
15 | "main": "dist/vue-search-highlight.common.js",
16 | "unpkg": "dist/vue-search-highlight.min.js",
17 | "module": "dist/vue-search-highlight.esm.js",
18 | "private": false,
19 | "files": [
20 | "dist"
21 | ],
22 | "scripts": {
23 | "serve": "vue-cli-service serve",
24 | "build": "vue-cli-service build",
25 | "lint": "vue-cli-service lint",
26 | "build-bundle": "vue-cli-service build --target lib --name vue-search-highlight ./src/index.js",
27 | "prepublishOnly": "git push && npm run build-bundle"
28 | },
29 | "dependencies": {
30 | "core-js": "^2.6.5",
31 | "vue": "^2.6.10"
32 | },
33 | "devDependencies": {
34 | "@vue/cli-plugin-babel": "^3.0.1",
35 | "@vue/cli-plugin-eslint": "^3.0.1",
36 | "@vue/cli-service": "^3.0.1",
37 | "babel-eslint": "^10.0.1",
38 | "eslint": "^5.16.0",
39 | "eslint-plugin-vue": "^5.0.0",
40 | "uglifyjs-webpack-plugin": "^2.2.0",
41 | "vue-template-compiler": "^2.6.10"
42 | },
43 | "eslintConfig": {
44 | "root": true,
45 | "env": {
46 | "node": true
47 | },
48 | "extends": [
49 | "plugin:vue/essential",
50 | "eslint:recommended"
51 | ],
52 | "rules": {},
53 | "parserOptions": {
54 | "parser": "babel-eslint"
55 | }
56 | },
57 | "postcss": {
58 | "plugins": {
59 | "autoprefixer": {}
60 | }
61 | },
62 | "browserslist": [
63 | "> 1%",
64 | "last 2 versions"
65 | ]
66 | }
67 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wintc23/vue-search-highlight/49df28a8bcf8dc92da1005e855506fd4ac671bc8/public/favicon.ico
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | vue-search-highlight
9 |
10 |
11 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/src/App.vue:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
18 |
19 |
20 |
21 |
22 |
23 |
83 |
84 |
98 |
99 |
133 |
--------------------------------------------------------------------------------
/src/assets/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wintc23/vue-search-highlight/49df28a8bcf8dc92da1005e855506fd4ac671bc8/src/assets/logo.png
--------------------------------------------------------------------------------
/src/components/SearchHighlight.vue:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
214 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import SearchHighlight from '@/components/SearchHighlight'
2 |
3 | export default SearchHighlight
4 |
--------------------------------------------------------------------------------
/src/main.js:
--------------------------------------------------------------------------------
1 | import Vue from 'vue'
2 | import App from './App.vue'
3 |
4 | Vue.config.productionTip = false
5 |
6 | new Vue({
7 | render: h => h(App),
8 | }).$mount('#app')
9 |
--------------------------------------------------------------------------------
/vue.config.js:
--------------------------------------------------------------------------------
1 |
2 | const UglifyJsPlugin=require('uglifyjs-webpack-plugin')
3 | const isDev = process.env.NODE_ENV !== "production"
4 |
5 | const externals = {}
6 | !isDev && (externals.vue = 'vue')
7 |
8 | module.exports = {
9 | productionSourceMap: false,
10 | configureWebpack: {
11 | externals,
12 | optimization: {
13 | minimizer: [
14 | new UglifyJsPlugin({
15 | uglifyOptions: {
16 | output: {
17 | comments: false,
18 | },
19 | warnings: false,
20 | compress: {
21 | drop_debugger: true,
22 | drop_console: true
23 | }
24 | }
25 | })
26 | ]
27 | }
28 | }
29 | }
--------------------------------------------------------------------------------