on-codemerge
Version:
A WYSIWYG editor for on-codemerge is a user-friendly interface that allows users to edit and view their code in real time, exactly as it will appear in the final product
210 lines (208 loc) • 7.6 kB
JavaScript
/*! on-codemerge v1.3.1 @author Pavel Kuzmin @license MIT @homepage https://s00d.github.io/on-codemerge/ @repository git+https://github.com/s00d/on-codemerge.git Copyright (c) 2026 Pavel Kuzmin - Built on 2026-07-02T13:39:17.947Z */
var u = Object.defineProperty;
var d = (o, t, e) => t in o ? u(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
var s = (o, t, e) => d(o, typeof t != "symbol" ? t + "" : t, e);
/* empty css */
/* empty css */
import { PopupManager as m } from "../../core/ui/PopupManager.mjs";
import { createToolbarButton as c } from "../ToolbarPlugin/utils.mjs";
import v from "../../icons/ai-assistant.svg.mjs";
import { DeepSeekDriver as g } from "./drivers/DeepSeekDriver.mjs";
import { HuggingFaceDriver as f } from "./drivers/HuggingFaceDriver.mjs";
import { OpenAIDriver as y } from "./drivers/OpenAIDriver.mjs";
import { GitHubAzureDriver as b } from "./drivers/GitHubAzureDriver.mjs";
import { LlamaDriver as P } from "./drivers/LlamaDriver.mjs";
import { MistralDriver as O } from "./drivers/MistralDriver.mjs";
import { OllamaDriver as w } from "./drivers/OllamaDriver.mjs";
const p = "aiAssistantSettings", n = "Write an article about the benefits of using artificial intelligence in web development. Include examples of JavaScript code and explain how AI can simplify the development process.", l = `The response should be formatted as HTML code that can be inserted into a text editor. Follow these rules:
1. Headings should be wrapped in <h1>, <h2>, <h3>, etc.
2. Paragraphs should be wrapped in <p>.
3. Lists should use <ul>, <ol>, and <li>.
4. Code should be wrapped in <pre><code>.
5. Use <strong> and <em> for emphasis.
6. For images, use the <img> tag with the src attribute.
7. Links should use the <a> tag.
8. Tables should use <table>, <tr>, <th>, and <td>.
9. Do not include unnecessary tags like <html>, <head>, or <body>.
10. Ensure code examples are properly formatted.`;
class G {
constructor() {
s(this, "name", "ai-assistant");
s(this, "editor", null);
s(this, "popup", null);
s(this, "apiKey", "");
s(this, "driver", null);
s(this, "driverName", "openai");
s(this, "structurePrompt", l);
s(this, "prompt", n);
s(this, "driverOptions", {});
s(this, "toolbarButton", null);
s(this, "drivers", {});
this.loadSettings(), this.initializeDrivers();
}
initializeDrivers() {
this.drivers = {
openai: new y(this.apiKey),
deepseek: new g(this.apiKey),
huggingface: new f(this.apiKey),
github: new b(this.apiKey),
llama: new P(this.apiKey),
mistral: new O(this.apiKey),
ollama: new w(this.apiKey)
};
}
initialize(t) {
this.editor = t, this.setupPopup(), this.addToolbarButton();
}
addToolbarButton() {
var e;
const t = (e = this.editor) == null ? void 0 : e.getToolbar();
t && (this.toolbarButton = c({
icon: v,
title: "AI Assistant",
onClick: () => {
var i;
(i = this.popup) == null || i.show();
}
}), t.appendChild(this.toolbarButton));
}
createPopupItems() {
const t = [
{
type: "list",
id: "driver-select",
label: "AI Driver:",
options: ["openai", "deepseek", "huggingface", "github", "llama", "mistral", "ollama"],
value: this.driverName,
onChange: (i) => {
this.driverName = i, this.saveSettings(), this.updatePopupContent();
}
},
{
type: "input",
id: "api-key-input",
label: "API Key:",
placeholder: "Enter your API key",
value: this.apiKey,
onChange: (i) => {
this.apiKey = i, this.saveSettings();
}
},
{
type: "textarea",
id: "structure-prompt-textarea",
label: "Structure Prompt:",
placeholder: "Enter your structure prompt",
value: this.structurePrompt,
onChange: (i) => {
this.structurePrompt = i, this.saveSettings();
}
},
{
type: "textarea",
id: "prompt-textarea",
label: "Prompt:",
placeholder: "Enter your prompt",
value: this.prompt,
onChange: (i) => {
this.prompt = i, this.saveSettings();
}
}
], e = this.getDriverOptionsDescription();
for (const [i, r] of Object.entries(e)) {
const a = {
type: r.type,
id: `driver-option-${i}`,
label: r.label,
options: r.type === "list" ? r.options : void 0,
value: this.driverOptions[i] || r.default,
onChange: (h) => {
this.driverOptions[i] = h, this.saveSettings();
}
};
r.min && (a.min = r.min), r.max && (a.max = r.max), t.push(a);
}
return t;
}
setupPopup() {
if (!this.editor) return;
const t = this.createPopupItems();
this.popup = new m(this.editor, {
title: "AI Assistant",
className: "ai-assistant",
closeOnClickOutside: !0,
buttons: [
{
label: "Generate",
variant: "primary",
onClick: () => this.handleGenerate()
},
{
label: "Cancel",
variant: "secondary",
onClick: () => {
var e;
return (e = this.popup) == null ? void 0 : e.hide();
}
}
],
items: t
// Передаем массив стандартных и динамических полей
});
}
updatePopupContent() {
if (!this.popup) return;
const t = this.createPopupItems();
this.popup.rerender(t);
}
getDriverOptionsDescription() {
const t = this.drivers[this.driverName];
return t ? t.getOptionsDescription() : {};
}
async handleGenerate() {
if (!(!this.editor || !this.popup)) {
if (this.popup.rerender([
{
type: "loader",
id: "loader"
}
// Другие элементы попапа
]), this.driver = this.drivers[this.driverName], !this.driver)
throw new Error("Unsupported driver");
try {
const t = `${this.structurePrompt}
${this.prompt}`, e = await this.driver.generateText(t, this.driverOptions);
this.editor.insertContent(e), this.popup.hide();
} catch (t) {
console.error("Error generating text:", t), alert("Failed to generate text. Please check your API key and try again.");
} finally {
const t = this.createPopupItems();
this.popup.rerender(t);
}
}
}
saveSettings() {
const t = {
apiKey: this.apiKey,
driverName: this.driverName,
prompt: this.prompt,
structurePrompt: this.structurePrompt,
driverOptions: this.driverOptions
};
localStorage.setItem(p, JSON.stringify(t));
}
loadSettings() {
const t = localStorage.getItem(p);
if (t) {
const e = JSON.parse(t);
this.apiKey = e.apiKey || "", this.driverName = e.driverName || "openai", this.prompt = e.prompt || n, this.structurePrompt = e.structurePrompt || l, this.driverOptions = e.driverOptions || {};
}
}
destroy() {
var t, e, i, r;
this.popup && (this.popup.destroy(), this.popup = null), this.toolbarButton && (this.toolbarButton.remove(), this.toolbarButton = null), (t = this.editor) == null || t.off("ai-assistant"), (e = this.editor) == null || e.off("ai-request"), (i = this.editor) == null || i.off("ai-response"), (r = this.editor) == null || r.off("ai-error"), this.editor = null, this.driver = null, this.driverOptions = {};
}
}
export {
G as AIAssistantPlugin
};