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

[lexical-react][lexical-dev-tools-core] Feature: Allow TreeView custom print output #6180

Merged

Conversation

irahopkinson
Copy link
Contributor

@irahopkinson irahopkinson commented May 28, 2024

Description

  • Previously, the TreeView output couldn't be customized.
  • This PR allows you to customize existing output and support custom node output.
    • added optional customPrintNode function property to TreeView.
      • If the function is not defined then the built-in output is used in the TreeView.
      • If the function returns nothing then the built-in output is used.
      • If the function returns text then that text is used in the output for that node.
    • moved output of mark IDs to printNode so it can be customized (it didn't seem to belong where it was anyway).
    • made appropriate changes to flow.
    • Note I didn't know how to update the API documentation as requested in https://github.com/facebook/lexical/blob/main/CONTRIBUTING.md but it looks like it is auto-generated anyway.

Test plan

Before

Previously in the Playground the TreeView output couldn't be customized:
image

After

To test, I changed packages\lexical-playground\src\plugins\TreeViewPlugin\index.tsx to:

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */

import {$isMarkNode} from '@lexical/mark';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {TreeView} from '@lexical/react/LexicalTreeView';
import {LexicalNode} from 'lexical';
import * as React from 'react';

function $customPrintNode(node: LexicalNode) {
  if ($isMarkNode(node)) {
    return `uids: [ ${node.getIDs().join(', ')} ]`;
  }
  return '';
}

export default function TreeViewPlugin(): JSX.Element {
  const [editor] = useLexicalComposerContext();
  return (
    <TreeView
      viewClassName="tree-view-output"
      treeTypeButtonClassName="debug-treetype-button"
      timeTravelPanelClassName="debug-timetravel-panel"
      timeTravelButtonClassName="debug-timetravel-button"
      timeTravelPanelSliderClassName="debug-timetravel-panel-slider"
      timeTravelPanelButtonClassName="debug-timetravel-panel-button"
      customPrintNode={$customPrintNode}
      editor={editor}
    />
  );
}

And then the Playground displays:
image

Note in the TreeView the mark IDs array label is now "uids" (instead of "id").

Copy link

vercel bot commented May 28, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 29, 2024 0:54am
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 29, 2024 0:54am

@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 May 28, 2024
Copy link

github-actions bot commented May 28, 2024

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
packages/lexical/dist/Lexical.js 23.95 KB (0%) 480 ms (0%) 210 ms (+159.97% 🔺) 689 ms
packages/lexical-rich-text/dist/LexicalRichText.js 34.63 KB (0%) 693 ms (0%) 703 ms (+172.31% 🔺) 1.4 s
packages/lexical-plain-text/dist/LexicalPlainText.js 34.61 KB (0%) 693 ms (0%) 578 ms (+80% 🔺) 1.3 s

@@ -245,9 +249,17 @@ function normalize(text: string, obfuscateText: boolean = false) {
return textToPrint;
}

// TODO Pass via props to allow customizability
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment may still be valid, perhaps we shouldnt delete it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This PR allows full customizability for the TreeView print output for any existing node or any custom node and it it does it by passing a function in through the props. Perhaps I'm missing something but what part of that comment still needs implementing?

Comment on lines 257 to 262
const customPrint = customPrintNode
? customPrintNode(node, obfuscateText)
: undefined;
if (customPrint) {
return customPrint;
Copy link
Contributor

@potatowagon potatowagon May 28, 2024

Choose a reason for hiding this comment

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

Suggested change
const customPrint = customPrintNode
? customPrintNode(node, obfuscateText)
: undefined;
if (customPrint) {
return customPrint;
if (customPrintNode !== undefined) {
return customPrintNode(node, obfuscateText);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We only want the custom print output if customPrintNode(...) produces some text, i.e. if it returns an empty string because it doesn't handle a node then the existing code below would run and we would get the built-in print output. Your suggested change would not do that but return no output. That's not what we want here.

Copy link
Contributor

Choose a reason for hiding this comment

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

if (customPrint) isnt clear because customPrint isnt a boolean. preferably be more explicit about the comparison, such as

if (customPrint !== undefined && customPrint.length > 0) {
    return customPrint;
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense, done.

@potatowagon
Copy link
Contributor

what are some example usecases of custom print node?

: undefined;
if (customPrint) {
return customPrint;
} else if ($isTextNode(node)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

since the if block above returns we dont need to wrap this in an else

Suggested change
} else if ($isTextNode(node)) {
if ($isTextNode(node)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was just following the existing pattern as all the following else if blocks all return. Strictly they could all be changed to if blocks but I think that would make the code harder to read. I'm happy to change them all to if blocks if you prefer that?

Copy link
Contributor

Choose a reason for hiding this comment

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

the custom print case is a seperate case from the default print so its clearer to have it in its own if block rather then lump it tgt with the default print conditions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense also, done.

Copy link
Contributor Author

@irahopkinson irahopkinson left a comment

Choose a reason for hiding this comment

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

Thanks for your speedy response.

@@ -245,9 +249,17 @@ function normalize(text: string, obfuscateText: boolean = false) {
return textToPrint;
}

// TODO Pass via props to allow customizability
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This PR allows full customizability for the TreeView print output for any existing node or any custom node and it it does it by passing a function in through the props. Perhaps I'm missing something but what part of that comment still needs implementing?

Comment on lines 257 to 262
const customPrint = customPrintNode
? customPrintNode(node, obfuscateText)
: undefined;
if (customPrint) {
return customPrint;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We only want the custom print output if customPrintNode(...) produces some text, i.e. if it returns an empty string because it doesn't handle a node then the existing code below would run and we would get the built-in print output. Your suggested change would not do that but return no output. That's not what we want here.

: undefined;
if (customPrint) {
return customPrint;
} else if ($isTextNode(node)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was just following the existing pattern as all the following else if blocks all return. Strictly they could all be changed to if blocks but I think that would make the code harder to read. I'm happy to change them all to if blocks if you prefer that?

@irahopkinson
Copy link
Contributor Author

irahopkinson commented May 28, 2024

what are some example usecases of custom print node?

The test code in the PR description above is one example where we override the existing output for a MarkNode. So existing output can be changed when debugging, e.g. I could also include an attribute's value in the output.

Also you can add handling of TreeView output for custom nodes, e.g. if I created a Custom MarkNode that grouped IDs according to type then I could display the output of those grouped IDs when debugging.

Copy link
Contributor Author

@irahopkinson irahopkinson left a comment

Choose a reason for hiding this comment

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

I also rebased since generateContent.ts file changed on the main branch but I made my changes in a new commit for you to review.

@StyleT
Copy link
Contributor

StyleT commented May 28, 2024

Hi!

Thanks for contributing! I think that overall strategy here is to eventually kill TreeView (see V2 plan here #5675 (comment))... So I'm not sure it's a good idea to expand it now.

@potatowagon wdyt?

@irahopkinson
Copy link
Contributor Author

@StyleT yep totally get that and have seen that plan (and I'm excited for those changes ASAP) but there is no timing on that plan. So even if this change is only useful for a few months that would be helpful for me and hopefully others. Also given that the customPrintNode property is optional I wouldn't think adding it would hurt anything. However, I'm happy to let this PR go if it doesn't fit.

@StyleT
Copy link
Contributor

StyleT commented May 28, 2024

@StyleT yep totally get that and have seen that plan (and I'm excited for those changes ASAP) but there is no timing on that plan. So even if this change is only useful for a few months that would be helpful for me and hopefully others. Also given that the customPrintNode property is optional I wouldn't think adding it would hurt anything. However, I'm happy to let this PR go if it doesn't fit.

I'm not strictly against it for sure! Let's hear other opinions here

@@ -32,6 +32,11 @@ import {

import {LexicalCommandLog} from './useLexicalCommandsLog';

export type CustomPrintNode = (
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: preferably rename CustomPrintNode to CustomPrintNodeFn to not confuse this with other lexical nodes such as TextNode

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@potatowagon
Copy link
Contributor

potatowagon commented May 29, 2024

the re

Hi!

Thanks for contributing! I think that overall strategy here is to eventually kill TreeView (see V2 plan here #5675 (comment))... So I'm not sure it's a good idea to expand it now.

@potatowagon wdyt?

the example usecases listed by @irahopkinson makes sense to me so im good to have this merge, potentially helps unblock debugging of custom nodes

- added optional `customPrintNode` function property. If the function returns nothing then the built-in output is used. So you can customize existing output and support custom node output.
- moved output of mark IDs to `printNode` so it can be customized (it didn't seem to belong where it was anyway).
- made appropriate changes to flow.
Copy link
Contributor

@potatowagon potatowagon left a comment

Choose a reason for hiding this comment

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

👍

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.

4 participants