From 7783e18a504b703d23ab1faadd1d9172debebd6d Mon Sep 17 00:00:00 2001 From: Alasdair Smith Date: Fri, 2 Feb 2018 17:31:47 +0000 Subject: [PATCH] Allow Documents to be attached to CM --- .../public/coffee/ide/editor/Document.coffee | 48 ++++++++++++------- .../coffee/ide/editor/ShareJsDoc.coffee | 3 ++ 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/services/web/public/coffee/ide/editor/Document.coffee b/services/web/public/coffee/ide/editor/Document.coffee index b6ea177fc8..afdd924588 100644 --- a/services/web/public/coffee/ide/editor/Document.coffee +++ b/services/web/public/coffee/ide/editor/Document.coffee @@ -26,7 +26,8 @@ define [ @connected = @ide.socket.socket.connected @joined = false @wantToBeJoined = false - @_checkConsistency = _.bind(@_checkConsistency, @) + @_checkAceConsistency = _.bind(@_checkConsistency, @, @ace) + @_checkCMConsistency = _.bind(@_checkConsistency, @, @cm) @inconsistentCount = 0 @_bindToEditorEvents() @_bindToSocketEvents() @@ -34,32 +35,43 @@ define [ attachToAce: (@ace) -> @doc?.attachToAce(@ace) editorDoc = @ace.getSession().getDocument() - editorDoc.on "change", @_checkConsistency + editorDoc.on "change", @_checkAceConsistency @ide.$scope.$emit 'document:opened', @doc detachFromAce: () -> @doc?.detachFromAce() editorDoc = @ace?.getSession().getDocument() - editorDoc?.off "change", @_checkConsistency + editorDoc?.off "change", @_checkAceConsistency @ide.$scope.$emit 'document:closed', @doc - + + attachToCM: (@cm) -> + @doc?.attachToCM(@cm) + @cm?.on "change", @_checkCMConsistency + @ide.$scope.$emit 'document:opened', @doc + + detachFromCM: () -> + @doc?.detachFromCM() + @cm?.off "change", @_checkCMConsistency + @ide.$scope.$emit 'document:closed', @doc + submitOp: (args...) -> @doc?.submitOp(args...) - _checkConsistency: () -> - # We've been seeing a lot of errors when I think there shouldn't be - # any, which may be related to this check happening before the change is - # applied. If we use a timeout, hopefully we can reduce this. - setTimeout () => - editorValue = @ace?.getValue() - sharejsValue = @doc?.getSnapshot() - if editorValue != sharejsValue - @inconsistentCount++ - else - @inconsistentCount = 0 + _checkConsistency: (editor) -> + return () => + # We've been seeing a lot of errors when I think there shouldn't be + # any, which may be related to this check happening before the change is + # applied. If we use a timeout, hopefully we can reduce this. + setTimeout () => + editorValue = editor?.getValue() + sharejsValue = @doc?.getSnapshot() + if editorValue != sharejsValue + @inconsistentCount++ + else + @inconsistentCount = 0 - if @inconsistentCount >= 3 - @_onError new Error("Editor text does not match server text") - , 0 + if @inconsistentCount >= 3 + @_onError new Error("Editor text does not match server text") + , 0 getSnapshot: () -> @doc?.getSnapshot() diff --git a/services/web/public/coffee/ide/editor/ShareJsDoc.coffee b/services/web/public/coffee/ide/editor/ShareJsDoc.coffee index 569a6c7a9f..63fca95856 100644 --- a/services/web/public/coffee/ide/editor/ShareJsDoc.coffee +++ b/services/web/public/coffee/ide/editor/ShareJsDoc.coffee @@ -120,6 +120,9 @@ define [ attachToAce: (ace) -> @_doc.attach_ace(ace, false, window.maxDocLength) detachFromAce: () -> @_doc.detach_ace?() + attachToCM: (cm) -> @_doc.attach_cm(cm, false) + detachFromCM: () -> @_doc.detach_cm?() + INFLIGHT_OP_TIMEOUT: 5000 # Retry sending ops after 5 seconds without an ack WAIT_FOR_CONNECTION_TIMEOUT: 500 # If we're waiting for the project to join, try again in 0.5 seconds _startInflightOpTimeout: (update) ->