Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 130 lines (102 sloc) 2.967 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
$ = @jQuery or require('jquery')

class Wysiwyg
  className: 'wysiwyg'

  events:
    'click [data-type=bold]': 'bold'
    'click [data-type=italic]': 'italic'
    'click [data-type=list]': 'list'
    'click [data-type=link]': 'link'
    'click [data-type=h2]': 'h2'
    'click [data-type=h3]': 'h3'
    'click a': 'cancel'

  document: document

  constructor: (@options = {}) ->
    @el = $('<div />')

    for key, value of @options
      @[key] = value

    @el.addClass(@className)
    @delegateEvents(@events)
    @render()

  render: ->
    @el.empty()
    @el.append('<a href="#" data-type="bold">Bold</a>')
    @el.append('<a href="#" data-type="italic">Italic</a>')
    @el.append('<a href="#" data-type="list">List</a>')
    @el.append('<a href="#" data-type="link">Link</a>')
    @el.append('<a href="#" data-type="h2">Large</a>')
    @el.append('<a href="#" data-type="h3">Medium</a>')
    this

  bold: (e) ->
    e.preventDefault()
    return unless @selectTest()
    @exec 'bold'

  italic: (e) ->
    e.preventDefault()
    return unless @selectTest()
    @exec 'italic'

  list: (e) ->
    e.preventDefault()
    @exec 'insertUnorderedList'

  link: (e) ->
    e.preventDefault()
    return unless @selectTest()

    href = 'http://'
    parentElement = window.getSelection().focusNode.parentElement
    
    if parentElement?.href
      href = parentElement.href

    @exec 'unlink'
    href = prompt('Enter a link:', href)
    return if not href or href is 'http://'
    href = 'http://' + href unless (/:\/\//).test(href)
    @exec 'createLink', href

  h2: (e) ->
    e.preventDefault()
    if @query('formatBlock') is 'h2'
      @exec 'formatBlock', 'p'
    else
      @exec 'formatBlock', 'h2'

  h3: (e) ->
    e.preventDefault()
    if @query('formatBlock') is 'h3'
      @exec 'formatBlock', 'p'
    else
      @exec 'formatBlock', 'h3'

  move: (position) ->
    @el.css(position)

  # Private

  cancel: (e) ->
    e.preventDefault()
    e.stopImmediatePropagation()

  getSelectedText: ->
    if @document?.selection
      document.selection.createRange().text
    else if @document
      document.getSelection().toString()

  selectTest: ->
    if @getSelectedText().length is 0
      alert 'Select some text first.'
      return false
    true

  exec: (type, arg = null) ->
    @document.execCommand(type, false, arg)

  query: (type) ->
    @document.queryCommandValue(type)

  delegateEvents: (events) ->
    for key, method of events

      unless typeof(method) is 'function'
        # Always return true from event handlers
        method = do (method) => =>
          @[method].apply(this, arguments)
          true

      match = key.match(/^(\S+)\s*(.*)$/)
      eventName = match[1]
      selector = match[2]

      if selector is ''
        @el.bind(eventName, method)
      else
        @el.delegate(selector, eventName, method)

# Expose library
if module?
  module.exports = Wysiwyg
else
  @Wysiwyg = Wysiwyg
Something went wrong with that request. Please try again.