From 030bcd6d4f7a28ca6deea1da8607fcb82eb66bb9 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 11 Jun 2014 11:53:51 -0600 Subject: [PATCH] Make react cursor the width of a default character at the end of lines When the cursor is positioned before a character, we always make it the width of that character. But at the end of a line, there is no character to use to set the width, so we just use the defaultCharWidth. This makes the block cursor visible on empty lines in vim-mode. --- spec/editor-component-spec.coffee | 5 +++++ src/cursor-component.coffee | 3 ++- src/cursors-component.coffee | 4 ++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/spec/editor-component-spec.coffee b/spec/editor-component-spec.coffee index 3d8a303d7..631434ac5 100644 --- a/spec/editor-component-spec.coffee +++ b/spec/editor-component-spec.coffee @@ -630,6 +630,11 @@ describe "EditorComponent", -> expect(cursorRect.left).toBe rangeRect.left expect(cursorRect.width).toBe rangeRect.width + it "sets the cursor to the default character width at the end of a line", -> + editor.setCursorScreenPosition([0, Infinity]) + cursorNode = node.querySelector('.cursor') + expect(cursorNode.offsetWidth).toBe charWidth + it "gives the cursor a non-zero width even if it's inside atomic tokens", -> editor.setCursorScreenPosition([1, 0]) cursorNode = node.querySelector('.cursor') diff --git a/src/cursor-component.coffee b/src/cursor-component.coffee index 5fa7f10b2..a347d9565 100644 --- a/src/cursor-component.coffee +++ b/src/cursor-component.coffee @@ -6,10 +6,11 @@ CursorComponent = React.createClass displayName: 'CursorComponent' render: -> - {editor, screenRange, scrollTop, scrollLeft} = @props + {editor, screenRange, scrollTop, scrollLeft, defaultCharWidth} = @props {top, left, height, width} = editor.pixelRectForScreenRange(screenRange) top -= scrollTop left -= scrollLeft + width = defaultCharWidth if width is 0 WebkitTransform = "translate3d(#{left}px, #{top}px, 0px)" div className: 'cursor', style: {height, width, WebkitTransform} diff --git a/src/cursors-component.coffee b/src/cursors-component.coffee index 62c471c49..eb1cc14b6 100644 --- a/src/cursors-component.coffee +++ b/src/cursors-component.coffee @@ -12,7 +12,7 @@ CursorsComponent = React.createClass cursorBlinkIntervalHandle: null render: -> - {editor, cursorScreenRanges, scrollTop, scrollLeft} = @props + {editor, cursorScreenRanges, scrollTop, scrollLeft, defaultCharWidth} = @props {blinkOff} = @state className = 'cursors' @@ -21,7 +21,7 @@ CursorsComponent = React.createClass div {className}, if @isMounted() for key, screenRange of cursorScreenRanges - CursorComponent({key, editor, screenRange, scrollTop, scrollLeft}) + CursorComponent({key, editor, screenRange, scrollTop, scrollLeft, defaultCharWidth}) getInitialState: -> blinkOff: false