Emit will-insert-text event when pasting

This commit is contained in:
Max Brunsfeld 2015-07-06 18:29:44 -07:00
parent ec2033c599
commit 8c3400c084
2 changed files with 33 additions and 19 deletions

View File

@ -2925,6 +2925,17 @@ describe "TextEditor", ->
expect(editor.lineTextForBufferRow(0)).toBe "var first = function () {"
expect(editor.lineTextForBufferRow(1)).toBe " var first = function(items) {"
it "notifies ::onWillInsertText observers", ->
insertedStrings = []
editor.onWillInsertText ({text, cancel}) ->
insertedStrings.push(text)
cancel()
atom.clipboard.write("hello")
editor.pasteText()
expect(insertedStrings).toEqual ["hello"]
describe "when `autoIndentOnPaste` is true", ->
beforeEach ->
atom.config.set("editor.autoIndentOnPaste", true)

View File

@ -772,31 +772,24 @@ class TextEditor extends Model
# Returns a {Range} when the text has been inserted
# Returns a {Boolean} false when the text has not been inserted
insertText: (text, options={}) ->
willInsert = true
cancel = -> willInsert = false
willInsertEvent = {cancel, text}
@emit('will-insert-text', willInsertEvent) if includeDeprecatedAPIs
@emitter.emit 'will-insert-text', willInsertEvent
return false unless @emitWillInsertTextEvent(text)
groupingInterval = if options.groupUndo
atom.config.get('editor.undoGroupingInterval')
else
0
if willInsert
options.autoIndentNewline ?= @shouldAutoIndent()
options.autoDecreaseIndent ?= @shouldAutoIndent()
@mutateSelectedText(
(selection) =>
range = selection.insertText(text, options)
didInsertEvent = {text, range}
@emit('did-insert-text', didInsertEvent) if includeDeprecatedAPIs
@emitter.emit 'did-insert-text', didInsertEvent
range
, groupingInterval
)
else
false
options.autoIndentNewline ?= @shouldAutoIndent()
options.autoDecreaseIndent ?= @shouldAutoIndent()
@mutateSelectedText(
(selection) =>
range = selection.insertText(text, options)
didInsertEvent = {text, range}
@emit('did-insert-text', didInsertEvent) if includeDeprecatedAPIs
@emitter.emit 'did-insert-text', didInsertEvent
range
, groupingInterval
)
# Essential: For each selection, replace the selected text with a newline.
insertNewline: ->
@ -2602,6 +2595,8 @@ class TextEditor extends Model
# * `options` (optional) See {Selection::insertText}.
pasteText: (options={}) ->
{text: clipboardText, metadata} = atom.clipboard.readWithMetadata()
return false unless @emitWillInsertTextEvent(clipboardText)
metadata ?= {}
options.autoIndent = @shouldAutoIndentOnPaste()
@ -2977,6 +2972,14 @@ class TextEditor extends Model
logScreenLines: (start, end) -> @displayBuffer.logLines(start, end)
emitWillInsertTextEvent: (text) ->
result = true
cancel = -> result = false
willInsertEvent = {cancel, text}
@emit('will-insert-text', willInsertEvent) if includeDeprecatedAPIs
@emitter.emit 'will-insert-text', willInsertEvent
result
if includeDeprecatedAPIs
TextEditor.delegatesProperties '$lineHeightInPixels', '$defaultCharWidth', '$height', '$width',
'$verticalScrollbarWidth', '$horizontalScrollbarHeight', '$scrollTop', '$scrollLeft',