Replies: 3 comments 2 replies
-
did you get solution for this ? |
Beta Was this translation helpful? Give feedback.
-
Unfortunately there isn't anyway inside Lexical's API that I can see. Here is where the constants are defined, not the easiest to absorb at first glance: The formatting is dictated by a single byte using binary shift operations, i.e.:
If you've not seen this syntax, as it's not common in your everyday js, it's essentially this:
etc..
So I've written a function to help fix to convert this into a false/true object for myself and for anyone who wants it:
Output is like this
|
Beta Was this translation helpful? Give feedback.
-
Hi there, I'm pretty new to Lexical but I stumbled on this thread while also learning how to compare the format number to format strings.
// Returns an object with format mapped to boolean
// e.g. getFormattingStates(2) returns {
// IS_BOLD: false,
// IS_ITALIC: true,
// IS_STRIKETHROUGH: false,
// IS_UNDERLINE: false,
// IS_CODE: false,
// IS_SUBSCRIPT: false,
// IS_SUPERSCRIPT: false,
// IS_HIGHLIGHT: false,
// }
const getFormattingStates = (decimalNumber: number) =>
Object.fromEntries(Object.entries(states).map(([k, v]) => [k, !!(v & decimalNumber)]));
// Returns object with only true values
// e.g. getFormattingStates(2) returns {
// IS_ITALIC: true,
// }
const getFormattingStates = (decimalNumber: number) =>
Object.entries(states).reduce((acc, [k, v]) => (v & decimalNumber ? {...acc, [k]: true} : acc), {}); |
Beta Was this translation helpful? Give feedback.
-
I am actually actually trying to use the saved state from the database to generate React components. I am working on a way to convert the json state to components. But the text formatting is in numbers and a combination of numbers is always giving different number.
In the lexical json state the text formating is in number link 0,1,2 which represents bold, italic ..... etc and combination of all those. is there any way I can get the bold,italic directly instead on the number . Here is an example "children"
Beta Was this translation helpful? Give feedback.
All reactions