-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
Support Immutable Data Structure #84
Comments
A builder API is required to be able to convert from existing data structure to Rich Text Editor Currently classes have constructors that are ambiguous , requiring children and text and other values that must be left blank or given, This doesn't make sense as whether one should supply text or children buildParagraph {
addText("my text")
addText("my text", makeBold = true)
}
buildParagraph("complete text of the paragraph")
buildParagraph("complete text of the paragraph",Alignment.Left) Builder API makes JSON deserialization user's responsibility, They can traverse on their data and build rich text state using builder pattern
I will contribute a PR on this but in a distant future as soon as I am free |
I will take a look at this as soon as I get time. |
That sounds interesting but there's only one problem with this approach, which is that react app for example doesn't know anything about this structure and they need to implement a lot of logic to transform that JSON to HTML and to transform the HTML back to JSON, it's a lot of additional work and in this process the HTML could be changed as well so problem is not solved. |
No, We don't need to care about how other editors display our data structure A lot of rich text editors for web (good ones) have their own data structure, They don't require users to type out If I wanted to use another rich text editor on my website, I would just need to convert whatever is stored in my database to the data structure or JSON that their rich text editor supports I could even go further and invent my own data structure and store that in my database and whenever I want to display it in a rich text editor, I just convert it to the data structure that that rich text editor supports, this is what I should do because both rich text editors could change their data structure any time Three Choices
|
Ah ok I get your point, that would be really cool. You need to consider not using data classes. We can't provide data classes as public APIs in libraries because they don't support binary compatibility so the classes needs to be just normal class with hashCode and toString implementations. |
Good to have
|
|
I will take some time, before I start, I will do this... |
Take a look at this article by Jake Wharton: https://jakewharton.com/public-api-challenges-in-kotlin/ |
The initial importing and exporting using
HTML
orMarkdown
is good but its not the one that fits every requirementProblem
Suppose I have a backend, I can store the output of this editor as
HTML
orMarkdown
, Now that goes to my database, I have a website which is built usingReact
which needs to display this rich text in another editor which is built usingReact
orJavascript
, If I store it using HTML, well I must be able to parse and display the HTML exactly like this editor does otherwise differences will be introduced. If I store markdown, I will have to make sacrifices on features that markdown doesn't support, An immutable data structure makes it very easyWe make very clean, immutable data classes storing only primitive types like strings, representing the rich text in editor
Solution
A immutable data structure model must be created
Since data classes would be immutable, The structure will be clear so user knows all the types of models our editor has suppose
ImmutableParagraph
,ImmutableRichText(val isBold, ...)
The data structure would only contain properties that the editor supports and to methods to convert to the mutable data structure that is editable when editor is running
for example
In Kotlin, usually immutable classes don't start with
immutable
word and instead mutable classes havemutable
in their names like List and MutableList, since mutable classes are already internal, This change could be made any time when the data structure is mature enoughPros
Cons
Thoughts on Exposing Core Data Structure
The data structure must be separated out to another library
core
, The editor must makeimplementation
dependency on core, so Users cannot access the data structure unless they explicitly specify a implementation dependency on thecore
libraryThis would solve
internal
on each data class, making it unable to use with a solid use caseWhere when the user gives HTML , The HTML parser parses the html to this immutable data structure, When markdown is given the parser converts to this immutable data structure
Supporting JSON Serialization
If users were to store the immutable model as
json
in their databases, We must maintain compatibility backwards@JsonNames("previous_property_name")
annotation fromkotlinx.serialization
on the new propertyjson
using the optionignoreUnknownKeys
inkotlinx.serialization
library, or we need to make it nullable and set its default value to null and make the property deprecatedjson
with that property using the new version of the library, previous version of the library just cannot deserialize itLet me know if you need any help, I can contribute
The text was updated successfully, but these errors were encountered: