Merge pull request #3606 from atom/bo-model-placeholder-text

Model EditorView::setPlaceholderText
This commit is contained in:
Ben Ogle
2014-09-23 11:20:23 -07:00
5 changed files with 52 additions and 13 deletions

View File

@@ -2225,7 +2225,7 @@ describe "EditorComponent", ->
describe "when placholderText is specified", ->
it "renders the placeholder text when the buffer is empty", ->
component.setProps(placeholderText: 'Hello World')
editor.setPlaceholderText('Hello World')
expect(componentNode.querySelector('.placeholder-text')).toBeNull()
editor.setText('')
nextAnimationFrame()

View File

@@ -3665,3 +3665,21 @@ describe "Editor", ->
editor.selectPageUp()
expect(editor.getScrollTop()).toBe 0
expect(editor.getSelectedBufferRanges()).toEqual [[[0,0], [12,2]]]
describe '.get/setPlaceholderText()', ->
it 'can be created with placeholderText', ->
TextBuffer = require 'text-buffer'
newEditor = new Editor
buffer: new TextBuffer
mini: true
placeholderText: 'yep'
expect(newEditor.getPlaceholderText()).toBe 'yep'
it 'models placeholderText and emits an event when changed', ->
editor.onDidChangePlaceholderText handler = jasmine.createSpy()
expect(editor.getPlaceholderText()).toBeUndefined()
editor.setPlaceholderText('OK')
expect(handler).toHaveBeenCalledWith 'OK'
expect(editor.getPlaceholderText()).toBe 'OK'

View File

@@ -64,7 +64,7 @@ EditorComponent = React.createClass
decorations = editor.decorationsForScreenRowRange(renderedStartRow, renderedEndRow)
highlightDecorations = @getHighlightDecorations(decorations)
lineDecorations = @getLineDecorations(decorations)
placeholderText = @props.placeholderText if @props.placeholderText? and editor.isEmpty()
placeholderText = editor.getPlaceholderText() if editor.isEmpty()
visible = @isVisible()
scrollHeight = editor.getScrollHeight()
@@ -358,6 +358,7 @@ EditorComponent = React.createClass
@subscribe editor.observeDecorations(@onDecorationAdded)
@subscribe editor.onDidRemoveDecoration(@onDecorationRemoved)
@subscribe editor.onDidChangeCharacterWidths(@onCharacterWidthsChanged)
@subscribe editor.onDidChangePlaceholderText(@onPlaceholderTextChanged)
@subscribe editor.$scrollTop.changes, @onScrollTopChanged
@subscribe editor.$scrollLeft.changes, @requestUpdate
@subscribe editor.$verticalScrollbarWidth.changes, @requestUpdate
@@ -780,6 +781,9 @@ EditorComponent = React.createClass
onCharacterWidthsChanged: (@scopedCharacterWidthsChangeCount) ->
@requestUpdate()
onPlaceholderTextChanged: ->
@requestUpdate()
handleDragUntilMouseUp: (event, dragHandler) ->
{editor} = @props
dragging = false

View File

@@ -85,13 +85,13 @@ class EditorView extends View
{@editor, mini, placeholderText} = editorOrParams
props ?= {}
props.mini = mini
props.placeholderText = placeholderText
@editor ?= new Editor
buffer: new TextBuffer
softWrapped: false
tabLength: 2
softTabs: true
mini: mini
placeholderText: placeholderText
props = defaults({@editor, parentView: this}, props)
@component = React.renderComponent(EditorComponent(props), @element)
@@ -346,16 +346,9 @@ class EditorView extends View
redraw: ->
deprecate('Please remove from your code. ::redraw no longer does anything')
# Public: Set the text to appear in the editor when it is empty.
#
# This only affects mini editors.
#
# * `placeholderText` A {String} of text to display when empty.
setPlaceholderText: (placeholderText) ->
if @component?
@component.setProps({placeholderText})
else
@props.placeholderText = placeholderText
deprecate('Use TextEditor::setPlaceholderText instead. eg. editorView.getModel().setPlaceholderText(text)')
@getModel().setPlaceholderText(placeholderText)
lineElementForScreenRow: (screenRow) ->
$(@component.lineNodeForScreenRow(screenRow))

View File

@@ -77,7 +77,7 @@ class Editor extends Model
'$verticalScrollbarWidth', '$horizontalScrollbarHeight', '$scrollTop', '$scrollLeft',
'manageScrollPosition', toProperty: 'displayBuffer'
constructor: ({@softTabs, initialLine, initialColumn, tabLength, softWrapped, @displayBuffer, buffer, registerEditor, suppressCursorCreation, @mini}) ->
constructor: ({@softTabs, initialLine, initialColumn, tabLength, softWrapped, @displayBuffer, buffer, registerEditor, suppressCursorCreation, @mini, @placeholderText}) ->
super
@emitter = new Emitter
@@ -405,6 +405,15 @@ class Editor extends Model
onDidRemoveDecoration: (callback) ->
@displayBuffer.onDidRemoveDecoration(callback)
# Extended: Calls your `callback` when the placeholder text is changed.
#
# * `callback` {Function}
# * `placeholderText` {String} new text
#
# Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidChangePlaceholderText: (callback) ->
@emitter.on 'did-change-placeholder-text', callback
onDidChangeCharacterWidths: (callback) ->
@displayBuffer.onDidChangeCharacterWidths(callback)
@@ -2670,6 +2679,21 @@ class Editor extends Model
Section: Editor Rendering
###
# Public: Retrieves the greyed out placeholder of a mini editor.
#
# Returns a {String}.
getPlaceholderText: ->
@placeholderText
# Public: Set the greyed out placeholder of a mini editor. Placeholder text
# will be displayed when the editor has no content.
#
# * `placeholderText` {String} text that is displayed when the editor has no content.
setPlaceholderText: (placeholderText) ->
return if @placeholderText is placeholderText
@placeholderText = placeholderText
@emitter.emit 'did-change-placeholder-text', @placeholderText
# Extended: Retrieves the number of the row that is visible and currently at the
# top of the editor.
#