@galaxycore/ai-chat 0.4.0 → 0.6.0
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/CHANGELOG.md +17 -0
- package/README.md +3 -3
- package/dist/appearance.js +1 -1
- package/dist/context.js +1 -1
- package/dist/headless.d.ts +2 -2
- package/dist/headless.js +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +354 -240
- package/dist/index.js.map +1 -1
- package/dist/{labels-DJsjblzj.js → labels-BM5o9a9K.js} +5 -1
- package/dist/{labels-DJsjblzj.js.map → labels-BM5o9a9K.js.map} +1 -1
- package/dist/{labels-BBjaNAlS.d.ts → labels-D8smv23O.d.ts} +36 -2
- package/dist/markdown.d.ts +1 -1
- package/dist/markdown.js +1 -1
- package/dist/markdown.js.map +1 -1
- package/dist/styles.css +207 -112
- package/dist/targeting.css +205 -0
- package/dist/targeting.d.ts +101 -0
- package/dist/targeting.js +585 -0
- package/dist/targeting.js.map +1 -0
- package/dist/{ui-primitives-DlZ1hJkr.js → ui-primitives-BCbe9Ds8.js} +4 -4
- package/dist/{ui-primitives-DlZ1hJkr.js.map → ui-primitives-BCbe9Ds8.js.map} +1 -1
- package/docs/api-reference.md +2 -2
- package/docs/design.md +2 -2
- package/docs/integration.md +3 -4
- package/docs/patent-prototype.md +72 -0
- package/docs/patent-visual-baseline.json +121 -0
- package/docs/publishing.md +10 -10
- package/docs/validation.md +12 -0
- package/package.json +8 -10
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
|
|
2
|
+
import { ChatContextItem, ChatContextValue } from "./context.js";
|
|
3
|
+
//#region src/targeting.d.ts
|
|
4
|
+
export type ChatTargetRect = [number, number, number, number];
|
|
5
|
+
export type ChatTargetKind = 'text' | 'section' | 'region';
|
|
6
|
+
export interface ChatTargetHit {
|
|
7
|
+
kind: ChatTargetKind;
|
|
8
|
+
targetId: string;
|
|
9
|
+
label: string;
|
|
10
|
+
text?: string;
|
|
11
|
+
rect?: ChatTargetRect;
|
|
12
|
+
element?: Element;
|
|
13
|
+
comment?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface ChatTargetingLabels {
|
|
16
|
+
addToAi: string;
|
|
17
|
+
shortcutHint: string;
|
|
18
|
+
commentPlaceholder: string;
|
|
19
|
+
add: string;
|
|
20
|
+
addWithoutNote: string;
|
|
21
|
+
cancel: string;
|
|
22
|
+
regionSuffix: string;
|
|
23
|
+
sectionSuffix: string;
|
|
24
|
+
}
|
|
25
|
+
export declare const zhCNTargetingLabels: ChatTargetingLabels;
|
|
26
|
+
export declare const enUSTargetingLabels: ChatTargetingLabels;
|
|
27
|
+
export interface PendingChatTarget {
|
|
28
|
+
hit: ChatTargetHit;
|
|
29
|
+
/** 'pill' starts as the compact "+ Add to AI" button; 'popover' opens directly with the inline comment input. */
|
|
30
|
+
presentation: 'pill' | 'popover';
|
|
31
|
+
/** Viewport coordinates where the user released the pointer. */
|
|
32
|
+
anchor: {
|
|
33
|
+
x: number;
|
|
34
|
+
y: number;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export interface ActiveMarquee {
|
|
38
|
+
element: Element;
|
|
39
|
+
rect: ChatTargetRect;
|
|
40
|
+
/** Viewport coordinates for rendering the marquee overlay. */
|
|
41
|
+
box: {
|
|
42
|
+
left: number;
|
|
43
|
+
top: number;
|
|
44
|
+
width: number;
|
|
45
|
+
height: number;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export interface UseChatTargetingOptions {
|
|
49
|
+
/** Auto-activates text selection pill, double-click section targeting, and direct image drag when true. Defaults to true. */
|
|
50
|
+
enabled?: boolean;
|
|
51
|
+
/** Scope root for targeting listeners. Defaults to document.body. */
|
|
52
|
+
root?: Element | (() => Element | null);
|
|
53
|
+
/** Input/textarea element to focus after adding a reference; pressing Escape blurs it. */
|
|
54
|
+
focusTarget?: HTMLElement | (() => HTMLElement | null);
|
|
55
|
+
/** CSS selector for semantic section cards (double-click / target mode). Defaults to '[data-ai-target]'. */
|
|
56
|
+
targetSelector?: string;
|
|
57
|
+
/** CSS selector for direct-drag image surfaces. Defaults to '[data-ai-image]'. */
|
|
58
|
+
imageSelector?: string;
|
|
59
|
+
/** CSS selector for excluded regions (such as the chat panel itself). */
|
|
60
|
+
excludeSelector?: string;
|
|
61
|
+
/** CSS selector for regions that own their own text selection toolbar. */
|
|
62
|
+
selectionToolbarSelector?: string;
|
|
63
|
+
/** Minimum pixel drag size on an image to create a region. Defaults to 12. */
|
|
64
|
+
minDragPixels?: number;
|
|
65
|
+
/** Override whether a text selection should show the floating pill. */
|
|
66
|
+
shouldHandleSelection?: (selection: Selection, range: Range, event: MouseEvent) => boolean;
|
|
67
|
+
/** Override whether double-clicking an element should target its section block. */
|
|
68
|
+
shouldHandleDoubleClick?: (element: Element, event: MouseEvent) => boolean;
|
|
69
|
+
/** Override whether pointerdown on an image surface should start a region drag. */
|
|
70
|
+
canDragRegion?: (element: Element, event: PointerEvent) => boolean;
|
|
71
|
+
/** Override semantic target resolution. Defaults to element.closest(targetSelector). */
|
|
72
|
+
resolveTarget?: (element: Element, event: Event) => Element | null;
|
|
73
|
+
/** Called when the user commits a targeted reference (with optional inline comment). */
|
|
74
|
+
onAdd: (hit: ChatTargetHit) => void | Promise<void>;
|
|
75
|
+
}
|
|
76
|
+
export interface ChatTargetingController {
|
|
77
|
+
pending: PendingChatTarget | null;
|
|
78
|
+
marquee: ActiveMarquee | null;
|
|
79
|
+
commentExpanded: boolean;
|
|
80
|
+
setCommentExpanded: (expanded: boolean) => void;
|
|
81
|
+
targetMode: boolean;
|
|
82
|
+
setTargetMode: (active: boolean) => void;
|
|
83
|
+
commit: (comment?: string) => Promise<void>;
|
|
84
|
+
dismiss: () => void;
|
|
85
|
+
}
|
|
86
|
+
/** Converts a resolved targeting hit into a validated ChatContextItem for useChatContext. */
|
|
87
|
+
export declare function toChatContextItem(hit: ChatTargetHit, options?: {
|
|
88
|
+
id?: string;
|
|
89
|
+
metadata?: Record<string, ChatContextValue>;
|
|
90
|
+
}): ChatContextItem;
|
|
91
|
+
export declare function useChatTargeting(options: UseChatTargetingOptions): ChatTargetingController;
|
|
92
|
+
export interface ChatTargetingOverlayProps {
|
|
93
|
+
targeting: ChatTargetingController;
|
|
94
|
+
labels?: Partial<ChatTargetingLabels>;
|
|
95
|
+
theme?: 'light' | 'dark' | 'auto';
|
|
96
|
+
className?: string;
|
|
97
|
+
}
|
|
98
|
+
/** Default floating UI for useChatTargeting: centered "+ Add to AI" pill, inline comment popover, and marquee box. */
|
|
99
|
+
export declare function ChatTargetingOverlay({ targeting, labels: overrides, theme, className }: ChatTargetingOverlayProps): import("react").ReactPortal | null;
|
|
100
|
+
//#endregion
|
|
101
|
+
//# sourceMappingURL=targeting.d.ts.map
|
|
@@ -0,0 +1,585 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use client";
|
|
3
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
4
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
5
|
+
import { createPortal } from "react-dom";
|
|
6
|
+
//#region src/targeting.tsx
|
|
7
|
+
const zhCNTargetingLabels = {
|
|
8
|
+
addToAi: "添加到 AI",
|
|
9
|
+
shortcutHint: "⌘L",
|
|
10
|
+
commentPlaceholder: "可选备注…(按 Enter 添加)",
|
|
11
|
+
add: "添加",
|
|
12
|
+
addWithoutNote: "直接添加",
|
|
13
|
+
cancel: "取消",
|
|
14
|
+
regionSuffix: "区域",
|
|
15
|
+
sectionSuffix: "段落"
|
|
16
|
+
};
|
|
17
|
+
const enUSTargetingLabels = {
|
|
18
|
+
addToAi: "Add to AI",
|
|
19
|
+
shortcutHint: "⌘L",
|
|
20
|
+
commentPlaceholder: "Optional comment… (Enter to add)",
|
|
21
|
+
add: "Add",
|
|
22
|
+
addWithoutNote: "Add without note",
|
|
23
|
+
cancel: "Cancel",
|
|
24
|
+
regionSuffix: "region",
|
|
25
|
+
sectionSuffix: "section"
|
|
26
|
+
};
|
|
27
|
+
const DEFAULT_TARGET_SELECTOR = "[data-ai-target]";
|
|
28
|
+
const DEFAULT_IMAGE_SELECTOR = "[data-ai-image]";
|
|
29
|
+
const DEFAULT_EXCLUDE_SELECTOR = "[data-ai-target-exclude], [data-ai-context=\"exclude\"]";
|
|
30
|
+
const DEFAULT_SELECTION_TOOLBAR_SELECTOR = "[data-ai-selection-toolbar]";
|
|
31
|
+
const INTERACTIVE_SELECTOR = "button, a, input, textarea, select, option, [contenteditable=\"true\"]";
|
|
32
|
+
function resolveElement(source, fallback) {
|
|
33
|
+
if (typeof source === "function") return source();
|
|
34
|
+
if (source) return source;
|
|
35
|
+
return fallback ?? null;
|
|
36
|
+
}
|
|
37
|
+
function getTargetMeta(element, fallbackId, fallbackLabel) {
|
|
38
|
+
if (!element) return {
|
|
39
|
+
targetId: fallbackId,
|
|
40
|
+
label: fallbackLabel
|
|
41
|
+
};
|
|
42
|
+
const targetId = element.getAttribute("data-ai-target") || element.getAttribute("data-ai-image") || element.id || fallbackId;
|
|
43
|
+
return {
|
|
44
|
+
targetId,
|
|
45
|
+
label: element.getAttribute("data-ai-label") || element.getAttribute("aria-label") || targetId
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function extractVisibleText(element) {
|
|
49
|
+
const clone = element.cloneNode(true);
|
|
50
|
+
clone.querySelectorAll("script, style, [aria-hidden=\"true\"], [data-ai-target-exclude], [data-ai-context=\"exclude\"]").forEach((node) => node.remove());
|
|
51
|
+
return (clone.textContent ?? "").replace(/\s+/g, " ").trim();
|
|
52
|
+
}
|
|
53
|
+
function clamp(value, min, max) {
|
|
54
|
+
return Math.min(Math.max(value, min), max);
|
|
55
|
+
}
|
|
56
|
+
/** Converts a resolved targeting hit into a validated ChatContextItem for useChatContext. */
|
|
57
|
+
function toChatContextItem(hit, options) {
|
|
58
|
+
const metadata = {
|
|
59
|
+
targetId: hit.targetId,
|
|
60
|
+
targetKind: hit.kind,
|
|
61
|
+
...hit.rect ? { rect: [...hit.rect] } : {},
|
|
62
|
+
...hit.comment ? { comment: hit.comment } : {},
|
|
63
|
+
...options?.metadata ?? {}
|
|
64
|
+
};
|
|
65
|
+
const suffix = hit.kind === "region" && hit.rect ? `${Math.round(hit.rect[0] * 100)}-${Math.round(hit.rect[1] * 100)}-${Math.round(hit.rect[2] * 100)}-${Math.round(hit.rect[3] * 100)}` : hit.kind === "text" && hit.text ? `${hit.text.length}:${hit.text.slice(0, 24)}` : "full";
|
|
66
|
+
return {
|
|
67
|
+
id: options?.id ?? `${hit.kind}:${hit.targetId}:${suffix}`,
|
|
68
|
+
kind: hit.kind,
|
|
69
|
+
label: hit.label,
|
|
70
|
+
...hit.text !== void 0 ? { text: hit.text } : {},
|
|
71
|
+
metadata
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function useChatTargeting(options) {
|
|
75
|
+
const optionsRef = useRef(options);
|
|
76
|
+
optionsRef.current = options;
|
|
77
|
+
const enabled = options.enabled ?? true;
|
|
78
|
+
const [pending, setPending] = useState(null);
|
|
79
|
+
const [marquee, setMarquee] = useState(null);
|
|
80
|
+
const [commentExpanded, setCommentExpanded] = useState(false);
|
|
81
|
+
const [targetMode, setTargetMode] = useState(false);
|
|
82
|
+
const pendingRef = useRef(pending);
|
|
83
|
+
pendingRef.current = pending;
|
|
84
|
+
const targetModeRef = useRef(targetMode);
|
|
85
|
+
targetModeRef.current = targetMode;
|
|
86
|
+
const spaceHeldRef = useRef(false);
|
|
87
|
+
const selectedElRef = useRef(null);
|
|
88
|
+
const dragRef = useRef(null);
|
|
89
|
+
const clearSelectedHighlight = useCallback(() => {
|
|
90
|
+
if (selectedElRef.current) {
|
|
91
|
+
selectedElRef.current.removeAttribute("data-ai-target-selected");
|
|
92
|
+
selectedElRef.current = null;
|
|
93
|
+
}
|
|
94
|
+
}, []);
|
|
95
|
+
const dismiss = useCallback(() => {
|
|
96
|
+
clearSelectedHighlight();
|
|
97
|
+
setPending(null);
|
|
98
|
+
setMarquee(null);
|
|
99
|
+
setCommentExpanded(false);
|
|
100
|
+
}, [clearSelectedHighlight]);
|
|
101
|
+
const focusComposer = useCallback(() => {
|
|
102
|
+
const target = resolveElement(optionsRef.current.focusTarget, null);
|
|
103
|
+
if (target && typeof target.focus === "function") target.focus();
|
|
104
|
+
}, []);
|
|
105
|
+
const commit = useCallback(async (comment) => {
|
|
106
|
+
const current = pendingRef.current;
|
|
107
|
+
if (!current) return;
|
|
108
|
+
const trimmed = comment?.trim();
|
|
109
|
+
const hit = {
|
|
110
|
+
...current.hit,
|
|
111
|
+
...trimmed ? { comment: trimmed } : {}
|
|
112
|
+
};
|
|
113
|
+
dismiss();
|
|
114
|
+
if (typeof window !== "undefined") window.getSelection()?.removeAllRanges();
|
|
115
|
+
await optionsRef.current.onAdd(hit);
|
|
116
|
+
focusComposer();
|
|
117
|
+
}, [dismiss, focusComposer]);
|
|
118
|
+
useEffect(() => {
|
|
119
|
+
if (!enabled) {
|
|
120
|
+
dismiss();
|
|
121
|
+
setTargetMode(false);
|
|
122
|
+
}
|
|
123
|
+
}, [enabled, dismiss]);
|
|
124
|
+
useEffect(() => () => clearSelectedHighlight(), [clearSelectedHighlight]);
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
if (!enabled || typeof document === "undefined") return;
|
|
127
|
+
const getRoot = () => resolveElement(optionsRef.current.root, document.body);
|
|
128
|
+
const getSelectors = () => ({
|
|
129
|
+
target: optionsRef.current.targetSelector ?? DEFAULT_TARGET_SELECTOR,
|
|
130
|
+
image: optionsRef.current.imageSelector ?? DEFAULT_IMAGE_SELECTOR,
|
|
131
|
+
exclude: optionsRef.current.excludeSelector ?? DEFAULT_EXCLUDE_SELECTOR,
|
|
132
|
+
selectionToolbar: optionsRef.current.selectionToolbarSelector ?? DEFAULT_SELECTION_TOOLBAR_SELECTOR,
|
|
133
|
+
minDrag: optionsRef.current.minDragPixels ?? 12
|
|
134
|
+
});
|
|
135
|
+
const isExcluded = (node, excludeSelector) => {
|
|
136
|
+
if (!node) return true;
|
|
137
|
+
const el = node.nodeType === 1 ? node : node.parentElement;
|
|
138
|
+
if (!el) return true;
|
|
139
|
+
return Boolean(el.closest(excludeSelector) || el.closest("[data-ai-targeting-ui]"));
|
|
140
|
+
};
|
|
141
|
+
const resolveSemanticTarget = (el, event, targetSelector) => {
|
|
142
|
+
if (optionsRef.current.resolveTarget) return optionsRef.current.resolveTarget(el, event);
|
|
143
|
+
return el.closest(targetSelector);
|
|
144
|
+
};
|
|
145
|
+
let mouseUpTimer = null;
|
|
146
|
+
const onMouseUp = (event) => {
|
|
147
|
+
if (event.button !== 0 || event.detail >= 2) return;
|
|
148
|
+
if (targetModeRef.current && !spaceHeldRef.current) return;
|
|
149
|
+
if (dragRef.current) return;
|
|
150
|
+
const root = getRoot();
|
|
151
|
+
if (!root) return;
|
|
152
|
+
const selectors = getSelectors();
|
|
153
|
+
const targetNode = event.target instanceof Node ? event.target : null;
|
|
154
|
+
if (isExcluded(targetNode, selectors.exclude)) return;
|
|
155
|
+
const releasePoint = {
|
|
156
|
+
x: event.clientX,
|
|
157
|
+
y: event.clientY
|
|
158
|
+
};
|
|
159
|
+
if (mouseUpTimer) clearTimeout(mouseUpTimer);
|
|
160
|
+
mouseUpTimer = setTimeout(() => {
|
|
161
|
+
const selection = root.ownerDocument.getSelection();
|
|
162
|
+
if (!selection || selection.isCollapsed || !selection.rangeCount) {
|
|
163
|
+
if (pendingRef.current?.presentation === "pill") dismiss();
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const text = selection.toString().trim();
|
|
167
|
+
if (!text) return;
|
|
168
|
+
const range = selection.getRangeAt(0);
|
|
169
|
+
if (!root.contains(range.startContainer) || !root.contains(range.endContainer)) return;
|
|
170
|
+
const common = range.commonAncestorContainer.nodeType === 1 ? range.commonAncestorContainer : range.commonAncestorContainer.parentElement;
|
|
171
|
+
if (!common || isExcluded(common, selectors.exclude)) return;
|
|
172
|
+
if (common.closest(selectors.selectionToolbar)) return;
|
|
173
|
+
if (common.closest("input, textarea, select")) return;
|
|
174
|
+
if (optionsRef.current.shouldHandleSelection && !optionsRef.current.shouldHandleSelection(selection, range, event)) return;
|
|
175
|
+
const semantic = resolveSemanticTarget(common, event, selectors.target);
|
|
176
|
+
const { targetId, label } = getTargetMeta(semantic, "selection", text.slice(0, 40));
|
|
177
|
+
clearSelectedHighlight();
|
|
178
|
+
setMarquee(null);
|
|
179
|
+
setCommentExpanded(false);
|
|
180
|
+
setPending({
|
|
181
|
+
hit: {
|
|
182
|
+
kind: "text",
|
|
183
|
+
targetId,
|
|
184
|
+
label,
|
|
185
|
+
text,
|
|
186
|
+
element: semantic ?? common
|
|
187
|
+
},
|
|
188
|
+
presentation: "pill",
|
|
189
|
+
anchor: releasePoint
|
|
190
|
+
});
|
|
191
|
+
}, 0);
|
|
192
|
+
};
|
|
193
|
+
const onDoubleClick = (event) => {
|
|
194
|
+
if (spaceHeldRef.current) return;
|
|
195
|
+
const root = getRoot();
|
|
196
|
+
if (!root || !(event.target instanceof Element) || !root.contains(event.target)) return;
|
|
197
|
+
const selectors = getSelectors();
|
|
198
|
+
if (isExcluded(event.target, selectors.exclude)) return;
|
|
199
|
+
if (event.target.closest(INTERACTIVE_SELECTOR)) return;
|
|
200
|
+
const semantic = resolveSemanticTarget(event.target, event, selectors.target);
|
|
201
|
+
if (!semantic || !root.contains(semantic)) return;
|
|
202
|
+
if (optionsRef.current.shouldHandleDoubleClick && !optionsRef.current.shouldHandleDoubleClick(semantic, event)) return;
|
|
203
|
+
event.preventDefault();
|
|
204
|
+
root.ownerDocument.getSelection()?.removeAllRanges();
|
|
205
|
+
clearSelectedHighlight();
|
|
206
|
+
semantic.setAttribute("data-ai-target-selected", "true");
|
|
207
|
+
selectedElRef.current = semantic;
|
|
208
|
+
const { targetId, label } = getTargetMeta(semantic, "section", "Section");
|
|
209
|
+
setMarquee(null);
|
|
210
|
+
setCommentExpanded(false);
|
|
211
|
+
setPending({
|
|
212
|
+
hit: {
|
|
213
|
+
kind: "section",
|
|
214
|
+
targetId,
|
|
215
|
+
label,
|
|
216
|
+
text: extractVisibleText(semantic),
|
|
217
|
+
element: semantic
|
|
218
|
+
},
|
|
219
|
+
presentation: "popover",
|
|
220
|
+
anchor: {
|
|
221
|
+
x: event.clientX,
|
|
222
|
+
y: event.clientY
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
};
|
|
226
|
+
const onClickCapture = (event) => {
|
|
227
|
+
if (!targetModeRef.current || spaceHeldRef.current) return;
|
|
228
|
+
const root = getRoot();
|
|
229
|
+
if (!root || !(event.target instanceof Element) || !root.contains(event.target)) return;
|
|
230
|
+
const selectors = getSelectors();
|
|
231
|
+
if (isExcluded(event.target, selectors.exclude)) return;
|
|
232
|
+
const semantic = resolveSemanticTarget(event.target, event, selectors.target);
|
|
233
|
+
if (!semantic || !root.contains(semantic)) return;
|
|
234
|
+
event.preventDefault();
|
|
235
|
+
event.stopPropagation();
|
|
236
|
+
clearSelectedHighlight();
|
|
237
|
+
semantic.setAttribute("data-ai-target-selected", "true");
|
|
238
|
+
selectedElRef.current = semantic;
|
|
239
|
+
const { targetId, label } = getTargetMeta(semantic, "section", "Section");
|
|
240
|
+
setMarquee(null);
|
|
241
|
+
setCommentExpanded(false);
|
|
242
|
+
setPending({
|
|
243
|
+
hit: {
|
|
244
|
+
kind: "section",
|
|
245
|
+
targetId,
|
|
246
|
+
label,
|
|
247
|
+
text: extractVisibleText(semantic),
|
|
248
|
+
element: semantic
|
|
249
|
+
},
|
|
250
|
+
presentation: "popover",
|
|
251
|
+
anchor: {
|
|
252
|
+
x: event.clientX,
|
|
253
|
+
y: event.clientY
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
};
|
|
257
|
+
const onPointerDown = (event) => {
|
|
258
|
+
if (event.button !== 0 || spaceHeldRef.current) return;
|
|
259
|
+
const root = getRoot();
|
|
260
|
+
if (!root || !(event.target instanceof Element) || !root.contains(event.target)) return;
|
|
261
|
+
const selectors = getSelectors();
|
|
262
|
+
if (isExcluded(event.target, selectors.exclude)) return;
|
|
263
|
+
const imageEl = event.target.closest(selectors.image);
|
|
264
|
+
if (!imageEl || !root.contains(imageEl)) return;
|
|
265
|
+
if (optionsRef.current.canDragRegion && !optionsRef.current.canDragRegion(imageEl, event)) return;
|
|
266
|
+
const bounds = imageEl.getBoundingClientRect();
|
|
267
|
+
if (bounds.width <= 0 || bounds.height <= 0) return;
|
|
268
|
+
event.preventDefault();
|
|
269
|
+
dismiss();
|
|
270
|
+
dragRef.current = {
|
|
271
|
+
element: imageEl,
|
|
272
|
+
bounds,
|
|
273
|
+
startX: clamp(event.clientX - bounds.left, 0, bounds.width),
|
|
274
|
+
startY: clamp(event.clientY - bounds.top, 0, bounds.height),
|
|
275
|
+
curX: clamp(event.clientX - bounds.left, 0, bounds.width),
|
|
276
|
+
curY: clamp(event.clientY - bounds.top, 0, bounds.height)
|
|
277
|
+
};
|
|
278
|
+
};
|
|
279
|
+
const onPointerMove = (event) => {
|
|
280
|
+
const drag = dragRef.current;
|
|
281
|
+
if (!drag) return;
|
|
282
|
+
drag.curX = clamp(event.clientX - drag.bounds.left, 0, drag.bounds.width);
|
|
283
|
+
drag.curY = clamp(event.clientY - drag.bounds.top, 0, drag.bounds.height);
|
|
284
|
+
const x = Math.min(drag.startX, drag.curX);
|
|
285
|
+
const y = Math.min(drag.startY, drag.curY);
|
|
286
|
+
const w = Math.abs(drag.curX - drag.startX);
|
|
287
|
+
const h = Math.abs(drag.curY - drag.startY);
|
|
288
|
+
setMarquee({
|
|
289
|
+
element: drag.element,
|
|
290
|
+
rect: [
|
|
291
|
+
x / drag.bounds.width,
|
|
292
|
+
y / drag.bounds.height,
|
|
293
|
+
w / drag.bounds.width,
|
|
294
|
+
h / drag.bounds.height
|
|
295
|
+
],
|
|
296
|
+
box: {
|
|
297
|
+
left: drag.bounds.left + x,
|
|
298
|
+
top: drag.bounds.top + y,
|
|
299
|
+
width: w,
|
|
300
|
+
height: h
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
};
|
|
304
|
+
const onPointerUp = () => {
|
|
305
|
+
const drag = dragRef.current;
|
|
306
|
+
if (!drag) return;
|
|
307
|
+
dragRef.current = null;
|
|
308
|
+
const x = Math.min(drag.startX, drag.curX);
|
|
309
|
+
const y = Math.min(drag.startY, drag.curY);
|
|
310
|
+
const w = Math.abs(drag.curX - drag.startX);
|
|
311
|
+
const h = Math.abs(drag.curY - drag.startY);
|
|
312
|
+
const selectors = getSelectors();
|
|
313
|
+
if (w < selectors.minDrag || h < selectors.minDrag) {
|
|
314
|
+
setMarquee(null);
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
const rect = [
|
|
318
|
+
x / drag.bounds.width,
|
|
319
|
+
y / drag.bounds.height,
|
|
320
|
+
w / drag.bounds.width,
|
|
321
|
+
h / drag.bounds.height
|
|
322
|
+
];
|
|
323
|
+
const { targetId, label } = getTargetMeta(drag.element, "image", "Image");
|
|
324
|
+
setMarquee({
|
|
325
|
+
element: drag.element,
|
|
326
|
+
rect,
|
|
327
|
+
box: {
|
|
328
|
+
left: drag.bounds.left + x,
|
|
329
|
+
top: drag.bounds.top + y,
|
|
330
|
+
width: w,
|
|
331
|
+
height: h
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
setCommentExpanded(false);
|
|
335
|
+
setPending({
|
|
336
|
+
hit: {
|
|
337
|
+
kind: "region",
|
|
338
|
+
targetId,
|
|
339
|
+
label,
|
|
340
|
+
rect,
|
|
341
|
+
element: drag.element
|
|
342
|
+
},
|
|
343
|
+
presentation: "popover",
|
|
344
|
+
anchor: {
|
|
345
|
+
x: drag.bounds.left + x + w / 2,
|
|
346
|
+
y: drag.bounds.top + y + h
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
};
|
|
350
|
+
const onMouseDown = (event) => {
|
|
351
|
+
if (!pendingRef.current) return;
|
|
352
|
+
if (event.target instanceof Element && event.target.closest("[data-ai-targeting-ui]")) return;
|
|
353
|
+
dismiss();
|
|
354
|
+
};
|
|
355
|
+
const onKeyDown = (event) => {
|
|
356
|
+
if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "l") {
|
|
357
|
+
if (pendingRef.current?.hit.kind === "text") {
|
|
358
|
+
event.preventDefault();
|
|
359
|
+
commit();
|
|
360
|
+
}
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
if (event.key === "Escape") {
|
|
364
|
+
if (pendingRef.current) {
|
|
365
|
+
event.preventDefault();
|
|
366
|
+
dismiss();
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
if (targetModeRef.current) {
|
|
370
|
+
event.preventDefault();
|
|
371
|
+
setTargetMode(false);
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
const focusTarget = resolveElement(optionsRef.current.focusTarget, null);
|
|
375
|
+
if (focusTarget && document.activeElement === focusTarget) focusTarget.blur();
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
if (event.code === "Space" && targetModeRef.current) {
|
|
379
|
+
const activeTag = document.activeElement?.tagName;
|
|
380
|
+
if (activeTag !== "INPUT" && activeTag !== "TEXTAREA") spaceHeldRef.current = true;
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
const onKeyUp = (event) => {
|
|
384
|
+
if (event.code === "Space") spaceHeldRef.current = false;
|
|
385
|
+
};
|
|
386
|
+
document.addEventListener("mouseup", onMouseUp);
|
|
387
|
+
document.addEventListener("dblclick", onDoubleClick);
|
|
388
|
+
document.addEventListener("click", onClickCapture, true);
|
|
389
|
+
document.addEventListener("pointerdown", onPointerDown, true);
|
|
390
|
+
window.addEventListener("pointermove", onPointerMove);
|
|
391
|
+
window.addEventListener("pointerup", onPointerUp);
|
|
392
|
+
document.addEventListener("mousedown", onMouseDown);
|
|
393
|
+
document.addEventListener("keydown", onKeyDown);
|
|
394
|
+
document.addEventListener("keyup", onKeyUp);
|
|
395
|
+
return () => {
|
|
396
|
+
if (mouseUpTimer) clearTimeout(mouseUpTimer);
|
|
397
|
+
document.removeEventListener("mouseup", onMouseUp);
|
|
398
|
+
document.removeEventListener("dblclick", onDoubleClick);
|
|
399
|
+
document.removeEventListener("click", onClickCapture, true);
|
|
400
|
+
document.removeEventListener("pointerdown", onPointerDown, true);
|
|
401
|
+
window.removeEventListener("pointermove", onPointerMove);
|
|
402
|
+
window.removeEventListener("pointerup", onPointerUp);
|
|
403
|
+
document.removeEventListener("mousedown", onMouseDown);
|
|
404
|
+
document.removeEventListener("keydown", onKeyDown);
|
|
405
|
+
document.removeEventListener("keyup", onKeyUp);
|
|
406
|
+
};
|
|
407
|
+
}, [
|
|
408
|
+
enabled,
|
|
409
|
+
clearSelectedHighlight,
|
|
410
|
+
commit,
|
|
411
|
+
dismiss
|
|
412
|
+
]);
|
|
413
|
+
return {
|
|
414
|
+
pending,
|
|
415
|
+
marquee,
|
|
416
|
+
commentExpanded,
|
|
417
|
+
setCommentExpanded,
|
|
418
|
+
targetMode,
|
|
419
|
+
setTargetMode,
|
|
420
|
+
commit,
|
|
421
|
+
dismiss
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
/** Default floating UI for useChatTargeting: centered "+ Add to AI" pill, inline comment popover, and marquee box. */
|
|
425
|
+
function ChatTargetingOverlay({ targeting, labels: overrides, theme = "auto", className = "" }) {
|
|
426
|
+
const labels = {
|
|
427
|
+
...zhCNTargetingLabels,
|
|
428
|
+
...overrides
|
|
429
|
+
};
|
|
430
|
+
const [comment, setComment] = useState("");
|
|
431
|
+
const inputRef = useRef(null);
|
|
432
|
+
const surfaceRef = useRef(null);
|
|
433
|
+
const [coords, setCoords] = useState(null);
|
|
434
|
+
const { pending, marquee, commentExpanded, setCommentExpanded, commit, dismiss } = targeting;
|
|
435
|
+
useEffect(() => {
|
|
436
|
+
setComment("");
|
|
437
|
+
}, [pending]);
|
|
438
|
+
useEffect(() => {
|
|
439
|
+
if (pending && (pending.presentation === "popover" || commentExpanded)) inputRef.current?.focus();
|
|
440
|
+
}, [pending, commentExpanded]);
|
|
441
|
+
useEffect(() => {
|
|
442
|
+
if (!pending || typeof window === "undefined") {
|
|
443
|
+
setCoords(null);
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
const el = surfaceRef.current;
|
|
447
|
+
const width = el?.getBoundingClientRect().width || (pending.presentation === "popover" || commentExpanded ? 260 : 120);
|
|
448
|
+
const height = el?.getBoundingClientRect().height || (pending.presentation === "popover" ? 110 : 36);
|
|
449
|
+
const margin = 10;
|
|
450
|
+
const left = clamp(pending.anchor.x - width / 2, margin, Math.max(margin, window.innerWidth - width - margin));
|
|
451
|
+
const top = pending.anchor.y + 12 + height + margin <= window.innerHeight ? pending.anchor.y + 12 : Math.max(pending.anchor.y - height - 12, margin);
|
|
452
|
+
setCoords({
|
|
453
|
+
left,
|
|
454
|
+
top
|
|
455
|
+
});
|
|
456
|
+
}, [pending, commentExpanded]);
|
|
457
|
+
if (typeof document === "undefined" || !pending && !marquee) return null;
|
|
458
|
+
const title = pending ? pending.hit.kind === "region" ? `${pending.hit.label} ${labels.regionSuffix}` : pending.hit.kind === "section" ? `${pending.hit.label} ${labels.sectionSuffix}` : pending.hit.label : "";
|
|
459
|
+
const content = /* @__PURE__ */ jsxs("div", {
|
|
460
|
+
className: `gc-chat-targeting ${className}`,
|
|
461
|
+
"data-theme": theme,
|
|
462
|
+
"data-ai-targeting-ui": "true",
|
|
463
|
+
children: [
|
|
464
|
+
marquee && /* @__PURE__ */ jsx("div", {
|
|
465
|
+
className: "gc-chat-targeting__marquee",
|
|
466
|
+
style: {
|
|
467
|
+
left: `${marquee.box.left}px`,
|
|
468
|
+
top: `${marquee.box.top}px`,
|
|
469
|
+
width: `${marquee.box.width}px`,
|
|
470
|
+
height: `${marquee.box.height}px`
|
|
471
|
+
},
|
|
472
|
+
"aria-hidden": "true"
|
|
473
|
+
}),
|
|
474
|
+
pending && pending.presentation === "pill" && /* @__PURE__ */ jsx("div", {
|
|
475
|
+
ref: surfaceRef,
|
|
476
|
+
className: "gc-chat-targeting__pill",
|
|
477
|
+
role: "dialog",
|
|
478
|
+
"aria-label": labels.addToAi,
|
|
479
|
+
style: coords ? {
|
|
480
|
+
left: `${coords.left}px`,
|
|
481
|
+
top: `${coords.top}px`
|
|
482
|
+
} : {
|
|
483
|
+
left: `${pending.anchor.x}px`,
|
|
484
|
+
top: `${pending.anchor.y + 12}px`,
|
|
485
|
+
visibility: "hidden"
|
|
486
|
+
},
|
|
487
|
+
children: !commentExpanded ? /* @__PURE__ */ jsxs("button", {
|
|
488
|
+
type: "button",
|
|
489
|
+
className: "gc-chat-targeting__pill-btn",
|
|
490
|
+
onClick: () => setCommentExpanded(true),
|
|
491
|
+
children: [
|
|
492
|
+
/* @__PURE__ */ jsx("span", {
|
|
493
|
+
className: "gc-chat-targeting__plus",
|
|
494
|
+
"aria-hidden": "true",
|
|
495
|
+
children: "+"
|
|
496
|
+
}),
|
|
497
|
+
/* @__PURE__ */ jsx("span", { children: labels.addToAi }),
|
|
498
|
+
labels.shortcutHint ? /* @__PURE__ */ jsx("kbd", {
|
|
499
|
+
className: "gc-chat-targeting__kbd",
|
|
500
|
+
children: labels.shortcutHint
|
|
501
|
+
}) : null
|
|
502
|
+
]
|
|
503
|
+
}) : /* @__PURE__ */ jsxs("form", {
|
|
504
|
+
className: "gc-chat-targeting__inline-form",
|
|
505
|
+
onSubmit: (event) => {
|
|
506
|
+
event.preventDefault();
|
|
507
|
+
commit(comment);
|
|
508
|
+
},
|
|
509
|
+
children: [/* @__PURE__ */ jsx("input", {
|
|
510
|
+
ref: inputRef,
|
|
511
|
+
type: "text",
|
|
512
|
+
className: "gc-chat-targeting__input",
|
|
513
|
+
value: comment,
|
|
514
|
+
onChange: (event) => setComment(event.target.value),
|
|
515
|
+
placeholder: labels.commentPlaceholder,
|
|
516
|
+
"aria-label": labels.commentPlaceholder
|
|
517
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
518
|
+
type: "submit",
|
|
519
|
+
className: "gc-chat-targeting__submit",
|
|
520
|
+
children: labels.add
|
|
521
|
+
})]
|
|
522
|
+
})
|
|
523
|
+
}),
|
|
524
|
+
pending && pending.presentation === "popover" && /* @__PURE__ */ jsxs("div", {
|
|
525
|
+
ref: surfaceRef,
|
|
526
|
+
className: "gc-chat-targeting__popover",
|
|
527
|
+
role: "dialog",
|
|
528
|
+
"aria-label": title,
|
|
529
|
+
style: coords ? {
|
|
530
|
+
left: `${coords.left}px`,
|
|
531
|
+
top: `${coords.top}px`
|
|
532
|
+
} : {
|
|
533
|
+
left: `${pending.anchor.x}px`,
|
|
534
|
+
top: `${pending.anchor.y + 12}px`,
|
|
535
|
+
visibility: "hidden"
|
|
536
|
+
},
|
|
537
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
538
|
+
className: "gc-chat-targeting__popover-header",
|
|
539
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
540
|
+
className: "gc-chat-targeting__popover-title",
|
|
541
|
+
children: title
|
|
542
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
543
|
+
type: "button",
|
|
544
|
+
className: "gc-chat-targeting__close",
|
|
545
|
+
"aria-label": labels.cancel,
|
|
546
|
+
onClick: dismiss,
|
|
547
|
+
children: "×"
|
|
548
|
+
})]
|
|
549
|
+
}), /* @__PURE__ */ jsxs("form", {
|
|
550
|
+
className: "gc-chat-targeting__popover-form",
|
|
551
|
+
onSubmit: (event) => {
|
|
552
|
+
event.preventDefault();
|
|
553
|
+
commit(comment);
|
|
554
|
+
},
|
|
555
|
+
children: [/* @__PURE__ */ jsx("input", {
|
|
556
|
+
ref: inputRef,
|
|
557
|
+
type: "text",
|
|
558
|
+
className: "gc-chat-targeting__input",
|
|
559
|
+
value: comment,
|
|
560
|
+
onChange: (event) => setComment(event.target.value),
|
|
561
|
+
placeholder: labels.commentPlaceholder,
|
|
562
|
+
"aria-label": labels.commentPlaceholder
|
|
563
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
564
|
+
className: "gc-chat-targeting__popover-actions",
|
|
565
|
+
children: [/* @__PURE__ */ jsx("button", {
|
|
566
|
+
type: "button",
|
|
567
|
+
className: "gc-chat-targeting__secondary",
|
|
568
|
+
onClick: () => void commit(),
|
|
569
|
+
children: labels.addWithoutNote
|
|
570
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
571
|
+
type: "submit",
|
|
572
|
+
className: "gc-chat-targeting__submit",
|
|
573
|
+
children: labels.addToAi
|
|
574
|
+
})]
|
|
575
|
+
})]
|
|
576
|
+
})]
|
|
577
|
+
})
|
|
578
|
+
]
|
|
579
|
+
});
|
|
580
|
+
return createPortal(content, document.body);
|
|
581
|
+
}
|
|
582
|
+
//#endregion
|
|
583
|
+
export { ChatTargetingOverlay, enUSTargetingLabels, toChatContextItem, useChatTargeting, zhCNTargetingLabels };
|
|
584
|
+
|
|
585
|
+
//# sourceMappingURL=targeting.js.map
|