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

extraKeys option is ignored in extra CodeMirror options #4966

Closed
giuseppedandrea opened this issue Apr 18, 2022 · 3 comments
Closed

extraKeys option is ignored in extra CodeMirror options #4966

giuseppedandrea opened this issue Apr 18, 2022 · 3 comments
Labels

Comments

@giuseppedandrea
Copy link
Contributor

What happened?

In config.edn file, it is possible to add extra CodeMirror options using the :editor/extra-codemirror-options config map. But if you add the extraKeys option (used to specify extra key bindings for the editor), it is ignored.
This bug is due to the way the cm-options map is built in the following code:

cm-options (merge default-cm-options
(extra-codemirror-options)
{:mode mode
:readOnly (if ui-config/publishing? "nocursor" false)
:extraKeys #js {"Esc" (fn [cm]
;; Avoid reentrancy
(gobj/set cm "escPressed" true)
(save-file-or-block-when-blur-or-esc! cm textarea config state)
(when-let [block-id (:block/uuid config)]
(let [block (db/pull [:block/uuid block-id])]
(editor-handler/edit-block! block :max block-id))))}})

Using merge function, if a key occurs in more than one map, the mapping from the latter (left-to-right) will be the mapping in the result. For this reason, extraKeys option in the last map (used to bind Esc key) overrides a custom extraKeys option contained in extra-codemirror-options map.
Why is extraKeys option important? According to the CodeMirror documentation and a issue in the official repository, using extraKeys option is the only way to map the Tab key to insert spaces instead of a tab character.

Reproduce the Bug

  1. For example, in the config.edn file add the following configuration to map the Tab key to insert spaces instead of a tab character:
 ;; Extra CodeMirror options
 :editor/extra-codemirror-options
 {
  :extraKeys {
   "Tab" (fn [cm]
           (.execCommand cm "indentMore"))
   "Shift-Tab" (fn [cm]
           (.execCommand cm "indentLess"))
  }
 }
  1. Create a code block (```... ```)
  2. Press the Tab key in the editor
  3. Spaces should be inserted but a tab character is inserted instead

Expected Behavior

No response

Screenshots

No response

Desktop Platform Information

No response

Mobile Platform Information

No response

Additional Context

No response

@andelf andelf added the editor:code Editor CodeMirror block label Apr 22, 2022
@andelf
Copy link
Collaborator

andelf commented Apr 22, 2022

I personally prefer not to add support for this.
config.edn is a plain config file, not some extension script.

CC @tiensonqin @logseq-cldwalker Need some opinion on this.

@github-actions
Copy link

Hi There! 👋

We haven't seen any activity on this issue in a while 😴, and we just wanted to make sure that it's still relevant. If you're still experiencing this issue, you might find it helpful to update to the latest version of Logseq. The latest version includes bug fixes and new features that may help to resolve this issue, and you can download it from our website. If updating to the latest version doesn't help, please let us know by adding a comment 💬. We're here to help!

If the issue has been resolved or is no longer relevant, that's great news! 🎉
We'll go ahead and close this issue to keep our backlog organized. Please note that this issue will be closed automatically in 20 days if there is no further activity. If you need more time to resolve the issue or provide more information, please just let us know by adding a comment.

Access additional Logseq 🚀 resources:

Thanks for your contributions to Logseq! If you have any other issues or feature requests, please don't hesitate to let us know. We always welcome pull requests too!

@giuseppedandrea
Copy link
Contributor Author

#8415 partially solves this issue because it allows the use of the CodeMirror's default shortcuts to indent and dedent.
In particular:

  • Ctrl-[ is the CodeMirror's shortcut to the command indentLess (dedent the current line or selection by one indent unit)
  • Ctrl-] is the CodeMirror's shortcut to the command indentMore (indent the current line or selection by one indent unit)

By default, the indent unit is equal to 2 spaces.
Therefore, use Ctrl-] and Ctrl-[ instead of Tab and Shift-Tab to use spaces as indent unit.

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

No branches or pull requests

2 participants