Skip to content

Commit

Permalink
Fixing URI handler
Browse files Browse the repository at this point in the history
  • Loading branch information
lostintangent authored May 29, 2021
1 parent 2379829 commit 7ae9935
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
- Automatically set the "pattern" record mode when you create a new tour, and select `None` for the git ref
- Added support for opening a `*.tour` file in the VS Code notebook editor (Insiders only)

## v0.0.55

- The URI handler now allows specifying _just_ a step number, in order to index into a repo within only a single tour

## v0.0.54

- Added a URI handler, with support for launching a specific tour and step
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "CodeTour",
"description": "VS Code extension that allows you to record and playback guided tours of codebases, directly within the editor",
"publisher": "vsls-contrib",
"version": "0.0.54",
"version": "0.0.55",
"author": {
"name": "Microsoft Corporation"
},
Expand Down
78 changes: 46 additions & 32 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { URLSearchParams } from "url";
import * as vscode from "vscode";
import { initializeApi } from "./api";
import { initializeGitApi } from "./git";
Expand All @@ -26,49 +25,64 @@ function discoverTours(): Promise<void> {
return cachedDiscoverTours ?? (cachedDiscoverTours = _discoverTours());
}

function startTour(params: URLSearchParams) {
let tourPath = params.get("tour");
const step = params.get("step");

console.log("CT Tour: ", tourPath);
console.log("CT Step: ", step);

let stepNumber;
if (step) {
stepNumber = Number(step);
}

if (tourPath) {
if (!tourPath.endsWith(".tour")) {
tourPath = `${tourPath}.tour`;
}

console.log("CT Tour Path: ", tourPath);

console.log("CT Tours: ", store.tours);

const tour = store.tours.find(tour => tour.id.endsWith(tourPath as string));

console.log("CT Tour: ", tour);

if (tour) {
startCodeTour(tour, stepNumber);
}
} else {
startDefaultTour(undefined, undefined, stepNumber);
}
}

class URIHandler implements vscode.UriHandler {
private _didStartDefaultTour = false;
get didStartDefaultTour(): boolean {
return this._didStartDefaultTour;
}

async handleUri(uri: vscode.Uri): Promise<void> {
if (uri.path === "/startDefaultTour") {
this._didStartDefaultTour = true;

await discoverTours();
this._didStartDefaultTour = true;
await discoverTours();

let tourPath: string | null | undefined, stepNumber;
if (uri.path === "/startDefaultTour") {
if (uri.query) {
const origin = vscode.Uri.parse(uri.query);
if (origin.query) {
const params = new URLSearchParams(origin.query);
tourPath = params.get("tour");

const step = params.get("step");
if (step) {
stepNumber = Number(step);
console.log("CT Query: ", uri.query);
try {
const origin = vscode.Uri.parse(uri.query);
if (origin.query) {
const params = new URLSearchParams(origin.query);
startTour(params);
}
}
}

if (tourPath) {
if (!tourPath.endsWith(".tour")) {
tourPath = `${tourPath}.tour`;
}

const tour = store.tours.find(tour =>
tour.id.endsWith(tourPath as string)
);
if (tour) {
startCodeTour(tour, stepNumber);
}
} else {
startDefaultTour(undefined, undefined, stepNumber);
} catch {}
}
} else if (uri.path === "/starTour") {
const params = new URLSearchParams(uri.query);
startTour(params);
}

return;
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ export function startCodeTour(

export async function selectTour(
tours: CodeTour[],
workspaceRoot?: Uri
workspaceRoot?: Uri,
step: number = 0
): Promise<boolean> {
const items: any[] = tours.map(tour => ({
label: tour.title!,
Expand All @@ -87,7 +88,7 @@ export async function selectTour(
}));

if (items.length === 1) {
startCodeTour(items[0].tour, 0, workspaceRoot, false, true, tours);
startCodeTour(items[0].tour, step, workspaceRoot, false, true, tours);
return true;
}

Expand Down Expand Up @@ -195,7 +196,7 @@ export async function startDefaultTour(
startCodeTour(primaryTour, step, workspaceRoot, false, undefined, tours);
return true;
} else {
return selectTour(tours, workspaceRoot);
return selectTour(tours, workspaceRoot, step);
}
}

Expand Down

0 comments on commit 7ae9935

Please sign in to comment.