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

Feature Request: Code Line Wrapping in Editing Mode #12

Open
mayurankv opened this issue Jul 8, 2023 · 9 comments
Open

Feature Request: Code Line Wrapping in Editing Mode #12

mayurankv opened this issue Jul 8, 2023 · 9 comments
Assignees
Labels
editing mode Relevant to Obsidian editing mode enhancement New feature or request help wanted Extra attention is needed long term Non-immediate action to be taken

Comments

@mayurankv
Copy link
Owner

mayurankv commented Jul 8, 2023

Summary

Add code line wrapping options in editing mode.

Basic example

Similar to how they are done in reading mode. The whole codeblock should scroll though, not individual lines.

Motivation

More usable codeblocks in edit mode.

Existing Requests?

No existing requests.

@mayurankv mayurankv added the enhancement New feature or request label Jul 8, 2023
@mayurankv mayurankv self-assigned this Jul 8, 2023
@mayurankv
Copy link
Owner Author

Consider this css I have used in the past. Scrolling document could be ok as long as codeblock covers length of code.

/*! Wrap Live Preview Codeblock */
.HyperMD-codeblock:not(.cm-active):not(:hover) > .cm-hmd-codeblock {
    white-space: nowrap !important;
}
.markdown-source-view.mod-cm6 .block-language-preview code {
    white-space: pre-wrap !important;
}

@mayurankv mayurankv added long term Non-immediate action to be taken help wanted Extra attention is needed editing mode Relevant to Obsidian editing mode labels Jul 8, 2023
@mayurankv mayurankv changed the title Feature Request: Code Wrapping in Editing Mode Feature Request: Code Line Wrapping in Editing Mode Aug 5, 2023
@mayurankv
Copy link
Owner Author

mayurankv commented Aug 9, 2023

Information courtesy of @Dimava

Wrapping in editor requires EditorView.lineWrapping extension
https://discuss.codemirror.net/t/word-wrapping/4512

new EditorView({
    state: EditorState.create({
        extensions: [
            EditorView.lineWrapping
        ]
    })
})

@mayurankv
Copy link
Owner Author

Unfortunately, this doesn't help editor wrapping since the default is already wrap. Implementing unwrap is the issue (as well as active unwrap). The only way to implement this currently that I can envisage is setting nowrap similar to the above snippets and extending the codeblock visually to cover the unwrapped lines.

The issues with this approach are:

  • Scrolls the whole document which is a bit weird (but better than scrolling individual lines)
  • How do you know how far to extend the background for the unwrapped lines?
  • Can you actually visually extend the codeblock in this way

@mayurankv
Copy link
Owner Author

Template css:

.HyperMD-codeblock:has(> .cm-widgetBuffer) > .cm-hmd-codeblock {
    white-space: nowrap !important;
}

.code-styler-line {
    overflow: visible !important;
}

.code-styler-line.HyperMD-codeblock-end {
    overflow: hidden !important
}

.code-styler-line::after {
    content: "";
    background: red;
    position: absolute;
    top: 0;
    bottom:0;
    left:0;
    right: -100px;
    z-index: -1;
}

@mayurankv
Copy link
Owner Author

See this

@mmassenzio
Copy link

mmassenzio commented Mar 2, 2024

I am not sure why this is so challenging (I have seen the comments in the Obsidian Forum, where the devs have essentially stated that this feature is beyond their abilities) but I must say that, while wrapping lines in reading mode is nice, not having it in edit mode is a major drawback.

I wish I could offer to help, but I'm a "backend guy" and CSS is pretty much like Japanese to me, so not sure how can I contribute, but if there's a way please let me know.

Love what you've done with this plugin, BTW ❤️

@mayurankv
Copy link
Owner Author

Hiya! The real problem is that the Obsidian editor is implemented in code mirror - a functional implementation of text editing framework. The problem with CodeMirror is that it treats each line as independent of other lines. This means doing things like scrolling a single line and having the other lines in the codeblock scroll is extremely hard. (You can actually unwrap lines in code blocks but they will individually scroll which is a bit useless). There is probably a way to do it and I will continue looking into it (in fact if I could find a performant way to set how far a specific line is scrolled, I think it could be done) but I don't know when I'll have the time to not only test/find a solution but also to implement it well.

Thanks for the kind words, and I hope to continue providing features!

Repository owner deleted a comment from github-actions bot Mar 2, 2024
@mayurankv
Copy link
Owner Author

In terms of helping, if you ever get familiarised with CodeMirror, please do let me know but it took me a while to get my head around it (though this is my first front-end project as well so I'm sure it wouldn't take you long). Having said this, I don't believe a code mirror based solution exists. The key struggle is grouping the scroll state of different html objects and I haven't really looked into it but I think this would need to be done in pure javascript.

@kyldvs
Copy link

kyldvs commented Jun 19, 2024

The problem with CodeMirror is that it treats each line as independent of other lines

I guess that makes sense why it's been so hard to find any plugin solving this. Would love to see this eventually :) I guess I'll have to deal with the wrapping for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
editing mode Relevant to Obsidian editing mode enhancement New feature or request help wanted Extra attention is needed long term Non-immediate action to be taken
Projects
None yet
Development

No branches or pull requests

3 participants