Skip to content

[flutter_markdown] Pass parent TextStyle down to MarkdownElementBuilder.visitElementAfter #3281

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

Conversation

IchordeDionysos
Copy link
Contributor

@IchordeDionysos IchordeDionysos commented Feb 23, 2023

The parent TextStyle should be passed down to the MarkdownElementBuilder.visitElementAfter method to allow custom markdown tags to override only part of the text style, e.g. the color, but keep all the rest of the styles the same.

This is especially useful when trying to color markdown headers in a certain color, as the parent font size, font family, etc. all are passed down and can be kept, while only the color is overridden.

This will unfortunately lead to a breaking change, due to the nature of how the class is typically used. As all usages of the class are sub-classes any change to the method schema will result in a breaking change!

Enables the following flutter/flutter#105571

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@IchordeDionysos
Copy link
Contributor Author

@domesticmouse, how do we deal with the breaking change of the PR?
Can I just bump the version to the next breaking version, or do you want to consolidate multiple breaking changes into one major release?

@domesticmouse
Copy link
Contributor

@domesticmouse, how do we deal with the breaking change of the PR?

Can I just bump the version to the next breaking version, or do you want to consolidate multiple breaking changes into one major release?

@stuartmorgan thoughts?

@IchordeDionysos
Copy link
Contributor Author

I've also exposed now the BuildContext in the visitElementAfter method if you need to access the context for some reason, you no longer have to fallback to workarounds like this:

Which is a very hacky workaround and has it's own drawbacks 🙈

  @override
  Widget visitElementAfter(md.Element element, TextStyle? preferredStyle) {
    return RichText(
      text: TextSpan(
        children: [
          WidgetSpan(
            child: Builder(
              builder: (context) {
                return Text(
                  innerText,
                  style: preferredStyle
                );
              },
            ),
          ),
        ],
      ),
    );
  }

Feels free to leave any suggestions/feedback/change requests!

@stuartmorgan-g
Copy link
Contributor

@domesticmouse, how do we deal with the breaking change of the PR?
Can I just bump the version to the next breaking version, or do you want to consolidate multiple breaking changes into one major release?

@stuartmorgan thoughts?

Does it have to be a breaking change? What about a new method with a default implementation that calls the existing method (discarding the extra parameters)?

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@IchordeDionysos
Copy link
Contributor Author

@stuartmorgan thanks for the hint, I've just implemented it, not sure yet about the name of the new function 🤔

/cc @domesticmouse

@IchordeDionysos
Copy link
Contributor Author

@domesticmouse if you find the time to give this a review here that would be awesome :)

It's no longer a breaking change so should be easier to merge

@stuartmorgan-g stuartmorgan-g self-requested a review April 4, 2023 20:07
Copy link
Contributor

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

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

CI is failing the autoformat check, so the code will need to be autoformatted.


* Introduce a new `MarkdownElementBuilder.visitElementAfterWithContext()` method passing the widget `BuildContext` and
Copy link
Contributor

Choose a reason for hiding this comment

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

"Introduces"

See the CHANGELOG style guide linked from the PR descirption.


* Introduce a new `MarkdownElementBuilder.visitElementAfterWithContext()` method passing the widget `BuildContext` and
the parent texts' `TextStyle`.
This should allow custom syntax implementations to get data from the current context, as well as properly style any
Copy link
Contributor

Choose a reason for hiding this comment

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

The changelog should just describe the change (the first sentence); it doesn't need to provide the motivation for it.


* Introduce a new `MarkdownElementBuilder.visitElementAfterWithContext()` method passing the widget `BuildContext` and
the parent texts' `TextStyle`.
Copy link
Contributor

Choose a reason for hiding this comment

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

text's

/// Called when an Element has been reached, after its children have been
/// visited.
///
/// If [MarkdownWidget.styleSheet] has a style of this tag, will passing
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't parse as a sentence. I'm assuming this should say something like "has a style for this tag, it will be passed as [preferredStyle]."?

/// If [MarkdownWidget.styleSheet] has a style of this tag, will passing
/// to [preferredStyle].
///
/// If parent element has [TextStyle]'s set, it will be passed as
Copy link
Contributor

Choose a reason for hiding this comment

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

"has a [TextStyle] set"

@stuartmorgan-g
Copy link
Contributor

@IchordeDionysos Are you still planning on updating this based on the last round of feedback?

@stuartmorgan-g stuartmorgan-g marked this pull request as draft May 23, 2023 19:42
@stuartmorgan-g
Copy link
Contributor

I'm making this a draft just to get it off of the review queue; please mark it as ready for review once you've had a chance to update. Thanks!

@stuartmorgan-g
Copy link
Contributor

@tarrinneal to adopt, per triage meeting.

@tarrinneal
Copy link
Contributor

Closing and moving changes over to #4393 as I cannot make changes to this pr.

@tarrinneal tarrinneal closed this Jul 7, 2023
auto-submit bot pushed a commit that referenced this pull request Jul 7, 2023
…er.visitElementAfter (#4393)

The parent `TextStyle` should be passed down to the `MarkdownElementBuilder.visitElementAfter` method to allow custom markdown tags to override only part of the text style, e.g. the color, but keep all the rest of the styles the same.

This is especially useful when trying to color markdown headers in a certain color, as the parent font size, font family, etc. all are passed down and can be kept, while only the color is overridden.

This will unfortunately lead to a breaking change, due to the nature of how the class is typically used. As all usages of the class are sub-classes any change to the method schema will result in a breaking change!

Enables the following flutter/flutter#105571

replaces #3281
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants