From 70ce70dbd67b7a61948508f1d1ac45009f6f656a Mon Sep 17 00:00:00 2001 From: ymq1 Date: Thu, 8 May 2025 07:05:42 +0000 Subject: [PATCH] bugfix --- bricks/form.js | 1 + bricks/input.js | 63 +++++++++++++++++++++---------------------------- bricks/tab.js | 19 +++++++++++---- 3 files changed, 43 insertions(+), 40 deletions(-) diff --git a/bricks/form.js b/bricks/form.js index 6c7fa5c..c2e62c5 100644 --- a/bricks/form.js +++ b/bricks/form.js @@ -338,6 +338,7 @@ bricks.Form = class extends bricks.FormBase { height:"100%", value:f.value }); + txtw.bind('active', txtw.focus.bind(txtw)); tp_options.items.push({ name:f.name, label:f.label, diff --git a/bricks/input.js b/bricks/input.js index 0b6d7cf..ca0d5b7 100644 --- a/bricks/input.js +++ b/bricks/input.js @@ -856,12 +856,19 @@ bricks.UiText =class extends bricks.UiType { this.handle_tab_indent(erase); break; case 'Enter': - e.preventDefault(); // 阻止默认Tab行为 + e.preventDefault(); // 阻止默认行为 this.handle_enter(); + break; + case 'ArrowUp': + case 'ArrowDown': default: break; } } + set_editor_focus(editor, pos){ + editor.selectionStart = editor.selectionEnd = pos; + editor.focus(); + } handle_enter(){ var editor = this.dom_element; const cursorPos = editor.selectionStart; @@ -870,6 +877,8 @@ bricks.UiText =class extends bricks.UiType { const after = value.substring(cursorPos); editor.value = before + '\n' + after; editor.selectionStart = editor.selectionEnd = cursorPos + 1; + editor.focus(); + schedule_once(this.set_editor_focus.bind(this, editor, cursorPos+1), 0.5); } handle_tab_indent(erase){ /* erase == true : delete tabspace @@ -877,6 +886,7 @@ bricks.UiText =class extends bricks.UiType { add tabspace */ var editor = this.dom_element; + var indent_cnt; const cursorPos = editor.selectionStart; const value = editor.value; @@ -884,49 +894,30 @@ bricks.UiText =class extends bricks.UiType { const beforeCursor = value.substring(0, cursorPos); const lastLineStart = beforeCursor.lastIndexOf("\n") + 1; // 上一行的结束位置 const currentLineStart = lastLineStart; // 当前行的开始位置 - - // 获取当前行的文本 - const currentLine = value.substring(currentLineStart, value.indexOf("\n", currentLineStart) !== -1 ? value.indexOf("\n", currentLineStart) : undefined); - - // 判断当前行的缩进(获取行首的空格数) - const leadingSpaces = currentLine.match(/^\s*/)[0]; - const spacesCount = leadingSpaces.length; - - // 设置插入的空格数量(确保每次按Tab后光标前总是4个空格的缩进) - let indentationCount = 0; - if (spacesCount % 4 === 0) { - indentationCount = 4; // 如果已经是4的倍数,插入4个空格 - } else if (spacesCount % 4 === 1) { - indentationCount = 3; // 如果有1个空格,插入3个空格 - } else if (spacesCount % 4 === 2) { - indentationCount = 2; // 如果有2个空格,插入2个空格 - } else if (spacesCount % 4 === 3) { - indentationCount = 1; // 如果有3个空格,插入1个空格 - } - - // 处理Tab键按下 + const cur_col = cursorPos - currentLineStart; + var pos; if (!erase){ + indent_cnt = 4 - cur_col % 4; + if (indent_cnt == 0) indent_cnt = 4; // 根据光标前的空格数动态插入合适数量的空格 const before = value.substring(0, cursorPos); const after = value.substring(cursorPos); - - const indentation = ' '.repeat(indentationCount); // 生成合适数量的空格 - - // 更新文本框的值,插入缩进 + const indentation = ' '.repeat(indent_cnt); // 生成合适数量的空格 editor.value = before + indentation + after; - // 更新光标位置(将光标移到插入的缩进后面) - editor.selectionStart = editor.selectionEnd = cursorPos + indentationCount; + pos = cursorPos + indent_cnt; + editor.selectionStart = editor.selectionEnd = cursorPos + indent_cnt; } else { - // 处理Shift + Tab(减少缩进) - // 如果当前行有4个空格的缩进,删除它 - if (spacesCount >= 4) { - const before = value.substring(0, cursorPos - 4); - const after = value.substring(cursorPos); - editor.value = before + after; - editor.selectionStart = editor.selectionEnd = cursorPos - 4; - } + indent_cnt = cur_col % 4; + if (indent_cnt == 0) indent_cnt = 4; + const before = value.substring(0, cursorPos - indent_cnt); + const after = value.substring(cursorPos); + editor.value = before + after; + pos = cursorPos - indent_cnt; + editor.selectionStart = editor.selectionEnd = cursorPos - indent_cnt; } + editor.focus(); + schedule_once(this.set_editor_focus.bind(this, editor, pos), 0.5); } } diff --git a/bricks/tab.js b/bricks/tab.js index 8174ce0..b0da7b3 100644 --- a/bricks/tab.js +++ b/bricks/tab.js @@ -25,6 +25,13 @@ bricks.TabPanel = class extends bricks.Layout { tab-button-active tab-button-hover tab-content + + events: + switch: fired when switch content + event data: actived content widget + + content wedget event: + active: fired when the content event is switch to show */ constructor(options){ super(options); @@ -87,9 +94,8 @@ bricks.TabPanel = class extends bricks.Layout { if (tdesc.name == items[i].name){ var w = objget(this.content_buffer, tdesc.name); if (w && ! items[i].refresh){ - this.content_container.clear_widgets(); - this.content_container.add_widget(w); this.cur_tab_name = name; + this.switch_content(w); return; } try { @@ -103,9 +109,8 @@ bricks.TabPanel = class extends bricks.Layout { return; } this.content_buffer[tdesc.name] = w; - this.content_container.clear_widgets(); - this.content_container.add_widget(w); this.cur_tab_name = tdesc.name; + this.switch_content(w); return; } catch (e) { @@ -115,6 +120,12 @@ bricks.TabPanel = class extends bricks.Layout { } bricks.debug('TabPanel(): click event handled but noting to do', tdesc) } + switch_content(w){ + this.content_container.clear_widgets(); + this.content_container.add_widget(w); + this.dispatch('switch', w); + w.dispatch('active'); + } add_tab(desc){ var item = this.toolbar.createTool(desc); if (desc.removable){