Skip to content

Commit

Permalink
fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
linanwx committed Jul 21, 2024
1 parent 42e63e1 commit 7f3d5be
Show file tree
Hide file tree
Showing 10 changed files with 2,988 additions and 9,550 deletions.
5 changes: 3 additions & 2 deletions card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ParserCollection } from './ParserCollection';
import { Pattern } from './Pattern';
import { cyrb53 } from './hash';
import { DatabaseHelper } from 'db';
import { getAppInstance } from 'main';

// 卡片
// 卡片由源码和注释两部分组成,
Expand Down Expand Up @@ -193,7 +194,7 @@ class defaultCard implements Card {
}
async commitFile(committype: commitType) {
// 首先读取原文
let fileText = await app.vault.read(this.note)
let fileText = await getAppInstance().vault.read(this.note)
if (committype.ID == true && this.idGenFlag == false) {
// 如果卡片内容被更改, 则阻止添加ID, 因为这会破坏卡片格式
if (fileText.contains(this.cardText)) {
Expand All @@ -220,7 +221,7 @@ class defaultCard implements Card {
this.updateAnnotation()
}
// 提交变更
await app.vault.modify(this.note, fileText)
await getAppInstance().vault.modify(this.note, fileText)
}
}

Expand Down
7 changes: 4 additions & 3 deletions cardSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CardIDTag } from 'cardHead';
import { Notice, TFile } from 'obsidian';
import { TagParser } from 'tag';
import { Card, NewCard } from "./card";
import { getAppInstance } from 'main';

