First Commit

This commit is contained in:
Gabriele Cannata 2022-09-24 19:13:25 +02:00
commit a2fb510e7c
20 changed files with 6272 additions and 0 deletions

10
.editorconfig Normal file
View file

@ -0,0 +1,10 @@
# top-most EditorConfig file
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
tab_width = 4

2
.eslintignore Normal file
View file

@ -0,0 +1,2 @@
npm node_modules
build

23
.eslintrc Normal file
View file

@ -0,0 +1,23 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"env": { "node": true },
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error", { "args": "none" }],
"@typescript-eslint/ban-ts-comment": "off",
"no-prototype-builtins": "off",
"@typescript-eslint/no-empty-function": "off"
}
}

23
.gitignore vendored Normal file
View file

@ -0,0 +1,23 @@
# vscode
.vscode
# Intellij
*.iml
.idea
# npm
node_modules
# Don't include the compiled main.js file in the repo.
# They should be uploaded to GitHub releases instead.
main.js
styles.css
# Exclude sourcemaps
*.map
# obsidian
data.json
# Exclude macOS Finder (System Explorer) View States
.DS_Store

1
.npmrc Normal file
View file

@ -0,0 +1 @@
tag-version-prefix=""

1
README.md Normal file
View file

@ -0,0 +1 @@
TODO:

92
Views/TemplateView.tsx Normal file
View file

@ -0,0 +1,92 @@
/* eslint-disable @typescript-eslint/ban-types */
import { debounce, finishRenderMath, ItemView, WorkspaceLeaf } from "obsidian";
import * as React from "react";
import { createRoot, Root } from "react-dom/client";
import { loadMathJax } from "obsidian";
import { TemplateSettings } from "src/Settings";
import { getTemplateSettings } from "src/main";
export const TEMPLATE_VIEW = "Template-view";
export const TemplateContext = React.createContext<any>({});
export class TemplateView extends ItemView {
settings: TemplateSettings;
root: Root;
state = {
};
constructor(leaf: WorkspaceLeaf) {
super(leaf);
// this.settings = (this.app as any).plugins.plugins["obsidian-Template"].settings as TemplateSettings;
this.settings = getTemplateSettings();
this.state = {
};
this.icon = "sigma";
}
getViewType() {
return TEMPLATE_VIEW;
}
getDisplayText() {
return "Template";
}
override onResize(): void {
super.onResize();
this.handleResize();
}
handleResize = debounce(() => {
this.render();
}, 300);
render() {
this.root.render(
<React.StrictMode>
<TemplateContext.Provider value={{
width: this.contentEl.innerWidth,
settings: this.settings
}}>
<div>TODO:</div>
</TemplateContext.Provider>
</React.StrictMode>
);
}
async onOpen() {
const { contentEl } = this;
// contentEl.setText('Woah!');
// this.titleEl.setText("Obsidian Janitor")
this.root = createRoot(contentEl/*.children[1]*/);
await loadMathJax();
await finishRenderMath();
this.render();
// const e = nerdamer('x^2+2*(cos(x)+x*x)');
// const latex = e.toTeX();
// console.log(latex);
// const mathEl = renderMath(latex, true);
// contentEl.appendChild(mathEl);
}
async onClose() {
this.root.unmount();
}
}

53
esbuild.config.mjs Normal file
View file

@ -0,0 +1,53 @@
import esbuild from "esbuild";
import process from "process";
import builtins from 'builtin-modules'
import {sassPlugin} from 'esbuild-sass-plugin'
import svgrPlugin from 'esbuild-plugin-svgr';
const banner =
`/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
`;
const prod = (process.argv[2] === 'production');
esbuild.build({
banner: {
js: banner,
},
entryPoints: ['src/main.ts'],
bundle: true,
minify: prod,
external: [
'obsidian',
'electron',
'@codemirror/autocomplete',
'@codemirror/collab',
'@codemirror/commands',
'@codemirror/language',
'@codemirror/lint',
'@codemirror/search',
'@codemirror/state',
'@codemirror/view',
'@lezer/common',
'@lezer/highlight',
'@lezer/lr',
...builtins],
format: 'cjs',
watch: !prod,
target: 'es2016',
logLevel: "info",
sourcemap: prod ? false : 'inline',
treeShaking: true,
outfile: 'main.js',
plugins: [svgrPlugin()]
}).catch(() => process.exit(1));
esbuild.build({
entryPoints: ['styles.scss'],
outfile: "styles.css",
watch: !prod,
plugins: [sassPlugin()]
}).catch(() => process.exit(1));

