Skip to content

Commit

Permalink
增加 image Viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhongjia committed Dec 16, 2023
1 parent 01b9979 commit 7af9767
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
Binary file modified bun.lockb
Binary file not shown.
55 changes: 55 additions & 0 deletions learn/.vitepress/theme/ImgViewer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Viewer from 'viewerjs';
import { nextTick, onMounted, watch } from 'vue';
import { Route } from 'vitepress';

/**
* 给图片添加预览功能
*/
const setViewer = (el: string = '.vp-doc img', option?: Viewer.Options) => {
// 默认配置
const defaultBaseOption: Viewer.Options = {
navbar: false,
title: false,
toolbar: {
zoomIn: 4,
zoomOut: 4,
prev: 4,
next: 4,
reset: 4,
oneToOne: 4
}
}
document.querySelectorAll(el).forEach((item: Element) => {
(item as HTMLElement).onclick = () => {
const viewer = new Viewer(<HTMLElement>item, {
...defaultBaseOption,
...option,
hide(e) {
viewer.destroy();
}
});
viewer.show()
}
(item as HTMLElement).style.cursor = "zoom-in";
});
};

/**
* set imageViewer
* 设置图片查看器
* @param route 路由
* @param el The string corresponding to the CSS selector, the default is ".vp-doc img".
* <br/>CSS选择器对应的字符串,默认为 ".vp-doc img"
* @param option viewerjs options
* <br/>viewerjs 设置选项
*/
const imageViewer = (route: Route, el?: string, option?: Viewer.Options) => {
onMounted(() => {
setViewer(el, option);
})
watch(() => route.path, () => nextTick(() => {
setViewer(el, option);
}));
}

export default imageViewer;
9 changes: 9 additions & 0 deletions learn/.vitepress/theme/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
// .vitepress/theme/index.js
import DefaultTheme from 'vitepress/theme';

import giscusTalk from 'vitepress-plugin-comment-with-giscus';

import codeblocksFold from 'vitepress-plugin-codeblocks-fold';
import 'vitepress-plugin-codeblocks-fold/style/index.scss';

import 'viewerjs/dist/viewer.min.css';
import imageViewer from './ImgViewer';

import { useData, useRoute } from 'vitepress';

export default {
Expand All @@ -18,6 +24,9 @@ export default {
// code fold support
codeblocksFold({ route, frontmatter }, true, 400);

// imageView
imageViewer(route);

// Obtain configuration from: https://giscus.app/
giscusTalk({
repo: 'learnzig/learnzig',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"dependencies": {
"sass": "^1.69.5",
"viewerjs": "^1.11.6",
"vitepress-plugin-codeblocks-fold": "^1.2.28",
"vitepress-plugin-comment-with-giscus": "^1.1.11"
}
Expand Down

0 comments on commit 7af9767

Please sign in to comment.