コミットを比較
4 コミット
| 作成者 | SHA1 | 日付 | |
|---|---|---|---|
| 5cf46406a9 | |||
| 29c385ed9f | |||
| abeee76ddd | |||
| 347a0ebbba |
@@ -0,0 +1,75 @@
|
|||||||
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
|
|
||||||
|
import DraggableDroppableTagRow from '@/components/DraggableDroppableTagRow'
|
||||||
|
import { buildTag } from '@/test/factories'
|
||||||
|
import { renderWithProviders } from '@/test/render'
|
||||||
|
|
||||||
|
const dndKit = vi.hoisted (() => ({
|
||||||
|
useDraggable: vi.fn (),
|
||||||
|
useDroppable: vi.fn (),
|
||||||
|
}))
|
||||||
|
|
||||||
|
vi.mock ('@dnd-kit/core', () => dndKit)
|
||||||
|
|
||||||
|
const tag = buildTag ({ id: 7, name: 'ドラッグ元', postCount: 3 })
|
||||||
|
|
||||||
|
const renderRow = (activeDndId?: string) => {
|
||||||
|
renderWithProviders (
|
||||||
|
<DraggableDroppableTagRow
|
||||||
|
activeDndId={activeDndId}
|
||||||
|
tag={tag}
|
||||||
|
nestLevel={2}
|
||||||
|
pathKey="cat-general-7"
|
||||||
|
suppressClickRef={{ current: false }}/>,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const tagBody = (): HTMLElement => {
|
||||||
|
const link = document.querySelector<HTMLElement> ('a[title="ドラッグ元"]')
|
||||||
|
|
||||||
|
if (link == null)
|
||||||
|
throw new Error ('tag link not found')
|
||||||
|
|
||||||
|
const body = link.closest ('div')
|
||||||
|
|
||||||
|
if (body == null)
|
||||||
|
throw new Error ('tag body not found')
|
||||||
|
|
||||||
|
return body
|
||||||
|
}
|
||||||
|
|
||||||
|
describe ('DraggableDroppableTagRow', () => {
|
||||||
|
beforeEach (() => {
|
||||||
|
vi.clearAllMocks ()
|
||||||
|
dndKit.useDraggable.mockReturnValue ({
|
||||||
|
attributes: { 'aria-describedby': 'drag-source' },
|
||||||
|
listeners: { onPointerDown: vi.fn () },
|
||||||
|
setNodeRef: vi.fn (),
|
||||||
|
transform: null })
|
||||||
|
dndKit.useDroppable.mockReturnValue ({
|
||||||
|
isOver: false,
|
||||||
|
setNodeRef: vi.fn () })
|
||||||
|
})
|
||||||
|
|
||||||
|
it ('passes dndId through draggable data for active-row tracking', () => {
|
||||||
|
renderRow ()
|
||||||
|
|
||||||
|
expect (dndKit.useDraggable).toHaveBeenCalledWith (
|
||||||
|
expect.objectContaining ({
|
||||||
|
id: 'tag-node:cat-general-7',
|
||||||
|
data: expect.objectContaining ({
|
||||||
|
dndId: 'tag-node:cat-general-7',
|
||||||
|
nestLevel: 2,
|
||||||
|
tagId: 7 }) }))
|
||||||
|
})
|
||||||
|
|
||||||
|
it ('hides only the active drag source from the explicit active dnd id', () => {
|
||||||
|
renderRow ('tag-node:cat-general-7')
|
||||||
|
expect (tagBody ()).toHaveStyle ({ visibility: 'hidden' })
|
||||||
|
})
|
||||||
|
|
||||||
|
it ('keeps inactive tag rows visible while another tag is dragged', () => {
|
||||||
|
renderRow ('tag-node:other')
|
||||||
|
expect (tagBody ()).toHaveStyle ({ visibility: 'visible' })
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -13,6 +13,7 @@ import type { CSSProperties, FC, MutableRefObject } from 'react'
|
|||||||
import type { Tag } from '@/types'
|
import type { Tag } from '@/types'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
|
activeDndId?: string
|
||||||
tag: Tag
|
tag: Tag
|
||||||
nestLevel: number
|
nestLevel: number
|
||||||
pathKey: string
|
pathKey: string
|
||||||
@@ -21,7 +22,15 @@ type Props = {
|
|||||||
sp?: boolean }
|
sp?: boolean }
|
||||||
|
|
||||||
|
|
||||||
const DraggableDroppableTagRow: FC<Props> = ({ tag, nestLevel, pathKey, parentTagId, suppressClickRef, sp }) => {
|
const DraggableDroppableTagRow: FC<Props> = ({
|
||||||
|
activeDndId,
|
||||||
|
tag,
|
||||||
|
nestLevel,
|
||||||
|
pathKey,
|
||||||
|
parentTagId,
|
||||||
|
suppressClickRef,
|
||||||
|
sp,
|
||||||
|
}) => {
|
||||||
const behaviourSettings = useClientBehaviourSettings ()
|
const behaviourSettings = useClientBehaviourSettings ()
|
||||||
const animationMode = behaviourSettings.animation ?? 'normal'
|
const animationMode = behaviourSettings.animation ?? 'normal'
|
||||||
const layoutTransition = clientAnimationTransition (
|
const layoutTransition = clientAnimationTransition (
|
||||||
@@ -53,9 +62,9 @@ const DraggableDroppableTagRow: FC<Props> = ({ tag, nestLevel, pathKey, parentTa
|
|||||||
const { attributes,
|
const { attributes,
|
||||||
listeners,
|
listeners,
|
||||||
setNodeRef: setDragRef,
|
setNodeRef: setDragRef,
|
||||||
transform,
|
transform } = useDraggable ({ id: dndId,
|
||||||
isDragging: dragging } = useDraggable ({ id: dndId,
|
|
||||||
data: { kind: 'tag',
|
data: { kind: 'tag',
|
||||||
|
dndId,
|
||||||
tagId: tag.id,
|
tagId: tag.id,
|
||||||
parentTagId,
|
parentTagId,
|
||||||
nestLevel } })
|
nestLevel } })
|
||||||
@@ -64,8 +73,12 @@ const DraggableDroppableTagRow: FC<Props> = ({ tag, nestLevel, pathKey, parentTa
|
|||||||
id: dndId,
|
id: dndId,
|
||||||
data: { kind: 'tag', tagId: tag.id } })
|
data: { kind: 'tag', tagId: tag.id } })
|
||||||
|
|
||||||
|
const activeDragging = activeDndId === dndId
|
||||||
const style: CSSProperties = { transform: CSS.Translate.toString (transform),
|
const style: CSSProperties = { transform: CSS.Translate.toString (transform),
|
||||||
visibility: dragging ? 'hidden' : 'visible' }
|
visibility: activeDragging ? 'hidden' : 'visible' }
|
||||||
|
const innerClassName = cn (
|
||||||
|
'inline-flex min-w-0 max-w-full items-baseline overflow-hidden',
|
||||||
|
sp && 'touch-pan-y')
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
@@ -101,12 +114,21 @@ const DraggableDroppableTagRow: FC<Props> = ({ tag, nestLevel, pathKey, parentTa
|
|||||||
sp && 'touch-pan-y',
|
sp && 'touch-pan-y',
|
||||||
over && 'ring-2 ring-offset-2')}
|
over && 'ring-2 ring-offset-2')}
|
||||||
>
|
>
|
||||||
|
{activeDragging
|
||||||
|
? (
|
||||||
|
<div
|
||||||
|
ref={setDragRef}
|
||||||
|
style={style}
|
||||||
|
className={innerClassName}
|
||||||
|
{...attributes}
|
||||||
|
{...listeners}>
|
||||||
|
<TagLink tag={tag} nestLevel={nestLevel}/>
|
||||||
|
</div>)
|
||||||
|
: (
|
||||||
<motion.div
|
<motion.div
|
||||||
ref={setDragRef}
|
ref={setDragRef}
|
||||||
style={style}
|
style={style}
|
||||||
className={cn (
|
className={innerClassName}
|
||||||
'inline-flex min-w-0 max-w-full items-baseline overflow-hidden',
|
|
||||||
sp && 'touch-pan-y')}
|
|
||||||
{...attributes}
|
{...attributes}
|
||||||
{...listeners}
|
{...listeners}
|
||||||
transition={{ layout: layoutTransition }}
|
transition={{ layout: layoutTransition }}
|
||||||
@@ -114,7 +136,7 @@ const DraggableDroppableTagRow: FC<Props> = ({ tag, nestLevel, pathKey, parentTa
|
|||||||
? undefined
|
? undefined
|
||||||
: `tag-${ sp ? 'sp-' : '' }${ tag.id }`}>
|
: `tag-${ sp ? 'sp-' : '' }${ tag.id }`}>
|
||||||
<TagLink tag={tag} nestLevel={nestLevel}/>
|
<TagLink tag={tag} nestLevel={nestLevel}/>
|
||||||
</motion.div>
|
</motion.div>)}
|
||||||
</div>)
|
</div>)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import { DndContext,
|
|||||||
useDroppable,
|
useDroppable,
|
||||||
useSensor,
|
useSensor,
|
||||||
useSensors } from '@dnd-kit/core'
|
useSensors } from '@dnd-kit/core'
|
||||||
import { restrictToWindowEdges } from '@dnd-kit/modifiers'
|
import { restrictToWindowEdges, snapCenterToCursor } from '@dnd-kit/modifiers'
|
||||||
import { useQueryClient } from '@tanstack/react-query'
|
import { useQueryClient } from '@tanstack/react-query'
|
||||||
import { motion } from 'framer-motion'
|
import { motion } from 'framer-motion'
|
||||||
import { useEffect, useMemo, useRef, useState } from 'react'
|
import { useEffect, useMemo, useRef, useState } from 'react'
|
||||||
@@ -43,6 +43,7 @@ const alwaysMeasureDroppables = {
|
|||||||
|
|
||||||
|
|
||||||
const renderTagTree = (
|
const renderTagTree = (
|
||||||
|
activeDndId: string | undefined,
|
||||||
tag: TagWithSections,
|
tag: TagWithSections,
|
||||||
nestLevel: number,
|
nestLevel: number,
|
||||||
path: string,
|
path: string,
|
||||||
@@ -54,6 +55,7 @@ const renderTagTree = (
|
|||||||
const self = (
|
const self = (
|
||||||
<li key={key} className="mb-1">
|
<li key={key} className="mb-1">
|
||||||
<DraggableDroppableTagRow
|
<DraggableDroppableTagRow
|
||||||
|
activeDndId={activeDndId}
|
||||||
tag={tag}
|
tag={tag}
|
||||||
nestLevel={nestLevel}
|
nestLevel={nestLevel}
|
||||||
pathKey={key}
|
pathKey={key}
|
||||||
@@ -67,7 +69,14 @@ const renderTagTree = (
|
|||||||
...((tag.children
|
...((tag.children
|
||||||
?.sort ((a, b) => a.name < b.name ? -1 : 1)
|
?.sort ((a, b) => a.name < b.name ? -1 : 1)
|
||||||
.flatMap (child =>
|
.flatMap (child =>
|
||||||
renderTagTree (child, nestLevel + 1, key, suppressClickRef, tag.id, sp)))
|
renderTagTree (
|
||||||
|
activeDndId,
|
||||||
|
child,
|
||||||
|
nestLevel + 1,
|
||||||
|
key,
|
||||||
|
suppressClickRef,
|
||||||
|
tag.id,
|
||||||
|
sp)))
|
||||||
?? [])]
|
?? [])]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,6 +220,7 @@ type Props = {
|
|||||||
sp?: boolean }
|
sp?: boolean }
|
||||||
|
|
||||||
type ActiveTagDrag = {
|
type ActiveTagDrag = {
|
||||||
|
dndId: string
|
||||||
tagId: number
|
tagId: number
|
||||||
nestLevel: number }
|
nestLevel: number }
|
||||||
|
|
||||||
@@ -251,6 +261,7 @@ const TagDetailSidebar: FC<Props> = ({ className, post, sp }) => {
|
|||||||
() => buildFlatTagByCategory (tags),
|
() => buildFlatTagByCategory (tags),
|
||||||
[tags],
|
[tags],
|
||||||
)
|
)
|
||||||
|
const activeDndId = activeTagDrag?.dndId
|
||||||
|
|
||||||
const suppressClickRef = useRef (false)
|
const suppressClickRef = useRef (false)
|
||||||
|
|
||||||
@@ -359,17 +370,18 @@ const TagDetailSidebar: FC<Props> = ({ className, post, sp }) => {
|
|||||||
<TagSearch/>
|
<TagSearch/>
|
||||||
<DndContext
|
<DndContext
|
||||||
sensors={sensors}
|
sensors={sensors}
|
||||||
collisionDetection={sp ? tagCollisionDetection : undefined}
|
collisionDetection={tagCollisionDetection}
|
||||||
measuring={sp ? alwaysMeasureDroppables : undefined}
|
measuring={alwaysMeasureDroppables}
|
||||||
onDragStart={e => {
|
onDragStart={e => {
|
||||||
if (e.active.data.current?.kind === 'tag')
|
if (e.active.data.current?.kind === 'tag')
|
||||||
{
|
{
|
||||||
const tagId = e.active.data.current?.tagId
|
const tagId = e.active.data.current?.tagId
|
||||||
const nestLevel = e.active.data.current?.nestLevel
|
const nestLevel = e.active.data.current?.nestLevel
|
||||||
|
const dndId = e.active.data.current?.dndId
|
||||||
setActiveTagDrag (
|
setActiveTagDrag (
|
||||||
tagId == null || nestLevel == null
|
tagId == null || nestLevel == null || dndId == null
|
||||||
? null
|
? null
|
||||||
: { tagId, nestLevel })
|
: { dndId, tagId, nestLevel })
|
||||||
}
|
}
|
||||||
setDragging (true)
|
setDragging (true)
|
||||||
suppressClickRef.current = true
|
suppressClickRef.current = true
|
||||||
@@ -426,6 +438,7 @@ const TagDetailSidebar: FC<Props> = ({ className, post, sp }) => {
|
|||||||
{(tagRelationDisplay === 'grouped'
|
{(tagRelationDisplay === 'grouped'
|
||||||
? (tags[cat] ?? []).flatMap (tag =>
|
? (tags[cat] ?? []).flatMap (tag =>
|
||||||
renderTagTree (
|
renderTagTree (
|
||||||
|
activeDndId,
|
||||||
tag,
|
tag,
|
||||||
0,
|
0,
|
||||||
`cat-${ cat }`,
|
`cat-${ cat }`,
|
||||||
@@ -437,6 +450,7 @@ const TagDetailSidebar: FC<Props> = ({ className, post, sp }) => {
|
|||||||
: (flatTagsByCategory[cat] ?? []).map (tag => (
|
: (flatTagsByCategory[cat] ?? []).map (tag => (
|
||||||
<li key={`flat-${ cat }-${ tag.id }`} className="mb-1">
|
<li key={`flat-${ cat }-${ tag.id }`} className="mb-1">
|
||||||
<DraggableDroppableTagRow
|
<DraggableDroppableTagRow
|
||||||
|
activeDndId={activeDndId}
|
||||||
tag={tag}
|
tag={tag}
|
||||||
nestLevel={0}
|
nestLevel={0}
|
||||||
pathKey={`flat-${ cat }-${ tag.id }`}
|
pathKey={`flat-${ cat }-${ tag.id }`}
|
||||||
@@ -489,7 +503,10 @@ const TagDetailSidebar: FC<Props> = ({ className, post, sp }) => {
|
|||||||
</ul>
|
</ul>
|
||||||
</motion.div>)}
|
</motion.div>)}
|
||||||
|
|
||||||
<DragOverlay adjustScale={false}>
|
<DragOverlay
|
||||||
|
adjustScale={false}
|
||||||
|
modifiers={[snapCenterToCursor]}
|
||||||
|
dropAnimation={animationMode === 'off' ? null : undefined}>
|
||||||
<div className="pointer-events-none">
|
<div className="pointer-events-none">
|
||||||
{activeTagDrag != null && (() => {
|
{activeTagDrag != null && (() => {
|
||||||
const tag = findTag (tags, activeTagDrag.tagId)
|
const tag = findTag (tags, activeTagDrag.tagId)
|
||||||
|
|||||||
新しい課題から参照
ユーザをブロックする