-
Notifications
You must be signed in to change notification settings - Fork 884
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Uplift 1.62.x] AI chat issues cr121 1.62.x (#21629)
* aichat: input is growable (#21124) * aichat: scroll is interruptable (#21235) * aichat: model maker text shouldnt look like a link (#21220) * aichat: code formatting (#21342) * make claude output formatted code (#21599)
- Loading branch information
Showing
16 changed files
with
707 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
components/ai_chat/resources/page/components/code_block/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* Copyright (c) 2023 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
import * as React from 'react' | ||
|
||
import styles from './style.module.scss' | ||
import Button from '@brave/leo/react/button' | ||
import Icon from '@brave/leo/react/icon' | ||
import { Light as SyntaxHighlighter } from 'react-syntax-highlighter' | ||
import hljsStyle from 'react-syntax-highlighter/dist/esm/styles/hljs/ir-black' | ||
import cpp from 'react-syntax-highlighter/dist/esm/languages/hljs/cpp' | ||
import javascript from 'react-syntax-highlighter/dist/esm/languages/hljs/javascript' | ||
import python from 'react-syntax-highlighter/dist/esm/languages/hljs/python' | ||
import json from 'react-syntax-highlighter/dist/esm/languages/hljs/json' | ||
|
||
SyntaxHighlighter.registerLanguage('cpp', cpp) | ||
SyntaxHighlighter.registerLanguage('javascript', javascript) | ||
SyntaxHighlighter.registerLanguage('python', python) | ||
SyntaxHighlighter.registerLanguage('json', json) | ||
|
||
interface CodeInlineProps { | ||
code: string | ||
} | ||
interface CodeBlockProps { | ||
code: string | ||
lang: string | ||
} | ||
|
||
function Inline(props: CodeInlineProps) { | ||
return ( | ||
<span className={styles.container}> | ||
<code> | ||
{props.code} | ||
</code> | ||
</span> | ||
) | ||
} | ||
|
||
function Block(props: CodeBlockProps) { | ||
const [hasCopied, setHasCopied] = React.useState(false) | ||
|
||
const handleCopy = () => { | ||
navigator.clipboard.writeText(props.code).then(() => { | ||
setHasCopied(true) | ||
setTimeout(() => setHasCopied(false), 1000) | ||
}) | ||
} | ||
|
||
return ( | ||
<div className={styles.container}> | ||
<div className={styles.toolbar}> | ||
<div>{props.lang}</div> | ||
<Button | ||
kind='plain-faint' | ||
onClick={handleCopy} | ||
> | ||
<div slot="icon-before"> | ||
<Icon className={styles.icon} name={hasCopied ? 'check-circle-outline' : 'copy'} /> | ||
</div> | ||
<div>Copy code</div> | ||
</Button> | ||
</div> | ||
<SyntaxHighlighter | ||
language={props.lang} | ||
style={hljsStyle} | ||
wrapLines | ||
wrapLongLines | ||
codeTagProps={{ style: { wordBreak: 'break-word' } }} | ||
> | ||
{props.code} | ||
</SyntaxHighlighter> | ||
</div> | ||
) | ||
} | ||
|
||
export default { | ||
Inline, | ||
Block | ||
} |
37 changes: 37 additions & 0 deletions
37
components/ai_chat/resources/page/components/code_block/style.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2023 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// you can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
.container { | ||
overflow: auto; | ||
background: var(--leo-color-page-background); | ||
border: 1px solid var(--leo-color-divider-subtle); | ||
border-radius: 8px; | ||
|
||
pre, | ||
code { | ||
white-space: pre-wrap; | ||
margin: 0; | ||
} | ||
|
||
pre { | ||
padding: var(--leo-spacing-xl); | ||
} | ||
|
||
code { | ||
padding: var(--leo-spacing-s); | ||
} | ||
} | ||
|
||
.toolbar { | ||
background: var(--leo-color-container-background); | ||
padding: var(--leo-spacing-m) 16px var(--leo-spacing-m) var(--leo-spacing-2xl); | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-between; | ||
|
||
leo-button { | ||
max-width: max-content; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.