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

Enable keyboard control for 3d canvases #2163

Merged
merged 4 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
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
42 changes: 30 additions & 12 deletions app/javascript/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ class ObjectPreview {
window.addEventListener('resize', this.onResize.bind(this))
this.onResize()
// Handle interaction events
const eventHandlers = [
'pointerdown',
'pointermove',
'pointerup',
'wheel',
'keydown',
'keyup',
'contextmenu'
]
eventHandlers.forEach((eventName) => {
const pointerEvents = ['pointerdown', 'pointermove', 'pointerup']
pointerEvents.forEach((eventName) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

this.canvas.addEventListener(eventName, this.onPointerEvent.bind(this))
})
const keyEvents = ['keydown', 'keyup']
keyEvents.forEach((eventName) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

this.canvas.addEventListener(eventName, this.onKeyEvent.bind(this))
})
const otherEvents = ['wheel', 'contextmenu']
otherEvents.forEach((eventName) => {
this.canvas.addEventListener(eventName, this.onEvent.bind(this))
})
// Monitor visibility
Expand All @@ -61,11 +61,29 @@ class ObjectPreview {
}
}

onEvent (event): void {
event.preventDefault()
onPointerEvent (event): void {
if (event.type === 'pointerdown') {
this.canvas.focus()
this.canvas.setPointerCapture(event.pointerId)
}
this.onEvent(event)
}

onKeyEvent (event): void {
if ([
'ArrowUp',
'ArrowDown',
'ArrowLeft',
'ArrowRight',
'Minus',
'Equal'
].includes(event.code)) {
this.onEvent(event)
}
}

onEvent (event): void {
event.preventDefault()
this.renderer.handleEvent(event)
}

Expand Down
2 changes: 1 addition & 1 deletion app/views/application/_object_preview.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% if renderable?(file.extension) %>
<div class="position-relative">
<canvas id="preview-file-<%= file.id %>" class="object-preview position-relative" data-preview
<canvas id="preview-file-<%= file.id %>" class="object-preview position-relative" tabindex="0" data-preview
data-preview-url="<%= library_model_model_file_path(library, model, file, file.extension.to_sym) %>"
data-worker-url="<%= javascript_path "offscreen_renderer.js" %>"
data-format="<%= file.extension %>"
Expand Down
Loading