UNPKG

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

15 lines (13 loc) 5.82 kB
/*! 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 */ "use strict";var h=Object.defineProperty;var d=(o,e,t)=>e in o?h(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var s=(o,e,t)=>d(o,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});;/* empty css */;/* empty css */const c=require("../../core/ui/PopupManager.cjs"),m=require("../ToolbarPlugin/utils.cjs"),v=require("../../icons/ai-assistant.svg.cjs"),g=require("./drivers/DeepSeekDriver.cjs"),y=require("./drivers/HuggingFaceDriver.cjs"),b=require("./drivers/OpenAIDriver.cjs"),f=require("./drivers/GitHubAzureDriver.cjs"),P=require("./drivers/LlamaDriver.cjs"),D=require("./drivers/MistralDriver.cjs"),O=require("./drivers/OllamaDriver.cjs"),n="aiAssistantSettings",p="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 A{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",p);s(this,"driverOptions",{});s(this,"toolbarButton",null);s(this,"drivers",{});this.loadSettings(),this.initializeDrivers()}initializeDrivers(){this.drivers={openai:new b.OpenAIDriver(this.apiKey),deepseek:new g.DeepSeekDriver(this.apiKey),huggingface:new y.HuggingFaceDriver(this.apiKey),github:new f.GitHubAzureDriver(this.apiKey),llama:new P.LlamaDriver(this.apiKey),mistral:new D.MistralDriver(this.apiKey),ollama:new O.OllamaDriver(this.apiKey)}}initialize(e){this.editor=e,this.setupPopup(),this.addToolbarButton()}addToolbarButton(){var t;const e=(t=this.editor)==null?void 0:t.getToolbar();e&&(this.toolbarButton=m.createToolbarButton({icon:v.default,title:"AI Assistant",onClick:()=>{var i;(i=this.popup)==null||i.show()}}),e.appendChild(this.toolbarButton))}createPopupItems(){const e=[{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()}}],t=this.getDriverOptionsDescription();for(const[i,r]of Object.entries(t)){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:u=>{this.driverOptions[i]=u,this.saveSettings()}};r.min&&(a.min=r.min),r.max&&(a.max=r.max),e.push(a)}return e}setupPopup(){if(!this.editor)return;const e=this.createPopupItems();this.popup=new c.PopupManager(this.editor,{title:"AI Assistant",className:"ai-assistant",closeOnClickOutside:!0,buttons:[{label:"Generate",variant:"primary",onClick:()=>this.handleGenerate()},{label:"Cancel",variant:"secondary",onClick:()=>{var t;return(t=this.popup)==null?void 0:t.hide()}}],items:e})}updatePopupContent(){if(!this.popup)return;const e=this.createPopupItems();this.popup.rerender(e)}getDriverOptionsDescription(){const e=this.drivers[this.driverName];return e?e.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 e=`${this.structurePrompt} ${this.prompt}`,t=await this.driver.generateText(e,this.driverOptions);this.editor.insertContent(t),this.popup.hide()}catch(e){console.error("Error generating text:",e),alert("Failed to generate text. Please check your API key and try again.")}finally{const e=this.createPopupItems();this.popup.rerender(e)}}}saveSettings(){const e={apiKey:this.apiKey,driverName:this.driverName,prompt:this.prompt,structurePrompt:this.structurePrompt,driverOptions:this.driverOptions};localStorage.setItem(n,JSON.stringify(e))}loadSettings(){const e=localStorage.getItem(n);if(e){const t=JSON.parse(e);this.apiKey=t.apiKey||"",this.driverName=t.driverName||"openai",this.prompt=t.prompt||p,this.structurePrompt=t.structurePrompt||l,this.driverOptions=t.driverOptions||{}}}destroy(){var e,t,i,r;this.popup&&(this.popup.destroy(),this.popup=null),this.toolbarButton&&(this.toolbarButton.remove(),this.toolbarButton=null),(e=this.editor)==null||e.off("ai-assistant"),(t=this.editor)==null||t.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={}}}exports.AIAssistantPlugin=A;