@riebeckite/plugin-text-fragment 0.0.46 → 0.0.48
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/dist/client.js +100 -43
- package/dist/index.js +105 -49
- package/dist/src/popover.d.ts +19 -0
- package/package.json +4 -3
package/dist/client.js
CHANGED
|
@@ -1,5 +1,65 @@
|
|
|
1
1
|
// src/text-fragment.client.ts
|
|
2
2
|
import { copyText } from "@riebeckite/ui/copy";
|
|
3
|
+
import { findOverlayHost } from "@riebeckite/ui/overlay";
|
|
4
|
+
|
|
5
|
+
// src/popover.tsx
|
|
6
|
+
import { createRoot } from "hono/jsx/dom/client";
|
|
7
|
+
import { jsx, jsxs } from "hono/jsx/jsx-runtime";
|
|
8
|
+
function TextFragmentPopover({
|
|
9
|
+
labels,
|
|
10
|
+
visible,
|
|
11
|
+
status,
|
|
12
|
+
onCopyLink,
|
|
13
|
+
onCopyQuote
|
|
14
|
+
}) {
|
|
15
|
+
return (
|
|
16
|
+
// biome-ignore lint/a11y/useSemanticElements: Keep the existing popover markup and styling.
|
|
17
|
+
/* @__PURE__ */ jsxs(
|
|
18
|
+
"div",
|
|
19
|
+
{
|
|
20
|
+
class: "rr-text-fragment",
|
|
21
|
+
role: "group",
|
|
22
|
+
"aria-label": labels.regionLabel,
|
|
23
|
+
hidden: !visible,
|
|
24
|
+
onMouseDown: (event) => event.preventDefault(),
|
|
25
|
+
children: [
|
|
26
|
+
/* @__PURE__ */ jsxs("div", { class: "rr-text-fragment__actions", children: [
|
|
27
|
+
/* @__PURE__ */ jsx(
|
|
28
|
+
"button",
|
|
29
|
+
{
|
|
30
|
+
type: "button",
|
|
31
|
+
class: "rr-text-fragment__button",
|
|
32
|
+
onClick: onCopyLink,
|
|
33
|
+
children: labels.copyLinkLabel
|
|
34
|
+
}
|
|
35
|
+
),
|
|
36
|
+
/* @__PURE__ */ jsx(
|
|
37
|
+
"button",
|
|
38
|
+
{
|
|
39
|
+
type: "button",
|
|
40
|
+
class: "rr-text-fragment__button",
|
|
41
|
+
onClick: onCopyQuote,
|
|
42
|
+
children: labels.copyQuoteLabel
|
|
43
|
+
}
|
|
44
|
+
)
|
|
45
|
+
] }),
|
|
46
|
+
/* @__PURE__ */ jsx("p", { class: "rr-text-fragment__status", role: "status", "aria-live": "polite", children: status })
|
|
47
|
+
]
|
|
48
|
+
}
|
|
49
|
+
)
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
function mountTextFragmentPopover(container, options) {
|
|
53
|
+
const root = createRoot(container);
|
|
54
|
+
const render = (state) => {
|
|
55
|
+
root.render(/* @__PURE__ */ jsx(TextFragmentPopover, { ...options, ...state }));
|
|
56
|
+
};
|
|
57
|
+
render({ visible: false, status: "" });
|
|
58
|
+
return {
|
|
59
|
+
render,
|
|
60
|
+
unmount: () => root.unmount()
|
|
61
|
+
};
|
|
62
|
+
}
|
|
3
63
|
|
|
4
64
|
// src/text-fragment.ts
|
|
5
65
|
var TEXT_FRAGMENT_PREFIX = "#:~:text=";
|
|
@@ -70,37 +130,46 @@ function initTextFragmentShare(labels = {}) {
|
|
|
70
130
|
if (initialized) return;
|
|
71
131
|
initialized = true;
|
|
72
132
|
const resolved = { ...DEFAULT_TEXT_FRAGMENT_LABELS, ...labels };
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
actions.appendChild(quoteButton);
|
|
88
|
-
popover.appendChild(actions);
|
|
89
|
-
popover.appendChild(status);
|
|
90
|
-
document.body.appendChild(popover);
|
|
133
|
+
const overlayHost = findOverlayHost("text-fragment");
|
|
134
|
+
const host = overlayHost ?? document.createElement("div");
|
|
135
|
+
if (!overlayHost) document.body.appendChild(host);
|
|
136
|
+
const view = mountTextFragmentPopover(host, {
|
|
137
|
+
labels: resolved,
|
|
138
|
+
onCopyLink: () => {
|
|
139
|
+
onLinkButtonClick();
|
|
140
|
+
},
|
|
141
|
+
onCopyQuote: () => {
|
|
142
|
+
onQuoteButtonClick();
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
const popover = host.querySelector(".rr-text-fragment");
|
|
146
|
+
if (!popover) throw new Error("Failed to create the text fragment popover.");
|
|
91
147
|
let active = null;
|
|
148
|
+
let visible = false;
|
|
149
|
+
let statusText = "";
|
|
92
150
|
const copyingTimers = /* @__PURE__ */ new Set();
|
|
93
151
|
let isCopying = false;
|
|
152
|
+
const render = () => {
|
|
153
|
+
view.render({ visible, status: statusText });
|
|
154
|
+
};
|
|
155
|
+
const position = () => {
|
|
156
|
+
requestAnimationFrame(() => {
|
|
157
|
+
if (!visible || !active) return;
|
|
158
|
+
positionPopover(popover, active.rect);
|
|
159
|
+
});
|
|
160
|
+
};
|
|
94
161
|
const hide = () => {
|
|
95
162
|
active = null;
|
|
96
|
-
|
|
97
|
-
|
|
163
|
+
visible = false;
|
|
164
|
+
statusText = "";
|
|
165
|
+
render();
|
|
98
166
|
};
|
|
99
167
|
const show = (selection, resetStatus) => {
|
|
100
168
|
active = selection;
|
|
101
|
-
if (resetStatus)
|
|
102
|
-
|
|
103
|
-
|
|
169
|
+
if (resetStatus) statusText = "";
|
|
170
|
+
visible = true;
|
|
171
|
+
render();
|
|
172
|
+
position();
|
|
104
173
|
};
|
|
105
174
|
const refresh = () => {
|
|
106
175
|
if (isCopying) return;
|
|
@@ -117,10 +186,11 @@ function initTextFragmentShare(labels = {}) {
|
|
|
117
186
|
isCopying = true;
|
|
118
187
|
try {
|
|
119
188
|
const copied = await copyText(buildValue(selection.text));
|
|
120
|
-
|
|
189
|
+
statusText = copied ? successMessage : resolved.copyFailedLabel;
|
|
121
190
|
active = selection;
|
|
122
|
-
|
|
123
|
-
|
|
191
|
+
visible = true;
|
|
192
|
+
render();
|
|
193
|
+
position();
|
|
124
194
|
} finally {
|
|
125
195
|
const timer = window.setTimeout(() => {
|
|
126
196
|
copyingTimers.delete(timer);
|
|
@@ -145,21 +215,17 @@ function initTextFragmentShare(labels = {}) {
|
|
|
145
215
|
resolved.quoteCopiedLabel
|
|
146
216
|
);
|
|
147
217
|
};
|
|
148
|
-
const onPopoverMouseDown = (event) => event.preventDefault();
|
|
149
218
|
const onKeyDown = (event) => {
|
|
150
|
-
if (event.key === "Escape" &&
|
|
219
|
+
if (event.key === "Escape" && visible) {
|
|
151
220
|
event.preventDefault();
|
|
152
221
|
hide();
|
|
153
222
|
}
|
|
154
223
|
};
|
|
155
224
|
const onMouseDown = (event) => {
|
|
156
|
-
if (
|
|
225
|
+
if (!visible) return;
|
|
157
226
|
if (event.target instanceof Node && popover.contains(event.target)) return;
|
|
158
227
|
hide();
|
|
159
228
|
};
|
|
160
|
-
linkButton.addEventListener("click", onLinkButtonClick);
|
|
161
|
-
quoteButton.addEventListener("click", onQuoteButtonClick);
|
|
162
|
-
popover.addEventListener("mousedown", onPopoverMouseDown);
|
|
163
229
|
document.addEventListener("selectionchange", refresh);
|
|
164
230
|
document.addEventListener("mouseup", refresh);
|
|
165
231
|
document.addEventListener("keyup", refresh);
|
|
@@ -170,25 +236,16 @@ function initTextFragmentShare(labels = {}) {
|
|
|
170
236
|
initialized = false;
|
|
171
237
|
for (const timer of copyingTimers) window.clearTimeout(timer);
|
|
172
238
|
copyingTimers.clear();
|
|
173
|
-
linkButton.removeEventListener("click", onLinkButtonClick);
|
|
174
|
-
quoteButton.removeEventListener("click", onQuoteButtonClick);
|
|
175
|
-
popover.removeEventListener("mousedown", onPopoverMouseDown);
|
|
176
239
|
document.removeEventListener("selectionchange", refresh);
|
|
177
240
|
document.removeEventListener("mouseup", refresh);
|
|
178
241
|
document.removeEventListener("keyup", refresh);
|
|
179
242
|
document.removeEventListener("keydown", onKeyDown);
|
|
180
243
|
document.removeEventListener("mousedown", onMouseDown);
|
|
181
244
|
window.removeEventListener("scroll", hide);
|
|
182
|
-
|
|
245
|
+
view.unmount();
|
|
246
|
+
if (!overlayHost) host.remove();
|
|
183
247
|
};
|
|
184
248
|
}
|
|
185
|
-
function createButton(label) {
|
|
186
|
-
const button = document.createElement("button");
|
|
187
|
-
button.type = "button";
|
|
188
|
-
button.className = "rr-text-fragment__button";
|
|
189
|
-
button.textContent = label;
|
|
190
|
-
return button;
|
|
191
|
-
}
|
|
192
249
|
function readShareableSelection() {
|
|
193
250
|
const selection = window.getSelection();
|
|
194
251
|
if (!selection || selection.isCollapsed || selection.rangeCount === 0) {
|
package/dist/index.js
CHANGED
|
@@ -7,6 +7,66 @@ import {
|
|
|
7
7
|
|
|
8
8
|
// src/text-fragment.client.ts
|
|
9
9
|
import { copyText } from "@riebeckite/ui/copy";
|
|
10
|
+
import { findOverlayHost } from "@riebeckite/ui/overlay";
|
|
11
|
+
|
|
12
|
+
// src/popover.tsx
|
|
13
|
+
import { createRoot } from "hono/jsx/dom/client";
|
|
14
|
+
import { jsx, jsxs } from "hono/jsx/jsx-runtime";
|
|
15
|
+
function TextFragmentPopover({
|
|
16
|
+
labels,
|
|
17
|
+
visible,
|
|
18
|
+
status,
|
|
19
|
+
onCopyLink,
|
|
20
|
+
onCopyQuote
|
|
21
|
+
}) {
|
|
22
|
+
return (
|
|
23
|
+
// biome-ignore lint/a11y/useSemanticElements: Keep the existing popover markup and styling.
|
|
24
|
+
/* @__PURE__ */ jsxs(
|
|
25
|
+
"div",
|
|
26
|
+
{
|
|
27
|
+
class: "rr-text-fragment",
|
|
28
|
+
role: "group",
|
|
29
|
+
"aria-label": labels.regionLabel,
|
|
30
|
+
hidden: !visible,
|
|
31
|
+
onMouseDown: (event) => event.preventDefault(),
|
|
32
|
+
children: [
|
|
33
|
+
/* @__PURE__ */ jsxs("div", { class: "rr-text-fragment__actions", children: [
|
|
34
|
+
/* @__PURE__ */ jsx(
|
|
35
|
+
"button",
|
|
36
|
+
{
|
|
37
|
+
type: "button",
|
|
38
|
+
class: "rr-text-fragment__button",
|
|
39
|
+
onClick: onCopyLink,
|
|
40
|
+
children: labels.copyLinkLabel
|
|
41
|
+
}
|
|
42
|
+
),
|
|
43
|
+
/* @__PURE__ */ jsx(
|
|
44
|
+
"button",
|
|
45
|
+
{
|
|
46
|
+
type: "button",
|
|
47
|
+
class: "rr-text-fragment__button",
|
|
48
|
+
onClick: onCopyQuote,
|
|
49
|
+
children: labels.copyQuoteLabel
|
|
50
|
+
}
|
|
51
|
+
)
|
|
52
|
+
] }),
|
|
53
|
+
/* @__PURE__ */ jsx("p", { class: "rr-text-fragment__status", role: "status", "aria-live": "polite", children: status })
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
)
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
function mountTextFragmentPopover(container, options) {
|
|
60
|
+
const root = createRoot(container);
|
|
61
|
+
const render = (state) => {
|
|
62
|
+
root.render(/* @__PURE__ */ jsx(TextFragmentPopover, { ...options, ...state }));
|
|
63
|
+
};
|
|
64
|
+
render({ visible: false, status: "" });
|
|
65
|
+
return {
|
|
66
|
+
render,
|
|
67
|
+
unmount: () => root.unmount()
|
|
68
|
+
};
|
|
69
|
+
}
|
|
10
70
|
|
|
11
71
|
// src/text-fragment.ts
|
|
12
72
|
var TEXT_FRAGMENT_PREFIX = "#:~:text=";
|
|
@@ -77,37 +137,46 @@ function initTextFragmentShare(labels = {}) {
|
|
|
77
137
|
if (initialized) return;
|
|
78
138
|
initialized = true;
|
|
79
139
|
const resolved = { ...DEFAULT_TEXT_FRAGMENT_LABELS, ...labels };
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
actions.appendChild(quoteButton);
|
|
95
|
-
popover.appendChild(actions);
|
|
96
|
-
popover.appendChild(status);
|
|
97
|
-
document.body.appendChild(popover);
|
|
140
|
+
const overlayHost = findOverlayHost("text-fragment");
|
|
141
|
+
const host = overlayHost ?? document.createElement("div");
|
|
142
|
+
if (!overlayHost) document.body.appendChild(host);
|
|
143
|
+
const view = mountTextFragmentPopover(host, {
|
|
144
|
+
labels: resolved,
|
|
145
|
+
onCopyLink: () => {
|
|
146
|
+
onLinkButtonClick();
|
|
147
|
+
},
|
|
148
|
+
onCopyQuote: () => {
|
|
149
|
+
onQuoteButtonClick();
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
const popover = host.querySelector(".rr-text-fragment");
|
|
153
|
+
if (!popover) throw new Error("Failed to create the text fragment popover.");
|
|
98
154
|
let active = null;
|
|
155
|
+
let visible = false;
|
|
156
|
+
let statusText = "";
|
|
99
157
|
const copyingTimers = /* @__PURE__ */ new Set();
|
|
100
158
|
let isCopying = false;
|
|
159
|
+
const render = () => {
|
|
160
|
+
view.render({ visible, status: statusText });
|
|
161
|
+
};
|
|
162
|
+
const position = () => {
|
|
163
|
+
requestAnimationFrame(() => {
|
|
164
|
+
if (!visible || !active) return;
|
|
165
|
+
positionPopover(popover, active.rect);
|
|
166
|
+
});
|
|
167
|
+
};
|
|
101
168
|
const hide = () => {
|
|
102
169
|
active = null;
|
|
103
|
-
|
|
104
|
-
|
|
170
|
+
visible = false;
|
|
171
|
+
statusText = "";
|
|
172
|
+
render();
|
|
105
173
|
};
|
|
106
174
|
const show = (selection, resetStatus) => {
|
|
107
175
|
active = selection;
|
|
108
|
-
if (resetStatus)
|
|
109
|
-
|
|
110
|
-
|
|
176
|
+
if (resetStatus) statusText = "";
|
|
177
|
+
visible = true;
|
|
178
|
+
render();
|
|
179
|
+
position();
|
|
111
180
|
};
|
|
112
181
|
const refresh = () => {
|
|
113
182
|
if (isCopying) return;
|
|
@@ -124,10 +193,11 @@ function initTextFragmentShare(labels = {}) {
|
|
|
124
193
|
isCopying = true;
|
|
125
194
|
try {
|
|
126
195
|
const copied = await copyText(buildValue(selection.text));
|
|
127
|
-
|
|
196
|
+
statusText = copied ? successMessage : resolved.copyFailedLabel;
|
|
128
197
|
active = selection;
|
|
129
|
-
|
|
130
|
-
|
|
198
|
+
visible = true;
|
|
199
|
+
render();
|
|
200
|
+
position();
|
|
131
201
|
} finally {
|
|
132
202
|
const timer = window.setTimeout(() => {
|
|
133
203
|
copyingTimers.delete(timer);
|
|
@@ -152,21 +222,17 @@ function initTextFragmentShare(labels = {}) {
|
|
|
152
222
|
resolved.quoteCopiedLabel
|
|
153
223
|
);
|
|
154
224
|
};
|
|
155
|
-
const onPopoverMouseDown = (event) => event.preventDefault();
|
|
156
225
|
const onKeyDown = (event) => {
|
|
157
|
-
if (event.key === "Escape" &&
|
|
226
|
+
if (event.key === "Escape" && visible) {
|
|
158
227
|
event.preventDefault();
|
|
159
228
|
hide();
|
|
160
229
|
}
|
|
161
230
|
};
|
|
162
231
|
const onMouseDown = (event) => {
|
|
163
|
-
if (
|
|
232
|
+
if (!visible) return;
|
|
164
233
|
if (event.target instanceof Node && popover.contains(event.target)) return;
|
|
165
234
|
hide();
|
|
166
235
|
};
|
|
167
|
-
linkButton.addEventListener("click", onLinkButtonClick);
|
|
168
|
-
quoteButton.addEventListener("click", onQuoteButtonClick);
|
|
169
|
-
popover.addEventListener("mousedown", onPopoverMouseDown);
|
|
170
236
|
document.addEventListener("selectionchange", refresh);
|
|
171
237
|
document.addEventListener("mouseup", refresh);
|
|
172
238
|
document.addEventListener("keyup", refresh);
|
|
@@ -177,25 +243,16 @@ function initTextFragmentShare(labels = {}) {
|
|
|
177
243
|
initialized = false;
|
|
178
244
|
for (const timer of copyingTimers) window.clearTimeout(timer);
|
|
179
245
|
copyingTimers.clear();
|
|
180
|
-
linkButton.removeEventListener("click", onLinkButtonClick);
|
|
181
|
-
quoteButton.removeEventListener("click", onQuoteButtonClick);
|
|
182
|
-
popover.removeEventListener("mousedown", onPopoverMouseDown);
|
|
183
246
|
document.removeEventListener("selectionchange", refresh);
|
|
184
247
|
document.removeEventListener("mouseup", refresh);
|
|
185
248
|
document.removeEventListener("keyup", refresh);
|
|
186
249
|
document.removeEventListener("keydown", onKeyDown);
|
|
187
250
|
document.removeEventListener("mousedown", onMouseDown);
|
|
188
251
|
window.removeEventListener("scroll", hide);
|
|
189
|
-
|
|
252
|
+
view.unmount();
|
|
253
|
+
if (!overlayHost) host.remove();
|
|
190
254
|
};
|
|
191
255
|
}
|
|
192
|
-
function createButton(label) {
|
|
193
|
-
const button = document.createElement("button");
|
|
194
|
-
button.type = "button";
|
|
195
|
-
button.className = "rr-text-fragment__button";
|
|
196
|
-
button.textContent = label;
|
|
197
|
-
return button;
|
|
198
|
-
}
|
|
199
256
|
function readShareableSelection() {
|
|
200
257
|
const selection = window.getSelection();
|
|
201
258
|
if (!selection || selection.isCollapsed || selection.rangeCount === 0) {
|
|
@@ -245,15 +302,14 @@ function textFragment(options = {}) {
|
|
|
245
302
|
)
|
|
246
303
|
],
|
|
247
304
|
clientEntries: [
|
|
248
|
-
createClientEntry(
|
|
249
|
-
"text-fragment",
|
|
250
|
-
"
|
|
251
|
-
|
|
252
|
-
{
|
|
305
|
+
createClientEntry("text-fragment", {
|
|
306
|
+
moduleSpecifier: "@riebeckite/plugin-text-fragment/client",
|
|
307
|
+
exportName: "initTextFragmentShare",
|
|
308
|
+
publicConfig: {
|
|
253
309
|
...DEFAULT_TEXT_FRAGMENT_LABELS,
|
|
254
310
|
...options.labels
|
|
255
311
|
}
|
|
256
|
-
)
|
|
312
|
+
})
|
|
257
313
|
]
|
|
258
314
|
});
|
|
259
315
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** @jsxRuntime automatic */
|
|
2
|
+
/** @jsxImportSource hono/jsx */
|
|
3
|
+
import type { TextFragmentLabels } from "./text-fragment.client.js";
|
|
4
|
+
export type TextFragmentPopoverProps = {
|
|
5
|
+
labels: TextFragmentLabels;
|
|
6
|
+
visible: boolean;
|
|
7
|
+
status: string;
|
|
8
|
+
onCopyLink: () => void;
|
|
9
|
+
onCopyQuote: () => void;
|
|
10
|
+
};
|
|
11
|
+
export declare function TextFragmentPopover({ labels, visible, status, onCopyLink, onCopyQuote, }: TextFragmentPopoverProps): import("hono/jsx/jsx-dev-runtime").JSX.Element;
|
|
12
|
+
export type TextFragmentPopoverState = {
|
|
13
|
+
visible: boolean;
|
|
14
|
+
status: string;
|
|
15
|
+
};
|
|
16
|
+
export declare function mountTextFragmentPopover(container: HTMLElement, options: Omit<TextFragmentPopoverProps, "visible" | "status">): {
|
|
17
|
+
render: (state: TextFragmentPopoverState) => void;
|
|
18
|
+
unmount: () => void;
|
|
19
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@riebeckite/plugin-text-fragment",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.48",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -21,8 +21,9 @@
|
|
|
21
21
|
"./style.css": "./style.css"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"
|
|
25
|
-
"@riebeckite/
|
|
24
|
+
"hono": "^4.13.18",
|
|
25
|
+
"@riebeckite/core": "0.0.48",
|
|
26
|
+
"@riebeckite/ui": "0.0.48"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
29
|
"tsx": "^4.22.4"
|