Skip to content

Commit a51a5e2

Browse files
committed
add command
1 parent 608bfcc commit a51a5e2

File tree

4 files changed

+25
-31
lines changed

4 files changed

+25
-31
lines changed

manifest.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"id": "obsidian-echarts",
33
"name": "obsidian echarts",
4-
"version": "0.0.2",
4+
"version": "0.0.3",
55
"minAppVersion": "0.9.12",
66
"description": "obsidian echarts",
77
"author": "windily-cloud && Cuman",
8-
"authorUrl": "",
8+
"authorUrl": "https://github.com/cumany",
99
"isDesktopOnly": false
1010
}

src/main.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Plugin, parseYaml } from 'obsidian'
22
import Renderer from 'renderer'
33
import ChartsCreator from 'chartsCreator';
44
import { OptionsType } from './type';
5+
import * as echarts from 'echarts'
56

67
export default class EchartsPlugin extends Plugin {
78
async onload(): Promise<void> {
@@ -21,7 +22,6 @@ export default class EchartsPlugin extends Plugin {
2122
if (!options) {
2223
return
2324
}
24-
2525
const renderer = new Renderer(options, el)
2626

2727
if (options.chartType === 'pie') {
@@ -31,7 +31,9 @@ export default class EchartsPlugin extends Plugin {
3131
}
3232
})
3333
}
34-
34+
echarts() {
35+
return echarts
36+
}
3537
render(options:OptionsType, el: HTMLElement) {
3638
const renderer = new Renderer(options, el)
3739
renderer.render()

src/modal.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { App, Modal, Setting } from 'obsidian'
1+
var obsidian = require('obsidian');
2+
import { App, Modal, Setting, Notice } from 'obsidian'
23

34
export default class EchartsModal extends Modal {
45
app: App
@@ -24,10 +25,11 @@ export default class EchartsModal extends Modal {
2425
new Setting(this.contentEl).addButton((b) => {
2526
b.setButtonText('Create')
2627
b.onClick(() => {
27-
//@ts-ignore
28-
const editor = this.app.workspace.activeLeaf.view.editor
29-
editor.replaceRange(`\`\`\`echarts\ntype:pie\nsource:${option.source}\n\`\`\``, editor.getCursor())
30-
console.log(option)
28+
const view = this.app.workspace.getActiveViewOfType(obsidian.MarkdownView)
29+
if (!view) {
30+
return new Notice('Can only be created in editing mode',3000);
31+
}
32+
view.editor.replaceRange(`\`\`\`echarts\ntype:pie\nsource:${option.source}\n\`\`\``, view.editor.getCursor())
3133
this.close()
3234
})
3335
})

src/renderer.ts

+12-22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as echarts from 'echarts'
22
import 'echarts-wordcloud'
33
import { OptionsType } from './type'
44
import { getAPI } from 'obsidian-dataview'
5+
import { Notice } from 'obsidian'
56

67
export default class Renderer {
78
constructor(public options: OptionsType, public el: HTMLElement) {}
@@ -32,17 +33,15 @@ export default class Renderer {
3233
const myChart = this.initChart()
3334
const { width, height, ...option } = this.options
3435
const source = option.source
35-
//@ts-ignore
36-
const pages = getAPI(app).pages(`"${source}"`)
37-
//console.log(source, pages)
36+
const dv = getAPI(app);
37+
if ( typeof dv == 'undefined' ) { return new Notice('Dataview is not installed. This plugin requires Dataview to work properly.',3000); }
38+
const pages = dv.pages(`"${source}"`)
3839
const data = pages.map((page) => {
39-
//console.log(page.file.size)
4040
return {
4141
name: page.file.name,
4242
value: page.file.size
4343
}
4444
})
45-
console.log(data)
4645
const chartOption = {
4746
backgroundColor: '#2c343c',
4847
title: {
@@ -108,36 +107,27 @@ export default class Renderer {
108107
myChart.on('click', function (params) {
109108
let prefix: string = ''
110109
let searchWord: string = ''
111-
function search(searchWord: string) {
112-
const tmpLink = window.document.body.createEl('a', {
113-
href: `obsidian://search?query=${searchWord}`,
114-
})
115-
tmpLink.click()
116-
tmpLink.remove()
117-
}
118110
if (params.data['search']) {
119111
let search = params.data['search']
120-
if (search === 'tag') prefix = 'tag%3A'
121-
if (search === 'content') prefix = 'content%3A'
122-
if (search === 'path') prefix = 'path%3A'
123-
if (search === 'file') prefix = 'file%3A'
112+
if (search === 'tag') prefix = 'tag:'
113+
if (search === 'content') prefix = 'content:'
114+
if (search === 'path') prefix = 'path:'
115+
if (search === 'file') prefix = 'file:'
124116
searchWord = prefix + params.name
125117
}
126118
if (params.data['path']) {
127-
searchWord = searchWord + ' ' + 'path%3A' + params.data['path']
119+
searchWord = searchWord + ' ' + 'path:' + params.data['path']
128120
}
129121
if (params.data['file']) {
130-
searchWord = searchWord + ' ' + 'file%3A' + params.data['file']
122+
searchWord = searchWord + ' ' + 'file:' + params.data['file']
131123
}
132124
if (searchWord) {
133-
search(searchWord)
125+
app.internalPlugins.getPluginById('global-search')?.instance.openGlobalSearch(searchWord);
134126
} else {
135-
//@ts-ignore
136127
const filePath = app.metadataCache.getFirstLinkpathDest(
137128
params.name,
138-
params.name
129+
""
139130
)
140-
//@ts-ignore
141131
app.workspace.getUnpinnedLeaf().openFile(filePath)
142132
}
143133
})

0 commit comments

Comments
 (0)