Skip to content

Commit 443a19a

Browse files
authored
refactor: simplify drag state (#154)
1 parent 1bd30fb commit 443a19a

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

src/Bookmarks/component.css

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,22 @@
3636
position: relative; /* for .BookmarkEditButton */
3737
}
3838

39-
/* when a bookmark is moved to another folder, "From" should be hidden */
39+
/* when move a bookmark across folders, "From" should be hidden */
4040
.Bookmark__DragDrop__From:not(.Bookmark__DragDrop__To) .Bookmark {
4141
visibility: hidden;
4242
}
4343

44-
/* when a bookmark is moved to another folder but not hovered, "To" should be placeholder */
45-
.Bookmark__DragDrop__To:not(.Bookmark__DragDrop__Hover) {
44+
/* when the pointer is out of "To" element, it should be a placeholder */
45+
.Bookmark__DragDrop__To[data-bookmark-drag-state='leave'] {
4646
outline: var(--palette-1) dotted 2px;
4747
}
4848

49-
.Bookmark__DragDrop__To:not(.Bookmark__DragDrop__Hover) .Bookmark {
49+
.Bookmark__DragDrop__To[data-bookmark-drag-state='leave'] .Bookmark {
5050
visibility: hidden;
5151
}
5252

53-
.Bookmark__DragDrop__Hover {
53+
/* when the pointer is over a bookmark, it should be outlined */
54+
.Bookmark__DragDrop__To[data-bookmark-drag-state='enter'] {
5455
outline: var(--palette-1) solid 2px;
5556
}
5657

src/Bookmarks/component.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ const BookmarkDragDrop: FC<BookmarkDragDropProps> = ({ bookmark, position, drag,
155155
className={classNameOfMap({
156156
Bookmark__DragDrop__From: bookmark.dragFrom,
157157
Bookmark__DragDrop__To: bookmark.dragTo,
158-
Bookmark__DragDrop__Hover: bookmark.hover,
159158
})}
159+
data-bookmark-drag-state={bookmark.state}
160160
onDragStart={(e) => {
161161
setDrag(Drag.start(bookmark, position))
162162
e.dataTransfer.effectAllowed = 'move'

src/Bookmarks/viewmodel.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
import { Bookmark, BookmarkFolderID, Position } from './model'
22

3+
type DragState = 'enter' | 'leave'
4+
35
export class Drag {
46
readonly bookmark: Bookmark
57
readonly from: Position
68
readonly to: Position
7-
readonly hover: boolean
9+
readonly state: DragState
810

9-
private constructor(bookmark: Bookmark, from: Position, to: Position, hover: boolean) {
11+
private constructor(bookmark: Bookmark, from: Position, to: Position, state: DragState) {
1012
this.bookmark = bookmark
1113
this.from = from
1214
this.to = to
13-
this.hover = hover
15+
this.state = state
1416
}
1517

1618
static start(bookmark: Bookmark, from: Position) {
17-
return new Drag(bookmark, from, from, true)
19+
return new Drag(bookmark, from, from, 'enter')
1820
}
1921

2022
enterTo(to: Position) {
21-
return new Drag(this.bookmark, this.from, to, true)
23+
return new Drag(this.bookmark, this.from, to, 'enter')
2224
}
2325

2426
leave() {
25-
return new Drag(this.bookmark, this.from, this.to, false)
27+
return new Drag(this.bookmark, this.from, this.to, 'leave')
2628
}
2729

2830
calculateDestination(): Position {
@@ -37,7 +39,7 @@ export class Drag {
3739
export type BookmarkWithDragProps = Bookmark & {
3840
readonly dragFrom?: true
3941
readonly dragTo?: true
40-
readonly hover?: true
42+
readonly state?: DragState
4143
}
4244

4345
export const reorderBookmarks = (
@@ -49,25 +51,25 @@ export const reorderBookmarks = (
4951
return bookmarks
5052
}
5153

54+
// move the bookmark in the folder
5255
if (folderID === drag.from.folderID && drag.from.folderID === drag.to.folderID) {
53-
// move the bookmark in the folder
5456
const r: BookmarkWithDragProps[] = [...bookmarks]
5557
r.splice(drag.from.index, 1)
56-
r.splice(drag.to.index, 0, { ...drag.bookmark, dragFrom: true, dragTo: true, hover: drag.hover || undefined })
58+
r.splice(drag.to.index, 0, { ...drag.bookmark, dragFrom: true, dragTo: true, state: drag.state })
5759
return r
5860
}
5961

60-
// when move across the folder, keep the element to receive the dragEnd event
62+
// move the bookmark across the folders
6163
if (folderID === drag.from.folderID) {
6264
const r: BookmarkWithDragProps[] = [...bookmarks]
65+
// keep the element to receive the dragEnd event
6366
r.splice(drag.from.index, 1)
64-
r.push({ ...drag.bookmark, dragFrom: true })
67+
r.push({ ...drag.bookmark, dragFrom: true, state: drag.state })
6568
return r
6669
}
67-
6870
if (folderID === drag.to.folderID) {
6971
const r: BookmarkWithDragProps[] = [...bookmarks]
70-
r.splice(drag.to.index, 0, { ...drag.bookmark, dragTo: true, hover: drag.hover || undefined })
72+
r.splice(drag.to.index, 0, { ...drag.bookmark, dragTo: true, state: drag.state })
7173
return r
7274
}
7375

0 commit comments

Comments
 (0)