-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrenderer.js
More file actions
294 lines (240 loc) · 8.18 KB
/
Copy pathrenderer.js
File metadata and controls
294 lines (240 loc) · 8.18 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
import { showManager, hideManagers } from "./managers/router.js";
// renderer.js
document.addEventListener("DOMContentLoaded", () => {
// ---------- DOM references ----------
const nodeInput = document.getElementById("nodeInput");
const scidInput = document.getElementById("scidInput");
const saveNodeBtn = document.getElementById("saveNodeBookmarkButton");
const saveSCIDBtn = document.getElementById("saveSCIDBookmarkButton");
const loadTelaBtn = document.getElementById("loadTelaButton");
const applyNodeBtn = document.getElementById("applyNodeButton");
const newTabBtn = document.getElementById("newTabBtn");
const tabsContainer = document.getElementById("tabsContainer");
const sidebar = document.getElementById("sidebar");
const sidebarNodeList = document.getElementById("sidebarNodeList");
const sidebarSCIDList = document.getElementById("sidebarSCIDList");
const sidebarToggleBtn = document.getElementById("sidebarToggleBtn");
const api = window.electronAPI;
document.querySelectorAll("[data-manager]").forEach(btn => {
btn.addEventListener("click", () => {
const managerId = btn.dataset.manager;
// Show manager UI (renderer only)
showManager(managerId);
});
});
// ---------- SCID selection from search / picker ----------
api.onSCIDSelected((scid) => {
scidInput.value = scid;
scidInput.focus();
scidInput.select();
});
// ---------- State ----------
let tabs = []; // { id, scid, node, serverId, title }
let activeTabId = null;
// ---------- Handle Tab Titles ----------
api.onTelaTabTitle(({ tabId, title }) => {
const tab = tabs.find(t => t.serverId === tabId);
if (!tab) return;
tab.title = title;
renderTabs();
});
// ---------- Bookmark Helpers ----------
async function renderNodeBookmarks() {
const list = await api.loadNodeBookmarks();
sidebarNodeList.innerHTML = "";
list.forEach(b => {
const div = document.createElement("div");
div.className = "bookmark-item";
div.innerHTML = `<span class="bookmark-name">${b.name}</span><button class="bookmark-delete">✖</button>`;
div.querySelector("span").onclick = () => {
nodeInput.value = b.node;
};
div.querySelector(".bookmark-delete").onclick = async (e) => {
e.stopPropagation();
await api.deleteNodeBookmark(b.node);
renderNodeBookmarks();
};
sidebarNodeList.appendChild(div);
});
}
async function renderSCIDBookmarks() {
const list = await api.loadSCIDBookmarks();
sidebarSCIDList.innerHTML = "";
list.forEach(b => {
const div = document.createElement("div");
div.className = "bookmark-item";
div.innerHTML = `<span class="bookmark-name">${b.name}</span><button class="bookmark-delete">✖</button>`;
div.querySelector("span").onclick = () => {
scidInput.value = b.scid;
};
div.querySelector(".bookmark-delete").onclick = async (e) => {
e.stopPropagation();
await api.deleteSCIDBookmark(b.scid);
renderSCIDBookmarks();
};
sidebarSCIDList.appendChild(div);
});
}
// ---------- Bookmark Modal ----------
async function openBookmarkModal(type, value) {
const [winWidth, winHeight] = await api.getWindowSize();
const modalWidth = 400;
const modalHeight = 200;
const x = Math.max(20, (winWidth - modalWidth) / 2);
const y = Math.max(100, (winHeight - modalHeight) / 2 + 80);
await api.modalShow({
bounds: { x, y, width: modalWidth, height: modalHeight },
bookmarkType: type,
bookmarkValue: value
});
// Give modal time to save + close
setTimeout(async () => {
await renderNodeBookmarks();
await renderSCIDBookmarks();
}, 200);
}
// ---------- Tabs ----------
function renderTabs() {
tabsContainer.innerHTML = "";
tabs.forEach(tab => {
const el = document.createElement("div");
el.className = "tab" + (tab.id === activeTabId ? " active" : "");
el.dataset.tabId = tab.id;
el.innerHTML = `
<span class="tab-title">${tab.title || "blank"}</span>
<button class="close">✕</button>
`;
el.onclick = (e) => {
if (!e.target.classList.contains("close")) activateTab(tab.id);
};
el.querySelector(".close").onclick = (e) => {
e.stopPropagation();
closeTab(tab.id);
};
tabsContainer.appendChild(el);
});
}
async function activateTab(id) {
activeTabId = id;
renderTabs();
hideManagers();
const tab = tabs.find(t => t.id === id);
if (!tab) return;
const viewId = tab.serverId || tab.id; // SCID tab uses serverId, blank uses tab id
window.electronAPI.switchToTab(viewId);
}
async function closeTab(id) {
const tab = tabs.find(t => t.id === id);
if (!tab) return;
if (tab.serverId) {
await api.closeTelaTab({ tabId: tab.serverId, scid: tab.scid });
}
tabs = tabs.filter(t => t.id !== id);
if (activeTabId === id) {
if (tabs.length) activateTab(tabs[tabs.length - 1].id);
else createBlankTab();
} else renderTabs();
}
async function createBlankTab() {
const id = crypto.randomUUID();
const tab = {
id,
scid: null,
node: null,
serverId: null,
title: "Start Page"
};
tabs.push(tab);
activeTabId = id;
renderTabs();
hideManagers();
const { id: serverId } = await api.startTela(null, null);
tab.serverId = serverId;
window.electronAPI.switchToTab(serverId);
return tab;
}
async function createTabWithSCID(node, scid) {
const id = crypto.randomUUID();
const tab = { id, scid, node, serverId: null, title: scid };
tabs.push(tab);
activateTab(id);
try {
const { id: serverId } = await api.startTela(node, scid);
tab.serverId = serverId;
} catch (err) {
console.error("Failed to start Tela:", err);
alert("Failed to start SCID server");
tabs = tabs.filter(t => t.id !== id);
if (!tabs.length) createBlankTab();
else activateTab(tabs[0].id);
}
}
// ---------- Event Handlers ----------
saveNodeBtn.onclick = async () => {
const value = nodeInput.value.trim();
if (!value) return alert("Enter Node");
await openBookmarkModal("node", value);
};
saveSCIDBtn.onclick = async () => {
const value = scidInput.value.trim();
if (!value) return alert("Enter SCID");
await openBookmarkModal("scid", value);
};
loadTelaBtn.onclick = async () => {
const node = nodeInput.value.trim();
const scid = scidInput.value.trim();
if (!node || !scid) return alert("Enter Node + SCID");
const current = tabs.find(t => t.id === activeTabId);
if (current && current.serverId) {
await api.closeTelaTab({ tabId: current.serverId, scid: current.scid });
current.serverId = null;
current.scid = null;
}
if (current && !current.serverId && !current.scid) {
current.scid = scid;
current.node = node;
current.title = scid;
renderTabs();
try {
const { id: serverId } = await api.startTela(node, scid);
current.serverId = serverId;
} catch (err) {
console.error(err);
current.scid = null;
current.node = null;
current.title = "Start Page";
renderTabs();
}
} else {
await createTabWithSCID(node, scid);
}
};
// -------------------------------------------------------
applyNodeBtn.onclick = async () => {
const node = nodeInput.value.trim();
if (!node) {
alert("Enter a node address");
return;
}
const res = await window.electronAPI.applyGnomonNode(node);
if (!res.ok) {
alert(res.error || "Failed to apply node");
return;
}
alert(`Gnomon node set to ${res.node}`);
};
// ------------------------------------------------------
newTabBtn.onclick = () => createBlankTab();
sidebarToggleBtn.addEventListener("click", () => {
sidebar.classList.toggle("collapsed");
api.sendSidebarToggle(sidebar.classList.contains("collapsed"));
});
api.onBookmarksUpdated(async () => {
await renderNodeBookmarks();
await renderSCIDBookmarks();
});
// ---------- Init ----------
createBlankTab();
renderNodeBookmarks();
renderSCIDBookmarks();
});