fix: 拖拽图片文件夹时会影响文章文件夹的排序

This commit is contained in:
xiaozzzi 2024-04-08 02:16:41 +08:00
parent 9e561358eb
commit 9f0dedaae9
4 changed files with 75 additions and 20 deletions

View File

@ -583,7 +583,7 @@ const closeParentIfNoChild = (pid: string) => {
* 拖拽后处理各个节点排序 * 拖拽后处理各个节点排序
*/ */
const handleDrop = (drag: Node, enter: Node, dropType: NodeDropType, _event: DragEvents) => { const handleDrop = (drag: Node, enter: Node, dropType: NodeDropType, _event: DragEvents) => {
handleTreeDrop(drag, enter, dropType, _event, DocTreeRef, docTreeData, (needUpd) => { handleTreeDrop(drag, enter, dropType, _event, DocTreeRef, docTreeData, 1, (needUpd) => {
docUpdSortApi({ docs: needUpd, folderType: 1 }) docUpdSortApi({ docs: needUpd, folderType: 1 })
.then((resp) => { .then((resp) => {
docTreeData.value = resp.data docTreeData.value = resp.data

View File

@ -41,6 +41,8 @@ export interface NeedUpd {
* @param _event * @param _event
* @param DocTreeRef * @param DocTreeRef
* @param docTreeData * @param docTreeData
* @param folderType 文件夹类型: 1:|2:图片文件夹
* @param updateFn
*/ */
export const handleTreeDrop = ( export const handleTreeDrop = (
drag: Node, drag: Node,
@ -49,6 +51,7 @@ export const handleTreeDrop = (
_event: DragEvents, _event: DragEvents,
DocTreeRef: Ref, DocTreeRef: Ref,
docTreeData: Ref<DocTree[]>, docTreeData: Ref<DocTree[]>,
folderType: FolderType,
updateFn: (needUpd: NeedUpd[]) => void updateFn: (needUpd: NeedUpd[]) => void
) => { ) => {
// 是否同级别 // 是否同级别
@ -69,7 +72,7 @@ export const handleTreeDrop = (
}) })
} }
// console.log(`same: ${isSame}, dropType: ${dropType}, drag: ${dragSourceSort}, enter: ${enterSourceSort}`) console.log(`same: ${isSame}, dropType: ${dropType}, drag-srot: ${dragSourceSort}, enter-srot: ${enterSourceSort}`)
if (isSame) { if (isSame) {
// drag 在 enter 前 // drag 在 enter 前
if (dropType === 'before') { if (dropType === 'before') {
@ -79,7 +82,7 @@ export const handleTreeDrop = (
addUpd(drag) addUpd(drag)
for (let i = 0; i < enter.parent.childNodes.length; i++) { for (let i = 0; i < enter.parent.childNodes.length; i++) {
const node = enter.parent.childNodes[i] const node = enter.parent.childNodes[i]
if (node.data.s >= enterSourceSort && node.data.s < dragSourceSort && node.data.i != drag.data.i) { if (checkFolderType(node, folderType) && node.data.s >= enterSourceSort && node.data.s < dragSourceSort && node.data.i != drag.data.i) {
node.data.s += 1 node.data.s += 1
addUpd(node) addUpd(node)
} }
@ -91,7 +94,7 @@ export const handleTreeDrop = (
addUpd(drag) addUpd(drag)
for (let i = 0; i < enter.parent.childNodes.length; i++) { for (let i = 0; i < enter.parent.childNodes.length; i++) {
const node = enter.parent.childNodes[i] const node = enter.parent.childNodes[i]
if (node.data.s < enterSourceSort && node.data.s > dragSourceSort && node.data.i != drag.data.i) { if (checkFolderType(node, folderType) && node.data.s < enterSourceSort && node.data.s > dragSourceSort && node.data.i != drag.data.i) {
node.data.s -= 1 node.data.s -= 1
addUpd(node) addUpd(node)
} }
@ -107,7 +110,7 @@ export const handleTreeDrop = (
addUpd(drag) addUpd(drag)
for (let i = 0; i < enter.parent.childNodes.length; i++) { for (let i = 0; i < enter.parent.childNodes.length; i++) {
const node = enter.parent.childNodes[i] const node = enter.parent.childNodes[i]
if (node.data.s > enterSourceSort && node.data.s < dragSourceSort && node.data.i != drag.data.i) { if (checkFolderType(node, folderType) && node.data.s > enterSourceSort && node.data.s < dragSourceSort && node.data.i != drag.data.i) {
node.data.s += 1 node.data.s += 1
addUpd(node) addUpd(node)
} }
@ -117,7 +120,7 @@ export const handleTreeDrop = (
addUpd(drag) addUpd(drag)
for (let i = 0; i < enter.parent.childNodes.length; i++) { for (let i = 0; i < enter.parent.childNodes.length; i++) {
const node = enter.parent.childNodes[i] const node = enter.parent.childNodes[i]
if (node.data.s <= enterSourceSort && node.data.s > dragSourceSort && node.data.i != drag.data.i) { if (checkFolderType(node, folderType) && node.data.s <= enterSourceSort && node.data.s > dragSourceSort && node.data.i != drag.data.i) {
node.data.s -= 1 node.data.s -= 1
addUpd(node) addUpd(node)
} }
@ -132,7 +135,7 @@ export const handleTreeDrop = (
if (dragSourceParent) { if (dragSourceParent) {
for (let i = 0; i < dragSourceParent.childNodes.length; i++) { for (let i = 0; i < dragSourceParent.childNodes.length; i++) {
const node = dragSourceParent.childNodes[i] const node = dragSourceParent.childNodes[i]
if (node.data.s > dragSourceSort) { if (checkFolderType(node, folderType) && node.data.s > dragSourceSort) {
node.data.s -= 1 node.data.s -= 1
addUpd(node) addUpd(node)
} }
@ -160,7 +163,7 @@ export const handleTreeDrop = (
if (dragSourcePid === '0') { if (dragSourcePid === '0') {
for (let i = 0; i < docTreeData.value.length; i++) { for (let i = 0; i < docTreeData.value.length; i++) {
const node = docTreeData.value[i] const node = docTreeData.value[i]
if (node.s > dragSourceSort) { if (checkFolderTypeByOrigin(node, folderType) && node.s > dragSourceSort) {
node.s -= 1 node.s -= 1
needUpd.push({ i: node.i, p: node.p, n: node.n, s: node.s, ty: node.ty }) needUpd.push({ i: node.i, p: node.p, n: node.n, s: node.s, ty: node.ty })
} }
@ -170,7 +173,7 @@ export const handleTreeDrop = (
if (dragSourceParent) { if (dragSourceParent) {
for (let i = 0; i < dragSourceParent.childNodes.length; i++) { for (let i = 0; i < dragSourceParent.childNodes.length; i++) {
const node = dragSourceParent.childNodes[i] const node = dragSourceParent.childNodes[i]
if (node.data.s > dragSourceSort) { if (checkFolderType(node, folderType) && node.data.s > dragSourceSort) {
node.data.s -= 1 node.data.s -= 1
addUpd(node) addUpd(node)
} }
@ -178,13 +181,16 @@ export const handleTreeDrop = (
} }
} }
// 拖拽与被拖拽的文件夹属于不同的父级 > 将一个文件夹拖拽到另一个文件夹之前
if (dropType === 'before') { if (dropType === 'before') {
drag.data.p = enter.data.p drag.data.p = enter.data.p
drag.data.s = enterSourceSort drag.data.s = enterSourceSort
// 拖拽的文件夹需修改
addUpd(drag) addUpd(drag)
//
for (let i = 0; i < enter.parent.childNodes.length; i++) { for (let i = 0; i < enter.parent.childNodes.length; i++) {
const node = enter.parent.childNodes[i] const node = enter.parent.childNodes[i]
if (node.data.s >= enterSourceSort && node.data.i != drag.data.i) { if (checkFolderType(node, folderType) && node.data.s >= enterSourceSort && node.data.i != drag.data.i) {
node.data.s += 1 node.data.s += 1
addUpd(node) addUpd(node)
} }
@ -197,7 +203,7 @@ export const handleTreeDrop = (
addUpd(drag) addUpd(drag)
for (let i = 0; i < enter.parent.childNodes.length; i++) { for (let i = 0; i < enter.parent.childNodes.length; i++) {
const node = enter.parent.childNodes[i] const node = enter.parent.childNodes[i]
if (node.data.s > enterSourceSort && node.data.i != drag.data.i) { if (checkFolderType(node, folderType) && node.data.s > enterSourceSort && node.data.i != drag.data.i) {
node.data.s += 1 node.data.s += 1
addUpd(node) addUpd(node)
} }
@ -221,8 +227,8 @@ export const handleTreeDrop = (
if (needUpd.length > 0) { if (needUpd.length > 0) {
updateFn(needUpd) updateFn(needUpd)
} }
// console.log(needUpd) console.log(needUpd)
// console.log('==========================================') console.log('==========================================')
} }
/** /**
@ -239,3 +245,40 @@ export const getMaxSort = (drag: Node, nodes: Node[]): number => {
) )
return maxSort return maxSort
} }
/**
* ,
*
* @param node
* @param folderType
* @returns true/false
*/
const checkFolderType = (node: Node, folderType: FolderType): boolean => {
const ty = node.data.ty
if (folderType === 1) {
if (ty === 1 || ty === 3) {
return true
}
} else if (folderType === 2) {
if (ty === 2) {
return true
}
}
return false
}
const checkFolderTypeByOrigin = (node: DocTree, folderType: FolderType): boolean => {
const ty = node.ty
if (folderType === 1) {
if (ty === 1 || ty === 3) {
return true
}
} else if (folderType === 2) {
if (ty === 2) {
return true
}
}
return false
}

View File

@ -68,6 +68,9 @@ declare interface DocInfo {
syncTime?: string syncTime?: string
} }
/** 文件夹类型: 1:文章文件夹|2:图片文件夹 */
declare type FolderType = 1 | 2
/** 文档类型: 1:文章文件夹|2:图片文件夹|3:文章|11:分隔文档; */ /** 文档类型: 1:文章文件夹|2:图片文件夹|3:文章|11:分隔文档; */
declare type DocType = 1 | 2 | 3 | 11 declare type DocType = 1 | 2 | 3 | 11

View File

@ -120,10 +120,10 @@
<PictureInfo ref="PictureInfoRef" @saved="savedCallback"></PictureInfo> <PictureInfo ref="PictureInfoRef" @saved="savedCallback"></PictureInfo>
</el-dialog> </el-dialog>
<!-- <div class="doc-tree-debug"> <div class="doc-tree-debug">
<div>所有展开{{ Array.from(docTreeCurrentExpandId) + '' }}</div> <div>所有展开{{ Array.from(docTreeCurrentExpandId) + '' }}</div>
<div>当前选中{{ docTreeCurrentId }}</div> <div>当前选中{{ docTreeCurrentId }}</div>
</div> --> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
@ -240,7 +240,7 @@ const endLoading = () => {
const addTreeDivider = (data: any) => { const addTreeDivider = (data: any) => {
const docTree: DocTree[] = data const docTree: DocTree[] = data
// //
let lastPicIndex: number = docTree.length - 1 let lastPicIndex: number = -1
// //
for (let i = 0; i < docTree.length; i++) { for (let i = 0; i < docTree.length; i++) {
let doc = docTree[i] let doc = docTree[i]
@ -250,8 +250,9 @@ const addTreeDivider = (data: any) => {
} }
} }
// console.log(lastPicIndex, docTree.length)
docTree.splice(Math.max(lastPicIndex, 1), 0, {
const divider: DocTree = {
i: (Number(docTree[0].i) - 100000).toString(), i: (Number(docTree[0].i) - 100000).toString(),
p: '0', p: '0',
n: '', n: '',
@ -261,7 +262,15 @@ const addTreeDivider = (data: any) => {
icon: '', icon: '',
ty: 11, ty: 11,
star: 0 star: 0
}) }
if (lastPicIndex > -1) {
//
docTree.splice(Math.max(lastPicIndex, 1), 0, divider)
} else {
// docTree.push(divider)
}
docTreeData.value = docTree docTreeData.value = docTree
} }
//#endregion //#endregion
@ -438,7 +447,7 @@ const closeParentIfNoChild = (pid: string) => {
* 拖拽后处理各个节点排序 * 拖拽后处理各个节点排序
*/ */
const handleDrop = (drag: Node, enter: Node, dropType: NodeDropType, _event: DragEvents) => { const handleDrop = (drag: Node, enter: Node, dropType: NodeDropType, _event: DragEvents) => {
handleTreeDrop(drag, enter, dropType, _event, DocTreeRef, docTreeData, (needUpd) => { handleTreeDrop(drag, enter, dropType, _event, DocTreeRef, docTreeData, 2, (needUpd) => {
docUpdSortApi({ docs: needUpd, folderType: 2, onlyPicture: true }) docUpdSortApi({ docs: needUpd, folderType: 2, onlyPicture: true })
.then((resp) => { .then((resp) => {
addTreeDivider(resp.data) addTreeDivider(resp.data)