-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
321 lines (283 loc) · 12.5 KB
/
index.html
File metadata and controls
321 lines (283 loc) · 12.5 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<title>Loom — グループ対応 P2P チャット</title>
<meta name="viewport" content="width=device-width,initial-scale=1" />
<!-- CSS: 青基調 + 安定レイアウト + アニメーション -->
<style>
:root{
--main1:#2563eb;
--main2:#60a5fa;
--bg:#e0f4ff;
--card:#f0f8ff;
--accent:#93c5fd;
}
html,body{height:100%;margin:0;padding:0;font-family:"Segoe UI",system-ui, -apple-system, "Helvetica Neue", Arial; background:var(--bg);}
body{display:flex;height:100vh;box-sizing:border-box;}
/* LEFT */
#friends {
width:260px;
min-width:220px;
border-right:1px solid rgba(0,0,0,0.08);
background: linear-gradient(180deg,var(--main2),var(--main1));
color:white;
display:flex;
flex-direction:column;
padding:12px;
box-sizing:border-box;
gap:6px;
}
#friends h2{margin:0 0 4px 0;font-size:1.4rem;}
#friends p{margin:0 0 4px 0;font-size:0.95rem;} /* 名前とIDの間隔 = 4px */
#control-row{display:flex;gap:6px;align-items:center;}
#my-id{font-weight:600;background:rgba(255,255,255,0.12);padding:4px 6px;border-radius:6px;font-size:0.9rem;}
#create-group-btn{padding:6px 8px;border-radius:8px;background: linear-gradient(90deg,var(--main2),var(--main1)); border:none;color:white;font-weight:600;cursor:pointer; transition:transform .12s ease;}
#create-group-btn:hover{transform:scale(1.03);}
.section-title{color:#e8f2ff;margin:6px 0 4px 0;font-weight:600;font-size:0.95rem;}
#group-list, #friend-list{overflow:auto;flex:1;padding-right:6px;}
.list-item{
display:flex;align-items:center;justify-content:space-between;
gap:8px;padding:8px 10px;margin-bottom:6px;border-radius:10px;background:rgba(255,255,255,0.12);
transition: all .18s ease; cursor:pointer; color:white;
white-space:nowrap;text-overflow:ellipsis;overflow:hidden;
opacity:0; animation: itemFade .28s forwards;
}
.list-item:hover{transform:translateY(-2px); background:rgba(255,255,255,0.18);}
@keyframes itemFade{ to {opacity:1;} }
/* small badges */
.badge{background:rgba(255,255,255,0.15);padding:2px 6px;border-radius:999px;font-size:0.8rem;color:#fff;}
/* Add friend inputs (vertical) */
#add-friend-container{display:flex;flex-direction:column;gap:6px;margin-top:6px;}
#friend-name-input,#friend-id-input{
width:100%;padding:8px;border-radius:8px;border:none;outline:none;box-sizing:border-box;
}
#add-friend{padding:8px;border-radius:10px;border:none;background:linear-gradient(90deg,var(--main2),var(--main1));color:white;font-weight:600;cursor:pointer;transition:transform .12s}
#add-friend:hover{transform:scale(1.03);}
/* Right (chat) */
#chat-container{flex:1;display:flex;flex-direction:column;padding:14px;box-sizing:border-box;gap:8px;}
#chat-header{font-size:1.15rem;margin:0;font-weight:700;}
#chat-area{flex:1;background:var(--card);border-radius:12px;padding:14px;overflow:auto;display:flex;flex-direction:column;gap:8px;box-shadow:0 6px 18px rgba(37,99,235,0.06)}
/* message */
.msg{max-width:70%;padding:10px 14px;border-radius:16px;line-height:1.3;font-size:0.95rem;box-shadow:0 2px 6px rgba(2,6,23,0.04);opacity:0;transform:translateY(8px);animation:msgIn .22s forwards;}
.msg-you{background:linear-gradient(135deg,var(--main2),var(--main1));color:white;align-self:flex-end;border-bottom-right-radius:6px;}
.msg-friend{background:linear-gradient(135deg,#dbeafe,#93c5fd);color:#062a4e;align-self:flex-start;border-bottom-left-radius:6px;}
.msg-meta{font-size:0.75rem;opacity:0.8;margin-bottom:4px;}
@keyframes msgIn{ to {opacity:1;transform:translateY(0);} }
/* input */
#input-row{display:flex;gap:8px;align-items:center;margin-top:6px}
#msg-input{flex:1;padding:12px 16px;border-radius:24px;border:1px solid rgba(37,99,235,0.25);outline:none;font-size:0.95rem}
#send-btn{padding:10px 16px;border-radius:24px;border:none;background:linear-gradient(90deg,var(--main2),var(--main1));color:white;font-weight:700;cursor:pointer;transition:transform .12s}
#send-btn:hover{transform:scale(1.04);}
/* modal (group create) */
#modal-backdrop{position:fixed;left:0;top:0;width:100%;height:100%;background:rgba(2,6,23,0.35);display:flex;align-items:center;justify-content:center;z-index:60;display:none;}
#modal{width:420px;max-width:94%;background:white;border-radius:12px;padding:14px;box-sizing:border-box;}
#modal h3{margin:0 0 8px 0;}
#modal .friend-checkbox{display:flex;align-items:center;gap:8px;padding:8px;border-radius:8px;border:1px solid #eef6ff;margin-bottom:6px;}
#modal .controls{display:flex;gap:8px;justify-content:flex-end;margin-top:8px}
.ghost-small{font-size:0.85rem;color:#98bff6}
/* responsive small */
@media (max-width:720px){
#friends{display:none}
body{flex-direction:column}
#chat-container{padding:10px}
}
.list-item.unread {
background: rgba(255, 255, 255, 0.35);
font-weight: bold;
box-shadow: 0 0 6px rgba(255,255,255,0.4);
}
#footer {
text-align: center;
color: rgba(255, 255, 255, 0.7);
}
</style>
<script src="https://unpkg.com/peerjs@1.4.7/dist/peerjs.min.js"></script>
</head>
<body>
<!-- LEFT -->
<div id="friends">
<h2>Loom</h2>
<p>あなたのID: <span id="my-id">...</span></p>
<div id="control-row">
<button id="create-group-btn">+グループ作成</button>
<div class="badge ghost-small" id="peer-count">0</div>
</div>
<div class="section-title">グループ</div>
<div id="group-list"></div>
<div class="section-title">フレンド</div>
<div id="friend-list"></div>
<div id="add-friend-container">
<input id="friend-name-input" placeholder="表示名 (例: 太郎)">
<input id="friend-id-input" placeholder="Peer ID (相手のあなたのID)">
<button id="add-friend">追加して接続</button>
</div>
<div id="footer">© 2025 Loom</div>
</div>
<!-- RIGHT -->
<div id="chat-container">
<h3 id="chat-header">チャット</h3>
<div id="chat-area"></div>
<div id="input-row">
<input id="msg-input" placeholder="メッセージを入力...">
<button id="send-btn">送信</button>
</div>
</div>
<!-- Modal: Create Group -->
<div id="modal-backdrop">
<div id="modal">
<h3>グループを作成</h3>
<input id="group-name-input" placeholder="グループ名" style="width:100%;padding:8px;border:1px solid #eef6ff;border-radius:8px;margin-bottom:10px;">
<div id="modal-friend-list" style="max-height:240px;overflow:auto;margin-bottom:8px;"></div>
<div style="display:flex;justify-content:space-between;align-items:center;">
<div class="ghost-small">選択したメンバーに招待が送信されます</div>
<div class="controls">
<button id="cancel-create">キャンセル</button>
<button id="confirm-create" style="background:linear-gradient(90deg,var(--main2),var(--main1));color:white;padding:8px 12px;border:none;border-radius:8px;">作成</button>
</div>
</div>
</div>
</div>
<script>
const peer = new Peer({ host: '0.peerjs.com', port: 443, secure: true });
let friends = {}, groups = {}, connections = {}, current = null;
let myName = prompt("あなたの表示名を入力してください") || "匿名";
// helper
function $(id) { return document.getElementById(id); }
// Peer接続オープン
peer.on('open', id => { $('my-id').textContent = id; });
// Peer接続受信
peer.on('connection', conn => {
connections[conn.peer] = conn;
conn.on('data', raw => {
const msg = JSON.parse(raw);
// フレンド自己紹介
if (msg.type === 'friend-intro') {
if (!friends[msg.id]) friends[msg.id] = { name: msg.name, messages: [] };
addFriendItem(msg.id, msg.name);
// 自分も相手に自己紹介返す
if (!connections[msg.id]) {
const back = peer.connect(msg.id);
connections[msg.id] = back;
back.on('open', () => back.send(JSON.stringify({ type: 'friend-intro', id: peer.id, name: myName })));
}
return;
}
if (msg.type === 'direct') {
if (!friends[msg.from]) friends[msg.from] = { name: msg.from, messages: [] };
friends[msg.from].messages.push({ from: msg.from, text: msg.text });
if (current === msg.from) showChat(msg.from);
else markUnread('f-' + msg.from);
}
if (msg.type === 'group') {
if (!groups[msg.group]) return;
groups[msg.group].messages.push({ from: msg.from, text: msg.text });
if (current === msg.group) showGroup(msg.group);
else markUnread('g-' + msg.group);
}
});
});
// フレンド追加
$('add-friend').onclick = () => {
const n = $('friend-name-input').value.trim(), id = $('friend-id-input').value.trim();
if (!n || !id) return;
if (!friends[id]) friends[id] = { name: n, messages: [] };
addFriendItem(id, n);
// P2P接続
const conn = peer.connect(id);
connections[id] = conn;
conn.on('open', () => {
conn.send(JSON.stringify({ type: 'friend-intro', id: peer.id, name: myName }));
});
};
// フレンドリストに追加
function addFriendItem(id, name) {
if ($('f-' + id)) return;
const d = document.createElement('div');
d.className = 'list-item'; d.id = 'f-' + id; d.textContent = name;
d.onclick = () => { current = id; showChat(id); };
$('friend-list').appendChild(d);
}
// グループ作成
$('create-group-btn').onclick = () => {
const name = prompt('グループ名:'); if (!name) return;
const members = Object.keys(friends).filter(fid => confirm(friends[fid].name + ' を追加?'));
if (members.length === 0) return;
const gid = 'grp-' + Date.now();
groups[gid] = { name, members: [peer.id, ...members], messages: [] };
addGroupItem(gid);
};
// グループリストに追加
function addGroupItem(gid) {
if ($('g-' + gid)) return;
const d = document.createElement('div');
d.className = 'list-item'; d.id = 'g-' + gid; d.textContent = groups[gid].name;
d.onclick = () => { current = gid; showGroup(gid); };
$('group-list').appendChild(d);
}
// チャット表示(個人)
function showChat(id) {
clearUnread('f-' + id);
const c = $('chat-area'); c.innerHTML = '';
$('chat-header').textContent = friends[id].name;
friends[id].messages.forEach(m => {
const div = document.createElement('div');
div.className = 'msg ' + (m.from === 'you' ? 'msg-you' : 'msg-friend');
div.textContent = m.text; c.appendChild(div);
});
c.scrollTop = c.scrollHeight;
}
// チャット表示(グループ)
function showGroup(gid) {
clearUnread('g-' + gid);
const c = $('chat-area'); c.innerHTML = '';
$('chat-header').textContent = groups[gid].name + '(グループ)';
groups[gid].messages.forEach(m => {
const div = document.createElement('div');
div.className = 'msg ' + (m.from === peer.id ? 'msg-you' : 'msg-friend');
div.textContent = (m.from === peer.id ? 'あなた' : m.from) + ': ' + m.text;
c.appendChild(div);
});
c.scrollTop = c.scrollHeight;
}
// 送信ボタン
$('send-btn').onclick = sendMsg;
$('msg-input').addEventListener('keydown', e => { if (e.key === 'Enter') sendMsg(); });
// 送信処理
function sendMsg() {
const text = $('msg-input').value.trim(); if (!text || !current) return;
if (current.startsWith('grp-')) sendGroupMsg(current, text);
else sendDirectMsg(current, text);
$('msg-input').value = '';
}
function sendDirectMsg(id, text) {
const conn = connections[id]; if (!conn || !conn.open) { alert('接続なし'); return; }
const msg = { type: 'direct', from: peer.id, text };
conn.send(JSON.stringify(msg));
friends[id].messages.push({ from: 'you', text });
showChat(id);
}
function sendGroupMsg(gid, text) {
const g = groups[gid];
g.messages.push({ from: peer.id, text });
showGroup(gid);
g.members.forEach(mid => {
if (mid === peer.id) return;
const conn = connections[mid] || peer.connect(mid);
connections[mid] = conn;
conn.on('open', () => conn.send(JSON.stringify({ type: 'group', group: gid, from: peer.id, text })));
if (conn.open) conn.send(JSON.stringify({ type: 'group', group: gid, from: peer.id, text }));
});
}
// 未読管理
function markUnread(id) {
const el = $(id);
if (el) el.classList.add('unread');
}
function clearUnread(id) {
const el = $(id);
if (el) el.classList.remove('unread');
}
</script>
</body>
</html>