@infinity_cheng/dsh-prompt-editor 0.3.0 → 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.
Files changed (3) hide show
  1. package/README.md +12 -2
  2. package/client.js +127 -34
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -47,7 +47,7 @@ Client 可以热更新,但 Host 的 JavaScript 模块换代必须**完全退
47
47
  ## 输入框按钮
48
48
 
49
49
  1. 在输入框里输入草稿,或选中其中一段。
50
- 2. 点输入框右侧的「增强」,或在输入框里按 `⌘⇧E` / `Ctrl+Shift+E`。
50
+ 2. 点输入框右侧的「增强」,或在输入框里按 `⌘E` / `Ctrl+E`(`⌘⇧E` / `Ctrl+Shift+E` 也兼容)。
51
51
  3. 预览弹窗会流式显示改写结果;左侧是原文,右侧是改写,下面是确定性检查。
52
52
  4. 选 `替换选中` / `替换全文` 才写回草稿;`重试` 可换规则重跑,`取消` 不改任何内容。
53
53
  5. 有选区时只替换选区;没有选区时替换全文。替换后仍可用输入框自己的 `Ctrl+Z` 撤销。
@@ -214,9 +214,19 @@ 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
+
223
+ ### 0.3.1 快捷键修复
224
+
225
+ - 主快捷键改为 `⌘E` / `Ctrl+E`,避开 Mac 输入法常吞掉的 Shift+字母;`⌘⇧E` / `Ctrl+Shift+E` 仍兼容。
226
+
217
227
  ### 0.3.0 Phase 2
218
228
 
219
- - 输入框内 `⌘⇧E` / `Ctrl+Shift+E` 打开增强。
229
+ - 输入框内 `⌘E` / `Ctrl+E` 打开增强;兼容 `⌘⇧E` / `Ctrl+Shift+E`。
220
230
  - 含引用块的草稿可选中不含引用块的一段增强,引用块原样保留;全文替换含引用块仍不支持。
221
231
 
222
232
  ### 0.2.1 修复
package/client.js CHANGED
@@ -23,7 +23,7 @@ window.__ModuleLoader__.load({
23
23
  const OPEN_TAG = '<enhanced_prompt>'
24
24
  const CLOSE_TAG = '</enhanced_prompt>'
25
25
  const IS_MAC = typeof navigator !== 'undefined' && /Mac|iPhone|iPad/.test(navigator.platform || '')
26
- const SHORTCUT_LABEL = IS_MAC ? '⌘⇧E' : 'Ctrl+Shift+E'
26
+ const SHORTCUT_LABEL = IS_MAC ? '⌘E' : 'Ctrl+E'
27
27
 
28
28
  const FALLBACK = {
29
29
  'meta.title': '提示词增强',
@@ -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
- /** Chip ranges in draft coordinates; tolerate the old offset/length shape. */
109
- function chipRanges(occurrences) {
110
- return (Array.isArray(occurrences) ? occurrences : [])
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 start = Number.isFinite(chip?.start) ? chip.start : chip?.offset
113
- const end = Number.isFinite(chip?.end)
114
- ? chip.end
115
- : (Number.isFinite(start) && Number.isFinite(chip?.length) ? start + chip.length : undefined)
116
- return { start, end }
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.start) && Number.isFinite(chip.end) && chip.end > chip.start)
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 overlapsChip(chips, span) {
122
- return chips.some((chip) => span.start < chip.end && span.end > chip.start)
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
- try {
488
- span = inputActions.captureInsertion()
489
- } catch {
490
- return
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 source = selected ? draft.slice(span.start, span.end) : draft
496
- if (source.trim() === '') return
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 (chips.length > 0 && !selected) {
549
+ if (hasChips && !selected) {
500
550
  setOpen(true)
501
551
  setState({
502
552
  source: draft, span, selected: false, mode, context: undefined, raw: '',
@@ -504,29 +554,72 @@ window.__ModuleLoader__.load({
504
554
  })
505
555
  return
506
556
  }
507
- if (chips.length > 0 && overlapsChip(chips, span)) {
508
- setOpen(true)
509
- setState({
510
- source, span, selected: true, mode, context: undefined, raw: '',
511
- result: null, error: text('dialog.chipsOverlap'), loading: false, retryable: false,
512
- })
513
- return
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, chips.length > 0 ? draft : undefined)
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
522
- if (!(event.metaKey || event.ctrlKey) || !event.shiftKey) return
523
- if (event.key.toLowerCase() !== 'e') return
604
+ if (!(event.metaKey || event.ctrlKey) || event.altKey) return
605
+ if (event.code !== 'KeyE' && event.key.toLowerCase() !== 'e') return
524
606
  const target = event.target
525
607
  if (!(target instanceof HTMLElement)) return
526
608
  if (target.closest('[contenteditable="true"]') === null
527
609
  && target.closest('.dsh-prompt-editor-trigger') === null) return
528
610
  event.preventDefault()
529
- openDialog()
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.0",
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",