Skip to content

Commit

Permalink
Implement plaintext editing
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarenholz committed Sep 2, 2021
1 parent 5fbf3a9 commit f133d68
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 100 deletions.
37 changes: 12 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
## Plaintext for Obsidian
# Plaintext for Obsidian

Obisidan (https://obsidian.md) plugin that allows it to open files as plaintext.
Developed for Obsidian v0.12.12, might work with older versions, but not tested.
Developed for Obsidian version 0.12.12. It might work with older versions, but it is not tested.

Code is really ugly, and a complete work in progress. Do not expect this to be functional.
Code is functional! You can actually edit plaintext files now. There's no fancy syntax highlighting, since the file
is interpreted as plaintext, as opposed to some other type of text.

All text below is left-over from the sample repository for now.
## Changelog

<hline>
**Version 0.0.2 (current)**:
* First actual release.
* Code is functional! You can open and edit files as plaintext.

### Releasing new releases

- Update your `manifest.json` with your new version number, such as `1.0.1`, and the minimum Obsidian version required for your latest release.
- 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.
- 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
- Upload the files `manifest.json`, `main.js`, `styles.css` as binary attachments.
- Publish the release.

### Adding your plugin to the community plugin list

- Publish an initial version.
- Make sure you have a `README.md` file in the root of your repo.
- Make a pull request at https://github.com/obsidianmd/obsidian-releases to add your plugin.

### Manually installing the plugin

- Copy over `main.js`, `styles.css`, `manifest.json` to your vault `VaultFolder/.obsidian/plugins/your-plugin-id/`.

### API Documentation

See https://github.com/obsidianmd/obsidian-api
**Version 0.0.1**:
* Not a release.
* Initial testing code.
* This included the functionality for parsing user-inputted extensions.
6 changes: 3 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"id": "obsidian-plaintext",
"name": "Obsidian Plaintext",
"version": "0.0.1",
"name": "Plaintext",
"version": "0.0.2",
"minAppVersion": "0.12.12",
"description": "Plugin for Obsidian (https://obsidian.md) to allow opening files as plaintext.",
"description": "Allow opening specified files as plaintext.",
"author": "dbarenholz",
"authorUrl": "https://github.com/dbarenholz/dbarenholz",
"isDesktopOnly": true
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "obsidian-plaintext",
"version": "0.0.1",
"description": "Plugin for Obsidian (https://obsidian.md) to allow opening files as plaintext.",
"version": "0.0.2",
"description": "Allow opening specified files as plaintext.",
"main": "main.js",
"scripts": {
"dev": "rollup --config rollup.config.js -w",
Expand All @@ -19,5 +19,8 @@
"rollup": "^2.32.1",
"tslib": "^2.2.0",
"typescript": "^4.2.4"
},
"dependencies": {
"codemirror": "^5.62.3"
}
}
}
1 change: 0 additions & 1 deletion src/constants.ts

This file was deleted.

24 changes: 24 additions & 0 deletions src/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Taken directly from here
// https://help.obsidian.md/Advanced+topics/Accepted+file+formats
// On sept 2, 2021. May need updates later.

export const obsidianExts: string[] = [
"md",
" png",
" jpg",
" jpeg",
" gif",
" bmp",
" svg",
" mp3",
" webm",
" wav",
" m4a",
" ogg",
" 3gp",
" flac",
" mp4",
" webm",
" ogv",
" pdf",
];
114 changes: 78 additions & 36 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,105 @@
// TODO: Do I need a custom view type?
// import { VIEW_TYPE_PLAINTEXT } from "constants";
import { Plugin, WorkspaceLeaf } from "obsidian";
import { Plugin, WorkspaceLeaf, ViewCreator } from "obsidian";
import { obsidianExts } from "./helper";
import { PlaintextSettings, PlaintextSettingTab, DEFAULT_SETTINGS } from "./settings";
import PlaintextView from "./view";

