Skip to content

Commit 1bd29ee

Browse files
authored
Add newFile command (#159)
* add newFile command * add all file extensions * add command to newFile menu of vscode * support untitled document * add workspace path if workspace is present
1 parent c4299b6 commit 1bd29ee

6 files changed

Lines changed: 91 additions & 4 deletions

File tree

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.prettierignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
dist/*
2+
node_modules/*
3+
*.json
4+
*.code-snippets
5+
webview/dist/*
6+
webview/node_modules/*

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@
3939
"title": "Color Theme",
4040
"icon": "$(color-mode)"
4141
},
42+
{
43+
"command": "excalidraw.newFile",
44+
"category": "Excalidraw",
45+
"title": "Scene",
46+
"icon": "$(color-mode)"
47+
},
48+
{
49+
"command": "excalidraw.newSceneFile",
50+
"category": "Excalidraw",
51+
"title": "New File",
52+
"icon": "$(color-mode)"
53+
},
4254
{
4355
"command": "excalidraw.preventDefault",
4456
"category": "Excalidraw",
@@ -245,6 +257,10 @@
245257
],
246258
"menus": {
247259
"commandPalette": [
260+
{
261+
"command": "excalidraw.newFile",
262+
"when": "false"
263+
},
248264
{
249265
"command": "excalidraw.updateTheme",
250266
"when": "activeCustomEditorId == 'editor.excalidraw'"
@@ -302,6 +318,11 @@
302318
"when": "activeCustomEditorId == 'editor.excalidraw'",
303319
"group": "navigation"
304320
}
321+
],
322+
"file/newFile": [
323+
{
324+
"command": "excalidraw.newFile"
325+
}
305326
]
306327
},
307328
"customEditors": [
@@ -347,6 +368,8 @@
347368
"package": "npm run build-webview && webpack --mode production --devtool hidden-source-map",
348369
"vscode:prepublish": "npm run package",
349370
"lint": "eslint --ext .ts src",
371+
"prettier": "prettier --check .",
372+
"prettierfix": "prettier --write .",
350373
"open-in-browser": "vscode-test-web --extensionDevelopmentPath=. ../examples"
351374
},
352375
"capabilities": {

src/commands.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as vscode from "vscode";
2+
import { newUntitledExcalidrawDocument } from "./utils";
23

34
function getConfigurationScope(
45
config: vscode.WorkspaceConfiguration,
@@ -89,7 +90,21 @@ function showImage(uri: vscode.Uri, viewColumn?: vscode.ViewColumn) {
8990
);
9091
}
9192

93+
async function newFile() {
94+
try {
95+
await newUntitledExcalidrawDocument();
96+
} catch (error) {
97+
vscode.window.showErrorMessage(`Failed to create new file: ${error}`);
98+
}
99+
}
100+
92101
export function registerCommands(context: vscode.ExtensionContext) {
102+
context.subscriptions.push(
103+
vscode.commands.registerCommand("excalidraw.newFile", newFile)
104+
);
105+
context.subscriptions.push(
106+
vscode.commands.registerCommand("excalidraw.newSceneFile", newFile)
107+
);
93108
context.subscriptions.push(
94109
vscode.commands.registerCommand("excalidraw.updateTheme", updateTheme)
95110
);
@@ -118,6 +133,6 @@ export function registerCommands(context: vscode.ExtensionContext) {
118133
)
119134
);
120135
context.subscriptions.push(
121-
vscode.commands.registerCommand("excalidraw.preventDefault", () => {})
136+
vscode.commands.registerCommand("excalidraw.preventDefault", () => { })
122137
);
123138
}

src/editor.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,16 @@ export class ExcalidrawEditorProvider
8484
uri: vscode.Uri,
8585
openContext: vscode.CustomDocumentOpenContext
8686
): Promise<ExcalidrawDocument> {
87-
const content = await vscode.workspace.fs.readFile(
88-
openContext.backupId ? vscode.Uri.parse(openContext.backupId) : uri
89-
);
87+
let content: Uint8Array;
88+
if (uri.scheme === "untitled") {
89+
content = new TextEncoder().encode(
90+
JSON.stringify({ type: "excalidraw", elements: [] })
91+
);
92+
} else {
93+
content = await vscode.workspace.fs.readFile(
94+
openContext.backupId ? vscode.Uri.parse(openContext.backupId) : uri
95+
);
96+
}
9097
const document = new ExcalidrawDocument(uri, content);
9198

9299
const onDidDocumentChange = document.onDidContentChange(() => {

src/utils.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as vscode from "vscode";
2+
import * as path from 'path';
3+
4+
export function getActiveWorkspace() {
5+
const activeEditor = vscode.window.activeTextEditor;
6+
if (activeEditor) {
7+
const doc = activeEditor.document;
8+
const ws = vscode.workspace.getWorkspaceFolder(doc.uri);
9+
return ws;
10+
}
11+
12+
const wsf = vscode.workspace.workspaceFolders;
13+
if (wsf && wsf.length > 0) {
14+
const ws = wsf[0];
15+
return ws;
16+
}
17+
return undefined;
18+
}
19+
20+
let runningCounter = 0;
21+
22+
export async function newUntitledExcalidrawDocument() {
23+
runningCounter += 1;
24+
const ws = getActiveWorkspace();
25+
let fileName = `Untitled-${runningCounter}.excalidraw`;
26+
if (ws) {
27+
fileName = path.join(ws.uri.fsPath, fileName);
28+
}
29+
const uri = vscode.Uri.parse(`untitled:${fileName}`);
30+
await vscode.commands.executeCommand(
31+
"vscode.openWith",
32+
uri,
33+
"editor.excalidraw"
34+
);
35+
}

0 commit comments

Comments
 (0)