Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Andi Braimllari committed May 4, 2024
1 parent c03d880 commit 224fd34
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/app/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ export class HomeComponent implements AfterViewInit, OnDestroy {
}
const text: string = $event.clipboardData.getData('text/plain');

// TODO: is there new code that properly does here what we want?
document.execCommand('insertText', false, text);
}

Expand All @@ -205,7 +206,9 @@ export class HomeComponent implements AfterViewInit, OnDestroy {
onTextCopy($event: ClipboardEvent): void {
$event.preventDefault();

const editorCopiableText: string = this.fetchEditorCopiableText();
const editorCopiableText: string = window.getSelection()!.toString().replace(/(?:\n){2,}/g, (match) => {
return match.substring(0, match.length - 1);
});

// NOTE: this text/plain might be fine for now, but probably should be amended if we want to have richer text
$event.clipboardData!.setData('text/plain', editorCopiableText);
Expand All @@ -216,12 +219,21 @@ export class HomeComponent implements AfterViewInit, OnDestroy {
* @param {ClipboardEvent} $event the event emitted
*/
onTextCut($event: ClipboardEvent): void {
$event.preventDefault();
if (window.getSelection()!.toString() === document.getElementById(this.EDITOR_KEY)!.innerText) {
$event.preventDefault();

const editorCopiableText: string = this.fetchEditorCopiableText();
const editorCopiableText: string = Array.from(document
.getElementById(this.EDITOR_KEY)!
.querySelectorAll('p'))
.map((p: HTMLParagraphElement) => p.textContent)
.join('\n');

// NOTE: this text/plain might be fine for now, but probably should be amended if we want to have richer text
$event.clipboardData!.setData('text/plain', editorCopiableText);
// NOTE: this text/plain might be fine for now, but probably should be amended if we want to have richer text
$event.clipboardData!.setData('text/plain', editorCopiableText);

// TODO: try to do this for partial cut as well
this.clearEditor();
}
}

onKeyDown($event: KeyboardEvent): void {
Expand Down Expand Up @@ -414,7 +426,11 @@ export class HomeComponent implements AfterViewInit, OnDestroy {
return;
}

const editorCopiableText: string = this.fetchEditorCopiableText();
const editorCopiableText: string = Array.from(document
.getElementById(this.EDITOR_KEY)!
.querySelectorAll('p'))
.map((p: HTMLParagraphElement) => p.textContent)
.join('\n');
navigator.clipboard.writeText(editorCopiableText).then();
} else {
// TODO some browsers still seem to use this deprecated method, keep it around for some more time
Expand Down Expand Up @@ -1486,13 +1502,4 @@ export class HomeComponent implements AfterViewInit, OnDestroy {
private fetchEditorMarkings(): NodeListOf<HTMLSpanElement> {
return document.querySelectorAll('#editor > p > span');
}

private fetchEditorCopiableText(): string {
const paragraphs: NodeListOf<HTMLParagraphElement> = document
.getElementById(this.EDITOR_KEY)!
.querySelectorAll('p');
return Array.from(paragraphs)
.map((p: HTMLParagraphElement) => p.textContent)
.join('\n');
}
}

0 comments on commit 224fd34

Please sign in to comment.