Skip to content

Commit

Permalink
fix: bard text area selector
Browse files Browse the repository at this point in the history
  • Loading branch information
rpidanny committed Oct 30, 2023
1 parent 2f6eac9 commit c545bb3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 36 deletions.
21 changes: 20 additions & 1 deletion apps/chrome-extension/src/pages/Content/base.dom.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IPrompt } from '@rpidanny/llm-prompt-templates';
import { message } from 'antd';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

Expand Down Expand Up @@ -28,7 +29,16 @@ export abstract class BaseDom {

private handlePromptSelected(prompt: IPrompt): void {
this.isPromptsViewOpen = false;
this.usePrompt(prompt);
try {
this.usePrompt(prompt);
} catch (error) {
console.error(error);
message.error(
'Failed to use prompt. Copying prompt to clipboard instead.',
5
);
this.copyPromptToClipboard(prompt);
}
this.hidePrompts();
}

Expand All @@ -50,6 +60,15 @@ export abstract class BaseDom {
);
}

protected copyPromptToClipboard(prompt: IPrompt) {
const clipboardItem = new ClipboardItem({
'text/plain': new Blob([prompt.content], { type: 'text/plain' }),
});
navigator.clipboard.write([clipboardItem]);

message.info(`${prompt.name} prompt copied to clipboard`, 7);
}

protected async showPrompts() {
this.isPromptsViewOpen = true;
await this.render();
Expand Down
8 changes: 1 addition & 7 deletions apps/chrome-extension/src/pages/Content/generic.dom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { IPrompt } from '@rpidanny/llm-prompt-templates';
import { message } from 'antd';

import { BaseDom } from './base.dom';

Expand All @@ -10,12 +9,7 @@ export class GenericDom extends BaseDom {
protected addCustomTrigger() {}

protected usePrompt(prompt: IPrompt) {
const clipboardItem = new ClipboardItem({
'text/plain': new Blob([prompt.content], { type: 'text/plain' }),
});
navigator.clipboard.write([clipboardItem]);

message.info(`${prompt.name} prompt copied to clipboard`);
this.copyPromptToClipboard(prompt);
}
}

Expand Down
16 changes: 13 additions & 3 deletions apps/chrome-extension/src/pages/Content/llms/bard/bard.dom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { ChatGPTDom } from '../chatgpt/chatgpt.dom';
import { IPrompt } from '@rpidanny/llm-prompt-templates';

export class BardDom extends ChatGPTDom {
import { LLMDom } from '../llm.dom';

export class BardDom extends LLMDom {
protected name = 'Bard';
protected textAreaSelector = 'div > textarea';
protected textAreaSelector = 'rich-textarea > .ql-editor';

protected usePrompt(prompt: IPrompt) {
const textArea = this.getTextArea();
console.log('Setting text', prompt.content);
textArea.focus();
textArea.textContent = prompt.content;
textArea.style.height = textArea.scrollHeight + 'px';
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
import { IPrompt } from '@rpidanny/llm-prompt-templates';

import { BaseDom } from '../../base.dom';
import { LLMDom } from '../llm.dom';

export class ChatGPTDom extends BaseDom {
export class ChatGPTDom extends LLMDom {
protected name = 'ChatGPT';
protected textAreaSelector = 'div.relative > textarea';

private getTextArea(): HTMLTextAreaElement {
const textArea = document.querySelector<HTMLTextAreaElement>(
this.textAreaSelector
);

if (!textArea) throw new Error('Could not find text area');

return textArea;
}

protected addCustomTrigger() {
this.getTextArea().addEventListener('input', (event) => {
const input = event.target as HTMLTextAreaElement;
const text = input.value;

if (text === '/templates' || text === '/lpt') {
this.showPrompts();
} else if (this.isPromptsViewOpen) {
this.hidePrompts();
}
});
}

protected usePrompt(prompt: IPrompt) {
const textArea = this.getTextArea();
console.log('Setting text', prompt.content);
Expand Down
31 changes: 31 additions & 0 deletions apps/chrome-extension/src/pages/Content/llms/llm.dom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { BaseDom } from '../base.dom';

export abstract class LLMDom extends BaseDom {
protected abstract name: string;
protected abstract textAreaSelector: string;

protected getTextArea(): HTMLTextAreaElement {
const textArea = document.querySelector<HTMLTextAreaElement>(
this.textAreaSelector
);

if (!textArea) {
throw new Error('Could not find text area');
}

return textArea;
}

protected addCustomTrigger() {
this.getTextArea().addEventListener('input', (event) => {
const input = event.target as HTMLTextAreaElement;
const text = input.value;

if (text === '/templates' || text === '/lpt') {
this.showPrompts();
} else if (this.isPromptsViewOpen) {
this.hidePrompts();
}
});
}
}

0 comments on commit c545bb3

Please sign in to comment.