From 8873e592055d09b64be7faeb35ed8027604f9026 Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Thu, 5 Nov 2020 15:39:56 +1100 Subject: [PATCH] Added sorting to notes --- src/components/map/Map.js | 44 +++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/src/components/map/Map.js b/src/components/map/Map.js index 29467db..adc9029 100644 --- a/src/components/map/Map.js +++ b/src/components/map/Map.js @@ -208,7 +208,7 @@ function Map({ } // Sort so vehicles render below other tokens - function sortMapTokenStates(a, b, draggingTokenOptions) { + function sortMapTokenStates(a, b, tokenDraggingOptions) { const tokenA = tokensById[a.tokenId]; const tokenB = tokensById[b.tokenId]; if (tokenA && tokenB) { @@ -218,16 +218,16 @@ function Map({ const bWeight = getMapTokenCategoryWeight(tokenB.category); return bWeight - aWeight; } else if ( - draggingTokenOptions && - draggingTokenOptions.dragging && - draggingTokenOptions.tokenState.id === a.id + tokenDraggingOptions && + tokenDraggingOptions.dragging && + tokenDraggingOptions.tokenState.id === a.id ) { // If dragging token a move above return 1; } else if ( - draggingTokenOptions && - draggingTokenOptions.dragging && - draggingTokenOptions.tokenState.id === b.id + tokenDraggingOptions && + tokenDraggingOptions.dragging && + tokenDraggingOptions.tokenState.id === b.id ) { // If dragging token b move above return -1; @@ -362,6 +362,27 @@ function Map({ setIsNoteMenuOpen(true); } + function sortNotes(a, b, noteDraggingOptions) { + if ( + noteDraggingOptions && + noteDraggingOptions.dragging && + noteDraggingOptions.noteId === a.id + ) { + // If dragging token `a` move above + return 1; + } else if ( + noteDraggingOptions && + noteDraggingOptions.dragging && + noteDraggingOptions.noteId === b.id + ) { + // If dragging token `b` move above + return -1; + } else { + // Else sort so last modified is on top + return a.lastModified - b.lastModified; + } + } + const mapNotes = ( + sortNotes(a, b, noteDraggingOptions) + ) + : [] + } onNoteMenuOpen={handleNoteMenuOpen} draggable={selectedToolId === "note" || selectedToolId === "pan"} onNoteDragStart={(e, noteId) =>