Skip to content

Commit 8d5e9a0

Browse files
author
Ben Preston
committed
Initial Commit
0 parents  commit 8d5e9a0

16 files changed

+2786
-0
lines changed

.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# top-most EditorConfig file
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf
7+
insert_final_newline = true
8+
indent_style = tab
9+
indent_size = 4
10+
tab_width = 4

.eslintignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
3+
main.js

.eslintrc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"root": true,
3+
"parser": "@typescript-eslint/parser",
4+
"env": { "node": true },
5+
"plugins": [
6+
"@typescript-eslint"
7+
],
8+
"extends": [
9+
"eslint:recommended",
10+
"plugin:@typescript-eslint/eslint-recommended",
11+
"plugin:@typescript-eslint/recommended"
12+
],
13+
"parserOptions": {
14+
"sourceType": "module"
15+
},
16+
"rules": {
17+
"no-unused-vars": "off",
18+
"@typescript-eslint/no-unused-vars": ["error", { "args": "none" }],
19+
"@typescript-eslint/ban-ts-comment": "off",
20+
"no-prototype-builtins": "off",
21+
"@typescript-eslint/no-empty-function": "off"
22+
}
23+
}

.gitignore

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
# vscode
3+
.vscode
4+
5+
# used with hot-reload plugin
6+
.hotreload
7+
8+
# Intellij
9+
*.iml
10+
.idea
11+
12+
# npm
13+
node_modules
14+
15+
# Don't include the compiled main.js file in the repo.
16+
# They should be uploaded to GitHub releases instead.
17+
main.js
18+
19+
# Exclude sourcemaps
20+
*.map
21+
22+
# obsidian
23+
data.json
24+
25+
# Exclude macOS Finder (System Explorer) View States
26+
.DS_Store

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tag-version-prefix=""

