dsh-selection-annotations 0.0.0-stage → 2.1.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 +166 -0
- package/LICENSE +21 -0
- package/README.md +161 -3
- package/README.zh-CN.md +161 -0
- package/client.js +11136 -0
- package/cordis.patch.yml +3 -0
- package/icon.svg +7 -0
- package/lib/annotations-db.js +1157 -0
- package/lib/index.js +1304 -0
- package/package.json +74 -6
- package/scripts/build.mjs +37 -0
- package/scripts/check-build.mjs +10 -0
- package/src/core/annotation-clipboard.js +93 -0
- package/src/core/annotation-utils.js +27 -0
- package/src/core/cards.js +502 -0
- package/src/core/config.js +91 -0
- package/src/core/constants.js +34 -0
- package/src/core/draft-codec.js +77 -0
- package/src/core/draft-store.js +85 -0
- package/src/core/hidden-payload.js +73 -0
- package/src/core/image-codec.js +111 -0
- package/src/core/jump-icon.js +25 -0
- package/src/core/locale.js +59 -0
- package/src/core/log.js +19 -0
- package/src/core/markdown.js +80 -0
- package/src/core/messages.js +311 -0
- package/src/core/note-tone.js +77 -0
- package/src/core/panel-render.js +315 -0
- package/src/core/payload.js +191 -0
- package/src/core/preview-bridge.js +216 -0
- package/src/core/remote-sync.js +206 -0
- package/src/core/scroll-trace.js +177 -0
- package/src/core/sent-store.js +148 -0
- package/src/core/session.js +60 -0
- package/src/core/settings.js +168 -0
- package/src/core/side-tab-state.js +30 -0
- package/src/core/source-capture.js +530 -0
- package/src/core/source-locate.js +220 -0
- package/src/core/source-resolve.js +89 -0
- package/src/core/styles.js +590 -0
- package/src/core/turn-anchor.js +54 -0
- package/src/core/util.js +55 -0
- package/src/core/uuid.js +23 -0
- package/src/core/web-picker.js +426 -0
- package/src/entry.js +22 -0
- package/src/plugin.js +8706 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 在每轮对话的尾部插一个空的锚点元素。
|
|
3
|
+
*
|
|
4
|
+
* DSH 每轮末尾的「已完成 / 本轮费用」来自 `conversation.chat.turnTail` 槽位,
|
|
5
|
+
* 槽位组件能直接拿到 `sessionId` 与 `turn`。我们在这里放一个带 data 属性的空 div,
|
|
6
|
+
* 卡片按 `[data-sa-turn][data-sa-session]` 精确落位,不必再扫描整个页面猜是哪一轮。
|
|
7
|
+
*/
|
|
8
|
+
export const TURN_ANCHOR_SLOT = 'conversation.chat.turnTail'
|
|
9
|
+
|
|
10
|
+
export function registerTurnAnchor(ctx, h) {
|
|
11
|
+
const slots = ctx.slots
|
|
12
|
+
if (slots === undefined || slots === null) return
|
|
13
|
+
if (typeof slots.inject !== 'function' || typeof slots.register !== 'function') return
|
|
14
|
+
|
|
15
|
+
const TurnAnchor = (props) => {
|
|
16
|
+
// 0.2.2 的 turnTail 槽位传的是 turn 对象;旧版直接传数字。两种都认。
|
|
17
|
+
const rawTurn = typeof props?.turn === 'object' && props?.turn !== null ? props.turn.turn : props?.turn
|
|
18
|
+
const turn = Number(rawTurn)
|
|
19
|
+
if (!Number.isFinite(turn) || turn <= 0) return null
|
|
20
|
+
const fromProps = typeof props?.sessionId === 'string' ? props.sessionId : ''
|
|
21
|
+
const fromTurn = typeof props?.turn?.sessionId === 'string' ? props.turn.sessionId : ''
|
|
22
|
+
const sessionId = fromProps !== '' ? fromProps : fromTurn
|
|
23
|
+
return h('div', {
|
|
24
|
+
className: 'sa-turn-anchor',
|
|
25
|
+
'data-sa-turn': String(turn),
|
|
26
|
+
'data-sa-session': sessionId,
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
TurnAnchor.displayName = 'SelectionAnnotationsTurnAnchor'
|
|
30
|
+
|
|
31
|
+
const register = () =>
|
|
32
|
+
slots.register(
|
|
33
|
+
{
|
|
34
|
+
name: TURN_ANCHOR_SLOT,
|
|
35
|
+
id: 'selection-annotations-turn-anchor',
|
|
36
|
+
order: 20,
|
|
37
|
+
},
|
|
38
|
+
TurnAnchor,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
// 槽位服务自己管生命周期;这里包一层 effect,确保插件卸载时一起撤掉。
|
|
42
|
+
ctx.effect(() => slots.inject(TURN_ANCHOR_SLOT, register), 'selection-annotations: turn anchors')
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 找到某一轮、某个会话的锚点元素;找不到就返回 null,让调用方退回旧逻辑。*/
|
|
46
|
+
export function findTurnAnchor(turn, sessionId) {
|
|
47
|
+
if (!Number.isFinite(turn) || turn <= 0) return null
|
|
48
|
+
const escaped = typeof sessionId === 'string' && sessionId !== '' ? sessionId.replace(/["\\]/g, '\\$&') : ''
|
|
49
|
+
const selector =
|
|
50
|
+
escaped === ''
|
|
51
|
+
? `[data-sa-turn="${turn}"]`
|
|
52
|
+
: `[data-sa-turn="${turn}"][data-sa-session="${escaped}"]`
|
|
53
|
+
return document.querySelector(selector)
|
|
54
|
+
}
|
package/src/core/util.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** 不在主流程里抛错的调用。onError 用于把异常交回调用方记录,避免「吞掉」。*/
|
|
2
|
+
export const safe = (fn, onError) => {
|
|
3
|
+
try {
|
|
4
|
+
return fn()
|
|
5
|
+
} catch (error) {
|
|
6
|
+
if (typeof onError === 'function') {
|
|
7
|
+
try {
|
|
8
|
+
onError(error)
|
|
9
|
+
} catch {
|
|
10
|
+
// 记录失败不能再制造新的异常。
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return undefined
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function toBase64(bytes) {
|
|
18
|
+
let binary = ''
|
|
19
|
+
const step = 0x8000
|
|
20
|
+
for (let i = 0; i < bytes.length; i += step) {
|
|
21
|
+
binary += String.fromCharCode.apply(null, bytes.subarray(i, i + step))
|
|
22
|
+
}
|
|
23
|
+
return btoa(binary)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function cssEscape(value) {
|
|
27
|
+
return String(value).replace(/["\\]/g, '\\$&')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function truncateQuote(value) {
|
|
31
|
+
const text = typeof value === 'string' ? value.replace(/\s+/g, ' ').trim() : ''
|
|
32
|
+
return text.length > 140 ? `${text.slice(0, 140)}…` : text
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function el(tag, className, text) {
|
|
36
|
+
const created = document.createElement(tag)
|
|
37
|
+
if (className !== undefined) created.className = className
|
|
38
|
+
if (text !== undefined) created.textContent = text
|
|
39
|
+
return created
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function button(className, text, onClick) {
|
|
43
|
+
const created = el('button', className, text)
|
|
44
|
+
created.type = 'button'
|
|
45
|
+
created.addEventListener('click', onClick)
|
|
46
|
+
return created
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 稳定的内容指纹:用于判断卡片是否需要按最新数据重建,替代手写的版本号。 */
|
|
50
|
+
export function hashString(input) {
|
|
51
|
+
const text = typeof input === 'string' ? input : JSON.stringify(input ?? null)
|
|
52
|
+
let hash = 5381
|
|
53
|
+
for (let i = 0; i < text.length; i += 1) hash = ((hash << 5) + hash + text.charCodeAt(i)) | 0
|
|
54
|
+
return (hash >>> 0).toString(36)
|
|
55
|
+
}
|
package/src/core/uuid.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 批注条目 uuid:捕获那一刻就生成,随条目带进数据块、批次创建与答复。
|
|
3
|
+
* 优先用浏览器原生实现;不可用时(非安全上下文等)回退到时间戳 + 随机数,
|
|
4
|
+
* 形状仍符合 UUID v4,宿主与数据库都只把它当不透明主键。
|
|
5
|
+
*/
|
|
6
|
+
export function uuidV4() {
|
|
7
|
+
try {
|
|
8
|
+
const native = globalThis.crypto?.randomUUID
|
|
9
|
+
if (typeof native === 'function') {
|
|
10
|
+
const value = native.call(globalThis.crypto)
|
|
11
|
+
if (typeof value === 'string' && value !== '') return value
|
|
12
|
+
}
|
|
13
|
+
} catch {
|
|
14
|
+
// randomUUID 在部分环境里会抛错,继续走回退。
|
|
15
|
+
}
|
|
16
|
+
const hex = (length) => {
|
|
17
|
+
let out = ''
|
|
18
|
+
while (out.length < length) out += Math.floor(Math.random() * 16).toString(16)
|
|
19
|
+
return out.slice(0, length)
|
|
20
|
+
}
|
|
21
|
+
const time = Date.now().toString(16).slice(-12).padStart(12, '0')
|
|
22
|
+
return `${time.slice(0, 8)}-${time.slice(8, 12)}-4${hex(3)}-${'89ab'[Math.floor(Math.random() * 4)]}${hex(3)}-${hex(12)}`
|
|
23
|
+
}
|
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 网页元素拾取与常驻标记。
|
|
3
|
+
*
|
|
4
|
+
* 常驻部分:宿主把「网页来源的批注」同步进页面,脚本按选择器解析出元素,
|
|
5
|
+
* 已批注的元素保留矩形框(3px、采纳绿其余橙、80% 不透明),左上角白色控制柄点开修改。
|
|
6
|
+
*
|
|
7
|
+
* 拾取部分:鼠标经过元素时画无填充、无圆角的框,并给一组合并的按钮:
|
|
8
|
+
* 没有批注给「批注反馈 / 批注采纳」,已有批注给「修改批注 / 删除批注」。
|
|
9
|
+
* 按钮文字始终可见;指针进入具体按钮时该按钮底色加深到 80%。
|
|
10
|
+
* 按钮组默认放在框外,按鼠标靠近的一侧放上方或下方;框外确实没空间才放进框内。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export const PICK_SCRIPT = String.raw`(function () {
|
|
14
|
+
if (window.__saPicker) { try { window.__saWebRender && window.__saWebRender() } catch (e) {} return 'already' }
|
|
15
|
+
var state = { el: null, note: null }
|
|
16
|
+
var host = document.createElement('div')
|
|
17
|
+
host.setAttribute('data-sa-web-picker', '')
|
|
18
|
+
host.style.cssText = 'position:fixed;inset:0;z-index:2147483000;pointer-events:none;contain:layout style;'
|
|
19
|
+
// 页面自己的 button/svg 样式会污染按钮组(圆角、隐藏图标、字号),整组放进
|
|
20
|
+
// Shadow DOM 隔离;宿主要 pointer-events:none,内部按钮再单独打开命中。
|
|
21
|
+
var root = typeof host.attachShadow === 'function' ? host.attachShadow({ mode: 'open' }) : host
|
|
22
|
+
var box = document.createElement('div')
|
|
23
|
+
box.style.cssText = 'position:fixed;border:2px solid rgba(36,123,191,.95);border-radius:0;background:transparent;display:none;pointer-events:none;box-sizing:border-box;'
|
|
24
|
+
var actions = document.createElement('div')
|
|
25
|
+
actions.style.cssText = 'position:fixed;display:none;pointer-events:auto;z-index:2147483001;align-items:center;gap:0;padding:0;border-radius:8px;overflow:hidden;background:rgba(0,0,0,.15);-webkit-mask-image:linear-gradient(to right,transparent,#000 6px,#000 calc(100% - 6px),transparent);mask-image:linear-gradient(to right,transparent,#000 6px,#000 calc(100% - 6px),transparent);transition:background .12s;'
|
|
26
|
+
var layers = document.createElement('div')
|
|
27
|
+
layers.setAttribute('data-sa-web-notes', '')
|
|
28
|
+
root.appendChild(layers)
|
|
29
|
+
root.appendChild(box)
|
|
30
|
+
root.appendChild(actions)
|
|
31
|
+
document.documentElement.appendChild(host)
|
|
32
|
+
|
|
33
|
+
var ICONS = {
|
|
34
|
+
adopt: ['M2.25 8.5L5.49732 11.7473C5.90519 12.1552 6.57263 12.1344 6.95426 11.7018L13.75 4'],
|
|
35
|
+
feedback: [
|
|
36
|
+
'M8.85596 2.69971H4.19971C3.37141 2.69971 2.69992 3.37146 2.69971 4.19971V11.8003C2.69992 12.6285 3.37141 13.3003 4.19971 13.3003H11.8003C12.6283 13.2999 13.3001 12.6283 13.3003 11.8003V7.89893H14.3003V11.8003C14.3001 13.1806 13.1806 14.2999 11.8003 14.3003H4.19971C2.81913 14.3003 1.69992 13.1808 1.69971 11.8003V4.19971C1.69992 2.81918 2.81913 1.69971 4.19971 1.69971H8.85596V2.69971Z|fill',
|
|
37
|
+
'M7.7849 8.23878L13.888 2.13574',
|
|
38
|
+
],
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
var FEATHER = 'linear-gradient(to right,transparent,#000 6px,#000 calc(100% - 6px),transparent)'
|
|
42
|
+
|
|
43
|
+
function buttonsOf() {
|
|
44
|
+
return actions.querySelectorAll('button')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** 未进入按钮组:整组 15% 底、文字 50%,两侧羽化(羽化宽度为字号的一半)。 */
|
|
48
|
+
function resetTone() {
|
|
49
|
+
actions.style.background = 'rgba(0,0,0,.15)'
|
|
50
|
+
actions.style.webkitMaskImage = FEATHER
|
|
51
|
+
actions.style.maskImage = FEATHER
|
|
52
|
+
var buttons = buttonsOf()
|
|
53
|
+
for (var i = 0; i < buttons.length; i += 1) {
|
|
54
|
+
buttons[i].style.background = 'transparent'
|
|
55
|
+
buttons[i].style.color = 'rgba(255,255,255,.5)'
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** 已进入按钮组:取消羽化,未悬停的按钮背景 33%、文字 67%;指针下的按钮背景 67%、文字不透明。 */
|
|
60
|
+
function toneOnEnter() {
|
|
61
|
+
actions.style.background = 'transparent'
|
|
62
|
+
actions.style.webkitMaskImage = 'none'
|
|
63
|
+
actions.style.maskImage = 'none'
|
|
64
|
+
var buttons = buttonsOf()
|
|
65
|
+
for (var i = 0; i < buttons.length; i += 1) {
|
|
66
|
+
buttons[i].style.background = 'rgba(0,0,0,.33)'
|
|
67
|
+
buttons[i].style.color = 'rgba(255,255,255,.67)'
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
actions.addEventListener('pointerenter', toneOnEnter)
|
|
72
|
+
actions.addEventListener('pointerleave', resetTone)
|
|
73
|
+
|
|
74
|
+
function iconFor(action) {
|
|
75
|
+
var svgNS = 'http://www.w3.org/2000/svg'
|
|
76
|
+
var svg = document.createElementNS(svgNS, 'svg')
|
|
77
|
+
svg.setAttribute('viewBox', '0 0 16 16')
|
|
78
|
+
svg.setAttribute('width', '14')
|
|
79
|
+
svg.setAttribute('height', '14')
|
|
80
|
+
svg.setAttribute('fill', 'none')
|
|
81
|
+
svg.setAttribute('stroke-width', '1')
|
|
82
|
+
svg.style.cssText = 'flex:0 0 auto;display:block;'
|
|
83
|
+
var paths = action === 'delete'
|
|
84
|
+
? ['M3 4.2h10', 'M6 4.2V2.8h4v1.4', 'M4.4 4.2l.5 8h6.2l.5-8']
|
|
85
|
+
: (ICONS[action] || ICONS.feedback)
|
|
86
|
+
for (var i = 0; i < paths.length; i += 1) {
|
|
87
|
+
var spec = paths[i]
|
|
88
|
+
var fill = spec.indexOf('|fill') > -1
|
|
89
|
+
var path = document.createElementNS(svgNS, 'path')
|
|
90
|
+
path.setAttribute('d', fill ? spec.slice(0, -5) : spec)
|
|
91
|
+
if (fill) {
|
|
92
|
+
path.setAttribute('fill', 'currentColor')
|
|
93
|
+
path.setAttribute('stroke', 'none')
|
|
94
|
+
} else {
|
|
95
|
+
path.setAttribute('stroke', 'currentColor')
|
|
96
|
+
}
|
|
97
|
+
svg.appendChild(path)
|
|
98
|
+
}
|
|
99
|
+
return svg
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function makeButton(action, label, id, last) {
|
|
103
|
+
var button = document.createElement('button')
|
|
104
|
+
button.type = 'button'
|
|
105
|
+
button.dataset.action = action
|
|
106
|
+
button.style.cssText = 'pointer-events:auto;display:inline-flex;align-items:center;justify-content:center;gap:4px;height:24px;padding:0 9px;border:0;border-radius:0;border-right:' + (last ? '0' : '1px solid rgba(255,255,255,.16)') + ';background:transparent;color:rgba(255,255,255,.8);font:12px/1 system-ui,sans-serif;cursor:pointer;text-shadow:0 1px 3px rgba(0,0,0,.85);transition:background .12s;'
|
|
107
|
+
button.appendChild(iconFor(action))
|
|
108
|
+
button.appendChild(document.createTextNode(label))
|
|
109
|
+
button.addEventListener('pointerenter', function () {
|
|
110
|
+
button.style.background = 'rgba(0,0,0,.67)'
|
|
111
|
+
button.style.color = '#fff'
|
|
112
|
+
})
|
|
113
|
+
button.addEventListener('pointerleave', function () {
|
|
114
|
+
button.style.background = 'rgba(0,0,0,.33)'
|
|
115
|
+
button.style.color = 'rgba(255,255,255,.67)'
|
|
116
|
+
})
|
|
117
|
+
button.addEventListener('click', function (event) { onClick(event, action, id) })
|
|
118
|
+
return button
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function setActionsFor(el) {
|
|
122
|
+
var note = findNote(el)
|
|
123
|
+
state.note = note
|
|
124
|
+
var signature = note ? 'note:' + note.id : 'empty'
|
|
125
|
+
if (actions.dataset.signature !== signature) {
|
|
126
|
+
actions.dataset.signature = signature
|
|
127
|
+
actions.textContent = ''
|
|
128
|
+
if (note) {
|
|
129
|
+
actions.appendChild(makeButton('modify', '修改批注', note.id, false))
|
|
130
|
+
actions.appendChild(makeButton('delete', '删除批注', note.id, true))
|
|
131
|
+
} else {
|
|
132
|
+
actions.appendChild(makeButton('feedback', '批注反馈', '', false))
|
|
133
|
+
actions.appendChild(makeButton('adopt', '批注采纳', '', true))
|
|
134
|
+
}
|
|
135
|
+
resetTone()
|
|
136
|
+
}
|
|
137
|
+
return note
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function findNote(el) {
|
|
141
|
+
var notes = Array.isArray(window.__saWebNotes) ? window.__saWebNotes : []
|
|
142
|
+
for (var i = 0; i < notes.length; i += 1) {
|
|
143
|
+
if (!notes[i] || !notes[i].selector) continue
|
|
144
|
+
var target = null
|
|
145
|
+
try { target = document.querySelector(notes[i].selector) } catch (e) { target = null }
|
|
146
|
+
if (target && target === el) return notes[i]
|
|
147
|
+
}
|
|
148
|
+
return null
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function selectorOf(el) {
|
|
152
|
+
if (!el || el.nodeType !== 1) return ''
|
|
153
|
+
if (el.id) return '#' + CSS.escape(el.id)
|
|
154
|
+
var attrs = ['data-testid', 'data-test', 'name', 'aria-label']
|
|
155
|
+
for (var i = 0; i < attrs.length; i += 1) {
|
|
156
|
+
var value = el.getAttribute(attrs[i])
|
|
157
|
+
if (value) return el.tagName.toLowerCase() + '[' + attrs[i] + '="' + value.replace(/"/g, '\\"') + '"]'
|
|
158
|
+
}
|
|
159
|
+
var parts = []
|
|
160
|
+
var node = el
|
|
161
|
+
while (node && node.nodeType === 1 && parts.length < 8) {
|
|
162
|
+
var tag = node.tagName.toLowerCase()
|
|
163
|
+
var index = 1
|
|
164
|
+
var prev = node.previousElementSibling
|
|
165
|
+
while (prev) { if (prev.tagName === node.tagName) index += 1; prev = prev.previousElementSibling }
|
|
166
|
+
parts.unshift(tag + ':nth-of-type(' + index + ')')
|
|
167
|
+
node = node.parentElement
|
|
168
|
+
if (node === document.body || node === document.documentElement) break
|
|
169
|
+
}
|
|
170
|
+
return parts.join(' > ')
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function labelOf(el) {
|
|
174
|
+
var text = (el.innerText || el.textContent || '').replace(/\s+/g, ' ').trim().slice(0, 60)
|
|
175
|
+
return el.tagName.toLowerCase() + (text ? ' · ' + text : '')
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function rectOf(el) {
|
|
179
|
+
var rect = el.getBoundingClientRect()
|
|
180
|
+
return { left: rect.left, top: rect.top, width: rect.width, height: rect.height }
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function place(el, mouseX, mouseY) {
|
|
184
|
+
if (!el || el.closest('[data-sa-web-picker]')) return
|
|
185
|
+
var rect = el.getBoundingClientRect()
|
|
186
|
+
if (rect.width < 2 || rect.height < 2) return
|
|
187
|
+
state.el = el
|
|
188
|
+
var note = setActionsFor(el)
|
|
189
|
+
// 已经有批注的元素不再叠拾取框,只保留常驻框与按钮组。
|
|
190
|
+
box.style.display = note ? 'none' : 'block'
|
|
191
|
+
if (!note) {
|
|
192
|
+
box.style.left = rect.left + 'px'
|
|
193
|
+
box.style.top = rect.top + 'px'
|
|
194
|
+
box.style.width = rect.width + 'px'
|
|
195
|
+
box.style.height = rect.height + 'px'
|
|
196
|
+
}
|
|
197
|
+
actions.style.display = 'flex'
|
|
198
|
+
var width = actions.offsetWidth || 180
|
|
199
|
+
var height = actions.offsetHeight || 24
|
|
200
|
+
var marginX = 4
|
|
201
|
+
var top
|
|
202
|
+
// 默认放框外:鼠标在元素上半部分就放上方,下半部分放下方;那一侧没空间才换另一侧。
|
|
203
|
+
var preferAbove = mouseY === undefined ? true : mouseY < rect.top + rect.height / 2
|
|
204
|
+
var above = rect.top - height
|
|
205
|
+
var below = rect.bottom
|
|
206
|
+
if (above >= 4 || below + height > window.innerHeight - 4) {
|
|
207
|
+
top = preferAbove ? above : below
|
|
208
|
+
if (top < 4) top = below
|
|
209
|
+
if (top + height > window.innerHeight - 4) top = above >= 4 ? above : Math.max(4, window.innerHeight - height - 4)
|
|
210
|
+
} else {
|
|
211
|
+
top = below
|
|
212
|
+
}
|
|
213
|
+
var outsideOk = top < rect.top || top >= rect.bottom
|
|
214
|
+
if (!outsideOk) {
|
|
215
|
+
// 框外上下都没有空间:放进框内,居中偏上。
|
|
216
|
+
top = rect.top + Math.max(4, Math.min((rect.height - height) / 2, 4))
|
|
217
|
+
}
|
|
218
|
+
var left = Math.max(marginX, Math.min((mouseX === undefined ? rect.left + width / 2 : mouseX) - width / 2, window.innerWidth - width - marginX))
|
|
219
|
+
if (!outsideOk) {
|
|
220
|
+
left = Math.max(rect.left + 2, Math.min(left, rect.right - width - 2))
|
|
221
|
+
}
|
|
222
|
+
actions.style.left = left + 'px'
|
|
223
|
+
actions.style.top = Math.max(4, top) + 'px'
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function inActions(x, y) {
|
|
227
|
+
var rect = actions.getBoundingClientRect()
|
|
228
|
+
return x >= rect.left - 6 && x <= rect.right + 6 && y >= rect.top - 6 && y <= rect.bottom + 6
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function onMove(event) {
|
|
232
|
+
if (actions.style.display !== 'none' && inActions(event.clientX, event.clientY)) return
|
|
233
|
+
var el = document.elementFromPoint(event.clientX, event.clientY)
|
|
234
|
+
if (el && el.closest && el.closest('[data-sa-web-picker]')) return
|
|
235
|
+
place(el, event.clientX, event.clientY)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function onClick(event, action, id) {
|
|
239
|
+
event.preventDefault()
|
|
240
|
+
event.stopPropagation()
|
|
241
|
+
var el = state.el
|
|
242
|
+
if (!el) return
|
|
243
|
+
window.__saPicked = {
|
|
244
|
+
action: action,
|
|
245
|
+
id: id || '',
|
|
246
|
+
selector: selectorOf(el),
|
|
247
|
+
label: labelOf(el),
|
|
248
|
+
url: location.href,
|
|
249
|
+
title: document.title,
|
|
250
|
+
text: el.innerText || el.textContent || '',
|
|
251
|
+
rect: rectOf(el),
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function onKey(event) {
|
|
256
|
+
if (event.key === 'Escape') {
|
|
257
|
+
event.preventDefault()
|
|
258
|
+
actions.style.display = 'none'
|
|
259
|
+
box.style.display = 'none'
|
|
260
|
+
state.el = null
|
|
261
|
+
state.note = null
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function renderNotes() {
|
|
266
|
+
var notes = Array.isArray(window.__saWebNotes) ? window.__saWebNotes : []
|
|
267
|
+
layers.textContent = ''
|
|
268
|
+
notes.forEach(function (note) {
|
|
269
|
+
if (!note || !note.selector) return
|
|
270
|
+
if (note.url && note.url !== location.href) return
|
|
271
|
+
var el = null
|
|
272
|
+
try { el = document.querySelector(note.selector) } catch (e) { el = null }
|
|
273
|
+
if (!el) return
|
|
274
|
+
var rect = el.getBoundingClientRect()
|
|
275
|
+
if (rect.width < 2 || rect.height < 2) return
|
|
276
|
+
var color = note.adopt ? 'rgba(52,199,89,.8)' : 'rgba(245,158,11,.8)'
|
|
277
|
+
var frame = document.createElement('div')
|
|
278
|
+
frame.style.cssText = 'position:fixed;border:3px solid ' + color + ';border-radius:0;background:transparent;pointer-events:none;box-sizing:border-box;'
|
|
279
|
+
frame.style.left = rect.left + 'px'
|
|
280
|
+
frame.style.top = rect.top + 'px'
|
|
281
|
+
frame.style.width = rect.width + 'px'
|
|
282
|
+
frame.style.height = rect.height + 'px'
|
|
283
|
+
layers.appendChild(frame)
|
|
284
|
+
var handle = document.createElement('button')
|
|
285
|
+
handle.type = 'button'
|
|
286
|
+
handle.setAttribute('aria-label', '修改这条网页批注')
|
|
287
|
+
handle.style.cssText = 'position:fixed;width:14px;height:14px;padding:0;border-radius:50%;border:1px solid rgba(0,0,0,.35);background:#fff;pointer-events:auto;cursor:pointer;box-shadow:0 1px 3px rgba(0,0,0,.4);'
|
|
288
|
+
handle.style.left = (rect.left - 5) + 'px'
|
|
289
|
+
handle.style.top = (rect.top - 5) + 'px'
|
|
290
|
+
handle.addEventListener('click', function (event) {
|
|
291
|
+
event.preventDefault()
|
|
292
|
+
event.stopPropagation()
|
|
293
|
+
window.__saPicked = { action: 'open', id: note.id, selector: note.selector, rect: rectOf(el) }
|
|
294
|
+
})
|
|
295
|
+
layers.appendChild(handle)
|
|
296
|
+
})
|
|
297
|
+
// 刚新增/删除批注时,正悬停的元素要立刻换成对应的按钮组。
|
|
298
|
+
if (state.el) setActionsFor(state.el)
|
|
299
|
+
if (state.el && state.note) box.style.display = 'none'
|
|
300
|
+
}
|
|
301
|
+
window.__saWebRender = renderNotes
|
|
302
|
+
renderNotes()
|
|
303
|
+
window.addEventListener('scroll', renderNotes, true)
|
|
304
|
+
window.addEventListener('resize', renderNotes)
|
|
305
|
+
|
|
306
|
+
window.addEventListener('mousemove', onMove, true)
|
|
307
|
+
window.addEventListener('keydown', onKey, true)
|
|
308
|
+
window.__saPicker = { stop: function () { window.removeEventListener('mousemove', onMove, true) } }
|
|
309
|
+
return 'ok'
|
|
310
|
+
})()`
|
|
311
|
+
|
|
312
|
+
const POLL_MS = 400
|
|
313
|
+
|
|
314
|
+
export function createWebPicker({ onPick }) {
|
|
315
|
+
let timer = 0
|
|
316
|
+
let lastNotes = []
|
|
317
|
+
|
|
318
|
+
function view() {
|
|
319
|
+
return (
|
|
320
|
+
document.querySelector('webview[data-sidebar-browser-frame="webview"]') ?? document.querySelector('webview')
|
|
321
|
+
)
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function execute(script) {
|
|
325
|
+
const webview = view()
|
|
326
|
+
if (webview === null || typeof webview.executeJavaScript !== 'function') return undefined
|
|
327
|
+
try {
|
|
328
|
+
return webview.executeJavaScript(script)
|
|
329
|
+
} catch {
|
|
330
|
+
return undefined
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function stop() {
|
|
335
|
+
if (timer !== 0) {
|
|
336
|
+
window.clearInterval(timer)
|
|
337
|
+
timer = 0
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** 把网页来源的批注同步给页面:{id, selector, url, adopt}。 */
|
|
342
|
+
function sync(notes) {
|
|
343
|
+
lastNotes = Array.isArray(notes) ? notes : []
|
|
344
|
+
const json = JSON.stringify(lastNotes)
|
|
345
|
+
execute(`window.__saWebNotes = ${json}; window.__saWebRender && window.__saWebRender()`)
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* 按设置截当前页:scope 为 'page' 截整页,'element' 截目标元素区域,'off' 不截。
|
|
350
|
+
* 失败返回 undefined,批注仍可正常创建。
|
|
351
|
+
*/
|
|
352
|
+
async function capture(scope, rect) {
|
|
353
|
+
if (scope !== 'page' && scope !== 'element') return undefined
|
|
354
|
+
const webview = view()
|
|
355
|
+
if (webview === null || typeof webview.capturePage !== 'function') return undefined
|
|
356
|
+
try {
|
|
357
|
+
const area =
|
|
358
|
+
scope === 'element' && rect !== null && typeof rect === 'object'
|
|
359
|
+
? {
|
|
360
|
+
x: Math.max(0, Math.round(rect.left)),
|
|
361
|
+
y: Math.max(0, Math.round(rect.top)),
|
|
362
|
+
width: Math.max(1, Math.round(rect.width)),
|
|
363
|
+
height: Math.max(1, Math.round(rect.height)),
|
|
364
|
+
}
|
|
365
|
+
: undefined
|
|
366
|
+
const image = await webview.capturePage(area)
|
|
367
|
+
if (image === null || image === undefined || typeof image.toDataURL !== 'function') return undefined
|
|
368
|
+
const dataUrl = image.toDataURL()
|
|
369
|
+
return typeof dataUrl === 'string' && dataUrl.startsWith('data:image/') ? dataUrl : undefined
|
|
370
|
+
} catch {
|
|
371
|
+
return undefined
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function start() {
|
|
376
|
+
if (timer !== 0) return
|
|
377
|
+
timer = window.setInterval(() => {
|
|
378
|
+
const webview = view()
|
|
379
|
+
if (webview === null || typeof webview.executeJavaScript !== 'function') return
|
|
380
|
+
let raw
|
|
381
|
+
try {
|
|
382
|
+
raw = webview.executeJavaScript('({ live: !!window.__saPicker, picked: window.__saPicked || null })')
|
|
383
|
+
} catch {
|
|
384
|
+
return
|
|
385
|
+
}
|
|
386
|
+
Promise.resolve(raw)
|
|
387
|
+
.then((state) => {
|
|
388
|
+
if (state === null || typeof state !== 'object') return
|
|
389
|
+
if (state.picked !== null && typeof state.picked === 'object') {
|
|
390
|
+
safeCall(() => webview.executeJavaScript('window.__saPicked = null'))
|
|
391
|
+
onPick({
|
|
392
|
+
action: typeof state.picked.action === 'string' ? state.picked.action : 'feedback',
|
|
393
|
+
id: typeof state.picked.id === 'string' ? state.picked.id : '',
|
|
394
|
+
quote: typeof state.picked.label === 'string' ? state.picked.label : '',
|
|
395
|
+
selector: typeof state.picked.selector === 'string' ? state.picked.selector : '',
|
|
396
|
+
url: typeof state.picked.url === 'string' ? state.picked.url : '',
|
|
397
|
+
text: typeof state.picked.text === 'string' ? state.picked.text : '',
|
|
398
|
+
rect: state.picked.rect ?? null,
|
|
399
|
+
})
|
|
400
|
+
return
|
|
401
|
+
}
|
|
402
|
+
if (state.live !== true) {
|
|
403
|
+
safeCall(() => webview.executeJavaScript(PICK_SCRIPT))
|
|
404
|
+
// 页面重新注入时把已有的批注一并推回去,换页或首次打开浏览器都能立刻恢复常驻框。
|
|
405
|
+
if (lastNotes.length > 0) sync(lastNotes)
|
|
406
|
+
}
|
|
407
|
+
})
|
|
408
|
+
.catch(() => {})
|
|
409
|
+
}, POLL_MS)
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function dispose() {
|
|
413
|
+
execute('window.__saPicker && window.__saPicker.stop()')
|
|
414
|
+
stop()
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
return { start, sync, capture, dispose, available: () => view() !== null }
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function safeCall(fn) {
|
|
421
|
+
try {
|
|
422
|
+
return fn()
|
|
423
|
+
} catch {
|
|
424
|
+
return undefined
|
|
425
|
+
}
|
|
426
|
+
}
|
package/src/entry.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createPlugin } from './plugin.js'
|
|
2
|
+
|
|
3
|
+
window.__ModuleLoader__.load({
|
|
4
|
+
id: 'dsh-selection-annotations',
|
|
5
|
+
factory(require) {
|
|
6
|
+
const React = require('react')
|
|
7
|
+
// 这两项是可选能力:宿主版本缺少它们时退回纯文本渲染,不影响插件加载。
|
|
8
|
+
let MarkdownText
|
|
9
|
+
let createRoot
|
|
10
|
+
try {
|
|
11
|
+
MarkdownText = require('@deepseek-ai/dsh-client-ui-primitives').MarkdownText
|
|
12
|
+
} catch {
|
|
13
|
+
MarkdownText = undefined
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
createRoot = require('react-dom/client').createRoot
|
|
17
|
+
} catch {
|
|
18
|
+
createRoot = undefined
|
|
19
|
+
}
|
|
20
|
+
return createPlugin(React, { MarkdownText, createRoot })
|
|
21
|
+
},
|
|
22
|
+
})
|