homebrewery icon indicating copy to clipboard operation
homebrewery copied to clipboard

[Editor] Prevent unfolding code in Editor when adding a Snippet

Open G-Ambatte opened this issue 2 years ago • 2 comments

This PR resolves #2135.

After investigating Gazook89's draft PR #2391, I was unable to automate the re-folding of previously folded code blocks in the Editor. However, on comparing the handleInject function to the shortcuts for page and column breaks, I realized that the handleInject function was operating in a very different fashion, causing the document to effectively be regenerated, losing the folded marks. By moving this function out of the editor.jsx to codeEditor.jsx and using the same method (replaceSelection) as the shortcut keys, folded code segments are no longer unfolded when a snippet is injected.

At this stage, I have included an optional overwrite parameter to overwrite the existing selection, but the handleInject function is currently called with this set to false in order to mimic the previous functionality.

G-Ambatte avatar Sep 24 '22 01:09 G-Ambatte

I think there is potentially value in storing fold information; however I think that would be a very different problem to solve than what this PR has been about.

G-Ambatte avatar Sep 24 '22 04:09 G-Ambatte

Folded Preview Content

While you are in the folding functions, do you want to visit this issue #1907 concerning what appears in the folded 'preview' of the page? Displaying just the first header on the page would be ideal for me. Even if the first lines are not a header I think it should find the first header-- it's a better descriptor of "what" is on the page than just a random line of text or code. And if no header exists on the page, resort to just whatever is in the first line.


Folded Preview Styling

At the same time, I think it's a good idea to rethink the styling of the folded preview text-- bold bright blue text for the preview text draws a lot of visual attention to text that likely should be taking a backseat, since it is folded. Changing it to regular weight and gray would be my recommendation.


Again, separate PR might be needed though I think the above two points could be tackled together easily.

Gazook89 avatar Sep 25 '22 00:09 Gazook89

It's either/or, unfortunately. There's a post-replacement option which is currently set to around, which selects the newly inserted text. Changing this to end should place the cursor after the inserted snippet.

I can push a change using that option so we can determine what the preferred behavior is.

G-Ambatte avatar Sep 26 '22 03:09 G-Ambatte

@calculuschild I've pushed a change to the post-insert selection now, so it uses the same parameter as inserting a page or column break. It's a bit different when inserting a snippet, as it doesn't (currently) use a hot key, so the user needs to click back into the Editor anyway. Give it a try and let me know what you think!

G-Ambatte avatar Sep 27 '22 04:09 G-Ambatte

Yep this is good. I also just added cm.focus() at the end which allows immediately typing again without needing a click (same as previous behavior).

Merging now! 💯 🎉

calculuschild avatar Oct 18 '22 17:10 calculuschild