1
0
silverbullet/plugs/core/debug.ts

152 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-10-14 13:11:33 +00:00
import { sandbox } from "$sb/plugos-syscall/mod.ts";
2022-05-09 12:59:12 +00:00
import {
2022-10-14 13:11:33 +00:00
editor,
markdown,
sandbox as serverSandbox,
2023-01-14 17:51:00 +00:00
system,
2022-10-14 13:11:33 +00:00
} from "$sb/silverbullet-syscall/mod.ts";
2022-05-09 12:59:12 +00:00
export async function parsePageCommand() {
2022-07-04 13:51:04 +00:00
console.log(
"AST",
2022-10-14 13:11:33 +00:00
JSON.stringify(
await markdown.parseMarkdown(await editor.getText()),
null,
2,
),
2022-07-04 13:51:04 +00:00
);
2022-05-09 12:59:12 +00:00
}
export async function showLogsCommand() {
2023-01-14 17:51:00 +00:00
// Running in client/server mode?
const clientServer = !!(await system.getEnv());
2022-05-09 12:59:12 +00:00
2023-01-14 17:51:00 +00:00
if (clientServer) {
const clientLogs = await sandbox.getLogs();
const serverLogs = await serverSandbox.getServerLogs();
await editor.showPanel(
"bhs",
1,
`
2022-05-09 12:59:12 +00:00
<style>
2023-01-14 17:51:00 +00:00
#reload {
width: 75%;
}
#close {
width: 20%;
}
2022-05-09 12:59:12 +00:00
#client-log-header {
position: absolute;
left: 0;
2023-01-14 17:51:00 +00:00
top: 35px;
2022-05-09 12:59:12 +00:00
}
#server-log-header {
position: absolute;
right: 0;
2023-01-14 17:51:00 +00:00
top: 35px;
2022-05-09 12:59:12 +00:00
width: 50%;
}
#client-log {
position: absolute;
left: 0;
2023-01-14 17:51:00 +00:00
top: 60px;
2022-05-09 12:59:12 +00:00
bottom: 0;
width: 50%;
overflow: scroll;
}
#server-log {
position: absolute;
right: 0;
2023-01-14 17:51:00 +00:00
top: 60px;
2022-05-09 12:59:12 +00:00
bottom: 0;
width: 50%;
overflow: scroll;
}
</style>
2023-01-14 17:51:00 +00:00
<button onclick="self.reloadLogs()" id="reload">Reload</button>
<button onclick="self.close()" id="close">Close</button>
2022-05-09 12:59:12 +00:00
<div id="client-log-header">Client logs (max 100)</div>
<div id="client-log">
2022-10-12 09:47:13 +00:00
<pre>${
2023-01-14 17:51:00 +00:00
clientLogs
.map((le) => `[${le.level}] ${le.message}`)
.join("\n")
}</pre>
2022-05-09 12:59:12 +00:00
</div>
<div id="server-log-header">Server logs (max 100)</div>
<div id="server-log">
2022-10-12 09:47:13 +00:00
<pre>${
2023-01-14 17:51:00 +00:00
serverLogs
.map((le) => `[${le.level}] ${le.message}`)
.join("\n")
}</pre>
2022-05-09 12:59:12 +00:00
</div>`,
2023-01-14 17:51:00 +00:00
`
2022-05-09 12:59:12 +00:00
var clientDiv = document.getElementById("client-log");
clientDiv.scrollTop = clientDiv.scrollHeight;
var serverDiv = document.getElementById("server-log");
serverDiv.scrollTop = serverDiv.scrollHeight;
2023-01-14 17:51:00 +00:00
self.reloadLogs = () => {
2022-05-11 18:10:45 +00:00
sendEvent("log:reload");
2023-01-14 17:51:00 +00:00
};
self.close = () => {
sendEvent("log:hide");
};
2022-10-12 09:47:13 +00:00
`,
2023-01-14 17:51:00 +00:00
);
} else {
const logs = await sandbox.getLogs();
await editor.showPanel(
"bhs",
1,
`
<style>
#reload {
width: 75%;
}
#close {
width: 20%;
}
#log-header {
position: absolute;
left: 0;
top: 35px;
}
#log {
position: absolute;
left: 0;
top: 60px;
bottom: 0;
width: 100%;
overflow: scroll;
}
</style>
<button onclick="self.reloadLogs()" id="reload">Reload</button>
<button onclick="self.close()" id="close">Close</button>
<div id="log-header">Logs (max 100)</div>
<div id="log">
<pre>${
logs
.map((le) => `[${le.level}] ${le.message}`)
.join("\n")
}</pre>
</div>`,
`
var clientDiv = document.getElementById("log");
clientDiv.scrollTop = clientDiv.scrollHeight;
self.reloadLogs = () => {
sendEvent("log:reload");
};
self.close = () => {
sendEvent("log:hide");
};
`,
);
}
2022-05-09 12:59:12 +00:00
}
export async function hideBhsCommand() {
2022-10-14 13:11:33 +00:00
await editor.hidePanel("bhs");
2022-05-09 12:59:12 +00:00
}