Skip to content

Commit 7a93e5f

Browse files
committed
better drag and drop and various fixes
1 parent ea8d997 commit 7a93e5f

File tree

4 files changed

+117
-18
lines changed

4 files changed

+117
-18
lines changed

src/webpage/channel.ts

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -633,17 +633,28 @@ class Channel extends SnowFlake {
633633
decoration.classList.add("hiddencat");
634634
childrendiv.style.height = "0px";
635635
}
636-
decdiv.onclick = () => {
637-
if (childrendiv.style.height !== "0px") {
636+
const handleColapse = async (animate: boolean = true) => {
637+
if (this.perminfo.collapsed) {
638638
decoration.classList.add("hiddencat");
639-
this.perminfo.collapsed = true;
639+
childrendiv.style.height = childrendiv.scrollHeight + "px";
640+
await new Promise((res) => setTimeout(res, 0));
640641
childrendiv.style.height = "0px";
641642
} else {
642643
decoration.classList.remove("hiddencat");
643-
this.perminfo.collapsed = false;
644-
childrendiv.style.height = childrendiv.scrollHeight + "px";
644+
if (childrendiv.style.height === "0px" && animate) {
645+
childrendiv.style.height = childrendiv.scrollHeight + "px";
646+
} else {
647+
childrendiv.style.removeProperty("height");
648+
}
645649
}
646650
};
651+
const observer = new MutationObserver(handleColapse.bind(this, false));
652+
observer.observe(childrendiv, {childList: true, subtree: true});
653+
654+
decdiv.onclick = () => {
655+
this.perminfo.collapsed = !this.perminfo.collapsed;
656+
handleColapse();
657+
};
647658
} else {
648659
div.classList.add("channel");
649660
this.unreads();
@@ -786,27 +797,42 @@ class Channel extends SnowFlake {
786797
}
787798
}
788799

789-
coatDropDiv(div: HTMLDivElement, container: HTMLElement | boolean = false) {
800+
coatDropDiv(div: HTMLDivElement, container: HTMLElement | false = false) {
790801
div.addEventListener("dragenter", (event) => {
791802
console.log("enter");
792803
event.preventDefault();
793804
});
794805

795806
div.addEventListener("dragover", (event) => {
807+
const height = div.getBoundingClientRect().height;
808+
if (event.offsetY / height < 0.5) {
809+
div.classList.add("dragTopView");
810+
div.classList.remove("dragBottomView");
811+
} else {
812+
div.classList.remove("dragTopView");
813+
div.classList.add("dragBottomView");
814+
}
796815
event.preventDefault();
797816
});
798-
817+
div.addEventListener("dragleave", () => {
818+
div.classList.remove("dragTopView");
819+
div.classList.remove("dragBottomView");
820+
});
799821
div.addEventListener("drop", (event) => {
822+
div.classList.remove("dragTopView");
823+
div.classList.remove("dragBottomView");
800824
const that = Channel.dragged[0];
801825
if (!that) return;
802826
event.preventDefault();
803-
if (container && that.type !== 4) {
827+
const height = div.getBoundingClientRect().height;
828+
const before = event.offsetY / height < 0.5;
829+
if (container && that.type !== 4 && !before) {
804830
that.move_id = this.id;
805831
if (that.parent) {
806832
that.parent.children.splice(that.parent.children.indexOf(that), 1);
807833
}
808834
that.parent = this;
809-
(container as HTMLElement).prepend(Channel.dragged[1] as HTMLDivElement);
835+
container.prepend(Channel.dragged[1] as HTMLDivElement);
810836
this.children.unshift(that);
811837
} else {
812838
console.log(this, Channel.dragged);
@@ -830,23 +856,32 @@ class Channel extends SnowFlake {
830856
for (let i = 0; i < that.parent.children.length; i++) {
831857
build.push(that.parent.children[i]);
832858
if (that.parent.children[i] === thisy) {
859+
if (before) build.pop();
833860
build.push(that);
861+
if (before) build.push(thisy);
834862
}
835863
}
836864
that.parent.children = build;
865+
console.log(build);
837866
} else {
838867
const build: Channel[] = [];
839868
for (let i = 0; i < thisy.guild.headchannels.length; i++) {
840869
build.push(thisy.guild.headchannels[i]);
841870
if (thisy.guild.headchannels[i] === thisy) {
871+
if (before) build.pop();
842872
build.push(that);
873+
if (before) build.push(thisy);
843874
}
844875
}
845876
thisy.guild.headchannels = build;
846877
}
847878
if (Channel.dragged[1]) {
848879
if (this === thisy && this.type !== 4) {
849-
div.after(Channel.dragged[1]);
880+
if (before) {
881+
div.before(Channel.dragged[1]);
882+
} else {
883+
div.after(Channel.dragged[1]);
884+
}
850885
} else {
851886
div = div.parentElement as HTMLDivElement;
852887
if (!div) return;
@@ -855,11 +890,15 @@ class Channel extends SnowFlake {
855890

856891
console.log(div);
857892
Channel.dragged[1].remove();
858-
div.after(Channel.dragged[1]);
893+
if (before) {
894+
div.before(Channel.dragged[1]);
895+
} else {
896+
div.after(Channel.dragged[1]);
897+
}
859898
}
860899
}
861900
}
862-
this.guild.calculateReorder();
901+
this.guild.calculateReorder(that.id);
863902
});
864903

865904
return div;

src/webpage/guild.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,18 +1149,19 @@ class Guild extends SnowFlake {
11491149
let build = "";
11501150
for (const thing of this.headchannels) {
11511151
build += thing.name + ":" + thing.position + "\n";
1152+
console.log(thing.children);
11521153
for (const thingy of thing.children) {
11531154
build += " " + thingy.name + ":" + thingy.position + "\n";
11541155
}
11551156
}
11561157
console.log(build);
11571158
}
1158-
calculateReorder() {
1159+
calculateReorder(movedId?: string) {
11591160
let position = -1;
11601161
const build: {
11611162
id: string;
11621163
position: number | undefined;
1163-
parent_id: string | undefined;
1164+
parent_id: string | undefined | null;
11641165
}[] = [];
11651166
for (const thing of this.headchannels) {
11661167
const thisthing: {
@@ -1188,6 +1189,18 @@ class Guild extends SnowFlake {
11881189
}
11891190
}
11901191
}
1192+
const find = build.find((_) => _.id === movedId);
1193+
const channel = this.channels.find((_) => _.id === movedId);
1194+
if (!find) {
1195+
if (channel)
1196+
build.push({
1197+
id: channel.id,
1198+
position: channel.position,
1199+
parent_id: channel.parent?.id || null,
1200+
});
1201+
} else {
1202+
if (channel) find.parent_id = channel.parent?.id || null;
1203+
}
11911204
console.log(build);
11921205
this.printServers();
11931206
if (build.length === 0) {
@@ -1489,6 +1502,10 @@ class Guild extends SnowFlake {
14891502
this.headchannels.push(thing);
14901503
}
14911504
}
1505+
for (const channel of this.channels) {
1506+
channel.children = channel.children.sort((a, b) => a.position - b.position);
1507+
}
1508+
this.channels = this.channels.sort((a, b) => a.position - b.position);
14921509
this.printServers();
14931510
}
14941511
}

src/webpage/role.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ class RoleList extends Buttons {
274274
headers: this.headers,
275275
});
276276
},
277+
{
278+
visable: (role) => role.id !== role.guild.id,
279+
},
277280
);
278281
return menu;
279282
}
@@ -296,6 +299,7 @@ class RoleList extends Buttons {
296299
buttonRoleMap = new WeakMap<HTMLButtonElement, Role>();
297300
dragged?: HTMLButtonElement;
298301
buttonDragEvents(button: HTMLButtonElement, role: Role) {
302+
const height = 35;
299303
this.buttonRoleMap.set(button, role);
300304
button.addEventListener("dragstart", (e) => {
301305
this.dragged = button;
@@ -307,23 +311,38 @@ class RoleList extends Buttons {
307311
});
308312

309313
button.addEventListener("dragenter", (event) => {
310-
console.log("enter");
311314
event.preventDefault();
312315
return true;
313316
});
314317

315318
button.addEventListener("dragover", (event) => {
316319
event.preventDefault();
320+
if (event.offsetY / height < 0.5) {
321+
button.classList.add("dragTopView");
322+
button.classList.remove("dragBottomView");
323+
} else {
324+
button.classList.remove("dragTopView");
325+
button.classList.add("dragBottomView");
326+
}
317327
return true;
318328
});
329+
button.addEventListener("dragleave", () => {
330+
button.classList.remove("dragTopView");
331+
button.classList.remove("dragBottomView");
332+
});
319333

320-
button.addEventListener("drop", (_) => {
334+
button.addEventListener("drop", (event) => {
321335
const role2 = this.buttonRoleMap.get(this.dragged as HTMLButtonElement);
322-
if (!role2) return;
336+
if (!role2 || this.dragged === button) return;
323337
const index2 = this.guild.roles.indexOf(role2);
324338
this.guild.roles.splice(index2, 1);
325339
const index = this.guild.roles.indexOf(role);
326-
this.guild.roles.splice(index + 1, 0, role2);
340+
if (event.offsetY / height < 0.5) {
341+
this.guild.roles.splice(index, 0, role2);
342+
} else {
343+
this.guild.roles.splice(index + 1, 0, role2);
344+
}
345+
327346
this.guild.recalcRoles();
328347
console.log(role);
329348
});

src/webpage/style.css

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,7 @@ span.instanceStatus {
10461046
display: flex;
10471047
flex-direction: column;
10481048
margin-bottom: 2px;
1049+
position: relative;
10491050
}
10501051
.channelbutton {
10511052
height: 2em;
@@ -1939,6 +1940,8 @@ img.bigembedimg {
19391940
background: var(--secondary-bg);
19401941
border-radius: 4px;
19411942
box-shadow: 0 0 8px var(--shadow);
1943+
z-index: 2;
1944+
19421945
hr {
19431946
width: 90%;
19441947
height: 1px;
@@ -2424,6 +2427,7 @@ fieldset input[type="radio"] {
24242427
color: var(--primary-text-soft);
24252428
border: none;
24262429
transition: background 0.1s;
2430+
position: relative;
24272431
}
24282432
.activeSetting {
24292433
background: color-mix(in srgb, var(--secondary-bg) 60%, transparent);
@@ -3052,6 +3056,26 @@ img.error::after {
30523056
top: 0;
30533057
left: 0;
30543058
}
3059+
.dragTopView::after {
3060+
content: "";
3061+
width: 100%;
3062+
height: 4px;
3063+
border-radius: 4px;
3064+
background: var(--primary-text-soft);
3065+
top: -1px;
3066+
left: 0px;
3067+
position: absolute;
3068+
}
3069+
.dragBottomView::after {
3070+
content: "";
3071+
width: 100%;
3072+
height: 4px;
3073+
border-radius: 4px;
3074+
background: var(--primary-text-soft);
3075+
bottom: -3px;
3076+
left: 0px;
3077+
position: absolute;
3078+
}
30553079
.gifBox {
30563080
img {
30573081
max-width: 196px;

0 commit comments

Comments
 (0)