浏览代码

feat: support opening new tab in markdown button (#12213)

Bowen Liang 4 月之前
父节点
当前提交
ab469aa07d
共有 1 个文件被更改,包括 18 次插入1 次删除
  1. 18 1
      web/app/components/base/markdown-blocks/button.tsx

+ 18 - 1
web/app/components/base/markdown-blocks/button.tsx

@@ -6,13 +6,30 @@ const MarkdownButton = ({ node }: any) => {
   const { onSend } = useChatContext()
   const { onSend } = useChatContext()
   const variant = node.properties.dataVariant
   const variant = node.properties.dataVariant
   const message = node.properties.dataMessage
   const message = node.properties.dataMessage
+  const link = node.properties.dataLink
   const size = node.properties.dataSize
   const size = node.properties.dataSize
 
 
+  function is_valid_url(url: string): boolean {
+    try {
+      const parsed_url = new URL(url)
+      return ['http:', 'https:'].includes(parsed_url.protocol)
+    }
+    catch {
+      return false
+    }
+  }
+
   return <Button
   return <Button
     variant={variant}
     variant={variant}
     size={size}
     size={size}
     className={cn('!h-8 !px-3 select-none')}
     className={cn('!h-8 !px-3 select-none')}
-    onClick={() => onSend?.(message)}
+    onClick={() => {
+      if (is_valid_url(link)) {
+        window.open(link, '_blank')
+        return
+      }
+      onSend?.(message)
+    }}
   >
   >
     <span className='text-[13px]'>{node.children[0]?.value || ''}</span>
     <span className='text-[13px]'>{node.children[0]?.value || ''}</span>
   </Button>
   </Button>