GithubHelp home page GithubHelp logo

Comments (5)

outoftime avatar outoftime commented on August 28, 2024 1

Sure!

from popcode.

thayerve avatar thayerve commented on August 28, 2024

Hey @outoftime, I've been working on a solution to this with @paulproteus, using CodeMirror's blockComment function. If I submit a pull request for the feature, would you want to enable it? If not, no worries, I'm learning a lot in the process anyway. :)

from popcode.

thayerve avatar thayerve commented on August 28, 2024

Hey @outofotime!

We (@paulproteus and I) worked on this today, and came up with this implementation:

diff --git a/src/components/Editor.jsx b/src/components/Editor.jsx
index 10c173e..878e37a 100644
--- a/src/components/Editor.jsx
+++ b/src/components/Editor.jsx
@@ -13,6 +13,7 @@ import 'codemirror/mode/javascript/javascript';
 import 'codemirror/addon/edit/matchbrackets';
 import 'codemirror/addon/lint/lint';
 import 'codemirror/addon/selection/active-line';
+import 'codemirror/addon/comment/comment';
 
 import {EditorLocation} from '../records';
 import bowser from '../services/bowser';
@@ -121,6 +122,22 @@ export default function Editor({
     editor.setOption('extraKeys', {
       [`${modifier}-I`]: onAutoFormat,
       [`${modifier}-S`]: onSave,
+      [`${modifier}-/`]: () => {
+        // Get the current selection, defined by these two getCursor() calls
+        const head = editor.doc.getCursor('head');
+        const anchor = editor.doc.getCursor('anchor');
+        // Convert them into two items in an array so we can sort them, so that blockComment() can
+        // go from the start to the end
+        const positions = [
+          [head.line, head.ch],
+          [anchor.line, anchor.ch],
+        ];
+        positions.sort();
+        editor.blockComment(
+          {line: positions[0][0], ch: positions[0][1]},
+          {line: positions[1][0], ch: positions[1][1]},
+        );
+      },
     });
   }, [onAutoFormat, onSave]);
 

This would directly configure CodeMirror to respond to the Ctrl-/ (or Cmd-/) event, rather than pass it through Redux.

The main reason we went that route was because we weren't sure if we could call editor.blockComment() from within Redux. What we seemed to find was that the onAutoFormat function would return new text which contained the new modified autoformatted text, whereas our code relies on CodeMirror to modify the contents of the editor itself.

The main question -- is that OK to ask CodeMirror to modify the contents of the editor by itself?

If not, then we'll need a way to get the current selection in a Redux context, and also to figure out what to do with it (can we call blockComment() directly? is the editor available in a Redux context?).

Looking for advice, and also, if this is more trouble than it's worth, we can switch to a different issue. Wanted to do this to learn more. :)

One thing we noticed while leaving this comment is that we could easily write a function that takes the editor object and does this work, rather than doing it imperatively right there.

from popcode.

adrianfalleiro avatar adrianfalleiro commented on August 28, 2024

@thayerve I think you can also use the toggleComment method which is added by importing codemirror/addon/comment/comment.

import 'codemirror/addon/comment/comment';
...
useEffect(() => {
  const editor = editorRef.current;
  const modifier = bowser.isOS('macOS') ? 'Cmd' : 'Ctrl';
  editor.setOption('extraKeys', {
    [`${modifier}-I`]: onAutoFormat,
    [`${modifier}-S`]: onSave,
    [`${modifier}-/`]: cm => cm.toggleComment({indent: true}),
  });
}, [onAutoFormat, onSave]);

from popcode.

paulproteus avatar paulproteus commented on August 28, 2024

@outoftime since #2386 is merged, I propose you mark this issue as closed since I think it's now fixed!

from popcode.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.