comfy-ts 0.2.0 → 0.4.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 +363 -0
- package/dist/cli.js +764 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.cjs +4171 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +9910 -0
- package/dist/index.d.ts +9894 -58
- package/dist/index.js +4092 -2
- package/dist/index.js.map +1 -1
- package/dist/log-CdfgILde.js +10 -0
- package/dist/log-CdfgILde.js.map +1 -0
- package/dist/run-tui-SDSJo1Qf.js +3896 -0
- package/dist/run-tui-SDSJo1Qf.js.map +1 -0
- package/package.json +103 -75
- package/src/cli/comfy-ts-cli.ts +36 -0
- package/src/cli/gen.ts +47 -0
- package/src/cli/outline.ts +82 -0
- package/src/cli/tui/components/DraftsOverlay.tsx +44 -0
- package/src/cli/tui/components/EditField.tsx +27 -0
- package/src/cli/tui/components/Header.tsx +56 -0
- package/src/cli/tui/components/HostPanel.tsx +37 -0
- package/src/cli/tui/components/LogsPanel.tsx +23 -0
- package/src/cli/tui/components/LorasOverlay.tsx +49 -0
- package/src/cli/tui/components/OverlayList.tsx +25 -0
- package/src/cli/tui/components/PickerOverlays.tsx +66 -0
- package/src/cli/tui/components/PreviewPanel.tsx +78 -0
- package/src/cli/tui/components/PromptOverlay.tsx +16 -0
- package/src/cli/tui/components/StatusBar.tsx +171 -0
- package/src/cli/tui/components/TextOverlay.tsx +72 -0
- package/src/cli/tui/components/TreePanel.tsx +51 -0
- package/src/cli/tui/components/TuiApp.tsx +246 -0
- package/src/cli/tui/components/VarsPanel.tsx +86 -0
- package/src/cli/tui/findDefinedWorkflow.ts +16 -0
- package/src/cli/tui/keys.ts +20 -0
- package/src/cli/tui/protocolImagePainter.ts +95 -0
- package/src/cli/tui/run-tui.tsx +76 -0
- package/src/cli/tui/state/DraftsSt.ts +273 -0
- package/src/cli/tui/state/EditorSt.ts +289 -0
- package/src/cli/tui/state/ExecSt.ts +173 -0
- package/src/cli/tui/state/HostSt.ts +230 -0
- package/src/cli/tui/state/LogsSt.ts +107 -0
- package/src/cli/tui/state/LorasSt.ts +185 -0
- package/src/cli/tui/state/PickerSt.ts +112 -0
- package/src/cli/tui/state/PreviewSt.ts +288 -0
- package/src/cli/tui/state/QueueSt.ts +59 -0
- package/src/cli/tui/state/SettingsSt.ts +100 -0
- package/src/cli/tui/state/TreeSt.ts +122 -0
- package/src/cli/tui/state/TuiSt.ts +175 -0
- package/src/cli/tui/state/WorkflowsSt.ts +93 -0
- package/src/cli/tui-env.ts +8 -0
- package/src/errors/InvalidPromptError.ts +12 -0
- package/src/graph/ComfyNode.ts +403 -0
- package/src/graph/ComfyNodeID.ts +28 -0
- package/src/graph/ComfyNodeOutput.ts +16 -0
- package/src/graph/NodeSlotSize.ts +4 -0
- package/src/graph/autoValue.ts +17 -0
- package/src/host/ComfyHost.ts +690 -0
- package/src/host/ComfyInstallType.ts +15 -0
- package/src/host/ComfyManager.ts +232 -0
- package/src/host/ComfyUploader.ts +110 -0
- package/src/host/ComfyWorkflowBuilder.ts +60 -0
- package/src/host/Requirements.ts +55 -0
- package/src/host/ResilientWebsocket.ts +147 -0
- package/src/host/loraManagerApi.ts +98 -0
- package/src/image-utils/_getPngMetadata.ts +53 -0
- package/src/image-utils/_getWebpMetadata.ts +38 -0
- package/src/image-utils/_parseExifData.ts +53 -0
- package/src/index.ts +89 -0
- package/src/litegraph/LiteGraphGroup.ts +22 -0
- package/src/litegraph/LiteGraphJSON.ts +46 -0
- package/src/litegraph/LiteGraphLink.ts +23 -0
- package/src/litegraph/LiteGraphLinkID.ts +6 -0
- package/src/litegraph/LiteGraphNode.ts +69 -0
- package/src/litegraph/LiteGraphNodeInput.ts +27 -0
- package/src/litegraph/LiteGraphNodeOutput.ts +23 -0
- package/src/litegraph/LiteGraphSlotIndex.ts +6 -0
- package/src/litegraph/convertFlowToLiteGraphJSON.ts +114 -0
- package/src/manager/ComfyRegistry.ts +124 -0
- package/src/manager/generated/KnownComfyCustomNodeName.ts +19280 -0
- package/src/manager/generated/KnownComfyPluginTitle.ts +5130 -0
- package/src/manager/generated/KnownComfyPluginURL.ts +2589 -0
- package/src/manager/generated/KnownModel_Base.ts +126 -0
- package/src/manager/generated/KnownModel_FileName.ts +2287 -0
- package/src/manager/generated/KnownModel_Name.ts +2287 -0
- package/src/manager/generated/KnownModel_SavePath.ts +263 -0
- package/src/manager/generated/KnownModel_Type.ts +97 -0
- package/src/manager/json/alter-list.json +224 -0
- package/src/manager/json/custom-node-list.extra.ts +13 -0
- package/src/manager/json/custom-node-list.json +21948 -0
- package/src/manager/json/extension-node-map.json +31282 -0
- package/src/manager/json/github-stats.json +12602 -0
- package/src/manager/json/model-list.extra.ts +26 -0
- package/src/manager/json/model-list.json +5052 -0
- package/src/manager/loaders/step1_downloadComfyManagerJSONs.ts +56 -0
- package/src/manager/loaders/step2_custom-node-list-loader.ts +149 -0
- package/src/manager/loaders/step3_extension-node-map-loader.ts +189 -0
- package/src/manager/loaders/step4_model-list-loader.ts +159 -0
- package/src/manager/script-update-manager.bun.ts +18 -0
- package/src/manager/types/ComfyManagerAPIFetchPolicy.ts +9 -0
- package/src/manager/types/ComfyManagerAPIModelList.ts +10 -0
- package/src/manager/types/ComfyManagerAPIPluginList.ts +11 -0
- package/src/manager/types/ComfyManagerFileModelInfo.ts +5 -0
- package/src/manager/types/ComfyManagerFilePluginContent.ts +13 -0
- package/src/manager/types/ComfyManagerFilePluginList.ts +5 -0
- package/src/manager/types/ComfyManagerModelInfo.ts +18 -0
- package/src/manager/types/ComfyManagerPluginContentMetadata.ts +16 -0
- package/src/manager/types/ComfyManagerPluginEnums.ts +8 -0
- package/src/manager/types/ComfyManagerPluginInfo.ts +50 -0
- package/src/runner/ComfyExecution.ts +301 -0
- package/src/runner/ComfyExecutionStatus.ts +2 -0
- package/src/runner/ComfyWorkflow.ts +395 -0
- package/src/runner/ComfyWorkflowLayout.ts +96 -0
- package/src/runner/ComfyWsApi.ts +170 -0
- package/src/runner/MediaImage.ts +262 -0
- package/src/sdk-generator/ComfyNodeSchema.ts +79 -0
- package/src/sdk-generator/ComfySchema.ts +369 -0
- package/src/sdk-generator/ComfyUIObjectInfoTypes.ts +95 -0
- package/src/sdk-generator/Primitives.ts +76 -0
- package/src/sdk-generator/_getUnionNameBasedOnFirstFoundEnumName.ts +39 -0
- package/src/sdk-generator/_pythonModuleToNamespace.ts +27 -0
- package/src/sdk-generator/_toQualifiedNodeKey.ts +5 -0
- package/src/sdk-generator/comfy-api-json.ts +16 -0
- package/src/sdk-generator/comfyui-sdk-codegen.ts +287 -0
- package/src/sdk-generator/comfyui-types.ts +56 -0
- package/src/sdk-generator/litegraphToApiRequestPayload.ts +157 -0
- package/src/ssh-host-manager/ssh-config-manager.ts +89 -0
- package/src/ssh-host-manager/ssh-config.windows.md +32 -0
- package/src/ssh-host-manager/ssh-manager.ts +114 -0
- package/src/state.ts +136 -0
- package/src/types/comfy-sdk.ts +106 -0
- package/src/types/index.ts +55 -0
- package/src/utils/CodeBuffer.ts +61 -0
- package/src/utils/ComfyColors.ts +38 -0
- package/src/utils/ansi.ts +33 -0
- package/src/utils/ansiImage.ts +36 -0
- package/src/utils/bang.ts +39 -0
- package/src/utils/deepCopyNaive.ts +1 -0
- package/src/utils/downloadFile.ts +77 -0
- package/src/utils/escapeJSKey.ts +32 -0
- package/src/utils/exhaust.ts +4 -0
- package/src/utils/extractErrorMessage.ts +11 -0
- package/src/utils/fuzzyMatch.ts +8 -0
- package/src/utils/githubRegexes.ts +2 -0
- package/src/utils/hashArrayBuffer.ts +6 -0
- package/src/utils/log.ts +9 -0
- package/src/utils/loraTag.ts +10 -0
- package/src/utils/mojibake.ts +60 -0
- package/src/utils/printArkResultInConsole.ts +16 -0
- package/src/utils/softValidate.ts +8 -0
- package/src/utils/stringifyReadable.ts +32 -0
- package/src/utils/toposort.ts +59 -0
- package/src/utils/tryExtracting.ts +41 -0
- package/src/utils/writeFile.ts +16 -0
- package/src/vars/ComfyVars.ts +531 -0
- package/src/vars/DefinedWorkflow.ts +112 -0
- package/src/vars/loraKeywords.ts +56 -0
- package/dist/index.d.mts +0 -74
- package/dist/index.mjs +0 -3
- package/dist/index.mjs.map +0 -1
- package/readme.md +0 -40
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import {
|
|
2
|
+
copyFileSync,
|
|
3
|
+
existsSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
readdirSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
renameSync,
|
|
8
|
+
rmSync,
|
|
9
|
+
writeFileSync,
|
|
10
|
+
} from 'node:fs'
|
|
11
|
+
import { makeAutoObservable, reaction } from 'mobx'
|
|
12
|
+
import { basename, dirname, join } from 'pathe'
|
|
13
|
+
import { extractErrorMessage } from 'src/utils/extractErrorMessage.ts'
|
|
14
|
+
import type { TuiMode, TuiSt } from 'src/cli/tui/state/TuiSt.ts'
|
|
15
|
+
|
|
16
|
+
/** module basename (`01-txt2img`) keys the draft folder — knowable for EVERY tree file without importing it */
|
|
17
|
+
export function draftKeyForFile(file: string): string {
|
|
18
|
+
return basename(file).replace(/\.cflow\.tsx?$/, '')
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function draftsDirForFile(file: string): string {
|
|
22
|
+
return comfyts.resolveFromDrafts(draftKeyForFile(file))
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function listDraftsInDir(dir: string): string[] {
|
|
26
|
+
if (!existsSync(dir)) return []
|
|
27
|
+
return readdirSync(dir)
|
|
28
|
+
.filter((f) => f.endsWith('.json'))
|
|
29
|
+
.map((f) => f.slice(0, -'.json'.length))
|
|
30
|
+
.sort()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** draft names stored for a workflow module (fs read — pair with DraftsSt.version for reactivity) */
|
|
34
|
+
export function listDraftsForFile(file: string): string[] {
|
|
35
|
+
return listDraftsInDir(draftsDirForFile(file))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* named var-value snapshots at `.comfy-ts/drafts/<module-basename>/<name>.json`
|
|
40
|
+
* (LOCAL state, gitignored). The active draft autosaves debounced; the TUI is
|
|
41
|
+
* ALWAYS in a draft (`default` unless another one is activated).
|
|
42
|
+
*/
|
|
43
|
+
export class DraftsSt {
|
|
44
|
+
constructor(private st: TuiSt) {
|
|
45
|
+
makeAutoObservable<DraftsSt, 'st'>(this, { st: false })
|
|
46
|
+
// active-draft autosave: var values + draft name are observable
|
|
47
|
+
this.st.disposers.push(
|
|
48
|
+
reaction(
|
|
49
|
+
() => (this.active == null ? null : JSON.stringify(this.snapshot())),
|
|
50
|
+
(json) => {
|
|
51
|
+
if (json != null && this.active != null) this.write(this.active, json)
|
|
52
|
+
},
|
|
53
|
+
{ delay: 300 },
|
|
54
|
+
),
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** ALWAYS set after construction; null only transiently while activate() loads values */
|
|
59
|
+
active: string | null = null
|
|
60
|
+
list: string[] = []
|
|
61
|
+
ix: number = 0
|
|
62
|
+
/** bumped on every fs write/rename so fs-derived getters (tree rows) recompute */
|
|
63
|
+
version: number = 0
|
|
64
|
+
|
|
65
|
+
private get dir(): string {
|
|
66
|
+
const cur = this.st.workflows.currentPath
|
|
67
|
+
return cur != null ? draftsDirForFile(cur) : comfyts.resolveFromDrafts(this.st.wf.spec.id ?? 'workflow')
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private snapshot(): Record<string, unknown> {
|
|
71
|
+
// toJSON so seed persists {mode, value}; other vars round-trip their value
|
|
72
|
+
return Object.fromEntries(this.st.entries.map(([k, varDef]) => [k, varDef.toJSON()]))
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private write(name: string, json: string): void {
|
|
76
|
+
const path = join(this.dir, `${name}.json`)
|
|
77
|
+
mkdirSync(dirname(path), { recursive: true })
|
|
78
|
+
writeFileSync(path, json)
|
|
79
|
+
this.version++
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** module key of the currently loaded workflow (settings.lastDraft is keyed by it) */
|
|
83
|
+
private get moduleKey(): string {
|
|
84
|
+
const cur = this.st.workflows.currentPath
|
|
85
|
+
return cur != null ? draftKeyForFile(cur) : (this.st.wf.spec.id ?? 'workflow')
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** switch to a draft: load its stored values when the file exists, else it starts from current values */
|
|
89
|
+
activate(name: string): void {
|
|
90
|
+
this.active = null // the autosave expression must not see old values under the new name
|
|
91
|
+
const path = join(this.dir, `${name}.json`)
|
|
92
|
+
if (existsSync(path)) {
|
|
93
|
+
try {
|
|
94
|
+
const values: Record<string, unknown> = JSON.parse(readFileSync(path, 'utf8'))
|
|
95
|
+
for (const [k, varDef] of this.st.entries) if (k in values) varDef.loadJSON(values[k])
|
|
96
|
+
} catch (e) {
|
|
97
|
+
this.st.exec.error = extractErrorMessage(e)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
this.active = name
|
|
101
|
+
this.st.settings.rememberDraft(this.moduleKey, name)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** open the draft this workflow was last in (settings), else 'default' */
|
|
105
|
+
activateRemembered(): void {
|
|
106
|
+
const remembered = this.st.settings.lastDraft[this.moduleKey]
|
|
107
|
+
if (remembered != null && remembered !== 'default' && existsSync(join(this.dir, `${remembered}.json`)))
|
|
108
|
+
this.activate(remembered)
|
|
109
|
+
else this.activate('default')
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** every workflow starts in a draft; kept for callers that want 'default' explicitly */
|
|
113
|
+
activateDefault(): void {
|
|
114
|
+
this.activate('default')
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// ---- draft CRUD (also drives the tree's draft rows — `file` overrides the target workflow) ----
|
|
118
|
+
|
|
119
|
+
private dirFor(file: string | undefined): string {
|
|
120
|
+
return file != null ? draftsDirForFile(file) : this.dir
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** true when ops on `file` touch the ACTIVE draft bookkeeping */
|
|
124
|
+
private isCurrent(file: string | undefined): boolean {
|
|
125
|
+
return file == null || file === this.st.workflows.currentPath
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private validName(rawName: string): string | null {
|
|
129
|
+
const name = rawName.trim()
|
|
130
|
+
if (/^[\w][\w .-]*$/.test(name)) return name
|
|
131
|
+
this.st.exec.notice = `invalid draft name: '${rawName}'`
|
|
132
|
+
return null
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** create a NEW draft from the current var values (current workflow only) */
|
|
136
|
+
createNamed(rawName: string): boolean {
|
|
137
|
+
const name = this.validName(rawName)
|
|
138
|
+
if (name == null) return false
|
|
139
|
+
if (existsSync(join(this.dir, `${name}.json`))) {
|
|
140
|
+
this.st.exec.notice = `draft '${name}' already exists`
|
|
141
|
+
return false
|
|
142
|
+
}
|
|
143
|
+
this.write(name, JSON.stringify(this.snapshot(), null, 2))
|
|
144
|
+
this.active = name
|
|
145
|
+
this.st.settings.rememberDraft(this.moduleKey, name)
|
|
146
|
+
this.st.exec.notice = `new draft: ${name} (autosaves)`
|
|
147
|
+
return true
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
rename(oldName: string, rawName: string, file?: string): boolean {
|
|
151
|
+
const newName = this.validName(rawName)
|
|
152
|
+
if (newName == null) return false
|
|
153
|
+
if (newName === oldName) return true
|
|
154
|
+
const dir = this.dirFor(file)
|
|
155
|
+
const to = join(dir, `${newName}.json`)
|
|
156
|
+
if (existsSync(to)) {
|
|
157
|
+
this.st.exec.notice = `draft '${newName}' already exists`
|
|
158
|
+
return false
|
|
159
|
+
}
|
|
160
|
+
const from = join(dir, `${oldName}.json`)
|
|
161
|
+
if (existsSync(from)) renameSync(from, to)
|
|
162
|
+
else if (this.isCurrent(file)) this.write(newName, JSON.stringify(this.snapshot(), null, 2))
|
|
163
|
+
if (this.isCurrent(file) && this.active === oldName) {
|
|
164
|
+
this.active = newName
|
|
165
|
+
this.st.settings.rememberDraft(this.moduleKey, newName)
|
|
166
|
+
}
|
|
167
|
+
this.version++
|
|
168
|
+
this.st.exec.notice = `draft renamed: ${oldName} → ${newName}`
|
|
169
|
+
return true
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
duplicate(name: string, rawName: string, file?: string): boolean {
|
|
173
|
+
const newName = this.validName(rawName)
|
|
174
|
+
if (newName == null) return false
|
|
175
|
+
const dir = this.dirFor(file)
|
|
176
|
+
const from = join(dir, `${name}.json`)
|
|
177
|
+
const to = join(dir, `${newName}.json`)
|
|
178
|
+
if (!existsSync(from)) {
|
|
179
|
+
this.st.exec.notice = `draft '${name}' has no file yet (edit a var first)`
|
|
180
|
+
return false
|
|
181
|
+
}
|
|
182
|
+
if (existsSync(to)) {
|
|
183
|
+
this.st.exec.notice = `draft '${newName}' already exists`
|
|
184
|
+
return false
|
|
185
|
+
}
|
|
186
|
+
copyFileSync(from, to)
|
|
187
|
+
this.version++
|
|
188
|
+
this.st.exec.notice = `draft duplicated: ${name} → ${newName}`
|
|
189
|
+
return true
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
deleteDraft(name: string, file?: string): void {
|
|
193
|
+
const path = join(this.dirFor(file), `${name}.json`)
|
|
194
|
+
if (existsSync(path)) rmSync(path)
|
|
195
|
+
this.version++
|
|
196
|
+
// deleting the draft you are IN falls back to default
|
|
197
|
+
if (this.isCurrent(file) && this.active === name) this.activateDefault()
|
|
198
|
+
this.st.exec.notice = `draft deleted: ${name}`
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// ---- name prompts (ONE mechanism: EditorSt custom session → PromptOverlay) ----
|
|
202
|
+
|
|
203
|
+
promptNew(returnMode: TuiMode = 'nav'): void {
|
|
204
|
+
this.st.editor.beginCustom({
|
|
205
|
+
title: 'new draft (from current values)',
|
|
206
|
+
initial: '',
|
|
207
|
+
onCommit: (raw) => {
|
|
208
|
+
const ok = this.createNamed(raw)
|
|
209
|
+
if (ok && returnMode === 'overlay-drafts') this.begin()
|
|
210
|
+
return ok
|
|
211
|
+
},
|
|
212
|
+
returnMode,
|
|
213
|
+
})
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
promptRename(name: string, file?: string, returnMode: TuiMode = 'nav'): void {
|
|
217
|
+
this.st.editor.beginCustom({
|
|
218
|
+
title: `rename draft '${name}'`,
|
|
219
|
+
initial: name,
|
|
220
|
+
onCommit: (raw) => {
|
|
221
|
+
const ok = this.rename(name, raw, file)
|
|
222
|
+
if (ok && returnMode === 'overlay-drafts') this.begin()
|
|
223
|
+
return ok
|
|
224
|
+
},
|
|
225
|
+
returnMode,
|
|
226
|
+
})
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
promptDuplicate(name: string, file?: string, returnMode: TuiMode = 'nav'): void {
|
|
230
|
+
this.st.editor.beginCustom({
|
|
231
|
+
title: `duplicate draft '${name}'`,
|
|
232
|
+
initial: `${name}-copy`,
|
|
233
|
+
onCommit: (raw) => {
|
|
234
|
+
const ok = this.duplicate(name, raw, file)
|
|
235
|
+
if (ok && returnMode === 'overlay-drafts') this.begin()
|
|
236
|
+
return ok
|
|
237
|
+
},
|
|
238
|
+
returnMode,
|
|
239
|
+
})
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** f2 in nav: rename the ACTIVE draft */
|
|
243
|
+
beginRename(): void {
|
|
244
|
+
this.promptRename(this.active ?? 'default')
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
begin(): void {
|
|
248
|
+
this.list = listDraftsInDir(this.dir)
|
|
249
|
+
this.st.mode = 'overlay-drafts'
|
|
250
|
+
const ix = this.active == null ? -1 : this.list.indexOf(this.active)
|
|
251
|
+
// row 0 = "new draft", rows 1.. = existing drafts
|
|
252
|
+
this.ix = ix === -1 ? 0 : ix + 1
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
move(delta: number): void {
|
|
256
|
+
const len = this.list.length + 1
|
|
257
|
+
this.ix = (this.ix + delta + len) % len
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** draft name under the overlay cursor (row 0 is 'new') */
|
|
261
|
+
get selectedName(): string | null {
|
|
262
|
+
return this.ix === 0 ? null : (this.list[this.ix - 1] ?? null)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
commit(): void {
|
|
266
|
+
if (this.ix === 0) return this.promptNew('overlay-drafts')
|
|
267
|
+
const name = this.selectedName
|
|
268
|
+
if (name == null) return
|
|
269
|
+
this.activate(name)
|
|
270
|
+
this.st.exec.notice = `draft: ${name} (autosaves)`
|
|
271
|
+
this.st.mode = 'nav'
|
|
272
|
+
}
|
|
273
|
+
}
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { makeAutoObservable } from 'mobx'
|
|
2
|
+
import type { TuiMode, TuiSt } from 'src/cli/tui/state/TuiSt.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* line/multiline editor. Inline mode ('edit') escapes newlines into a single
|
|
6
|
+
* line (numbers only, in practice); multiline mode ('overlay-text') keeps REAL
|
|
7
|
+
* newlines. All ops are code-point based so emoji/astral input never splits a
|
|
8
|
+
* surrogate pair (flag/ZWJ clusters may still count as several points).
|
|
9
|
+
*/
|
|
10
|
+
export class EditorSt {
|
|
11
|
+
constructor(private st: TuiSt) {
|
|
12
|
+
makeAutoObservable<EditorSt, 'st'>(this, { st: false })
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
buffer: string = ''
|
|
16
|
+
/** caret position, counted in CODE POINTS (0..pointLen) */
|
|
17
|
+
cursor: number = 0
|
|
18
|
+
invalid: boolean = false
|
|
19
|
+
|
|
20
|
+
/** raw value -> escaped single-line buffer form (order matters: backslashes first) */
|
|
21
|
+
static escapeForBuffer(raw: string): string {
|
|
22
|
+
return raw.replaceAll('\\', '\\\\').replaceAll('\r\n', '\n').replaceAll('\r', '\n').replaceAll('\n', '\\n')
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** escaped buffer form -> raw value */
|
|
26
|
+
static unescapeBuffer(buffer: string): string {
|
|
27
|
+
return buffer.replace(/\\\\|\\n/g, (m) => (m === '\\\\' ? '\\' : '\n'))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** non-var session (draft naming etc.): commit goes to the callback, not the selected var */
|
|
31
|
+
customCommit: ((raw: string) => boolean) | null = null
|
|
32
|
+
customTitle: string = ''
|
|
33
|
+
private returnMode: TuiMode = 'nav'
|
|
34
|
+
|
|
35
|
+
get isCustom(): boolean {
|
|
36
|
+
return this.customCommit != null
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** inline single-line editor (numbers, seed `mode value`) */
|
|
40
|
+
beginInline(): void {
|
|
41
|
+
const sel = this.st.selected?.[1]
|
|
42
|
+
if (sel == null) return
|
|
43
|
+
this.st.mode = 'edit'
|
|
44
|
+
this.buffer = EditorSt.escapeForBuffer(sel.toEditBuffer())
|
|
45
|
+
this.cursor = [...this.buffer].length
|
|
46
|
+
this.invalid = false
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** modal prompt over an arbitrary string (rendered as PromptOverlay);
|
|
50
|
+
* onCommit returns false to keep editing (invalid); esc/apply lands on returnMode */
|
|
51
|
+
beginCustom(p: { title: string; initial: string; onCommit: (raw: string) => boolean; returnMode?: TuiMode }): void {
|
|
52
|
+
this.st.mode = 'edit'
|
|
53
|
+
this.customCommit = p.onCommit
|
|
54
|
+
this.customTitle = p.title
|
|
55
|
+
this.returnMode = p.returnMode ?? 'nav'
|
|
56
|
+
this.buffer = EditorSt.escapeForBuffer(p.initial)
|
|
57
|
+
this.cursor = [...this.buffer].length
|
|
58
|
+
this.invalid = false
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** modal multiline editor (text vars), no escaping */
|
|
62
|
+
beginMultiline(): void {
|
|
63
|
+
const sel = this.st.selected?.[1]
|
|
64
|
+
if (sel == null) return
|
|
65
|
+
this.st.mode = 'overlay-text'
|
|
66
|
+
this.buffer = String(sel.value)
|
|
67
|
+
this.cursor = [...this.buffer].length
|
|
68
|
+
this.invalid = false
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
commitInline(): void {
|
|
72
|
+
if (this.customCommit != null) {
|
|
73
|
+
if (this.customCommit(EditorSt.unescapeBuffer(this.buffer))) {
|
|
74
|
+
this.customCommit = null
|
|
75
|
+
this.st.mode = this.returnMode
|
|
76
|
+
} else this.invalid = true
|
|
77
|
+
return
|
|
78
|
+
}
|
|
79
|
+
const sel = this.st.selected?.[1]
|
|
80
|
+
if (sel == null) return
|
|
81
|
+
if (sel.parse(EditorSt.unescapeBuffer(this.buffer))) this.st.mode = 'nav'
|
|
82
|
+
else this.invalid = true
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
commitMultiline(): void {
|
|
86
|
+
const sel = this.st.selected?.[1]
|
|
87
|
+
if (sel == null) return
|
|
88
|
+
if (sel.parse(this.buffer)) this.st.mode = 'nav'
|
|
89
|
+
else this.invalid = true
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
cancel(): void {
|
|
93
|
+
this.st.mode = this.customCommit != null ? this.returnMode : 'nav'
|
|
94
|
+
this.invalid = false
|
|
95
|
+
this.customCommit = null
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// ---- readline-style ops (code-point based) ----
|
|
99
|
+
private get chars(): string[] {
|
|
100
|
+
return [...this.buffer]
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private setChars(cs: string[], cursor: number): void {
|
|
104
|
+
this.buffer = cs.join('')
|
|
105
|
+
this.cursor = Math.max(0, Math.min(cursor, cs.length))
|
|
106
|
+
this.invalid = false
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
input(chunk: string): void {
|
|
110
|
+
// multiline keeps real newlines; inline escapes to a single line
|
|
111
|
+
const normalized =
|
|
112
|
+
this.st.mode === 'overlay-text'
|
|
113
|
+
? chunk.replaceAll('\r\n', '\n').replaceAll('\r', '\n')
|
|
114
|
+
: EditorSt.escapeForBuffer(chunk)
|
|
115
|
+
const insert = [...normalized]
|
|
116
|
+
const cs = this.chars
|
|
117
|
+
cs.splice(this.cursor, 0, ...insert)
|
|
118
|
+
this.setChars(cs, this.cursor + insert.length)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
backspace(): void {
|
|
122
|
+
if (this.cursor === 0) return
|
|
123
|
+
const cs = this.chars
|
|
124
|
+
cs.splice(this.cursor - 1, 1)
|
|
125
|
+
this.setChars(cs, this.cursor - 1)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
newline(): void {
|
|
129
|
+
const cs = this.chars
|
|
130
|
+
cs.splice(this.cursor, 0, '\n')
|
|
131
|
+
this.setChars(cs, this.cursor + 1)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** index of the start of the word left of the cursor */
|
|
135
|
+
private wordBoundLeft(): number {
|
|
136
|
+
const cs = this.chars
|
|
137
|
+
let i = this.cursor
|
|
138
|
+
while (i > 0 && /\s/.test(cs[i - 1] ?? '')) i--
|
|
139
|
+
while (i > 0 && !/\s/.test(cs[i - 1] ?? '')) i--
|
|
140
|
+
return i
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** index just past the word right of the cursor */
|
|
144
|
+
private wordBoundRight(): number {
|
|
145
|
+
const cs = this.chars
|
|
146
|
+
let i = this.cursor
|
|
147
|
+
while (i < cs.length && /\s/.test(cs[i] ?? '')) i++
|
|
148
|
+
while (i < cs.length && !/\s/.test(cs[i] ?? '')) i++
|
|
149
|
+
return i
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** alt+backspace / ctrl+w */
|
|
153
|
+
deleteWordBack(): void {
|
|
154
|
+
const from = this.wordBoundLeft()
|
|
155
|
+
const cs = this.chars
|
|
156
|
+
cs.splice(from, this.cursor - from)
|
|
157
|
+
this.setChars(cs, from)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** ctrl+u: clear from start to cursor */
|
|
161
|
+
killToStart(): void {
|
|
162
|
+
this.setChars(this.chars.slice(this.cursor), 0)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** ctrl+k: clear from cursor to end */
|
|
166
|
+
killToEnd(): void {
|
|
167
|
+
this.setChars(this.chars.slice(0, this.cursor), this.cursor)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
cursorLeft(): void {
|
|
171
|
+
if (this.cursor > 0) this.cursor--
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
cursorRight(): void {
|
|
175
|
+
if (this.cursor < this.chars.length) this.cursor++
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** alt+left / esc-b */
|
|
179
|
+
wordLeft(): void {
|
|
180
|
+
this.cursor = this.wordBoundLeft()
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** alt+right / esc-f */
|
|
184
|
+
wordRight(): void {
|
|
185
|
+
this.cursor = this.wordBoundRight()
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** start index (in code points) of the line containing ix */
|
|
189
|
+
private lineStartAt(ix: number): number {
|
|
190
|
+
const cs = this.chars
|
|
191
|
+
let i = ix
|
|
192
|
+
while (i > 0 && cs[i - 1] !== '\n') i--
|
|
193
|
+
return i
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** end index (exclusive, before the \n) of the line containing ix */
|
|
197
|
+
private lineEndAt(ix: number): number {
|
|
198
|
+
const cs = this.chars
|
|
199
|
+
let i = ix
|
|
200
|
+
while (i < cs.length && cs[i] !== '\n') i++
|
|
201
|
+
return i
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Home / ⌘← / ⌃A: start of the LOGICAL line; already there → the previous line's start */
|
|
205
|
+
lineHome(): void {
|
|
206
|
+
const start = this.lineStartAt(this.cursor)
|
|
207
|
+
if (this.cursor !== start || start === 0) return void (this.cursor = start)
|
|
208
|
+
this.cursor = this.lineStartAt(start - 1)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** End / ⌘→ / ⌃E: end of the LOGICAL line; already there → the next line's end */
|
|
212
|
+
lineEnd(): void {
|
|
213
|
+
const end = this.lineEndAt(this.cursor)
|
|
214
|
+
if (this.cursor !== end || end === this.chars.length) return void (this.cursor = end)
|
|
215
|
+
this.cursor = this.lineEndAt(end + 1)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** ⌥↑/⌥↓: swap the cursor's logical line with its neighbor, cursor rides along */
|
|
219
|
+
moveLine(delta: -1 | 1): void {
|
|
220
|
+
const cs = this.chars
|
|
221
|
+
const start = this.lineStartAt(this.cursor)
|
|
222
|
+
const end = this.lineEndAt(this.cursor)
|
|
223
|
+
const col = this.cursor - start
|
|
224
|
+
const cur = cs.slice(start, end)
|
|
225
|
+
if (delta === -1) {
|
|
226
|
+
if (start === 0) return
|
|
227
|
+
const prevStart = this.lineStartAt(start - 1)
|
|
228
|
+
const prev = cs.slice(prevStart, start - 1)
|
|
229
|
+
this.setChars(
|
|
230
|
+
[...cs.slice(0, prevStart), ...cur, '\n', ...prev, ...cs.slice(end)],
|
|
231
|
+
prevStart + Math.min(col, cur.length),
|
|
232
|
+
)
|
|
233
|
+
} else {
|
|
234
|
+
if (end === cs.length) return
|
|
235
|
+
const nextEnd = this.lineEndAt(end + 1)
|
|
236
|
+
const next = cs.slice(end + 1, nextEnd)
|
|
237
|
+
this.setChars(
|
|
238
|
+
[...cs.slice(0, start), ...next, '\n', ...cur, ...cs.slice(nextEnd)],
|
|
239
|
+
start + next.length + 1 + Math.min(col, cur.length),
|
|
240
|
+
)
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** ⌘/ / ⌃/ / ⌥/: toggle `// ` at the start of the cursor's line */
|
|
245
|
+
toggleComment(): void {
|
|
246
|
+
const cs = this.chars
|
|
247
|
+
const start = this.lineStartAt(this.cursor)
|
|
248
|
+
const end = this.lineEndAt(this.cursor)
|
|
249
|
+
const line = cs.slice(start, end).join('')
|
|
250
|
+
const m = /^(\s*)\/\/ ?/.exec(line)
|
|
251
|
+
if (m == null) {
|
|
252
|
+
cs.splice(start, 0, '/', '/', ' ')
|
|
253
|
+
this.setChars(cs, this.cursor + 3)
|
|
254
|
+
return
|
|
255
|
+
}
|
|
256
|
+
const indentLen = [...(m[1] ?? '')].length
|
|
257
|
+
const removeLen = [...m[0]].length - indentLen
|
|
258
|
+
cs.splice(start + indentLen, removeLen)
|
|
259
|
+
this.setChars(cs, Math.max(start, this.cursor - removeLen))
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
cursorUp(): void {
|
|
263
|
+
const start = this.lineStartAt(this.cursor)
|
|
264
|
+
if (start === 0) {
|
|
265
|
+
this.cursor = 0
|
|
266
|
+
return
|
|
267
|
+
}
|
|
268
|
+
const col = this.cursor - start
|
|
269
|
+
const prevStart = this.lineStartAt(start - 1)
|
|
270
|
+
const prevLen = start - 1 - prevStart
|
|
271
|
+
this.cursor = prevStart + Math.min(col, prevLen)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
cursorDown(): void {
|
|
275
|
+
const cs = this.chars
|
|
276
|
+
const start = this.lineStartAt(this.cursor)
|
|
277
|
+
const col = this.cursor - start
|
|
278
|
+
let end = this.cursor
|
|
279
|
+
while (end < cs.length && cs[end] !== '\n') end++
|
|
280
|
+
if (end === cs.length) {
|
|
281
|
+
this.cursor = cs.length
|
|
282
|
+
return
|
|
283
|
+
}
|
|
284
|
+
const nextStart = end + 1
|
|
285
|
+
let nextEnd = nextStart
|
|
286
|
+
while (nextEnd < cs.length && cs[nextEnd] !== '\n') nextEnd++
|
|
287
|
+
this.cursor = nextStart + Math.min(col, nextEnd - nextStart)
|
|
288
|
+
}
|
|
289
|
+
}
|