Skip to content

Commit 88015df

Browse files
committed
fix(SPA): 修复通用输入框输入emoji字符时被算作1个字符的问题 (hack)
issue #506
1 parent d010f69 commit 88015df

File tree

7 files changed

+64
-113
lines changed

7 files changed

+64
-113
lines changed

resources/spa/src/components/common/TextareaInput.vue

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
<template>
2-
<div
3-
:class="{'auto-height': !rows}"
4-
class="textarea-wrap"
5-
>
6-
<div
7-
v-if="!rows"
8-
class="textarea-shadow c-textarea-input"
9-
>
2+
<div :class="{'auto-height': !rows}" class="c-textarea-input">
3+
<div v-if="!rows" class="textarea-shadow textarea">
104
{{ value }}
115
</div> <!-- 用于撑起文本框自适应高度 -->
126
<textarea
@@ -16,13 +10,10 @@
1610
:maxlength="maxlength"
1711
:readonly="readonly"
1812
:rows="rows ? rows : 1"
19-
class="c-textarea-input"
20-
@input="$emit('input', $event.target.value)"
13+
class="textarea"
14+
@input="onInput"
2115
/>
22-
<span
23-
v-show="maxlength && value.length > warnlength"
24-
class="word-length"
25-
>
16+
<span v-show="maxlength && value.length > warnlength" class="word-length">
2617
{{ value.length }} / {{ maxlength }}
2718
</span>
2819
</div>
@@ -39,39 +30,48 @@ export default {
3930
placeholder: { type: String, default: '' },
4031
rows: { type: [Number, String], default: 0 },
4132
},
33+
methods: {
34+
onInput ($event) {
35+
let content = $event.target.value
36+
// 解决 emoji 字符被用于 maxlength 时被算作1个字符的问题(而'😫'.length => 2)
37+
this.maxlength && (content = content.substr(0, Number(this.maxlength)))
38+
this.$emit('input', content)
39+
},
40+
},
4241
}
4342
</script>
4443

