simple editing working
This commit is contained in:
@@ -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.
|
||||
#
|
||||
|
||||
@@ -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
|
||||
|
||||
#
|
||||
|
||||
@@ -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: ()->
|
||||
|
||||
Reference in New Issue
Block a user