custom selection tools (#710, #14, #322, #312, #193, #209, #134, #77, #13, #4, #719, #714, #399, #366, #357, #332, #216, #167, #395, #261, #673, #541, #523)

This commit is contained in:
josc146
2024-06-01 15:27:47 +08:00
parent 6f17e5cdf4
commit c0a84ed78c
16 changed files with 316 additions and 28 deletions
+29 -15
View File
@@ -113,27 +113,41 @@ function FloatingToolbar(props) {
</div>
)
} else {
if (config.activeSelectionTools.length === 0) return <div />
if (
config.activeSelectionTools.length === 0 &&
config.customSelectionTools.reduce((count, tool) => count + (tool.active ? 1 : 0), 0) === 0
)
return <div />
const tools = []
const pushTool = (iconKey, name, genPrompt) => {
tools.push(
cloneElement(toolsConfig[iconKey].icon, {
size: 24,
className: 'chatgptbox-selection-toolbar-button',
title: name,
onClick: async () => {
const p = getClientPosition(props.container)
props.container.style.position = 'fixed'
setPosition(p)
setPrompt(await genPrompt(selection))
setTriggered(true)
},
}),
)
}
for (const key in toolsConfig) {
if (config.activeSelectionTools.includes(key)) {
const toolConfig = toolsConfig[key]
tools.push(
cloneElement(toolConfig.icon, {
size: 24,
className: 'chatgptbox-selection-toolbar-button',
title: t(toolConfig.label),
onClick: async () => {
const p = getClientPosition(props.container)
props.container.style.position = 'fixed'
setPosition(p)
setPrompt(await toolConfig.genPrompt(selection))
setTriggered(true)
},
}),
)
pushTool(key, t(toolConfig.label), toolConfig.genPrompt)
}
}
for (const tool of config.customSelectionTools) {
if (tool.active) {
pushTool(tool.iconKey, tool.name, async (selection) => {
return tool.prompt.replace('{{selection}}', selection)
})
}
}