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

Saving content of Rich Text #334

Closed
IcarusKR opened this issue Jun 29, 2016 · 4 comments
Closed

Saving content of Rich Text #334

IcarusKR opened this issue Jun 29, 2016 · 4 comments
Labels

Comments

@IcarusKR
Copy link

Is there an easy way to save the formated text and reload it?

I have been checking out the example and it exactly what I need, but cant figure out how to get the formated text and save it to some file or database, and reload it again.

@afester
Copy link
Collaborator

afester commented Jun 29, 2016

You can check #315 where I have implemented a load/save functionality for the rich text editor example. There are still some issues which need to be solved, but it should give you an idea on how to start.

@TomasMikula From an API side, one thing I was missing is the possibility to build the model by calling methods such as addParagraph(), addSegment() and the like, using classes from the model layer only, and then use the resulting StyledDocument and create a StyledTextArea with that model (or set the model on an existing StyledTextArea). As far as I understand, StyledTextArea.append() and its variants is currently the only possibility to put content into the styled text area.

In summary, it should be possible to

  • Build the model, using classes from the modelpackage only. Result is a StyledDocument.
  • Create a StyledTextArea and set the previously created StyledDocument on it.
  • Get back the current document from the StyledTextArea and traverse its structure to create any kind of external representation.

@JordanMartinez
Copy link
Contributor

@afester just to clarify, by 'model,' do you mean the StyledDocument (i.e. the content) and not the StyledTextAreaModel?

From an API side, one thing I was missing is the possibility to build the model by calling methods such as addParagraph(), addSegment() and the like..... In summary, it should be possible to build the model, using classes from the model package only. Result is a StyledDocument.

Were you thinking of some builder-like API for this?

ReadOnlyStyledDocument.startParagraph(paragraphStyle)
    .addSegment(text, style)
    .addSegment(text, style)
    .addSegment(text, style)
    .endParagraph()
    .startParagraph(paragraphStyle)

or even

// t(text, style) --> Tuple2<String, Style>, which is later turned into a StyledText object
ReadOnlyStyledDocument.addParagraph(paragraphStyle, t(text, style), t(text, style))
    .addParagraph(paragraphStyle, t(text, style), t(text, style))
    .addParagraph(paragraphStyle, t(text, style), t(text, style))

Create a StyledTextArea and set the previously created StyledDocument on it.

Isn't this already possible in one of StyledTextArea's constructors? By default, an EditableStyledDocument is created for you if you don't pass in your own. (at least for the current snapshot release)

Get back the current document from the StyledTextArea and traverse its structure to create any kind of external representation.

What do you have in mind here?

@TomasMikula
Copy link
Member

@IcarusKR There is no one-liner to do that. In the most general case (StyledTextArea), this cannot be done all automatically by RichTextFX, because the style objects are of unknown type (for StyledTextArea, they are just type parameters). It could be supported if we ask the user to provide Codecs to serialize the style objects and RichTextFX handles the rest (this is done for serializing the document to the clipboard). That, however, would create a kind of commitment for us to not change the binary format in the future—if people used it to save their documents, they might not be able to load that document with a newer version of RichTextFX, if we changed the binary format in the meantime. I think it is best for everyone if the persistent representation is under the user's control.

Here's how you can proceed:

  1. Call getDocument to obtain the content of the text area, of type StyledDocument, which is basically a list of Paragraphs (which you obtain by calling getParagraphs). So you just need to serialize a list of paragraphs. (If you define a Codec for Paragraph, listCodec gives you a Codec for a list of paragraphs, but no guarantees on backwards compatibility).

    StyledDocument<PS, S> ≃ List<Paragraph<PS, S>>
    
  2. A Paragraph is basically a paragraph style description (an object of type PS, where PS is a user-defined type argument) and a list of styled text segments, so you just need to serialize those.

    Paragraph<PS, S> ≃ (PS, List<StyledText<S>>)
    
  3. StyledText is basically just a string and a style description (an object of type S, which is a user-supplied type argument).

    StyledText<S> ≃ (String, S)
    

All in all, we have

StyledDocument<PS, S> ≃ List<(PS, List<(String, S)>)>

You can see that it is not too hard to write your own code to serialize this.

@afester

  • Build the model, using classes from the modelpackage only. Result is a StyledDocument.

This is possible, although maybe we are missing some convenience methods.

  • Create a StyledTextArea and set the previously created StyledDocument on it.

See @JordanMartinez's comment above. I don't see much benefit in being able to change the underlying document of the area after construction, if that's what you mean. You can just create a new area.

  • Get back the current document from the StyledTextArea and traverse its structure to create any kind of external representation.

StyledTextArea.getDocument()

@JordanMartinez
Copy link
Contributor

Due to lack of activity on this issue, I'm closing it. @IcarusKR If you still need help, reopen it and ask your questions.

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

No branches or pull requests

4 participants