Skip to content

Commit cd53076

Browse files
authored
Merge pull request #63 from philoserf/add-test-vault
refactor: move styles to external CSS file and add test-vault
2 parents 481c2b8 + 0894a6b commit cd53076

File tree

13 files changed

+124
-49
lines changed

13 files changed

+124
-49
lines changed

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ updates:
77
- package-ecosystem: "github-actions"
88
directory: "/"
99
schedule:
10-
interval: "daily"
10+
interval: "daily"

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
main.js
22
main.js.map
33
node_modules/
4+
test-vault/.obsidian/plugins/obsidian-vault-changelog/
5+
!test-vault/.obsidian/plugins/journals/.hotreload
6+
test-vault/.obsidian/plugins/*
7+
!test-vault/.obsidian/plugins/hotreload/main.js
8+
!test-vault/.obsidian/plugins/hotreload/manifest.json
9+
test-vault/.obsidian/app.json
10+
test-vault/.obsidian/appearance.json
11+
test-vault/.obsidian/core-plugins-migration.json
12+
test-vault/.obsidian/core-plugins.json
13+
test-vault/.obsidian/workspace.json
14+
test-vault/**/*.md
15+
!test-vault/Change.md
16+
!test-vault/Changelog.md
17+
!test-vault/Delete.md
18+
!test-vault/Home.md

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,18 @@ Practice a **"Yes, and"** worldview.
9292
1. Clone this repository `gh repo clone philoserf/obsidian-vault-changelog`.
9393
2. Install dependencies with `npm install`.
9494
3. Build the project with `npm run build`.
95-
4. Copy `manifest.json` and `main.js` into your **Obsidian plugins folder** (`.obsidian/plugins/obsidian-vault-changelog`).
96-
5. Reload Obsidian and enable the plugin.
95+
96+
### Using the test-vault
97+
98+
The project includes a test-vault for development:
99+
100+
1. Open the test-vault in Obsidian (`File > Open another vault > Open folder as vault` and select the `test-vault` directory)
101+
2. Build the plugin with hot-reload: `npm run dev`
102+
3. Changes will automatically be applied to the plugin in the test-vault
103+
104+
### Manual installation
105+
106+
Alternatively, you can manually install the plugin in your own vault:
107+
108+
1. Copy `manifest.json` and `main.js` into your **Obsidian plugins folder** (`.obsidian/plugins/obsidian-vault-changelog`).
109+
2. Reload Obsidian and enable the plugin.

esbuild.config.mjs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
import esbuild from "esbuild";
2+
import { existsSync, mkdirSync } from "fs";
3+
import { join } from "path";
24

35
const production = process.argv[2] === "production";
46

7+
// Setup output paths
8+
const outDir = production
9+
? "."
10+
: "test-vault/.obsidian/plugins/obsidian-vault-changelog";
11+
// Ensure the directory exists
12+
if (!existsSync(outDir)) {
13+
mkdirSync(outDir, { recursive: true });
14+
}
15+
16+
// Copy manifest.json and styles.css to output directory in dev mode
17+
if (!production) {
18+
const { copyFileSync } = await import("fs");
19+
copyFileSync("manifest.json", join(outDir, "manifest.json"));
20+
copyFileSync("styles.css", join(outDir, "styles.css"));
21+
}
22+
523
const context = await esbuild.context({
624
entryPoints: ["src/main.ts"],
725
bundle: true,
826
external: ["obsidian"],
927
format: "cjs",
1028
target: "es2018",
11-
outfile: "main.js",
29+
outfile: join(outDir, "main.js"),
1230
sourcemap: !production,
1331
minify: production,
1432
});

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,8 @@ import {
66
DEFAULT_SETTINGS,
77
} from "./settings";
88

9-
// Add styles for the excluded folders list
10-
// TODO: Move this to a styles.css file
11-
const EXCLUDED_FOLDERS_STYLES = `
12-
.excluded-folders-list {
13-
margin-bottom: 1em;
14-
}
15-
16-
.excluded-folder-item {
17-
display: flex;
18-
justify-content: space-between;
19-
align-items: center;
20-
background-color: var(--background-secondary);
21-
border-radius: 4px;
22-
padding: 4px 8px;
23-
margin-bottom: 6px;
24-
}
25-
26-
.excluded-folder-remove {
27-
cursor: pointer;
28-
border: none;
29-
background: transparent;
30-
color: var(--text-muted);
31-
padding: 0 4px;
32-
font-size: 14px;
33-
}
34-
35-
.excluded-folder-remove:hover {
36-
color: var(--text-error);
37-
}`;
38-
399
export default class ChangelogPlugin extends Plugin {
4010
settings: ChangelogSettings = DEFAULT_SETTINGS;
41-
private styleEl: HTMLStyleElement;
4211

4312
async onload() {
4413
await this.loadSettings();
@@ -50,20 +19,28 @@ export default class ChangelogPlugin extends Plugin {
5019
callback: () => this.updateChangelog(),
5120
});
5221

53-
// Add styles for the excluded folders feature
54-
this.styleEl = document.createElement("style");
55-
this.styleEl.textContent = EXCLUDED_FOLDERS_STYLES;
56-
document.head.appendChild(this.styleEl);
22+
this.loadStyles();
5723

5824
this.onVaultChange = debounce(this.onVaultChange.bind(this), 200);
5925
this.enableAutoUpdate();
6026
}
6127

6228
onunload() {
63-
// Remove styles when plugin is disabled
64-
if (this.styleEl && this.styleEl.parentNode) {
65-
this.styleEl.parentNode.removeChild(this.styleEl);
66-
}
29+
// Cleanup happens automatically
30+
}
31+
32+
async loadStyles() {
33+
const cssFile = await this.app.vault.adapter.read(
34+
this.manifest.dir + "/styles.css",
35+
);
36+
this.registerStyles(cssFile);
37+
}
38+
39+
registerStyles(cssText: string) {
40+
const styleEl = document.createElement("style");
41+
styleEl.textContent = cssText;
42+
this.register(() => styleEl.remove());
43+
document.head.appendChild(styleEl);
6744
}
6845

6946
enableAutoUpdate() {

styles.css

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.excluded-folders-list {
2+
margin-bottom: 1em;
3+
}
4+
5+
.excluded-folder-item {
6+
display: flex;
7+
justify-content: space-between;
8+
align-items: center;
9+
background-color: var(--background-secondary);
10+
border-radius: 4px;
11+
padding: 4px 8px;
12+
margin-bottom: 6px;
13+
}
14+
15+
.excluded-folder-remove {
16+
cursor: pointer;
17+
border: none;
18+
background: transparent;
19+
color: var(--text-muted);
20+
padding: 0 4px;
21+
font-size: 14px;
22+
}
23+
24+
.excluded-folder-remove:hover {
25+
color: var(--text-error);
26+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
"hot-reload",
3+
"obsidian-vault-changelog"
4+
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"id": "hot-reload",
3+
"name": "Hot Reload",
4+
"author": "PJ Eby",
5+
"authorUrl": "https://github.com/pjeby",
6+
"version": "0.1.10",
7+
"minAppVersion": "0.15.9",
8+
"description": "Automatically reload in-development plugins when their files are changed",
9+
"isDesktopOnly": true
10+
}

test-vault/Change.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
change me

0 commit comments

Comments
 (0)