 
   # https://www.w3.org/TR/input-events-2/#interface-InputEvent-Attributes
 
-  deleteByComposition: (event) ->
-
-  deleteByCut: (event) ->
-
-  deleteByDrag: (event) ->
+  # deleteByComposition: (event) ->
+  #
+  # deleteByCut: (event) ->
+  #
+  # deleteByDrag: (event) ->
 
   deleteCompositionText: (event) ->
     @composition = ""
 
-  deleteContent: (event) ->
+  # deleteContent: (event) ->
 
   deleteContentBackward: (event) ->
-    textDeleted = getTargetText(event)
-    {length} = textDeleted
+    {length} = getTargetText(event)
     @delegate?.inputControllerWillPerformTyping()
     @responder?.deleteInDirection("backward", {length})
-    {textDeleted}
 
-  deleteContentForward: (event) ->
-
-  deleteEntireSoftLine: (event) ->
-
-  deleteHardLineBackward: (event) ->
-
-  deleteHardLineForward: (event) ->
-
-  deleteSoftLineBackward: (event) ->
-
-  deleteSoftLineForward: (event) ->
+  # deleteContentForward: (event) ->
+  #
+  # deleteEntireSoftLine: (event) ->
+  #
+  # deleteHardLineBackward: (event) ->
+  #
+  # deleteHardLineForward: (event) ->
+  #
+  # deleteSoftLineBackward: (event) ->
+  #
+  # deleteSoftLineForward: (event) ->
 
   deleteWordBackward: (event) ->
-    textDeleted = getTargetText(event)
-    {length} = textDeleted
+    {length} = getTargetText(event)
     @delegate?.inputControllerWillPerformTyping()
     @responder?.deleteInDirection("backward", {length})
-    {textDeleted}
 
   deleteWordForward: (event) ->
-    textDeleted = getTargetText(event)
-    {length} = textDeleted
+    {length} = getTargetText(event)
     @delegate?.inputControllerWillPerformTyping()
     @responder?.deleteInDirection("forward", {length})
-    {textDeleted}
 
-  formatBackColor: (event) ->
+  # formatBackColor: (event) ->
 
   formatBold: (event) ->
     event.preventDefault()
-    @toggleAttribute("bold")
+    @delegate?.inputControllerWillPerformFormatting()
+    @responder?.toggleCurrentAttribute("bold")
 
-  formatFontColor: (event) ->
-
-  formatFontName: (event) ->
-
-  formatIndent: (event) ->
+  # formatFontColor: (event) ->
+  #
+  # formatFontName: (event) ->
+  #
+  # formatIndent: (event) ->
 
   formatItalic: (event) ->
     event.preventDefault()
-    @toggleAttribute("italic")
+    @delegate?.inputControllerWillPerformFormatting()
+    @responder?.toggleCurrentAttribute("italic")
 
-  formatJustifyCenter: (event) ->
-
-  formatJustifyFull: (event) ->
-
-  formatJustifyLeft: (event) ->
-
-  formatJustifyRight: (event) ->
-
-  formatOutdent: (event) ->
-
-  formatRemove: (event) ->
-
-  formatSetBlockTextDirection: (event) ->
-
-  formatSetInlineTextDirection: (event) ->
+  # formatJustifyCenter: (event) ->
+  #
+  # formatJustifyFull: (event) ->
+  #
+  # formatJustifyLeft: (event) ->
+  #
+  # formatJustifyRight: (event) ->
+  #
+  # formatOutdent: (event) ->
+  #
+  # formatRemove: (event) ->
+  #
+  # formatSetBlockTextDirection: (event) ->
+  #
+  # formatSetInlineTextDirection: (event) ->
 
   formatStrikeThrough: (event) ->
     event.preventDefault()
-    @toggleAttribute("strike")
+    @delegate?.inputControllerWillPerformFormatting()
+    @responder?.toggleCurrentAttribute("strike")
 
-  formatSubscript: (event) ->
-
-  formatSuperscript: (event) ->
-
-  formatUnderline: (event) ->
-
-  historyRedo: (event) ->
-
-  historyUndo: (event) ->
+  # formatSubscript: (event) ->
+  #
+  # formatSuperscript: (event) ->
+  #
+  # formatUnderline: (event) ->
+  #
+  # historyRedo: (event) ->
+  #
+  # historyUndo: (event) ->
 
   insertCompositionText: (event) ->
     @composition = event.data

{objectsAreEqual, compact, summarizeStringChange} = Trix

class Trix.Level2InputController extends Trix.AbstractInputController
  elementDidMutate: (mutationSummary) ->
    if @composition?
      @delegate?.inputControllerDidAllowUnhandledInput?()
    else
      super

  mutationIsExpected: (mutationSummary) ->
    result = objectsAreEqual(mutationSummary, @inputSummary)
    console.log("[mutation] [#{if result then "expected" else "unexpected"}] #{JSON.stringify({mutationSummary})}")
    delete @inputSummary
    result

  events:
    beforeinput: (event) ->
      @inputSummary = compact(@[event.inputType]?(event))
      console.group(event.inputType)
      console.log("[#{event.type}] #{JSON.stringify(event.data)} #{JSON.stringify({@inputSummary})}")

    input: (event) ->
      console.log("[#{event.type}] #{JSON.stringify(event.data)}")
      Promise.resolve().then(console.groupEnd)

    compositionend: (event) ->
      if @composition?

    delete @composition
    @insertReplacementText(event)

  # insertFromDrop: (event) ->
  #
  # insertFromPaste: (event) ->
  #
  # insertFromYank: (event) ->
  #
  # insertHorizontalRule: (event) ->

  insertLineBreak: (event) ->
    @delegate?.inputControllerWillPerformTyping()
    @responder?.insertString("\n")

  # insertLink: (event) ->
  #
  # insertOrderedList: (event) ->

  insertParagraph: (event) ->
    @delegate?.inputControllerWillPerformTyping()
    @responder?.insertLineBreak()

  insertReplacementText: (event) ->
    string = event.data ? event.dataTransfer.getData("text/plain")
    {length} = getTargetText(event)
    @delegate?.inputControllerWillPerformTyping()
    @responder?.expandSelectionInDirection("backward", {length})
    @responder?.insertString(string)

  insertText: (event) ->
    string = event.data
    @delegate?.inputControllerWillPerformTyping()
    @responder?.insertString(string)

  # insertTranspose: (event) ->
  #
  # insertUnorderedList: (event) ->

getTargetText = (event) ->
  [event.getTargetRanges()...]
