Skip to content

adding copy to clipboard feature #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/src/components/content/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export const Settings = (props: Props) => {
if (
// model.capabilities.includes("FINE_TUNE") &&
model.capabilities.includes("TEXT_GENERATION") &&
model.vendor == "cohere" &&
model.version > 15
(model.vendor == "cohere" || model.vendor == "") &&
model.version != "14.2"
)
return model;
});
Expand Down
57 changes: 57 additions & 0 deletions app/src/components/content/stomp-interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export const InitStomp = (
? "localhost:8080"
: window.location.hostname;
const serviceURL = `${protocol}${hostname}/websocket`;
let snippets: Array<string> = [];
let codesnipIdx = 0;

console.log("in the stomp init module");
const client = new Client({
brokerURL: serviceURL,
Expand Down Expand Up @@ -44,6 +47,58 @@ export const InitStomp = (
});
client.activate();

const getIdxs = (searchTxt: string, data: string) => {
let idx = 0;
let tempArray = [];
while (idx !== -1) {
idx = data.indexOf(searchTxt, idx + 1);
tempArray.push(idx);
}
return tempArray;
};

const getCodeSnippets = (indexes: Array<number>, data: string) => {
let i = 0;
snippets = [];
while (i < indexes.length - 1 && indexes[0] !== -1) {
let tempStr = data.substring(indexes[i], indexes[i + 1]);
let langLen = tempStr.indexOf("\n");
let trimmed = tempStr.substring(langLen);
snippets.push(trimmed);
i += 2;
}
console.log("snippets: ", snippets);
console.log("codesnipIdx in getCodeSnippets: ", codesnipIdx);
return snippets;
};

const addButtons = () => {
setTimeout(() => {
const codeSections: HTMLCollectionOf<HTMLPreElement> =
document.getElementsByTagName("pre");
let x = 0;
for (let i = codesnipIdx; i < Array.from(codeSections).length; i++) {
let tempStr = JSON.stringify(snippets[x]);
let btn = document.createElement("button");
btn.setAttribute("id", "btn-" + codesnipIdx);
btn.setAttribute("onclick", "copytoclip(" + tempStr + ")");
// btn.setAttribute("class", "copy-to-clip-btn");
btn.innerText = "Copy";
btn.style.cssText = `position:relative;float:right`;
codeSections[codesnipIdx]?.prepend(btn);
console.log("codesnipIdx before increment in addButton: ", codesnipIdx);
codesnipIdx++;
x++;
}
}, 750);
};
const getClipboardOptions = (data: string) => {
const searchTxt = "```";
const indexes = getIdxs(searchTxt, data);
const snippets = getCodeSnippets(indexes, data);
return indexes[0] != -1 ? true : false;
};

const onMessage = (msg: any) => {
let aiAnswer = JSON.parse(msg.body).content;
if (msg.data !== "connected") {
Expand All @@ -52,12 +107,14 @@ export const InitStomp = (
setBusy(false);
tempArray.pop();
messagesDP.current.data = [];
const snipsExist = getClipboardOptions(aiAnswer);
tempArray.push({
id: tempArray.length as number,
answer: aiAnswer,
});
chatData.current = tempArray;
setUpdate(chatData.current);
snipsExist ? addButtons() : null;
}
};
return client;
Expand Down
23 changes: 15 additions & 8 deletions app/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,23 @@
/>
<!-- This is where you would add any app specific styling -->
<link rel="stylesheet" href="styles/app.css" type="text/css" />

<script type="text/javascript">
const copytoclip = (snip) => {
if (window.isSecureContext) {
if (snip) {
navigator.clipboard.writeText(snip);
}
} else {
console.log(
"Copy to clipboard requires a secure HTTP server (HTTPS) or localhost domain"
);
}
};
</script>
</head>

<body
class="oj-web-applayout-body"
data-oj-binding-provider="none"
>
<!-- <body
class="oj-web-applayout-body oj-color-invert"
data-oj-binding-provider="none"
> -->
<body class="oj-web-applayout-body" data-oj-binding-provider="none">
<app-root></app-root>
<!-- This injects script tags for the main javascript files -->
<!-- injector:scripts -->
Expand Down
12 changes: 12 additions & 0 deletions app/src/styles/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ oj-sample-markdown-viewer .legacyStyling pre {
color: black;
}

.copy-to-clip-btn {
position: relative;
border-bottom-left-radius: 5px;
border-width: thin;
padding: 5px;
float: right;
top: -10px;
right: -10px;
border-top: none;
border-right: none;
}

.demo-copy-paste {
user-select: text;
}
Expand Down