Skip to content

Commit

Permalink
feat: add new runtime button
Browse files Browse the repository at this point in the history
  • Loading branch information
ambar committed Apr 5, 2024
1 parent 38b1864 commit 7e6ce7b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@
"contributes": {
"commands": [
{
"command": "liveCode.openPreviewToSide",
"title": "Choose Preview to the Side",
"command": "liveCode.choosePreviewToTheSide",
"title": "Choose A Runtime Preview to the Side",
"category": "Live Code",
"icon": "images/logo-plain.svg"
},
{
"command": "liveCode.openDefaultPreviewToSide",
"title": "Open Default Preview to the Side",
"category": "Live Code",
"icon": "images/logo-plain.svg"
},
Expand All @@ -45,19 +51,14 @@
"icon": "images/node-plain.svg"
},
{
"command": "liveCode.changePlatform",
"title": "Change Platform",
"command": "liveCode.changeCurrentRuntimeOfPreview",
"title": "Change Current Runtime of Preview",
"category": "Live Code",
"icon": "$(notebook-kernel-select)"
"icon": "images/logo-plain.svg"
}
],
"menus": {
"editor/context": [
{
"command": "liveCode.openPreviewToSide",
"when": "editorLangId in liveCode.supportedLanguageIds",
"group": "liveCode"
},
{
"command": "liveCode.openBrowserPreviewToSide",
"when": "editorLangId in liveCode.supportedLanguageIds",
Expand All @@ -81,20 +82,20 @@
"group": "navigation"
},
{
"command": "liveCode.openPreviewToSide",
"command": "liveCode.choosePreviewToTheSide",
"when": "editorLangId in liveCode.supportedLanguageIds",
"group": "navigation"
},
{
"command": "liveCode.changePlatform",
"command": "liveCode.changeCurrentRuntimeOfPreview",
"when": "liveCode.isPreviewFocus",
"group": "navigation"
}
]
},
"keybindings": [
{
"command": "liveCode.openPreviewToSide",
"command": "liveCode.choosePreviewToTheSide",
"key": "ctrl+k l",
"mac": "cmd+k l",
"when": "editorLangId in liveCode.supportedLanguageIds && !liveCode.isPreviewFocus"
Expand Down
41 changes: 29 additions & 12 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ export function activate(context: vscode.ExtensionContext) {

context.subscriptions.push(
// TODO: unregister if no panel is showing
vscode.commands.registerCommand('liveCode.changePlatform', switchPlatform),
vscode.commands.registerCommand(
'liveCode.changeCurrentRuntimeOfPreview',
choosePreviewToTheSide
),
vscode.commands.registerCommand('liveCode.reloadPreview', () => {
log('existingPanel reload')
const doc = vscode.window.activeTextEditor?.document
Expand All @@ -112,7 +115,11 @@ export function activate(context: vscode.ExtensionContext) {
return
}
}),
vscode.commands.registerCommand('liveCode.openPreviewToSide', () => {
vscode.commands.registerCommand(
'liveCode.choosePreviewToTheSide',
choosePreviewToTheSide
),
vscode.commands.registerCommand('liveCode.openDefaultPreviewToSide', () => {
openPreviewToSide(getDefaultRuntime())
}),
vscode.commands.registerCommand('liveCode.openBrowserPreviewToSide', () => {
Expand All @@ -124,13 +131,7 @@ export function activate(context: vscode.ExtensionContext) {
)
}

async function switchPlatform() {
const entry = [...documentPanelMap].find(([, x]) => x.active)
if (!entry) {
return
}
const [document, panel] = entry
const {currentRuntime} = panelConfigMap.get(panel)!
async function showRuntimePick(currentRuntime?: Runtime) {
const items: vscode.QuickPickItem[] = [
{
label: 'node',
Expand All @@ -149,13 +150,29 @@ async function switchPlatform() {
}`,
})),
{
placeHolder: 'Change platform in current preview of Live Code',
placeHolder: `Choose a runtime for Live Code`,
}
)
if (result) {

return result ? (result.label as Runtime) : null
}

async function choosePreviewToTheSide() {
const entry = [...documentPanelMap].find(([, x]) => x.active)
if (!entry) {
const runtime = await showRuntimePick()
if (runtime) {
openPreviewToSide(runtime)
}
return
}
const [document, panel] = entry
const {currentRuntime} = panelConfigMap.get(panel)!
const runtime = await showRuntimePick(currentRuntime)
if (runtime) {
panelConfigMap.set(panel, {
...panelConfigMap.get(panel),
currentRuntime: result.label as Runtime,
currentRuntime: runtime,
})
void processDocument(document)
}
Expand Down

0 comments on commit 7e6ce7b

Please sign in to comment.