-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 标签页可拖拽 - 修改持久化标签配置参数名 - 优化useSortable hooks 逻辑 - 支持删除拖拽功能
- Loading branch information
Showing
9 changed files
with
94 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,54 @@ | ||
import { tryOnMounted } from '@vueuse/core'; | ||
import type { Options } from 'sortablejs'; | ||
import Sortable from 'sortablejs'; | ||
import { tryOnMounted } from '@vueuse/core'; | ||
import type { Ref } from 'vue'; | ||
import { unref } from 'vue'; | ||
import { isRef, ref, unref } from 'vue'; | ||
|
||
function useSortable(el: Ref<HTMLElement | null>, options?: Options) { | ||
tryOnMounted(() => { | ||
if (!unref(el)) { | ||
console.error('SortableHooks Unable To Get HTML Element'); | ||
return; | ||
} | ||
Sortable.create(unref(el as Ref<HTMLElement>), { | ||
group: 'name', | ||
animation: 500, | ||
/** | ||
* | ||
* @param options sortableJs配置项 | ||
* @param elRef Ref<HTMLElement | null> | ||
* @returns | ||
* - initSortable(el: Ref<HTMLElement | null> | (HTMLElement | null)): 初始化排序功能。 | ||
* - destroy: 销毁排序实例,并完全删除可排序功能。 | ||
* - sortableJs: SortableJS 实例。 | ||
*/ | ||
|
||
function useSortable(options?: Options, elRef?: Ref<HTMLElement | null>) { | ||
const sortableJs = ref<Sortable | null>(null); | ||
|
||
const createSortable = (el: HTMLElement) => { | ||
sortableJs.value = new Sortable(el, { | ||
animation: 300, | ||
delay: 400, | ||
delayOnTouchOnly: true, | ||
...options, | ||
}); | ||
}; | ||
|
||
const initSortable = (el: Ref<HTMLElement | null> | (HTMLElement | null)) => { | ||
if (sortableJs.value) return; | ||
|
||
let element: HTMLElement | null; | ||
if (isRef(el)) { | ||
element = unref(el); | ||
} else { | ||
element = el; | ||
} | ||
|
||
if (element) createSortable(element); | ||
}; | ||
|
||
tryOnMounted(() => { | ||
if (elRef) initSortable(elRef); | ||
}); | ||
|
||
const destroy = () => { | ||
sortableJs.value?.destroy(); | ||
sortableJs.value = null; | ||
}; | ||
|
||
return { initSortable, destroy, sortableJs }; | ||
} | ||
|
||
export default useSortable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters