@@ -154,8 +154,11 @@ function ACE_RunCode(editor) {
154154 return ;
155155 }
156156 try {
157- eval ( code ) ;
157+ // 使用 IIFE:定义并立即调用 async 函数
158+ const newcode = `(async () => {\n ${ code } \n})();` ;
159+ eval ( newcode ) ;
158160 } catch ( error ) {
161+ eda . sys_Message . showToastMessage ( '啊偶,执行出错了' , 'error' , 2 ) ;
159162 console . error ( '执行出错:' , error ) ;
160163 }
161164}
@@ -950,3 +953,41 @@ function ExportFileForJs(text, filename = 'script.js') {
950953 document . body . removeChild ( link ) ;
951954 URL . revokeObjectURL ( url ) ;
952955}
956+
957+ /**
958+ * 格式化 Ace 编辑器中的 JavaScript 代码,并将光标移至原光标所在行的行末
959+ *
960+ * @param {Object } editor - Ace 编辑器实例(必须已初始化)
961+ * @throws {Error } 如果 editor 无效或无法获取内容
962+ * @returns {void }
963+ */
964+ function formatEditorCode ( editor ) {
965+ if ( ! editor || typeof editor . getValue !== 'function' ) {
966+ throw new Error ( 'Invalid Ace editor instance provided.' ) ;
967+ }
968+ const rawCode = editor . getValue ( ) ;
969+ // 若内容为空或仅空白,清空编辑器并退出
970+ if ( ! rawCode || rawCode . trim ( ) === '' ) {
971+ editor . setValue ( '' , - 1 ) ;
972+ return ;
973+ }
974+ // 记录当前光标所在行(0-based)
975+ const originalRow = editor . getCursorPosition ( ) . row ;
976+ // 使用 js-beautify 格式化代码
977+ const formattedCode = js_beautify ( rawCode , {
978+ indent_size : 4 ,
979+ space_in_empty_paren : true ,
980+ end_with_newline : false ,
981+ } ) ;
982+ // 应用格式化后的内容
983+ editor . setValue ( formattedCode , - 1 ) ;
984+ // 获取格式化后的总行数(getLength() 返回行数,0 行时为 1)
985+ const session = editor . getSession ( ) ;
986+ const totalRows = session . getLength ( ) - 1 ; // 最大有效行索引(0-based)
987+ // 确保目标行不越界
988+ const targetRow = Math . min ( originalRow , totalRows ) ;
989+ // 获取该行内容长度(即行末列位置)
990+ const lineEndColumn = session . getLine ( targetRow ) . length ;
991+ // 移动光标到目标行末尾(Ace 的 gotoLine 行号是 1-based)
992+ editor . gotoLine ( targetRow + 1 , lineEndColumn , false ) ;
993+ }
0 commit comments