-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_simulation.html
979 lines (912 loc) · 41.6 KB
/
chat_simulation.html
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
<!DOCTYPE html>
<html>
<head>
<title> 聊天模拟器 </title>
<meta name="viewport" content="width=device-width"/>
<style>
/* 通用属性 */
h1, h2, h3, h4, h5, h6 {
text-align: center;
}
body {
margin: 0;
}
button {
border: 2px solid;
border-radius: 5px;
}
#main_window {
margin: auto;
border: solid 2px;
width: 75%;
}
#chat_box {
min-height: 70vh;
}
</style>
<style>
/* 工具类 (我懒得每一条都加相同属性了) */
.overflow_auto {
overflow: auto;
}
.clear_both {
clear: both;
}
</style>
<style>
/* 消息通用属性 (头像, 名称) */
.msg_profile_photo {
float: left;
margin: 5px;
border-radius: 5px;
width: 100px;
height: 100px;
}
.msg_char_name {
float: left;
margin-left: 5px;
min-width: 10px;
min-height: 1px;
}
.msg_delete_button {
display: none;
float: right;
width: 10%;
}
.msg:hover > .msg_delete_button {
display: block;
}
</style>
<style>
/* 消息通用, main_char 属性 */
.msg.main_char > .msg_delete_button {
float: left;
}
.msg.main_char > .msg_profile_photo,
.msg.main_char > .msg_char_name {
float: right;
}
</style>
<style>
/* 文字消息, 通用属性 */
.msg_text > .msg_content {
float: left;
background-color: white;
/* 可缩放的聊天框 */
margin-top: min(40px, max(5%, 20px));
margin-bottom: min(40px, max(5%, 20px));
word-wrap: break-word;
width: fit-content;
max-width: 20%;
border-radius: 7px;
padding: 20px;
}
.msg_text > .msg_content::before {
content: "";
float: left;
border-bottom: 1em solid transparent;
border-top: 1em solid transparent;
border-right: 1em solid white;
margin-right: -1em;
position: relative;
right: 2.2em;
}
</style>
<style>
/* 文字消息, main_char 专有属性 */
.main_char.msg_text > .msg_content {
float: right;
background-color: #86d45e;
}
.main_char.msg_text > .msg_content::before {
float: right;
border-right: 0;
border-left: 1em solid #86d45e;
margin-right: 0;
margin-left: -1em;
left: 2.2em;
}
</style>
<style>
/* 底部横栏属性 */
#bottom_bar {
display: flex;
justify-content: space-around;
}
#show_char_list_button {
height: 100%;
margin-bottom: 2%;
background-size: cover;
}
#msg_input_box {
width: 75%;
max-width: 75%;
margin: .5%;
}
#send_button {
height: 100%;
margin-bottom: 2%;
}
</style>
<style>
/* 角色列表属性 */
#char_list_window {
border: 2px solid;
position: absolute;
bottom: 110%;
left: 10%;
background: #f6f5f4;
width: max-content;
max-width: 70vw;
max-height: 70vh;
overflow: scroll;
}
#clw_add_character_button {
width: 100%;
}
/* clw == char_list_window */
.clw_list {
margin: 0;
padding: 0;
}
.clw_item {
display: flex;
align-items: center;
justify-content: space-between;
list-style: none;
width: 100%;
}
/* 角色被选中时的样式 (应用于 .clw_item) */
.clw_selected {
background-color: #aaa;
}
</style>
<style>
/* 添加 & 修改角色窗口属性
* 让内部窗口居中放置 & 背景透明色 */
#add_new_character_window,
#modify_character_window {
position: fixed;
/* display: flex; */
align-items: center;
justify-content: center;
background-color: #00000060;
width: 100vw;
height: 100vh;
bottom: 0;
left: 0;
}
#add_new_character_window > section,
#modify_character_window > section {
width: 50%;
height: min-content;
border: 2px solid;
border-radius: 5px;
background-color: #f6f5f4;
}
/* 所有内容水平铺开
* ancw == add_new_character_window */
#add_new_character_window > section > section,
#modify_character_window > section > section {
display: flex;
height: 15vh;
}
#ancw_profile_photo, #mcw_profile_photo {
/* 此处大小只用于什么也没有的时候,
* 加载图片以后将会调整为图片大小 */
width: 20%;
}
.ancw_hint, .mcw_hint {
margin: auto;
}
#ancw_commit, #ancw_cancel,
#mcw_commit, #mcw_cancel {
width: 50%;
}
</style>
<style>
/* 为了爱护凌晨时的眼睛 */
@media (prefers-color-scheme: dark) {
h1, h2, h3, h4, h5, h6 {
color: #ddd;
}
html {
background-color: #111;
}
button {
background-color: #222;
color: #999;
}
button:hover {
background-color: #333;
}
button:active {
background-color: #444;
}
}
</style>
<style>
@media (prefers-color-scheme: dark) {
.msg_char_name {
color: #ddd;
}
.msg_text > .msg_content {
background-color: #2c2c2c;
color: #ddd;
}
.msg_text > .msg_content::before {
border-right: 1em solid #2c2c2c;
}
}
</style>
<style>
@media (prefers-color-scheme: dark) {
.msg_text.main_char > .msg_content {
color: black;
background-color: #38b572;
}
.msg_text.main_char > .msg_content::before {
border-left: 1em solid #38b572;
}
}
</style>
<style>
@media (prefers-color-scheme: dark) {
#char_list_window, #msg_input_box {
background-color: #111;
}
.clw_char_name, #msg_input_box {
color: #ddd;
}
}
</style>
<style>
@media (prefers-color-scheme: dark) {
#add_new_character_window > section, #ancw_name, #ancw_id,
#modify_character_window > section, #mcw_name, #mcw_id {
background-color: #111;
}
#ancw_profile_photofile, .ancw_hint, #ancw_name, #ancw_id,
#mcw_profile_photofile, .mcw_hint, #mcw_name, #mcw_id {
color: #ddd;
}
}
</style>
<!-- sub_char 文本消息模板 -->
<template id="text_sub_template"><section data-sender-id="${id}" class="msg msg_text sub_char overflow_auto clear_both">
<img class="msg_profile_photo" src="${photoURL}"/>
<div class="msg_char_name"> ${name} </div>
<div class="msg_content"> ${message} </div>
<button class="msg_delete_button">删除</button>
</section></template>
<!-- main_char 文本消息模板 -->
<template id="text_main_template"><section data-sender-id="${id}" class="msg msg_text main_char overflow_auto clear_both">
<img class="msg_profile_photo" src="${photoURL}"/>
<div class="msg_char_name"> ${name} </div>
<div class="msg_content"> ${message} </div>
<button class="msg_delete_button">删除</button>
</section></template>
<!-- 角色列表信息模板 -->
<template id="character_template"><li data-char-id="${id}" class="clw_item overflow_auto">
<img class="clw_profile_photo" src="${photoURL}"/>
<span class="clw_char_name"> ${name} (${id}) </span>
<button class="clw_set_as_main_button clw_no_select_when_click" style="display: block">设为主角色</button>
<button class="clw_set_as_sub_button clw_no_select_when_click" style="display: none">设为副角色</button>
<button class="clw_modify_character_button clw_no_select_when_click">修改角色</button>
<button class="clw_delete_character_button clw_no_select_when_click">删除角色</button>
</li></template>
<script>
// 角色数据库
/* 存储所有的角色
* key: 角色 ID
* value: 角色对象 */
var charactersMap = {};
// 当前选择的角色
var selectedCharacter = null;
// 当前正在修改的角色
var modifyingCharacter = null;
/* XXX 我想把Character 的
* id, name, photoFile 和 photo URL
* 用 getter 和 setter 重写 */
/* 角色对象
* @param id 角色 ID
* @param name 角色名
* @param photoFile 角色头像, File 对象
* @param photoURL 角色头像的 ObjectURL
* @param node 与这个角色关联的列表内的节点 */
function Character(id, name, photoFile, photoURL, node)
{
this.id = id;
this.name = name;
this.photoFile = photoFile;
this.photoURL = photoURL;
this.node = node;
// 这个角色是否是主角色
this.isMain = false;
}
// 被选中时 callback
Character.prototype.onSelect = function() {
if (selectedCharacter)
if (selectedCharacter === this) // 我们已经被选中了
return;
else // 首先取消选中之前选中的角色
selectedCharacter.onUnselect();
selectedCharacter = this;
this.node.classList.add("clw_selected");
}
// 被取消选中时 callback
Character.prototype.onUnselect = function() {
if (selectedCharacter !== this) // 我们不是被选中的
return;
selectedCharacter = null;
this.node.classList.remove("clw_selected");
}
// 设为主角色时 callback
Character.prototype.onSetAsMain = function() {
if (this.isMain) // 已经是主角色了
return;
this.isMain = true;
// 将所有此人发送的消息转为主角色格式
document.querySelectorAll(
`.msg[data-sender-id=${this.id}]`
).forEach(
function(node)
{
node.classList.remove("sub_char");
node.classList.add("main_char");
}
);
// 显示转为主角色按钮,隐藏转为副角色按钮
this.node.querySelector(".clw_set_as_sub_button").style.display = "block";
this.node.querySelector(".clw_set_as_main_button").style.display = "none";
}
// 取消设为主角色时 callback
Character.prototype.onUnsetAsMain = function() {
if (!this.isMain) // 已经是副角色了
return;
this.isMain = false;
// 将所有此人发送的消息转为副角色格式
document.querySelectorAll(
`.msg[data-sender-id=${this.id}]`
).forEach(
function(node)
{
node.classList.add("sub_char");
node.classList.remove("main_char");
}
);
// 显示转为主角色按钮,隐藏转为副角色按钮
this.node.querySelector(".clw_set_as_main_button").style.display = "block";
this.node.querySelector(".clw_set_as_sub_button").style.display = "none";
}
// 默认头像
// 不会了
var _b64 =
atob("iVBORw0KGgoAAAANSUhEUgAAAGwAAABsCAMAAAC4uKf/AAAAw1BMVEXu7u7t7e3c3dzOz87BwsG5" +
"urm0tbS1trXCwsLQ0NDe3t7p6um7vLvT1NPr6+vZ2dm4ubi6u7rd3d3Nzs3S09LP0M/T09Pb29vf" +
"39/r7Ou8vLzs7OzU1dTW19a+vr6/wL/m5+bn6Ofa2trR0dHR0tHJysnIycjV1tXh4eHLzMvi4+LD" +
"xMPj4+PMzcy9vb3k5OS3uLe2t7bKysrn5+fS0tLq6urAwMC5ubnX2Nfc3Ny8vbzl5eXDw8Pm5ubb" +
"3NvMzMzPz8/qEwsQAAABg0lEQVR4Ae3WZWIbMRAF4Gd6ZlttmJnRzHD/U/VXeNVG2plS5juAcAhG" +
"gzHGGGOMyWRz+QKLhVK5koGuarnAZ7V6A4qajm98+w41K/xgFUrWmGAdKjaYaBMKtgpMtL0Debv0" +
"2IO8fXocQNwhvY4g7Zhex5B2Qq9TSDuj1xmkndPrHNJss38vQFbotQJpF/S6gLRLel1CWsbRw2Ug" +
"7ooeV5B3TY9rKLhhohtouGWiW6i4Y4I76Gjc84P7BpQ8PPKd1gPUtPlOG3o6j3zj8QiKVvlGF5p6" +
"Rb5S7EFVX2E89SrxlRJUZQYK9d5nyDeG8NDI6scHqNl0fMdtQkdvhQlWepCXuRgwUeEiA2GjMb3G" +
"G5B0medP5S8hpXPOXzrvQMJWucVPaJW3kNpxjZ9UO0Y6kykDTCeIl1ltMUhrNYNIsz6D9WeIUnGM" +
"8JhFhHmRUYpzBDtsMVJrhlALRlsg1JjRxgjVYrQWQjGFv3szx2gOoVYYbYlQmRXHKG4lA2OMMcYY" +
"Y1L4AXbkP0W4ehXHAAAAAElFTkSuQmCC");
var _a = [];
for (var i in _b64)
_a.push(_b64.charCodeAt(i));
var defaultProfilePhoto = new Blob([Uint8Array.from(_a)], { type: "image/png" });
</script>
<script>
// 工具
</script>
<script>
// 角色处理 (添加, 删除, 修改)
/* 添加新角色
* @param id 角色 ID
* @param name 角色名
* @param photoFile 角色头像, File 对象
* @return 新创建的角色对象 */
function addNewCharacter(id, name, photoFile)
{
var oldNode = document.querySelector("#character_template");
if (!name) {
alert("名称不可为空!");
return;
}
if (charactersMap[id]) {
alert(`已存在相同 ID 的角色: "${charactersMap[id].name}"!`);
return;
}
var newNode = oldNode.cloneNode();
var photoURL = URL.createObjectURL(photoFile);
var newChar = null;
// 替换模板内的字符串 (我不会了)
newNode.innerHTML = eval(`\`${oldNode.innerHTML}\``);
var t = document.createElement("div");
t.appendChild(newNode.content);
newNode = t.childNodes[0];
newChar = new Character(id, name, photoFile, photoURL, newNode);
charactersMap[id] = newChar;
// 点击列表时选中
newNode.addEventListener(
"click", function(event) {
// 如果点击的元素不想触发选择事件则退出
if (event.target.classList.contains("clw_no_select_when_click")) {
event.stopPropagation();
return;
}
newChar.onSelect();
}
);
// 点击删除按钮时
newNode.querySelector(".clw_delete_character_button").addEventListener(
"click", function() {
deleteCharacterConfirm(newChar.id);
}
);
// 点击转为主角色按钮时
newNode.querySelector(".clw_set_as_main_button").addEventListener(
"click", function() {
newChar.onSetAsMain();
}
);
// 点击转为副角色按钮时
newNode.querySelector(".clw_set_as_sub_button").addEventListener(
"click", function() {
newChar.onUnsetAsMain();
}
);
// 点击修改按钮
newNode.querySelector(".clw_modify_character_button").addEventListener(
"click", function() {
modifyCharacterShowWindow(newChar.id);
}
);
// 因为现在只有一个列表, 所以用 querySelector 了
document.querySelector(".clw_list").appendChild(newNode);
return newChar;
}
/* 提示删除角色
* @param id 角色 ID */
function deleteCharacterConfirm(id)
{
// 再次确认
if (!confirm(`你确定要删除角色"${charactersMap[id].name}"吗?`))
return;
deleteCharacter(id);
}
/* 直接删除角色
* @param id 角色 ID */
function deleteCharacter(id)
{
if (!charactersMap[id]) // 没有这个角色
return;
if (charactersMap[id] === selectedCharacter)
selectedCharacter = null;
URL.revokeObjectURL(charactersMap[id].photoURL);
document.querySelector(`.clw_item[data-char-id=${id}]`).remove();
document.querySelectorAll(`.msg[data-sender-id=${id}]`).forEach(deleteMessage);
delete charactersMap[id];
}
/* 修改角色 (显示窗口)
* 这里指的是显示一个对话框让用户修改
* 但这个函数只管前面的显示窗口, 赋值等操作
* 后续的确认等都由修改窗口的回调进行处理
* @param id 角色 ID */
function modifyCharacterShowWindow(id)
{
var mcw = document.querySelector("#modify_character_window");
if (!charactersMap[id]) // 没有这个角色
return;
mcw.style.display = "flex"; // 显示修改界面, 然后填写角色信息
document.querySelector("#mcw_profile_photo").src = charactersMap[id].photoURL;
document.querySelector("#mcw_name").value = charactersMap[id].name;
document.querySelector("#mcw_id").value = charactersMap[id].id;
modifyingCharacter = charactersMap[id];
// 到这里我们的工作结束了, 接下来让修改窗口的回调处理吧
}
/* 修改角色, 显示窗口
* @param oldId 原角色 ID
* @param newId 新角色 ID (可选)
* @param name 新角色名称 (可选)
* @param photoFile 新角色头像, File 对象 (可选) */
function modifyCharacter(oldId, newId, name, photoFile)
{
var charObj = charactersMap[oldId];
if (!newId) {
alert("新 ID 不可为空!");
return;
}
if (charactersMap[newId] && charactersMap[newId] !== charactersMap[oldId]) {
alert(`已存在相同 ID 的角色: "${charactersMap[id].name}"!`);
return;
}
if (!name) {
alert("新名称不可为空!");
return;
}
if (!photoFile) {
alert("新头像 File 不可为空!");
return;
}
// 在 Map 里删除旧的 ID
delete charactersMap[oldId];
charactersMap[newId] = charObj;
// 更新属性
charObj.id = newId;
charObj.name = name;
// 如果图片没换的话那么就不用管了
if (photoFile !== charObj.photoFile) {
// 更新 URL
charObj.photoFile = photoFile;
URL.revokeObjectURL(charObj.photoURL);
charObj.photoURL = URL.createObjectURL(photoFile);
// 更新角色列表和消息列表里的头像
document.querySelector(
`.clw_item[data-char-id=${oldId}] .clw_profile_photo`
).src = charObj.photoURL;
document.querySelectorAll(
`.msg[data-sender-id=${oldId}] .msg_profile_photo`
).forEach(
function(node)
{
node.src = charObj.photoURL;
}
);
}
// 更新角色列表和消息列表里的名称信息
var node = document.querySelector(
`.clw_item[data-char-id=${oldId}] .clw_char_name`
);
node.innerHTML = `${charObj.name} (${charObj.id})`;
node.parentNode.setAttribute("data-char-id", charObj.id);
document.querySelectorAll(
`.msg[data-sender-id=${oldId}]`
).forEach(
function(node)
{
node.setAttribute("data-sender-id", newId);
node.querySelector(".msg_char_name").innerHTML = name;
}
);
}
</script>
<script>
// 消息处理 (添加, 删除)
/* 添加新消息
* @param id 角色 ID
* @param message 消息内容 (由消息类型决定)
* @param type 消息类型, 字符串形式 (现在只有 "text" 一种) */
function addMessage(id, message, type)
{
// 根据角色是主还是副, 获取对应的模板
var oldNode = document.querySelector(
selectedCharacter.isMain ? `#${type}_main_template` : `#${type}_sub_template`
);
var newNode = oldNode.cloneNode();
with (charactersMap[id])
newNode.innerHTML = eval(`\`${oldNode.innerHTML}\``);
var t = document.createElement("div");
t.appendChild(newNode.content);
newNode = t.childNodes[0];
newNode.querySelector(".msg_delete_button").addEventListener(
"click", function() {
deleteMessageConfirm(newNode);
}
);
document.querySelector("#chat_box").appendChild(newNode);
}
/* 提示删除信息
* @param node 消息的 Node (li) */
function deleteMessageConfirm(node)
{
// 再次确认
if (!confirm("你确定要删除此消息吗?"))
return;
deleteMessage(node);
}
/* 直接删除信息
* @param node 消息的 Node (li) */
function deleteMessage(node)
{
node.remove();
}
</script>
<script>
// 预设定的事件触发器
// 省略几百句没用的废话
function addOnLoadEventListener(elementSelector, event, func)
{
window.addEventListener("load",
function()
{
document.querySelector(elementSelector).addEventListener(event, func);
}
);
}
// 显示角色列表窗口
function showCharacterListWindow()
{
var clw = document.querySelector("#char_list_window");
clw.style.display = clw.style.display === "none" ? "inline-block" : "none";
}
addOnLoadEventListener("#show_char_list_button", "click", showCharacterListWindow);
// 显示添加角色窗口
function showAddNewCharacterWindow()
{
document.querySelector("#add_new_character_window").style.display = "flex";
}
addOnLoadEventListener("#clw_add_character_button", "click", showAddNewCharacterWindow);
// 关闭添加角色窗口
function closeAddNewCharacterWindow()
{
document.querySelector("#add_new_character_window").style.display = "none";
}
addOnLoadEventListener("#ancw_cancel", "click", closeAddNewCharacterWindow);
// 点击发送时加入新消息
function addNewMessage()
{
var msg = document.querySelector("#msg_input_box").value;
var oldNode = null;
var newNode = null;
if (!msg) {
alert("消息为空!");
return;
}
if (!selectedCharacter) {
alert("没有选中角色!");
return;
}
addMessage(selectedCharacter.id, msg, "text");
document.querySelector("#msg_input_box").value = "";
}
addOnLoadEventListener("#send_button", "click", addNewMessage);
// 角色头像切换时更新 #ancw_profile_photo
var _tmp_profile_photoURL = null;
function updateAncwProfilePhoto()
{
// 如果以前有 URL 的话, 首先释放
if (_tmp_profile_photoURL)
URL.revokeObjectURL(_tmp_profile_photoURL);
var url = URL.createObjectURL(this.files[0]);
// 绑定到 _tmp_profile_photoURL, 以便改变图片的时候释放
_tmp_profile_photoURL = url;
document.querySelector("#ancw_profile_photo").src = url;
}
addOnLoadEventListener("#ancw_profile_photofile", "change", updateAncwProfilePhoto);
// 角色头像切换时更新 #mcw_profile_photo
var _tmp2_profile_photoURL = null;
function updateMcwProfilePhoto()
{
// 如果以前有 URL 的话, 首先释放
if (_tmp2_profile_photoURL)
URL.revokeObjectURL(_tmp2_profile_photoURL);
var url = URL.createObjectURL(this.files[0]);
// 绑定到 _tmp2_profile_photoURL, 以便改变图片的时候释放
_tmp2_profile_photoURL = url;
document.querySelector("#mcw_profile_photo").src = url;
}
addOnLoadEventListener("#mcw_profile_photofile", "change", updateMcwProfilePhoto);
// 添加角色窗口点击确定时提交
function commitNewCharacter()
{
var id = document.querySelector("#ancw_id").value ||
`c${Object.keys(charactersMap).length}`
var name = document.querySelector("#ancw_name").value;
var photoFile = document.querySelector("#ancw_profile_photofile").files[0] ||
defaultProfilePhoto;
addNewCharacter(id, name, photoFile);
closeAddNewCharacterWindow();
}
addOnLoadEventListener("#ancw_commit", "click", commitNewCharacter);
// 修改角色窗口点击确定时提交
function commitModifyCharacter()
{
var id = document.querySelector("#mcw_id").value ||
`c${Object.keys(charactersMap).length}`
var name = document.querySelector("#mcw_name").value;
var photoFile = document.querySelector("#mcw_profile_photofile").files[0] ||
modifyingCharacter.photoFile;
var charObj = charactersMap[modifyingCharacter.id];
modifyCharacter(modifyingCharacter.id, id, name, photoFile);
modifyingCharacter = null;
document.querySelector("#modify_character_window").style.display = "none";
}
addOnLoadEventListener("#mcw_commit", "click", commitModifyCharacter);
// 关闭修改角色窗口
function closeModifyCharacterWindow()
{
document.querySelector("#modify_character_window").style.display = "none";
}
addOnLoadEventListener("#mcw_cancel", "click", closeModifyCharacterWindow);
</script>
</head>
<body>
<h1 style="margin-top: 0;"> 聊天模拟器 </h1>
<!-- TODO 这里准备做保存支持, 所以需要一个会话名 -->
<!-- <h2> 会话 <<span id="title">未命名</span>> </h2> -->
<!-- 主窗口 (包含聊天窗口和横栏) -->
<main id="main_window" class="overflow_auto">
<!-- 聊天窗口 -->
<article id="chat_box">
<!-- 这些都是布局测试 -->
<!--<section data-sender-id="xiaoxi" class="msg msg_text sub_char overflow_auto clear_both">
<!-- 短消息测试 >
<img class="msg_profile_photo" src="/home/careful/Desktop/赛尔号/图库/09.jpg"/>
<div class=""> 赛小息 </div>
<div class="msg_content"> wow </div>
<button class="msg_delete_button">删除</button>
</section>
<section data-sender-id="xiaoxi" class="msg msg_text sub_char overflow_auto clear_both">
<!-- 长消息测试 >
<img class="msg_profile_photo" src="/home/careful/Desktop/赛尔号/图库/09.jpg"/>
<div class="msg_char_name"> 赛小息 </div>
<div class="msg_content"> wowwowwowwowwowwowwowwowwowwowwowwow </div>
<button class="msg_delete_button">删除</button>
</section>
<section data-sender-id="xiaoxi" class="msg msg_text main_char overflow_auto clear_both">
<img class="msg_profile_photo" src="/home/careful/Desktop/赛尔号/图库/09.jpg"/>
<div class=""> 赛小息 </div>
<div class="msg_content"> wow </div>
<button class="msg_delete_button">删除</button>
</section>
<section data-sender-id="xiaoxi" class="msg msg_text main_char overflow_auto clear_both">
<img class="msg_profile_photo" src="/home/careful/Desktop/赛尔号/图库/09.jpg"/>
<div class=""> 赛小息 </div>
<div class="msg_content"> wowwowwowwowwowwowwowwowwowwowwowwow </div>
<button class="msg_delete_button">删除</button>
</section>
<section data-sender-id="atieda" class="msg msg_text sub_char overflow_auto clear_both">
<img class="msg_profile_photo" src="/home/careful/Desktop/赛尔号/图库/10.jpg"/>
<div class=""> 阿铁打 </div>
<div class="msg_content"> wow </div>
<button class="msg_delete_button">删除</button>
</section>
<section data-sender-id="atieda" class="msg msg_text sub_char overflow_auto clear_both">
<img class="msg_profile_photo" src="/home/careful/Desktop/赛尔号/图库/10.jpg"/>
<div class=""> 阿铁打 </div>
<div class="msg_content"> wowwowwowwowwowwowwowwowwowwowwowwow </div>
<button class="msg_delete_button">删除</button>
</section>
<section data-sender-id="atieda" class="msg msg_text main_char overflow_auto clear_both">
<img class="msg_profile_photo" src="/home/careful/Desktop/赛尔号/图库/10.jpg"/>
<div class=""> 阿铁打 </div>
<div class="msg_content"> wow </div>
<button class="msg_delete_button">删除</button>
</section>
<section data-sender-id="atieda" class="msg msg_text main_char overflow_auto clear_both">
<img class="msg_profile_photo" src="/home/careful/Desktop/赛尔号/图库/10.jpg"/>
<div class=""> 阿铁打 </div>
<div class="msg_content"> wowwowwowwowwowwowwowwowwowwowwowwow </div>
<button class="msg_delete_button">删除</button>
</section>
<section data-sender-id="kalulu" class="msg msg_text sub_char overflow_auto clear_both">
<img class="msg_profile_photo" src="/home/careful/Desktop/赛尔号/图库/01.jpg"/>
<div class=""> 卡璐璐 </div>
<div class="msg_content"> wow </div>
<button class="msg_delete_button">删除</button>
</section>
<section data-sender-id="kalulu" class="msg msg_text sub_char overflow_auto clear_both">
<img class="msg_profile_photo" src="/home/careful/Desktop/赛尔号/图库/01.jpg"/>
<div class=""> 卡璐璐 </div>
<div class="msg_content"> wowwowwowwowwowwowwowwowwowwowwowwow </div>
<button class="msg_delete_button">删除</button>
</section>
<section data-sender-id="kalulu" class="msg msg_text main_char overflow_auto clear_both">
<img class="msg_profile_photo" src="/home/careful/Desktop/赛尔号/图库/01.jpg"/>
<div class=""> 卡璐璐 </div>
<div class="msg_content"> wow </div>
<button class="msg_delete_button">删除</button>
</section>
<section data-sender-id="kalulu" class="msg msg_text main_char overflow_auto clear_both">
<img class="msg_profile_photo" src="/home/careful/Desktop/赛尔号/图库/01.jpg"/>
<div class=""> 卡璐璐 </div>
<div class="msg_content"> wowwowwowwowwowwowwowwowwowwowwowwow </div>
<button class="msg_delete_button">删除</button>
</section>-->
</article>
<hr/>
<!-- 横栏 -->
<article id="bottom_bar">
<span style="position: relative">
<button id="show_char_list_button">角色列表</button>
<article id="char_list_window" style="display: none">
<h3> 角色列表 (点击以选择) </h3>
<hr/>
<ul class="clw_list overflow_auto">
<!-- 点击事件会在添加的时候加进去 -->
<!-- 这些都是布局测试 -->
<!--<li data-char-id="xiaoxi" class="clw_item clw_selected overflow_auto">
<img class="clw_profile_photo" src="/home/careful/Desktop/赛尔号/图库/09.jpg"/>
<span class="clw_char_name"> 赛小息 (xiaoxi) </span>
<button class="clw_set_as_main_button clw_no_select_when_click" style="display: block">设为主角色</button>
<button class="clw_set_as_sub_button clw_no_select_when_click" style="display: none">设为副角色</button>
<button class="clw_modify_character_button clw_no_select_when_click">修改角色</button>
<button class="clw_delete_character_button clw_no_select_when_click">删除角色</button>
</li>
<hr/>
<li data-char-id="atieda" class="clw_item overflow_auto">
<img class="clw_profile_photo" src="/home/careful/Desktop/赛尔号/图库/10.jpg"/>
<span class="clw_char_name"> 阿铁打 (atieda) </span>
<button class="clw_set_as_main_button clw_no_select_when_click" style="display: block">设为主角色</button>
<button class="clw_set_as_sub_button clw_no_select_when_click" style="display: none">设为副角色</button>
<button class="clw_modify_character_button clw_no_select_when_click">修改角色</button>
<button class="clw_delete_character_button clw_no_select_when_click">删除角色</button>
</li>
<hr/>
<li data-char-id="kalulu" class="clw_item overflow_auto">
<img class="clw_profile_photo" src="/home/careful/Desktop/赛尔号/图库/01.jpg"/>
<span class="clw_char_name"> 卡璐璐 (kalulu) </span>
<button class="clw_set_as_main_button clw_no_select_when_click" style="display: block">设为主角色</button>
<button class="clw_set_as_sub_button clw_no_select_when_click" style="display: none">设为副角色</button>
<button class="clw_modify_character_button clw_no_select_when_click">修改角色</button>
<button class="clw_delete_character_button clw_no_select_when_click">删除角色</button>
</li>-->
</ul>
<hr/>
<button id="clw_add_character_button">添加角色</button>
</article>
</span>
<textarea id="msg_input_box" placeholder="输入消息 (支持 HTML)"></textarea>
<span><button id="send_button">发送</button></span>
</article>
</main>
<!-- 添加角色窗口 -->
<!-- 默认是全屏的 -->
<!-- 这只是一个背景 -->
<article id="add_new_character_window" style="display: none">
<!-- 实际窗口 -->
<section>
<h3> 添加新角色 </h3>
<section style="height: min-content">
<input id="ancw_profile_photofile" type="file"/>
</section>
<section>
<img id="ancw_profile_photo"/>
<span class="ancw_hint">角色头像</span>
</section>
<section>
<input id="ancw_name" type="text"/>
<span class="ancw_hint">角色名称</span>
</section>
<section>
<input id="ancw_id" type="text"/>
<span class="ancw_hint">角色ID (若未指定则自动生成)</span>
</section>
<section>
<button id="ancw_commit">确定</button>
<button id="ancw_cancel">取消</button>
</section>
</section>
</article>
<!-- 修改角色窗口 -->
<!-- 由add_new_character_window 改写 -->
<article id="modify_character_window" style="display: none">
<section>
<h3> 修改角色 </h3>
<section style="height: min-content">
<input id="mcw_profile_photofile" type="file"/>
</section>
<section>
<img id="mcw_profile_photo"/>
<span class="mcw_hint">角色头像</span>
</section>
<section>
<input id="mcw_name" type="text"/>
<span class="mcw_hint">角色名称</span>
</section>
<section>
<input id="mcw_id" type="text"/>
<span class="mcw_hint">角色ID (若未指定则自动生成)</span>
</section>
<section>
<button id="mcw_commit">确定</button>
<button id="mcw_cancel">取消</button>
</section>
</section>
</article>
</body>
</html>