-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreview-field.html
93 lines (91 loc) · 2.47 KB
/
preview-field.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<link rel="import" href="../polymer/polymer-element.html">
<dom-module id="preview-field">
<template>
<style>
:host {
display: block;
--link-color: #14acd5;
}
#field {
@apply --field-class;
white-space: pre-line;
}
.link {
@apply --link-class;
color: var(--link-color);
text-decoration: none;
}
</style>
<span id="field">
</span>
</template>
<script>
/**
* `preview-field`
*
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class PreviewField extends Polymer.Element {
static get is() { return 'preview-field'; }
static get properties() {
return {
text: {
type: String,
observer: 'parseText',
},
metions: Boolean,
hashtags: Boolean,
};
}
/**
* Parsing text to be preview and instert in the field span
*
* @param {newVal} The text to preview
*/
parseText(newVal) {
let text = this.urlify(newVal);
if(this.metions){
text = this.mentionsfy(text);
}
if(this.hashtags){
text = this.hashtagsfy(text);
}
this.$.field.innerHTML = text;
}
/**
* Replace the links for tags href
*
* @param {text} text that replace links
* @return {String} The new text with the links replaced
*/
urlify(text) {
const urlRegex = /(https?:\/\/[^\s]+)/g;
return text.replace(urlRegex, '<a class="link" target="'+this.targetUrl+'" href="$1">$1</a>');
}
/**
* Replace the @mentions for tags href
*
* @param {text} text that replace @mentions
* @return {String} The new text with the @mentions replaced
*/
mentionsfy(text) {
const urlRegex = /(@[^,\s]+)/g;
return text.replace(urlRegex, '<a class="link"target="' + this.targetMentions +'" href="$1">$1</a>');
}
/**
* Replace the #hasthags for tags href
*
* @param {text} text that replace @hasthags
* @return {String} The new text with the #hasthags replaced
*/
hashtagsfy(text) {
const urlRegex = /(#[^,\s]+)/g;
return text.replace(urlRegex, '<a class="link"target="' + this.targetHashtag+'" href="$1">$1</a>');
}
}
window.customElements.define(PreviewField.is, PreviewField);
</script>
</dom-module>