/**
* The plugin class, extends Obsidian Plugin.
* Plaintext plugin.
*
* This plugin allows you to edit specified extensions as plaintext files.
* It does NOT check if a file is binary or textual!
*
* @author dbarenholz
* @version 0.1.0
*/
export default class PlaintextPlugin extends Plugin {
public settings: PlaintextSettings;
private view: PlaintextView;

async onload() {
/**
* Code that runs (once) when plugin is loaded.
*/
async onload(): Promise<void> {
console.log("Obsidian Plaintext: loaded plugin.");

// Load the settings
await this.loadSettings();

// Add settings tab
this.addSettingTab(new PlaintextSettingTab(this.app, this));

// Adds an item to the status bar (bottom row of the screen)
// TODO: When editing a file in plaintext mode, add item stating "editing plaintext"
// this.addStatusBarItem().setText("Plain Text");

// Stuff for editor
// this.registerCodeMirror((cm: CodeMirror.Editor) => {
// console.log("codemirror", cm);
// });

// TODO: Do I need this? If so, how does it work?
// this.registerView(VIEW_TYPE_PLAINTEXT, (leaf: WorkspaceLeaf) => (this.view = new PlaintextView(leaf)));

// Relevant items have class: nav-file-title is-unsupported
const items = document.getElementsByClassName("nav-file-title is-unsupported");
const numItems = items.length;
for (let i = 0; i < numItems; i++) {
const thing = items.item(i) as HTMLElement;
this.registerDomEvent(thing, "click", (evt: MouseEvent) => {
// TODO: Test if this prevents defaukt behaviour
evt.stopImmediatePropagation();
// TODO: Open the actual file
console.log("TODO: Open the file.");

// TODO: Do I need a custom leaf type?
this.app.workspace.createLeafBySplit(null, "vertical", false);
});
}
// Do the work
this.processExts(this.settings.extensions);
}

onunload() {
/**
* Code that runs (once) when the plugin is unloaded.
*/
onunload(): void {
console.log("Obsidian Plaintext: unloaded plugin.");
}

async loadSettings() {
/**
* Loads the settings.
*/
async loadSettings(): Promise<void> {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

async saveSettings() {
/**
* Saves the settings.
*/
async saveSettings(): Promise<void> {
await this.saveData(this.settings);
}

/**
* Creates a view for a plaintext file.
* Plaintext views have <b>NO</b> syntax highlighting or other fancy features!
*
* @param leaf The leaf to create the view at
* @param ext Plaintext extension
* @returns Plaintext view
*/
viewCreator: ViewCreator = (leaf: WorkspaceLeaf, ext?: string): PlaintextView => {
return new PlaintextView(leaf, ext);
};

/**
* Processes the extensions.
*
* @param exts extensions
*/
processExts = (exts: string[]): void => {
if (exts.length == 0) {
console.log("Plaintext: No extensions to process.");
return;
}

for (const ext of exts) {
// Disallow using obsidian defaults
if (ext in obsidianExts) {
if (this.settings.debug) {
console.log(`Plaintext: Extension '${ext}' is used by Obsidian already! Don't override Obsidian.`);
}
}

// Try to register view
try {
this.registerView(ext, this.viewCreator);
} catch {
if (this.settings.debug) {
console.log(`Plaintext: Extension '${ext}' already has a view registered, ignoring...`);
}
}

// Try to register extension
try {
this.registerExtensions([ext], ext);
} catch {
if (this.settings.debug) {
console.log(`Plaintext: Extension '${ext}' is already registered, ignoring...`);
}
}
}
};
}
5 changes: 0 additions & 5 deletions src/plaintextLeaf.ts

This file was deleted.

24 changes: 7 additions & 17 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const DEFAULT_SETTINGS: PlaintextSettings = {
/**
* The settings tab itself.
*
* @version 0.0.1
* @version 0.0.2
* @author dbarenholz
*/
export class PlaintextSettingTab extends PluginSettingTab {
Expand All @@ -47,7 +47,7 @@ export class PlaintextSettingTab extends PluginSettingTab {

containerEl.empty();

containerEl.createEl("h2", { text: "Obsidian Plaintext" });
containerEl.createEl("h2", { text: "Plaintext" });

// Debug settings
new Setting(containerEl)
Expand All @@ -64,9 +64,9 @@ export class PlaintextSettingTab extends PluginSettingTab {

// Extension settings
new Setting(containerEl)
.setName("Plaintext Extensions")
.setName("Extensions")
.setDesc(
"List of extensions to interpret as plaintext, comma-separated. Will automatically convert to a set when reopening the Obsidian Plaintext settings window"
"List of extensions to interpret as plaintext, comma-separated. Will automatically convert to a set when reopening the Obsidian Plaintext settings window. Obsidian default extensions are filtered out!"
)
.addText((text) => {
text
Expand Down Expand Up @@ -154,20 +154,10 @@ export class PlaintextSettingTab extends PluginSettingTab {
// Save settings
this.plugin.settings.extensions = current_exts;
await this.plugin.saveSettings();

// Do the work
this.plugin.processExts(this.plugin.settings.extensions);
};
});
// .addButton((button) => {
// button
// .setButtonText("Update")
// .setTooltip("Updates the extensions")
// .onClick(async () => {
// // If changes are null, do nothing
// if (this.changes == null && this.plugin.settings.debug) {
// console.log("Obsidian Plaintext: No changes made.");
// await this.plugin.saveSettings();
// return;
// }

// });
}
}
Loading

0 comments on commit f133d68

Please sign in to comment.