mirror of
https://github.com/FranP-code/ChatGPT.git
synced 2025-10-13 00:13:25 +00:00
feat: chatgpt prompts
This commit is contained in:
@@ -63,4 +63,11 @@ pub fn form_msg(app: AppHandle, label: &str, title: &str, msg: &str) {
|
||||
#[command]
|
||||
pub fn open_file(path: PathBuf) {
|
||||
utils::open_file(path);
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub fn get_chat_model() -> serde_json::Value {
|
||||
let path = utils::chat_root().join("chat.model.json");
|
||||
let content = fs::read_to_string(path).unwrap_or_else(|_| r#"{"data":[]}"#.to_string());
|
||||
serde_json::from_str(&content).unwrap()
|
||||
}
|
||||
@@ -19,6 +19,7 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>
|
||||
std::thread::spawn(move || {
|
||||
#[cfg(target_os = "macos")]
|
||||
WindowBuilder::new(&app, "core", WindowUrl::App(url.into()))
|
||||
.title("ChatGPT")
|
||||
.resizable(true)
|
||||
.fullscreen(false)
|
||||
.inner_size(800.0, 600.0)
|
||||
@@ -31,6 +32,7 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>
|
||||
.initialization_script(include_str!("../assets/jspdf.js"))
|
||||
.initialization_script(include_str!("../assets/core.js"))
|
||||
.initialization_script(include_str!("../assets/export.js"))
|
||||
.initialization_script(include_str!("../assets/cmd.js"))
|
||||
.user_agent(&chat_conf.ua_window)
|
||||
.build().unwrap();
|
||||
|
||||
@@ -47,6 +49,7 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>
|
||||
.initialization_script(include_str!("../assets/jspdf.js"))
|
||||
.initialization_script(include_str!("../assets/core.js"))
|
||||
.initialization_script(include_str!("../assets/export.js"))
|
||||
.initialization_script(include_str!("../assets/cmd.js"))
|
||||
.user_agent(&chat_conf.ua_window)
|
||||
.build().unwrap();
|
||||
});
|
||||
|
||||
@@ -8,6 +8,7 @@ pub fn tray_window(handle: &tauri::AppHandle) {
|
||||
|
||||
std::thread::spawn(move || {
|
||||
WindowBuilder::new(&app, "tray", WindowUrl::App(chat_conf.origin.into()))
|
||||
.title("ChatGPT")
|
||||
.resizable(false)
|
||||
.fullscreen(false)
|
||||
.inner_size(360.0, 540.0)
|
||||
@@ -19,6 +20,7 @@ pub fn tray_window(handle: &tauri::AppHandle) {
|
||||
.initialization_script(include_str!("../assets/jspdf.js"))
|
||||
.initialization_script(include_str!("../assets/core.js"))
|
||||
.initialization_script(include_str!("../assets/export.js"))
|
||||
.initialization_script(include_str!("../assets/cmd.js"))
|
||||
.user_agent(&chat_conf.ua_tray)
|
||||
.build()
|
||||
.unwrap()
|
||||
|
||||
130
src-tauri/src/assets/cmd.js
vendored
Normal file
130
src-tauri/src/assets/cmd.js
vendored
Normal file
@@ -0,0 +1,130 @@
|
||||
function init() {
|
||||
const styleDom = document.createElement('style');
|
||||
styleDom.innerHTML = `form {
|
||||
position: relative;
|
||||
}
|
||||
.chat-model-cmd-list {
|
||||
position: absolute;
|
||||
width: 400px;
|
||||
bottom: 60px;
|
||||
max-height: 100px;
|
||||
overflow: auto;
|
||||
z-index: 9999;
|
||||
}
|
||||
.chat-model-cmd-list>div {
|
||||
border: solid 2px #d8d8d8;
|
||||
border-radius: 5px;
|
||||
background-color: #fff;
|
||||
}
|
||||
.chat-model-cmd-list .cmd-item {
|
||||
font-size: 12px;
|
||||
border-bottom: solid 1px #888;
|
||||
padding: 2px 4px;
|
||||
display: flex;
|
||||
user-select: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
.chat-model-cmd-list .cmd-item:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
.chat-model-cmd-list .cmd-item b {
|
||||
display: inline-block;
|
||||
width: 120px;
|
||||
border-radius: 4px;
|
||||
margin-right: 10px;
|
||||
color: #2a2a2a;
|
||||
}
|
||||
.chat-model-cmd-list .cmd-item i {
|
||||
width: 270px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
text-align: right;
|
||||
color: #888;
|
||||
}`;
|
||||
document.head.append(styleDom);
|
||||
|
||||
if (window.formInterval) {
|
||||
clearInterval(window.formInterval);
|
||||
}
|
||||
window.formInterval = setInterval(() => {
|
||||
const form = document.querySelector("form");
|
||||
if (!form) return;
|
||||
clearInterval(window.formInterval);
|
||||
cmdTip();
|
||||
}, 200);
|
||||
}
|
||||
|
||||
async function cmdTip() {
|
||||
const chatModelJson = await invoke('get_chat_model') || {};
|
||||
if (!chatModelJson.data && chatModelJson.data.length <= 0) return;
|
||||
const data = chatModelJson.data || [];
|
||||
|
||||
const modelDom = document.createElement('div');
|
||||
modelDom.classList.add('chat-model-cmd-list');
|
||||
document.querySelector('form').appendChild(modelDom);
|
||||
const itemDom = (v) => `<div class="cmd-item" data-prompt="${encodeURIComponent(v.prompt)}"><b>/${v.cmd}</b><i>${v.act}</i></div>`;
|
||||
const searchInput = document.querySelector('form textarea');
|
||||
|
||||
searchInput.addEventListener('input', debounce(function() {
|
||||
const query = this.value;
|
||||
console.log(query);
|
||||
if (!query || !/^\//.test(query)) {
|
||||
modelDom.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
const result = data.filter(i => i.enable && new RegExp(query.substring(1)).test(i.cmd));
|
||||
if (result.length > 0) {
|
||||
modelDom.innerHTML = `<div>${result.map(itemDom).join('')}</div>`;
|
||||
}
|
||||
// Enter a command starting with `/` and press a space to automatically fill `chatgpt prompt`.
|
||||
// If more than one command appears in the search results, the first one will be used by default.
|
||||
searchInput.addEventListener('keydown', (event) => {
|
||||
if (event.keyCode === 32) {
|
||||
searchInput.value = result[0]?.prompt.trim();
|
||||
}
|
||||
if (event.keyCode = 13) {
|
||||
modelDom.innerHTML = '';
|
||||
}
|
||||
});
|
||||
}, 250),
|
||||
{
|
||||
capture: false,
|
||||
passive: true,
|
||||
once: false
|
||||
});
|
||||
|
||||
if (window.searchInterval) {
|
||||
clearInterval(window.searchInterval);
|
||||
}
|
||||
window.searchInterval = setInterval(() => {
|
||||
// The `chatgpt prompt` fill can be done by clicking on the event.
|
||||
const searchDom = document.querySelector("form .chat-model-cmd-list>div");
|
||||
if (!searchDom) return;
|
||||
searchDom.addEventListener('click', (event) => {
|
||||
// .cmd-item
|
||||
const item = event.target.closest("div");
|
||||
if (item) {
|
||||
document.querySelector('form textarea').value = decodeURIComponent(item.getAttribute('data-prompt'));
|
||||
document.querySelector('form textarea').focus();
|
||||
}
|
||||
});
|
||||
}, 200);
|
||||
}
|
||||
|
||||
function debounce(fn, delay) {
|
||||
let timeoutId;
|
||||
return function(...args) {
|
||||
clearTimeout(timeoutId);
|
||||
timeoutId = setTimeout(() => fn.apply(this, args), delay);
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
document.readyState === "complete" ||
|
||||
document.readyState === "interactive"
|
||||
) {
|
||||
init();
|
||||
} else {
|
||||
document.addEventListener("DOMContentLoaded", init);
|
||||
}
|
||||
@@ -27,6 +27,7 @@ fn main() {
|
||||
cmd::form_confirm,
|
||||
cmd::form_msg,
|
||||
cmd::open_file,
|
||||
cmd::get_chat_model,
|
||||
])
|
||||
.setup(setup::init)
|
||||
.plugin(tauri_plugin_positioner::init())
|
||||
|
||||
Reference in New Issue
Block a user