[Question] Losing the component information on saving the code from Code Editor
See original GitHub issueI have integrated the code edit option in the Web builder.
-
Using
editor.getHtml()
to get the current HTML content and populate in the code editor -
While saving the code from the code editor, I am using
editor.setComponents()
to set the edited content to the builder canvas.
Issues
- On Saving the html from the code editor, it loses all the old component related information and acts as a new component with default values in the traits.
data-gjs-type
becomes default and it is not recognizing the custom components.
Anaysis & Thoughts On checking it seems like,
- when we use
editor.getHtml()
it gives the final html without anydata-gjs-*
attributes. editor.setComponents()
method seems to override the old component information.
Questions
- In order to retain all the old component information and use it after editing the HTML in the editor, how can we do that?
- If it is by using
editor.getComponents()
, how can we store and reuse after saving the content from the code editor?
Issue Analytics
- State:
- Created 3 years ago
- Comments:7
Top Results From Across the Web
react native - Visual Studio Code Formatting fail on save
I'm using Visual Studio Code as my editor. I've set up the boiler plate and started the app. But whenever I change a...
Read more >Visual Studio Code Frequently Asked Questions
Our docs contain a Common questions section as needed for specific topics. ... What is the difference between Visual Studio Code and Visual...
Read more >Code editor features - Visual Studio (Windows)
Learn about the features that the code editor in Visual Studio provides to make it easier for you to write and manage your...
Read more >How to get content from the editor and set content
The TinyMCE getContent and setContent methods. Once a user has entered content in the editor, you'll probably want to save the content to...
Read more >HTML Editor: Tips and Troubleshooting | D2L Help Pages
1. Problem: Font is Different Sizes When you create your content in Word (or any similar program), you generate content and computer codes....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@sathyanarayananaCES First, to echo others that it is best to use
get/setComponents
andget/setStyle
to save the content of the editor. If I understand your usage correctly, that you want HTML forhtmlCodeEditor
and be able to import back into theeditor
after user edit. Take a look at the demo of the editor import which simply callsetComponents
with raw HTML. Refs - https://github.com/artf/grapesjs/blob/dev/docs/api/editor.md#examples and https://github.com/artf/grapesjs/blob/dev/test/specs/grapesjs/index.js#L180To do what you want will require call to
editor.genHtml()
for thehtmlCodeViewer.setContent
and import back witheditor.setComponents
with the raw HTML fromhtmlCodeViewer
instead of dealing with components array. The gotcha/pitfall that you have to understand is, internally, grapejs try its best to parse that raw HTML back into components array. Parsing HTML is difficult even with valid HTML. Therefore, you will get unexpected result and may lose data that grapejs cannot parse. This is the reason why everyone is suggesting to usegetComponents
andgetStyle
if you ever want to store and load it back. I hope that provide some clarity into why you are losing data? It’s because you are using import/grapesjs HTML parsing.@jenter #1331 comment and #2644 comment are good examples. Also checkout #2664 comment which is a pitfall to avoid! Cheers!