Skip to content

Commit

Permalink
✨ feat: Add MDI icons easily in answer text
Browse files Browse the repository at this point in the history
Something like:

PDF - [[i|file-pdf|red]
Word Doc - [[i|file-word-box|blue]
Excel - [[i|microsoft-excel|green]
Text Document - [[i|text-box-outline]
Tag - [[i|tag-text|purple]
Check Mark - [[i|check|green]
Link 1 - [[i|link-variant|red]
Link 2 - [[i|link-box|orange]
Link New Window - [[i|open-in-new]
Alert - [[i|alert-decagram|red]
  • Loading branch information
jolzee committed Nov 13, 2020
1 parent 43126ee commit bb0c33a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
queryParamStringAsObject,
sleep,
uuid,
addTtsPauses
addTtsPauses,
handleIcons
} from "@/utils/utils";
import { accountsSdk } from "@livechat/accounts-sdk";
import LiveChat from "@livechat/agent-app-widget-sdk";
Expand Down Expand Up @@ -2226,8 +2227,10 @@ async function handleTeneoResponse(currentUserInput, context, params, vuetify) {
const response = {
userInput: currentUserInput,
id: uuid(),
teneoAnswer: md.render(
cleanEmptyChunks(tResp.getOutputText()).replace(/onclick="[^"]+"/g, 'class="sendInput"')
teneoAnswer: handleIcons(
md.render(
cleanEmptyChunks(tResp.getOutputText()).replace(/onclick="[^"]+"/g, 'class="sendInput"')
)
),
teneoResponse: json
};
Expand Down Expand Up @@ -2734,8 +2737,10 @@ function handleLoginResponse(context, json, vuetify, resolve) {
const response = {
type: "reply",
id: uuid(),
text: md.render(
cleanEmptyChunks(tResp.getOutputText()).replace(/onclick="[^"]+"/g, 'class="sendInput"')
text: handleIcons(
md.render(
cleanEmptyChunks(tResp.getOutputText()).replace(/onclick="[^"]+"/g, 'class="sendInput"')
)
),
bodyText: "",
teneoResponse: json,
Expand Down
43 changes: 43 additions & 0 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,49 @@ export const download = (data, filename, type = "text/plain") => {
}
};

export const handleIcons = myString => {
var regexp = /\[\[(.{1,30}?)\]/g;
const matches = myString.matchAll(regexp);

let reformattedStr = "";
let cursorPos = 0;

for (const match of matches) {
console.log(match);
// the complete match
const index = match.index;

const fullMatch = match[0];
// console.log(fullMatch);

// append string content from the last match
reformattedStr += myString.substr(cursorPos, index - cursorPos);

//update cursorPos to end of match.
cursorPos = index + fullMatch.length;

// isolating each capture group match
const group1 = match[1];

const leopardInfo = group1.split("|");
// console.log(leopardInfo);

if (leopardInfo.length > 0 && leopardInfo[0] === "i") {
let html = `<i aria-hidden="true" class="v-icon mdi mdi-${leopardInfo[1]} ${
leopardInfo[2] ? leopardInfo[2].toLowerCase() + "--text" : ""
}"></i>`;
reformattedStr += html;
}
}

// if there is still string content present after the last match, append it here
if (cursorPos < myString.length) {
reformattedStr += myString.substr(cursorPos, myString.length - cursorPos);
}

return reformattedStr;
};

export const getBase64Image = img => {
const canvas = document.createElement("canvas");
canvas.width = img.width;
Expand Down

0 comments on commit bb0c33a

Please sign in to comment.