Skip to content

Commit

Permalink
close #52
Browse files Browse the repository at this point in the history
  • Loading branch information
phibr0 committed Sep 11, 2021
1 parent 139da33 commit dfe5e7e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 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-dictionary-plugin",
"name": "Dictionary",
"version": "2.17.0",
"version": "2.17.1",
"minAppVersion": "0.12.11",
"description": "This is a simple dictionary for the Obsidian Note-Taking Tool.",
"author": "phibr0",
Expand Down
22 changes: 13 additions & 9 deletions src/localDictionaryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class LocalDictionaryBuilder {
return string;
}

async newNote(content: DictionaryWord): Promise<void> {
async newNote(content: DictionaryWord, openNote = true): Promise<void> {

const { plugin, settings } = this;

Expand Down Expand Up @@ -93,23 +93,27 @@ export default class LocalDictionaryBuilder {
await plugin.app.vault.createFolder(normalizePath(`${settings.folder ? settings.folder + '/' : ''}${settings.languageSpecificSubFolders ? langString + '/' : ''}`));
}
file = await plugin.app.vault.create(normalizePath(path), contents);
const leaf = plugin.app.workspace.splitActiveLeaf();
await leaf.openFile(file);
plugin.app.workspace.setActiveLeaf(leaf);
if (openNote) {
const leaf = plugin.app.workspace.splitActiveLeaf();
await leaf.openFile(file);
plugin.app.workspace.setActiveLeaf(leaf);
}
} catch (error) {
new OverwriteModal(this.plugin, normalizePath(path), contents).open();
new OverwriteModal(this.plugin, normalizePath(path), contents, openNote).open();
}
}
}

class OverwriteModal extends Modal {
path: string;
content: string;
openNote: boolean;

constructor(plugin: DictionaryPlugin, path: string, content: string) {
constructor(plugin: DictionaryPlugin, path: string, content: string, openNote: boolean) {
super(plugin.app);
this.path = path;
this.content = content;
this.openNote = openNote;
}

onOpen() {
Expand All @@ -119,14 +123,14 @@ class OverwriteModal extends Modal {
this.app.vault.modify(this.app.vault.getAbstractFileByPath(this.path) as TFile, this.content);
let oldPaneOpen = false;
this.app.workspace.iterateAllLeaves((leaf) => {
if(leaf.view instanceof MarkdownView) {
if((leaf.getViewState().state.file as string).endsWith(this.path)) {
if (leaf.view instanceof MarkdownView) {
if ((leaf.getViewState().state.file as string).endsWith(this.path)) {
oldPaneOpen = true;
this.app.workspace.setActiveLeaf(leaf);
}
}
});
if(!oldPaneOpen) {
if (!oldPaneOpen && this.openNote) {
const leaf = this.app.workspace.splitActiveLeaf();
await leaf.openFile(this.app.vault.getAbstractFileByPath(this.path) as TFile);
this.app.workspace.setActiveLeaf(leaf);
Expand Down
11 changes: 8 additions & 3 deletions src/ui/dictionary/dictionaryView.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import type APIManager from "src/apiManager";
import type { DictionaryWord } from "src/integrations/types";
import type { DictionaryWord, Synonym } from "src/integrations/types";
import type LocalDictionaryBuilder from "src/localDictionaryBuilder";
import PhoneticComponent from "./phoneticComponent.svelte";
Expand Down Expand Up @@ -73,6 +73,12 @@
document.querySelector("#dictionary-search-input").focus();
}
async function generateNote(event: MouseEvent) {
if(query.trim() && promise) {
await localDictionary.newNote(await promise, event.ctrlKey ? false : true);
}
}
addEventListener("obsidian-dictionary-plugin-search", () => {
search();
});
Expand Down Expand Up @@ -109,8 +115,7 @@
id="localDictionaryBuilder"
class="nav-action-button"
aria-label={t("New Note")}
on:click={async () =>
promise && query.trim() && (await localDictionary.newNote(await promise))}
on:click={generateNote}
/>
</div>
<div class="search-input-container">
Expand Down

0 comments on commit dfe5e7e

Please sign in to comment.