@@ -2,7 +2,6 @@ import React, {RefObject, useContext, useEffect, useRef, useState} from "react";
2
2
import * as RS from "reactstrap" ;
3
3
import { Label } from "reactstrap" ;
4
4
import {
5
- ClozeItemDTO ,
6
5
IsaacClozeQuestionDTO ,
7
6
ItemChoiceDTO ,
8
7
ItemDTO
@@ -21,7 +20,7 @@ import {
21
20
ResponderProvided
22
21
} from "react-beautiful-dnd" ;
23
22
import ReactDOM from 'react-dom' ;
24
- import { ClozeDropRegionContext } from "../../../IsaacAppTypes" ;
23
+ import { ClozeDropRegionContext , ClozeItemDTO } from "../../../IsaacAppTypes" ;
25
24
import { setCurrentAttempt } from "../../state/actions" ;
26
25
import uuid from "uuid" ;
27
26
import { Item } from "../../services/select" ;
@@ -35,9 +34,9 @@ function Item({item}: {item: ItemDTO}) {
35
34
}
36
35
37
36
interface InlineDropRegionProps {
38
- id : string ; item ?: ClozeItemDTO ; contentHolder : RefObject < HTMLDivElement > ; readonly ?: boolean ; updateAttempt : ( dropResult : DropResult ) => void ;
37
+ id : string ; item ?: ClozeItemDTO ; contentHolder : RefObject < HTMLDivElement > ; readonly ?: boolean ; updateAttempt : ( dropResult : DropResult ) => void ; showBorder : boolean ;
39
38
}
40
- function InlineDropRegion ( { id, item, contentHolder, readonly, updateAttempt} : InlineDropRegionProps ) {
39
+ function InlineDropRegion ( { id, item, contentHolder, readonly, updateAttempt, showBorder } : InlineDropRegionProps ) {
41
40
42
41
function clearInlineDropZone ( ) {
43
42
updateAttempt ( { source : { droppableId : id , index : 0 } , draggableId : ( item ?. replacementId as string ) } as DropResult ) ;
@@ -46,11 +45,11 @@ function InlineDropRegion({id, item, contentHolder, readonly, updateAttempt}: In
46
45
const droppableTarget = contentHolder . current ?. querySelector ( `#${ id } ` ) ;
47
46
if ( droppableTarget ) {
48
47
return ReactDOM . createPortal (
49
- < div style = { { minHeight : "inherit" , position : "relative" } } >
48
+ < div style = { { minHeight : "inherit" , position : "relative" , margin : "2px" } } >
50
49
< Droppable droppableId = { id } isDropDisabled = { readonly } direction = "vertical" >
51
50
{ ( provided , snapshot ) => < div
52
51
ref = { provided . innerRef } { ...provided . droppableProps }
53
- className = " d-flex justify-content-center align-items-center bg-grey rounded w-100 overflow-hidden"
52
+ className = { ` d-flex justify-content-center align-items-center bg-grey rounded w-100 overflow-hidden ${ showBorder && "border border-dark" } ` }
54
53
style = { { minHeight : "inherit" } }
55
54
>
56
55
{ item && < Draggable key = { item . replacementId } draggableId = { item ?. replacementId as string } index = { 0 } isDragDisabled = { true } >
@@ -111,6 +110,8 @@ export function IsaacClozeQuestion({doc, questionId, readonly}: {doc: IsaacCloze
111
110
const registeredDropRegionIDs = useRef < string [ ] > ( [ ] ) . current ;
112
111
const [ inlineDropValues , setInlineDropValues ] = useState < ( ClozeItemDTO | undefined ) [ ] > ( ( ) => currentAttempt ?. items || [ ] ) ;
113
112
113
+ const [ borderMap , setBorderMap ] = useState < { [ dropId : string ] : boolean } > ( { } ) ;
114
+
114
115
useEffect ( ( ) => {
115
116
if ( currentAttempt ?. items ) {
116
117
const idvs = currentAttempt . items as ( ClozeItemDTO | undefined ) [ ] ;
@@ -128,6 +129,7 @@ export function IsaacClozeQuestion({doc, questionId, readonly}: {doc: IsaacCloze
128
129
if ( ! registeredDropRegionIDs . includes ( dropRegionId ) ) {
129
130
registeredDropRegionIDs . push ( dropRegionId ) ;
130
131
setInlineDropValues ( registeredDropRegionIDs . map ( ( ) => undefined ) ) ;
132
+ setBorderMap ( registeredDropRegionIDs . reduce ( ( dict : { [ dropId : string ] : boolean } , id ) => Object . assign ( dict , { [ id ] : false } ) , { } ) ) ;
131
133
}
132
134
}
133
135
@@ -138,21 +140,14 @@ export function IsaacClozeQuestion({doc, questionId, readonly}: {doc: IsaacCloze
138
140
// This is run on drag update to highlight the droppable that the user is dragging over
139
141
// this gives more control over when a droppable is highlighted
140
142
function fixInlineZones ( { destination} : DragUpdate , provided : ResponderProvided ) {
141
- registeredDropRegionIDs . map ( ( dropid , i ) => {
142
- const inlineDrop = document . querySelector ( `[data-rbd-droppable-id="${ dropid } "]` ) as HTMLElement ;
143
- const destinationDropIndex = destination ? registeredDropRegionIDs . indexOf ( dropid ) : - 1 ;
143
+ registeredDropRegionIDs . map ( ( dropId , i ) => {
144
+ const destinationDropIndex = destination ? registeredDropRegionIDs . indexOf ( dropId ) : - 1 ;
144
145
const destinationDragIndex = destination ?. index ?? - 1 ;
145
146
146
- if ( inlineDrop ) {
147
- if ( dropid === destination ?. droppableId && destinationDropIndex !== - 1 && destinationDragIndex === 0 ) {
148
- // Reselect zone (or make sure it's got a border)
149
- inlineDrop . className = inlineDrop . className . replace ( "border border-dark" , "" ) + " border border-dark" ;
150
- } else {
151
- // Deselect zone
152
- inlineDrop . className = inlineDrop . className . replace ( "border border-dark" , "" ) ;
153
- }
154
- }
147
+ borderMap [ dropId ] = ( dropId === destination ?. droppableId && destinationDropIndex !== - 1 && destinationDragIndex === 0 ) ;
155
148
} ) ;
149
+ // Tell React about the changes to borderMap
150
+ setBorderMap ( { ...borderMap } ) ;
156
151
}
157
152
158
153
// Run after a drag action ends
@@ -279,6 +274,7 @@ export function IsaacClozeQuestion({doc, questionId, readonly}: {doc: IsaacCloze
279
274
id = { dropRegionId } item = { inlineDropValues [ index ] } updateAttempt = { ( dropResult ) => {
280
275
updateAttempt ( { ...dropResult , destination : { droppableId : itemsSection , index : nonSelectedItems . length } } , { announce : ( _ ) => { return ; } } ) ;
281
276
} }
277
+ showBorder = { borderMap [ dropRegionId ] }
282
278
/>
283
279
) }
284
280
</ DragDropContext >
0 commit comments