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

how to extract rendered html text ? #318

Closed
elmissouri16 opened this issue Sep 23, 2020 · 16 comments
Closed

how to extract rendered html text ? #318

elmissouri16 opened this issue Sep 23, 2020 · 16 comments
Labels
question Further information is requested

Comments

@elmissouri16
Copy link

first thanks to the amazing work, I have a question is there any way to get rendered HTML text ? , I want to copy it to use it else where

@daohoangson
Copy link
Owner

What did you mean by extracted?

The input HTML was passed into the widget. If you need it somewhere else, just forward it yourself I think?

@daohoangson daohoangson added the question Further information is requested label Sep 23, 2020
@elmissouri16
Copy link
Author

@daohoangson thanks for the quick reply, whats I mean is extract the rendered HTML as text from it

@daohoangson
Copy link
Owner

Looks like you want to copy the text of the rendered paragraph? That's currently not possible because SelectableText widget is not fully implemented.

See #102 and flutter/flutter#38474

@elmissouri16
Copy link
Author

ok got it this was i used its kinda work but not whats i want

 Widget buildText(BuildMetadata meta, TextStyleHtml tsh, InlineSpan text) {
    final textWidget = super.buildText(meta, tsh, text);
    if (textWidget == null) return textWidget;
    return SelectableText.rich(
      TextSpan(children: [text]),
      textDirection: this.enableRtl ? TextDirection.rtl : TextDirection.ltr,
      style: tsh.style,
    );
  }

@daohoangson
Copy link
Owner

Yep, doing that will make the text selectable but the WidgetSpans won't work...

And you said that was not it, so... what do you want exactly? Do you have some app in mind that does something special? Link or screenshot would be useful here.

@elmissouri16
Copy link
Author

I'm building a news app the pull the data from WordPress rest API I want to be to able to give user option to select the text of the full article and use it , the approach I used only allow to select paragraph at a time the Textspan I suppose her my app how its looks like image

@daohoangson
Copy link
Owner

That is correct. Your change will allow copying text within a paragraph but it won't work for the full article. Actually it depends on the HTML, if there is no special block, it may be rendered as one RichText (which will be replaced by your code to use SelectableText.rich) so it's possible to copy everything at once.

For complicated HTML, that won't work of course...

@elmissouri16
Copy link
Author

"\n<p>Tesla took another shot at its skeptics by delivering solid second-quarter earnings — and its fourth consecutive quarterly profit — even amid the coronavirus pandemic.</p>\n\n\n\n<p>The California-based automaker also announced on Wednesday that it will move ahead on plans to set up a second U.S. assembly plant, awarding the prize to Austin, Texas. The Lone Star State had been battling it out in a multi-state bidding war. Tesla already operates two car plants, including its original factory in Fremont, California, and another in Shanghai that opened last year. A third facility near Berlin is under construction.</p>\n\n\n\n<p>n a teleconference with analysts and reporters, Tesla CEO Elon Musk rolled off a list of things he said Tesla has in store, including new vehicles and other lines of business that could substantially boost future revenues.</p>\n\n\n\n<p>“I’ve never been more excited or optimistic about the future of Tesla in the history of the company,” he said, adding, “There’s so much to be excited about it’s almost hard to fit into this call.”</p>\n\n\n\n<p>Tesla officials pointed to a number of things that helped drive Tesla’s fourth consecutive quarterly profit, including not only its new Model Y and the China plant, but also its expanding presence in the energy storage business.</p>\n\n\n\n<p>“Tesla Energy will be bigger than automotive,” Musk said during the company&#8217;s quarterly earnings call on Wednesday night, pointing to a shift to sustainable sources such as wind and solar.</p>\n\n\n\n<p>Looking forward, Musk outlined plans for an expanded line-up of battery-electric vehicles, with the company set to launch production of its heavy-duty Semi truck next year. It is also moving ahead with the development of its Cybertruck pickup. In the longer term, Musk hinted, it may be “reasonable to assume” Tesla will add a compact vehicle, “and probably a higher capacity passenger vehicle.” He did not offer additional details on Wednesday night, however.</p>\n"

the HTML be like this maybe I can remove all the p tags on the middle and leave only the opening and closing one the paragraphs will be separated by a new line breaks

@daohoangson
Copy link
Owner

maybe I can remove all the p tags on the middle and leave only the opening and closing one the paragraphs will be separated by a new line breaks

I think that might work 👍

@emvaized
Copy link

emvaized commented Dec 29, 2020

@elmissouri16
I've managed to achieve great results using extended_text.
Nice addition is little pop-up with 'copy' command on selecting text.

  @override
  Widget buildText(BuildMetadata meta, TextStyleHtml tsh, InlineSpan text) {
    final textWidget = super.buildText(meta, tsh, text);
    if (textWidget == null) return textWidget;
    return ExtendedText.rich(
      text,
      style: tsh.style,
      selectionEnabled: true,
      selectionColor: Colors.cyan,
    );
  }

@elmissouri16
Copy link
Author

@emvaized thank you i will try it definitely

@emvaized
Copy link

emvaized commented Jan 4, 2021

@daohoangson
I just found out however, that my solution above has a flaw - each paragraph of text is treated as a separate RichText, so that such weird situations occur with text selection:
Screenshot_20210105_001319

Could you think of any fix for this?

@daohoangson
Copy link
Owner

Ideally you should be able to listen to on select event and dismiss unrelated selections. After taking a look into the extended_text library document, they don't seem to have such callback and you can't "deselect"... I think a possible workaround is to use onTap and toggle selectionEnabled.

@daohoangson daohoangson reopened this Jan 5, 2021
@emvaized
Copy link

emvaized commented Jan 5, 2021

Thanks, this might work!
However, features like "Select all" still won't, as every paragraph seems to be treated as a separate RichText widget...

Maybe I can somehow wrap all the text in one single RichText widget, by overriding for example buildColumn instead of buildText?

@arianneorpilla
Copy link

Thanks, this might work!
However, features like "Select all" still won't, as every paragraph seems to be treated as a separate RichText widget...

Maybe I can somehow wrap all the text in one single RichText widget, by overriding for example buildColumn instead of buildText?

Did you ever figure out how to fix this behavior? I want my paragraphs to be merged as one and not show multiple text selection menus as I'm building an app with EPUB reader functionality for mobile.

@daohoangson
Copy link
Owner

Regarding the multiple SelectableText issue, we will need framework support, something like flutter/flutter#81839.

I'm closing this issue to consolidate all thing selectable into #102.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants