Git Product home page Git Product logo

Comments (10)

astghikk-adobe avatar astghikk-adobe commented on August 17, 2024 1

yes I've tried that, but exportDOM is private in ParagraphNode. That is the blocker

from lexical.

2wheeh avatar 2wheeh commented on August 17, 2024 1

yes I've tried that, but exportDOM is private in ParagraphNode. That is the blocker

Now I get what your first question is.

from lexical.

astghikk-adobe avatar astghikk-adobe commented on August 17, 2024 1

Thank you, that was helpful.

from lexical.

2wheeh avatar 2wheeh commented on August 17, 2024

You can override HTML with HTMLConfig from the editor config:

    html: {
      export: new Map([[TextNode, () => ({ element: document.createElement('figure') })]]),
      import: { 'figure': () => ({ conversion: () => ({ node: $createTextNode('yolo') }), priority: 4 }) }
    },

This is my workaround to maintain consistency in indent style:

export const htmlConfig: HTMLConfig = {
  export: new Map([
    [
      ParagraphNode,
      (editor, node) => {
        const targetNode = node as ParagraphNode;
        const element = document.createElement('p');

        if (element && isHTMLElement(element)) {
          if (targetNode.isEmpty()) {
            element.append(document.createElement('br'));
          }

          const formatType = targetNode.getFormatType();
          element.style.textAlign = formatType;

          const direction = targetNode.getDirection();
          if (direction) {
            element.dir = direction;
          }
          const indent = targetNode.getIndent();
          setElementIndent(element, indent, editor._config.theme.indent);
        }

        return {
          element,
        };
      },
    ],
  ]),
};

const DEFAULT_INDENT_VALUE = '40px';

function setElementIndent(dom: HTMLElement, indent: number, indentClassName?: string): void {
  if (indent < 1) {
    return;
  }

  if (typeof indentClassName === 'string') {
    dom.classList.add(indentClassName);
  }

  const indentationBaseValue =
    window.getComputedStyle(dom).getPropertyValue('--lexical-indent-base-value') ||
    DEFAULT_INDENT_VALUE;

  dom.style.setProperty(
    // padding-inline-start is not widely supported in email HTML, but
    // Lexical Reconciler uses padding-inline-start. Using text-indent instead.
    'text-indent',
    `${indent * parseInt(indentationBaseValue, 10)}px`
  );
}

from lexical.

astghikk-adobe avatar astghikk-adobe commented on August 17, 2024

@2wheeh will this approach override the entire ParagraphNode? I want to avoid this in order to maintain consistency with the Lexical's ParagraphNode and just add missing inline styles.

from lexical.

astghikk-adobe avatar astghikk-adobe commented on August 17, 2024

I would like to do something like this

export class CustomParagraphNode extends ParagraphNode {
  exportDOM(editor) {
    const el = super.exportDOM(editor);
    el.element.style.margin = '0px'

    return { element: el.element };
  }
}
const editorConfig = {
  namespace: 'rte',
  onError(error: Error) {
    throw error;
  },
  theme: Theme,
  nodes: [
    CustomParagraphNode,
    {
      replace: ParagraphNode,
      with: (node: ParagraphNode) => {
        return new CustomParagraphNode();
      }
    }
  ],
};

from lexical.

2wheeh avatar 2wheeh commented on August 17, 2024

this approach override the entire ParagraphNode?

This override exportDOM method of entire ParagraphNode. So use ParagraphNode.exportDOM implementation with your style manipulation.

or you can override the whole Node like this:

const editorConfig = {
    ...
    nodes=[
        // Don't forget to register your custom node separately!
        CustomParagraphNode,
        {
            replace: ParagraphNode,
            with: (node: ParagraphNode) => {
                return new CustomParagraphNode();
            }
        }
    ]
}

with this approach, you can call super() to leverage default paragraphNode's behavior somehow if you prefer this to copy and paste.

from lexical.

2wheeh avatar 2wheeh commented on August 17, 2024

Ah yes you got it

from lexical.

2wheeh avatar 2wheeh commented on August 17, 2024

Is it private method? It doesn't seem so. But you need to implement other methods too for full functionality, which is why i prefer HTMLConfig.

from lexical.

2wheeh avatar 2wheeh commented on August 17, 2024
const htmlConfig: HTMLConfig = {
  export: new Map([
    [
      ParagraphNode,
      (editor, node) => {
        const {element} = node.exportDOM(editor);

        if (element && isHTMLElement(element)) {
          element.style.margin = '0px';
        }

        return {
          element,
        };
      },
    ],
  ]),
};

In this way, you can leverage default ParagraphNode.exportDOM instead of call super with customNode.

from lexical.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.