Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: text selection on table row triggers visit #3708

Merged
merged 1 commit into from
Mar 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions app/javascript/js/controllers/table_row_controller.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
connect() {
this.isSelecting = false
this.#bindSelectionEvents()
}

visitRecord(event) {
if (event.type !== 'click') {
return
}

if (this.#isTextSelected()) {
return
}

// We won't navigate if shift is pressed. That is usually used to select multiple rows.
const isShiftPressed = event.shiftKey
// We won't navigate if the user clicks on the item selector cell
Expand Down Expand Up @@ -33,6 +42,31 @@ export default class extends Controller {
}
}

#bindSelectionEvents() {
this.element.addEventListener('mousedown', this.#handleMouseDown.bind(this))
this.element.addEventListener('mousemove', this.#handleMouseMove.bind(this))
}

#handleMouseDown() {
this.isSelecting = false
}

#handleMouseMove(event) {
if (event.buttons === 1) { // Left mouse button is being held
this.isSelecting = true
}
}

#isTextSelected() {
if (this.isSelecting || window.getSelection().toString().length > 0) {
this.isSelecting = false

return true
}

return false
}

#visitInSameTab(url) {
window.Turbo.visit(url)
}
Expand Down
Loading