Spaces:
Sleeping
Sleeping
File size: 5,610 Bytes
26ab438 e698591 26ab438 3cb3466 26ab438 e698591 26ab438 3607be8 26ab438 3607be8 26ab438 3607be8 26ab438 3607be8 26ab438 3607be8 26ab438 3607be8 26ab438 3607be8 26ab438 f7ed3df 26ab438 86235dc a76151e 26ab438 4941fc6 8650d80 26ab438 e698591 26ab438 909b565 26ab438 909b565 26ab438 909b565 e698591 26ab438 909b565 e698591 26ab438 909b565 be3ad36 909b565 be3ad36 909b565 26ab438 1378e63 26ab438 4d0fc3b 26ab438 3607be8 26ab438 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
import { utils } from "./utils.js";
import { view } from "./view.js";
function getUrlParams() {
const config = {};
new URLSearchParams(window.location.search).forEach((value, key) => {
config[key] = value;
});
return config;
}
let config = {
first_language: 'にほんご,中文',
second_language: 'English'
}
config ={...config,... getUrlParams()};
console.log('esltool.js config is :',config);
// Create toolbar element
const toolbar = document.createElement("div");
toolbar.id = "esl-toolbar";
toolbar.style.position = "fixed"; // Make it stick to the top
toolbar.style.top = "0"; // Position at the very top
toolbar.style.left = "0"; // Position at the leftmost side
toolbar.style.width = "100%"; // Make it span the entire width
toolbar.style.backgroundColor = "lightgray"; // Example background color
toolbar.style.padding = "10px"; // Add some padding
// Create buttons
const askButton = document.createElement("button");
askButton.id = "ask-btn";
askButton.textContent = "Ask";
askButton.classList.add('esltool-btn');
askButton.addEventListener('pointerdown', e=>{
e.preventDefault();
});
const correctBtn = document.createElement("button");
correctBtn.id = "correct-btn";
correctBtn.textContent = "Correct";
correctBtn.classList.add('esltool-btn');
const translateBtn = document.createElement("button");
translateBtn.id = "translate-btn";
translateBtn.textContent = "Translate";
translateBtn.classList.add('esltool-btn');
const explainBtn = document.createElement("button");
explainBtn.id = "explain-btn";
explainBtn.textContent = "Explain";
explainBtn.classList.add('esltool-btn');
const defineBtn = document.createElement("button");
defineBtn.id = "define-btn";
defineBtn.textContent = "Define";
defineBtn.classList.add('esltool-btn');
const removeButton = document.createElement("button");
removeButton.id = "remove-btn";
removeButton.textContent = "Hide";
removeButton.classList.add('esltool-btn');
// Append buttons to toolbar
toolbar.appendChild(askButton);
toolbar.appendChild(correctBtn);
toolbar.appendChild(translateBtn);
toolbar.appendChild(explainBtn);
toolbar.appendChild(defineBtn);
toolbar.appendChild(removeButton);
// Create content area
const contentArea = document.createElement("div");
contentArea.id = "content";
contentArea.contentEditable = true; // Allow user to select text
// Append toolbar and content area to the body
document.body.appendChild(toolbar);
document.body.appendChild(contentArea);
// Function to get selected text
function getSelectedText() {
return window.getSelection().toString();
}
// Implement functionalities for each button (same as before)
function ask(prompt) {
let currentLineString = utils.getCurrentLineString(document.activeElement);
let selectText = window.getSelection().toString();
prompt=`
${config.cmd || ' '}
${prompt} **${selectText.length >= 1 ? selectText : currentLineString}** `
utils.displayMarkdown(prompt+'...');
utils.AIComplete(prompt,{
url: '/openai/v1/chat/completions',
model: config.model ||
'llama-3.1-70b-versatile',
max_tokens: parseInt(config.max_tokens) || 8000,
});
}
askButton.addEventListener("pointerdown", () => {
ask(' ');
});
// Correct Button:
correctBtn.addEventListener("pointerdown", () => {
ask('correct mistakes of the text: \n');
});
// Translate Button:
translateBtn.addEventListener("pointerdown", () => {
ask(`translate to English, ${config.first_language} French :\n `);
});
// Explain Button:
explainBtn.addEventListener("pointerdown", () => {
ask(`explain this for ${config.second_language} seconds language learner in simple english first,then analyse step by step:\n `);
});
// Define Button:
defineBtn.addEventListener("pointerdown", () => {
ask(`define the word in simple ${config.second_language} , tell me all definitions if it has,and part of speech, and give some usage example:\n ` )
});
removeButton.addEventListener('pointerdown', () => {
var urlDiv = document.getElementById("url_div");
urlDiv.style.display = urlDiv.style.display === "none" ? "block" : "none";
});
// Style the toolbar
toolbar.style.position = "fixed";
toolbar.style.top = "0%";
toolbar.style.left = "0";
toolbar.style.width = "wrap-content";
toolbar.style.backgroundColor = "black"; // Light gray background
toolbar.style.padding = "5px";
toolbar.style.boxShadow = "0 2px 4px rgba(0, 0, 0, 0.1)"; // Add a subtle shadow
// Style the buttons
const buttonStyles = {
backgroundColor: "black", // Green background
border: "none",
color: "white",
padding: "2px",
textAlign: "center",
textDecoration: "none",
display: "inline-block",
fontSize: "small",
margin: "2px",
cursor: "pointer",
borderRadius: "5px", // Rounded corners
};
// Apply styles to each button
for (const button of [askButton, correctBtn, translateBtn, explainBtn, defineBtn, removeButton]) {
Object.assign(button.style, buttonStyles);
utils.makeButtonFeedback(button);
}
setTimeout(() => {
utils.displayMarkdown(`
hello, this is note app for language learner, press the blue circle then speak now! try ask button or ctrl + enter to send current line or selected text to AI
code will display like this:
\`\`\`
this code \`\${value}\`
\`\`\`
`)
view.createMenu(window.innerWidth*0.9, 300);
let allEslToolBtns=document.querySelectorAll('.esltool-btn');
for (const btn of allEslToolBtns) {
console.log(btn);
btn.addEventListener('pointerdown', e=>{
e.preventDefault();
});
}
}, 1000);
|