textareas fit to content in UserBio and Problems
[vchess.git] / client / src / components / UserBio.vue
1 <template lang="pug">
2 div(:id="'player_' + uid")
3 input.modal(
4 :id="'modalBio_' + uid"
5 type="checkbox"
6 )
7 div.bio-div(
8 role="dialog"
9 :data-checkbox="'modalBio_' + uid"
10 )
11 .card
12 div(v-if="st.user.id == uid")
13 div.buttons
14 button(@click="toggleEdit()")
15 | {{ st.tr[modeEdit ? "Cancel" : "Edit"] }}
16 button(
17 v-show="modeEdit"
18 @click="sendBio()"
19 )
20 | {{ st.tr["Send"] }}
21 fieldset(v-if="userBio !== undefined && modeEdit")
22 textarea(
23 @input="adjustHeight()"
24 v-model="userBio"
25 )
26 h3 {{ uname }}
27 .bio-content(
28 v-if="userBio !== undefined"
29 v-html="parseHtml(userBio)"
30 @click="modeEdit = !modeEdit"
31 )
32 .dialog.text-center {{ st.tr[infoMsg] }}
33 span(
34 :class="{ clickable: !!uname }"
35 @click="showBio()"
36 )
37 | {{ uname || "@nonymous" }}
38 </template>
39
40 <script>
41 import { store } from "@/store";
42 import { ajax } from "@/utils/ajax";
43 import { processModalClick } from "@/utils/modalClick.js";
44 export default {
45 name: "my-user-bio",
46 props: ["uid", "uname"],
47 data: function() {
48 return {
49 st: store.state,
50 userBio: undefined,
51 infoMsg: "",
52 modeEdit: false
53 };
54 },
55 methods: {
56 parseHtml: function(txt) {
57 return !txt.match(/<[/a-zA-Z]+>/)
58 ?
59 // No HTML tag
60 txt.replace(/\n\n/g, "<br/><div class='br'></div>")
61 .replace(/\n/g, "<br/>")
62 : txt;
63 },
64 adjustHeight: function() {
65 // https://stackoverflow.com/a/48460773
66 let t = document.querySelector("#player_" + this.uid + " textarea");
67 t.style.height = "";
68 t.style.height = (t.scrollHeight + 3) + "px";
69 },
70 toggleEdit: function() {
71 this.modeEdit = !this.modeEdit;
72 if (this.modeEdit) this.$nextTick(this.adjustHeight);
73 },
74 showBio: function() {
75 if (!this.uname)
76 // Anonymous users have no bio:
77 return;
78 this.infoMsg = "";
79 document.querySelector("#modalBio_" + this.uid).checked = true;
80 if (this.userBio === undefined) {
81 ajax(
82 "/userbio",
83 "GET",
84 {
85 data: { id: this.uid },
86 success: (res) => {
87 this.userBio = res.bio;
88 }
89 }
90 );
91 document.querySelector("#player_" + this.uid + " > .bio-div")
92 .addEventListener("click", processModalClick);
93 }
94 else if (this.modeEdit) this.adjustHeight();
95 },
96 sendBio: function() {
97 ajax(
98 "/userbio",
99 "PUT",
100 {
101 data: { bio: this.userBio },
102 success: () => {
103 this.infoMsg = this.st.tr["Modifications applied!"];
104 }
105 }
106 );
107 }
108 }
109 };
110 </script>
111
112 <style lang="sass">
113 // bio-content HTML elements are added after initial rendering
114 .bio-content
115 text-align: left
116 margin: 0 var(--universal-margin)
117 p, ul, ol
118 margin: var(--universal-margin) 0
119 .br
120 display: block
121 margin: 10px 0
122 </style>
123
124 <style lang="sass" scoped>
125 [type="checkbox"].modal+div .card
126 max-width: 500px
127 max-height: 100%
128
129 .buttons > button
130 margin-bottom: 0
131
132 h3
133 margin-bottom: 5px
134
135 textarea
136 display: block
137 margin: 0 var(--universal-margin)
138 width: calc(100% - 2 * var(--universal-margin))
139 min-height: 100px
140
141 .dialog
142 padding: 5px
143 color: blue
144 </style>