@kawapress/plugin-image-zoom 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +155 -2
- package/README.zh-CN.md +156 -0
- package/package.json +44 -3
- package/src/image-zoom.css +74 -0
- package/src/index.ts +46 -0
- package/src/labels.ts +73 -0
- package/src/locale.ts +18 -0
- package/src/markdown.ts +95 -0
- package/src/options-module.ts +41 -0
- package/src/runtime-plugin.ts +35 -0
- package/src/virtual-modules.d.ts +5 -0
- package/src/zoom-runtime.ts +163 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) KawaPress contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,156 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/ShenQingchuan/kawapress/main/docs/public/kawapress-logo-dark.png">
|
|
4
|
+
<img src="https://raw.githubusercontent.com/ShenQingchuan/kawapress/main/docs/public/kawapress-logo.png" alt="KawaPress" width="180">
|
|
5
|
+
</picture>
|
|
6
|
+
</p>
|
|
2
7
|
|
|
3
|
-
|
|
8
|
+
<h1 align="center">KawaPress</h1>
|
|
9
|
+
|
|
10
|
+
<p align="center">
|
|
11
|
+
<strong>A Vue-first static site generator for documentation and content-focused websites.</strong>
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
<p align="center">
|
|
15
|
+
Write in Markdown. Develop with real SSR. Extend everything through public APIs.
|
|
16
|
+
</p>
|
|
17
|
+
|
|
18
|
+
<p align="center">
|
|
19
|
+
<a href="https://www.npmjs.com/package/kawapress"><img src="https://img.shields.io/npm/v/kawapress?label=npm&color=42b883" alt="npm version"></a>
|
|
20
|
+
<a href="https://github.com/ShenQingchuan/kawapress/actions/workflows/deploy-pages.yml"><img src="https://github.com/ShenQingchuan/kawapress/actions/workflows/deploy-pages.yml/badge.svg" alt="documentation build"></a>
|
|
21
|
+
<a href="https://github.com/ShenQingchuan/kawapress/blob/main/LICENSE"><img src="https://img.shields.io/github/license/ShenQingchuan/kawapress" alt="MIT license"></a>
|
|
22
|
+
<img src="https://img.shields.io/badge/Node.js-%E2%89%A522.12-339933?logo=nodedotjs&logoColor=white" alt="Node.js 22.12 or newer">
|
|
23
|
+
</p>
|
|
24
|
+
|
|
25
|
+
<p align="center">
|
|
26
|
+
<a href="https://shenqingchuan.github.io/kawapress/en/">Documentation</a>
|
|
27
|
+
·
|
|
28
|
+
<a href="https://github.com/ShenQingchuan/kawapress/blob/main/README.zh-CN.md">简体中文</a>
|
|
29
|
+
</p>
|
|
30
|
+
|
|
31
|
+
If you run into any problems while using KawaPress, please [open an issue](https://github.com/ShenQingchuan/kawapress/issues). Your feedback helps shape the project during its early development.
|
|
32
|
+
|
|
33
|
+
## Why KawaPress?
|
|
34
|
+
|
|
35
|
+
KawaPress turns Markdown into pre-rendered Vue websites. It keeps the familiar simplicity of a documentation generator while giving themes and plugins the same public extension points used by the official experience.
|
|
36
|
+
|
|
37
|
+
- **Real SSR during development** — every initial request is server-rendered, so SSR-only problems appear before build or deployment.
|
|
38
|
+
- **A complete default experience** — the built-in nagi (凪) preset includes responsive documentation layouts, dark mode, local search, syntax highlighting, Twoslash, code groups, multilingual navigation, and more.
|
|
39
|
+
- **Vue from end to end** — Markdown becomes Vue components, Vue Router handles navigation, and regular Vue SFCs fit naturally into content.
|
|
40
|
+
- **AI-ready output** — nagi generates page-level Markdown, locale-aware `llms.txt` and `llms-full.txt`, plus built-in copy actions.
|
|
41
|
+
- **One extension model** — Generator Plugins, Runtime Plugins, and Presets cover build-time behavior, the Vue runtime, themes, and reusable product setups without private theme hooks.
|
|
42
|
+
|
|
43
|
+
KawaPress is an independent project. It is not a VitePress fork or a drop-in replacement, and it intentionally does not use VitePress configuration.
|
|
44
|
+
|
|
45
|
+
## Quick Start
|
|
46
|
+
|
|
47
|
+
You need [Node.js](https://nodejs.org/) 22.12 or newer.
|
|
48
|
+
|
|
49
|
+
Create a new site with the scaffolder:
|
|
50
|
+
|
|
51
|
+
```sh
|
|
52
|
+
npm create kawapress@latest my-kawapress-site
|
|
53
|
+
cd my-kawapress-site
|
|
54
|
+
npm run dev
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`pnpm create kawapress my-kawapress-site` and `yarn create kawapress my-kawapress-site` do the same thing. The scaffolder asks for a site title and a default language, and can install dependencies for you.
|
|
58
|
+
|
|
59
|
+
If you would rather see every file from the start, set the site up by hand:
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
mkdir my-kawapress-site
|
|
63
|
+
cd my-kawapress-site
|
|
64
|
+
pnpm init
|
|
65
|
+
pnpm add --save-dev kawapress
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Create `kawapress.config.ts`:
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { nagi } from 'kawapress/nagi'
|
|
72
|
+
|
|
73
|
+
export default nagi({
|
|
74
|
+
title: 'My Docs',
|
|
75
|
+
})
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Create `index.md`:
|
|
79
|
+
|
|
80
|
+
```md
|
|
81
|
+
---
|
|
82
|
+
layout: home
|
|
83
|
+
title: My Docs
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
# My Docs
|
|
87
|
+
|
|
88
|
+
Welcome to my KawaPress site.
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Start the development server:
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
pnpm exec kawapress dev
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Build static files into `dist`:
|
|
98
|
+
|
|
99
|
+
```sh
|
|
100
|
+
pnpm exec kawapress build
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
The deployed output is static and does not require a Node.js server. npm and Yarn commands are available in the [Getting Started guide](https://shenqingchuan.github.io/kawapress/en/guide/getting-started).
|
|
104
|
+
|
|
105
|
+
> If your site directly imports Vue APIs or Vue components, also install `vue` as a development dependency.
|
|
106
|
+
|
|
107
|
+
## What You Get
|
|
108
|
+
|
|
109
|
+
| Area | Included capability |
|
|
110
|
+
| --- | --- |
|
|
111
|
+
| Authoring | Markdown, frontmatter, Vue in Markdown, file-based routing, data loaders |
|
|
112
|
+
| Rendering | Shared development/build SSR semantics, hydration, Vue Router navigation |
|
|
113
|
+
| Documentation UI | nagi layouts, responsive sidebar, page outline, dark mode, i18n |
|
|
114
|
+
| Content | Shiki, Twoslash, code groups, code copy, containers, GitHub alerts |
|
|
115
|
+
| Discovery | Lazy local search and locale-aware navigation |
|
|
116
|
+
| AI consumption | Page `.md`, `llms.txt`, `llms-full.txt`, copy actions |
|
|
117
|
+
| Styling | UnoCSS integration with sensible nagi defaults |
|
|
118
|
+
|
|
119
|
+
The `kawapress` package includes nagi and its official plugins. A basic site only needs this one package; advanced users can compose the individual `@kawapress/*` packages directly.
|
|
120
|
+
|
|
121
|
+
## Extension Model
|
|
122
|
+
|
|
123
|
+
KawaPress keeps extension boundaries explicit:
|
|
124
|
+
|
|
125
|
+
- a **Generator Plugin** extends configuration, Markdown, page data, Vite, and build artifacts;
|
|
126
|
+
- a **Runtime Plugin** extends the real Vue App and Vue Router in both SSR and the browser;
|
|
127
|
+
- a **Preset** combines configuration and plugins into a ready-to-use experience such as nagi.
|
|
128
|
+
|
|
129
|
+
A logical plugin is installed once and can provide both generator and runtime behavior. Learn more in the [Plugin System guide](https://shenqingchuan.github.io/kawapress/en/guide/plugin-system).
|
|
130
|
+
|
|
131
|
+
## Documentation
|
|
132
|
+
|
|
133
|
+
- [Getting Started](https://shenqingchuan.github.io/kawapress/en/guide/getting-started)
|
|
134
|
+
- [What is KawaPress?](https://shenqingchuan.github.io/kawapress/en/guide/what-is-kawapress)
|
|
135
|
+
- [Markdown Extensions](https://shenqingchuan.github.io/kawapress/en/guide/markdown-extensions)
|
|
136
|
+
- [Internationalization](https://shenqingchuan.github.io/kawapress/en/guide/internationalization)
|
|
137
|
+
- [Plugin System](https://shenqingchuan.github.io/kawapress/en/guide/plugin-system)
|
|
138
|
+
- [Theme Customization](https://shenqingchuan.github.io/kawapress/en/guide/theme-customization)
|
|
139
|
+
|
|
140
|
+
## Contributing
|
|
141
|
+
|
|
142
|
+
Issues and pull requests are welcome. For a large feature, please open an issue first so the direction can be agreed on before implementation.
|
|
143
|
+
|
|
144
|
+
```sh
|
|
145
|
+
pnpm install
|
|
146
|
+
pnpm lint
|
|
147
|
+
pnpm typecheck
|
|
148
|
+
pnpm test
|
|
149
|
+
pnpm build
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
KawaPress uses Node.js 22.12 or newer and pnpm 11.22.0 for repository development.
|
|
153
|
+
|
|
154
|
+
## License
|
|
155
|
+
|
|
156
|
+
[MIT](https://github.com/ShenQingchuan/kawapress/blob/main/LICENSE) © KawaPress contributors
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/ShenQingchuan/kawapress/main/docs/public/kawapress-logo-dark.png">
|
|
4
|
+
<img src="https://raw.githubusercontent.com/ShenQingchuan/kawapress/main/docs/public/kawapress-logo.png" alt="KawaPress" width="180">
|
|
5
|
+
</picture>
|
|
6
|
+
</p>
|
|
7
|
+
|
|
8
|
+
<h1 align="center">KawaPress</h1>
|
|
9
|
+
|
|
10
|
+
<p align="center">
|
|
11
|
+
<strong>以 Vue 为核心,面向文档与内容型网站的静态站点生成器。</strong>
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
<p align="center">
|
|
15
|
+
用 Markdown 安心写作,在真实 SSR 中开发,通过公开能力自由扩展。
|
|
16
|
+
</p>
|
|
17
|
+
|
|
18
|
+
<p align="center">
|
|
19
|
+
<a href="https://www.npmjs.com/package/kawapress"><img src="https://img.shields.io/npm/v/kawapress?label=npm&color=42b883" alt="npm 版本"></a>
|
|
20
|
+
<a href="https://github.com/ShenQingchuan/kawapress/actions/workflows/deploy-pages.yml"><img src="https://github.com/ShenQingchuan/kawapress/actions/workflows/deploy-pages.yml/badge.svg" alt="文档构建状态"></a>
|
|
21
|
+
<a href="https://github.com/ShenQingchuan/kawapress/blob/main/LICENSE"><img src="https://img.shields.io/github/license/ShenQingchuan/kawapress" alt="MIT 许可"></a>
|
|
22
|
+
<img src="https://img.shields.io/badge/Node.js-%E2%89%A522.12-339933?logo=nodedotjs&logoColor=white" alt="Node.js 22.12 或更高版本">
|
|
23
|
+
</p>
|
|
24
|
+
|
|
25
|
+
<p align="center">
|
|
26
|
+
<a href="https://shenqingchuan.github.io/kawapress/">中文文档</a>
|
|
27
|
+
·
|
|
28
|
+
<a href="https://github.com/ShenQingchuan/kawapress/blob/main/README.md">English</a>
|
|
29
|
+
</p>
|
|
30
|
+
|
|
31
|
+
使用 KawaPress 时遇到任何问题,欢迎到 [提交 Issue](https://github.com/ShenQingchuan/kawapress/issues) 反馈,一起参与本项目的早期建设。
|
|
32
|
+
|
|
33
|
+
## 为什么选择 KawaPress?
|
|
34
|
+
|
|
35
|
+
KawaPress 会把 Markdown 编译成预渲染的 Vue 网站。它保留了文档生成器简单、轻快的写作方式,同时让官方主题和第三方扩展使用完全相同的公开能力。
|
|
36
|
+
|
|
37
|
+
- **开发时也执行真实 SSR**:每次首次请求都会在服务端渲染。只在 SSR 中出现的问题,不必等到构建或上线后才发现。
|
|
38
|
+
- **默认体验完整**:内置 nagi(凪)预设,包含响应式文档布局、深浅色模式、本地搜索、代码高亮、Twoslash、代码组和多语言导航等常用能力。
|
|
39
|
+
- **从头到尾使用 Vue**:Markdown 会变成 Vue 组件,页面跳转交给 Vue Router;需要交互时,也可以自然地使用普通 Vue SFC。
|
|
40
|
+
- **面向 AI 的文档产物**:nagi 默认生成每页 `.md`、各语言的 `llms.txt` 与 `llms-full.txt`,并提供复制正文和链接的操作。
|
|
41
|
+
- **统一的扩展模型**:Generator Plugin、Runtime Plugin 与 Preset 分别覆盖生成阶段、Vue 运行时和能力组合,不依赖主题私有入口。
|
|
42
|
+
|
|
43
|
+
KawaPress 是一个独立项目,不是 VitePress 的分支或配置兼容层,也不能直接使用 VitePress 配置。
|
|
44
|
+
|
|
45
|
+
## 快速开始
|
|
46
|
+
|
|
47
|
+
请先准备 [Node.js](https://nodejs.org/) 22.12 或更高版本。
|
|
48
|
+
|
|
49
|
+
用脚手架创建新站点:
|
|
50
|
+
|
|
51
|
+
```sh
|
|
52
|
+
npm create kawapress@latest my-kawapress-site
|
|
53
|
+
cd my-kawapress-site
|
|
54
|
+
npm run dev
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`pnpm create kawapress my-kawapress-site` 和 `yarn create kawapress my-kawapress-site` 的作用完全一样。脚手架会询问站点标题和默认语言,也可以顺便帮你安装依赖。
|
|
58
|
+
|
|
59
|
+
如果你更想从零看清每个文件,可以手动创建:
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
mkdir my-kawapress-site
|
|
63
|
+
cd my-kawapress-site
|
|
64
|
+
pnpm init
|
|
65
|
+
pnpm add --save-dev kawapress
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
创建 `kawapress.config.ts`:
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { nagi } from 'kawapress/nagi'
|
|
72
|
+
|
|
73
|
+
export default nagi({
|
|
74
|
+
title: 'My Docs',
|
|
75
|
+
})
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
创建 `index.md`:
|
|
79
|
+
|
|
80
|
+
```md
|
|
81
|
+
---
|
|
82
|
+
layout: home
|
|
83
|
+
title: My Docs
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
# My Docs
|
|
87
|
+
|
|
88
|
+
欢迎来到我的 KawaPress 站点。
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
启动开发服务器:
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
pnpm exec kawapress dev
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
构建静态文件到 `dist`:
|
|
98
|
+
|
|
99
|
+
```sh
|
|
100
|
+
pnpm exec kawapress build
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
部署产物是普通静态文件,不需要在服务器上继续运行 Node.js。npm 和 Yarn 的完整命令可以查看[快速开始](https://shenqingchuan.github.io/kawapress/guide/getting-started)。
|
|
104
|
+
|
|
105
|
+
> 如果站点会直接导入 Vue API 或 Vue 组件,还需要把 `vue` 安装为开发依赖。
|
|
106
|
+
|
|
107
|
+
## 默认包含什么?
|
|
108
|
+
|
|
109
|
+
| 范围 | 内置能力 |
|
|
110
|
+
| --- | --- |
|
|
111
|
+
| 内容编写 | Markdown、Frontmatter、在 Markdown 中使用 Vue、文件路由、Data Loader |
|
|
112
|
+
| 渲染 | 开发与构建共享 SSR 语义、Hydration、Vue Router 站内导航 |
|
|
113
|
+
| 文档界面 | nagi 布局、响应式 Sidebar、本页目录、深浅色模式、多语言 |
|
|
114
|
+
| 内容增强 | Shiki、Twoslash、代码组、代码复制、提示容器、GitHub Alerts |
|
|
115
|
+
| 内容发现 | 按需加载的本地搜索与多语言导航 |
|
|
116
|
+
| AI 读取 | 页面 `.md`、`llms.txt`、`llms-full.txt`、页面复制操作 |
|
|
117
|
+
| 样式 | UnoCSS 集成与 nagi 默认配置 |
|
|
118
|
+
|
|
119
|
+
`kawapress` 已经包含 nagi 和它使用的官方插件。普通文档站只需要安装这一个包;需要自行组合能力时,也可以直接使用独立的 `@kawapress/*` 包。
|
|
120
|
+
|
|
121
|
+
## 扩展模型
|
|
122
|
+
|
|
123
|
+
KawaPress 把扩展边界分得很清楚:
|
|
124
|
+
|
|
125
|
+
- **Generator Plugin** 负责配置、Markdown、页面数据、Vite 与构建产物;
|
|
126
|
+
- **Runtime Plugin** 在 SSR 和浏览器中扩展真实的 Vue App 与 Vue Router;
|
|
127
|
+
- **Preset** 把配置和一组插件组合成可以直接使用的产品体验,例如 nagi。
|
|
128
|
+
|
|
129
|
+
一个逻辑插件只需要安装一次,同时可以提供生成侧和运行侧能力。更多内容请查看[插件体系](https://shenqingchuan.github.io/kawapress/guide/plugin-system)。
|
|
130
|
+
|
|
131
|
+
## 文档
|
|
132
|
+
|
|
133
|
+
- [快速开始](https://shenqingchuan.github.io/kawapress/guide/getting-started)
|
|
134
|
+
- [KawaPress 是什么?](https://shenqingchuan.github.io/kawapress/guide/what-is-kawapress)
|
|
135
|
+
- [Markdown 语法扩展](https://shenqingchuan.github.io/kawapress/guide/markdown-extensions)
|
|
136
|
+
- [国际化](https://shenqingchuan.github.io/kawapress/guide/internationalization)
|
|
137
|
+
- [插件体系](https://shenqingchuan.github.io/kawapress/guide/plugin-system)
|
|
138
|
+
- [主题定制](https://shenqingchuan.github.io/kawapress/guide/theme-customization)
|
|
139
|
+
|
|
140
|
+
## 参与贡献
|
|
141
|
+
|
|
142
|
+
欢迎提交 Issue 和 Pull Request。如果准备增加较大的功能,请先创建 Issue,把目标和方案聊清楚后再开始实现。
|
|
143
|
+
|
|
144
|
+
```sh
|
|
145
|
+
pnpm install
|
|
146
|
+
pnpm lint
|
|
147
|
+
pnpm typecheck
|
|
148
|
+
pnpm test
|
|
149
|
+
pnpm build
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
KawaPress 仓库使用 Node.js 22.12 或更高版本,以及 pnpm 11.22.0 进行开发。
|
|
153
|
+
|
|
154
|
+
## 开源许可
|
|
155
|
+
|
|
156
|
+
[MIT](https://github.com/ShenQingchuan/kawapress/blob/main/LICENSE) © KawaPress 贡献者
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kawapress/plugin-image-zoom",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"description": "Click-to-zoom images for KawaPress Markdown content",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://shenqingchuan.github.io/kawapress/",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/ShenQingchuan/kawapress.git",
|
|
11
|
+
"directory": "packages/plugin-image-zoom"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/ShenQingchuan/kawapress/issues"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./src/index.ts",
|
|
19
|
+
"import": "./src/index.ts",
|
|
20
|
+
"default": "./src/index.ts"
|
|
21
|
+
},
|
|
22
|
+
"./runtime-plugin": {
|
|
23
|
+
"types": "./src/runtime-plugin.ts",
|
|
24
|
+
"import": "./src/runtime-plugin.ts",
|
|
25
|
+
"default": "./src/runtime-plugin.ts"
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"src/**/!(*.test.*)"
|
|
31
|
+
],
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=22.12.0"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public",
|
|
37
|
+
"registry": "https://registry.npmjs.org/"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"kawapress": "0.0.1"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"happy-dom": "^20.11.6",
|
|
44
|
+
"markdown-exit": "^1.3.0",
|
|
45
|
+
"vite": "^8.2.2"
|
|
46
|
+
}
|
|
6
47
|
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
@layer kawa.plugin {
|
|
2
|
+
.kawa-image-zoom {
|
|
3
|
+
/* Size limits of the enlarged image, private to this plugin. */
|
|
4
|
+
--kawa-image-zoom-max-width: min(92vw, 1600px);
|
|
5
|
+
--kawa-image-zoom-max-height: 92vh;
|
|
6
|
+
|
|
7
|
+
max-width: none;
|
|
8
|
+
max-height: none;
|
|
9
|
+
padding: 0;
|
|
10
|
+
color: var(--kawa-c-text-1);
|
|
11
|
+
border: 0;
|
|
12
|
+
background: transparent;
|
|
13
|
+
cursor: zoom-out;
|
|
14
|
+
transition:
|
|
15
|
+
opacity 200ms cubic-bezier(0.22, 1, 0.36, 1),
|
|
16
|
+
transform 200ms cubic-bezier(0.22, 1, 0.36, 1);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.kawa-image-zoom::backdrop {
|
|
20
|
+
background: color-mix(in oklab, var(--kawa-c-bg) 80%, transparent);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
.kawa-image-zoom[open] {
|
|
24
|
+
opacity: 1;
|
|
25
|
+
transform: scale(1);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/* First frame of the opening animation, see `@starting-style` below. */
|
|
29
|
+
@starting-style {
|
|
30
|
+
.kawa-image-zoom[open] {
|
|
31
|
+
opacity: 0;
|
|
32
|
+
transform: scale(0.96);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
.kawa-image-zoom__figure {
|
|
37
|
+
display: flex;
|
|
38
|
+
flex-direction: column;
|
|
39
|
+
gap: 0.75rem;
|
|
40
|
+
align-items: center;
|
|
41
|
+
margin: 0;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
.kawa-image-zoom__image {
|
|
45
|
+
display: block;
|
|
46
|
+
max-width: var(--kawa-image-zoom-max-width);
|
|
47
|
+
max-height: var(--kawa-image-zoom-max-height);
|
|
48
|
+
object-fit: contain;
|
|
49
|
+
border-radius: var(--kawa-radius);
|
|
50
|
+
box-shadow: var(--kawa-shadow-overlay);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.kawa-image-zoom__caption {
|
|
54
|
+
max-width: var(--kawa-image-zoom-max-width);
|
|
55
|
+
color: var(--kawa-c-text-2);
|
|
56
|
+
font-size: 0.875rem;
|
|
57
|
+
text-align: center;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
img[data-kawa-zoom] {
|
|
61
|
+
cursor: zoom-in;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
img[data-kawa-zoom]:focus-visible {
|
|
65
|
+
outline: 2px solid var(--kawa-c-brand);
|
|
66
|
+
outline-offset: 2px;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@media (prefers-reduced-motion: reduce) {
|
|
70
|
+
.kawa-image-zoom {
|
|
71
|
+
transition: none;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { KawaPressPlugin } from 'kawapress'
|
|
2
|
+
import type { ImageZoomLabels, ImageZoomPluginOptions } from './labels'
|
|
3
|
+
import type { ImageZoomLocaleLanguages } from './locale'
|
|
4
|
+
import { definePlugin } from 'kawapress'
|
|
5
|
+
import { createImageZoomLabelResolver, formatImageZoomLabel } from './labels'
|
|
6
|
+
import { resolveLocaleLanguages } from './locale'
|
|
7
|
+
import { installImageZoom } from './markdown'
|
|
8
|
+
import { imageZoomOptionsPlugin } from './options-module'
|
|
9
|
+
|
|
10
|
+
export type { ImageZoomLabels, ImageZoomPluginOptions }
|
|
11
|
+
export { formatImageZoomLabel, installImageZoom }
|
|
12
|
+
|
|
13
|
+
const DEFAULT_LOCALE_LANGUAGES: ImageZoomLocaleLanguages = { root: 'en' }
|
|
14
|
+
|
|
15
|
+
export function imageZoomPlugin(
|
|
16
|
+
options: ImageZoomPluginOptions = {},
|
|
17
|
+
): KawaPressPlugin {
|
|
18
|
+
let localeLanguages = DEFAULT_LOCALE_LANGUAGES
|
|
19
|
+
|
|
20
|
+
return definePlugin({
|
|
21
|
+
name: '@kawapress/plugin-image-zoom',
|
|
22
|
+
setup(api) {
|
|
23
|
+
api.config((config) => {
|
|
24
|
+
localeLanguages = resolveLocaleLanguages(config.locales)
|
|
25
|
+
})
|
|
26
|
+
api.vite((config) => {
|
|
27
|
+
config.plugins ??= []
|
|
28
|
+
config.plugins.push(imageZoomOptionsPlugin({
|
|
29
|
+
labels: options.labels ?? {},
|
|
30
|
+
localeLabels: options.localeLabels ?? {},
|
|
31
|
+
localeLanguages,
|
|
32
|
+
}))
|
|
33
|
+
})
|
|
34
|
+
api.markdown((markdown) => {
|
|
35
|
+
installImageZoom(markdown, {
|
|
36
|
+
resolveLabels: createImageZoomLabelResolver({
|
|
37
|
+
...options,
|
|
38
|
+
localeLanguages,
|
|
39
|
+
}),
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
},
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default imageZoomPlugin
|
package/src/labels.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { ImageZoomLocaleLanguages } from './locale'
|
|
2
|
+
import { resolveLocaleIndex } from 'kawapress'
|
|
3
|
+
|
|
4
|
+
export interface ImageZoomLabels {
|
|
5
|
+
/** Accessible name of a zoomable image. `{alt}` is replaced by the image alt text. */
|
|
6
|
+
zoom: string
|
|
7
|
+
/** Accessible name of the zoom dialog. */
|
|
8
|
+
dialog: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ImageZoomPluginOptions {
|
|
12
|
+
/** Override default labels for every locale. */
|
|
13
|
+
labels?: Partial<ImageZoomLabels>
|
|
14
|
+
/** Override default labels for one KawaPress locale key. */
|
|
15
|
+
localeLabels?: Record<string, Partial<ImageZoomLabels>>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ImageZoomLabelSources extends ImageZoomPluginOptions {
|
|
19
|
+
localeLanguages?: ImageZoomLocaleLanguages
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const ENGLISH_IMAGE_ZOOM_LABELS: ImageZoomLabels = {
|
|
23
|
+
zoom: 'Zoom image: {alt}',
|
|
24
|
+
dialog: 'Image zoom',
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const CHINESE_IMAGE_ZOOM_LABELS: ImageZoomLabels = {
|
|
28
|
+
zoom: '放大图片:{alt}',
|
|
29
|
+
dialog: '图片缩放',
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const ALT_PLACEHOLDER_RE = /\{alt\}/g
|
|
33
|
+
const TRAILING_SEPARATOR_RE = /[\s::]+$/
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Builds the label lookup both sides need. The generator resolves labels per
|
|
37
|
+
* page path while rendering, the runtime resolves them per route while the
|
|
38
|
+
* visitor interacts, and both share this function.
|
|
39
|
+
*/
|
|
40
|
+
export function createImageZoomLabelResolver(
|
|
41
|
+
sources: ImageZoomLabelSources,
|
|
42
|
+
): (path: string) => ImageZoomLabels {
|
|
43
|
+
const localeLanguages = sources.localeLanguages ?? {}
|
|
44
|
+
|
|
45
|
+
return (path) => {
|
|
46
|
+
const localeIndex = resolveLocaleIndex(path, Object.keys(localeLanguages))
|
|
47
|
+
const language = localeLanguages[localeIndex]
|
|
48
|
+
const builtIn = language?.toLowerCase().startsWith('zh')
|
|
49
|
+
? CHINESE_IMAGE_ZOOM_LABELS
|
|
50
|
+
: ENGLISH_IMAGE_ZOOM_LABELS
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
...builtIn,
|
|
54
|
+
...sources.labels,
|
|
55
|
+
...sources.localeLabels?.[localeIndex],
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Fills `{alt}` in a label template. Without alt text the separator in front of
|
|
62
|
+
* the placeholder is dropped too, so the name reads "Zoom image" instead of
|
|
63
|
+
* "Zoom image: ".
|
|
64
|
+
*/
|
|
65
|
+
export function formatImageZoomLabel(template: string, alt: string): string {
|
|
66
|
+
const text = alt.trim()
|
|
67
|
+
if (text) {
|
|
68
|
+
return template.replace(ALT_PLACEHOLDER_RE, () => text)
|
|
69
|
+
}
|
|
70
|
+
return template
|
|
71
|
+
.replace(ALT_PLACEHOLDER_RE, '')
|
|
72
|
+
.replace(TRAILING_SEPARATOR_RE, '')
|
|
73
|
+
}
|
package/src/locale.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { LocaleConfig } from 'kawapress'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Language of every locale key, e.g. `{ root: 'zh-CN', en: 'en' }`. The
|
|
5
|
+
* generator side needs it to pick built-in labels; the runtime receives the
|
|
6
|
+
* same map as JSON so both sides always resolve a route to the same language.
|
|
7
|
+
*/
|
|
8
|
+
export type ImageZoomLocaleLanguages = Record<string, string | undefined>
|
|
9
|
+
|
|
10
|
+
export function resolveLocaleLanguages(
|
|
11
|
+
locales: Record<string, LocaleConfig> | undefined,
|
|
12
|
+
): ImageZoomLocaleLanguages {
|
|
13
|
+
return Object.fromEntries(
|
|
14
|
+
Object.entries(locales ?? {}).map(([localeIndex, locale]) => (
|
|
15
|
+
[localeIndex, locale.lang]
|
|
16
|
+
)),
|
|
17
|
+
)
|
|
18
|
+
}
|
package/src/markdown.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { MarkdownExit, RenderRule, Token } from 'markdown-exit'
|
|
2
|
+
import type { ImageZoomLabels } from './labels'
|
|
3
|
+
import { ENGLISH_IMAGE_ZOOM_LABELS, formatImageZoomLabel } from './labels'
|
|
4
|
+
|
|
5
|
+
const ZOOM_ATTRIBUTE = 'data-kawa-zoom'
|
|
6
|
+
/** Hash an author appends to an image URL to keep that one image unzoomed. */
|
|
7
|
+
const NO_ZOOM_HASH = 'no-zoom'
|
|
8
|
+
|
|
9
|
+
type RenderRuleArgs = Parameters<RenderRule>
|
|
10
|
+
|
|
11
|
+
export interface ImageZoomRenderEnv {
|
|
12
|
+
/** Route of the page being compiled. */
|
|
13
|
+
path?: string
|
|
14
|
+
/** Frontmatter of the page, read to skip pages that opt out. */
|
|
15
|
+
frontmatter?: Record<string, unknown>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface InstallImageZoomOptions {
|
|
19
|
+
/** Labels of the page being rendered, resolved once per image. */
|
|
20
|
+
resolveLabels?: (path: string) => ImageZoomLabels
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Marks every Markdown image so the runtime can open it, and gives it the
|
|
25
|
+
* button semantics a click-to-zoom image needs. The attributes are rendered
|
|
26
|
+
* here, not in the browser, so server HTML and the first client render match.
|
|
27
|
+
*
|
|
28
|
+
* Raw HTML `<img>` tags never reach this rule, so they are never touched.
|
|
29
|
+
*/
|
|
30
|
+
export function installImageZoom(
|
|
31
|
+
markdown: MarkdownExit,
|
|
32
|
+
options: InstallImageZoomOptions = {},
|
|
33
|
+
): void {
|
|
34
|
+
const renderImage = markdown.renderer.rules.image
|
|
35
|
+
|
|
36
|
+
markdown.renderer.rules.image = (tokens, index, renderOptions, env, renderer) => {
|
|
37
|
+
const renderOriginal = (): string | Promise<string> => renderImage
|
|
38
|
+
? renderImage(tokens, index, renderOptions, env, renderer)
|
|
39
|
+
: renderer.renderToken(tokens, index, renderOptions)
|
|
40
|
+
|
|
41
|
+
const token = tokens[index]
|
|
42
|
+
const renderEnv = (env ?? {}) as ImageZoomRenderEnv
|
|
43
|
+
const source = token.attrGet('src')
|
|
44
|
+
|
|
45
|
+
if (!source) {
|
|
46
|
+
return renderOriginal()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const { src, zoom } = splitImageSource(source)
|
|
50
|
+
token.attrSet('src', src)
|
|
51
|
+
|
|
52
|
+
if (zoom && renderEnv.frontmatter?.imageZoom !== false) {
|
|
53
|
+
const labels = options.resolveLabels?.(renderEnv.path ?? '/')
|
|
54
|
+
?? ENGLISH_IMAGE_ZOOM_LABELS
|
|
55
|
+
const alt = readAltText(token, renderOptions, env, renderer)
|
|
56
|
+
|
|
57
|
+
token.attrSet(ZOOM_ATTRIBUTE, '')
|
|
58
|
+
token.attrSet('role', 'button')
|
|
59
|
+
token.attrSet('tabindex', '0')
|
|
60
|
+
token.attrSet('aria-label', formatImageZoomLabel(labels.zoom, alt))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return renderOriginal()
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Splits the `#no-zoom` opt-out off an image URL. Any other hash is kept,
|
|
69
|
+
* because it can be a meaningful part of the image address.
|
|
70
|
+
*/
|
|
71
|
+
function splitImageSource(source: string): { src: string, zoom: boolean } {
|
|
72
|
+
const hashIndex = source.indexOf('#')
|
|
73
|
+
if (hashIndex < 0 || source.slice(hashIndex + 1).toLowerCase() !== NO_ZOOM_HASH) {
|
|
74
|
+
return { src: source, zoom: true }
|
|
75
|
+
}
|
|
76
|
+
return { src: source.slice(0, hashIndex), zoom: false }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Alt text of the image, stripped of inline markup exactly like the `alt`
|
|
81
|
+
* attribute the renderer writes, so the accessible name and the visible alt
|
|
82
|
+
* text always say the same thing.
|
|
83
|
+
*/
|
|
84
|
+
function readAltText(
|
|
85
|
+
token: Token,
|
|
86
|
+
renderOptions: RenderRuleArgs[2],
|
|
87
|
+
env: RenderRuleArgs[3],
|
|
88
|
+
renderer: RenderRuleArgs[4],
|
|
89
|
+
): string {
|
|
90
|
+
const children = token.children
|
|
91
|
+
if (!children || children.length === 0) {
|
|
92
|
+
return token.content
|
|
93
|
+
}
|
|
94
|
+
return renderer.renderInlineAsText(children, renderOptions, env)
|
|
95
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Plugin } from 'vite'
|
|
2
|
+
import type { ImageZoomLabels } from './labels'
|
|
3
|
+
import type { ImageZoomLocaleLanguages } from './locale'
|
|
4
|
+
import { stringifyJson } from 'kawapress'
|
|
5
|
+
|
|
6
|
+
export const IMAGE_ZOOM_OPTIONS_MODULE_ID = 'virtual:kawapress-image-zoom-options'
|
|
7
|
+
const RESOLVED_MODULE_ID = `\0${IMAGE_ZOOM_OPTIONS_MODULE_ID}`
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* What the runtime plugin needs to know about the site. It is JSON only: the
|
|
11
|
+
* two sides of a plugin never share module state.
|
|
12
|
+
*/
|
|
13
|
+
export interface ImageZoomRuntimeOptions {
|
|
14
|
+
labels: Partial<ImageZoomLabels>
|
|
15
|
+
localeLabels: Record<string, Partial<ImageZoomLabels>>
|
|
16
|
+
localeLanguages: ImageZoomLocaleLanguages
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Exposes the generator-side options to the browser as a virtual module, so a
|
|
21
|
+
* label override in `kawapress.config.ts` also drives the runtime dialog.
|
|
22
|
+
*/
|
|
23
|
+
export function imageZoomOptionsPlugin(options: ImageZoomRuntimeOptions): Plugin {
|
|
24
|
+
return {
|
|
25
|
+
name: 'kawapress:image-zoom-options',
|
|
26
|
+
resolveId(id) {
|
|
27
|
+
if (id === IMAGE_ZOOM_OPTIONS_MODULE_ID) {
|
|
28
|
+
return RESOLVED_MODULE_ID
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
load(id) {
|
|
32
|
+
if (id !== RESOLVED_MODULE_ID) {
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
return `export const imageZoomOptions = ${stringifyJson(options, {
|
|
36
|
+
label: 'the options of @kawapress/plugin-image-zoom',
|
|
37
|
+
path: 'imageZoom',
|
|
38
|
+
})}`
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { ImageZoomRuntimeOptions } from './options-module'
|
|
2
|
+
import { defineRuntimePlugin } from 'kawapress'
|
|
3
|
+
import { imageZoomOptions } from 'virtual:kawapress-image-zoom-options'
|
|
4
|
+
import { createImageZoomLabelResolver } from './labels'
|
|
5
|
+
import { closeImageZoom, installImageZoom } from './zoom-runtime'
|
|
6
|
+
import './image-zoom.css'
|
|
7
|
+
|
|
8
|
+
export default defineRuntimePlugin({
|
|
9
|
+
name: '@kawapress/plugin-image-zoom',
|
|
10
|
+
setup(api) {
|
|
11
|
+
const resolvePageLabels = createImageZoomLabelResolver(
|
|
12
|
+
imageZoomOptions as ImageZoomRuntimeOptions,
|
|
13
|
+
)
|
|
14
|
+
// The route path is the only locale signal both sides share: the generator
|
|
15
|
+
// resolved the same labels per page path while it rendered the images.
|
|
16
|
+
let currentPath = '/'
|
|
17
|
+
|
|
18
|
+
api.vueApp(() => {
|
|
19
|
+
if (!import.meta.env.SSR) {
|
|
20
|
+
installImageZoom({ resolveLabels: () => resolvePageLabels(currentPath) })
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
api.router((router) => {
|
|
25
|
+
if (import.meta.env.SSR) {
|
|
26
|
+
return
|
|
27
|
+
}
|
|
28
|
+
router.afterEach((to) => {
|
|
29
|
+
currentPath = to.path
|
|
30
|
+
// Navigating away closes the dialog, but focus belongs to the new page.
|
|
31
|
+
closeImageZoom({ restoreFocus: false })
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
},
|
|
35
|
+
})
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type { ImageZoomLabels } from './labels'
|
|
2
|
+
import { ENGLISH_IMAGE_ZOOM_LABELS } from './labels'
|
|
3
|
+
|
|
4
|
+
const ZOOMABLE_IMAGE_SELECTOR = 'img[data-kawa-zoom]'
|
|
5
|
+
const DIALOG_CLASS = 'kawa-image-zoom'
|
|
6
|
+
|
|
7
|
+
export interface InstallImageZoomOptions {
|
|
8
|
+
/** Labels of the page the visitor is on, resolved again on every open. */
|
|
9
|
+
resolveLabels: () => ImageZoomLabels
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface CloseImageZoomOptions {
|
|
13
|
+
/** Keep focus where it is instead of returning it to the opening image. */
|
|
14
|
+
restoreFocus?: boolean
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface ImageZoomElements {
|
|
18
|
+
dialog: HTMLDialogElement
|
|
19
|
+
image: HTMLImageElement
|
|
20
|
+
caption: HTMLElement
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let installed = false
|
|
24
|
+
let resolveLabels: InstallImageZoomOptions['resolveLabels'] | undefined
|
|
25
|
+
let elements: ImageZoomElements | undefined
|
|
26
|
+
let opener: HTMLImageElement | undefined
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Installs one document-level listener pair for the whole site. Delegation
|
|
30
|
+
* keeps images that Markdown rendered, images inside Vue components and images
|
|
31
|
+
* of a page the visitor navigates to later working the same way.
|
|
32
|
+
*/
|
|
33
|
+
export function installImageZoom(options: InstallImageZoomOptions): void {
|
|
34
|
+
if (installed) {
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
installed = true
|
|
38
|
+
resolveLabels = options.resolveLabels
|
|
39
|
+
document.addEventListener('click', handleClick)
|
|
40
|
+
document.addEventListener('keydown', handleKeydown)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Opens the zoom dialog for one image. The dialog is created on first use and
|
|
45
|
+
* reused afterwards, so opening an image costs one `showModal()` call.
|
|
46
|
+
*/
|
|
47
|
+
export function openImageZoom(
|
|
48
|
+
source: HTMLImageElement,
|
|
49
|
+
labels: ImageZoomLabels = currentLabels(),
|
|
50
|
+
): void {
|
|
51
|
+
const zoom = ensureElements()
|
|
52
|
+
|
|
53
|
+
zoom.dialog.setAttribute('aria-label', labels.dialog)
|
|
54
|
+
zoom.image.src = source.currentSrc || source.src
|
|
55
|
+
if (source.srcset) {
|
|
56
|
+
zoom.image.srcset = source.srcset
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
zoom.image.removeAttribute('srcset')
|
|
60
|
+
}
|
|
61
|
+
zoom.image.alt = source.alt
|
|
62
|
+
zoom.caption.textContent = source.alt
|
|
63
|
+
zoom.caption.hidden = source.alt.trim() === ''
|
|
64
|
+
|
|
65
|
+
opener = source
|
|
66
|
+
if (!zoom.dialog.open) {
|
|
67
|
+
zoom.dialog.showModal()
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function closeImageZoom(options: CloseImageZoomOptions = {}): void {
|
|
72
|
+
const zoom = elements
|
|
73
|
+
if (!zoom?.dialog.open) {
|
|
74
|
+
return
|
|
75
|
+
}
|
|
76
|
+
if (options.restoreFocus === false) {
|
|
77
|
+
opener = undefined
|
|
78
|
+
}
|
|
79
|
+
zoom.dialog.close()
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function handleClick(event: MouseEvent): void {
|
|
83
|
+
if (event.defaultPrevented || event.button !== 0 || hasModifier(event)) {
|
|
84
|
+
return
|
|
85
|
+
}
|
|
86
|
+
const image = findZoomableImage(event.target)
|
|
87
|
+
if (!image) {
|
|
88
|
+
return
|
|
89
|
+
}
|
|
90
|
+
// A zoomable image wrapped in a link should zoom instead of navigating.
|
|
91
|
+
event.preventDefault()
|
|
92
|
+
openImageZoom(image)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function handleKeydown(event: KeyboardEvent): void {
|
|
96
|
+
if (event.key !== 'Enter' && event.key !== ' ') {
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
const image = findZoomableImage(event.target)
|
|
100
|
+
if (!image) {
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
event.preventDefault()
|
|
104
|
+
openImageZoom(image)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function hasModifier(event: MouseEvent): boolean {
|
|
108
|
+
return event.metaKey || event.ctrlKey || event.shiftKey || event.altKey
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function findZoomableImage(target: EventTarget | null): HTMLImageElement | null {
|
|
112
|
+
if (!(target instanceof Element)) {
|
|
113
|
+
return null
|
|
114
|
+
}
|
|
115
|
+
return target.closest<HTMLImageElement>(ZOOMABLE_IMAGE_SELECTOR)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function currentLabels(): ImageZoomLabels {
|
|
119
|
+
return resolveLabels?.() ?? ENGLISH_IMAGE_ZOOM_LABELS
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Builds the dialog with the native `<dialog>` element, which brings the focus
|
|
124
|
+
* trap, the top layer and Escape handling. A click anywhere and a wheel turn
|
|
125
|
+
* close it, matching how image zoom feels in other documentation sites.
|
|
126
|
+
*/
|
|
127
|
+
function ensureElements(): ImageZoomElements {
|
|
128
|
+
if (elements) {
|
|
129
|
+
return elements
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const dialog = document.createElement('dialog')
|
|
133
|
+
dialog.className = DIALOG_CLASS
|
|
134
|
+
|
|
135
|
+
const figure = document.createElement('figure')
|
|
136
|
+
figure.className = `${DIALOG_CLASS}__figure`
|
|
137
|
+
|
|
138
|
+
const image = document.createElement('img')
|
|
139
|
+
image.className = `${DIALOG_CLASS}__image`
|
|
140
|
+
image.alt = ''
|
|
141
|
+
|
|
142
|
+
const caption = document.createElement('figcaption')
|
|
143
|
+
caption.className = `${DIALOG_CLASS}__caption`
|
|
144
|
+
caption.hidden = true
|
|
145
|
+
|
|
146
|
+
figure.append(image, caption)
|
|
147
|
+
dialog.append(figure)
|
|
148
|
+
dialog.addEventListener('click', () => closeImageZoom())
|
|
149
|
+
dialog.addEventListener('wheel', () => closeImageZoom(), { passive: true })
|
|
150
|
+
dialog.addEventListener('close', restoreOpenerFocus)
|
|
151
|
+
document.body.append(dialog)
|
|
152
|
+
|
|
153
|
+
elements = { caption, dialog, image }
|
|
154
|
+
return elements
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function restoreOpenerFocus(): void {
|
|
158
|
+
const target = opener
|
|
159
|
+
opener = undefined
|
|
160
|
+
if (target?.isConnected) {
|
|
161
|
+
target.focus()
|
|
162
|
+
}
|
|
163
|
+
}
|