Skip to content

Commit 6b8cfd2

Browse files
committed
add inserttextAtCursor
1 parent d5407ad commit 6b8cfd2

File tree

4 files changed

+11
-28
lines changed

4 files changed

+11
-28
lines changed

projects/angular-editor-app/src/app/app.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ <h1>Angular Editor</h1>
1919
<p>
2020
Form Status: {{ form.status }}
2121
</p>
22-
<a (click)="insertAtCursor()">Insert at cursor</a>
22+
<a (click)="insertTextAtCursor()">Insert at cursor</a>
2323
</div>
2424

projects/angular-editor-app/src/app/app.component.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,9 @@ export class AppComponent implements OnInit {
9898
console.warn(this.form.value);
9999
}
100100
cnt = 0;
101-
insertAtCursor() {
102-
console.log(`insertAtCursors`);
103-
//this.editorRef.focus();
104-
this.editorRef.insert(`-${this.cnt}-`);
101+
insertTextAtCursor() {
102+
this.editorRef.insertTextAtCursor(`{${this.cnt}}`);
105103
this.cnt++;
106-
//this.editorRef.insert("dsfd");
107-
//
108104
}
109105
onTextAreaMouseOut(event) {
110106
console.log(`onTextAreaMouseOut`);

projects/angular-editor/src/lib/angular-editor.component.ts

+8-20
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class AngularEditorComponent
5252

5353
focusInstance: any;
5454
blurInstance: any;
55-
55+
currentCursorSelection: Range;
5656
@Input() id = "";
5757
@Input() config: AngularEditorConfig = angularEditorConfig;
5858
@Input() placeholder = "";
@@ -157,7 +157,7 @@ export class AngularEditorComponent
157157
*/
158158
public onTextAreaMouseOut(event: MouseEvent): void {
159159
this.editorService.saveSelection();
160-
this.savedSelection = this.editorService.savedSelection;
160+
this.currentCursorSelection = this.editorService.savedSelection;
161161
}
162162

163163
/**
@@ -431,27 +431,15 @@ export class AngularEditorComponent
431431
});
432432
return tags.join(",");
433433
}
434-
insert(value: string) {
435-
this.focus();
434+
insertTextAtCursor(text: string) {
436435
let selection = window.getSelection();
437-
//let range = document.createRange();
438-
//range.setEndAfter(this.selectiedNode);
439-
//selection.addRange(range);
440-
//selection.setPosition(this.textArea.nativeElement, this.selectiedIndex);
441-
// this.selectiedIndex += value.length;
442436
selection.removeAllRanges();
443-
selection.addRange(this.savedSelection);
444-
this.editorService.insertHtml(value);
445-
selection.setPosition(this.savedSelection.baseNode, 5);
446-
// selection.removeAllRanges();
447-
// let range = document.createRange();
448-
// selection.setPosition(
449-
// this.textArea.nativeElement,
450-
// this.savedSelection.baseOffet
451-
// );
452-
// this.savedSelection = selection.getRangeAt(0);
437+
selection.addRange(this.currentCursorSelection);
438+
this.editorService.insertHtml(text);
439+
this.editorService.saveSelection();
440+
this.currentCursorSelection = this.editorService.savedSelection;
453441
}
454-
savedSelection;
442+
455443
ngOnDestroy() {
456444
if (this.blurInstance) {
457445
this.blurInstance();

projects/angular-editor/src/lib/angular-editor.service.ts

-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ export class AngularEditorService {
9292
* save selection when the editor is focussed out
9393
*/
9494
public saveSelection = (): void => {
95-
console.log(this.doc.getSelection());
9695
if (this.doc.getSelection) {
9796
const sel = this.doc.getSelection();
9897
if (sel.getRangeAt && sel.rangeCount) {

0 commit comments

Comments
 (0)