Skip to content

Commit

Permalink
Get rid of windows.app as it's highly discouraged
Browse files Browse the repository at this point in the history
  • Loading branch information
vbeskrovnov committed Jan 11, 2023
1 parent da3e50e commit c6040c7
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-contacts",
"name": "Contacts",
"version": "1.0.0",
"version": "1.0.1",
"minAppVersion": "0.15.0",
"description": "Allows you to manage and organize your contacts.",
"author": "Vadim Beskrovnov",
Expand Down
15 changes: 6 additions & 9 deletions src/file/file.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { normalizePath, Notice, TFile, TFolder, Vault } from "obsidian";
import { normalizePath, Notice, TFile, TFolder, Vault, Workspace } from "obsidian";
import { join } from "path";

const { vault } = window.app;

export async function openFile(file: TFile) {
const { workspace } = window.app;
export async function openFile(file: TFile, workspace: Workspace) {
const leaf = workspace.getLeaf()
await leaf.openFile(file, { active: true });
}
Expand All @@ -19,14 +16,14 @@ export function findContactFiles(contactsFolder: TFolder) {
return contactFiles;
}

export function createContactFile(folderPath: string) {
export function createContactFile(folderPath: string, vault: Vault, workspace: Workspace) {
const folder = vault.getAbstractFileByPath(folderPath)
if (!folder) {
new Notice(`Can not find path: '${folderPath}'. Please update "Contacts" plugin settings`);
return;
}

vault.create(normalizePath(join(folderPath, `Contact ${findNextFileNumber(folderPath)}.md`)), `
vault.create(normalizePath(join(folderPath, `Contact ${findNextFileNumber(folderPath, vault)}.md`)), `
/---contact---/
| key | value |
| --------- | ----- |
Expand All @@ -39,10 +36,10 @@ export function createContactFile(folderPath: string) {
| Last chat | |
| Friends | |
/---contact---/`)
.then(createdFile => openFile(createdFile));
.then(createdFile => openFile(createdFile, workspace));
}

function findNextFileNumber(folderPath: string) {
function findNextFileNumber(folderPath: string, vault: Vault) {
const folder = vault.getAbstractFileByPath(
normalizePath(folderPath)
) as TFolder;
Expand Down
9 changes: 4 additions & 5 deletions src/parse/parse.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { TFile } from "obsidian";
import { TFile, Vault } from "obsidian";
import { Contact } from "./contact";

export async function parseContactFiles(files: TFile[]) {
export async function parseContactFiles(files: TFile[], vault: Vault) {
const contactsData: Contact[] = [];
for (const contactFile of files) {
const contact = await parseContactData(contactFile);
const contact = await parseContactData(contactFile, vault);
if (contact) {
contactsData.push(contact);
}
}
return contactsData;
}

async function parseContactData(file: TFile): Promise<Contact | null> {
const { vault } = window.app;
async function parseContactData(file: TFile, vault: Vault): Promise<Contact | null> {
const fileContents = await vault.cachedRead(file);
if (!isContactFile(fileContents)) {
return null;
Expand Down
7 changes: 6 additions & 1 deletion src/ui/sidebar/components/ContactView.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from "react";
import { useApp } from "src/context/hooks";
import { openFile } from "src/file/file";
import { Contact } from "src/parse/contact";
import { daysUntilBirthday, diffDateToday } from "src/util/dates";
Expand All @@ -8,9 +9,13 @@ type ContactProps = {
};

export const ContactView = (props: ContactProps) => {
const { workspace } = useApp();
const contact = props.contact;
return (
<div className="contact-card" onClick={() => openFile(contact.file)}>
<div
className="contact-card"
onClick={() => openFile(contact.file, workspace)}
>
<div className="content">
<div className="name">
{contact.name} {contact.lastName}
Expand Down
6 changes: 3 additions & 3 deletions src/ui/sidebar/components/SidebarRootView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type RootProps = {
};

export const SidebarRootView = (props: RootProps) => {
const { vault } = useApp();
const { vault, workspace } = useApp();
const [contacts, setContacts] = React.useState<Contact[]>([]);
const [sort, setSort] = React.useState<Sort>(Sort.LAST_CONTACT);
const folder = props.plugin.settings.contactsFolder;
Expand All @@ -30,7 +30,7 @@ export const SidebarRootView = (props: RootProps) => {

const contactFiles: TFile[] = findContactFiles(contactsFolder);

parseContactFiles(contactFiles).then((contactsData) =>
parseContactFiles(contactFiles, vault).then((contactsData) =>
setContacts(contactsData)
);
}, []);
Expand All @@ -39,7 +39,7 @@ export const SidebarRootView = (props: RootProps) => {
<div>
<HeaderView
onSortChange={setSort}
onCreateContact={() => createContactFile(folder)}
onCreateContact={() => createContactFile(folder, vault, workspace)}
sort={sort}
/>
<ContactsListView contacts={contacts} sort={sort} />
Expand Down
5 changes: 3 additions & 2 deletions versions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"1.0.0": "0.15.0"
}
"1.0.0": "0.15.0",
"1.0.1": "0.15.0"
}

0 comments on commit c6040c7

Please sign in to comment.