// 搜索的结果
export class SearchResult {
Expand Down Expand Up @@ -95,7 +96,7 @@ class defaultCardSearch implements cardSearcher {

async getCardFromVaultFile(): Promise<Card[]> {
let cards: Card[] = []
const notes: TFile[] = app.vault.getMarkdownFiles();
const notes: TFile[] = getAppInstance().vault.getMarkdownFiles();
for (const note of notes) {
cards.push(...await this.getCardFromFile(note))
}
Expand All @@ -104,7 +105,7 @@ class defaultCardSearch implements cardSearcher {
async getCardFromFile(note: TFile): Promise<Card[]> {
let cards: Card[] = []
try {
let fileText = await app.vault.read(note)
let fileText = await getAppInstance().vault.read(note)
if (fileText) {
cards = this.getCardFromText(fileText, note);
}
Expand All @@ -117,7 +118,7 @@ class defaultCardSearch implements cardSearcher {
private getCardFromText(fileText: string, note: TFile): Card[] {
let cards: Card[] = []
let results = this.matchText(fileText)
let cache = app.metadataCache.getFileCache(note)
let cache = getAppInstance().metadataCache.getFileCache(note)
for (let result of results) {
// 匹配注释段
let cardText = result.all;
Expand Down
9 changes: 5 additions & 4 deletions db.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Low } from 'lowdb';
import { Notice } from 'obsidian';
import { getAppInstance } from 'main';
import { App, Notice, Vault } from 'obsidian';
import { CardSchedule, scheduleData } from 'schedule';

export const DBNAME = 'aosr.db'
Expand All @@ -23,8 +24,8 @@ class ObsidianAdapter {
async read() {
try {
const filePath = this.basePath + this.fileName;
if (await app.vault.adapter.exists(filePath)) {
const data = await app.vault.adapter.read(filePath);
if (await getAppInstance().vault.adapter.exists(filePath)) {
const data = await getAppInstance().vault.adapter.read(filePath);
return JSON.parse(data);
}
return { docs: [] } as Data;
Expand All @@ -37,7 +38,7 @@ class ObsidianAdapter {
async write(data: any) {
const filePath = this.basePath + this.fileName;
const content = JSON.stringify(data);
await app.vault.adapter.write(filePath, content);
await getAppInstance().vault.adapter.write(filePath, content);
}
}

Expand Down
30 changes: 20 additions & 10 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { handlerDeckCode } from 'deck';
import { initLanguage } from 'language';
import { debounce } from 'lodash';
import { MigrateModal } from 'migrate';
import { EventRef, MarkdownView, Notice, Plugin, TFile, addIcon } from 'obsidian';
import { App, EventRef, MarkdownView, Notice, Plugin, TFile, addIcon } from 'obsidian';
import { ClozeParser } from 'patternCloze';
import { MultiLineParser, SingleLineParser } from 'patternLine';
import { AOSRSettingTab, GlobalSettings, setGlobalSettings } from 'setting';
Expand All @@ -15,6 +15,15 @@ import { ReviewView, VIEW_TYPE_REVIEW } from './view';
import { DatabaseHelper } from 'db';
import { t } from 'i18next';

let appInstance: App;

export function setAppInstance(app: App) {
appInstance = app;
}

export function getAppInstance(): App {
return appInstance;
}

class AosrWriterHelper {
cardCount: number = 0
Expand All @@ -27,19 +36,19 @@ class AosrWriterHelper {
}, 500);

unregister() {
app.workspace.offref(this.event1)
app.workspace.offref(this.event2)
getAppInstance().workspace.offref(this.event1)
getAppInstance().workspace.offref(this.event2)
}

constructor() {
this.event1 = app.workspace.on("active-leaf-change", async (leaf) => {
let view = app.workspace.getActiveViewOfType(MarkdownView)
this.event1 = getAppInstance().workspace.on("active-leaf-change", async () => {
let view = getAppInstance().workspace.getActiveViewOfType(MarkdownView)
if (!view || !view.file) {
return
}
this.checkDebounced(view.file, view.editor.getValue())
})
this.event2 = app.workspace.on("editor-change", async (editor, view) => {
this.event2 = getAppInstance().workspace.on("editor-change", async (editor, view) => {
if (!view || !view.file) {
return;
}
Expand Down Expand Up @@ -93,7 +102,7 @@ export default class AOSRPlugin extends Plugin {
this.app.workspace.iterateAllLeaves((leaf) => {
if (leaf.view.getViewType() == VIEW_TYPE_REVIEW) {
find = true
app.workspace.revealLeaf(leaf)
this.app.workspace.revealLeaf(leaf)
}
})
if (find) {
Expand All @@ -107,9 +116,10 @@ export default class AOSRPlugin extends Plugin {
type: VIEW_TYPE_REVIEW,
active: true,
})
app.workspace.revealLeaf(leaf)
this.app.workspace.revealLeaf(leaf)
}
async onload() {
setAppInstance(this.app)
let h = DatabaseHelper.getInstance()
this.registerEditorExtension(emojiTagPlugin)
await this.loadSettings();
Expand All @@ -119,7 +129,7 @@ export default class AOSRPlugin extends Plugin {
<path d="M 6.56 26.72 L 44.504 9.026 C 47.786 7.496 51.688 8.916 53.218 12.198 L 58.634 23.812 L 87.211 23.812 C 90.832 23.812 93.768 26.748 93.768 30.369 L 93.768 85.564 C 93.768 89.185 90.832 92.121 87.211 92.121 L 45.344 92.121 C 42.213 92.121 39.594 89.926 38.943 86.991 L 35.429 88.629 C 32.147 90.159 28.245 88.739 26.715 85.457 L 3.388 35.434 C 1.858 32.152 3.278 28.25 6.56 26.72 Z M 49.168 14.22 C 48.491 12.768 46.766 12.14 45.314 12.817 L 8.944 29.777 C 7.492 30.454 6.865 32.179 7.542 33.631 L 30.766 83.436 C 31.443 84.888 33.168 85.516 34.62 84.838 L 38.787 82.895 L 38.787 30.369 C 38.787 26.748 41.723 23.812 45.344 23.812 L 53.641 23.812 L 49.168 14.22 Z M 43.313 30.49 L 43.313 85.444 C 43.313 87.046 44.611 88.344 46.213 88.344 L 86.343 88.344 C 87.945 88.344 89.243 87.046 89.243 85.444 L 89.243 30.49 C 89.243 28.888 87.945 27.59 86.343 27.59 L 46.213 27.59 C 44.611 27.59 43.313 28.888 43.313 30.49 Z" fill="currentColor" stroke="currentColor"/>
`
)
this.addRibbonIcon('flashcards', 'Aosr', async ()=>{await this.openView()});
this.addRibbonIcon('flashcards', 'Aosr', async () => { await this.openView() });
this.addCommand({
id: 'aosr-open-review',
name: t('OpenAosr'),
Expand Down Expand Up @@ -157,7 +167,7 @@ export default class AOSRPlugin extends Plugin {
}

onunload() {
app.workspace.detachLeavesOfType(VIEW_TYPE_REVIEW)
this.app.workspace.detachLeavesOfType(VIEW_TYPE_REVIEW)
const parserCol = ParserCollection.getInstance();
parserCol.clean()
this.writerHelper.unregister()
Expand Down
15 changes: 8 additions & 7 deletions markdown.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

import { getAppInstance } from "main";
import {
Component,
MarkdownRenderer, TFile
Expand Down Expand Up @@ -53,7 +54,7 @@ function handleEmbeds(dom: HTMLDivElement, sourcePath: string) {
return;
}
const normalizedPath = getNormalizedPath(src);
const target = app.metadataCache.getFirstLinkpathDest(
const target = getAppInstance().metadataCache.getFirstLinkpathDest(
normalizedPath.root,
sourcePath
);
Expand Down Expand Up @@ -81,7 +82,7 @@ function handleEmbeds(dom: HTMLDivElement, sourcePath: string) {
return;
}
const normalizedPath = getNormalizedPath(href);
const target = app.metadataCache.getFirstLinkpathDest(
const target = getAppInstance().metadataCache.getFirstLinkpathDest(
normalizedPath.root,
sourcePath
);
Expand All @@ -99,7 +100,7 @@ function handleImage(el: HTMLElement, file: TFile) {
el.empty();
el.createEl(
'img',
{ attr: { src: app.vault.getResourcePath(file) } },
{ attr: { src: getAppInstance().vault.getResourcePath(file) } },
(img) => {
if (el.hasAttribute('width')) {
let v = el.getAttribute('width')
Expand Down Expand Up @@ -127,7 +128,7 @@ function handleImage(el: HTMLElement, file: TFile) {
function handleAudio(el: HTMLElement, file: TFile) {
el.empty();
el.createEl('audio', {
attr: { controls: '', src: app.vault.getResourcePath(file) },
attr: { controls: '', src: getAppInstance().vault.getResourcePath(file) },
});
el.addClasses(['media-embed', 'is-loaded']);
}
Expand All @@ -136,7 +137,7 @@ function handleVideo(el: HTMLElement, file: TFile) {
el.empty();
el.createEl(
'video',
{ attr: { controls: '', src: app.vault.getResourcePath(file) } },
{ attr: { controls: '', src: getAppInstance().vault.getResourcePath(file) } },
(video) => {
const handleLoad = () => {
video.removeEventListener('loadedmetadata', handleLoad);
Expand Down Expand Up @@ -166,7 +167,7 @@ function handleFile(el: HTMLElement, file: TFile) {
a.addEventListener('click', (event) => {
event.preventDefault(); // 阻止默认的链接跳转行为
// 手动调用 Obsidian 的 API 来打开文件
app.workspace.getLeaf(true).openFile(file);
getAppInstance().workspace.getLeaf(true).openFile(file);
});
}

Expand All @@ -185,7 +186,7 @@ function handleLink(el: HTMLElement, sourcePath: string, link: string) {
a.addEventListener('click', (event) => {
event.preventDefault(); // 阻止默认的链接跳转行为
// 手动调用 Obsidian 的 API 来打开文件
app.workspace.openLinkText(link, sourcePath, true)
getAppInstance().workspace.openLinkText(link, sourcePath, true)
});
}

Expand Down
7 changes: 4 additions & 3 deletions migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { App, Modal, Setting } from "obsidian";
import i18n from 'i18next';
import { NewCardSearch } from "cardSearch";
import { DatabaseHelper } from "db";
import { getAppInstance } from "main";

export class MigrateModal extends Modal {
div: HTMLDivElement;
Expand Down Expand Up @@ -76,10 +77,10 @@ export class MigrateModal extends Modal {
let cleanLinkRefReg = /\[\[\#\^[\w|\d]+\|?.+\]\]/gm
let countDataComment = 0
let countLinkRef = 0
let files = app.vault.getMarkdownFiles()
let files = getAppInstance().vault.getMarkdownFiles()
for (let i = 0; i < files.length; i++) {
let file = files[i]
let fileText = await app.vault.read(file)
let fileText = await getAppInstance().vault.read(file)
let newFileText = fileText.replace(cleanDataCommentReg, () => {
countDataComment++
return ""
Expand All @@ -98,7 +99,7 @@ export class MigrateModal extends Modal {
}
newFileText = lines.join("\n")
if (newFileText != fileText) {
await app.vault.modify(file, newFileText)
await getAppInstance().vault.modify(file, newFileText)
}
}
// 提示用户清理完成以及数量
Expand Down
Loading

0 comments on commit 7f3d5be

Please sign in to comment.