Created
June 27, 2020 13:16
-
-
Save mdmoin7/1075208e18a7834cbab102154eb35b23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, VERSION, OnInit } from '@angular/core'; | |
import { FormControl } from '@angular/forms'; | |
import { debounceTime, distinctUntilChanged } from "rxjs/operators"; | |
import { Clipboard } from "@angular/cdk/clipboard"; | |
@Component({ | |
selector: 'my-app', | |
templateUrl: './app.component.html', | |
styleUrls: [ './app.component.css' ] | |
}) | |
export class AppComponent implements OnInit { | |
templateText:string='Hello John Doe'; | |
editorData:FormControl=new FormControl('Hello %user_name%'); | |
fieldsAvailable=[ | |
{key:'user_name',example:'John Doe'}, | |
{key:'user_age',example:'20 years'}, | |
{key:'user_email',example:'[email protected]'}, | |
{key:'app_name',example:'My App'}, | |
{key:'app_url',example:'https://google.com'} | |
] | |
constructor(private clipboard: Clipboard){} | |
ngOnInit(){ | |
this.editorData.valueChanges | |
.pipe(debounceTime(1500), distinctUntilChanged()) | |
.subscribe((v) => this.updateText()); | |
} | |
updateText() { | |
if (this.editorData.value !== null) { | |
this.templateText = this.editorData.value.replace( | |
/%(\w+)%/g, // this is the regex to replace %variable% | |
(match, field) => { | |
const ex = this.fieldsAvailable.find( | |
(f) => f.key === field | |
); | |
if (ex) { | |
return ex.example; | |
} | |
return match; | |
} | |
); | |
this.templateText.trim(); | |
} | |
} | |
copyField(field) { | |
this.clipboard.copy(`%${field.key}%`); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment