dsh-better-sidebar 0.10.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/LICENSE +21 -0
- package/README.md +139 -0
- package/README_EN.md +139 -0
- package/lib/client-docx.js +7649 -0
- package/lib/client-editor.js +32677 -0
- package/lib/client-pptx.js +63438 -0
- package/lib/client-registry.js +8021 -0
- package/lib/client-terminal.js +8060 -0
- package/lib/client-xlsx.js +359755 -0
- package/lib/client.js +8021 -0
- package/lib/index.js +2604 -0
- package/lib/invariant.js +22 -0
- package/lib/types/agent-pty.d.ts +220 -0
- package/lib/types/browser-probe.d.ts +15 -0
- package/lib/types/bundle-route.d.ts +13 -0
- package/lib/types/client/BrowserView.d.ts +22 -0
- package/lib/types/client/DiffTab.d.ts +6 -0
- package/lib/types/client/DiffView.d.ts +51 -0
- package/lib/types/client/EditorHost.d.ts +10 -0
- package/lib/types/client/ExplorerView.d.ts +9 -0
- package/lib/types/client/GitView.d.ts +8 -0
- package/lib/types/client/OrphanedTab.d.ts +11 -0
- package/lib/types/client/PdfView.d.ts +6 -0
- package/lib/types/client/PptxView.d.ts +6 -0
- package/lib/types/client/SandboxStatusBar.d.ts +10 -0
- package/lib/types/client/SideCardSection.d.ts +27 -0
- package/lib/types/client/Sidebar.d.ts +6 -0
- package/lib/types/client/SubagentView.d.ts +16 -0
- package/lib/types/client/TabBar.d.ts +38 -0
- package/lib/types/client/TerminalView.d.ts +8 -0
- package/lib/types/client/TextEditor.d.ts +10 -0
- package/lib/types/client/api.d.ts +170 -0
- package/lib/types/client/binary-download.d.ts +12 -0
- package/lib/types/client/breakpoints.d.ts +12 -0
- package/lib/types/client/browser.d.ts +50 -0
- package/lib/types/client/builtins/index.d.ts +17 -0
- package/lib/types/client/builtins/tabs.d.ts +6 -0
- package/lib/types/client/builtins/viewers.d.ts +3 -0
- package/lib/types/client/chunk-loader.d.ts +74 -0
- package/lib/types/client/chunks/docx.d.ts +8 -0
- package/lib/types/client/chunks/editor.d.ts +9 -0
- package/lib/types/client/chunks/pptx.d.ts +8 -0
- package/lib/types/client/chunks/terminal.d.ts +9 -0
- package/lib/types/client/chunks/xlsx.d.ts +9 -0
- package/lib/types/client/cm-themes.d.ts +24 -0
- package/lib/types/client/conversation-draft.d.ts +14 -0
- package/lib/types/client/docx-view.d.ts +8 -0
- package/lib/types/client/editor-load.d.ts +66 -0
- package/lib/types/client/icons.d.ts +47 -0
- package/lib/types/client/image-types.d.ts +3 -0
- package/lib/types/client/ime-guard.d.ts +36 -0
- package/lib/types/client/index.d.ts +10 -0
- package/lib/types/client/intercept.d.ts +20 -0
- package/lib/types/client/lang.d.ts +12 -0
- package/lib/types/client/lazy-chunk.d.ts +25 -0
- package/lib/types/client/link-intercept.d.ts +35 -0
- package/lib/types/client/locales.d.ts +237 -0
- package/lib/types/client/office-shared.d.ts +33 -0
- package/lib/types/client/office-types.d.ts +36 -0
- package/lib/types/client/office-view.d.ts +12 -0
- package/lib/types/client/openpath-intercept.d.ts +41 -0
- package/lib/types/client/paths.d.ts +19 -0
- package/lib/types/client/pdf-types.d.ts +2 -0
- package/lib/types/client/prefs.d.ts +30 -0
- package/lib/types/client/produced-files.d.ts +27 -0
- package/lib/types/client/selection-payload.d.ts +27 -0
- package/lib/types/client/service.d.ts +214 -0
- package/lib/types/client/split-pane.d.ts +29 -0
- package/lib/types/client/state.d.ts +322 -0
- package/lib/types/client/subagent-activity.d.ts +35 -0
- package/lib/types/client/subagent-detect.d.ts +47 -0
- package/lib/types/client/subagent-jobs.d.ts +63 -0
- package/lib/types/client/theme.d.ts +28 -0
- package/lib/types/client/xlsx-to-univer.d.ts +33 -0
- package/lib/types/client/xlsx-view.d.ts +10 -0
- package/lib/types/config.d.ts +41 -0
- package/lib/types/context-types.d.ts +431 -0
- package/lib/types/fs-tree.d.ts +41 -0
- package/lib/types/git.d.ts +75 -0
- package/lib/types/html-route.d.ts +48 -0
- package/lib/types/index.d.ts +39 -0
- package/lib/types/invariant.d.ts +15 -0
- package/lib/types/jobs-routes.d.ts +45 -0
- package/lib/types/prefs-shared.d.ts +106 -0
- package/lib/types/pty-manager.d.ts +76 -0
- package/lib/types/tools.d.ts +28 -0
- package/lib/types/trust-fence.d.ts +24 -0
- package/lib/types/wire.d.ts +38 -0
- package/package.json +168 -0
- package/src/agent-pty.ts +519 -0
- package/src/browser-probe.ts +26 -0
- package/src/bundle-route.ts +130 -0
- package/src/client/BrowserView.tsx +253 -0
- package/src/client/DiffTab.tsx +110 -0
- package/src/client/DiffView.tsx +266 -0
- package/src/client/EditorHost.tsx +101 -0
- package/src/client/ExplorerView.tsx +283 -0
- package/src/client/GitView.tsx +540 -0
- package/src/client/OrphanedTab.tsx +27 -0
- package/src/client/PdfView.tsx +110 -0
- package/src/client/PptxView.tsx +131 -0
- package/src/client/SandboxStatusBar.tsx +60 -0
- package/src/client/SideCardSection.module.css +271 -0
- package/src/client/SideCardSection.tsx +426 -0
- package/src/client/Sidebar.tsx +861 -0
- package/src/client/SubagentView.module.css +547 -0
- package/src/client/SubagentView.tsx +866 -0
- package/src/client/TabBar.tsx +188 -0
- package/src/client/TerminalView.tsx +233 -0
- package/src/client/TextEditor.tsx +382 -0
- package/src/client/api.ts +215 -0
- package/src/client/binary-download.tsx +23 -0
- package/src/client/breakpoints.ts +51 -0
- package/src/client/browser.ts +119 -0
- package/src/client/builtins/index.ts +33 -0
- package/src/client/builtins/tabs.tsx +211 -0
- package/src/client/builtins/viewers.tsx +151 -0
- package/src/client/chunk-loader.ts +196 -0
- package/src/client/chunks/docx.tsx +8 -0
- package/src/client/chunks/editor.tsx +9 -0
- package/src/client/chunks/pptx.tsx +8 -0
- package/src/client/chunks/terminal.tsx +9 -0
- package/src/client/chunks/xlsx.tsx +9 -0
- package/src/client/cm-themes.ts +129 -0
- package/src/client/conversation-draft.ts +29 -0
- package/src/client/css-modules.d.ts +5 -0
- package/src/client/docx-view.tsx +113 -0
- package/src/client/editor-load.ts +92 -0
- package/src/client/icons.tsx +137 -0
- package/src/client/image-types.ts +8 -0
- package/src/client/ime-guard.ts +47 -0
- package/src/client/index.tsx +238 -0
- package/src/client/intercept.tsx +94 -0
- package/src/client/lang.ts +95 -0
- package/src/client/layout.css +54 -0
- package/src/client/lazy-chunk.tsx +89 -0
- package/src/client/link-intercept.ts +66 -0
- package/src/client/locales.ts +476 -0
- package/src/client/office-shared.tsx +40 -0
- package/src/client/office-types.ts +48 -0
- package/src/client/office-view.tsx +12 -0
- package/src/client/openpath-intercept.ts +62 -0
- package/src/client/paths.ts +28 -0
- package/src/client/pdf-types.ts +4 -0
- package/src/client/prefs.ts +100 -0
- package/src/client/produced-files.ts +87 -0
- package/src/client/selection-payload.ts +86 -0
- package/src/client/service.ts +434 -0
- package/src/client/sidebar.module.css +1917 -0
- package/src/client/split-pane.tsx +292 -0
- package/src/client/state.ts +1048 -0
- package/src/client/subagent-activity.ts +67 -0
- package/src/client/subagent-detect.ts +125 -0
- package/src/client/subagent-jobs.ts +161 -0
- package/src/client/theme.ts +44 -0
- package/src/client/xlsx-to-univer.ts +260 -0
- package/src/client/xlsx-view.tsx +116 -0
- package/src/config.ts +95 -0
- package/src/context-types.ts +445 -0
- package/src/fs-tree.ts +115 -0
- package/src/git.ts +236 -0
- package/src/html-route.ts +70 -0
- package/src/index.ts +897 -0
- package/src/invariant.ts +32 -0
- package/src/jobs-routes.ts +257 -0
- package/src/prefs-shared.ts +127 -0
- package/src/pty-manager.ts +199 -0
- package/src/tools.ts +477 -0
- package/src/trust-fence.ts +77 -0
- package/src/wire.ts +94 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-side read of the user-facing "Side card" preferences. The host owns
|
|
3
|
+
* the namespace through the settings seam (in-process); the DSH settings RPC
|
|
4
|
+
* domain only serves allowlisted namespaces to configuration clients, so the
|
|
5
|
+
* client reads and writes THIS namespace through the plugin's own fenced
|
|
6
|
+
* /sidebar routes instead (api.settingsGet/settingsUpdate). Any failure
|
|
7
|
+
* (route rejected, namespace absent, a field of the wrong type, a value out
|
|
8
|
+
* of the contract range) falls back to the schema defaults — the side card
|
|
9
|
+
* must keep working exactly as composed when the settings surface is missing.
|
|
10
|
+
*/
|
|
11
|
+
import type { api } from './api.ts'
|
|
12
|
+
import {
|
|
13
|
+
clampWidthPercent,
|
|
14
|
+
SIDEBAR_PREFS_DEFAULTS,
|
|
15
|
+
type SidebarPrefs,
|
|
16
|
+
} from '../prefs-shared.ts'
|
|
17
|
+
|
|
18
|
+
export { SIDEBAR_PREFS_DEFAULTS, clampWidthPercent }
|
|
19
|
+
export type { SidebarPrefs }
|
|
20
|
+
|
|
21
|
+
/** The settings wire face the preferences need (a subset of the plugin api). */
|
|
22
|
+
export type SidebarSettingsClient = Pick<typeof api, 'settingsGet' | 'settingsUpdate'>
|
|
23
|
+
|
|
24
|
+
/** Validate one raw resolved value into {@link SidebarPrefs}. Used for the
|
|
25
|
+
* settings.get payload AND the settings.update response (both carry the
|
|
26
|
+
* layered resolved value); any malformed field falls back to its default.
|
|
27
|
+
* @param value - the raw resolved section from the settings wire.
|
|
28
|
+
* @returns validated prefs (always well-formed).
|
|
29
|
+
*/
|
|
30
|
+
export function parsePrefs(value: unknown): SidebarPrefs {
|
|
31
|
+
if (value === null || typeof value !== 'object') return { ...SIDEBAR_PREFS_DEFAULTS }
|
|
32
|
+
const record = value as Record<string, unknown>
|
|
33
|
+
return {
|
|
34
|
+
openByDefault: typeof record.openByDefault === 'boolean'
|
|
35
|
+
? record.openByDefault
|
|
36
|
+
: SIDEBAR_PREFS_DEFAULTS.openByDefault,
|
|
37
|
+
defaultWidthPercent: typeof record.defaultWidthPercent === 'number' && Number.isFinite(record.defaultWidthPercent)
|
|
38
|
+
? clampWidthPercent(record.defaultWidthPercent)
|
|
39
|
+
: SIDEBAR_PREFS_DEFAULTS.defaultWidthPercent,
|
|
40
|
+
autoOpenSubagent: typeof record.autoOpenSubagent === 'boolean'
|
|
41
|
+
? record.autoOpenSubagent
|
|
42
|
+
: SIDEBAR_PREFS_DEFAULTS.autoOpenSubagent,
|
|
43
|
+
autoOpenJobs: typeof record.autoOpenJobs === 'boolean'
|
|
44
|
+
? record.autoOpenJobs
|
|
45
|
+
: SIDEBAR_PREFS_DEFAULTS.autoOpenJobs,
|
|
46
|
+
agentTerminalTools: typeof record.agentTerminalTools === 'boolean'
|
|
47
|
+
? record.agentTerminalTools
|
|
48
|
+
: SIDEBAR_PREFS_DEFAULTS.agentTerminalTools,
|
|
49
|
+
bottomPanelAutoTerminal: typeof record.bottomPanelAutoTerminal === 'boolean'
|
|
50
|
+
? record.bottomPanelAutoTerminal
|
|
51
|
+
: SIDEBAR_PREFS_DEFAULTS.bottomPanelAutoTerminal,
|
|
52
|
+
interceptOpenPath: typeof record.interceptOpenPath === 'boolean'
|
|
53
|
+
? record.interceptOpenPath
|
|
54
|
+
: SIDEBAR_PREFS_DEFAULTS.interceptOpenPath,
|
|
55
|
+
htmlViewerNoSandbox: typeof record.htmlViewerNoSandbox === 'boolean'
|
|
56
|
+
? record.htmlViewerNoSandbox
|
|
57
|
+
: SIDEBAR_PREFS_DEFAULTS.htmlViewerNoSandbox,
|
|
58
|
+
htmlViewerDefaultUnsafe: typeof record.htmlViewerDefaultUnsafe === 'boolean'
|
|
59
|
+
? record.htmlViewerDefaultUnsafe
|
|
60
|
+
: SIDEBAR_PREFS_DEFAULTS.htmlViewerDefaultUnsafe,
|
|
61
|
+
browserNoSandbox: typeof record.browserNoSandbox === 'boolean'
|
|
62
|
+
? record.browserNoSandbox
|
|
63
|
+
: SIDEBAR_PREFS_DEFAULTS.browserNoSandbox,
|
|
64
|
+
browserInterceptLinks: typeof record.browserInterceptLinks === 'boolean'
|
|
65
|
+
? record.browserInterceptLinks
|
|
66
|
+
: SIDEBAR_PREFS_DEFAULTS.browserInterceptLinks,
|
|
67
|
+
tabsEnabled: booleanMapOf(record.tabsEnabled),
|
|
68
|
+
viewersEnabled: booleanMapOf(record.viewersEnabled),
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Validate one enable-switch map (per-tab / per-viewer). Only boolean values
|
|
74
|
+
* survive; a non-object or a non-boolean entry falls back to the empty map /
|
|
75
|
+
* drops the entry — an absent key means the feature stays enabled.
|
|
76
|
+
*/
|
|
77
|
+
function booleanMapOf(value: unknown): Record<string, boolean> {
|
|
78
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) return {}
|
|
79
|
+
const out: Record<string, boolean> = {}
|
|
80
|
+
for (const [key, item] of Object.entries(value as Record<string, unknown>)) {
|
|
81
|
+
if (typeof item === 'boolean') out[key] = item
|
|
82
|
+
}
|
|
83
|
+
return out
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Read the resolved side card preferences through the plugin's settings route.
|
|
88
|
+
* @param settings - the settings wire face (the plugin api by default).
|
|
89
|
+
* @returns validated prefs, or the schema defaults when the route rejects,
|
|
90
|
+
* the namespace is absent, or a stored value violates the contract.
|
|
91
|
+
*/
|
|
92
|
+
export async function loadPrefs(settings: SidebarSettingsClient): Promise<SidebarPrefs> {
|
|
93
|
+
try {
|
|
94
|
+
const view = await settings.settingsGet()
|
|
95
|
+
return parsePrefs(view.value)
|
|
96
|
+
} catch {
|
|
97
|
+
// Transport/fence rejection or a malformed response: keep the defaults.
|
|
98
|
+
return { ...SIDEBAR_PREFS_DEFAULTS }
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure derivation of one turn's produced files from finalized conversation
|
|
3
|
+
* nodes — a structural replica of ui-deliverables' `producedForClosing`
|
|
4
|
+
* (the mutation tools' follow-along `locations`, by render intent: a diff
|
|
5
|
+
* card or a generic edit card; reads/deletes/failures produce nothing).
|
|
6
|
+
* Kept dependency-free so the takeover logic is unit-testable and the
|
|
7
|
+
* replica is easy to diff against upstream when it drifts.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** Paths a tool-result view reports as produced, by render intent. */
|
|
11
|
+
export function producedPaths(view: unknown): readonly string[] {
|
|
12
|
+
if (view === null || typeof view !== 'object') return []
|
|
13
|
+
const record = view as { card?: unknown; kind?: unknown; locations?: unknown }
|
|
14
|
+
const isEdit = record.card === 'diff' || (record.card === 'generic' && record.kind === 'edit')
|
|
15
|
+
if (!isEdit) return []
|
|
16
|
+
if (!Array.isArray(record.locations)) return []
|
|
17
|
+
const paths: string[] = []
|
|
18
|
+
for (const location of record.locations) {
|
|
19
|
+
if (location !== null && typeof location === 'object' && typeof (location as { path?: unknown }).path === 'string') {
|
|
20
|
+
paths.push((location as { path: string }).path)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return paths
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Files produced by the turn the assistant at `seq` closes. Accumulation
|
|
28
|
+
* resets on turn boundaries (a user message, or a node reporting a different
|
|
29
|
+
* turn number); paths keep first-seen order and appear once.
|
|
30
|
+
* @param nodes - snapshot nodes in surface order (structural, unknown-safe).
|
|
31
|
+
* @param seq - the closing assistant's seq (the render site's anchor).
|
|
32
|
+
* @returns produced paths; empty when the turn wrote nothing.
|
|
33
|
+
*/
|
|
34
|
+
export function producedForClosing(nodes: readonly unknown[], seq: number): readonly string[] {
|
|
35
|
+
let pending: string[] = []
|
|
36
|
+
let seen = new Set<string>()
|
|
37
|
+
let turn: number | undefined
|
|
38
|
+
for (const node of nodes) {
|
|
39
|
+
if (node === null || typeof node !== 'object') continue
|
|
40
|
+
const record = node as { kind?: unknown; isError?: unknown; callView?: unknown; turn?: unknown; seq?: unknown }
|
|
41
|
+
if (record.kind === 'tool-result') {
|
|
42
|
+
if (record.isError === true) continue
|
|
43
|
+
for (const path of producedPaths(record.callView)) {
|
|
44
|
+
if (seen.has(path)) continue
|
|
45
|
+
seen.add(path)
|
|
46
|
+
pending.push(path)
|
|
47
|
+
}
|
|
48
|
+
continue
|
|
49
|
+
}
|
|
50
|
+
if (record.kind === 'user') {
|
|
51
|
+
turn = undefined
|
|
52
|
+
pending = []
|
|
53
|
+
seen = new Set()
|
|
54
|
+
} else if (typeof record.turn === 'number') {
|
|
55
|
+
if (turn !== undefined && record.turn !== turn) {
|
|
56
|
+
pending = []
|
|
57
|
+
seen = new Set()
|
|
58
|
+
}
|
|
59
|
+
turn = record.turn
|
|
60
|
+
}
|
|
61
|
+
if (record.kind === 'assistant' && record.seq === seq) return pending
|
|
62
|
+
}
|
|
63
|
+
return []
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Claim the turn-tail chain only when the closing turn produced files.
|
|
68
|
+
* @param owner - the turn-tail owner currency ({nodes, seq}).
|
|
69
|
+
* @returns produced paths as the matched value, or null to decline.
|
|
70
|
+
*/
|
|
71
|
+
export function selectProducedFiles(owner: unknown): readonly string[] | null {
|
|
72
|
+
const record = owner as { nodes?: unknown; seq?: unknown } | null
|
|
73
|
+
if (record === null || typeof record !== 'object') return null
|
|
74
|
+
if (!Array.isArray(record.nodes) || typeof record.seq !== 'number') return null
|
|
75
|
+
const paths = producedForClosing(record.nodes, record.seq)
|
|
76
|
+
return paths.length === 0 ? null : paths
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Resolve a (possibly relative) path against the session cwd for the sidebar. */
|
|
80
|
+
export function resolveSidebarPath(cwd: string | undefined, path: string): string {
|
|
81
|
+
const absolute = path.startsWith('/') || /^[A-Za-z]:[\\/]/.test(path)
|
|
82
|
+
if (absolute) return path
|
|
83
|
+
const base = cwd ?? ''
|
|
84
|
+
if (base === '') return path
|
|
85
|
+
const separator = base.includes('\\') ? '\\' : '/'
|
|
86
|
+
return `${base.replace(/[\\/]+$/, '')}${separator}${path}`
|
|
87
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure payload builders for the "add selection to conversation" popup in the
|
|
3
|
+
* text viewers (markdown preview + the catch-all code viewer). Everything
|
|
4
|
+
* here is string math — no React, no ctx — so the unit tests cover it
|
|
5
|
+
* directly.
|
|
6
|
+
*
|
|
7
|
+
* Insert shape (agreed with the product owner):
|
|
8
|
+
* - Selection ≤ SELECTION_LIMIT characters: a fenced code block whose info
|
|
9
|
+
* line is `相对路径:起止行` and whose body is the selected text.
|
|
10
|
+
* - Selection over the limit: a single plain-text line `相对路径:起止行`
|
|
11
|
+
* (no fence, no content).
|
|
12
|
+
* - The path is relative to the session cwd (the same projection the
|
|
13
|
+
* explorer's @ button uses); an unknown cwd falls back to the absolute
|
|
14
|
+
* path.
|
|
15
|
+
* - Line numbers: single-line selections write `path:12`, multi-line write
|
|
16
|
+
* `path:12-15`. The markdown preview cannot map rendered DOM back to
|
|
17
|
+
* source lines directly, so it reverse-searches the selected text in the
|
|
18
|
+
* source and only reports lines on an unambiguous hit (see
|
|
19
|
+
* {@link linesOfSelection}).
|
|
20
|
+
*/
|
|
21
|
+
import { relativeTo } from './paths.ts'
|
|
22
|
+
|
|
23
|
+
/** Max inserted selection length (UTF-16 code units, i.e. JS `.length`). */
|
|
24
|
+
export const SELECTION_LIMIT = 500
|
|
25
|
+
|
|
26
|
+
/** The source line span a selection maps to (1-based, inclusive). */
|
|
27
|
+
export interface SelectionLines {
|
|
28
|
+
start: number
|
|
29
|
+
end: number
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The fence info line: `rel[:start[-end]]` — lines are omitted entirely
|
|
34
|
+
* when unknown (the preview reverse-search missed).
|
|
35
|
+
*/
|
|
36
|
+
export function headerOf(path: string, cwd: string | undefined, lines?: SelectionLines): string {
|
|
37
|
+
const rel = cwd !== undefined ? relativeTo(cwd, path) : path
|
|
38
|
+
if (lines === undefined) return rel
|
|
39
|
+
if (lines.end > lines.start) return `${rel}:${lines.start}-${lines.end}`
|
|
40
|
+
return `${rel}:${lines.start}`
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The full text appended to the composer draft for one selection.
|
|
45
|
+
* Over the limit the content is dropped: the plain path line is the whole
|
|
46
|
+
* payload (an empty fenced block would just occupy the draft).
|
|
47
|
+
*/
|
|
48
|
+
export function buildSelectionInsert(
|
|
49
|
+
path: string,
|
|
50
|
+
cwd: string | undefined,
|
|
51
|
+
lines: SelectionLines | undefined,
|
|
52
|
+
selected: string,
|
|
53
|
+
): string {
|
|
54
|
+
const header = headerOf(path, cwd, lines)
|
|
55
|
+
if (selected.length > SELECTION_LIMIT) return header
|
|
56
|
+
return `\`\`\`${header}\n${selected}\n\`\`\``
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** 1-based line number of a character index in a text. */
|
|
60
|
+
function lineAt(source: string, index: number): number {
|
|
61
|
+
let line = 1
|
|
62
|
+
for (let i = 0; i < index && i < source.length; i++) {
|
|
63
|
+
if (source[i] === '\n') line++
|
|
64
|
+
}
|
|
65
|
+
return line
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Reverse-map a rendered-DOM selection back to source line numbers. The
|
|
70
|
+
* preview selection is plain text (block boundaries come out as `\n`), so
|
|
71
|
+
* this is a best-effort substring search: a single trailing newline is
|
|
72
|
+
* stripped first (DOM block selections tend to carry one), and only an
|
|
73
|
+
* EXACTLY-ONE occurrence yields lines — an ambiguous or missing match
|
|
74
|
+
* returns null (the header then carries the path without line numbers).
|
|
75
|
+
*/
|
|
76
|
+
export function linesOfSelection(source: string, selected: string): SelectionLines | null {
|
|
77
|
+
const text = selected.endsWith('\n') ? selected.slice(0, -1) : selected
|
|
78
|
+
if (text === '') return null
|
|
79
|
+
const at = source.indexOf(text)
|
|
80
|
+
if (at === -1) return null
|
|
81
|
+
if (source.indexOf(text, at + 1) !== -1) return null
|
|
82
|
+
return {
|
|
83
|
+
start: lineAt(source, at),
|
|
84
|
+
end: lineAt(source, at + Math.max(text.length - 1, 0)),
|
|
85
|
+
}
|
|
86
|
+
}
|