-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.html
More file actions
141 lines (125 loc) · 4.42 KB
/
example.html
File metadata and controls
141 lines (125 loc) · 4.42 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="description" content="Типографика на лету, позволяет типографировать текст при вводе в текстовое поле." />
<script
src="https://code.jquery.com/jquery-3.3.1.slim.js"
integrity="sha256-fNXJFIlca05BIO2Y5zh1xrShK3ME+/lYZ0j+ChxX2DA="
crossorigin="anonymous"></script>
<script src="https://unpkg.com/typograf/dist/typograf.js"></script>
<script src="jquery.autotypograf.js"></script>
<title>Пример использования jquery.autotypograf.js</title>
<style>
.page
{
padding: 20px 50px;
}
h1
{
font-family: Georgia, serif;
font-style: italic;
font-weight: normal;
}
textarea
{
width: 100%;
}
#example
{
display: none;
}
#example-label,
#example-html-label
{
font-style: italic;
}
#example-text,
#example-html-text
{
width: 100%;
padding: 5px;
margin-bottom: 1em;
min-height: 2em;
background: #f0f0f0;
}
.row
{
margin-top: 1em;
}
.entity-nb
{
opacity: .4;
background: green;
}
</style>
</head>
<body>
<div class="page">
<h1>Типографика на лету</h1>
<div class="row">
<textarea autocomplete="off" placeholder="Введите текст" rows="10"></textarea><br />
</div>
<div class="row"><input type="button" id="start" value="Пример ввода" /></div>
<div class="row">
<div id="example">
<div id="example-label">Вводимый текст:</div>
<div id="example-text"></div>
</div>
</div>
<div class="row">
<div id="example-html">
<div id="example-html-label">Подсветка неразрывных пробелов:</div>
<div id="example-html-text"></div>
</div>
</div>
</div>
<script>
(function() {
function highlightText(text) {
[
[/(\u00A0| | )/g, '\u00A0', 'NO-BREAK SPACE'],
[/(\u202F| )/g, '\u202F', 'NARROW NO-BREAK SPACE'],
[/(\u2011|‑)/g, '\u2011', 'NON-BREAKING HYPHEN']
].forEach(function(el) {
text = text.replace(el[0], '<span class="entity-nb" title="' + el[2] + '">' + el[1] + '</span>');
});
return text;
}
var textarea = $('textarea'),
text = '"Будь прост, но не слишком! Простейшее - амёба." Э. Кроткий',
position = 0,
currentText = '',
timer;
$(document).ready(function() {
textarea.autotypograf({
locale: ['ru', 'en-US']
});
})
textarea.on('input', function() {
setTimeout(function() {
$('#example-html-text').html(highlightText(textarea.val()));
}, 1);
});
$('#start').click(function() {
if(timer) {
clearInterval(timer);
timer = null;
}
position = 0;
$('#example').show();
timer = setInterval(function() {
currentText = text.substr(0, position);
position++;
textarea.val(currentText).trigger('input');
$('#example-text').html(highlightText(currentText));
if(position > text.length) {
clearInterval(timer);
timer = null;
}
}, 100);
});
})();
</script>
</body>
</html>