Skip to content
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

Pass full node to renderMark and/or allow overriding renderText #8

Open
janus-reith opened this issue May 30, 2024 · 0 comments
Open

Comments

@janus-reith
Copy link

I apply custom styles to my marks, e.g. color, similar to how it's done in the Lexical playground.

Therefore I need to parse style:

const renderText = React.useCallback(
    (node: TextNode): React.ReactNode | null => {
      if (!renderMark) {
        throw new Error("'renderMark' prop not provided.");
      }

      if (!node.format) {
        return renderMark({
          text: node.text,
          style: node.style,
        });
      }

      return renderMark({
        text: node.text,
        bold: (node.format & IS_BOLD) > 0,
        [...]
        style: node.style,
      });
    },
    [renderMark]
  );

And use it like this:

  export const defaultRenderMark: RenderMark = (mark) => {
  const style: CSSProperties = { ...parseStyleString(mark.style) };

  [...]

  if (Object.keys(style).length === 0) {
    return <>{mark.text}</>;
  }

  return <span style={style}>{mark.text}</span>;
};

I had to fork the component in order to pass node.style to renderMark.

Some ideas:

  1. A. Allow passing a custom function for renderText, similar to renderMark.
    B. Pass the full node to renderMark. In addition, consider removing renderText altogether and just calling a function that matches IS_BOLD etc. from within renderMark. That one could also be exported, so custom renderMark implementations could reuse it.
  2. Consider making parsing of styles and maybe other relevant node attributes like class part of the included renderMark function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant