refactory-id / bootstrap-markdown

Bootstrap plugin for markdown editing
Apache License 2.0
1.99k stars 371 forks source link

Add keyboard events #44

Closed emagnier closed 10 years ago

emagnier commented 10 years ago

This helps to debug certain additional buttons and to add features like word count.

toopay commented 10 years ago

I like the idea of extending these events, but keydown and keypress will always proxiying event to keyup, which will makes the callback inconsistent. Thought?

emagnier commented 10 years ago

Yeah I see.. I'll re-take a look on it this weekend to see if I can make something better or not.

mrcasals commented 10 years ago

Hi! Any updates on this? I would like to add a word count to my bootstrap-markdown textareas and can't find a way to do it :(

acrobat commented 10 years ago

I think this will be possible if PR #61 is merged,the onchange event that is added triggers when a user is typing in the textarea

acrobat commented 10 years ago

Please take a look at PR #72, i think this pull request has the changes from this PR already included

mrcasals commented 10 years ago

I finally solved this with CoffeeScript code (sorry for not posting before, totally forgot it!). In my case, the user couldn't write more than maxwords (say 140).

updateCounter = (textarea, counter, max, button) ->
  words = textarea.val().trim().split(/\s/)
  empty_lines = words.filter (x) -> x == ""
  cnt = words.length
  cnt = cnt - empty_lines.length
  counter = $(counter)
  counter.text (max - cnt) + " Words left"
  if cnt > max
    counter.attr "class", "over-limit"
    button.toggleClass('inactive')
  else
    counter.attr "class", "on-limit"
    button.toggleClass('inactive')

window.wordCount = (textarea, counter, max, button) ->
  textarea = $(textarea)
  submit_button = $(button)

  textarea.on 'keyup blur', ->
    updateCounter(textarea, counter, max, submit_button)

  updateCounter(textarea, counter, max, submit_button)

counter is a span tag that shows the words you can still write.

Thanks!

emagnier commented 10 years ago

@acrobat: Thanks for your following up. I had rebased my branch, but indeed #72 seems to include my changes. So I'm closing my PR for now.