Skip to content

Commit

Permalink
Merge pull request #2 from Dmitriy-Shulha/develop
Browse files Browse the repository at this point in the history
Update to 0.0.5
  • Loading branch information
Dmytro-Shulha authored Oct 5, 2021
2 parents 42d621e + 727b197 commit 18b87ea
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 653 deletions.
63 changes: 24 additions & 39 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,37 @@
import { App, Modal, Notice, Plugin, PluginSettingTab, Setting, Menu, Editor, MarkdownView } from 'obsidian';
import { addIcons, PLOTLY_LOGO } from "./ui/icons";
import { Editor, MarkdownView, Plugin } from 'obsidian';

import { preprocessor } from './preprocessor';
import PlotlyModal from './ui/modal';
import PlotlyPluginSettingTab from './ui/settingsTab'
import './show-hint'

const basicPlotlyChart = [
'```plotly',
'data:',
'\t- x: [0,1,2]',
'\t y: [0,1,0]',
'```\n'
].join('\n')

const NEW_PLOTLY_CHART_NAME = "New Plotly Chart";
const newPlotlyChart = (editor: Editor)=>{
let doc = editor.getDoc();
let cursor = doc.getCursor();
doc.replaceRange(basicPlotlyChart, cursor);
}

export default class PlotlyPlugin extends Plugin {
settingsTab: PlotlyPluginSettingTab;

async onload() {
console.log('loading plugin');
console.log('loading Plotly plugin');

this.settingsTab = new PlotlyPluginSettingTab(this.app, this)
await this.settingsTab.loadSettings();
// this.addSettingTab(this.settingsTab);

addIcons();

this.registerCodeMirror((cm: CodeMirror.Editor) => {
console.log('codemirror', cm);
});

this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
console.log('click', evt);
});

this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));

this.registerMarkdownCodeBlockProcessor('plotly', preprocessor);

//@ts-ignore
this.registerEvent(this.app.workspace.on('editor-menu',
(menu: Menu, editor: Editor, view: MarkdownView) => {
if (view) {
menu.addItem((item) => {
item.setTitle("New Plotly Chart")
.setIcon(PLOTLY_LOGO)
.onClick((_) => {
let doc = editor.getDoc();
let cursor = doc.getCursor();
doc.replaceRange("```plotly\ndata:\n\t- x: [0,1,2]\n\t y: [0,1,0]\n```\n", cursor);
});
});
}
}));
this.addCommand({
id: "add-plotly-example",
name: NEW_PLOTLY_CHART_NAME,
editorCallback: (editor: Editor, view: MarkdownView)=>newPlotlyChart(editor)
});
}

onunload() {
console.log('unloading plugin');
console.log('unloading Plotly plugin');
}
}
9 changes: 4 additions & 5 deletions src/preprocessor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as Plotly from 'plotly.js-dist-min';
import { parseYaml, MarkdownPostProcessorContext } from "obsidian";

export const preprocessor = async (content: string, el: HTMLElement, ctx: MarkdownPostProcessorContext)=>{
import { MarkdownPostProcessorContext, parseYaml } from "obsidian";

export const preprocessor = (content: string, el: HTMLElement, ctx: MarkdownPostProcessorContext)=>{
let json;
try{
json = await parseYaml(content);
json = parseYaml(content);

validate(json, el)

Expand All @@ -20,8 +21,6 @@ export const preprocessor = async (content: string, el: HTMLElement, ctx: Markdo
} catch (error) {
el.innerHTML = "Couldn't render plot:<br><pre><code style=\"color:crimson\">" + error + "</code></pre>";
}

console.log(json);
}

const allowValues = ["data", "layout", "config"];
Expand Down
Loading

0 comments on commit 18b87ea

Please sign in to comment.