Add initial editor command super class

This can be extended by extensions targetted
towards acting on text inside the editor and
not contributing any UI
This commit is contained in:
Kevin Sawicki
2012-10-03 17:40:59 -07:00
parent b3ce062be9
commit 2d8be51e71
4 changed files with 205 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
module.exports =
class EditorCommand
@activate: (rootView) ->
keymaps = @getKeymaps()
return unless keymaps
window.keymap.bindKeys '.editor', keymaps
for editor in rootView.getEditors()
@subscribeToEditor(rootView, editor)
rootView.on 'editor-open', (e, editor) =>
@subscribeToEditor(rootView, editor)
@subscribeToEditor: (rootView, editor) ->
keymaps = @getKeymaps(rootView, editor)
return unless keymaps
for key, event of keymaps
editor.on event, => @execute(editor, event)
@alterSelection: (editor, transform) ->
selection = editor.getSelection()
return false if selection.isEmpty()
range = selection.getBufferRange()
reverse = selection.isReversed()
text = transform(editor.getTextInRange(range))
return false if text is null or text is undefined
editor.insertText(text)
selection.setBufferRange(range, {reverse})
true

View File

@@ -0,0 +1,11 @@
EditorCommand = require 'editor-command'
module.exports =
class LowerCaseCommand extends EditorCommand
@getKeymaps: (editor) ->
'meta-Y': 'lowercase'
@execute: (editor, event) ->
@alterSelection editor, (text) ->
text.toLowerCase()

View File

@@ -0,0 +1,11 @@
EditorCommand = require 'editor-command'
module.exports =
class UpperCaseCommand extends EditorCommand
@getKeymaps: (editor) ->
'meta-X': 'uppercase'
@execute: (editor, event) ->
@alterSelection editor, (text) ->
text.toUpperCase()