Include ShareJS CM adapter in compiled lib and fix bug with CM adapter

Because the CM & Ace scripts are naively concat-ed together, the Ace
applyToShareJS function would be overwritten by the CM version.

Also fixes bugs where adapter was calling old version of ShareJS api
and the old CM api
This commit is contained in:
Alasdair Smith 2018-02-02 17:24:27 +00:00
parent f5b6d3ff3e
commit 09fbafa1f6
4 changed files with 12 additions and 10 deletions

View file

@ -108,6 +108,7 @@ module.exports = (grunt) ->
"public/coffee/ide/editor/sharejs/vendor/client/microevent.coffee"
"public/coffee/ide/editor/sharejs/vendor/client/doc.coffee"
"public/coffee/ide/editor/sharejs/vendor/client/ace.coffee"
"public/coffee/ide/editor/sharejs/vendor/client/cm.coffee"
]
client:

View file

@ -21,7 +21,8 @@ SHAREJS_COFFEE_FILES := \
public/coffee/ide/editor/sharejs/vendor/types/text-api.coffee \
public/coffee/ide/editor/sharejs/vendor/client/microevent.coffee \
public/coffee/ide/editor/sharejs/vendor/client/doc.coffee \
public/coffee/ide/editor/sharejs/vendor/client/ace.coffee
public/coffee/ide/editor/sharejs/vendor/client/ace.coffee \
public/coffee/ide/editor/sharejs/vendor/client/cm.coffee
LESS_FILES := $(shell find public/stylesheets -name '*.less')
CSS_FILES := public/stylesheets/style.css public/stylesheets/ol-style.css

View file

@ -3,7 +3,7 @@
Range = ace.require("ace/range").Range
# Convert an ace delta into an op understood by share.js
applyToShareJS = (editorDoc, delta, doc, fromUndo) ->
applyAceToShareJS = (editorDoc, delta, doc, fromUndo) ->
# Get the start position of the range, in no. of characters
getStartOffsetPosition = (start) ->
# This is quite inefficient - getLines makes a copy of the entire
@ -81,7 +81,7 @@ window.sharejs.extendDoc 'attach_ace', (editor, keepEditorContents, maxDocLength
fromUndo = !!(editor.getSession().$fromUndo or editor.getSession().$fromReject)
applyToShareJS editorDoc, change, doc, fromUndo
applyAceToShareJS editorDoc, change, doc, fromUndo
check()

View file

@ -3,7 +3,7 @@
# It is heavily inspired from the Ace editor hook.
# Convert a CodeMirror delta into an op understood by share.js
applyToShareJS = (editorDoc, delta, doc) ->
applyCMToShareJS = (editorDoc, delta, doc) ->
# CodeMirror deltas give a text replacement.
# I tuned this operation a little bit, for speed.
startPos = 0 # Get character position from # of chars in each line.
@ -26,7 +26,7 @@ applyToShareJS = (editorDoc, delta, doc) ->
doc.del startPos, delLen
doc.insert startPos, delta.text.join '\n' if delta.text
applyToShareJS editorDoc, delta.next, doc if delta.next
applyCMToShareJS editorDoc, delta.next, doc if delta.next
# Attach a CodeMirror editor to the document. The editor's contents are replaced
# with the document's contents unless keepEditorContents is true. (In which case
@ -39,14 +39,14 @@ window.sharejs.extendDoc 'attach_cm', (editor, keepEditorContents) ->
check = ->
window.setTimeout ->
editorText = editor.getValue()
otText = sharedoc.getValue()
otText = sharedoc.getText()
if editorText != otText
console.error "Text does not match!"
console.error "editor: #{editorText}"
console.error "ot: #{otText}"
# Replace the editor text with the doc snapshot.
editor.setValue sharedoc.getValue()
editor.setValue sharedoc.getText()
, 0
if keepEditorContents
@ -64,11 +64,11 @@ window.sharejs.extendDoc 'attach_cm', (editor, keepEditorContents) ->
# Listen for edits in CodeMirror.
editorListener = (ed, change) ->
return if suppress
applyToShareJS editor, change, sharedoc
applyCMToShareJS editor, change, sharedoc
check()
editor.setOption 'onChange', editorListener
editor.on 'change', editorListener
@on 'insert', (pos, text) ->
suppress = true
@ -87,7 +87,7 @@ window.sharejs.extendDoc 'attach_cm', (editor, keepEditorContents) ->
@detach_cm = ->
# TODO: can we remove the insert and delete event callbacks?
editor.setOption 'onChange', null
editor.off 'onChange', editorListener
delete @detach_cm
return