-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
107 lines (82 loc) · 2.91 KB
/
main.js
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
///@ts-nocheck
GMEdit.register('room-order', {
init: function () {
GMEdit.on('projectOpen', ({ project }) => {
const element = $gmedit['ui.treeview.TreeView'].makeItem('Room Order');
element.addEventListener('click', () => {
OpenRoomOrderPage(project);
});
$gmedit['ui.treeview.TreeView'].element.appendChild(element);
});
},
});
function OpenRoomOrderPage(yyProject) {
const project = getYYProject(yyProject.path);
const file = new $gmedit['gml.file.GmlFile'](
'Room Order',
null,
$gmedit['file.kind.gml.KGmlSearchResults'].inst,
'',
);
const page = document.createElement('div');
page.innerHTML = `<h1>Drag the item to change the order of the rooms</h1><h5>When you change an order, it is automatically saved</h5>`;
page.className = 'room-order-container';
const list = document.createElement('ul');
list.addEventListener(`dragstart`, (evt) => {
evt.target.classList.add(`selected`);
});
list.addEventListener(`dragend`, (evt) => {
evt.target.classList.remove(`selected`);
saveProject();
});
list.addEventListener(`dragover`, (evt) => {
evt.preventDefault();
const activeElement = list.querySelector(`.selected`);
const currentElement = evt.target;
const isMoveable =
activeElement !== currentElement && currentElement.classList.contains(`room-item`);
if (!isMoveable) {
return;
}
const nextElement = getNextElement(evt.clientY, currentElement);
if (
(nextElement && activeElement === nextElement.previousElementSibling) ||
activeElement === nextElement
) {
return;
}
list.insertBefore(activeElement, nextElement);
});
const saveProject = () => {
const items = Array.from(list.children).map((item) => item.innerText);
const RoomOrderNodes = items.map((room) => ({
roomId: { name: room, path: `rooms/${room}/${room}.yy` },
}));
project.RoomOrderNodes = RoomOrderNodes;
saveYYProject(yyProject.path, project);
};
project.RoomOrderNodes.forEach((room) => {
const li = document.createElement('li');
li.innerText = room.roomId.name;
li.draggable = true;
li.className = 'room-item';
list.appendChild(li);
});
page.appendChild(list);
file.editor.element = page;
$gmedit['gml.file.GmlFile'].openTab(file);
}
function getYYProject(path) {
const data = Electron_FS.readFileSync(path, 'utf-8');
return $gmedit['yy.YyJson'].parse(data);
}
function saveYYProject(path, data) {
Electron_FS.writeFileSync(path, $gmedit['yy.YyJsonPrinter'].stringify(data, true));
}
const getNextElement = (cursorPosition, currentElement) => {
const currentElementCoord = currentElement.getBoundingClientRect();
const currentElementCenter = currentElementCoord.y + currentElementCoord.height / 2;
const nextElement =
cursorPosition < currentElementCenter ? currentElement : currentElement.nextElementSibling;
return nextElement;
};