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
| from fasthtml.common import * from .toolbar import Toolbar
editor_script = Script(""" let editor; let completionTippy; let currentCompletion = '';
function initEditor() { editor = ace.edit("editor"); editor.setTheme("ace/theme/monokai"); editor.session.setMode("ace/mode/javascript"); editor.setOptions({ fontSize: "14px", showPrintMargin: false, showGutter: true, highlightActiveLine: true, wrap: true }); editor.setValue("// Your code here"); window.addEventListener('resize', function() { editor.resize(); });
document.getElementById('language').addEventListener('change', function(e) { let mode = "ace/mode/" + e.target.value; editor.session.setMode(mode); });
editor.session.on('change', function(delta) { if (delta.action === 'insert' && (delta.lines[0] === '.' || delta.lines[0] === ' ')) { showCompletionSuggestion(); } });
completionTippy = tippy(document.getElementById('editor'), { content: 'Loading...', trigger: 'manual', placement: 'top-start', arrow: true, interactive: true });
// Override the default tab behavior editor.commands.addCommand({ name: 'insertCompletion', bindKey: {win: 'Tab', mac: 'Tab'}, exec: function(editor) { if (currentCompletion) { editor.insert(currentCompletion); currentCompletion = ''; completionTippy.hide(); } else { editor.indent(); } } }); }
async function showCompletionSuggestion() { const cursorPosition = editor.getCursorPosition(); const screenPosition = editor.renderer.textToScreenCoordinates(cursorPosition.row, cursorPosition.column);
completionTippy.setContent('Loading...'); completionTippy.setProps({ getReferenceClientRect: () => ({ width: 0, height: 0, top: screenPosition.pageY, bottom: screenPosition.pageY, left: screenPosition.pageX, right: screenPosition.pageX, }) }); completionTippy.show();
try { const response = await fetch('/complete', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ code: editor.getValue(), row: cursorPosition.row, column: cursorPosition.column }), }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); currentCompletion = data.completion; completionTippy.setContent(`${currentCompletion} (Press Tab to insert)`); } catch (error) { console.error('Error:', error); completionTippy.setContent('Error fetching completion'); currentCompletion = ''; }
setTimeout(() => { if (currentCompletion) { completionTippy.hide(); currentCompletion = ''; } }, 5000); }
document.addEventListener('DOMContentLoaded', initEditor); """)
def CodeEditor(): return ( Div( Toolbar(), Div( Div(id="editor", cls="w-full h-full"), Script(""" me().on('contextmenu', ev => { ev.preventDefault() me('#context-menu').send('show', {x: ev.pageX, y: ev.pageY}) }) """), cls="flex-grow w-full" ), cls="flex flex-col h-screen w-full" ), editor_script )
|