10
manifest-beta.json Normal file
View file

@ -0,0 +1,10 @@
{
"id": "obsidian-template",
"name": "template",
"version": "0.5.7",
"minAppVersion": "0.15.0",
"description": "Computer Algebra System for Onsidian",
"author": "Gabriele Cannata",
"authorUrl": "https://github.com/Canna71",
"isDesktopOnly": true
}

10
manifest.json Normal file
View file

@ -0,0 +1,10 @@
{
"id": "obsidian-template",
"name": "template",
"version": "1.0.0",
"minAppVersion": "0.15.0",
"description": "TODO:",
"author": "Gabriele Cannata",
"authorUrl": "https://github.com/Canna71",
"isDesktopOnly": false
}

5762
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

34
package.json Normal file
View file

@ -0,0 +1,34 @@
{
"name": "obsidian-template",
"version": "1.0.0",
"description": "TODO:",
"main": "main.js",
"scripts": {
"dev": "node esbuild.config.mjs",
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
"version": "node version-bump.mjs && git add manifest.json versions.json",
"scss": "ode-sass -w styles.scss styles.css"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"@codemirror/language": "^6.2.1",
"@types/node": "^16.11.6",
"@types/react": "^18.0.17",
"@types/react-dom": "^18.0.6",
"@typescript-eslint/eslint-plugin": "5.29.0",
"@typescript-eslint/parser": "5.29.0",
"builtin-modules": "3.3.0",
"esbuild": "0.14.47",
"esbuild-plugin-svgr": "^1.0.1",
"esbuild-sass-plugin": "^2.3.2",
"obsidian": "latest",
"tslib": "2.4.0",
"typescript": "4.7.4"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}

3
src/Globals.d.ts vendored Normal file
View file

@ -0,0 +1,3 @@
declare module "*.module.css";
declare module "*.module.scss";
declare module "*.svg";

46
src/SettingTab.ts Normal file
View file

@ -0,0 +1,46 @@
import TemplatePlugin from "src/main";
import { App, PluginSettingTab, Setting } from "obsidian";
export class TemplateSettingsTab extends PluginSettingTab {
plugin: TemplatePlugin;
constructor(app: App, plugin: TemplatePlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'Template Settings'});
this.createToggle(containerEl, "Add Ribbon Icon",
"Adds an icon to the ribbon to launch scan",
"addRibbonIcon"
);
this.createToggle(containerEl, "Show Template Sidebar",
"Opens Template sidebar at startup",
"showAtStartUp"
);
}
private createToggle(containerEl: HTMLElement, name: string, desc: string, prop: string) {
new Setting(containerEl)
.setName(name)
.setDesc(desc)
.addToggle(bool => bool
.setValue((this.plugin.settings as any)[prop] as boolean)
.onChange(async (value) => {
(this.plugin.settings as any)[prop] = value;
await this.plugin.saveSettings();
this.display();
})
);
}
}

13
src/Settings.ts Normal file
View file

@ -0,0 +1,13 @@
export interface TemplateSettings {
addRibbonIcon: boolean;
showAtStartup: boolean;
}
export const DEFAULT_SETTINGS: TemplateSettings = {
addRibbonIcon: true,
showAtStartup: true,
}

145
src/main.ts Normal file
View file

@ -0,0 +1,145 @@
import { DEFAULT_SETTINGS, TemplateSettings } from "src/Settings";
import { addIcon, MarkdownView } from "obsidian";
// import { MathResult } from './Extensions/ResultMarkdownChild';
/* eslint-disable @typescript-eslint/no-unused-vars */
import { TemplateView, TEMPLATE_VIEW } from "../Views/TemplateView";
import {
App,
finishRenderMath,
loadMathJax,
Modal,
Plugin,
WorkspaceLeaf,
} from "obsidian";
import { TemplateSettingsTab } from "src/SettingTab";
const sigma = `<path stroke="currentColor" fill="none" d="M78.6067 22.8905L78.6067 7.71171L17.8914 7.71171L48.2491 48.1886L17.8914 88.6654L78.6067 88.6654L78.6067 73.4866" opacity="1" stroke-linecap="round" stroke-linejoin="round" stroke-width="6" />
`;
// Remember to rename these classes and interfaces!
let gSettings: TemplateSettings;
export function getTemplateSettings() { return gSettings; }
export default class TemplatePlugin extends Plugin {
settings: TemplateSettings;
async onload() {
await this.loadSettings();
this.registerView(TEMPLATE_VIEW, (leaf) => new TemplateView(leaf));
addIcon("sigma",sigma);
if (this.settings.addRibbonIcon) {
// This creates an icon in the left ribbon.
const ribbonIconEl = this.addRibbonIcon(
"sigma",
"Open Template",
(evt: MouseEvent) => {
this.activateView();
}
);
// Perform additional things with the ribbon
ribbonIconEl.addClass("Template-ribbon-class");
}
this.addCommand({
id: "show-Template-view",
name: "Show Template Sidebar",
callback: () => this.activateView(),
});
this.app.workspace.onLayoutReady(() => {
if(this.settings.showAtStartup){
this.activateView();
}
});
this.registerCodeBlock();
this.registerPostProcessor();
this.registerEditorExtensions();
this.app.workspace.on(
"active-leaf-change",
(leaf: WorkspaceLeaf | null) => {
// console.log("active-leaf-change", leaf);
if (leaf?.view instanceof MarkdownView) {
// @ts-expect-error, not typed
const editorView = leaf.view.editor.cm as EditorView;
}
},
this
);
this.app.workspace.on(
"codemirror",
(cm: CodeMirror.Editor) => {
console.log("codemirror", cm);
},
this
);
this.addSettingTab(new TemplateSettingsTab(this.app, this));
}
onunload() {
this.app.workspace.detachLeavesOfType(TEMPLATE_VIEW);
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
gSettings = this.settings;
}
async saveSettings() {
await this.saveData(this.settings);
}
async activateView() {
this.app.workspace.detachLeavesOfType(TEMPLATE_VIEW);
await this.app.workspace.getRightLeaf(false).setViewState(
{
type: TEMPLATE_VIEW,
active: true,
},
{ settings: this.settings }
);
this.app.workspace.revealLeaf(
this.app.workspace.getLeavesOfType(TEMPLATE_VIEW)[0]
);
}
async registerCodeBlock() {
await loadMathJax();
await finishRenderMath();
this.registerMarkdownCodeBlockProcessor(
"Template",
(source, el, ctx) => {
// processCodeBlock(source, el, this.settings, ctx);
}
);
}
async registerPostProcessor() {
console.log("registerPostProcessor");
// await loadMathJax();
// await finishRenderMath();
// this.registerMarkdownPostProcessor(getPostPrcessor(this.settings));
}
async registerEditorExtensions() {
// this.registerEditorExtension([resultField, TemplateConfigField]);
}
}

0
styles.scss Normal file
View file

27
tsconfig.json Normal file
View file

@ -0,0 +1,27 @@
{
"compilerOptions": {
"baseUrl": ".",
"inlineSourceMap": true,
"inlineSources": true,
"module": "ESNext",
"target": "ES6",
"allowJs": true,
"noImplicitAny": true,
"moduleResolution": "node",
"importHelpers": true,
"isolatedModules": true,
"strictNullChecks": true,
"esModuleInterop": true,
// "allowSyntheticDefaultImports": true,
"lib": [
"DOM",
"ES5",
"ES6",
"ES7"
],
"jsx": "react"
},
"include": [
"**/*.ts","**/*.tsx","**/*.svg"
]
}

14
version-bump.mjs Normal file
View file

@ -0,0 +1,14 @@
import { readFileSync, writeFileSync } from "fs";
const targetVersion = process.env.npm_package_version;
// read minAppVersion from manifest.json and bump version to target version
let manifest = JSON.parse(readFileSync("manifest.json", "utf8"));
const { minAppVersion } = manifest;
manifest.version = targetVersion;
writeFileSync("manifest.json", JSON.stringify(manifest, null, "\t"));
// update versions.json with target version and minAppVersion from manifest.json
let versions = JSON.parse(readFileSync("versions.json", "utf8"));
versions[targetVersion] = minAppVersion;
writeFileSync("versions.json", JSON.stringify(versions, null, "\t"));

3
versions.json Normal file
View file

@ -0,0 +1,3 @@
{
"1.0.0": "0.15.0"
}