Skip to content

Commit

Permalink
Changed textarea code to use the new text API
Browse files Browse the repository at this point in the history
  • Loading branch information
josephg committed Aug 14, 2011
1 parent 407b6ac commit e74e3e2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 36 deletions.
43 changes: 28 additions & 15 deletions src/client/textarea.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,60 @@
# always localised, the diffing is quite easy.
#
# This algorithm is O(N), but I suspect you could speed it up somehow using regular expressions.
opFromDiff = (oldval, newval) ->
return [] if oldval == newval
applyChange = (doc, oldval, newval) ->
return if oldval == newval
commonStart = 0
commonStart++ while oldval.charAt(commonStart) == newval.charAt(commonStart)

commonEnd = 0
commonEnd++ while oldval.charAt(oldval.length - 1 - commonEnd) == newval.charAt(newval.length - 1 - commonEnd) and
commonEnd + commonStart < oldval.length and commonEnd + commonStart < newval.length

window.sharejs.types.text.normalize [{p:commonStart, d:oldval[commonStart ... oldval.length - commonEnd]},
{p:commonStart, i:newval[commonStart ... newval.length - commonEnd]}]
doc.del oldval.length - commonStart - commonEnd, commonStart unless oldval.length == commonStart + commonEnd
doc.insert newval[commonStart ... newval.length - commonEnd], commonStart unless newval.length == commonStart + commonEnd

window.sharejs.Document::attach_textarea = (elem) ->
doc = this
elem.value = @snapshot
prevvalue = elem.value

@on 'remoteop', (op) ->
replaceText = (newText, transformCursor) ->
newSelection = [
doc.type.transformCursor elem.selectionStart, op, true
doc.type.transformCursor elem.selectionEnd, op, true
transformCursor elem.selectionStart
transformCursor elem.selectionEnd
]
scrollTop = elem.scrollTop

elem.value = doc.snapshot

scrollTop = elem.scrollTop
elem.value = newText
elem.scrollTop = scrollTop if elem.scrollTop != scrollTop
[elem.selectionStart, elem.selectionEnd] = newSelection

genOp = (event) ->
#console.log event
@on 'insert', (text, pos) ->
transformCursor = (cursor) ->
if pos <= cursor
cursor + text.length
else
cursor

replaceText elem.value[...pos] + text + elem.value[pos..], transformCursor

@on 'delete', (text, pos) ->
transformCursor = (cursor) ->
if pos < cursor
cursor - Math.min(text.length, cursor - pos)
else
cursor

replaceText elem.value[...pos] + elem.value[pos + text.length..], transformCursor

genOp = (event) ->
onNextTick = (fn) -> setTimeout fn, 0
onNextTick ->
#console.log doc.snapshot, elem.value
if elem.value != prevvalue
# IE constantly replaces unix newlines with \r\n. ShareJS docs
# should only have unix newlines.
prevvalue = elem.value
op = opFromDiff doc.snapshot, elem.value.replace /\r\n/g, '\n'
doc.submitOp op unless op.length == 0
applyChange doc, doc.getText(), elem.value.replace /\r\n/g, '\n'

for event in ['textInput', 'keydown', 'keyup', 'select', 'cut', 'paste']
if elem.addEventListener
Expand Down
57 changes: 36 additions & 21 deletions webclient/textarea.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e74e3e2

Please sign in to comment.