Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate the delta in a cursor-aware manner. #381

Merged
merged 1 commit into from
May 29, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/core/editor.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
_ = require('lodash')
Delta = require('rich-text/lib/delta')
dom = require('../lib/dom')
Document = require('./document')
Line = require('./line')
Expand All @@ -18,6 +19,10 @@ class Editor
@length = @delta.length()
@selection = new Selection(@doc, @quill)
@timer = setInterval(_.bind(this.checkUpdate, this), @options.pollInterval)
@savedRange = null;
@quill.on("selection-change", (range) =>
@savedRange = range
)
this.enable() unless @options.readOnly

destroy: ->
Expand Down Expand Up @@ -168,11 +173,22 @@ class Editor
)

_trackDelta: (fn) ->
oldIndex = @savedRange?.start
fn()
newDelta = @doc.toDelta()
# TODO need to get this to prefer earlier insertions
delta = @delta.diff(newDelta)
return delta
@savedRange = @selection.getRange()
newIndex = @savedRange?.start
try
if oldIndex? and newIndex? and oldIndex <= @delta.length() and newIndex <= newDelta.length()
oldLeftDelta = @delta.slice(0, oldIndex)
oldRightDelta = @delta.slice(oldIndex)
newLeftDelta = newDelta.slice(0, newIndex)
newRightDelta = newDelta.slice(newIndex)
diffLeft = oldLeftDelta.diff(newLeftDelta)
diffRight = oldRightDelta.diff(newRightDelta)
return new Delta(diffLeft.ops.concat(diffRight.ops))
catch ignored
return @delta.diff(newDelta)

_update: ->
return false if @innerHTML == @root.innerHTML
Expand Down