fix(editor): prevent floating toolbar from hiding on button click#350
Conversation
|
Note |
Add toolbarButtonClicked flag to prevent air popover toolbar from
being hidden when user clicks toolbar buttons. The flag is set on
mousedown and reset after click, blocking concurrent hide events.
添加toolbarButtonClicked标志位,防止点击悬浮工具栏按钮时工具栏被误隐藏。
该标志在mousedown时设置,click后重置,阻止并发的隐藏事件。
Log: 修复悬浮工具栏点击按钮后被误隐藏的问题
PMS: BUG-354381
Influence: 修复后用户点击悬浮编辑工具栏上的按钮时,工具栏不再被误隐藏,
编辑操作可以正常完成。
d8e7ae2 to
756366b
Compare
deepin pr auto review你好!我是CodeGeeX。我已仔细审查了你提供的 Git Diff 代码。 这段代码的主要目的是解决一个前端交互冲突:当用户点击悬浮工具栏(air popover)上的按钮时,会触发选区变化或右键菜单逻辑,导致工具栏被意外隐藏。你通过引入一个全局标志位 以下是详细的审查意见和改进建议: 1. 语法与逻辑
2. 代码质量
3. 代码性能
4. 代码安全
💡 重构建议与改进代码最优雅的解决“点击内部元素不触发外部隐藏”的方法是利用事件对象的传播机制,而不是用全局变量和定时器去“猜”时序。 当点击工具栏按钮时, 但考虑到你的 改进后的代码:// 使用 IIFE 封装,避免污染全局命名空间
(function() {
// 常量定义,消除魔法数字
const TOOLBAR_HIDE_DELAY = 100;
// 将状态变量限制在闭包作用域内
let hideToolbarTimer = null;
let toolbarButtonClicked = false;
// 监听悬浮工具栏按钮的 mousedown 事件
// 建议将事件委托挂载在更近的父级,如果 #summernote 是父级,则改为 $('#summernote').on(...)
$(document).on('mousedown', '.note-air-popover .note-btn', function(e) {
e.preventDefault();
// 标记点击发生
toolbarButtonClicked = true;
if (hideToolbarTimer) {
clearTimeout(hideToolbarTimer);
hideToolbarTimer = null;
}
});
// 移除 click 事件中的 setTimeout,改为在 mousedown 时利用 event.stopPropagation()
// 或者在确定安全的同步逻辑中重置状态。
// 如果必须在 click 重置,去掉延迟:
$(document).on('click', '.note-air-popover .note-btn', function(e) {
// click 事件发生后,危险期已过,可以立即重置状态
toolbarButtonClicked = false;
});
// 右键隐藏悬浮工具栏
window.hideRightMenu = function() { // 如果需要外部访问,挂载到 window
// 如果刚刚点击了工具栏,阻止隐藏,并重置状态
if (toolbarButtonClicked) {
toolbarButtonClicked = false;
return;
}
if (hideToolbarTimer) clearTimeout(hideToolbarTimer);
hideToolbarTimer = setTimeout(function() {
$('#summernote').summernote('airPopover.hide');
hideToolbarTimer = null; // 执行完毕清理定时器引用
}, TOOLBAR_HIDE_DELAY);
};
})();
// 对应 changeContent 函数的修改
function changeContent(we, contents, $editable) {
if ($editable.summernote('isEmpty')) {
$('.note-editable').html('<p><br></p>')
}
if (webobj && initFinish) {
// 这里的逻辑保持不变,因为闭包内的 toolbarButtonClicked 无法直接在这里访问
// 如果采用闭包,可以将 toolbarButtonClicked 挂载到 window,或者提供一个 getter 方法
// 为了兼容现有架构,建议保留全局变量,但加上命名空间,例如:window.AppState.toolbarButtonClicked
}
}如果不使用闭包(最小改动方案):如果你不想改动太大影响其他地方对变量的读取,请至少做以下优化: var hideToolbarTimer = null;
var toolbarButtonClicked = false;
var TOOLBAR_HIDE_DELAY = 100; // 提取魔法数字
$(document).on('mousedown', '.note-air-popover .note-btn', function(e) {
e.preventDefault();
toolbarButtonClicked = true;
if (hideToolbarTimer) {
clearTimeout(hideToolbarTimer);
hideToolbarTimer = null;
}
});
// 移除 setTimeout,click 事件触发说明按钮点击流程已完成,同步重置即可
$(document).on('click', '.note-air-popover .note-btn', function(e) {
toolbarButtonClicked = false;
});
function hideRightMenu() {
if (toolbarButtonClicked) {
toolbarButtonClicked = false;
return;
}
if (hideToolbarTimer) clearTimeout(hideToolbarTimer);
hideToolbarTimer = setTimeout(function() {
$('#summernote').summernote('airPopover.hide');
}, TOOLBAR_HIDE_DELAY);
}总结:核心改进点是移除 |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: lzwind, pengfeixx The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/merge |
Add toolbarButtonClicked flag to prevent air popover toolbar from being hidden when user clicks toolbar buttons. The flag is set on mousedown and reset after click, blocking concurrent hide events.
添加toolbarButtonClicked标志位,防止点击悬浮工具栏按钮时工具栏被误隐藏。
该标志在mousedown时设置,click后重置,阻止并发的隐藏事件。
Also: simplify lrelease path detection and filter JS console output to Warning level in QML.
同时简化翻译脚本中lrelease路径检测,并优化QML端JS日志过滤。
Log: 修复悬浮工具栏点击按钮后被误隐藏的问题
PMS: BUG-354381
Influence: 修复后用户点击悬浮编辑工具栏上的按钮时,工具栏不再被误隐藏,
编辑操作可以正常完成。