Skip to content

Commit

Permalink
added snwAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
TfT Hacker committed Sep 10, 2022
1 parent 6878789 commit ca6c263
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/header-incoming-count.ts → src/headerImageCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function setHeaderWithReferenceCounts(thePlugin: ThePlugin) {

function processHeader(thePlugin: ThePlugin, mdView: MarkdownView) {
const allLinks: Link[] = thePlugin.app.fileManager.getAllLinkResolutions();
const incomingLinks = allLinks.filter(f=>f.resolvedFile.path===mdView.file.path);
const incomingLinks = allLinks.filter(f=>f?.resolvedFile.path===mdView.file.path);

const containerTitleDiv: HTMLDivElement = mdView.containerEl.querySelector(".view-content");
const fileList = (incomingLinks.map(link => link.sourceFile.path.replace(".md", ""))).slice(0,20).join("\n")
Expand Down
23 changes: 18 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import InlineReferenceExtension, {setPluginVariableForCM6} from "./references-cm
import {buildLinksAndReferences} from "./indexer";
import markdownPreviewProcessor from "./references-preview";
import {SidePaneView, VIEW_TYPE_SNW} from "./sidepane";
import setHeaderWithReferenceCounts from "./header-incoming-count";
import setHeaderWithReferenceCounts from "./headerImageCount";
import {SettingsTab, Settings, DEFAULT_SETTINGS} from "./settingsTab";
import SnwAPI from "./snwApi";

export default class ThePlugin extends Plugin {
pluginInitialized = false;
appName = "Obsidian42 - Strange New Worlds";
appID = "obsidian42-strange-new-worlds";
settings: Settings;
lastSelectedReferenceKey : string;
lastSelectedReferenceType : string;
lastSelectedReferenceLink : string;
Expand All @@ -20,8 +23,8 @@ export default class ThePlugin extends Plugin {
buildLinksAndReferences(this.app)
}, 3000, true);

const initializeEnvironment = () => {

const initializeEnvironment = async () => {
await this.loadSettings();
setPluginVariableForCM6(this);

this.registerEditorExtension([InlineReferenceExtension]); // enable the codemirror extensions
Expand All @@ -32,13 +35,19 @@ export default class ThePlugin extends Plugin {
this.app.workspace.on("layout-change", async () => {
setHeaderWithReferenceCounts(this);
});

this.addSettingTab(new SettingsTab(this.app, this));

//@ts-ignore
globalThis.snwAPI = new SnwAPI(this);

}

// managing state for debugging purpsoes
setTimeout(() => {
setTimeout(async () => {
if (!this.pluginInitialized) {
this.pluginInitialized = true;
initializeEnvironment();
await initializeEnvironment();
}
}, 4000);

Expand Down Expand Up @@ -66,4 +75,8 @@ export default class ThePlugin extends Plugin {
this.app.workspace.detachLeavesOfType(VIEW_TYPE_SNW);
console.log("unloading " + this.appName)
}

async loadSettings(): Promise<void> { this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()) }

async saveSettings(): Promise<void> { await this.saveData(this.settings) }
}
5 changes: 0 additions & 5 deletions src/references-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ export default function markdownPreviewProcessor(el : HTMLElement, ctx : Markdow
// check for incompatibility with other plugins
if(app.metadataCache.getFileCache(currentFile)?.frontmatter?.["kanban-plugin"] ) return; //no support for kanban board

console.log("el", el)
console.log( "currentFile", currentFile)
console.log( app.metadataCache.getFileCache( currentFile )?.frontmatter?.["kanban-plugin"] );


const currentFilePath = currentFile.path;
const transformedCache = getCurrentPage(currentFile, thePlugin.app);

Expand Down
38 changes: 38 additions & 0 deletions src/settingsTab.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { App, PluginSettingTab, Setting, ToggleComponent } from 'obsidian';
import ThePlugin from './main';

export interface Settings {
debugMode: boolean
}

export const DEFAULT_SETTINGS: Settings = {
debugMode: false
}

export class SettingsTab extends PluginSettingTab {
plugin: ThePlugin;

constructor(app: App, plugin: ThePlugin) {
super(app, plugin);
this.plugin = plugin;
}

display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Obsidian42 - Strange New Worlds' });

new Setting(containerEl)
.setName('Developer Debugging Mode')
.setDesc('Enable Debugging Mode for troubleshooting in the console.')
.addToggle((cb: ToggleComponent) => {
cb.setValue(this.plugin.settings.debugMode);
cb.onChange(async (value: boolean) => {
this.plugin.settings.debugMode = value;
await this.plugin.saveSettings();
});
});


}
}
48 changes: 48 additions & 0 deletions src/snwApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { getCurrentPage } from "./indexer";
import ThePlugin from "./main";

/**
* Provide a simple API for use with Templater, Dataview and debugging the complexities of various pages.
* main.ts will attach this to window.snwAPI
*
* @export
* @class SnwAPI
*/
export default class SnwAPI {
plugin: ThePlugin;

constructor(plugin: ThePlugin) {
this.plugin = plugin
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
console = (logDescription: string, ...outputs: any[]): void => {
if(this.plugin.settings.debugMode===true)
console.log(logDescription, outputs)
}

/**
* For active file return the meta information used by various components of SNW
*
* @return {*} {Promise<any>} // Needs to be any since we might return just about anything
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getMetaInfoByCurrentFile = async (): Promise<any> => {
return this.getMetaInfoByFileName(app.workspace.getActiveFile().path)
}

/**
* For given file name passed into the function, get the meta info for that file
*
* @param {string} fileName (or file name path)
* @memberof SnwAPI
*/
getMetaInfoByFileName = async (fileName: string)=> {
const currentFile = app.metadataCache.getFirstLinkpathDest(fileName, "/")
return {
TFile: currentFile,
metadataCache: app.metadataCache.getFileCache(currentFile),
SnwTransformedCache: getCurrentPage(currentFile, this.plugin.app),
}
}
}
12 changes: 4 additions & 8 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,22 @@
.snw-header-count-wrapper {
position: absolute;
z-index:1000;
/* top: 10px; */
margin-top: 10px;
right: 20px;
/* width: 15px !important; */
font-size: 8pt;
/* min-width: 20px !important; */
height: 20px;
border-radius: 5px;
border: 1px dotted;
/* display: flex; */
/* justify-content: center; */
/* align-items: center; */
opacity: 0.5;
}

.snw-header-count {
text-align: center;
min-width: 15px !important;
/* opacity: 0.5 !important; */
/* height: 20px; */
height: 0px;
padding-top: 3px;
/* padding: 5px; */
/* border: 1px solid red; */
}

.snw-sidepane-container {
Expand Down

0 comments on commit ca6c263

Please sign in to comment.