Skip to content

Commit

Permalink
fix: 拖动排序和Tab记忆功能一起启用时拖动后应该也保持拖动后的顺序 (#1)
Browse files Browse the repository at this point in the history
Signed-off-by: ajiho <lujiahao@88.com>
  • Loading branch information
ajiho committed Jun 1, 2024
1 parent 9f644de commit c0a6d56
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 30 deletions.
48 changes: 48 additions & 0 deletions demo/issues1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html lang="zh">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="dist/css/quicktab.css">

</head>

<body>

<button onclick="add()">添加Tab</button>
<div id="qtab"></div>

<script src="dist/js/quicktab.js"></script>
<script>

const tab = new Quicktab('#qtab', {
id:'qtab',
defaultTabs: [
{
title:'欢迎页面1',
url: 'welcome.html'
},
{
title:'欢迎页面2',
url: 'welcome.html?id=10'
}
],
tab:{
dragSort:true,
remember:true,
}
})

function add(){
tab.addTab({
title:'新标签'+Math.random(),
url:'welcome.html?id='+ Math.random()
})
}

</script>
</body>

</html>
48 changes: 48 additions & 0 deletions src/docs/public/demo/issues1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html lang="zh">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="dist/css/quicktab.css">

</head>

<body>

<button onclick="add()">添加Tab</button>
<div id="qtab"></div>

<script src="dist/js/quicktab.js"></script>
<script>

const tab = new Quicktab('#qtab', {
id:'qtab',
defaultTabs: [
{
title:'欢迎页面1',
url: 'welcome.html'
},
{
title:'欢迎页面2',
url: 'welcome.html?id=10'
}
],
tab:{
dragSort:true,
remember:true,
}
})

function add(){
tab.addTab({
title:'新标签'+Math.random(),
url:'welcome.html?id='+ Math.random()
})
}

</script>
</body>

</html>
56 changes: 56 additions & 0 deletions src/docs/public/demo/issues4 copy.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!DOCTYPE html>
<html lang="zh">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="dist/css/quicktab.css">

<style>

.container{
position: fixed;
/* height: 10px; */
inset: 0;
}
</style>
</head>

<body>




<div class="container">
<div id="qtab"></div>
</div>



<script src="dist/js/quicktab.js"></script>
<script>
// 1.父元素的样式如果是设置的固定定位或者绝对定位时也没有设置固定高度
// 2.同时选项的高度设置100%时 会引发该bug

const tab = new Quicktab('#qtab',{
defaultTabs:[
{
url:'welcome.html'
},
{
url:'welcome.html?id=10'
}
],

height:'100%',
toolbar:{
dropdown:{
enable:true//启用下拉菜单
}
}
})

</script>
</body>
</html>
67 changes: 45 additions & 22 deletions src/js/quicktab.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class Quicktab {
//参数额外处理
this.#optionsProcess(options)


//参数验证
const result = Utils.validate(OptionsSchema, this.#options)
if (result !== true) {
Expand Down Expand Up @@ -690,28 +691,33 @@ class Quicktab {
)

//响应式处理
Utils.onResize(this.#element.parentNode, function (entry) {
//关闭tab右键菜单和下拉菜单
that.#closeContextmenu()
that.#closeDropdown()


if (that.#options.responsive.enable !== false) {
//如果启用了响应式就动态设置显示和隐藏
Utils.setProperty(
that.#containerEl,
that.#hideItemSelector,
'display',
entry.contentRect.width < that.#options.responsive.breakpoint ? 'none' : null,
)
}
Utils.onResize(
this.#element.parentNode,
function (entry) {
//关闭tab右键菜单和下拉菜单
that.#closeContextmenu()
that.#closeDropdown()

if (that.#options.tab.resizeCenterActive === true) {
that.#debounceCenterActive()
}
},{
type:'width'
})
if (that.#options.responsive.enable !== false) {
//如果启用了响应式就动态设置显示和隐藏
Utils.setProperty(
that.#containerEl,
that.#hideItemSelector,
'display',
entry.contentRect.width < that.#options.responsive.breakpoint
? 'none'
: null,
)
}

if (that.#options.tab.resizeCenterActive === true) {
that.#debounceCenterActive()
}
},
{
type: 'width',
},
)

//添加通过html属性添加tab的能力(这个非常方便)
that.#dataAttrAddTabEventRegister(document, Constants.DATAKEYS.addTabTarget)
Expand Down Expand Up @@ -965,6 +971,23 @@ class Quicktab {
//拖拽结束
event(this.#toolbarItemTabWrapperEl).on('dragend', function () {
dragging = null

//判断是否启用了tab缓存
if(that.#options.tab.remember === true){

that.#cacheHandle.delete(that.#cacheKey)



that.#getTabs().forEach(item=>{
const option = item[Constants.DATAKEYS.tabOptionDataKey];
delete option.timestamp
that.#addCacheTab(option)
})

}


})
}

Expand Down Expand Up @@ -1601,7 +1624,7 @@ class Quicktab {
#cacheTabsCheck(tabs) {
//要检查的键数组
let targetKeys = [
...Object.keys(Constants.TABDEFAULTS),
...Object.keys(Constants.OPTIONS.TabDefault),
Constants.CLASSES.tabActive,
]

Expand Down
2 changes: 1 addition & 1 deletion src/js/schema/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const ContextmenuItemSchema = v.object({
})

const OptionsSchema = v.object({
id: v.string(),
id: v.pipe(v.string(), v.minLength(1)),
minHeight: v.optional(v.string()),
height: v.optional(v.string()),
width: v.optional(v.string()),
Expand Down
8 changes: 1 addition & 7 deletions src/js/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ export default {
},

onResize(element, callback, options) {

const Default = {
//是否立即执行
immediate: false,
Expand All @@ -262,8 +261,6 @@ export default {

options = Object.assign(Default, typeof options === 'object' && options)



const resizeObserver = new ResizeObserver((entries) => {
// 处理大小变化的回调函数
entries.forEach((entry) => {
Expand All @@ -275,9 +272,6 @@ export default {
return
}
}

console.log(options);

// 获取当前元素的宽度和高度
const newWidth = entry.contentRect.width
const newHeight = entry.contentRect.height
Expand All @@ -303,7 +297,7 @@ export default {
})
})
resizeObserver.observe(element)
return resizeObserver;
return resizeObserver
},

// 获取开启和激活的选项
Expand Down

0 comments on commit c0a6d56

Please sign in to comment.