Skip to content

Commit

Permalink
rework directory to be relative to file (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
hall authored Feb 24, 2023
1 parent 045676e commit ea15ec8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 25 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

This release reworks the `draw.directory` setting to behave like a more standard filepath. Specifically, it's now relative to the current file (unless the path is absolute).

To use the previous default behavior of inlining the svg content, set it to `null`.

To make the path relative to workspace root, use the `workspaceFolder` variable (e.g., `${workspaceFolder}/assets`).

### Added

- allow using the `${workspaceFolder}` variable in the `draw.directory` setting

### Changed

- **BREAKING**: `draw.directory` is now relative to the current file if an absolute path is not given
- **BREAKING**: to save files inline, `draw.directory` now must be `null` instead of an empty string
- rename main command title from `Edit Current Line` to `Edit Drawing` for clarity

## [0.1.21] - 2023-02-04
Expand Down
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,19 @@ Consider binding the main command (perhaps to a stylus, if available). For examp

The following settings are available (prefixed with `draw`).

| setting | description | default |
| --------- | --------------------------------------------------------------------- | ------- |
| directory | if set, save files to this directory (relative to the workspace root) | `""` |
| buttons | add [custom buttons](#custom-buttons) to the toolbar | `[]` |
| setting | description | default | example |
| ----------- | ---------------------------------------------------- | ------- | --------------------------- |
| `directory` | where to save files | `""` | `${workSpaceFolder}/assets` |
| `buttons` | add [custom buttons](#custom-buttons) to the toolbar | `[]` | _see below_ |

### directory

To save contents inline, set `draw.directory` to `null`.

The following [variables](https://code.visualstudio.com/docs/editor/variables-reference) are currently supported:

- `workspaceFolder`


### custom buttons

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@
}
},
"draw.directory": {
"type": "string",
"type": ["string", "null"],
"default": "",
"description": "Save to directory (write inline if blank)"
"description": "Save to directory (write inline if null)"
}
}
}
Expand Down
17 changes: 7 additions & 10 deletions src/draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ export class Draw {
this.target = target;
this.setState();

if (Draw.settings.directory) {
if (target.text.startsWith("<svg")) {
// text is probably an svg element
if (this.target.editor && (this.check[0] !== this.check[1]) && (target.text === this.check[0])) {
if (push) Draw.panel?.webview.postMessage({ command: 'currentLine', content: target.text });
}
} else {
const link = langs.readLink(this.target.editor?.document.languageId || "markdown", target.text);
const paths = this.target.editor?.document.uri.path.split("/");
if (link && paths) {
Expand All @@ -199,15 +204,7 @@ export class Draw {
if (push) Draw.panel?.webview.postMessage({ command: 'currentLine', content: target.text });
});
}
} else {
// text is probably an svg element
if (target.text.startsWith("<svg")) {

if (this.target.editor && (this.check[0] !== this.check[1]) && (target.text === this.check[0])) {
if (push) Draw.panel?.webview.postMessage({ command: 'currentLine', content: target.text });
}

}
}
}, 100);
}
Expand All @@ -226,7 +223,7 @@ export class Draw {
}

// if a directory is set, and current line is not latex, replace the text with a link
if (Draw.settings.directory && Draw.settings.directory !== "" && !text.startsWith("$$")) {
if (Draw.settings.directory !== null && !text.startsWith("$$")) {
text = langs.createLink(this.target.editor, text);
}

Expand Down
28 changes: 19 additions & 9 deletions src/langs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,25 @@ export function createLink(editor: vscode.TextEditor, text: string): string {
let uri = vscode.Uri.file(`${uuidv4()}.svg`);
let alt = "";

let settings = Draw.settings.directory;

// prepend workspace folder to settings directory
const ws = vscode.workspace.getWorkspaceFolder(editor.document.uri);
settings = vscode.Uri.joinPath(ws?.uri || vscode.Uri.file(""), settings).path;
// create variables for replacement
const workspaceFolder = vscode.workspace.getWorkspaceFolder(editor.document.uri)?.uri.fsPath;

// replace supported variables
let directory = Draw.settings.directory.replace('${workspaceFolder}', workspaceFolder)

// if not an absolute path, prepend current directory
if (!directory.startsWith("/")) {
// get path to current file
const fsPath = editor.document.uri.fsPath;
// get directory of said file
const fileDirname = fsPath.slice(0, fsPath.lastIndexOf("/"));
// prepend directory to setting
directory = vscode.Uri.joinPath(vscode.Uri.file(fileDirname) || vscode.Uri.file(""), directory).path;
}

// create the directory, if necessary
if (!vscode.Uri.file(settings)) {
vscode.workspace.fs.createDirectory(vscode.Uri.file(settings));
if (!vscode.Uri.file(directory)) {
vscode.workspace.fs.createDirectory(vscode.Uri.file(directory));
}

// reuse existing alt and filename, if available
Expand All @@ -34,10 +44,10 @@ export function createLink(editor: vscode.TextEditor, text: string): string {
}

// write contents to absolute path {settings}/{filename}
vscode.workspace.fs.writeFile(vscode.Uri.joinPath(vscode.Uri.file(settings), uri.path), Buffer.from(text));
vscode.workspace.fs.writeFile(vscode.Uri.joinPath(vscode.Uri.file(directory), uri.path), Buffer.from(text));

// prepend absolute path to settings directory to filename
uri = vscode.Uri.joinPath(vscode.Uri.file(settings), uri.path);
uri = vscode.Uri.joinPath(vscode.Uri.file(directory), uri.path);

// get relative path from editor to settings directory
const filename = scoped(editor.document.uri, uri).fsPath.substring(1);
Expand Down

0 comments on commit ea15ec8

Please sign in to comment.