Skip to content

Commit

Permalink
Handle selections correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhabib committed Feb 13, 2024
1 parent 1eafb13 commit 15c2ac3
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/Playroom/CodeEditor/keymaps/comment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type CodeMirror from 'codemirror';
import { type Editor, Pos } from 'codemirror';
// import type { Selection } from './types';
import type { Selection } from './types';

interface TagRange {
from: CodeMirror.Position;
Expand All @@ -10,6 +10,7 @@ interface TagRange {
}

export const wrapInComment = (cm: Editor) => {
const newSelections: Selection[] = [];
const tagRanges: TagRange[] = [];

// let linesAdded = 0;
Expand All @@ -35,6 +36,19 @@ export const wrapInComment = (cm: Editor) => {
existingIndent,
});

// Todo - change offset "+ 4" for prop comment
const newSelectionRangeAnchor = new Pos(from.line, from.ch + 4);

const newSelectionRangeHead = new Pos(
to.line,
to.ch + (isMultiLineSelection ? 0 : 4)
);

newSelections.push({
anchor: newSelectionRangeAnchor,
head: newSelectionRangeHead,
});

// Todo - incorporate lines added
// if (isMultiLineSelection) {
// linesAdded += 2;
Expand All @@ -43,11 +57,21 @@ export const wrapInComment = (cm: Editor) => {

cm.operation(() => {
for (const range of [...tagRanges].reverse()) {
// Todo - handle partial line selection
// if (range.from.ch !== 0) {
// const newFrom = new Pos(range.from.line, 0);
// cm.replaceRange('/* ', newFrom, range.from);
// } else {
// cm.replaceRange('/* ', range.from, range.from);
// }

const existingContent = cm.getRange(range.from, range.to);

// const isMultiLineSelection = to.line !== from.line;

cm.replaceRange(`{/* ${existingContent} */}`, range.from, range.to);
}

cm.setSelections(newSelections);
});
};

0 comments on commit 15c2ac3

Please sign in to comment.