From 0c2c739741077c0d359dfd993f4a79e8c463f121 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Mon, 25 Nov 2013 14:46:54 -0800 Subject: [PATCH] Add ability for placeholder text in mini editor --- spec/editor-view-spec.coffee | 26 +++++++++++++++++++++++++- src/editor-view.coffee | 26 ++++++++++++++++++++++++-- static/editor.less | 5 +++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/spec/editor-view-spec.coffee b/spec/editor-view-spec.coffee index 34445a661..467136dbe 100644 --- a/spec/editor-view-spec.coffee +++ b/spec/editor-view-spec.coffee @@ -1934,7 +1934,6 @@ describe "EditorView", -> miniEditor.setText(" and indented line") expect(miniEditor.renderedLines.find('.indent-guide').length).toBe 0 - it "lets you set the grammar", -> miniEditor = new EditorView(mini: true) miniEditor.setText("var something") @@ -1946,6 +1945,31 @@ describe "EditorView", -> # doesn't allow regular editors to set grammars expect(-> editorView.setGrammar()).toThrow() + describe "placeholderText", -> + it "is hidden and shown when appropriate", -> + miniEditor = new EditorView(mini: true, placeholderText: 'octokitten') + miniEditor.attachToDom() + + expect(miniEditor.underlayer.find('.placeholder-text')).toExist() + + miniEditor.setText("var something") + expect(miniEditor.underlayer.find('.placeholder-text')).not.toExist() + + miniEditor.setText("") + expect(miniEditor.underlayer.find('.placeholder-text')).toExist() + + it "can be set", -> + miniEditor = new EditorView(mini: true) + miniEditor.attachToDom() + + expect(miniEditor.find('.placeholder-text').text()).toEqual '' + + miniEditor.setPlaceholderText 'octokitten' + expect(miniEditor.find('.placeholder-text').text()).toEqual 'octokitten' + + miniEditor.setPlaceholderText 'new one' + expect(miniEditor.find('.placeholder-text').text()).toEqual 'new one' + describe "when the editor.showLineNumbers config is false", -> it "doesn't render any line numbers", -> expect(editorView.gutter.lineNumbers).toBeVisible() diff --git a/src/editor-view.coffee b/src/editor-view.coffee index c3e85cdf3..9bb805b59 100644 --- a/src/editor-view.coffee +++ b/src/editor-view.coffee @@ -1,4 +1,4 @@ -{View, $, $$} = require './space-pen-extensions' +{View, $, $$$} = require './space-pen-extensions' TextBuffer = require './text-buffer' Gutter = require './gutter' {Point, Range} = require 'telepath' @@ -86,7 +86,7 @@ class EditorView extends View if editorOrOptions instanceof Editor editor = editorOrOptions else - {editor, editSession, @mini} = editorOrOptions ? {} + {editor, editSession, @mini, placeholderText} = editorOrOptions ? {} editor ?= editSession # TODO: Remove this line after packages have updated their api to use Editor and EditorView @id = EditorView.nextEditorId++ @@ -101,6 +101,8 @@ class EditorView extends View @newCursors = [] @newSelections = [] + @setPlaceholderText(placeholderText) if placeholderText + if editor? @edit(editor) else if @mini @@ -587,6 +589,14 @@ class EditorView extends View @showIndentGuide = showIndentGuide @resetDisplay() + setPlaceholderText: (placeholderText) -> + return unless @mini + @placeholderText = placeholderText + @requestDisplayUpdate() + + getPlaceholderText: -> + @placeholderText + # Checkout the HEAD revision of this editor's file. checkoutHead: -> if path = @getPath() @@ -1210,6 +1220,7 @@ class EditorView extends View return @updateRenderedLines() + @updatePlaceholderText() @highlightCursorLine() @updateCursorViews() @updateSelectionViews() @@ -1269,6 +1280,17 @@ class EditorView extends View selectionView.highlight() selectionView.clearAutoscroll() + updatePlaceholderText: -> + return unless @mini + if (not @placeholderText) or @getText() + @find('.placeholder-text').remove() + else if @placeholderText and not @getText() + element = @find('.placeholder-text') + if element.length + element.text(@placeholderText) + else + @underlayer.append($('', class: 'placeholder-text', text: @placeholderText)) + updateRenderedLines: -> firstVisibleScreenRow = @getFirstVisibleScreenRow() lastScreenRowToRender = firstVisibleScreenRow + @heightInLines - 1 diff --git a/static/editor.less b/static/editor.less index 40e66a392..a7204eb70 100644 --- a/static/editor.less +++ b/static/editor.less @@ -1,3 +1,4 @@ +@import "ui-variables"; @import "octicon-utf-codes"; @import "octicon-mixins"; @@ -173,4 +174,8 @@ .scroll-view { overflow: hidden; } + + .placeholder-text { + color: @text-color-subtle; + } }