forked from TileDB-Inc/jupyter-iframe-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
179 lines (158 loc) · 6.61 KB
/
main.js
File metadata and controls
179 lines (158 loc) · 6.61 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
/* eslint-disable @typescript-eslint/quotes */
/* eslint-disable no-undef */
import { createBridge } from 'jupyter-iframe-commands-host';
const commandBridge = createBridge({ iframeId: 'jupyterlab' });
const statusIndicator = document.getElementById('bridge-status');
statusIndicator.style.backgroundColor = '#ffa500'; // Orange for connecting
let bridgeReady = false;
const submitCommand = async (command, args) => {
// Don't allow command execution until bridge is ready
if (!bridgeReady) {
document.getElementById('error-dialog').innerHTML =
'<code>Command bridge is not ready yet. Please wait.</code>';
errorDialog.showModal();
return;
}
try {
await commandBridge.execute(command, args ? JSON.parse(args) : {});
} catch (e) {
document.getElementById('error-dialog').innerHTML = `<code>${e}</code>`;
errorDialog.showModal();
}
};
// Create and append dialogs to the document
const instructionsDialog = document.createElement('dialog');
instructionsDialog.innerHTML = `
<form method="dialog">
<div>
<h2 style="margin-top: 0;">Instructions</h2>
<p>To use this demo simply enter a command in the command input and any arguments for that command in the args input.</p>
<p>Click the <code style="background-color: lightsteelblue;">List Available Commands</code> button to see a list of available commands.</p>
<div style="display: flex; gap: 0.4rem; flex-direction: column; text-align: left; font-size: 0.9rem;">
<p style="font-weight: bold; padding: 0;">Some commands are listed here for convenience:</p>
<div class="command-example">
<ul style="list-style-type: none; display: flex; flex-direction: column; align-items: flex-start; gap: 0.25rem; margin: 0; padding: 0;">
<li>application:toggle-left-area</li>
<li>apputils:activate-command-palette</li>
<li>apputils:display-shortcuts</li>
<li>notebook:create-new</li>
</ul>
</div>
<p style="font-weight: bold; padding: 0;">And some with arguments:</p>
<div class="command-example">
<ul style="list-style-type: none; display: flex; flex-direction: column; align-items: flex-start; gap: 0.25rem; margin: 0; padding: 0;">
<li><span style="font-weight: bold;">Command:</span> apputils:change-theme</li>
<li><span style="font-weight: bold;">Args:</span> { 'theme': 'JupyterLab Light' }</li>
<br/>
<li><span style="font-weight: bold;">Command:</span> apputils:change-theme</li>
<li><span style="font-weight: bold;">Args:</span> { 'theme': 'JupyterLab Dark' }</li>
</ul>
</div>
</div>
<p>For even more convenience you can also select a command from the dropdown:</p>
<select id="command-select">
<option value="">Select a command</option>
<optgroup label="Commands">
<option value="application:toggle-left-area">Toggle Left Area</option>
<option value="apputils:display-shortcuts">Display Shortcuts</option>
<option value="notebook:create-new">Create New Notebook</option>
</optgroup>
<optgroup label="Commands with Arguments">
<option value="JupyterLab Light">Switch to Light Theme</option>
<option value="JupyterLab Dark">Switch to Dark Theme</option>
</optgroup>
</select>
</div>
<div class="dialog-buttons">
<button value="cancel">Cancel</button>
<button value="default" id="command-select-submit">OK</button>
</div>
</form>
<div style="margin-top: 1rem; font-size: 0.8rem; text-align: center;">
Check the <a href="https://github.com/TileDB-Inc/jupyter-iframe-commands?tab=readme-ov-file#usage" target="_blank">README</a> for more detailed instructions.
</div>
`;
const listCommandsDialog = document.createElement('dialog');
listCommandsDialog.innerHTML = `
<form method="dialog">
<h2 style="margin-top: 0;">Available Commands</h2>
<div id="commands-list"></div>
<div class="dialog-buttons">
<button value="close">Close</button>
</div>
</form>
`;
const errorDialog = document.createElement('dialog');
errorDialog.innerHTML = `
<form method="dialog">
<h2 style="margin: 0; color: #ED4337;">⚠ Error</h2>
<div id="error-dialog"></div>
<div class="dialog-buttons">
<button value="close">Close</button>
</div>
</form>
`;
document.body.appendChild(instructionsDialog);
document.body.appendChild(listCommandsDialog);
document.body.appendChild(errorDialog);
document.getElementById('instructions').addEventListener('click', () => {
instructionsDialog.showModal();
});
document
.getElementById('command-select-submit')
.addEventListener('click', async e => {
e.preventDefault();
const select = document.getElementById('command-select');
let command = select.value;
if (command) {
let args;
if (command.includes('Light') || command.includes('Dark')) {
args = `{"theme": "${command}"}`;
command = 'apputils:change-theme';
}
await submitCommand(command, args);
}
instructionsDialog.close();
});
document.getElementById('list-commands').addEventListener('click', async () => {
const commands = await commandBridge.listCommands();
commands.sort();
document.getElementById('commands-list').innerHTML = commands
.map(item => `<div>${item}</div>`)
.join('');
listCommandsDialog.showModal();
});
document.getElementById('commands').addEventListener('submit', async e => {
e.preventDefault();
const command = document.querySelector('input[name="command"]').value;
// Single quotes cause an error
const args = document
.querySelector('input[name="args"]')
.value.replace(/'/g, '"');
await submitCommand(command, args);
});
// Handle mode toggle
const iframe = document.getElementById('jupyterlab');
const modeRadios = document.querySelectorAll('input[name="mode"]');
modeRadios.forEach(radio => {
radio.addEventListener('change', e => {
const isNotebookView = e.target.value === 'notebook';
let currentUrl = new URL(iframe.src);
const isLite = currentUrl.pathname.includes('lite');
if (isLite) {
currentUrl = `./lite/${isNotebookView ? 'notebooks/index.html?path=example.ipynb' : 'lab'}`;
} else {
currentUrl.pathname = isNotebookView
? '/notebooks/example.ipynb'
: '/lab';
currentUrl.search = '';
}
iframe.src = currentUrl.toString();
});
});
// Wait for the command bridge to be ready
commandBridge.ready.then(() => {
bridgeReady = true;
statusIndicator.textContent = 'Connected to JupyterLab';
statusIndicator.style.backgroundColor = '#32CD32'; // Green for connected
});