simple editing working

This commit is contained in:
Kevin Jahns
2014-08-15 23:55:38 +02:00
parent eb541fd473
commit 2a10fe45fa
45 changed files with 1180 additions and 159 deletions

View File

@@ -112,6 +112,8 @@ createIwcConnector = (callback, initial_user_id)->
setTimeout init, 5000
undefined
module.exports = createIwcConnector
if window?
if not window.Y?

View File

@@ -20,37 +20,47 @@ createPeerJsConnector = (callback)->
constructor: (@engine, @HB, @execution_listener, @yatta)->
@peer = peer
@connections = []
@connections = {}
@peer.on 'connection', (conn)=>
conn.send "hey"
conn.send "hey" # is never send. But without it it won't work either..
@addConnection conn
send_ = (o)=>
@send o
@execution_listener.push send_
connectToPeer: (id)->
@addConnection peer.connect id
if not @connections[id]? and id isnt @yatta.getUserId()
@addConnection peer.connect id
getAllConnectionIds: ()->
for conn_id of @connections
conn_id
addConnection: (conn)->
@connections.push conn
@connections[conn.peer] = conn
conn.on 'data', (data)=>
if data is "hey"
console.log "Yatta: Connection received with init message (debug)" # I can remove this hey stuff when this happens.
else if data.HB?
@engine.applyOpsCheckDouble data.HB
else if data.op?
@engine.applyOp data.op
else if data.conns?
console.log "conns received"
for conn_id in data.conns
@connectToPeer conn_id
else
throw new Error "Can't parse this operation"
sendHB = ()=>
conn.send
HB: @yatta.getHistoryBuffer()._encode()
conn.send
conns: @getAllConnectionIds()
setTimeout sendHB, 1000
#
@@ -59,7 +69,7 @@ createPeerJsConnector = (callback)->
#
send: (o)->
if o.uid.creator is @HB.getUserId() and (typeof o.uid.op_number isnt "string")
for conn in @connections
for conn_id,conn of @connections
conn.send
op: o

View File

@@ -42,7 +42,7 @@ module.exports = (HB)->
# TODO: Do something with timeouts. You don't want this to fire for every operation (e.g. insert).
#
callEvent: (event, args)->
if @event_listeners[event]?
if @event_listeners?[event]?
for f in @event_listeners[event]
f.call @, event, args
@@ -52,6 +52,12 @@ module.exports = (HB)->
setParent: (o)->
@parent = o
#
# Get the parent of this operation.
#
getParent: ()->
@parent
#
# Computes a unique identifier (uid) that identifies this operation.
#
@@ -158,7 +164,6 @@ module.exports = (HB)->
if @validateSavedOperations()
@deletes.applyDelete @
super
@
else
false
@@ -205,6 +210,9 @@ module.exports = (HB)->
applyDelete: (o)->
@deleted_by ?= []
@deleted_by.push o
if @parent? and @deleted_by.length is 1
# call iff wasn't deleted earlyer
@parent.callEvent "delete", @
#
# If isDeleted() is true this operation won't be maintained in the sl
@@ -307,9 +315,24 @@ module.exports = (HB)->
@next_cl = @prev_cl.next_cl
@prev_cl.next_cl = @
@next_cl.prev_cl = @
parent = @prev_cl?.getParent()
if parent?
@setParent parent
@parent.callEvent "insert", @
super # notify the execution_listeners
#
# Compute the position of this operation.
#
getPosition: ()->
position = 0
prev = @prev_cl
while true
if prev instanceof Delimiter
break
position++
prev = prev.prev_cl
position
#
# Defines an object that is cannot be changed. You can use this to set an immutable string, or a number.
#

View File

@@ -122,6 +122,13 @@ module.exports = (HB)->
@end.execute()
super uid, prev, next, origin
execute: ()->
if @validateSavedOperations()
@beginning.setParent @
@end.setParent @
super
else
false
# Get the element previous to the delemiter at the end
getLastOperation: ()->
@@ -154,7 +161,9 @@ module.exports = (HB)->
if position is 0
break
if o instanceof types.Delimiter
throw new Error "position parameter exceeded the length of the document!"
console.log "position parameter exceeded the length of the document!"
o = null
break
o
#

View File

@@ -97,14 +97,18 @@ module.exports = (HB)->
deleteText: (position, length)->
o = @getOperationByPosition position
delete_ops = []
for i in [0...length]
d = HB.addOperation(new TextDelete undefined, o).execute()
o = o.next_cl
while o.isDeleted()
while o.isDeleted() and not (o instanceof types.Delimiter)
if o instanceof types.Delimiter
throw new Error "You can't delete more than there is.."
o = o.next_cl
d._encode()
delete_ops.push d._encode()
if o instanceof types.Delimiter
break
#
# Replace the content of this word with another one. Concurrent replacements are not merged!
@@ -141,6 +145,82 @@ module.exports = (HB)->
@validateSavedOperations
#
# Bind this Word to a textfield.
# TODO:
# insert_pressing+mouse new position
# concurrent pressing (two user pressing stuff)
bind: (textfield)->
word = @
textfield.value = @val()
position_start = null
document_length = null
@on "insert", (event, op)->
if op.creator isnt HB.getUserId()
o_pos = op.getPosition()
fix = (cursor)->
if cursor <= o_pos
cursor
else
cursor += 1
cursor
left = fix textfield.selectionStart
right = fix textfield.selectionEnd
if position_start?
document_length += 1
position_start = fix position_start
textfield.value = word.val()
textfield.setSelectionRange left, right
@on "delete", (event, op)->
if op.creator isnt HB.getUserId()
o_pos = op.getPosition()
fix = (cursor)->
if cursor <= o_pos
cursor
else
cursor -= 1
cursor
left = fix textfield.selectionStart
right = fix textfield.selectionEnd
if position_start?
document_length -= 1
position_start = fix position_start
textfield.value = word.val()
textfield.setSelectionRange left, right
update_yatta = ()->
if position_start?
document_length_diff = textfield.value.length - document_length
current_position = Math.min textfield.selectionStart, textfield.selectionEnd
if document_length_diff < 0 # deletion
deletion_position = Math.min current_position, position_start
word.deleteText deletion_position, Math.abs document_length_diff
else if document_length_diff > 0 # insertion
text_insert = textfield.value.substring position_start, (position_start + document_length_diff)
word.insertText position_start, text_insert
position_start = null
document_length = null
textfield.onkeydown = (event)->
#console.log "down"
if position_start?
update_yatta()
selection_range = Math.abs(textfield.selectionEnd - textfield.selectionStart)
position_start = Math.min(textfield.selectionStart, textfield.selectionEnd)
word.deleteText position_start, selection_range
document_length = textfield.value.length - selection_range
textfield.onkeyup = (event)->
#console.log "up"
update_yatta()
#
# Encode this operation in such a way that it can be parsed by remote peers.
#
_encode: ()->