-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move copy to clipboard as composable
- Loading branch information
Showing
4 changed files
with
79 additions
and
24 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
53 changes: 53 additions & 0 deletions
53
packages/chat-component/src/components/copy-entry-to-clipboard.ts
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,53 @@ | ||
import { injectable } from 'inversify'; | ||
import { container, type ChatEntryActionButtonComponent, ComponentType } from './composable.js'; | ||
import { type ReactiveControllerHost, html } from 'lit'; | ||
import { chatEntryToString } from '../utils/index.js'; | ||
import { globalConfig } from '../config/global-config.js'; | ||
|
||
import iconSuccess from '../../public/svg/success-icon.svg?raw'; | ||
import iconCopyToClipboard from '../../public/svg/copy-icon.svg?raw'; | ||
|
||
@injectable() | ||
export class CopyToClipboardAction implements ChatEntryActionButtonComponent { | ||
private _isResponseCopied: boolean = false; | ||
|
||
set isResponseCopied(value: boolean) { | ||
this._isResponseCopied = value; | ||
this.host.requestUpdate(); | ||
} | ||
|
||
get isResponseCopied() { | ||
return this._isResponseCopied; | ||
} | ||
|
||
host: ReactiveControllerHost; | ||
|
||
attach(host: ReactiveControllerHost) { | ||
this.host = host; | ||
} | ||
|
||
// Copy response to clipboard | ||
copyResponseToClipboard(entry: ChatThreadEntry): void { | ||
const response = chatEntryToString(entry); | ||
|
||
navigator.clipboard.writeText(response); | ||
this.isResponseCopied = true; | ||
} | ||
|
||
render(entry: ChatThreadEntry, isDisabled: boolean) { | ||
return html` | ||
<chat-action-button | ||
.label="${globalConfig.COPY_RESPONSE_BUTTON_LABEL_TEXT}" | ||
.svgIcon="${this.isResponseCopied ? iconSuccess : iconCopyToClipboard}" | ||
.isDisabled="${isDisabled}" | ||
actionId="copy-to-clipboard" | ||
.tooltip="${this.isResponseCopied | ||
? globalConfig.COPIED_SUCCESSFULLY_MESSAGE | ||
: globalConfig.COPY_RESPONSE_BUTTON_LABEL_TEXT}" | ||
@click="${() => this.copyResponseToClipboard(entry)}" | ||
></chat-action-button> | ||
`; | ||
} | ||
} | ||
|
||
container.bind<ChatEntryActionButtonComponent>(ComponentType.ChatEntryActionButtonComponent).to(CopyToClipboardAction); |
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