Commit d5b331b7 authored by Phil Hughes's avatar Phil Hughes

Improved design

Updated JS to remove undo manager - instead let the browser handle it all
parent 14d08d14
((w) -> ((w) ->
w.gl ?= {} w.gl ?= {}
w.gl.text ?= {} w.gl.text ?= {}
w.gl.text.undoManager ?= {}
gl.text.randomString = -> Math.random().toString(36).substring(7) gl.text.randomString = -> Math.random().toString(36).substring(7)
gl.text.replaceRange = (s, start, end, substitute) -> gl.text.replaceRange = (s, start, end, substitute) ->
s.substring(0, start) + substitute + s.substring(end); s.substring(0, start) + substitute + s.substring(end);
gl.text.wrap = (textArea, tag) -> gl.text.selectedText = (text, textarea) ->
$textArea = $(textArea) text.substring(textarea.selectionStart, textarea.selectionEnd)
oldVal = $textArea.val()
$textArea.focus()
textArea = $textArea.get(0)
selObj = window.getSelection()
selRange = selObj.getRangeAt(0)
text = $textArea.val()
replaceWith = @replaceRange(
text,
textArea.selectionStart,
textArea.selectionEnd,
(tag+selObj.toString()+tag))
$textArea.data('old-val', text).val(replaceWith)
gl.text.undoManager.addUndo(oldVal, $textArea.val())
gl.text.prepend = (textArea, tag) -> gl.text.insertText = (textArea, text, tag, selected, wrap) ->
$textArea = $(textArea) startChar = if not wrap and textArea.selectionStart > 0 then '\n' else ''
oldVal = $textArea.val() insertText = "#{startChar}#{tag}#{selected}#{if wrap then tag else ' '}"
$textArea.focus()
textArea = $textArea.get(0) if document.queryCommandSupported('insertText')
selObj = window.getSelection() document.execCommand 'insertText', false, insertText
selRange = selObj.getRangeAt(0)
text = $textArea.val()
if textArea.selectionStart > 0
lineBreak = '\n'
else else
lineBreak = '' try
document.execCommand("ms-beginUndoUnit")
replaceWith = @replaceRange( textArea.value = @replaceRange(
text, text,
textArea.selectionStart, textArea.selectionStart,
textArea.selectionEnd, textArea.selectionEnd,
("#{lineBreak}#{tag}#{selObj.toString()} \n") insertText)
) try
$textArea.data('old-val', text).val(replaceWith); document.execCommand("ms-endUndoUnit")
gl.text.undoManager.addUndo(oldVal, $textArea.val())
gl.text.undoManager.history = {} @moveCursor(textArea, tag, wrap)
gl.text.undoManager.undoHistory = {}
gl.text.undoManager.addUniqueIfNotExists = ($ta) -> gl.text.moveCursor = (textArea, tag, wrapped) ->
unique = $ta.attr('data-unique') return unless textArea.setSelectionRange
if not unique?
unique = gl.text.randomString()
$ta.attr('data-unique', unique)
gl.text.undoManager.history[unique] = []
gl.text.undoManager.undoHistory[unique] = []
unique
gl.text.undoManager.addUndo = (oldVal, newVal) -> if textArea.selectionStart is textArea.selectionEnd
$thisTextarea = $('textarea:focus') if wrapped
unique = gl.text.undoManager.addUniqueIfNotExists($thisTextarea) pos = textArea.selectionStart - tag.length
gl.text.undoManager.history[unique].push({ else
oldVal: oldVal, pos = textArea.selectionStart
newVal: newVal
}) textArea.setSelectionRange pos, pos
gl.text.undoManager.undo = () -> gl.text.updateText = (textArea, tag, wrap) ->
$thisTextarea = $('textarea:focus') $textArea = $(textArea)
unique = gl.text.undoManager.addUniqueIfNotExists($thisTextarea) oldVal = $textArea.val()
if not gl.text.undoManager.history[unique].length textArea = $textArea.get(0)
return text = $textArea.val()
latestChange = gl.text.undoManager.history[unique].pop() selected = @selectedText(text, textArea)
gl.text.undoManager.undoHistory[unique].push(latestChange) $textArea.focus()
$thisTextarea.val(latestChange.oldVal)
gl.text.undoManager.redo = () -> @insertText(textArea, text, tag, selected, wrap)
$thisTextarea = $('textarea:focus')
unique = gl.text.undoManager.addUniqueIfNotExists($thisTextarea)
if not gl.text.undoManager.undoHistory[unique].length
return
latestUndo = gl.text.undoManager.undoHistory[unique].pop()
gl.text.undoManager.history[unique].push(latestUndo)
$thisTextarea.val(latestUndo.newVal)
gl.text.addListeners = () -> gl.text.addListeners = ->
self = @ self = @
$('.js-md').on 'click', -> $('.js-md').on 'click', ->
$this = $(@) $this = $(@)
if $this.data('md-wrap')? self.updateText(
self.wrap( $this.closest('.md-area').find('textarea'),
$this.closest('.md-area').find('textarea'), $this.data('md-tag'),
$this.data('md-tag') not $this.data('md-prepend')
) )
else if $this.data('md-prepend')?
self.prepend(
$this.closest('.md-area').find('textarea'),
$this.data('md-tag')
)
else
self.wrap(
$this.closest('.md-area').find('textarea'),
$this.data('md-tag')
)
gl.text._previousState = null
$(window).on 'keydown', (e) =>
$thisTextarea = $('textarea:focus')
if e.ctrlKey or e.metaKey
if String.fromCharCode(e.which).toLowerCase() is 'z' and !e.shiftKey
e.preventDefault()
self.undoManager.undo()
else if ((String.fromCharCode(e.which).toLowerCase() is 'z' and e.shiftKey) or (String.fromCharCode(e.which).toLowerCase() is 'y'))
e.preventDefault()
self.undoManager.redo()
else if e.which is 13 or e.which is 8 # enter key or backspace key has been pressed
if gl.text._previousState?
gl.text.undoManager.addUndo(
gl.text._previousState,
$thisTextarea.val()
)
gl.text._previousState = $thisTextarea.val()
gl.text.removeListeners = () -> gl.text.removeListeners = ->
$('.js-md').off() $('.js-md').off()
) window ) window
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment