Skip to content

Commit

Permalink
Add command to insert literature note content into active pane. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
hans committed Jan 14, 2021
1 parent 8a247ec commit 2c7acf9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ The plugin offers three simple features at the moment:

1. **Open literature note** (<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>O</kbd>): automatically create or open a literature note for a particular reference. The title, folder, and initial content of the note can be configured in the plugin settings.
2. **Insert literature note reference** (<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>E</kbd>): insert a link to the literature note corresponding to a particular reference.
3. **Insert Markdown citation** (no hotkey by default): insert a [Pandoc-style citation][3] for a particular reference. (The exact format of the citation can be configured in the plugin settings.)
3. **Insert literature note content in the current pane** (no hotkey by default): insert content describing a particular reference into the current pane. (This can be useful for updating literature notes you already have but which are missing reference information.)
4. **Insert Markdown citation** (no hotkey by default): insert a [Pandoc-style citation][3] for a particular reference. (The exact format of the citation can be configured in the plugin settings.)

## License

Expand Down
22 changes: 19 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import {
InsertCitationModal,
InsertNoteLinkModal,
InsertNoteContentModal,
OpenNoteModal,
} from './modals';

Expand Down Expand Up @@ -128,6 +129,15 @@ export default class CitationPlugin extends Plugin {
},
});

this.addCommand({
id: 'insert-literature-note-content',
name: 'Insert literature note content in the current pane',
callback: () => {
const modal = new InsertNoteContentModal(this.app, this);
modal.open();
},
});

this.addCommand({
id: 'insert-markdown-citation',
name: 'Insert Markdown citation',
Expand Down Expand Up @@ -306,9 +316,6 @@ export default class CitationPlugin extends Plugin {
async insertLiteratureNoteLink(citekey: string): Promise<void> {
this.getOrCreateLiteratureNoteFile(citekey)
.then(() => {
// TODO what is the API for this?
console.log(this.app.workspace.activeLeaf);

const title = this.getTitleForCitekey(citekey),
linkText = `[[${title}]]`;
// console.log(this.app.metadataCache.fileToLinktext(file, this.app.vault.getRoot().path, true))
Expand All @@ -317,6 +324,15 @@ export default class CitationPlugin extends Plugin {
.catch(console.error);
}

/**
* Format literature note content for a given reference and insert in the
* currently active pane.
*/
async insertLiteratureNoteContent(citekey: string): Promise<void> {
const content = this.getInitialContentForCitekey(citekey);
this.editor.replaceRange(content, this.editor.getCursor());
}

async insertMarkdownCitation(
citekey: string,
alternative = false,
Expand Down
20 changes: 20 additions & 0 deletions src/modals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,26 @@ export class InsertNoteLinkModal extends SearchModal {
}
}

export class InsertNoteContentModal extends SearchModal {
constructor(app: App, plugin: CitationPlugin) {
super(app, plugin);

this.setInstructions([
{ command: '↑↓', purpose: 'to navigate' },
{
command: '↵',
purpose: 'to insert literature note content in active pane',
},
{ command: 'esc', purpose: 'to dismiss' },
]);
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
onChooseItem(item: Entry, evt: unknown): void {
this.plugin.insertLiteratureNoteContent(item.id).catch(console.error);
}
}

export class InsertCitationModal extends SearchModal {
constructor(app: App, plugin: CitationPlugin) {
super(app, plugin);
Expand Down

0 comments on commit 2c7acf9

Please sign in to comment.