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

Improvement/recipe: Escape to close and undo support #129

Open
airtable-JayRansijn opened this issue Nov 4, 2022 · 1 comment
Open

Improvement/recipe: Escape to close and undo support #129

airtable-JayRansijn opened this issue Nov 4, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@airtable-JayRansijn
Copy link

airtable-JayRansijn commented Nov 4, 2022

I found this escape and undo handling pretty useful to enable for a plugin, possibly some utils to help could be nice, More info here: https://forum.figma.com/t/pass-shortcuts-to-figma-when-focused-on-the-plugin-window-and-vice-versa/10229/2

Types

export interface KeyActionHandler extends EventHandler {
    name: "KEY_ACTION";
    handler: (message: { action: 'Escape' | 'Undo' }) => void;
}

In UI code

useEffect(() => {
    // https://forum.figma.com/t/pass-shortcuts-to-figma-when-focused-on-the-plugin-window-and-vice-versa/10229/2
    function onKeyDown(e: KeyboardEvent) {
        if (e.key === "Escape") {
            // Close plugin when pressing Escape
            emit<KeyActionHandler>("KEY_ACTION", { action: "Escape" });
        } else if (e.key === "z" && !e.shiftKey && !e.altKey) {
            // Undo the action in Figma
            const isMac = navigator.userAgent.indexOf("Macintosh") >= 0;
            const isCmd = isMac && e.metaKey && !e.ctrlKey;
            const isCtrl = !isMac && !e.metaKey && e.ctrlKey;
            if (isCmd || isCtrl) {
                emit<KeyActionHandler>("KEY_ACTION", { action: "Undo" });
            }
        }
    }
    window.addEventListener("keydown", onKeyDown);
    return () => {
        window.removeEventListener("keydown", onKeyDown);
    };
}, []);

In plugin code

on<KeyActionHandler>(
    "KEY_ACTION",
    function onKeyAction(message) {
        switch (message.action) {
            case "Escape":
                figma.closePlugin()
                break
            case 'Undo':
                const currentSelection = figma.currentPage.selection;
                figma.triggerUndo()
                figma.currentPage.selection = currentSelection;
                break
        }
    }
);
@airtable-JayRansijn airtable-JayRansijn changed the title Escape to close and undo support Improvement/recipe: Escape to close and undo support Nov 4, 2022
@airtable-JayRansijn
Copy link
Author

airtable-JayRansijn commented Nov 4, 2022

FIxed. One issue I am noticing is that it throws out the selection, perhaps it needs to save and reset the selection on trigger undo.

@yuanqing yuanqing added the enhancement New feature or request label Nov 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants