Skip to content

Commit

Permalink
Remove almost all of the TypeScript errors.
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan McQuen <rpcm@linux.com>
  • Loading branch information
ryanpcmcquen committed Jun 6, 2021
1 parent ca295fd commit 67ca3c1
Show file tree
Hide file tree
Showing 10 changed files with 1,977 additions and 168 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
## Obsidian Org Mode

Add Org Mode support to Obsidian.

<img width="502" alt="Screen Shot 2021-05-14 at 10 08 41 AM" src="https://user-images.githubusercontent.com/772937/118305097-65c15900-b49c-11eb-8437-b9f5da3dad75.png">

### Manually installing the plugin

- Copy over `main.js`, `styles.css`, `manifest.json` to your vault `VaultFolder/.obsidian/plugins/obsidian-org-mode/`.
## Obsidian Org Mode

Add Org Mode support to Obsidian.

<img width="502" alt="Screen Shot 2021-05-14 at 10 08 41 AM" src="https://user-images.githubusercontent.com/772937/118305097-65c15900-b49c-11eb-8437-b9f5da3dad75.png">

### Manually installing the plugin

- Copy over `main.js`, `styles.css`, `manifest.json` to your vault `VaultFolder/.obsidian/plugins/obsidian-org-mode/`.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-org-mode",
"name": "Org Mode",
"version": "1.3.0",
"version": "1.3.1",
"minAppVersion": "0.10.12",
"description": "Add Org Mode support to Obsidian.",
"author": "ryanpcmcquen",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-org-mode",
"version": "1.3.0",
"version": "1.3.1",
"description": "Add Org Mode support to Obsidian.",
"main": "main.js",
"repository": "https://github.com/ryanpcmcquen/obsidian-org-mode",
Expand Down
50 changes: 25 additions & 25 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import typescript from '@rollup/plugin-typescript';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';

const isProd = process.env.BUILD === 'production';

const banner = `/*
THIS IS A GENERATED/BUNDLED FILE BY ROLLUP
if you want to view the source visit the plugins github repository
*/
`;

export default {
input: 'src/main.ts',
output: {
dir: '.',
sourcemap: 'inline',
sourcemapExcludeSources: isProd,
format: 'cjs',
exports: 'default',
banner,
},
external: ['obsidian'],
plugins: [typescript(), nodeResolve({ browser: true }), commonjs()],
};
import typescript from "@rollup/plugin-typescript";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";

const isProd = process.env.BUILD === "production";

const banner = `/*
THIS IS A GENERATED/BUNDLED FILE BY ROLLUP
if you want to view the source visit the plugins github repository
*/
`;

export default {
input: "src/main.ts",
output: {
dir: ".",
sourcemap: "inline",
sourcemapExcludeSources: isProd,
format: "cjs",
exports: "default",
banner,
},
external: ["obsidian"],
plugins: [typescript(), nodeResolve({ browser: true }), commonjs()],
};
1,813 changes: 1,813 additions & 0 deletions src/lib/codemirror.d.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/lib/codemirror.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = CodeMirror;
export default CodeMirror;
224 changes: 108 additions & 116 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,116 +1,108 @@
import "./lib/codemirror.js";
import "./mode/simple/simple.js";
import "./mode/orgmode/orgmode-fold.js";
import "./mode/orgmode/orgmode-mode.js";
import { Plugin, TextFileView, WorkspaceLeaf } from "obsidian";

export default class OrgMode extends Plugin {
async onload() {
super.onload();
console.log("Loading Org Mode plugin ...");

this.registerView("orgmode", this.orgViewCreator);
this.registerExtensions(["org"], "orgmode");
}

orgViewCreator = (leaf: WorkspaceLeaf) => {
return new OrgView(leaf);
};

onunload() {
console.log("Unloading Org Mode plugin ...");
}
}

// This is the custom view:
class OrgView extends TextFileView {
// Internal code mirror instance:
codeMirror: CodeMirror.Editor;

// this.contentEl is not exposed, so cheat a bit.
public get extContentEl(): HTMLElement {
// @ts-ignore
return this.contentEl;
}

constructor(leaf: WorkspaceLeaf) {
super(leaf);
// @ts-ignore
this.codeMirror = CodeMirror(this.extContentEl, {
theme: "obsidian",

// I am not sure autofocus is necessary ...
autofocus: true,
foldGutter: {
minFoldSize: 1,
},
foldOptions: {
widget: " ...",
},
gutters: ["CodeMirror-foldgutter"],
});

this.codeMirror.on("changes", this.changed);
}

// When the view is resized, refresh CodeMirror (thanks Licat!).
onResize() {
this.codeMirror.refresh();
}

changed = async (
_instance: CodeMirror.Editor,
_changes: CodeMirror.EditorChangeLinkedList[]
) => {
this.requestSave();
};

getViewData = () => {
return this.codeMirror.getValue();
};

setViewData = (data: string, clear: boolean) => {
if (clear) {
// @ts-ignore
this.codeMirror.swapDoc(CodeMirror.Doc(data, "orgmode"));
} else {
this.codeMirror.setValue(data);
}

// HACK:
// Ideally this would actually be able to find the user defined
// keyMap, and not just check the 'vimMode' boolean.
// @ts-ignore
if (this.app?.vault?.config?.vimMode) {
this.codeMirror.setOption("keyMap", "vim");
}

// This seems to fix some odd visual bugs:
this.codeMirror.refresh();

// This focuses the editor, which is analogous to the
// default Markdown behavior in Obsidian:
this.codeMirror.focus();
};

clear = () => {
this.codeMirror.setValue("");
this.codeMirror.clearHistory();
};

getDisplayText() {
if (this.file) {
return this.file.basename;
} else {
return "org (No File)";
}
}

canAcceptExtension(extension: string) {
return extension === "org";
}

getViewType() {
return "orgmode";
}
}
import { Plugin, TextFileView, WorkspaceLeaf } from "obsidian";
import "./mode/simple/simple.js";
import "./mode/orgmode/orgmode-fold.js";
import "./mode/orgmode/orgmode-mode.js";
import CodeMirror from "./lib/codemirror";

