@infinity_cheng/dsh-prompt-editor 0.3.1 → 0.3.2
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/README.md +6 -0
- package/client.js +124 -31
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -214,6 +214,12 @@ Safari 验证结果。
|
|
|
214
214
|
- Host + Client 两半;Client 是 lazy-CJS bundle,无独立构建步骤
|
|
215
215
|
- Client 槽位:`conversation.input.right`、`settings.section`
|
|
216
216
|
|
|
217
|
+
### 0.3.2 引用块修复(本地验证版)
|
|
218
|
+
|
|
219
|
+
- 快捷键触发前缓存选区,避免 `⌘E` 先收掉选区。
|
|
220
|
+
- 修正 chip 的 detect/clipboard 坐标映射,正例只发送选区文本,不再截断。
|
|
221
|
+
- 实测:无选区拦截、跨 chip 选区拦截、正例替换后 chip 保留。
|
|
222
|
+
|
|
217
223
|
### 0.3.1 快捷键修复
|
|
218
224
|
|
|
219
225
|
- 主快捷键改为 `⌘E` / `Ctrl+E`,避开 Mac 输入法常吞掉的 Shift+字母;`⌘⇧E` / `Ctrl+Shift+E` 仍兼容。
|
package/client.js
CHANGED
|
@@ -105,21 +105,69 @@ window.__ModuleLoader__.load({
|
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
/** Show only the prompt body while the model is still emitting its tagged answer. */
|
|
108
|
-
/**
|
|
109
|
-
|
|
110
|
-
|
|
108
|
+
/**
|
|
109
|
+
* Map clipboard-text coordinates (the draft string) to the editor's
|
|
110
|
+
* detect coordinates (a chip is one character) and back. Selection spans
|
|
111
|
+
* and insertText use detect coordinates; the model needs clipboard text.
|
|
112
|
+
*/
|
|
113
|
+
function draftSegments(draft, occurrences) {
|
|
114
|
+
const chips = (Array.isArray(occurrences) ? occurrences : [])
|
|
111
115
|
.map((chip) => {
|
|
112
|
-
const
|
|
113
|
-
const
|
|
114
|
-
? chip.
|
|
115
|
-
: (Number.isFinite(
|
|
116
|
-
return {
|
|
116
|
+
const offset = Number.isFinite(chip?.offset) ? chip.offset : chip?.start
|
|
117
|
+
const length = Number.isFinite(chip?.length)
|
|
118
|
+
? chip.length
|
|
119
|
+
: (Number.isFinite(chip?.end) && Number.isFinite(offset) ? chip.end - offset : chip?.clipboardText?.length)
|
|
120
|
+
return { offset, length }
|
|
117
121
|
})
|
|
118
|
-
.filter((chip) => Number.isFinite(chip.
|
|
122
|
+
.filter((chip) => Number.isFinite(chip.offset) && Number.isFinite(chip.length)
|
|
123
|
+
&& chip.offset >= 0 && chip.length > 0)
|
|
124
|
+
.sort((a, b) => a.offset - b.offset)
|
|
125
|
+
|
|
126
|
+
const segments = []
|
|
127
|
+
let detectCursor = 0
|
|
128
|
+
let clipCursor = 0
|
|
129
|
+
let shift = 0
|
|
130
|
+
for (const chip of chips) {
|
|
131
|
+
if (chip.offset > clipCursor) {
|
|
132
|
+
const length = chip.offset - clipCursor
|
|
133
|
+
segments.push({
|
|
134
|
+
kind: 'text',
|
|
135
|
+
detectStart: detectCursor,
|
|
136
|
+
detectEnd: detectCursor + length,
|
|
137
|
+
clipStart: clipCursor,
|
|
138
|
+
clipEnd: chip.offset,
|
|
139
|
+
})
|
|
140
|
+
detectCursor += length
|
|
141
|
+
}
|
|
142
|
+
const detectStart = chip.offset - shift
|
|
143
|
+
segments.push({
|
|
144
|
+
kind: 'chip',
|
|
145
|
+
detectStart,
|
|
146
|
+
detectEnd: detectStart + 1,
|
|
147
|
+
clipStart: chip.offset,
|
|
148
|
+
clipEnd: chip.offset + chip.length,
|
|
149
|
+
})
|
|
150
|
+
detectCursor = detectStart + 1
|
|
151
|
+
clipCursor = chip.offset + chip.length
|
|
152
|
+
shift += chip.length - 1
|
|
153
|
+
}
|
|
154
|
+
if (clipCursor < draft.length) {
|
|
155
|
+
const length = draft.length - clipCursor
|
|
156
|
+
segments.push({
|
|
157
|
+
kind: 'text',
|
|
158
|
+
detectStart: detectCursor,
|
|
159
|
+
detectEnd: detectCursor + length,
|
|
160
|
+
clipStart: clipCursor,
|
|
161
|
+
clipEnd: draft.length,
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
return segments
|
|
119
165
|
}
|
|
120
166
|
|
|
121
|
-
function
|
|
122
|
-
return
|
|
167
|
+
function textSegmentFor(segments, span) {
|
|
168
|
+
return segments.find((segment) => segment.kind === 'text'
|
|
169
|
+
&& span.start >= segment.detectStart && span.end <= segment.detectEnd
|
|
170
|
+
&& span.end > span.start)
|
|
123
171
|
}
|
|
124
172
|
|
|
125
173
|
function visibleText(raw) {
|
|
@@ -393,6 +441,7 @@ window.__ModuleLoader__.load({
|
|
|
393
441
|
const abortRef = useRef(null)
|
|
394
442
|
const mountedRef = useRef(true)
|
|
395
443
|
const triggerRef = useRef(null)
|
|
444
|
+
const selectionCacheRef = useRef(null)
|
|
396
445
|
|
|
397
446
|
useEffect(() => () => {
|
|
398
447
|
mountedRef.current = false
|
|
@@ -481,22 +530,23 @@ window.__ModuleLoader__.load({
|
|
|
481
530
|
}
|
|
482
531
|
}, [props.sessionId, text])
|
|
483
532
|
|
|
484
|
-
const openDialog = useCallback(() => {
|
|
533
|
+
const openDialog = useCallback((providedSpan) => {
|
|
485
534
|
if (inputActions === undefined || typeof inputActions.captureInsertion !== 'function') return
|
|
486
|
-
let span
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
535
|
+
let span = providedSpan
|
|
536
|
+
if (span === undefined || span === null) {
|
|
537
|
+
try {
|
|
538
|
+
span = inputActions.captureInsertion()
|
|
539
|
+
} catch {
|
|
540
|
+
return
|
|
541
|
+
}
|
|
491
542
|
}
|
|
492
543
|
if (span === null || span === undefined || !Number.isFinite(span.start) || !Number.isFinite(span.end)) return
|
|
493
544
|
if (span.draftRev !== undefined && draftRev !== undefined && span.draftRev !== draftRev) return
|
|
494
545
|
const selected = span.end > span.start
|
|
495
|
-
const
|
|
496
|
-
|
|
497
|
-
const chips = chipRanges(occurrences)
|
|
546
|
+
const segments = draftSegments(draft, occurrences)
|
|
547
|
+
const hasChips = segments.some((segment) => segment.kind === 'chip')
|
|
498
548
|
const mode = readMode()
|
|
499
|
-
if (
|
|
549
|
+
if (hasChips && !selected) {
|
|
500
550
|
setOpen(true)
|
|
501
551
|
setState({
|
|
502
552
|
source: draft, span, selected: false, mode, context: undefined, raw: '',
|
|
@@ -504,18 +554,50 @@ window.__ModuleLoader__.load({
|
|
|
504
554
|
})
|
|
505
555
|
return
|
|
506
556
|
}
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
557
|
+
let source = draft
|
|
558
|
+
if (selected) {
|
|
559
|
+
const segment = textSegmentFor(segments, span)
|
|
560
|
+
if (hasChips && segment === undefined) {
|
|
561
|
+
setOpen(true)
|
|
562
|
+
setState({
|
|
563
|
+
source: draft.slice(span.start, span.end), span, selected: true, mode, context: undefined, raw: '',
|
|
564
|
+
result: null, error: text('dialog.chipsOverlap'), loading: false, retryable: false,
|
|
565
|
+
})
|
|
566
|
+
return
|
|
567
|
+
}
|
|
568
|
+
source = segment === undefined
|
|
569
|
+
? draft.slice(span.start, span.end)
|
|
570
|
+
: draft.slice(
|
|
571
|
+
segment.clipStart + (span.start - segment.detectStart),
|
|
572
|
+
segment.clipStart + (span.end - segment.detectStart),
|
|
573
|
+
)
|
|
514
574
|
}
|
|
575
|
+
if (source.trim() === '') return
|
|
515
576
|
setOpen(true)
|
|
516
|
-
run(source, span, selected, mode,
|
|
577
|
+
run(source, span, selected, mode, hasChips ? draft : undefined)
|
|
517
578
|
}, [draft, draftRev, inputActions, occurrences, run, text])
|
|
518
579
|
|
|
580
|
+
useEffect(() => {
|
|
581
|
+
if (inputActions === undefined || typeof inputActions.captureInsertion !== 'function') return undefined
|
|
582
|
+
const remember = () => {
|
|
583
|
+
try {
|
|
584
|
+
const span = inputActions.captureInsertion()
|
|
585
|
+
if (span !== null && span !== undefined && span.end > span.start) selectionCacheRef.current = span
|
|
586
|
+
} catch {
|
|
587
|
+
// The editor may be between revisions; keep the last good selection.
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
const clear = () => {
|
|
591
|
+
selectionCacheRef.current = null
|
|
592
|
+
}
|
|
593
|
+
document.addEventListener('selectionchange', remember)
|
|
594
|
+
window.addEventListener('pointerdown', clear, true)
|
|
595
|
+
return () => {
|
|
596
|
+
document.removeEventListener('selectionchange', remember)
|
|
597
|
+
window.removeEventListener('pointerdown', clear, true)
|
|
598
|
+
}
|
|
599
|
+
}, [inputActions])
|
|
600
|
+
|
|
519
601
|
useEffect(() => {
|
|
520
602
|
const onKey = (event) => {
|
|
521
603
|
if (open) return
|
|
@@ -526,7 +608,18 @@ window.__ModuleLoader__.load({
|
|
|
526
608
|
if (target.closest('[contenteditable="true"]') === null
|
|
527
609
|
&& target.closest('.dsh-prompt-editor-trigger') === null) return
|
|
528
610
|
event.preventDefault()
|
|
529
|
-
|
|
611
|
+
let current
|
|
612
|
+
try {
|
|
613
|
+
current = inputActions.captureInsertion()
|
|
614
|
+
} catch {
|
|
615
|
+
current = undefined
|
|
616
|
+
}
|
|
617
|
+
const cached = selectionCacheRef.current
|
|
618
|
+
const span = current !== undefined && current !== null && current.end > current.start
|
|
619
|
+
? current
|
|
620
|
+
: (cached !== null && cached.draftRev === draftRev ? cached : current)
|
|
621
|
+
selectionCacheRef.current = null
|
|
622
|
+
openDialog(span)
|
|
530
623
|
}
|
|
531
624
|
window.addEventListener('keydown', onKey, true)
|
|
532
625
|
return () => window.removeEventListener('keydown', onKey, true)
|
|
@@ -582,7 +675,7 @@ window.__ModuleLoader__.load({
|
|
|
582
675
|
title: reason,
|
|
583
676
|
'aria-label': `${text('button.label')}:${reason}`,
|
|
584
677
|
onMouseDown: (event) => event.preventDefault(),
|
|
585
|
-
onClick: openDialog,
|
|
678
|
+
onClick: () => openDialog(),
|
|
586
679
|
}, ICON, h('span', null, text('button.label'))),
|
|
587
680
|
open && state !== null
|
|
588
681
|
? h(PreviewDialog, {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@infinity_cheng/dsh-prompt-editor",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Rewrite a rough prompt draft into a clear, executable prompt in DeepSeek Harness: streaming preview, selection-aware replace, and fact-preservation checks.",
|
|
5
5
|
"icon": "icon.svg",
|
|
6
6
|
"type": "module",
|