Giter Site home page Giter Site logo

Ignoring Hotkeys not working about svelte-put HOT 2 OPEN

wesbos avatar wesbos commented on June 14, 2024 1
Ignoring Hotkeys not working

from svelte-put.

Comments (2)

wesbos avatar wesbos commented on June 14, 2024 1

thanks so much for the clarification here! Your approach is about what I was thinking so I apprecaite you showing me that! Will have it implemented soon

from svelte-put.

vnphanquang avatar vnphanquang commented on June 14, 2024

@wesbos thank you for reporting. When shortcut is placed on window, it's essentially window.addEventListener('keydown', ...). So the event has already reached window by the time your callback executes, calling preventDefault or stopPropagation effectively does nothing useful (except cases when you want to prevent browser default, for example overriding ctrl/cmd+k) .

That newly added example snippet might have been misleading, my apologies! I'll adjust to clarify this.

In your use case I think the issue can be resolved by either:

  1. checking for target in each callback, or
  2. use a 'centralized' on:shortcut event handler and check for target there before doing anything else, just as you wrote in syntaxfm/website#1464 (comment)

For example

  1. turn your handlePlayPause into:

    function handlePlayPause(detail) {
      if (ignoredTags.include(detail.originalEvent.target.tagName)) return;
      // ...
    }
  2. turn your setup into the following:

    <script>
      // your src/lib/hotkeys/Hotkeys.svelte
      // ...
    
      const allTriggers = [
        // can reuse your hotkeys object here
        ...Object.entries(hotkeys).map(([id, hotkey]) => {
          // exclude callback here and use in onShortcut bellow
          const { callback, ...trigger } = hotkey.trigger;
          return {
            id,
            ...trigger,
          };
        }),
      ];
    
      function onShortcut(event) {
        const detail = event.detail;
        const { originalEvent, trigger } = detail;
        const ignoredTags = [`INPUT`, `TEXTAREA`, `SELECT`, `OPTION`, `BUTTON`];
        if (ignoredTags.includes(originalEvent.target.tagName)) return;
        hotkeys[trigger.id].trigger.callback();
      }
    </script>
    
    <svelte:window use:shortcut={{ trigger: allTriggers }} on:shortcut={onShortcut} />

    I try to reuse your code here, which makes it not so elegant but I hope the idea gets through.

If you think the API is not ergonomic enough, I'd love to hear what public interface you have in mind.


ps thanks for using this I never thought something i write would be used by wesbos himself.

from svelte-put.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.