2018-10-28 06:02:57 +01:00
|
|
|
import { Component, OnInit, Input, EventEmitter, Output, Renderer2, ViewChild, ElementRef } from '@angular/core';
|
2018-10-28 05:45:32 +01:00
|
|
|
import { forEach } from '@angular/router/src/utils/collection';
|
2018-10-28 04:58:00 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-databinded-text',
|
|
|
|
templateUrl: './databinded-text.component.html',
|
|
|
|
styleUrls: ['./databinded-text.component.scss']
|
|
|
|
})
|
|
|
|
export class DatabindedTextComponent implements OnInit {
|
|
|
|
private accounts: string[] = [];
|
2018-10-28 05:45:32 +01:00
|
|
|
private hashtags: string[] = [];
|
2018-10-28 06:25:13 +01:00
|
|
|
// private links: string[] = [];
|
2018-10-28 04:58:00 +01:00
|
|
|
|
|
|
|
processedText: string;
|
|
|
|
|
2018-10-28 06:02:57 +01:00
|
|
|
@ViewChild('content') contentElement: ElementRef;
|
|
|
|
|
2018-10-28 04:58:00 +01:00
|
|
|
@Output() accountSelected = new EventEmitter<string>();
|
|
|
|
@Output() hashtagSelected = new EventEmitter<string>();
|
|
|
|
@Output() textSelected = new EventEmitter();
|
|
|
|
|
|
|
|
@Input('text')
|
2018-10-28 05:45:32 +01:00
|
|
|
set text(value: string) {
|
2018-10-28 06:02:57 +01:00
|
|
|
this.processedText = '';
|
2018-10-28 05:45:32 +01:00
|
|
|
|
2018-10-28 06:02:57 +01:00
|
|
|
let linksSections = value.split('<a ');
|
2018-10-28 05:45:32 +01:00
|
|
|
|
|
|
|
for (let section of linksSections) {
|
|
|
|
if (!section.includes('href')) {
|
|
|
|
this.processedText += section;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (section.includes('class="mention hashtag"')) {
|
|
|
|
let extractedLinkAndNext = section.split('</a>');
|
|
|
|
let extractedHashtag = extractedLinkAndNext[0].split('#')[1].replace('<span>', '').replace('</span>', '');
|
|
|
|
|
|
|
|
this.processedText += ` <a href class="${extractedHashtag}">#${extractedHashtag}</a>`;
|
2018-10-28 06:02:57 +01:00
|
|
|
if (extractedLinkAndNext[1]) this.processedText += extractedLinkAndNext[1];
|
2018-10-28 05:45:32 +01:00
|
|
|
this.hashtags.push(extractedHashtag);
|
|
|
|
|
|
|
|
} else if (section.includes('class="u-url mention"')) {
|
|
|
|
let extractedAccountAndNext = section.split('</a></span>');
|
|
|
|
|
|
|
|
let extractedAccountName = extractedAccountAndNext[0].split('@<span>')[1].replace('<span>', '').replace('</span>', '');
|
|
|
|
let extractedAccountLink = extractedAccountAndNext[0].split('" class="u-url mention"')[0].replace('href="https://', '').replace(' ', '').replace('@', '').split('/');
|
|
|
|
let extractedAccount = `@${extractedAccountLink[1]}@${extractedAccountLink[0]}`;
|
|
|
|
|
2018-10-28 06:25:13 +01:00
|
|
|
let classname = this.getClassName(extractedAccount);
|
2018-10-28 06:02:57 +01:00
|
|
|
this.processedText += ` <a href class="${classname}" title="${extractedAccount}">@${extractedAccountName}</a>`;
|
2018-10-28 05:45:32 +01:00
|
|
|
|
2018-10-28 06:02:57 +01:00
|
|
|
if (extractedAccountAndNext[1]) this.processedText += extractedAccountAndNext[1];
|
2018-10-28 05:45:32 +01:00
|
|
|
this.accounts.push(extractedAccount);
|
|
|
|
} else {
|
2018-10-28 06:25:13 +01:00
|
|
|
this.processedText += `<a class="link" ${section}`;
|
2018-10-28 05:45:32 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-28 04:58:00 +01:00
|
|
|
}
|
|
|
|
|
2018-10-28 05:45:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
constructor(private renderer: Renderer2) { }
|
2018-10-28 04:58:00 +01:00
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
}
|
|
|
|
|
2018-10-28 05:45:32 +01:00
|
|
|
ngAfterViewInit() {
|
2018-10-28 06:02:57 +01:00
|
|
|
for (const hashtag of this.hashtags) {
|
|
|
|
let el = this.contentElement.nativeElement.querySelector(`.${hashtag}`);
|
|
|
|
|
2018-10-28 06:25:13 +01:00
|
|
|
this.renderer.listen(el, 'click', (event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
|
2018-10-28 06:02:57 +01:00
|
|
|
this.selectHashtag(hashtag);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const account of this.accounts) {
|
2018-10-28 06:25:13 +01:00
|
|
|
let classname = this.getClassName(account);
|
2018-10-28 06:02:57 +01:00
|
|
|
let el = this.contentElement.nativeElement.querySelector(`.${classname}`);
|
|
|
|
|
2018-10-28 06:25:13 +01:00
|
|
|
this.renderer.listen(el, 'click', (event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
|
2018-10-28 06:02:57 +01:00
|
|
|
this.selectAccount(account);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-28 06:25:13 +01:00
|
|
|
// let allLinksEl = this.contentElement.nativeElement.querySelectorAll(`.link`);
|
|
|
|
// for (const link of allLinksEl) {
|
|
|
|
// this.renderer.listen(link, 'click', (event) => {
|
|
|
|
// //event.preventDefault();
|
|
|
|
// event.stopImmediatePropagation();
|
2018-10-28 05:45:32 +01:00
|
|
|
// return false;
|
|
|
|
// });
|
2018-10-28 06:25:13 +01:00
|
|
|
// }
|
2018-10-28 05:45:32 +01:00
|
|
|
}
|
|
|
|
|
2018-10-28 06:25:13 +01:00
|
|
|
private getClassName(value: string): string {
|
|
|
|
let res = value;
|
|
|
|
while(res.includes('.')) res = res.replace('.', '-');
|
|
|
|
while(res.includes('@')) res = res.replace('@', '-');
|
|
|
|
return res;
|
2018-10-28 06:02:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private selectAccount(account: string) {
|
|
|
|
console.warn(`select ${account}`);
|
2018-10-28 05:45:32 +01:00
|
|
|
this.accountSelected.next(account);
|
|
|
|
}
|
|
|
|
|
2018-10-28 06:02:57 +01:00
|
|
|
private selectHashtag(hashtag: string) {
|
|
|
|
console.warn(`select ${hashtag}`);
|
2018-10-28 05:45:32 +01:00
|
|
|
this.hashtagSelected.next(hashtag);
|
|
|
|
}
|
|
|
|
|
2018-10-28 06:33:53 +01:00
|
|
|
selectText() {
|
2018-10-28 06:25:13 +01:00
|
|
|
console.warn(`selectText`);
|
2018-10-28 05:45:32 +01:00
|
|
|
this.textSelected.next();
|
|
|
|
}
|
|
|
|
|
2018-10-28 04:58:00 +01:00
|
|
|
}
|