From 55e400841680a0ea7ff3a4b537b3f48bdbcb1503 Mon Sep 17 00:00:00 2001 From: Ben Jervis Date: Tue, 13 Feb 2024 15:12:54 +1100 Subject: [PATCH] Support reverse selections --- src/Playroom/CodeEditor/keymaps/comment.ts | 81 ++++++---------------- 1 file changed, 23 insertions(+), 58 deletions(-) diff --git a/src/Playroom/CodeEditor/keymaps/comment.ts b/src/Playroom/CodeEditor/keymaps/comment.ts index 28577c7d..eb8c40de 100644 --- a/src/Playroom/CodeEditor/keymaps/comment.ts +++ b/src/Playroom/CodeEditor/keymaps/comment.ts @@ -2,41 +2,22 @@ import type CodeMirror from 'codemirror'; import { type Editor, Pos } from 'codemirror'; import type { Selection } from './types'; -const COMMENT_SYNTAX_OFFSET = '{/* '.length; - -// Todo - find word for bookend in software -interface GetSelectionRangeOffsetCountOptions { - pointType: 'anchor' | 'head'; - multiLine?: boolean; - reverseSelection: boolean; -} - -function getSelectionRangeOffsetCount({ - pointType, - multiLine, - reverseSelection, -}: GetSelectionRangeOffsetCountOptions): number { - if (!multiLine) { - return COMMENT_SYNTAX_OFFSET; - } - - if ( - (pointType === 'head' && !reverseSelection) || - (pointType === 'anchor' && reverseSelection) - ) { - return 0; - } - - return COMMENT_SYNTAX_OFFSET; -} +const BLOCK_COMMENT_OFFSET = '{/* '.length; +// const LINE_COMMENT_OFFSET = '// '.length; interface IsReverseSelectionOptions { - from: CodeMirror.Position; - to: CodeMirror.Position; + anchor: CodeMirror.Position; + head: CodeMirror.Position; } -function isReverseSelection({ from, to }: IsReverseSelectionOptions): boolean { - return from.line > to.line || (from.line === to.line && from.ch > to.ch); +function isReverseSelection({ + anchor, + head, +}: IsReverseSelectionOptions): boolean { + return ( + anchor.line > head.line || + (anchor.line === head.line && anchor.ch > head.ch) + ); } interface TagRange { @@ -71,37 +52,21 @@ export const wrapInComment = (cm: Editor) => { existingIndent, }); - // Todo - change offset "+ 4" for prop comment - const anchorOffset = getSelectionRangeOffsetCount({ - pointType: 'anchor', - multiLine: isMultiLineSelection, - reverseSelection: isReverseSelection({ from, to }), - }); - - const headOffset = getSelectionRangeOffsetCount({ - pointType: 'head', - multiLine: isMultiLineSelection, - reverseSelection: isReverseSelection({ from, to }), - }); + // Todo - change offset from BLOCK_COMMENT_OFFSET to LINE_COMMENT_OFFSET for prop comment - const newSelectionRangeAnchor = new Pos(from.line, from.ch + anchorOffset); - const newSelectionRangeHead = new Pos(to.line, to.ch + headOffset); + const toOffset = isMultiLineSelection ? 0 : BLOCK_COMMENT_OFFSET; - newSelections.push({ - anchor: newSelectionRangeAnchor, - head: newSelectionRangeHead, - }); + const newSelectionRangeFrom = new Pos( + from.line, + from.ch + BLOCK_COMMENT_OFFSET + ); + const newSelectionRangeTo = new Pos(to.line, to.ch + toOffset); - if (from.ch < to.ch) { - console.log('from.ch < to.ch'); - } else { - console.log('from.ch >= to.ch'); - } + const newSelection = isReverseSelection(range) + ? { anchor: newSelectionRangeTo, head: newSelectionRangeFrom } + : { anchor: newSelectionRangeFrom, head: newSelectionRangeTo }; - // Todo - incorporate lines added - // if (isMultiLineSelection) { - // linesAdded += 2; - // } + newSelections.push(newSelection); } cm.operation(() => {