4544
<style lang="less" scoped>
46-
.textarea-wrap {
45+
.c-textarea-input {
4746
position: relative;
4847
width: 100%;
48+
margin-bottom: 40px;
4949
height: calc(~"100% + 40px");
50-
padding-right: 20px;
5150
align-self: flex-start;
5251
53-
.textarea-shadow {
54-
opacity: 0;
52+
.textarea {
53+
width: 100%;
5554
min-height: 100px;
5655
word-wrap: break-word;
5756
word-break: break-all;
57+
font-size: inherit;
58+
text-align: inherit;
59+
line-height: inherit;
60+
}
61+
62+
.textarea-shadow {
63+
opacity: 0;
5864
}
5965
6066
textarea {
6167
position: absolute;
6268
top: 0;
6369
display: block;
64-
font-size: 30px;
65-
padding-bottom: 28px;
66-
width: calc(~"100% - 20px");
6770
height: 100%;
6871
overflow: hidden;
6972
resize: none;
7073
outline: none;
71-
min-height: 100px;
7274
background-color: transparent;
73-
word-wrap: break-word;
74-
word-break: break-all;
7575
7676
&[readonly] {
7777
color: #999;
@@ -80,6 +80,9 @@ export default {
8080
}
8181
8282
.word-length {
83+
position: absolute;
84+
bottom: -34px;
85+
right: 10px;
8386
display: block;
8487
font-size: 22px;
8588
text-align: right;

resources/spa/src/components/form/FormInputItem.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ export default {
9797
9898
.c-form-textarea-item {
9999
height: auto !important;
100+
padding: 28px 20px;
100101
101102
> label {
102103
align-self: flex-start;
103-
padding-top: 28px;
104104
}
105105
106106
+ .c-form-item {

resources/spa/src/page/UserInfo.vue

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<a
88
v-else
99
:class="{ disabled }"
10-
class="m-send-btn"
10+
class="submit-btn"
1111
@click.prevent="handleOk"
1212
>
1313
完成
@@ -229,45 +229,18 @@ export default {
229229

230230
<style lang="less" scoped>
231231
.p-user-info {
232-
main {
233-
background-color: #fff;
234-
}
235-
236-
input[type="text"] {
237-
height: 100%;
238-
font-size: 28px;
239-
line-height: normal;
240-
vertical-align: middle;
241-
}
242-
243-
textarea {
244-
font-size: inherit;
245-
font-family: inherit;
246-
line-height: inherit;
247-
background-color: transparent;
248-
outline: 0;
249-
border: 0;
250-
resize: none;
251-
padding: 0;
252-
margin: 0;
253-
width: 100%;
254-
box-sizing: border-box;
255-
-webkit-appearance: none !important;
256-
-moz-appearance: none !important;
257-
}
258-
259-
.m-avatar-box {
260-
margin-right: 35px;
261-
}
262-
263-
.m-send-btn {
232+
.submit-btn {
264233
color: @primary;
265234
266235
&.disabled {
267236
color: @gray;
268237
}
269238
}
270239
240+
main {
241+
background-color: #fff;
242+
}
243+
271244
.m-entry-append {
272245
margin-right: 20px;
273246
}
@@ -302,9 +275,3 @@ export default {
302275
}
303276
}
304277
</style>
305-
306-
<style lang="less">
307-
.c-textarea-input {
308-
padding-top: 28px;
309-
}
310-
</style>

resources/spa/src/page/post/PostImage.vue

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
</CommonHeader>
2626

2727
<main>
28-
<TextareaInput
29-
v-model="contentText"
30-
:rows="11"
31-
:maxlength="255"
32-
:warnlength="200"
33-
placeholder="输入要说的话,图文结合更精彩哦"
34-
class="textarea-input"
35-
/>
28+
<div class="content-wrap">
29+
<TextareaInput
30+
v-model="contentText"
31+
:rows="11"
32+
:maxlength="255"
33+
:warnlength="200"
34+
placeholder="输入要说的话,图文结合更精彩哦"
35+
/>
36+
</div>
3637
<ImageList :edit="pinned" style="padding: 0 .3rem .3rem" />
3738
</main>
3839

@@ -143,8 +144,8 @@ export default {
143144
background-color: #fff;
144145
145146
main {
146-
.textarea-input {
147-
padding-left: 20px;
147+
.content-wrap {
148+
padding: 20px;
148149
}
149150
}
150151

resources/spa/src/page/post/PostNews.vue

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@
111111
<div class="m-box m-flex-grow1 m-flex-shrink1 m-aln-center m-justify-end">
112112
<TextareaInput
113113
v-model="news.subject"
114-
class="textarea-input"
115114
maxlength="200"
116115
warnlength="150"
117116
placeholder="请输入摘要信息,最多200字"
@@ -510,11 +509,13 @@ export default {
510509
.m-post-news-row {
511510
font-size: 30px;
512511
padding: 40px 30px;
512+
513513
.m-entry-append {
514514
margin-left: 10px;
515515
}
516516
&-label {
517-
flex: 0 0 auto;
517+
flex: none;
518+
align-self: flex-start;
518519
width: 150px;
519520
}
520521
input {
@@ -523,23 +524,14 @@ export default {
523524
line-height: 30px;
524525
font-size: 28px;
525526
}
527+
528+
.c-textarea-input {
529+
text-align: right;
530+
}
526531
}
527532
528533
.placeholder {
529534
color: #ccc;
530535
}
531536
}
532537
</style>
533-
534-
<style lang="less">
535-
.p-post-news {
536-
.textarea-input {
537-
padding-right: 0;
538-
539-
textarea {
540-
text-align: right;
541-
width: 100%;
542-
}
543-
}
544-
}
545-
</style>

resources/spa/src/page/post/PostText.vue

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@
2424
</CommonHeader>
2525

2626
<main>
27-
<div style="height: 100%;">
27+
<div class="content-wrap">
2828
<TextareaInput
2929
v-model="contentText"
3030
:maxlength="255"
3131
:warnlength="200"
3232
:rows="11"
33-
class="textarea-input"
3433
/>
3534
</div>
3635
</main>
@@ -163,9 +162,8 @@ export default {
163162
flex: auto;
164163
padding-top: 90px;
165164
166-
.textarea-input {
167-
padding-top: 20px;
168-
padding-left: 20px;
165+
.content-wrap {
166+
padding: 20px;
169167
}
170168
}
171169

resources/spa/src/page/profile/Certificate.vue

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,12 @@ export default {
386386
}
387387
}
388388
389-
.m-input input {
390-
text-align: right;
389+
.m-input {
390+
padding: 0;
391+
392+
input {
393+
text-align: right;
394+
}
391395
}
392396
393397
&.auto-height {
@@ -397,10 +401,10 @@ export default {
397401
padding-top: 0.4rem;
398402
padding-bottom: 0.4rem;
399403
400-
textarea {
401-
width: 100%;
404+
.c-textarea-input {
405+
text-align: right;
406+
font-size: 28px;
402407
line-height: 1.4;
403-
font-size: 0.28rem;
404408
}
405409
}
406410
}
@@ -416,17 +420,3 @@ export default {
416420
}
417421
}
418422
</style>
419-
420-
<style lang="less">
421-
.p-profile-certificate {
422-
.textarea-wrap {
423-
padding-right: 0;
424-
425-
.c-textarea-input {
426-
text-align: right;
427-
width: 100%;
428-
font-size: 28px;
429-
}
430-
}
431-
}
432-
</style>

0 commit comments

Comments
 (0)