@riebeckite/plugin-ux 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 +201 -0
- package/README.md +119 -0
- package/README_ja.md +100 -0
- package/dist/client.d.ts +2 -0
- package/dist/client.js +286 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +104 -0
- package/dist/src/init.d.ts +5 -0
- package/dist/src/inject.d.ts +16 -0
- package/dist/src/options.d.ts +40 -0
- package/package.json +61 -0
- package/style.css +127 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright Rerurate_514
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# @riebeckite/plugin-ux
|
|
2
|
+
|
|
3
|
+
Client-side progressive enhancements for reading. The plugin leaves the built
|
|
4
|
+
article HTML untouched and adds a reading progress bar, a back-to-top button,
|
|
5
|
+
table-of-contents scroll-spy, and code copy buttons at runtime.
|
|
6
|
+
|
|
7
|
+
[日本語](./README_ja.md)
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Reading progress bar** — a thin fixed bar (`.rb-ux__progress`) that follows
|
|
12
|
+
the scroll position through the article.
|
|
13
|
+
- **Back-to-top button** — a real `button` (`.rb-ux__back-to-top`) with an
|
|
14
|
+
`aria-label` that appears after scrolling and smooth-scrolls to the top.
|
|
15
|
+
- **TOC scroll-spy** — observes `a[href^="#"]` links inside the article table of
|
|
16
|
+
contents with `IntersectionObserver` and marks the active link with
|
|
17
|
+
`.rb-ux__toc-active`.
|
|
18
|
+
- **Code copy buttons** — wraps each `pre > code` in `.rb-ux__code` and adds a
|
|
19
|
+
copy button (`.rb-ux__copy`) with a transient "copied" state.
|
|
20
|
+
|
|
21
|
+
Every feature is a no-op when the relevant DOM is absent, and `initUx()` is
|
|
22
|
+
idempotent.
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { defineConfig } from "@riebeckite/core";
|
|
28
|
+
import { uxPlugin } from "@riebeckite/plugin-ux";
|
|
29
|
+
|
|
30
|
+
export default defineConfig({
|
|
31
|
+
// ...
|
|
32
|
+
plugins: [
|
|
33
|
+
uxPlugin({
|
|
34
|
+
progress: true,
|
|
35
|
+
backToTop: true,
|
|
36
|
+
tocScrollSpy: true,
|
|
37
|
+
codeCopy: true,
|
|
38
|
+
backToTopLabel: "Back to top",
|
|
39
|
+
copyLabel: "Copy",
|
|
40
|
+
copiedLabel: "Copied",
|
|
41
|
+
}),
|
|
42
|
+
],
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The factory is also exported as `ux`.
|
|
47
|
+
|
|
48
|
+
## Options
|
|
49
|
+
|
|
50
|
+
| Option | Type | Default | Description |
|
|
51
|
+
| --- | --- | --- | --- |
|
|
52
|
+
| `progress` | `boolean` | `true` | Show the reading progress bar |
|
|
53
|
+
| `backToTop` | `boolean` | `true` | Show the back-to-top button |
|
|
54
|
+
| `tocScrollSpy` | `boolean` | `true` | Highlight the active table-of-contents link |
|
|
55
|
+
| `codeCopy` | `boolean` | `true` | Add a copy button to each code block |
|
|
56
|
+
| `backToTopLabel` | `string` | `"Back to top"` | `aria-label` for the back-to-top button |
|
|
57
|
+
| `copyLabel` | `string` | `"Copy"` | Label of the copy button |
|
|
58
|
+
| `copiedLabel` | `string` | `"Copied"` | Transient label shown after copying |
|
|
59
|
+
|
|
60
|
+
## How configuration reaches the client
|
|
61
|
+
|
|
62
|
+
Client initializers are bundled statically and cannot receive plugin options.
|
|
63
|
+
At build time the plugin prepends an inert JSON element to each article HTML:
|
|
64
|
+
|
|
65
|
+
```html
|
|
66
|
+
<script type="application/json" id="rb-ux-config" data-rb-ux-config>{...}</script>
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`initUx()` reads that element to restore the options; when it is missing, every
|
|
70
|
+
feature falls back to its enabled default. Injection happens in
|
|
71
|
+
`onPostProcessed` (the object `getProcessedContent()` caches and renders) and is
|
|
72
|
+
mirrored onto remaining manifest entries in `onManifestCreated`. The
|
|
73
|
+
`data-rb-ux-config` attribute doubles as the marker that keeps the element from
|
|
74
|
+
being inserted twice per page.
|
|
75
|
+
|
|
76
|
+
## Emitted HTML / CSS hooks
|
|
77
|
+
|
|
78
|
+
| Class | Target |
|
|
79
|
+
| --- | --- |
|
|
80
|
+
| `rb-ux__progress` | Progress bar track (`role="progressbar"`) |
|
|
81
|
+
| `rb-ux__progress-bar` | Bar scaled with `scaleX` |
|
|
82
|
+
| `rb-ux__back-to-top` | Back-to-top button |
|
|
83
|
+
| `rb-ux__back-to-top--visible` | Added while the button is visible |
|
|
84
|
+
| `rb-ux__toc-active` | Active table-of-contents link |
|
|
85
|
+
| `rb-ux__code` | Code block wrapper |
|
|
86
|
+
| `rb-ux__copy` | Copy button |
|
|
87
|
+
| `rb-ux__copy--copied` | Transient copied state |
|
|
88
|
+
|
|
89
|
+
Styles ship as `@riebeckite/plugin-ux/style.css` and use the theme's
|
|
90
|
+
`--rb-color-*` tokens so they do not fight the existing theme.
|
|
91
|
+
|
|
92
|
+
## Accessibility
|
|
93
|
+
|
|
94
|
+
- The back-to-top control is a `button` with an `aria-label`.
|
|
95
|
+
- The progress bar exposes `role="progressbar"` with `aria-valuemin`,
|
|
96
|
+
`aria-valuemax`, and `aria-valuenow`.
|
|
97
|
+
- The active table-of-contents link receives `aria-current="true"`.
|
|
98
|
+
- Under `prefers-reduced-motion: reduce`, progress and back-to-top transitions
|
|
99
|
+
are disabled and the back-to-top jump becomes an immediate scroll.
|
|
100
|
+
- Progress bar, buttons, and copy buttons are hidden when printing.
|
|
101
|
+
|
|
102
|
+
## Limitations
|
|
103
|
+
|
|
104
|
+
- Client-only: without JavaScript nothing is added.
|
|
105
|
+
- No SPA support; a full page load re-initializes the enhancements.
|
|
106
|
+
- The TOC scroll-spy looks for `.rr-table-of-contents`, `.table-of-contents`,
|
|
107
|
+
or `[data-rb-toc]` as the table-of-contents container.
|
|
108
|
+
- Copy buttons are skipped inside `.rr-code` blocks (managed by the
|
|
109
|
+
code-enhance plugin) to avoid duplicate copy UI.
|
|
110
|
+
|
|
111
|
+
## Exports
|
|
112
|
+
|
|
113
|
+
- `uxPlugin(options?)` / `ux(options?)` — plugin factory
|
|
114
|
+
- `initUx` — browser initializer
|
|
115
|
+
- Types: `UxOptions`, `UxResolvedConfig`
|
|
116
|
+
|
|
117
|
+
## See also
|
|
118
|
+
|
|
119
|
+
- [Plugin guide](../../../docs/en/plugin-system.md)
|
package/README_ja.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# @riebeckite/plugin-ux
|
|
2
|
+
|
|
3
|
+
記事の読みやすさを高めるクライアント側のプログレッシブ・エンハンスメントをまとめたプラグインです。ビルド後の記事 HTML はそのままに、読み進捗バー・トップへ戻るボタン・目次のスクロール連動ハイライト・コードのコピーボタンを追加します。
|
|
4
|
+
|
|
5
|
+
[English](./README.md)
|
|
6
|
+
|
|
7
|
+
## できること
|
|
8
|
+
|
|
9
|
+
- **読み進捗バー**: 画面上部に固定した細いバー(`.rb-ux__progress`)が記事のスクロール量に追従します。
|
|
10
|
+
- **トップへ戻るボタン**: 一定量スクロールすると表示される button 要素(`.rb-ux__back-to-top`)。`aria-label` を持ち、スムーズスクロールで先頭へ戻ります。
|
|
11
|
+
- **目次のスクロール連動**: 記事の目次コンテナ内の `a[href^="#"]` を `IntersectionObserver` で監視し、現在位置の見出しリンクに `.rb-ux__toc-active` を付けます。
|
|
12
|
+
- **コードのコピーボタン**: 各 `pre > code`(`.rb-ux__code` でラップ)にコピーボタン(`.rb-ux__copy`)を追加します。コピー直後は一時的に「コピーしました」表示へ切り替わります。
|
|
13
|
+
|
|
14
|
+
いずれも対応する DOM が無いページでは何もしません。`initUx()` は何度呼んでも二重に初期化しません。
|
|
15
|
+
|
|
16
|
+
## 使い方
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { defineConfig } from "@riebeckite/core";
|
|
20
|
+
import { uxPlugin } from "@riebeckite/plugin-ux";
|
|
21
|
+
|
|
22
|
+
export default defineConfig({
|
|
23
|
+
// ...
|
|
24
|
+
plugins: [
|
|
25
|
+
uxPlugin({
|
|
26
|
+
progress: true,
|
|
27
|
+
backToTop: true,
|
|
28
|
+
tocScrollSpy: true,
|
|
29
|
+
codeCopy: true,
|
|
30
|
+
backToTopLabel: "トップへ戻る",
|
|
31
|
+
copyLabel: "コピー",
|
|
32
|
+
copiedLabel: "コピーしました",
|
|
33
|
+
}),
|
|
34
|
+
],
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`uxPlugin` は `ux` という別名でもエクスポートしています。
|
|
39
|
+
|
|
40
|
+
## オプション
|
|
41
|
+
|
|
42
|
+
| 項目 | 型 | 既定値 | 説明 |
|
|
43
|
+
| --- | --- | --- | --- |
|
|
44
|
+
| `progress` | `boolean` | `true` | 読み進捗バーを表示する |
|
|
45
|
+
| `backToTop` | `boolean` | `true` | トップへ戻るボタンを表示する |
|
|
46
|
+
| `tocScrollSpy` | `boolean` | `true` | 目次のスクロール連動ハイライトを有効にする |
|
|
47
|
+
| `codeCopy` | `boolean` | `true` | コードのコピーボタンを追加する |
|
|
48
|
+
| `backToTopLabel` | `string` | `"Back to top"` | トップへ戻るボタンの `aria-label` |
|
|
49
|
+
| `copyLabel` | `string` | `"Copy"` | コピーボタンのラベル |
|
|
50
|
+
| `copiedLabel` | `string` | `"Copied"` | コピー成功時に一時表示するラベル |
|
|
51
|
+
|
|
52
|
+
## 設定がクライアントへ渡る仕組み
|
|
53
|
+
|
|
54
|
+
クライアント側の初期化コードは静的にバンドルされ、プラグインのオプションを直接受け取れません。そのためこのプラグインは、ビルド時に各記事 HTML の先頭へ次の不活性な JSON 要素を挿入します。
|
|
55
|
+
|
|
56
|
+
```html
|
|
57
|
+
<script type="application/json" id="rb-ux-config" data-rb-ux-config>{...}</script>
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
`initUx()` はこの要素を読み取って設定を復元します。要素が無い場合は全機能が有効な既定値で動作します。挿入は `onPostProcessed` で行い、`onManifestCreated` でも未挿入の記事エントリへ反映し、`getProcessedContent()` が返すキャッシュ済み HTML も同時に更新します。挿入はページごとに一度だけで、`data-rb-ux-config` を目印に重複挿入を防ぎます。
|
|
61
|
+
|
|
62
|
+
## 出力される HTML / CSS フック
|
|
63
|
+
|
|
64
|
+
| クラス | 対象 |
|
|
65
|
+
| --- | --- |
|
|
66
|
+
| `rb-ux__progress` | 進捗バーのトラック(`role="progressbar"`) |
|
|
67
|
+
| `rb-ux__progress-bar` | 進捗に応じて `scaleX` されるバー |
|
|
68
|
+
| `rb-ux__back-to-top` | トップへ戻る button |
|
|
69
|
+
| `rb-ux__back-to-top--visible` | 表示状態のとき付与 |
|
|
70
|
+
| `rb-ux__toc-active` | 現在位置の目次リンク |
|
|
71
|
+
| `rb-ux__code` | コードブロックのラッパー |
|
|
72
|
+
| `rb-ux__copy` | コピーボタン |
|
|
73
|
+
| `rb-ux__copy--copied` | コピー直後の一時状態 |
|
|
74
|
+
|
|
75
|
+
スタイルは `@riebeckite/plugin-ux/style.css` として配布し、テーマの `--rb-color-*` トークンを参照するため既存テーマと競合しません。
|
|
76
|
+
|
|
77
|
+
## アクセシビリティ
|
|
78
|
+
|
|
79
|
+
- トップへ戻るボタンは `button` 要素で、`aria-label` を持ちます。
|
|
80
|
+
- 進捗バーは `role="progressbar"` と `aria-valuemin` / `aria-valuemax` / `aria-valuenow` を持ちます。
|
|
81
|
+
- 目次の現在位置には `aria-current="true"` を付けます。
|
|
82
|
+
- `prefers-reduced-motion: reduce` では進捗バーとトップへ戻るボタンのアニメーションを無効化し、トップへ戻る動作も即時スクロールへ切り替えます。
|
|
83
|
+
- 印刷時は進捗バー・ボタン・コピーボタンを非表示にします。
|
|
84
|
+
|
|
85
|
+
## 制限事項
|
|
86
|
+
|
|
87
|
+
- クライアント専用です。JavaScript が無効な環境では何も追加されません。
|
|
88
|
+
- SPA ナビゲーションには対応しません。ページ遷移後は再読み込み時に初期化されます。
|
|
89
|
+
- 目次のスクロール連動は `.rr-table-of-contents`、`.table-of-contents`、`[data-rb-toc]` のいずれかを目次コンテナとして探します。
|
|
90
|
+
- コードのコピーボタンは `.rr-code`(code-enhance プラグインが管理するブロック)には追加しません。二重のコピー UI を避けるためです。
|
|
91
|
+
|
|
92
|
+
## 主なエクスポート
|
|
93
|
+
|
|
94
|
+
- `uxPlugin(options?)` / `ux(options?)`: プラグインを作成する
|
|
95
|
+
- `initUx`: クライアント側の初期化
|
|
96
|
+
- 型: `UxOptions`、`UxResolvedConfig`
|
|
97
|
+
|
|
98
|
+
## 関連資料
|
|
99
|
+
|
|
100
|
+
- [プラグインシステム](../../../docs/ja/plugin-system.md)
|
package/dist/client.d.ts
ADDED
package/dist/client.js
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
// src/options.ts
|
|
2
|
+
var UX_CONFIG_ELEMENT_ID = "rb-ux-config";
|
|
3
|
+
var DEFAULT_UX_CONFIG = {
|
|
4
|
+
progress: true,
|
|
5
|
+
backToTop: true,
|
|
6
|
+
tocScrollSpy: true,
|
|
7
|
+
codeCopy: true,
|
|
8
|
+
backToTopLabel: "Back to top",
|
|
9
|
+
copyLabel: "Copy",
|
|
10
|
+
copiedLabel: "Copied"
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// src/init.ts
|
|
14
|
+
var PROGRESS_CLASS = "rb-ux__progress";
|
|
15
|
+
var PROGRESS_BAR_CLASS = "rb-ux__progress-bar";
|
|
16
|
+
var BACK_TO_TOP_CLASS = "rb-ux__back-to-top";
|
|
17
|
+
var BACK_TO_TOP_VISIBLE_CLASS = "rb-ux__back-to-top--visible";
|
|
18
|
+
var TOC_ACTIVE_CLASS = "rb-ux__toc-active";
|
|
19
|
+
var CODE_CLASS = "rb-ux__code";
|
|
20
|
+
var COPY_CLASS = "rb-ux__copy";
|
|
21
|
+
var COPY_COPIED_CLASS = "rb-ux__copy--copied";
|
|
22
|
+
var BACK_TO_TOP_THRESHOLD = 320;
|
|
23
|
+
var COPIED_STATE_DURATION = 1500;
|
|
24
|
+
var started = false;
|
|
25
|
+
var copiedTimers = /* @__PURE__ */ new WeakMap();
|
|
26
|
+
function initUx() {
|
|
27
|
+
if (started) return;
|
|
28
|
+
started = true;
|
|
29
|
+
const config = readUxConfig();
|
|
30
|
+
const run = () => {
|
|
31
|
+
if (config.progress) setupProgress();
|
|
32
|
+
if (config.backToTop) setupBackToTop(config);
|
|
33
|
+
if (config.tocScrollSpy) setupTocScrollSpy();
|
|
34
|
+
if (config.codeCopy) setupCodeCopy(config);
|
|
35
|
+
};
|
|
36
|
+
if (document.readyState === "loading") {
|
|
37
|
+
document.addEventListener("DOMContentLoaded", run, { once: true });
|
|
38
|
+
} else {
|
|
39
|
+
run();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function readUxConfig() {
|
|
43
|
+
const element = document.getElementById(UX_CONFIG_ELEMENT_ID);
|
|
44
|
+
const parsed = parseConfig(element?.textContent ?? null);
|
|
45
|
+
if (!parsed) return { ...DEFAULT_UX_CONFIG };
|
|
46
|
+
return {
|
|
47
|
+
progress: readBoolean(parsed.progress, DEFAULT_UX_CONFIG.progress),
|
|
48
|
+
backToTop: readBoolean(parsed.backToTop, DEFAULT_UX_CONFIG.backToTop),
|
|
49
|
+
tocScrollSpy: readBoolean(
|
|
50
|
+
parsed.tocScrollSpy,
|
|
51
|
+
DEFAULT_UX_CONFIG.tocScrollSpy
|
|
52
|
+
),
|
|
53
|
+
codeCopy: readBoolean(parsed.codeCopy, DEFAULT_UX_CONFIG.codeCopy),
|
|
54
|
+
backToTopLabel: readString(
|
|
55
|
+
parsed.backToTopLabel,
|
|
56
|
+
DEFAULT_UX_CONFIG.backToTopLabel
|
|
57
|
+
),
|
|
58
|
+
copyLabel: readString(parsed.copyLabel, DEFAULT_UX_CONFIG.copyLabel),
|
|
59
|
+
copiedLabel: readString(parsed.copiedLabel, DEFAULT_UX_CONFIG.copiedLabel)
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function parseConfig(raw) {
|
|
63
|
+
if (!raw) return null;
|
|
64
|
+
try {
|
|
65
|
+
const parsed = JSON.parse(raw);
|
|
66
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
67
|
+
return parsed;
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
} catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function readBoolean(value, fallback) {
|
|
75
|
+
return typeof value === "boolean" ? value : fallback;
|
|
76
|
+
}
|
|
77
|
+
function readString(value, fallback) {
|
|
78
|
+
return typeof value === "string" && value.trim().length > 0 ? value : fallback;
|
|
79
|
+
}
|
|
80
|
+
function setupProgress() {
|
|
81
|
+
const article = findArticle();
|
|
82
|
+
if (!article || hasElement(PROGRESS_CLASS)) return;
|
|
83
|
+
const track = document.createElement("div");
|
|
84
|
+
track.className = PROGRESS_CLASS;
|
|
85
|
+
track.setAttribute("role", "progressbar");
|
|
86
|
+
track.setAttribute("aria-label", "Reading progress");
|
|
87
|
+
track.setAttribute("aria-valuemin", "0");
|
|
88
|
+
track.setAttribute("aria-valuemax", "100");
|
|
89
|
+
const fill = document.createElement("span");
|
|
90
|
+
fill.className = PROGRESS_BAR_CLASS;
|
|
91
|
+
track.append(fill);
|
|
92
|
+
document.body.append(track);
|
|
93
|
+
const update = () => {
|
|
94
|
+
const ratio = readingProgress(article);
|
|
95
|
+
fill.style.transform = `scaleX(${ratio})`;
|
|
96
|
+
track.setAttribute("aria-valuenow", String(Math.round(ratio * 100)));
|
|
97
|
+
};
|
|
98
|
+
scheduleOnScroll(update);
|
|
99
|
+
}
|
|
100
|
+
function readingProgress(article) {
|
|
101
|
+
const top = article.getBoundingClientRect().top + window.scrollY;
|
|
102
|
+
const end = top + article.offsetHeight - window.innerHeight;
|
|
103
|
+
if (end <= top) return window.scrollY >= top ? 1 : 0;
|
|
104
|
+
return clamp((window.scrollY - top) / (end - top), 0, 1);
|
|
105
|
+
}
|
|
106
|
+
function setupBackToTop(config) {
|
|
107
|
+
if (hasElement(BACK_TO_TOP_CLASS)) return;
|
|
108
|
+
const button = document.createElement("button");
|
|
109
|
+
button.type = "button";
|
|
110
|
+
button.className = BACK_TO_TOP_CLASS;
|
|
111
|
+
button.setAttribute("aria-label", config.backToTopLabel);
|
|
112
|
+
button.textContent = "↑";
|
|
113
|
+
button.addEventListener("click", () => {
|
|
114
|
+
window.scrollTo({
|
|
115
|
+
top: 0,
|
|
116
|
+
behavior: prefersReducedMotion() ? "auto" : "smooth"
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
document.body.append(button);
|
|
120
|
+
const threshold = () => Math.max(BACK_TO_TOP_THRESHOLD, window.innerHeight * 0.5);
|
|
121
|
+
const update = () => {
|
|
122
|
+
button.classList.toggle(
|
|
123
|
+
BACK_TO_TOP_VISIBLE_CLASS,
|
|
124
|
+
window.scrollY >= threshold()
|
|
125
|
+
);
|
|
126
|
+
};
|
|
127
|
+
scheduleOnScroll(update);
|
|
128
|
+
}
|
|
129
|
+
function setupTocScrollSpy() {
|
|
130
|
+
const container = document.querySelector(
|
|
131
|
+
".rr-table-of-contents, .table-of-contents, [data-rb-toc]"
|
|
132
|
+
);
|
|
133
|
+
if (!container || container.dataset.rbUxToc === "true") return;
|
|
134
|
+
const links = Array.from(
|
|
135
|
+
container.querySelectorAll('a[href^="#"]')
|
|
136
|
+
);
|
|
137
|
+
if (links.length === 0) return;
|
|
138
|
+
const linksById = /* @__PURE__ */ new Map();
|
|
139
|
+
for (const link of links) {
|
|
140
|
+
const id = hashToId(link.getAttribute("href"));
|
|
141
|
+
if (!id) continue;
|
|
142
|
+
const existing = linksById.get(id) ?? [];
|
|
143
|
+
existing.push(link);
|
|
144
|
+
linksById.set(id, existing);
|
|
145
|
+
}
|
|
146
|
+
const headings = Array.from(linksById.keys()).map((id) => document.getElementById(id)).filter((heading) => heading !== null);
|
|
147
|
+
if (headings.length === 0) return;
|
|
148
|
+
container.dataset.rbUxToc = "true";
|
|
149
|
+
const activate = (id) => {
|
|
150
|
+
for (const link of links) {
|
|
151
|
+
link.classList.remove(TOC_ACTIVE_CLASS);
|
|
152
|
+
link.removeAttribute("aria-current");
|
|
153
|
+
}
|
|
154
|
+
for (const link of linksById.get(id) ?? []) {
|
|
155
|
+
link.classList.add(TOC_ACTIVE_CLASS);
|
|
156
|
+
link.setAttribute("aria-current", "true");
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
const visible = /* @__PURE__ */ new Map();
|
|
160
|
+
let activeId = null;
|
|
161
|
+
const updateActive = () => {
|
|
162
|
+
let nextId = null;
|
|
163
|
+
if (visible.size > 0) {
|
|
164
|
+
let bestTop = Number.POSITIVE_INFINITY;
|
|
165
|
+
for (const [id, top] of visible) {
|
|
166
|
+
if (top < bestTop) {
|
|
167
|
+
bestTop = top;
|
|
168
|
+
nextId = id;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
} else {
|
|
172
|
+
for (const heading of headings) {
|
|
173
|
+
if (heading.getBoundingClientRect().top <= 0) nextId = heading.id;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
nextId ??= headings[0]?.id ?? null;
|
|
177
|
+
if (nextId && nextId !== activeId) {
|
|
178
|
+
activeId = nextId;
|
|
179
|
+
activate(nextId);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
if (typeof IntersectionObserver === "function") {
|
|
183
|
+
const observer = new IntersectionObserver(
|
|
184
|
+
(entries) => {
|
|
185
|
+
for (const entry of entries) {
|
|
186
|
+
const id = entry.target.id;
|
|
187
|
+
if (!id) continue;
|
|
188
|
+
if (entry.isIntersecting) {
|
|
189
|
+
visible.set(id, entry.boundingClientRect.top);
|
|
190
|
+
} else {
|
|
191
|
+
visible.delete(id);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
updateActive();
|
|
195
|
+
},
|
|
196
|
+
{ rootMargin: "-10% 0px -60% 0px", threshold: [0, 1] }
|
|
197
|
+
);
|
|
198
|
+
for (const heading of headings) observer.observe(heading);
|
|
199
|
+
}
|
|
200
|
+
scheduleOnScroll(updateActive);
|
|
201
|
+
}
|
|
202
|
+
function setupCodeCopy(config) {
|
|
203
|
+
const blocks = Array.from(document.querySelectorAll("pre > code"));
|
|
204
|
+
for (const code of blocks) {
|
|
205
|
+
const pre = code.parentElement;
|
|
206
|
+
if (!pre || pre.dataset.rbUxCopy === "true") continue;
|
|
207
|
+
if (pre.closest(".rr-code")) continue;
|
|
208
|
+
pre.dataset.rbUxCopy = "true";
|
|
209
|
+
const wrapper = document.createElement("div");
|
|
210
|
+
wrapper.className = CODE_CLASS;
|
|
211
|
+
pre.replaceWith(wrapper);
|
|
212
|
+
wrapper.append(pre);
|
|
213
|
+
const button = document.createElement("button");
|
|
214
|
+
button.type = "button";
|
|
215
|
+
button.className = COPY_CLASS;
|
|
216
|
+
button.setAttribute("aria-label", config.copyLabel);
|
|
217
|
+
button.textContent = config.copyLabel;
|
|
218
|
+
button.addEventListener("click", () => {
|
|
219
|
+
void copyCode(button, code, config);
|
|
220
|
+
});
|
|
221
|
+
wrapper.append(button);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
async function copyCode(button, code, config) {
|
|
225
|
+
const text = code.textContent ?? "";
|
|
226
|
+
const clipboard = navigator.clipboard;
|
|
227
|
+
if (text.length === 0 || !clipboard) return;
|
|
228
|
+
try {
|
|
229
|
+
await clipboard.writeText(text);
|
|
230
|
+
} catch {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
showCopiedState(button, config);
|
|
234
|
+
}
|
|
235
|
+
function showCopiedState(button, config) {
|
|
236
|
+
const previous = copiedTimers.get(button);
|
|
237
|
+
if (previous !== void 0) window.clearTimeout(previous);
|
|
238
|
+
button.textContent = config.copiedLabel;
|
|
239
|
+
button.classList.add(COPY_COPIED_CLASS);
|
|
240
|
+
const timer = window.setTimeout(() => {
|
|
241
|
+
button.textContent = config.copyLabel;
|
|
242
|
+
button.classList.remove(COPY_COPIED_CLASS);
|
|
243
|
+
copiedTimers.delete(button);
|
|
244
|
+
}, COPIED_STATE_DURATION);
|
|
245
|
+
copiedTimers.set(button, timer);
|
|
246
|
+
}
|
|
247
|
+
function findArticle() {
|
|
248
|
+
return document.querySelector(
|
|
249
|
+
".rb-article-body, .article-shell__body, [data-slot='article-body'], article"
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
function hasElement(className) {
|
|
253
|
+
return document.querySelector(`.${className}`) !== null;
|
|
254
|
+
}
|
|
255
|
+
function hashToId(href) {
|
|
256
|
+
if (typeof href !== "string" || href.length < 2) return null;
|
|
257
|
+
if (!href.startsWith("#")) return null;
|
|
258
|
+
const raw = href.slice(1);
|
|
259
|
+
try {
|
|
260
|
+
return decodeURIComponent(raw);
|
|
261
|
+
} catch {
|
|
262
|
+
return raw;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
function prefersReducedMotion() {
|
|
266
|
+
return typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
267
|
+
}
|
|
268
|
+
function clamp(value, min, max) {
|
|
269
|
+
return Math.min(Math.max(value, min), max);
|
|
270
|
+
}
|
|
271
|
+
function scheduleOnScroll(update) {
|
|
272
|
+
let frame = 0;
|
|
273
|
+
const schedule = () => {
|
|
274
|
+
if (frame) return;
|
|
275
|
+
frame = requestAnimationFrame(() => {
|
|
276
|
+
frame = 0;
|
|
277
|
+
update();
|
|
278
|
+
});
|
|
279
|
+
};
|
|
280
|
+
window.addEventListener("scroll", schedule, { passive: true });
|
|
281
|
+
window.addEventListener("resize", schedule);
|
|
282
|
+
update();
|
|
283
|
+
}
|
|
284
|
+
export {
|
|
285
|
+
initUx
|
|
286
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type UxOptions } from "./src/options.js";
|
|
2
|
+
export type { UxOptions, UxResolvedConfig } from "./src/options.js";
|
|
3
|
+
/**
|
|
4
|
+
* Client-only progressive enhancements: a reading progress bar, a
|
|
5
|
+
* back-to-top button, table-of-contents scroll-spy, and code copy buttons.
|
|
6
|
+
*
|
|
7
|
+
* None of the features change the rendered article markup, so the plugin
|
|
8
|
+
* only injects a JSON config element and registers a browser initializer.
|
|
9
|
+
*/
|
|
10
|
+
export declare function uxPlugin(options?: UxOptions): import("@riebeckite/core").RiebeckitePlugin<UxOptions>;
|
|
11
|
+
/** Alias kept for naming symmetry with the other plugin factories. */
|
|
12
|
+
export declare const ux: typeof uxPlugin;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
import {
|
|
3
|
+
createClientEntry,
|
|
4
|
+
createStyleAsset,
|
|
5
|
+
definePlugin,
|
|
6
|
+
isPublished
|
|
7
|
+
} from "@riebeckite/core";
|
|
8
|
+
|
|
9
|
+
// src/options.ts
|
|
10
|
+
var UX_CONFIG_ELEMENT_ID = "rb-ux-config";
|
|
11
|
+
var UX_CONFIG_ATTRIBUTE = "data-rb-ux-config";
|
|
12
|
+
var DEFAULT_UX_CONFIG = {
|
|
13
|
+
progress: true,
|
|
14
|
+
backToTop: true,
|
|
15
|
+
tocScrollSpy: true,
|
|
16
|
+
codeCopy: true,
|
|
17
|
+
backToTopLabel: "Back to top",
|
|
18
|
+
copyLabel: "Copy",
|
|
19
|
+
copiedLabel: "Copied"
|
|
20
|
+
};
|
|
21
|
+
function resolveUxConfig(options = {}) {
|
|
22
|
+
return {
|
|
23
|
+
progress: options.progress ?? DEFAULT_UX_CONFIG.progress,
|
|
24
|
+
backToTop: options.backToTop ?? DEFAULT_UX_CONFIG.backToTop,
|
|
25
|
+
tocScrollSpy: options.tocScrollSpy ?? DEFAULT_UX_CONFIG.tocScrollSpy,
|
|
26
|
+
codeCopy: options.codeCopy ?? DEFAULT_UX_CONFIG.codeCopy,
|
|
27
|
+
backToTopLabel: normalizeLabel(
|
|
28
|
+
options.backToTopLabel,
|
|
29
|
+
DEFAULT_UX_CONFIG.backToTopLabel
|
|
30
|
+
),
|
|
31
|
+
copyLabel: normalizeLabel(options.copyLabel, DEFAULT_UX_CONFIG.copyLabel),
|
|
32
|
+
copiedLabel: normalizeLabel(
|
|
33
|
+
options.copiedLabel,
|
|
34
|
+
DEFAULT_UX_CONFIG.copiedLabel
|
|
35
|
+
)
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function normalizeLabel(value, fallback) {
|
|
39
|
+
return typeof value === "string" && value.trim().length > 0 ? value : fallback;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/inject.ts
|
|
43
|
+
function renderUxConfigElement(config) {
|
|
44
|
+
const json = JSON.stringify(config).replace(/</g, "\\u003c").replace(/>/g, "\\u003e");
|
|
45
|
+
return `<script type="application/json" id="${UX_CONFIG_ELEMENT_ID}" ${UX_CONFIG_ATTRIBUTE}>${json}</script>`;
|
|
46
|
+
}
|
|
47
|
+
function injectUxConfig(html, config) {
|
|
48
|
+
if (html.includes(UX_CONFIG_ATTRIBUTE)) return html;
|
|
49
|
+
return `${renderUxConfigElement(config)}${html}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// index.ts
|
|
53
|
+
function uxPlugin(options = {}) {
|
|
54
|
+
const config = resolveUxConfig(options);
|
|
55
|
+
const tracked = /* @__PURE__ */ new Map();
|
|
56
|
+
return definePlugin({
|
|
57
|
+
name: "ux",
|
|
58
|
+
options,
|
|
59
|
+
validateOptions: validateUxOptions,
|
|
60
|
+
assets: [createStyleAsset("ux")],
|
|
61
|
+
clientEntries: [createClientEntry("ux", "initUx")],
|
|
62
|
+
onPostProcessed: (context) => {
|
|
63
|
+
context.content.html = injectUxConfig(context.content.html, config);
|
|
64
|
+
tracked.set(context.slug, context.content);
|
|
65
|
+
},
|
|
66
|
+
onManifestCreated: (context) => {
|
|
67
|
+
const siteConfig = context.config;
|
|
68
|
+
for (const entry of context.manifest.entries) {
|
|
69
|
+
if (siteConfig && !isPublished(siteConfig, entry.frontmatter)) continue;
|
|
70
|
+
const html = injectUxConfig(entry.html, config);
|
|
71
|
+
if (html === entry.html) continue;
|
|
72
|
+
entry.html = html;
|
|
73
|
+
const content = tracked.get(entry.slug);
|
|
74
|
+
if (content) content.html = html;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
var ux = uxPlugin;
|
|
80
|
+
function validateUxOptions(options) {
|
|
81
|
+
if (!options) return [];
|
|
82
|
+
const issues = [];
|
|
83
|
+
for (const key of [
|
|
84
|
+
"progress",
|
|
85
|
+
"backToTop",
|
|
86
|
+
"tocScrollSpy",
|
|
87
|
+
"codeCopy"
|
|
88
|
+
]) {
|
|
89
|
+
if (options[key] !== void 0 && typeof options[key] !== "boolean") {
|
|
90
|
+
issues.push({ path: key, message: "Expected a boolean." });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
for (const key of ["backToTopLabel", "copyLabel", "copiedLabel"]) {
|
|
94
|
+
const value = options[key];
|
|
95
|
+
if (value !== void 0 && (typeof value !== "string" || value.trim() === "")) {
|
|
96
|
+
issues.push({ path: key, message: "Expected a non-empty string." });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return issues;
|
|
100
|
+
}
|
|
101
|
+
export {
|
|
102
|
+
ux,
|
|
103
|
+
uxPlugin
|
|
104
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type UxResolvedConfig } from "./options.js";
|
|
2
|
+
/**
|
|
3
|
+
* Serializes the resolved options into an inert JSON script element.
|
|
4
|
+
*
|
|
5
|
+
* Client initializers are static and receive no plugin options, so the build
|
|
6
|
+
* injects this element into the article HTML and reads it back at runtime.
|
|
7
|
+
* `<` and `>` are escaped so labels can never terminate the script element.
|
|
8
|
+
*/
|
|
9
|
+
export declare function renderUxConfigElement(config: UxResolvedConfig): string;
|
|
10
|
+
/**
|
|
11
|
+
* Prepends the configuration element exactly once.
|
|
12
|
+
*
|
|
13
|
+
* The attribute doubles as the idempotency marker, so repeated calls (for
|
|
14
|
+
* example from both `onPostProcessed` and `onManifestCreated`) are no-ops.
|
|
15
|
+
*/
|
|
16
|
+
export declare function injectUxConfig(html: string, config: UxResolvedConfig): string;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public option and configuration types for the UX plugin.
|
|
3
|
+
*
|
|
4
|
+
* This module is imported by both the build-time plugin and the browser
|
|
5
|
+
* initializer, so it must stay free of Node-only and Riebeckite-only imports.
|
|
6
|
+
*/
|
|
7
|
+
export type UxOptions = {
|
|
8
|
+
/** Show a fixed reading-progress bar for the article. Defaults to `true`. */
|
|
9
|
+
progress?: boolean;
|
|
10
|
+
/** Show a back-to-top button after scrolling. Defaults to `true`. */
|
|
11
|
+
backToTop?: boolean;
|
|
12
|
+
/** Highlight the active entry in the article table of contents. Defaults to `true`. */
|
|
13
|
+
tocScrollSpy?: boolean;
|
|
14
|
+
/** Add a copy button to every code block. Defaults to `true`. */
|
|
15
|
+
codeCopy?: boolean;
|
|
16
|
+
/** Accessible label for the back-to-top button. */
|
|
17
|
+
backToTopLabel?: string;
|
|
18
|
+
/** Label for the code copy button. */
|
|
19
|
+
copyLabel?: string;
|
|
20
|
+
/** Transient label shown after a successful copy. */
|
|
21
|
+
copiedLabel?: string;
|
|
22
|
+
};
|
|
23
|
+
/** Options after defaults have been applied. Serialized into the page. */
|
|
24
|
+
export type UxResolvedConfig = {
|
|
25
|
+
progress: boolean;
|
|
26
|
+
backToTop: boolean;
|
|
27
|
+
tocScrollSpy: boolean;
|
|
28
|
+
codeCopy: boolean;
|
|
29
|
+
backToTopLabel: string;
|
|
30
|
+
copyLabel: string;
|
|
31
|
+
copiedLabel: string;
|
|
32
|
+
};
|
|
33
|
+
/** Id of the JSON configuration element injected into each article. */
|
|
34
|
+
export declare const UX_CONFIG_ELEMENT_ID = "rb-ux-config";
|
|
35
|
+
/** Attribute used both to stamp the config element and to detect it. */
|
|
36
|
+
export declare const UX_CONFIG_ATTRIBUTE = "data-rb-ux-config";
|
|
37
|
+
/** Defaults used when an option is omitted or the config element is absent. */
|
|
38
|
+
export declare const DEFAULT_UX_CONFIG: UxResolvedConfig;
|
|
39
|
+
/** Applies defaults to the user-provided options. */
|
|
40
|
+
export declare function resolveUxConfig(options?: UxOptions): UxResolvedConfig;
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@riebeckite/plugin-ux",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"source": "./index.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./client": {
|
|
16
|
+
"types": "./dist/client.d.ts",
|
|
17
|
+
"import": "./dist/client.js",
|
|
18
|
+
"source": "./client.ts",
|
|
19
|
+
"default": "./dist/client.js"
|
|
20
|
+
},
|
|
21
|
+
"./style.css": "./style.css"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@riebeckite/core": "0.0.1"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"LICENSE",
|
|
31
|
+
"README.md",
|
|
32
|
+
"README_ja.md",
|
|
33
|
+
"dist",
|
|
34
|
+
"style.css"
|
|
35
|
+
],
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "https://github.com/Rerurate514/riebeckite.git",
|
|
39
|
+
"directory": "packages/plugins/ux"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://github.com/Rerurate514/riebeckite#readme",
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/Rerurate514/riebeckite/issues"
|
|
44
|
+
},
|
|
45
|
+
"description": "Reading experience enhancements for Riebeckite sites.",
|
|
46
|
+
"keywords": [
|
|
47
|
+
"riebeckite",
|
|
48
|
+
"plugin",
|
|
49
|
+
"user-experience",
|
|
50
|
+
"progressive-enhancement"
|
|
51
|
+
],
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
54
|
+
},
|
|
55
|
+
"sideEffects": [
|
|
56
|
+
"./style.css"
|
|
57
|
+
],
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "node ../../../scripts/build_package.mjs"
|
|
60
|
+
}
|
|
61
|
+
}
|
package/style.css
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
.rb-ux__progress {
|
|
2
|
+
position: fixed;
|
|
3
|
+
inset-block-start: 0;
|
|
4
|
+
inset-inline: 0;
|
|
5
|
+
z-index: 60;
|
|
6
|
+
height: 3px;
|
|
7
|
+
pointer-events: none;
|
|
8
|
+
background: color-mix(
|
|
9
|
+
in srgb,
|
|
10
|
+
var(--rb-color-ink, #1c1a17) 8%,
|
|
11
|
+
transparent
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.rb-ux__progress-bar {
|
|
16
|
+
display: block;
|
|
17
|
+
width: 100%;
|
|
18
|
+
height: 100%;
|
|
19
|
+
transform: scaleX(0);
|
|
20
|
+
transform-origin: left center;
|
|
21
|
+
background: var(--rb-color-accent, #686868);
|
|
22
|
+
transition: transform 100ms linear;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
.rb-ux__back-to-top {
|
|
26
|
+
position: fixed;
|
|
27
|
+
inset-block-end: 1.5rem;
|
|
28
|
+
inset-inline-end: 1.5rem;
|
|
29
|
+
z-index: 60;
|
|
30
|
+
display: grid;
|
|
31
|
+
place-items: center;
|
|
32
|
+
width: 2.75rem;
|
|
33
|
+
height: 2.75rem;
|
|
34
|
+
padding: 0;
|
|
35
|
+
border: 1px solid
|
|
36
|
+
var(--rb-color-border-strong, var(--rb-color-border, #d0ccc4));
|
|
37
|
+
border-radius: 999px;
|
|
38
|
+
background: var(--rb-color-surface, var(--rb-color-paper, #ffffff));
|
|
39
|
+
color: var(--rb-color-ink, #1c1a17);
|
|
40
|
+
font-size: 1.15rem;
|
|
41
|
+
line-height: 1;
|
|
42
|
+
cursor: pointer;
|
|
43
|
+
opacity: 0;
|
|
44
|
+
visibility: hidden;
|
|
45
|
+
transform: translateY(0.5rem);
|
|
46
|
+
transition:
|
|
47
|
+
opacity 150ms ease,
|
|
48
|
+
transform 150ms ease,
|
|
49
|
+
visibility 150ms ease;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.rb-ux__back-to-top--visible {
|
|
53
|
+
opacity: 1;
|
|
54
|
+
visibility: visible;
|
|
55
|
+
transform: none;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.rb-ux__back-to-top:hover {
|
|
59
|
+
background: var(--rb-color-surface-hover, var(--rb-color-surface, #f4f2ee));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.rb-ux__back-to-top:focus-visible {
|
|
63
|
+
outline: 2px solid var(--rb-color-accent, #686868);
|
|
64
|
+
outline-offset: 2px;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.rb-ux__toc-active {
|
|
68
|
+
color: var(--rb-color-accent, #686868);
|
|
69
|
+
font-weight: 600;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
.rb-ux__code {
|
|
73
|
+
position: relative;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
.rb-ux__copy {
|
|
77
|
+
position: absolute;
|
|
78
|
+
inset-block-start: 0.5rem;
|
|
79
|
+
inset-inline-end: 0.5rem;
|
|
80
|
+
z-index: 1;
|
|
81
|
+
padding: 0.2rem 0.5rem;
|
|
82
|
+
border: 1px solid
|
|
83
|
+
var(--rb-color-border, color-mix(in srgb, currentColor 20%, transparent));
|
|
84
|
+
border-radius: 4px;
|
|
85
|
+
background: var(--rb-color-surface, var(--rb-color-paper, #ffffff));
|
|
86
|
+
color: var(--rb-color-muted, #69645c);
|
|
87
|
+
font-size: 0.75rem;
|
|
88
|
+
line-height: 1.4;
|
|
89
|
+
cursor: pointer;
|
|
90
|
+
opacity: 0.9;
|
|
91
|
+
transition:
|
|
92
|
+
opacity 120ms ease,
|
|
93
|
+
color 120ms ease,
|
|
94
|
+
border-color 120ms ease;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
.rb-ux__copy:hover,
|
|
98
|
+
.rb-ux__copy:focus-visible {
|
|
99
|
+
opacity: 1;
|
|
100
|
+
color: var(--rb-color-ink, #1c1a17);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
.rb-ux__copy:focus-visible {
|
|
104
|
+
outline: 2px solid var(--rb-color-accent, #686868);
|
|
105
|
+
outline-offset: 2px;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
.rb-ux__copy--copied {
|
|
109
|
+
color: var(--rb-color-accent, #686868);
|
|
110
|
+
border-color: var(--rb-color-accent, #686868);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
@media (prefers-reduced-motion: reduce) {
|
|
114
|
+
.rb-ux__progress-bar,
|
|
115
|
+
.rb-ux__back-to-top,
|
|
116
|
+
.rb-ux__copy {
|
|
117
|
+
transition: none;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
@media print {
|
|
122
|
+
.rb-ux__progress,
|
|
123
|
+
.rb-ux__back-to-top,
|
|
124
|
+
.rb-ux__copy {
|
|
125
|
+
display: none;
|
|
126
|
+
}
|
|
127
|
+
}
|