Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add support for linking to local files #123

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion extension/src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { Base64 } from "js-base64";
import { ExcalidrawDocument } from "./document";
import { languageMap } from "./lang";

const urlRegex = /^(http[s]?:\/\/)?(www\.)?[a-zA-Z0-9.-]+\.[a-zA-Z]{2,5}\.?/;
//^ Replace with URL.canParse once it becomes available in VSCode

export class ExcalidrawEditorProvider
implements vscode.CustomEditorProvider<ExcalidrawDocument>
{
Expand Down Expand Up @@ -163,7 +166,7 @@ export class ExcalidrawEditor {
await this.document.update(new Uint8Array(msg.content));
break;
case "link-open":
vscode.env.openExternal(vscode.Uri.parse(msg.url));
await this.handleLinkOpen(msg.url);
break;
case "error":
vscode.window.showErrorMessage(msg.content);
Expand Down Expand Up @@ -342,6 +345,31 @@ export class ExcalidrawEditor {
}
}

private async handleLinkOpen(url: string) {
if (url.match(urlRegex)) {
await vscode.env.openExternal(vscode.Uri.parse(url));
return;
}
const targetPath = path.resolve(
path.dirname(this.document.uri.fsPath),
url
);
const targetUri = vscode.Uri.file(targetPath);

if (url.match(/\.excalidraw(\.|$)/)) {
await vscode.commands.executeCommand(
"vscode.openWith",
targetUri,
"editor.excalidraw"
);
return;
}

await vscode.window.showTextDocument(targetUri, {
preview: true,
});
}

private async buildHtmlForWebview(config: any): Promise<string> {
const publicUri = vscode.Uri.joinPath(this.context.extensionUri, "public");
const content = await vscode.workspace.fs.readFile(
Expand Down