Skip to content

Commit

Permalink
Dehighlight on mouseup if != mousedown event target
Browse files Browse the repository at this point in the history
Previously, mousedown -> drag away from Cell -> mouseup would not clear
the highlight effect until the User reloaded the page or re-inspired the
highlight effect on all now-highlighted Cells. This can be mitigated by
implementing a dehighlight function. And, fortunately, our dehighlight
function is dead simple and also pretty innocuous since it requires no
change in database state anymore.

Remaining issue:
mousedown on Cell -> drag away from Cell to an area outside of the Game
board -> mouseup...
- In this case we will not detect the `mouseup` event since it has
  occurred outside of the board and, thus, outside of the bubble context
  of our Stimulus controller.
  - I'm not sure if I want to add a `mouseup` listener that covers
    everything on the page just in case the drag happens on anything
    outside of the board. So, for now, just punting on this.
  • Loading branch information
pdobb committed Jan 27, 2025
1 parent 9e4a992 commit 7029ce6
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class Games::Current::Board::Cells::DehighlightNeighborsController <
ApplicationController
include Games::Current::Board::Cells::ActionBehaviors

def create
updated_cells = cell.highlightable_neighbors

broadcast_updates(updated_cells)
end
end
4 changes: 4 additions & 0 deletions app/javascript/controllers/games/current/board/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class Cell {
this.#submit(url)
}

dehighlightNeighbors(url) {
this.#submit(url)
}

toggleFlag(url, beforeSubmit = () => {}) {
if (this.isNotFlaggable) return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class extends Controller {
revealUrl: String,
toggleFlagUrl: String,
highlightNeighborsUrl: String,
dehighlightNeighborsUrl: String,
revealNeighborsUrl: String,
}

Expand All @@ -25,13 +26,21 @@ export default class extends Controller {
dispatchMousedown(event) {
if (!mouse(event).actsAsLeftClick()) return

this.mousedownTarget = event.target

this.highlightNeighborsTimer = setTimeout(() => {
cell(event.target).highlightNeighbors(this.highlightNeighborsUrlValue)
}, this.constructor.highlightNeighborsDelay)
}

dispatchMouseup() {
dispatchMouseup(event) {
clearTimeout(this.highlightNeighborsTimer)

if (this.mousedownTarget && event.target != this.mousedownTarget) {
cell(this.mousedownTarget).dehighlightNeighbors(
this.dehighlightNeighborsUrlValue,
)
}
}

dispatchContextmenu(event) {
Expand Down
4 changes: 1 addition & 3 deletions app/views/games/current/board/_cell.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@
data-revealed="<%= cell.revealed? %>"
data-flagged="<%= cell.flagged? %>"
class="<%= class_names(cell.css) %>"
>
<%= cell %>
</td>
><%= cell %></td>
1 change: 1 addition & 0 deletions app/views/games/current/board/_content.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
data-<%= controller %>-reveal-url-value="<%= content.reveal_url %>"
data-<%= controller %>-toggle-flag-url-value="<%= content.toggle_flag_url %>"
data-<%= controller %>-highlight-neighbors-url-value="<%= content.highlight_neighbors_url %>"
data-<%= controller %>-dehighlight-neighbors-url-value="<%= content.dehighlight_neighbors_url %>"
data-<%= controller %>-reveal-neighbors-url-value="<%= content.reveal_neighbors_url %>"
data-action="
mousedown-><%= controller %>#dispatchMousedown
Expand Down
4 changes: 4 additions & 0 deletions app/views/games/current/board/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def highlight_neighbors_url
Router.game_board_cell_highlight_neighbors_path(game, NULL_CELL_ID)
end

def dehighlight_neighbors_url
Router.game_board_cell_dehighlight_neighbors_path(game, NULL_CELL_ID)
end

def reveal_neighbors_url
Router.game_board_cell_reveal_neighbors_path(game, NULL_CELL_ID)
end
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
resource :reveal, only: :create
resource :toggle_flag, only: :create
resource :highlight_neighbors, only: :create
resource :dehighlight_neighbors, only: :create
resource :reveal_neighbors, only: :create
end
end
Expand Down

0 comments on commit 7029ce6

Please sign in to comment.