Skip to content

Commit 76a5b8d

Browse files
committed
fixed newline character insertion
1 parent 474516f commit 76a5b8d

File tree

5 files changed

+31
-34
lines changed

5 files changed

+31
-34
lines changed

server/src/main/java/pl/piotrowski/remotetexteditor/controller/DocumentsController.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,9 @@ public DocumentsController(DocumentsService documentsService) {
3434
@MessageMapping("/update/{name}")
3535
@SendTo("/topic/updates/{name}")
3636
public Update updateDocumentsContent(@DestinationVariable String name, @Payload Update update) {
37-
Document document;
38-
39-
4037

4138
try {
42-
document = documentsService.updateDocumentsContent(name, update);
39+
documentsService.updateDocumentsContent(name, update);
4340
} catch (DocumentNotFoundException e) {
4441
e.printStackTrace();
4542
}

server/src/main/java/pl/piotrowski/remotetexteditor/model/Document.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ public int hashCode() {
8181

8282
@Override
8383
public void applyUpdate(Update update) {
84-
84+
if (update.isAppending()){
85+
content = content+update.getContent();
86+
} else {
87+
String first = content.substring(0, update.getStart());
88+
String last = content.substring(update.getEnd());
89+
90+
content = first+update.getContent()+last;
91+
}
8592
}
8693
}

webclient/src/main/web/src/app/document/document.component.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
2-
import {el} from "@angular/platform-browser/testing/src/browser_util";
32
import {applyUpdate, Document} from "../model/document";
43
import {createUpdate, Update} from "../model/update";
54
import {DocumentService} from "../services/document.service";
@@ -15,7 +14,7 @@ export class DocumentComponent implements OnInit {
1514
currentDocument: Document;
1615
newDocument: Document;
1716
newName: string;
18-
updateSubject:WebSocketSubject<Update>;
17+
updateSubject: WebSocketSubject<Update>;
1918
private textArea: ElementRef;
2019
selectionStart: number;
2120
selectionEnd: number;
@@ -31,7 +30,7 @@ export class DocumentComponent implements OnInit {
3130
this.newName = "";
3231
}
3332

34-
updateCurrentDocument(update: Update){
33+
updateCurrentDocument(update: Update) {
3534
this.updateSubject.send(update)
3635
}
3736

@@ -40,7 +39,7 @@ export class DocumentComponent implements OnInit {
4039
this.documents = docs;
4140
if (docs.length > 0) {
4241
this.currentDocument = docs[0];
43-
this.currentDocument.previousContent=this.currentDocument.content;
42+
this.currentDocument.previousContent = this.currentDocument.content;
4443
this.documentSelected(this.currentDocument)
4544
}
4645
}
@@ -72,20 +71,20 @@ export class DocumentComponent implements OnInit {
7271
)
7372
}
7473

75-
documentSelected(document: Document){
76-
if (this.updateSubject){
74+
documentSelected(document: Document) {
75+
if (this.updateSubject) {
7776
this.updateSubject.complete()
7877
}
79-
this.documentService.getWsConnection(document.name).subscribe((ws:WebSocketSubject<Update>)=>{
78+
this.documentService.getWsConnection(document.name).subscribe((ws: WebSocketSubject<Update>) => {
8079
this.updateSubject = ws;
81-
this.updateSubject.subscribe((update: Update)=>{
82-
applyUpdate(document ,update)
80+
this.updateSubject.subscribe((update: Update) => {
81+
applyUpdate(document, update)
8382
})
8483
});
8584
}
8685

8786
isRenamingDisabled() {
88-
return this.newName.length===0;
87+
return this.newName.length === 0;
8988
}
9089

9190
@ViewChild("textArea")
@@ -96,28 +95,28 @@ export class DocumentComponent implements OnInit {
9695

9796
textChanged(event, document: Document) {
9897

99-
let update:Update;
98+
let update: Update;
10099

101-
if(event.inputType==="deleteContentBackward"){
102-
if(this.selectionStart===this.selectionEnd){
103-
update = createUpdate("",this.selectionStart-1, this.selectionEnd, false)
100+
if (event.inputType === "deleteContentBackward") {
101+
if (this.selectionStart === this.selectionEnd) {
102+
update = createUpdate("", this.selectionStart - 1, this.selectionEnd, false)
104103
} else {
105-
update = createUpdate("",this.selectionStart, this.selectionEnd, false)
104+
update = createUpdate("", this.selectionStart, this.selectionEnd, false)
106105
}
107-
} else if(event.inputType==="deleteContentForward"){
108-
update = createUpdate("",this.selectionStart, this.selectionEnd+1, false)
106+
} else if (event.inputType === "deleteContentForward") {
107+
update = createUpdate("", this.selectionStart, this.selectionEnd + 1, false)
108+
} else if (event.inputType==="insertLineBreak") {
109+
update = createUpdate("\n",this.selectionStart,this.selectionEnd,false)
109110
} else {
110-
const appending: boolean =this.selectionStart===this.selectionEnd && document.content.length===this.selectionEnd+1;
111-
update = createUpdate(document.content.substring(this.selectionStart, this.selectionEnd+1), this.selectionStart, this.selectionEnd, appending)
111+
const appending: boolean = this.selectionStart === this.selectionEnd && document.content.length === this.selectionEnd + 1;
112+
update = createUpdate(document.content.substring(this.selectionStart, this.selectionEnd + 1), this.selectionStart, this.selectionEnd, appending)
112113
}
113114

114115
this.updateCurrentDocument(update)
115116
}
116117

117118
updateSelection() {
118-
this.selectionStart=this.textArea.nativeElement.selectionStart;
119-
this.selectionEnd=this.textArea.nativeElement.selectionEnd;
120-
console.log(this.selectionStart);
121-
console.log(this.selectionEnd);
119+
this.selectionStart = this.textArea.nativeElement.selectionStart;
120+
this.selectionEnd = this.textArea.nativeElement.selectionEnd;
122121
}
123122
}

webclient/src/main/web/src/app/model/document.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,12 @@ export class Document {
1515

1616

1717
export function applyUpdate(document: Document ,update: Update){
18-
console.log(update.content);
19-
console.log(document.previousContent);
2018
if(update.appending){
2119
document.previousContent = document.previousContent+update.content
2220
} else {
2321
const first = document.previousContent.substring(0, update.start);
2422
const last = document.previousContent.substring(update.end);
2523

26-
console.log("first:"+first);
27-
console.log("last:"+last);
28-
2924
document.previousContent = first+update.content+ last
3025
}
3126
if(document.content !== document.previousContent){

webclient/src/main/web/src/app/services/webSocketSubject.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export class WebSocketSubject<T>{
3636
const observable = Rx.Observable.create((obs) => {
3737
stomp.connect({}, (frame) => {
3838
stomp.subscribe(this.webConfig.topicUrl + "/" + name, (response) => {
39-
console.log(response.body);
4039
obs.next(JSON.parse(response.body))
4140
});
4241
});

0 commit comments

Comments
 (0)