Move EditorCommand helpers elsewhere

RootView and Editor now have helpers that support
binding events to callbacks, binding a callback to
all current and future editors, and replacing the
selected text via a transforming callback.
This commit is contained in:
Kevin Sawicki
2012-10-10 18:41:12 -07:00
parent 1fa32c48e7
commit d4aeb1bb95
10 changed files with 162 additions and 176 deletions

View File

@@ -965,3 +965,20 @@ class Editor extends View
@find('pre.line.cursor-line').removeClass('cursor-line')
if @getSelection().isSingleScreenLine()
@find("pre.line:eq(#{screenRow})").addClass('cursor-line')
bindToKeyedEvent: (key, event, callback) ->
binding = {}
binding[key] = event
window.keymap.bindKeys '.editor', binding
@on event, =>
callback(this, event)
replaceSelectedText: (replaceFn) ->
selection = @getSelection()
return false if selection.isEmpty()
text = replaceFn(@getTextInRange(selection.getBufferRange()))
return false if text is null or text is undefined
@insertText(text, select: true)
true

View File

@@ -232,3 +232,10 @@ class RootView extends View
saveAll: ->
editor.save() for editor in @getEditors()
eachEditor: (callback) ->
for editor in @getEditors()
callback(editor)
@on 'editor-open', (e, editor) ->
callback(editor)

View File

@@ -1,26 +0,0 @@
module.exports =
class EditorCommand
@activate: (rootView) ->
for editor in rootView.getEditors()
@onEditor(editor)
rootView.on 'editor-open', (e, editor) =>
@onEditor(editor)
@register: (editor, key, event, callback) ->
binding = {}
binding[key] = event
window.keymap.bindKeys '.editor', binding
editor.on event, =>
callback(editor, event)
@replaceSelectedText: (editor, replace) ->
selection = editor.getSelection()
return false if selection.isEmpty()
text = replace(editor.getTextInRange(selection.getBufferRange()))
return false if text is null or text is undefined
editor.insertText(text, select: true)
true

View File

@@ -1,9 +1,10 @@
EditorCommand = require 'editor-command'
module.exports =
class LowerCaseCommand extends EditorCommand
class LowerCaseCommand
@activate: (rootView) ->
rootView.eachEditor(@onEditor)
@onEditor: (editor) ->
@register editor, 'meta-Y', 'lowercase', =>
@replaceSelectedText editor, (text) ->
editor.bindToKeyedEvent 'meta-Y', 'lowercase', =>
editor.replaceSelectedText (text) ->
text.toLowerCase()

View File

@@ -1,9 +1,10 @@
EditorCommand = require 'editor-command'
module.exports =
class UpperCaseCommand extends EditorCommand
class UpperCaseCommand
@activate: (rootView) ->
rootView.eachEditor(@onEditor)
@onEditor: (editor) ->
@register editor, 'meta-X', 'uppercase', =>
@replaceSelectedText editor, (text) ->
editor.bindToKeyedEvent 'meta-X', 'uppercase', =>
editor.replaceSelectedText (text) ->
text.toUpperCase()