Skip to content

Replace text format in DOMConversions with childConversion#1154

Merged
acywatson merged 5 commits intomainfrom
child-conversion
Jan 29, 2022
Merged

Replace text format in DOMConversions with childConversion#1154
acywatson merged 5 commits intomainfrom
child-conversion

Conversation

@acywatson
Copy link
Copy Markdown
Contributor

@acywatson acywatson commented Jan 21, 2022

Instead of having a format property that just applies formatting to all children of the node, this allows the user to specify a function that applies to all children of the specified DOM node type.

Closes #948

@acywatson acywatson requested review from trueadm and zurfyx January 21, 2022 07:01
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 21, 2022
Copy link
Copy Markdown
Member

@zurfyx zurfyx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I mentioned in #1153, I think that we should at least consider the ability to have transform-like functions. This can potentially grow to many edge cases, not sure if we want to introduce a separate hook for every one of these. Haven't thought too much about it though, maybe it's already the most correct approach

@vercel
Copy link
Copy Markdown

vercel bot commented Jan 24, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/fbopensource/lexical/7GSjfmz5djcMVxqfr77ppNfN6pnU
✅ Preview: https://lexical-git-child-conversion-fbopensource.vercel.app

@fantactuka
Copy link
Copy Markdown
Collaborator

New after API should cover forChild usage, or is there any new use cases you think won't work?

  u: (domNode: Node) => {
    return {
      node: null,
      after: (lexicalNodes) => {
        for (let i = 0; i < lexicalNodes.length; i++) {
          if ($isTextNode(lexicalNodes[i])) {
            lexicalNodes[i].toggleFormat('underline');
          }
        }
        return lexicalNodes;
      },
    };
  },

@acywatson
Copy link
Copy Markdown
Contributor Author

acywatson commented Jan 27, 2022

New after API should cover forChild usage, or is there any new use cases you think won't work?

  u: (domNode: Node) => {
    return {
      node: null,
      after: (lexicalNodes) => {
        for (let i = 0; i < lexicalNodes.length; i++) {
          if ($isTextNode(lexicalNodes[i])) {
            lexicalNodes[i].toggleFormat('underline');
          }
        }
        return lexicalNodes;
      },
    };
  },

Technically, it will, but in cases where the TextNodes are not direct children of the tag applying the formatting, we’d miss them. You’d have to visit every node in the subtree, not just the direct children. One example is:

<u><a>Hello</a></u>

In this case. I was thinking forChild is more convenient and performant than traversing the entire subtree after the transformation is complete.

@fantactuka
Copy link
Copy Markdown
Collaborator

Ah, got it! I think I was confused by the name, assuming it's only for immediate child nodes. Maybe can adjust it to reflect functionality

ul: {
  node: LexicalNode,
  after(Array<LexicalNode>) => Array<LexicalNode>,  <-- post process immediate children, returned value used as children
  forChild(LexicalNode) => void                     <-- recurse into tree, returned value ignored
}

Maybe other folks have ideas, or it can be merged as is too

@acywatson
Copy link
Copy Markdown
Contributor Author

Ah, got it! I think I was confused by the name, assuming it's only for immediate child nodes. Maybe can adjust it to reflect functionality

ul: {
  node: LexicalNode,
  after(Array<LexicalNode>) => Array<LexicalNode>,  <-- post process immediate children, returned value used as children
  forChild(LexicalNode) => void                     <-- recurse into tree, returned value ignored
}

Maybe other folks have ideas, or it can be merged as is too

Yea I used to call it "childConversion", which is also not obvious. I really hate the term "conversion" for some reason.

Hopefully some documentation will help. I am going to leave this open for a bit to give others a chance to weigh in, for sure.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this one should actually be italic for em instead of underline

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good catch!

@acywatson acywatson merged commit 298cae6 into main Jan 29, 2022
@trueadm trueadm deleted the child-conversion branch April 6, 2022 15:22
acywatson added a commit that referenced this pull request Apr 9, 2022
* replace text format in DOMConversions with childConversion

* rename child transform API

* remove unncessary dom Nodde argument

* fix formatting for em tag

* fix lint error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add transformChild property to DOMTransformOutput

5 participants