Last active
December 10, 2024 19:02
-
-
Save fantactuka/df38cedbc4c342cae78487b97d60347b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const wrappedNodes = new Set<Class<LexicalNode>>(); | |
function registerWhoDidThisListener<T: LexicalNode>( | |
klass: Class<T>, | |
callback: T => void, | |
): () => void { | |
const {prototype} = klass; | |
const { | |
constructor: {name}, | |
// $FlowExpectedError[method-unbinding] | |
getWritable, | |
} = prototype; | |
if (wrappedNodes.has(klass)) { | |
throw err(`${name} is already wrapped with registerWhoDidThisListener`); | |
} | |
wrappedNodes.add(klass); | |
// $FlowExpectedError[missing-this-annot] | |
// $FlowExpectedError[cannot-write] | |
prototype.getWritable = function () { | |
// We override prototype method, so it's all good | |
// eslint-disable-next-line fb-www/avoid-this-outside-classes | |
callback(this); | |
// We override prototype method, so it's all good | |
// eslint-disable-next-line fb-www/avoid-this-outside-classes | |
return getWritable.apply(this, arguments); | |
}; | |
return () => { | |
// $FlowExpectedError[cannot-write] | |
prototype.getWritable = getWritable; | |
wrappedNodes.delete(klass); | |
}; | |
} | |
component MLCWhoDidThisPlugin() { | |
const [editor] = useLexicalComposerContext(); | |
useEffect(() => { | |
return registerWhoDidThisListener(MyNodeClass, node => { | |
console.log(node); | |
debugger; | |
}); | |
}, [editor]); | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment