|
@@ -47,6 +47,13 @@ export function createPromptInputV2Store(input: PromptInputV2StoreInput) {
|
|
|
setStore()("cursor", content.length)
|
|
setStore()("cursor", content.length)
|
|
|
})
|
|
})
|
|
|
},
|
|
},
|
|
|
|
|
+ addText(content: string) {
|
|
|
|
|
+ const cursor = store().cursor ?? promptLength(store().prompt)
|
|
|
|
|
+ batch(() => {
|
|
|
|
|
+ setStore()("prompt", (prompt) => insertText(prompt, cursor, content))
|
|
|
|
|
+ setStore()("cursor", cursor + content.length)
|
|
|
|
|
+ })
|
|
|
|
|
+ },
|
|
|
reset() {
|
|
reset() {
|
|
|
batch(() => {
|
|
batch(() => {
|
|
|
setStore()("prompt", [{ type: "text", content: "", start: 0, end: 0 }])
|
|
setStore()("prompt", [{ type: "text", content: "", start: 0, end: 0 }])
|
|
@@ -86,6 +93,27 @@ export function createPromptInputV2Store(input: PromptInputV2StoreInput) {
|
|
|
|
|
|
|
|
export type PromptInputV2Store = ReturnType<typeof createPromptInputV2Store>
|
|
export type PromptInputV2Store = ReturnType<typeof createPromptInputV2Store>
|
|
|
|
|
|
|
|
|
|
+function insertText(prompt: PromptInputV2Prompt, cursor: number, content: string): PromptInputV2Prompt {
|
|
|
|
|
+ let position = 0
|
|
|
|
|
+ let inserted = false
|
|
|
|
|
+ const parts = prompt.flatMap<PromptInputV2Prompt[number]>((part) => {
|
|
|
|
|
+ if (part.type === "image") return [part]
|
|
|
|
|
+ const start = position
|
|
|
|
|
+ position += part.content.length
|
|
|
|
|
+ if (inserted) return [part]
|
|
|
|
|
+ if (part.type === "text" && cursor >= start && cursor <= position) {
|
|
|
|
|
+ inserted = true
|
|
|
|
|
+ const offset = cursor - start
|
|
|
|
|
+ return [{ ...part, content: part.content.slice(0, offset) + content + part.content.slice(offset) }]
|
|
|
|
|
+ }
|
|
|
|
|
+ if (cursor > start) return [part]
|
|
|
|
|
+ inserted = true
|
|
|
|
|
+ return [{ type: "text", content, start: 0, end: 0 }, part]
|
|
|
|
|
+ })
|
|
|
|
|
+ if (!inserted) parts.push({ type: "text", content, start: 0, end: 0 })
|
|
|
|
|
+ return withOffsets(parts)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
function insertMention(
|
|
function insertMention(
|
|
|
prompt: PromptInputV2Prompt,
|
|
prompt: PromptInputV2Prompt,
|
|
|
start: number,
|
|
start: number,
|
|
@@ -106,11 +134,19 @@ function insertMention(
|
|
|
{ type: "text" as const, content: ` ${after}`, start: 0, end: 0 },
|
|
{ type: "text" as const, content: ` ${after}`, start: 0, end: 0 },
|
|
|
]
|
|
]
|
|
|
})
|
|
})
|
|
|
|
|
+ return withOffsets(parts)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function withOffsets(prompt: PromptInputV2Prompt): PromptInputV2Prompt {
|
|
|
let offset = 0
|
|
let offset = 0
|
|
|
- return parts.map((part) => {
|
|
|
|
|
|
|
+ return prompt.map((part) => {
|
|
|
if (part.type === "image") return part
|
|
if (part.type === "image") return part
|
|
|
const next = { ...part, start: offset, end: offset + part.content.length }
|
|
const next = { ...part, start: offset, end: offset + part.content.length }
|
|
|
offset = next.end
|
|
offset = next.end
|
|
|
return next
|
|
return next
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+function promptLength(prompt: PromptInputV2Prompt) {
|
|
|
|
|
+ return prompt.reduce((length, part) => length + ("content" in part ? part.content.length : 0), 0)
|
|
|
|
|
+}
|