This commit is contained in:
ymq1 2025-05-08 07:05:42 +00:00
parent 446b78b0f6
commit 70ce70dbd6
3 changed files with 43 additions and 40 deletions

View File

@ -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,

View File

@ -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);
}
}

View File

@ -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){