11import { Component , ElementRef , OnInit , ViewChild } from '@angular/core' ;
2- import { applyUpdate , Document } from "../model/document" ;
2+ import { Document } from "../model/document" ;
33import { createUpdate , Update } from "../model/update" ;
44import { DocumentService } from "../services/document.service" ;
55import { WebSocketSubject } from "../services/webSocketSubject" ;
@@ -47,6 +47,8 @@ export class DocumentComponent implements OnInit {
4747 this . currentDocument = docs [ 0 ] ;
4848 this . currentDocument . previousContent = this . currentDocument . content ;
4949 this . documentSelected ( this . currentDocument )
50+ } else {
51+ this . currentDocument = null ;
5052 }
5153 }
5254 )
@@ -84,7 +86,7 @@ export class DocumentComponent implements OnInit {
8486 this . documentService . getWsConnection ( document . name ) . subscribe ( ( ws : WebSocketSubject < Update > ) => {
8587 this . updateSubject = ws ;
8688 this . updateSubject . subscribe ( ( update : Update ) => {
87- applyUpdate ( document , update )
89+ this . applyUpdate ( document , update ) ;
8890 } )
8991 } ) ;
9092 }
@@ -105,8 +107,8 @@ export class DocumentComponent implements OnInit {
105107 }
106108 } else if ( event . inputType === "deleteContentForward" ) {
107109 update = createUpdate ( "" , this . selectionStart , this . selectionEnd + 1 , false )
108- } else if ( event . inputType === "insertLineBreak" ) {
109- update = createUpdate ( "\n" , this . selectionStart , this . selectionEnd , false )
110+ } else if ( event . inputType === "insertLineBreak" ) {
111+ update = createUpdate ( "\n" , this . selectionStart , this . selectionEnd , false )
110112 } else {
111113 const appending : boolean = this . selectionStart === this . selectionEnd && document . content . length === this . selectionEnd + 1 ;
112114 update = createUpdate ( document . content . substring ( this . selectionStart , this . selectionEnd + 1 ) , this . selectionStart , this . selectionEnd , appending )
@@ -119,4 +121,22 @@ export class DocumentComponent implements OnInit {
119121 this . selectionStart = this . textArea . nativeElement . selectionStart ;
120122 this . selectionEnd = this . textArea . nativeElement . selectionEnd ;
121123 }
124+
125+ applyUpdate ( document : Document , update : Update ) {
126+ const start = this . textArea . nativeElement . selectionStart ;
127+ const end = this . textArea . nativeElement . selectionEnd ;
128+ const oldContent : String = document . content ;
129+
130+ if ( update . appending ) {
131+ document . previousContent = document . previousContent + update . content
132+ } else {
133+ const first = document . previousContent . substring ( 0 , update . start ) ;
134+ const last = document . previousContent . substring ( update . end ) ;
135+
136+ document . previousContent = first + update . content + last
137+ }
138+ if ( document . content !== document . previousContent ) {
139+ document . content = document . previousContent ;
140+ }
141+ }
122142}
0 commit comments