Replies: 1 comment
-
I had the same problem while using new prettier together with the playground prettier demo. Since the format is async fn I had to refactor the code this way. editor.getEditorState().read(
() => {
const codeNode = $getNearestNodeFromDOMNode(codeDOMNode);
if ($isCodeNode(codeNode)) {
return {
codeNode,
content: codeNode.getTextContent(),
};
}
},
{
editor,
},
); Just in case anyone is looking for the whole updated prettier handleClick fn based on the one from playground: async function handleClick(): Promise<void> {
const codeDOMNode = getCodeDOMNode();
try {
const format = await loadPrettierFormat();
const options = getPrettierOptions(lang);
options.plugins = [
await loadPrettierParserByLang(lang),
await loadPrettierGenericPlugins(),
];
if (!codeDOMNode) {
return;
}
const nodeAndContent = editor.getEditorState().read(
() => {
const codeNode = $getNearestNodeFromDOMNode(codeDOMNode);
if ($isCodeNode(codeNode)) {
return {
codeNode,
content: codeNode.getTextContent(),
};
}
},
{
editor,
},
);
if (nodeAndContent) {
const { content, codeNode } = nodeAndContent;
let parsed = '';
try {
parsed = await format(content, options);
} catch (error: unknown) {
setError(error);
}
if (parsed !== '') {
editor.update(() => {
const selection = codeNode.select(0);
selection.insertText(parsed);
setSyntaxError('');
setTipsVisible(false);
});
}
}
} catch (error: unknown) {
setError(error);
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello
Been learning and working with lexical for the past couple of months. And recently I've been building a Text editor. I decided to reproduce the same prettier functionality that's on the playground to format; code and I came across the below portion of code in
handleClick
function in the source codeThis function wasn't working. And after careful inspection, I noticed that the
format
function wasn't having anawait
. Whereas, the prettier format function returns aPromise
. So I was wondering how could this work? So knowing that lexical'supdate
norread
method doesn't accepts an async method, I tried this and it worked.First I thought it made more sense reading then updating but it didn't work
So without much experience and knowledge on Lexical, I just wanted to know if this is even lexically clean and acceptable 😄 ?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions