-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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: useKeyMap unmount removes keys (3778) #3788
base: main
Are you sure you want to change the base?
Conversation
|
|
f91730f
to
2b8e9e8
Compare
I don't think this does anything to fix the problem. The original issue was that the keyMap is added as an Object, but then removed via each individual key, which this PR still does. I can't verify 100% because I can't build this locally at the moment, but iirc from my original tests, editor.removeKeyMap() checks by reference, and thus this will fail to locate the original that you're trying to remove because each key of the Object is not the same as the Object itself. Did you check that after calling removeKeyMap() the originally registered keys are actually removed from the editor? Sorry I can't build locally right now to check myself. |
I should be back to where I can take a look either late tonight or tomorrow (US-Eastern Time) and I'll post a plan |
I can make a change to remove the keys by object instead of individual key. Should we also support individual keys as well? Or should we use objects. I see the function type accepts both.
As for the build issues. @isomx are you seeing this error?
|
Ok, had a chance to pull it up. And no, I'm not seeing that But to the problem at hand... The crux of the issue is the fundamental misunderstanding of how add/removeKeyMap works. It seems that you guys mistook the fact that So you loop over each command and provide that to But that's not what they mean by "name", and so that does nothing. They mean a A
None of the usages of And worse, some don't even memoize their I routinely see The easiest fix would be to concatenate the Like this: function useKeyMap(editor, keys, callback) {
React.useEffect(() => {
if (!editor) {
return;
}
// Optionally namespace them in addition
// to using the commands. And optionally
// use a separator. I'm using `___`, but
// it doesn't matter, or even none at all.
const name = `graphiql__${keys.join('___')}`;
editor.removeKeyMap(name);
if (callback) {
const keyMap = {
// Set name to be used for later removal.
// But if the `keys` change, this won't
// work, so it's not a perfect fix. But
// it will at least be an improvement since
// it looks like most usages provide the
// same keys each time, even if they don't
// memoize them.
name
};
for (const key of keys) {
keyMap[key] = () => callback();
}
editor.addKeyMap(keyMap);
}
}, [editor, keys, callback]);
} |
@isomx you now need to thanks everyone for working on this! I hope to release a resolution of some kind this week, once I have more time to focus on it |
No description provided.