README.md

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Obsidian Sample Plugin
2+
3+
This is a sample plugin for Obsidian (https://obsidian.md).
4+
5+
This project uses Typescript to provide type checking and documentation.
6+
The repo depends on the latest plugin API (obsidian.d.ts) in Typescript Definition format, which contains TSDoc comments describing what it does.
7+
8+
**Note:** The Obsidian API is still in early alpha and is subject to change at any time!
9+
10+
This sample plugin demonstrates some of the basic functionality the plugin API can do.
11+
- Adds a ribbon icon, which shows a Notice when clicked.
12+
- Adds a command "Open Sample Modal" which opens a Modal.
13+
- Adds a plugin setting tab to the settings page.
14+
- Registers a global click event and output 'click' to the console.
15+
- Registers a global interval which logs 'setInterval' to the console.
16+
17+
## First time developing plugins?
18+
19+
Quick starting guide for new plugin devs:
20+
21+
- Check if [someone already developed a plugin for what you want](https://obsidian.md/plugins)! There might be an existing plugin similar enough that you can partner up with.
22+
- Make a copy of this repo as a template with the "Use this template" button (login to GitHub if you don't see it).
23+
- Clone your repo to a local development folder. For convenience, you can place this folder in your `.obsidian/plugins/your-plugin-name` folder.
24+
- Install NodeJS, then run `npm i` in the command line under your repo folder.
25+
- Run `npm run dev` to compile your plugin from `main.ts` to `main.js`.
26+
- Make changes to `main.ts` (or create new `.ts` files). Those changes should be automatically compiled into `main.js`.
27+
- Reload Obsidian to load the new version of your plugin.
28+
- Enable plugin in settings window.
29+
- For updates to the Obsidian API run `npm update` in the command line under your repo folder.
30+
31+
## Releasing new releases
32+
33+
- Update your `manifest.json` with your new version number, such as `1.0.1`, and the minimum Obsidian version required for your latest release.
34+
- Update your `versions.json` file with `"new-plugin-version": "minimum-obsidian-version"` so older versions of Obsidian can download an older version of your plugin that's compatible.
35+
- Create new GitHub release using your new version number as the "Tag version". Use the exact version number, don't include a prefix `v`. See here for an example: https://github.com/obsidianmd/obsidian-sample-plugin/releases
36+
- Upload the files `manifest.json`, `main.js`, `styles.css` as binary attachments. Note: The manifest.json file must be in two places, first the root path of your repository and also in the release.
37+
- Publish the release.
38+
39+
> You can simplify the version bump process by running `npm version patch`, `npm version minor` or `npm version major` after updating `minAppVersion` manually in `manifest.json`.
40+
> The command will bump version in `manifest.json` and `package.json`, and add the entry for the new version to `versions.json`
41+
42+
## Adding your plugin to the community plugin list
43+
44+
- Check https://github.com/obsidianmd/obsidian-releases/blob/master/plugin-review.md
45+
- Publish an initial version.
46+
- Make sure you have a `README.md` file in the root of your repo.
47+
- Make a pull request at https://github.com/obsidianmd/obsidian-releases to add your plugin.
48+
49+
## How to use
50+
51+
- Clone this repo.
52+
- `npm i` or `yarn` to install dependencies
53+
- `npm run dev` to start compilation in watch mode.
54+
55+
## Manually installing the plugin
56+
57+
- Copy over `main.js`, `styles.css`, `manifest.json` to your vault `VaultFolder/.obsidian/plugins/your-plugin-id/`.
58+
59+
## Improve code quality with eslint (optional)
60+
- [ESLint](https://eslint.org/) is a tool that analyzes your code to quickly find problems. You can run ESLint against your plugin to find common bugs and ways to improve your code.
61+
- To use eslint with this project, make sure to install eslint from terminal:
62+
- `npm install -g eslint`
63+
- To use eslint to analyze this project use this command:
64+
- `eslint main.ts`
65+
- eslint will then create a report with suggestions for code improvement by file and line number.
66+
- If your source code is in a folder, such as `src`, you can use eslint with this command to analyze all files in that folder:
67+
- `eslint .\src\`
68+
69+
## Funding URL
70+
71+
You can include funding URLs where people who use your plugin can financially support it.
72+
73+
The simple way is to set the `fundingUrl` field to your link in your `manifest.json` file:
74+
75+
```json
76+
{
77+
"fundingUrl": "https://buymeacoffee.com"
78+
}
79+
```
80+
81+
If you have multiple URLs, you can also do:
82+
83+
```json
84+
{
85+
"fundingUrl": {
86+
"Buy Me a Coffee": "https://buymeacoffee.com",
87+
"GitHub Sponsor": "https://github.com/sponsors",
88+
"Patreon": "https://www.patreon.com/"
89+
}
90+
}
91+
```
92+
93+
## API Documentation
94+
95+
See https://github.com/obsidianmd/obsidian-api

esbuild.config.mjs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import esbuild from "esbuild";
2+
import process from "process";
3+
import builtins from "builtin-modules";
4+
5+
const banner =
6+
`/*
7+
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
8+
if you want to view the source, please visit the github repository of this plugin
9+
*/
10+
`;
11+
12+
const prod = (process.argv[2] === "production");
13+
14+
const context = await esbuild.context({
15+
banner: {
16+
js: banner,
17+
},
18+
entryPoints: ["main.ts"],
19+
bundle: true,
20+
external: [
21+
"obsidian",
22+
"electron",
23+
"@codemirror/autocomplete",
24+
"@codemirror/collab",
25+
"@codemirror/commands",
26+
"@codemirror/language",
27+
"@codemirror/lint",
28+
"@codemirror/search",
29+
"@codemirror/state",
30+
"@codemirror/view",
31+
"@lezer/common",
32+
"@lezer/highlight",
33+
"@lezer/lr",
34+
...builtins],
35+
format: "cjs",
36+
target: "es2018",
37+
logLevel: "info",
38+
sourcemap: prod ? false : "inline",
39+
treeShaking: true,
40+
outfile: "main.js",
41+
});
42+
43+
if (prod) {
44+
await context.rebuild();
45+
process.exit(0);
46+
} else {
47+
await context.watch();
48+
}

main.ts

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { App, Editor, MarkdownView, Modal, Notice, Plugin } from 'obsidian';
2+
import { AutoCanvasSettingsTab, AutoCanvasPluginSettings, DEFAULT_SETTINGS, FolderCanvasMap } from "./settings";
3+
const fs = require('fs');
4+
var path = require('path');
5+
6+
export default class AutoCanvasPlugin extends Plugin {
7+
settings: AutoCanvasPluginSettings;
8+
9+
async onload() {
10+
await this.loadSettings();
11+
12+
// This adds a settings tab so the user can configure various aspects of the plugin
13+
this.addSettingTab(new AutoCanvasSettingsTab(this.app, this));
14+
15+
this.registerEvent(
16+
this.app.vault.on("create", (page) => {
17+
18+
let targetCanvas = this.tryAddMatch(page);
19+
if (targetCanvas)
20+
this.openTargetCanvas(targetCanvas)
21+
})
22+
)
23+
}
24+
25+
26+
tryAddMatch(page) {
27+
28+
const folderPath = page.path.substring(0, page.path.lastIndexOf("/") + 1);
29+
for(const [key, val] of Object.entries(this.settings.folderCanvasMaps))
30+
{
31+
let map = val as FolderCanvasMap;
32+
if (map.folder == folderPath)
33+
{
34+
this.tryAddPageToCanvas(page, map.canvas);
35+
return map.canvas;
36+
}
37+
}
38+
39+
return null;
40+
}
41+
42+
43+
openTargetCanvas(canvasPath) {
44+
45+
if (this.settings.openTargetCanvasOnNewItem == false)
46+
return;
47+
48+
let activeLeaf;
49+
try { activeLeaf = this.app.workspace.getLeaf(); }
50+
catch { return; }
51+
if (!activeLeaf) { return; }
52+
53+
setTimeout( async () => {
54+
try {
55+
let pageData = this.app.vault.fileMap[canvasPath];
56+
57+
if (!pageData)
58+
return;
59+
60+
await activeLeaf.openFile(pageData);
61+
activeLeaf.setEphemeralState({ rename: 'all' });
62+
} catch (error)
63+
{
64+
console.log(error);
65+
}
66+
67+
}, 1500);
68+
}
69+
70+
71+
tryAddPageToCanvas(page: any, canvas: string) {
72+
73+
try {
74+
75+
let newNodeObj = {
76+
"type":"file",
77+
"file": page.path,
78+
"id": Math.random() * Date.now(),
79+
"x": 0,
80+
"y": 0,
81+
"width": 260,
82+
"height": 380
83+
}
84+
85+
let fullPath = path.join(page.vault.adapter.basePath, canvas);
86+
87+
fs.readFile(fullPath, 'utf8', (err, data) => {
88+
89+
if (err){ console.log(err);}
90+
if (!data) return;
91+
92+
let allObjs = JSON.parse(data);
93+
allObjs.nodes.push(newNodeObj);
94+
console.log(fullPath);
95+
fs.writeFile(fullPath, JSON.stringify(allObjs), (err2) => {
96+
if (err2)
97+
console.log(err2);
98+
})
99+
100+
return;
101+
})
102+
103+
104+
return;
105+
106+
} catch (error){
107+
console.log(error);
108+
}
109+
}
110+
111+
112+
onunload() {
113+
114+
}
115+
116+
async loadSettings() {
117+
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
118+
}
119+
120+
async saveSettings() {
121+
await this.saveData(this.settings);
122+
}
123+
}
124+
125+
126+
127+

manifest.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"id": "obsidian-auto-canvas",
3+
"name": "Auto Canvas",
4+
"version": "1.0.0",
5+
"minAppVersion": "0.15.0",
6+
"description": "Automatically adds items to a canvas when created in a given folder. Supports a one-to-one mapping from folder to canvas.",
7+
"author": "Obsidian",
8+
"authorUrl": "https://github.com/prestsauce",
9+
"fundingUrl": "",
10+
"isDesktopOnly": true
11+
}

0 commit comments

Comments
 (0)