Skip to content

Commit d5407ad

Browse files
committed
testing insert()
1 parent f32653d commit d5407ad

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"@angular/platform-browser-dynamic": "^10.2.0",
3131
"@angular/router": "^10.2.0",
3232
"font-awesome": "^4.7.0",
33-
"insert-text-at-cursor": "^0.3.0",
3433
"rxjs": "~6.5.4",
3534
"tslib": "^2.0.0",
3635
"zone.js": "~0.10.2"

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Component, OnInit, ViewChild } from "@angular/core";
2-
import { AngularEditorConfig } from "angular-editor";
32
import { FormBuilder, FormGroup, Validators } from "@angular/forms";
3+
import { AngularEditorConfig } from "angular-editor";
44
import { AngularEditorComponent } from "projects/angular-editor/src/public-api";
5-
import insertTextAtCursor from "insert-text-at-cursor";
65
@Component({
76
selector: "app-root",
87
templateUrl: "./app.component.html",
@@ -98,10 +97,12 @@ export class AppComponent implements OnInit {
9897
onChange2(event) {
9998
console.warn(this.form.value);
10099
}
100+
cnt = 0;
101101
insertAtCursor() {
102102
console.log(`insertAtCursors`);
103-
this.editorRef.focus();
104-
setTimeout(()=> this.editorRef.insert("dsfd"),2000);
103+
//this.editorRef.focus();
104+
this.editorRef.insert(`-${this.cnt}-`);
105+
this.cnt++;
105106
//this.editorRef.insert("dsfd");
106107
//
107108
}

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

+20-4
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ export class AngularEditorComponent
157157
*/
158158
public onTextAreaMouseOut(event: MouseEvent): void {
159159
this.editorService.saveSelection();
160+
this.savedSelection = this.editorService.savedSelection;
160161
}
161162

162163
/**
@@ -394,7 +395,6 @@ export class AngularEditorComponent
394395
}
395396
this.editorToolbar.triggerBlocks(els);
396397
}
397-
398398
private configure() {
399399
this.editorService.uploadUrl = this.config.uploadUrl;
400400
this.editorService.uploadWithCredentials = this.config.uploadWithCredentials;
@@ -432,10 +432,26 @@ export class AngularEditorComponent
432432
return tags.join(",");
433433
}
434434
insert(value: string) {
435-
436-
this.editorService.insertHtml("<p>helleo</p>");
435+
this.focus();
436+
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;
442+
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);
437453
}
438-
454+
savedSelection;
439455
ngOnDestroy() {
440456
if (this.blurInstance) {
441457
this.blurInstance();

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

-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ export class AngularEditorService {
8383
*/
8484
insertHtml(html: string): void {
8585
const isHTMLInserted = this.doc.execCommand("insertHTML", false, html);
86-
8786
if (!isHTMLInserted) {
8887
throw new Error("Unable to perform the operation");
8988
}

0 commit comments

Comments
 (0)