diff --git a/app/javascript/src/preview.ts b/app/javascript/src/preview.ts index a769aa877..3a35db9ad 100644 --- a/app/javascript/src/preview.ts +++ b/app/javascript/src/preview.ts @@ -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) => { + this.canvas.addEventListener(eventName, this.onPointerEvent.bind(this)) + }) + const keyEvents = ['keydown', 'keyup'] + keyEvents.forEach((eventName) => { + 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 @@ -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) } diff --git a/app/views/application/_object_preview.html.erb b/app/views/application/_object_preview.html.erb index f044d9b82..85e8499c4 100644 --- a/app/views/application/_object_preview.html.erb +++ b/app/views/application/_object_preview.html.erb @@ -1,6 +1,6 @@ <% if renderable?(file.extension) %>
- " data-format="<%= file.extension %>"