1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
6 |
|
7 | var vue = _interopDefault(require('vue'));
|
8 |
|
9 | var originalContent = undefined;
|
10 | var textColorDefault = '#fff';
|
11 | var bgColorDefault = '#009688';
|
12 | var paddingDefault = '0px 5px';
|
13 |
|
14 | function selectPattern(word, liveHighlighting) {
|
15 | if ( liveHighlighting === void 0 ) liveHighlighting = false;
|
16 |
|
17 | var result = "";
|
18 | if (!liveHighlighting) {
|
19 | result = "\\b(" + word + ")\\b";
|
20 | } else {
|
21 | result = "(" + word + ")";
|
22 | }
|
23 | return result;
|
24 | }
|
25 |
|
26 | function testTextColor(color) {
|
27 | if ( color === void 0 ) color = textColorDefault;
|
28 |
|
29 | var result = textColorDefault;
|
30 | if (testColor(color)) {
|
31 | result = color;
|
32 | }
|
33 | return result;
|
34 | }
|
35 |
|
36 | function testBgColor(color) {
|
37 | if ( color === void 0 ) color = bgColorDefault;
|
38 |
|
39 | var result = bgColorDefault;
|
40 | if (testColor(color)) {
|
41 | result = color;
|
42 | }
|
43 | return result;
|
44 | }
|
45 |
|
46 | function testColor(color) {
|
47 | var result = false;
|
48 | var isAColor = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(color);
|
49 | var isAString = /^[a-zA-Z]+$/.test(color);
|
50 | if (isAColor || isAString) {
|
51 | result = true;
|
52 | }
|
53 | return result;
|
54 | }
|
55 |
|
56 | function testPadding(padding) {
|
57 | var result = paddingDefault;
|
58 | var isAValidValue = /^(\d+(cm|mm|in|px|pt|pc|em|ex|ch|rem|vw|vh|vmin|vmax|%)\s?){1,4}$/i.test(padding);
|
59 | if (isAValidValue) {
|
60 | result = padding;
|
61 | }
|
62 | return result;
|
63 | }
|
64 |
|
65 | function highlight(content, word, patternSelected, color, bgColor, padding) {
|
66 | var spanStart = "<span style='padding:" + padding + "; background-color:" + bgColor + "; color:" + color + ";'>";
|
67 | var spanEnd = "</span>";
|
68 | var result = content;
|
69 | if (word != "") {
|
70 | var regex = new RegExp(patternSelected, "g");
|
71 | result = content.replace(regex, spanStart + "$&" + spanEnd);
|
72 | }
|
73 | return result;
|
74 | }
|
75 |
|
76 | var vueHighlighter = {
|
77 | bind: function bind(el, binding, vnode) {
|
78 | originalContent = el.innerHTML;
|
79 | var pattern = '';
|
80 | var word = '';
|
81 | var color = textColorDefault;
|
82 | var bgColor = bgColorDefault;
|
83 | var padding = paddingDefault;
|
84 | if (binding.value.word != undefined) {
|
85 | word = binding.value.word;
|
86 | }
|
87 | if (binding.value.live != undefined) {
|
88 | pattern = selectPattern(word, binding.value.live);
|
89 | }
|
90 | if (binding.value.style != undefined) {
|
91 | color = testTextColor(binding.value.style.color);
|
92 | bgColor = testBgColor(binding.value.style.bgColor);
|
93 | padding = testPadding(binding.value.style.padding);
|
94 | }
|
95 | el.innerHTML = highlight(originalContent, word, pattern, color, bgColor, padding);
|
96 | },
|
97 | update: function update(el, binding, vnode, oldVnode) {
|
98 | var pattern = '';
|
99 | var color = textColorDefault;
|
100 | var bgColor = bgColorDefault;
|
101 | var padding = paddingDefault;
|
102 | if (binding.value.style != undefined) {
|
103 | color = testTextColor(binding.value.style.color);
|
104 | bgColor = testBgColor(binding.value.style.bgColor);
|
105 | padding = testPadding(binding.value.style.padding);
|
106 | }
|
107 | if (binding.value.live) {
|
108 | pattern = selectPattern(binding.value.word, binding.value.live);
|
109 | el.innerHTML = highlight(vnode.children[0].text, binding.value.word, pattern, color, bgColor, padding);
|
110 | } else {
|
111 | pattern = selectPattern(binding.value.word);
|
112 | el.innerHTML = highlight(originalContent, binding.value.word, pattern, color, bgColor, padding);
|
113 | }
|
114 | },
|
115 | unbind: function unbind(el, binding, vnode) {
|
116 | el.innerHTML = originalContent;
|
117 | }
|
118 | };
|
119 |
|
120 | var Plugin = {
|
121 | install: function install(Vue, options) {
|
122 | Vue.directive('highlight', vueHighlighter);
|
123 | }
|
124 | };
|
125 |
|
126 |
|
127 | if (typeof window !== 'undefined' && window.Vue) {
|
128 | window.Vue.use(Plugin);
|
129 | }
|
130 |
|
131 | exports['default'] = Plugin;
|
132 | exports.highlight = vueHighlighter;
|