-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
167 lines (162 loc) · 4.77 KB
/
Copy pathindex.html
File metadata and controls
167 lines (162 loc) · 4.77 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Postit Wall</title>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
<link rel="stylesheet" href="./style/style.css">
</head>
<body>
<div id="app">
<transition-group name="fade">
<div class="postit" v-for="(p, pid) in postits" :style="postitCss(p)" :key="pid"
@mousedown="selectId($event,pid)" @touchstart="selectId($event,pid)">
<div class="text" v-html="newLinetoBr(p.text)"></div>
<div class="colorList">
<div class="colorBlock" v-for="color in colorList"
:style="{backgroundColor: color.color}" @click="setColor(pid, color.color)">
</div>
<i class="fas fa-pencil-alt" @click="editPostit(p, pid)"></i>
<i class="far fa-trash-alt" @click="removePostit(pid)"></i>
</div>
</div>
</transition-group>
<div class="dataList">
<button @click="addPostit">Add postit +</button>
<ul>
<li v-for="(p, pid) in postits">
<h3>{{p.text}}</h3>
<textarea v-model="p.text" @change="updateText(pid)"></textarea>
</li>
</ul>
</div>
</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.4.2/firebase.js"></script>
<script>
var vm = new Vue({
el: '#app',
data() {
return {
postits: [],
colorList: [
{
name: 'yellow',
color: '#ffeb67'
},
{
name: 'red',
color: '#ef898c'
},
{
name: 'blue',
color: '#a5d8d6'
},
{
name: 'green',
color: '#cbe196'
}
],
nowId: -1,
mousePos: {x: 0, y: 0},
startPos: {x: 0, y: 0}
}
},
methods: {
postitCss(p) {
let fontSize = ((240-10)/(1+Math.max((p.text).split("\n").map(t=>t.length)))-10);
return {
left: p.pos.x + 'px',
top: p.pos.y + 'px',
fontSize: fontSize >=1? fontSize + 'px': 1 + 'px',
// fontSize: ((240-10) / p.text.length) - 10 + 'px',
backgroundColor: this.colorList.find((item) => {
return item.color == p.color
}).color
}
},
newLinetoBr(text) {
return text.replace(new RegExp('\n'), '<br>');
},
selectId(e,id) {
if(e.srcElement.classList.contains('postit')) {
this.nowId = id;
this.startPos = {
x: e.offsetX,
y: e.offsetY
}
} else {
this.nowId = -1;
}
},
addPostit() {
let ran = Math.floor(Math.random()*4);
let ranColor = this.colorList[ran].color
postitRef.push({
text: '文字文字',
color: ranColor,
pos: {x: 350+Math.random()*200, y: 200+Math.random()*200}
})
},
setColor(id, color) {
this.postits[id].color = color;
postitRef.child(id).set(this.postits[id]);
},
editPostit(p, id) {
let text = prompt("輸入新內容:", p.text);
postitRef.child(id).child('text').set(text);
},
updateText(id) {
postitRef.child(id).set(this.postits[id]);
},
removePostit(id) {
postitRef.child(id).remove();
}
},
watch: {
mousePos() {
if(this.nowId != -1) {
let nowPostit = this.postits[this.nowId];
nowPostit.pos.x = this.mousePos.x - this.startPos.x;
nowPostit.pos.y = this.mousePos.y - this.startPos.y;
//同步firebase
postitRef.child(this.nowId).set(nowPostit);
}
}
}
})
window.onmousemove = (e) => {
if(vm.nowId != -1) {
vm.mousePos = {x: e.pageX, y: e.pageY}
}
}
window.ontouchmove = (e) => {
if(vm.nowId != -1) {
vm.mousePos = {x: e.pageX, y: e.pageY}
}
}
window.onmouseup = () => {
vm.nowId = -1;
}
window.ontouchend = () => {
vm.nowId = -1;
}
// Initialize Firebase
var config = {
apiKey: "AIzaSyCE6O3NdbIP5sVSA16l3qSFVTgkhZY_u20",
authDomain: "postit-wall.firebaseapp.com",
databaseURL: "https://postit-wall.firebaseio.com",
projectId: "postit-wall",
storageBucket: "postit-wall.appspot.com",
messagingSenderId: "511434022639"
};
firebase.initializeApp(config);
var postitRef = firebase.database().ref('/postit/');
postitRef.on('value', (snapshot) => {
vm.postits = snapshot.val();
})
</script>