chromamind-nexus / components /code-editor.js
ChatEngineAi's picture
can you give it a brain database, so it knows basic things, like understanding like im writing to you now, and add another menu where you can paste or write in code adding to (script.js). Do not change the interface cause i liked it. Can you also make so the log in sign up works with email and password
e3c44fe verified
Raw History Blame Contribute Delete
15 kB
class CodeEditor extends HTMLElement {
connectedCallback() {
this.render();
}
render() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<style>
:host {
display: block;
}
.code-panel {
background: rgba(15, 23, 42, 0.8);
border: 1px solid rgba(99, 102, 241, 0.2);
border-radius: 16px;
padding: 20px;
}
.panel-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 16px;
padding-bottom: 16px;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.panel-title {
font-size: 1.25rem;
font-weight: 600;
color: white;
display: flex;
align-items: center;
gap: 12px;
}
.icon-box {
width: 40px;
height: 40px;
border-radius: 12px;
background: linear-gradient(135deg, #6366f1 0%, #06b6d4 100%);
display: flex;
align-items: center;
justify-content: center;
}
.actions {
display: flex;
gap: 8px;
}
.btn {
padding: 8px 16px;
border-radius: 8px;
font-size: 0.875rem;
font-weight: 500;
cursor: pointer;
transition: all 0.2s;
border: none;
display: flex;
align-items: center;
gap: 8px;
}
.btn-primary {
background: linear-gradient(135deg, #6366f1 0%, #06b6d4 100%);
color: white;
}
.btn-primary:hover {
opacity: 0.9;
transform: scale(1.02);
}
.btn-secondary {
background: rgba(255, 255, 255, 0.1);
color: #9ca3af;
}
.btn-secondary:hover {
background: rgba(255, 255, 255, 0.15);
color: white;
}
.editor-container {
position: relative;
}
.code-textarea {
width: 100%;
min-height: 400px;
background: rgba(2, 6, 23, 0.8);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 12px;
padding: 16px;
color: #e2e8f0;
font-family: 'SF Mono', 'Monaco', 'Consolas', monospace;
font-size: 0.875rem;
line-height: 1.6;
resize: vertical;
tab-size: 2;
}
.code-textarea:focus {
outline: none;
border-color: rgba(99, 102, 241, 0.5);
}
.code-textarea::placeholder {
color: #64748b;
}
.status-bar {
display: flex;
align-items: center;
gap: 16px;
margin-top: 12px;
padding: 12px 16px;
background: rgba(2, 6, 23, 0.6);
border-radius: 8px;
font-size: 0.75rem;
color: #64748b;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #22c55e;
animation: pulse 2s infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.success-message {
display: none;
align-items: center;
gap: 8px;
padding: 12px 16px;
background: rgba(34, 197, 94, 0.1);
border: 1px solid rgba(34, 197, 94, 0.3);
border-radius: 8px;
color: #22c55e;
margin-top: 12px;
font-size: 0.875rem;
}
.success-message.show {
display: flex;
animation: fadeIn 0.3s ease;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(-10px); }
to { opacity: 1; transform: translateY(0); }
}
.help-text {
margin-top: 16px;
padding: 12px 16px;
background: rgba(99, 102, 241, 0.1);
border-radius: 8px;
border-left: 3px solid #6366f1;
}
.help-text h4 {
color: #6366f1;
font-size: 0.875rem;
font-weight: 600;
margin: 0 0 8px 0;
}
.help-text ul {
margin: 0;
padding-left: 16px;
color: #9ca3af;
font-size: 0.8rem;
line-height: 1.6;
}
.help-text li {
margin-bottom: 4px;
}
code {
background: rgba(255, 255, 255, 0.1);
padding: 2px 6px;
border-radius: 4px;
font-family: monospace;
font-size: 0.8em;
}
</style>
<div class="code-panel">
<div class="panel-header">
<div class="panel-title">
<div class="icon-box">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="color: white;"><polyline points="16 18 22 12 16 6"></polyline><polyline points="8 6 2 12 8 18"></polyline></svg>
</div>
Code Editor
<span style="font-size: 0.875rem; font-weight: 400; color: #9ca3af;">— Modify script.js directly</span>
</div>
<div class="actions">
<button class="btn btn-secondary" onclick="this.getRootNode().host.resetCode()">
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="23 4 23 10 17 10"></polyline><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"></path></svg>
Reset
</button>
<button class="btn btn-primary" onclick="this.getRootNode().host.saveCode()">
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"></path><polyline points="17 21 17 13 7 13 7 21"></polyline><polyline points="7 3 7 8 15 8"></polyline></svg>
Save to script.js
</button>
</div>
</div>
<div class="editor-container">
<textarea id="code-editor" class="code-textarea" spellcheck="false" placeholder="// Paste or write your JavaScript code here...
// This will be appended to script.js
// Example: Add a custom function
function myCustomFunction() {
console.log('Hello from custom code!');
return 'It works!';
}
// You can add event listeners, modify state, create new features...
document.addEventListener('DOMContentLoaded', () => {
// Your initialization code
});">// Your code will appear here...</textarea>
</div>
<div class="status-bar">
<span class="status-dot"></span>
<span id="line-count">1 line</span>
<span>|</span>
<span id="char-count">0 characters</span>
<span>|</span>
<span>UTF-8</span>
<span style="margin-left: auto;">Changes auto-saved to session</span>
</div>
<div id="save-success" class="success-message">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path><polyline points="22 4 12 14.01 9 11.01"></polyline></svg>
<span>Code saved successfully! Refresh to apply changes.</span>
</div>
<div class="help-text">
<h4>💡 How to use</h4>
<ul>
<li>Write or paste JavaScript code in the editor above</li>
<li>Click <b>Save to script.js</b> to store your code (saved to browser localStorage)</li>
<li>Your code is automatically loaded on page load and executed</li>
<li>Use <code>window.state</code> to access the app state, <code>window.brainDB</code> to access brain</li>
<li>Click <b>Reset</b> to clear your custom code (original script.js remains)</li>
</ul>
</div>
</div>
`;
this.setupEditor();
this.loadSavedCode();
}
setupEditor() {
const textarea = this.shadowRoot.querySelector('#code-editor');
// Handle tab key for indentation
textarea.addEventListener('keydown', (e) => {
if (e.key === 'Tab') {
e.preventDefault();
const start = textarea.selectionStart;
const end = textarea.selectionEnd;
textarea.value = textarea.value.substring(0, start) + ' ' + textarea.value.substring(end);
textarea.selectionStart = textarea.selectionEnd = start + 2;
}
});
// Update stats
textarea.addEventListener('input', () => {
this.updateStats();
this.saveSessionCode();
});
}
updateStats() {
const textarea = this.shadowRoot.querySelector('#code-editor');
const lines = textarea.value.split('\n').length;
const chars = textarea.value.length;
this.shadowRoot.querySelector('#line-count').textContent = `${lines} line${lines !== 1 ? 's' : ''}`;
this.shadowRoot.querySelector('#char-count').textContent = `${chars} character${chars !== 1 ? 's' : ''}`;
}
saveSessionCode() {
const code = this.shadowRoot.querySelector('#code-editor').value;
if (code && code !== '// Your code will appear here...') {
sessionStorage.setItem('nexusCodeDraft', code);
}
}
loadSavedCode() {
// First check session draft
let code = sessionStorage.getItem('nexusCodeDraft');
// Then check saved custom code
if (!code) {
code = localStorage.getItem('nexusCustomCode');
}
if (code && code.trim()) {
const textarea = this.shadowRoot.querySelector('#code-editor');
textarea.value = code;
this.updateStats();
}
}
saveCode() {
const code = this.shadowRoot.querySelector('#code-editor').value;
if (!code || code.trim() === '' || code === '// Your code will appear here...') {
alert('Please enter some code first!');
return;
}
// Store in localStorage
localStorage.setItem('nexusCustomCode', code);
sessionStorage.removeItem('nexusCodeDraft');
// Update saved codes list for script.js
let savedCodes = JSON.parse(localStorage.getItem('nexusSavedCodes') || '[]');
savedCodes.push({
code: code,
timestamp: new Date().toISOString(),
id: Date.now()
});
localStorage.setItem('nexusSavedCodes', JSON.stringify(savedCodes));
// Show success
const successMsg = this.shadowRoot.querySelector('#save-success');
successMsg.classList.add('show');
setTimeout(() => {
successMsg.classList.remove('show');
}, 4000);
// Execute the code
this.executeCode(code);
}
resetCode() {
if (!confirm('Clear all custom code? This cannot be undone.')) return;
this.shadowRoot.querySelector('#code-editor').value = '// Paste or write your JavaScript code here...\n// Your custom code goes here...\n';
this.updateStats();
localStorage.removeItem('nexusCustomCode');
sessionStorage.removeItem('nexusCodeDraft');
}
executeCode(code) {
try {
// Wrap in IIFE with access to app globals
const wrappedCode = `
(function() {
// Access to app globals
const state = window.state;
const brainDB = window.brainDB;
const APIs = window.APIs;
try {
${code}
} catch (err) {
console.error('Custom code error:', err);
}
})();
`;
// Use Function constructor for cleaner execution
new Function('state', 'brainDB', 'APIs', code)(
window.state,
window.brainDB,
window.APIs
);
console.log('✅ Custom code executed successfully');
} catch (error) {
console.error('❌ Error executing custom code:', error);
alert('Error in code: ' + error.message);
}
}
}
customElements.define('code-editor', CodeEditor);