export default class OrgMode extends Plugin {
async onload() {
super.onload();
console.log("Loading Org Mode plugin ...");

this.registerView("orgmode", this.orgViewCreator);
this.registerExtensions(["org"], "orgmode");
}

orgViewCreator = (leaf: WorkspaceLeaf) => {
return new OrgView(leaf);
};

onunload() {
console.log("Unloading Org Mode plugin ...");
}
}

// This is the custom view:
class OrgView extends TextFileView {
// Internal code mirror instance:
codeMirror: CodeMirror.Editor;

// this.contentEl is not exposed, so cheat a bit.
public get extContentEl(): HTMLElement {
return this.contentEl;
}

constructor(leaf: WorkspaceLeaf) {
super(leaf);

this.codeMirror = CodeMirror(this.extContentEl, {
theme: "obsidian",

// I am not sure autofocus is necessary ...
autofocus: true,
foldGutter: {
minFoldSize: 1,
},
foldOptions: {
widget: " ...",
},
gutters: ["CodeMirror-foldgutter"],
} as CodeMirror.EditorConfiguration);

this.codeMirror.on("changes", this.changed);
}

// When the view is resized, refresh CodeMirror (thanks Licat!).
onResize() {
this.codeMirror.refresh();
}

changed = async () => {
this.requestSave();
};

getViewData = () => {
return this.codeMirror.getValue();
};

setViewData = (data: string, clear: boolean) => {
if (clear) {
this.codeMirror.swapDoc(CodeMirror.Doc(data, "orgmode"));
} else {
this.codeMirror.setValue(data);
}

// @ts-ignore
if (this.app?.vault?.config?.vimMode) {
this.codeMirror.setOption("keyMap", "vim");
}

// This seems to fix some odd visual bugs:
this.codeMirror.refresh();

// This focuses the editor, which is analogous to the
// default Markdown behavior in Obsidian:
this.codeMirror.focus();
};

clear = () => {
this.codeMirror.setValue("");
this.codeMirror.clearHistory();
};

getDisplayText() {
if (this.file) {
return this.file.basename;
} else {
return "org (No File)";
}
}

canAcceptExtension(extension: string) {
return extension === "org";
}

getViewType() {
return "orgmode";
}
}
31 changes: 16 additions & 15 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"compilerOptions": {
"baseUrl": ".",
"inlineSourceMap": true,
"inlineSources": true,
"module": "ESNext",
"target": "es6",
"allowJs": false,
"noImplicitAny": true,
"moduleResolution": "node",
"importHelpers": true,
"lib": ["dom", "es5", "scripthost", "es2015"]
},
"include": ["**/*.ts"]
}
{
"compilerOptions": {
"baseUrl": ".",
"inlineSourceMap": true,
"inlineSources": true,
"module": "ESNext",
"target": "es6",
"allowJs": false,
"noImplicitAny": true,
"moduleResolution": "node",
"importHelpers": true,
"lib": ["dom", "es5", "scripthost", "es2015"],
"allowSyntheticDefaultImports": true
},
"include": ["**/*.ts"]
}
2 changes: 2 additions & 0 deletions upgrade-assets.cmd
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
curl https://cdn.jsdelivr.net/npm/codemirror@5/addon/mode/simple.js -o src/mode/simple/simple.js
curl https://cdn.jsdelivr.net/gh/mickael-kerjean/codemirror-orgmode/orgmode-fold.js -o src/mode/orgmode/orgmode-fold.js
curl https://cdn.jsdelivr.net/gh/mickael-kerjean/codemirror-orgmode/orgmode-mode.js -o src/mode/orgmode/orgmode-mode.js

curl https://cdn.jsdelivr.net/npm/@types/codemirror@5/index.d.ts -o src/lib/codemirror.d.ts
1 change: 1 addition & 0 deletions versions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"1.3.1": "0.10.12",
"1.3.0": "0.10.12",
"1.2.1": "0.10.12",
"1.2.0": "0.10.12",
Expand Down

0 comments on commit 67ca3c1

Please sign in to comment.