Skip to content

Instantly share code, notes, and snippets.

@fantactuka
Last active December 10, 2024 19:02
Show Gist options
  • Save fantactuka/df38cedbc4c342cae78487b97d60347b to your computer and use it in GitHub Desktop.
Save fantactuka/df38cedbc4c342cae78487b97d60347b to your computer and use it in GitHub Desktop.
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