generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #203 from DominikPieper/dev
Release 0.10.0
- Loading branch information
Showing
35 changed files
with
962 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,5 +47,6 @@ module.exports = { | |
ignoreDeclarationSort: true, | ||
}, | ||
], | ||
'no-useless-escape': 0 | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { Editor, Notice } from 'obsidian'; | ||
import ParserCreator from './parsers/ParserCreator'; | ||
import { VaultRepository } from './repository/VaultRepository'; | ||
import { Note } from './parsers/Note'; | ||
import FileExistsError from './error/FileExists'; | ||
import FileExistsAsk from './modal/FileExistsAsk'; | ||
import { FileExistsStrategy } from './enums/fileExistsStrategy'; | ||
import ReadItLaterPlugin from './main'; | ||
import { getAndCheckUrls } from './helpers/stringUtils'; | ||
|
||
export class NoteService { | ||
constructor( | ||
private parserCreator: ParserCreator, | ||
private plugin: ReadItLaterPlugin, | ||
private repository: VaultRepository, | ||
) {} | ||
|
||
public async createNote(content: string): Promise<void> { | ||
const note = await this.makeNote(content); | ||
try { | ||
await this.repository.saveNote(note); | ||
} catch (error) { | ||
if (error instanceof FileExistsError) { | ||
this.handleFileExistsError([note]); | ||
} | ||
} | ||
} | ||
|
||
public async createNotesFromBatch(contentBatch: string): Promise<void> { | ||
const urlCheckResult = getAndCheckUrls(contentBatch, this.plugin.settings.batchProcessDelimiter); | ||
const existingNotes: Note[] = []; | ||
|
||
for (const contentSegment of urlCheckResult.everyLineIsURL ? urlCheckResult.urls : [contentBatch]) { | ||
const note = await this.makeNote(contentSegment); | ||
try { | ||
await this.repository.saveNote(note); | ||
} catch (error) { | ||
if (error instanceof FileExistsError) { | ||
existingNotes.push(note); | ||
} | ||
} | ||
} | ||
|
||
if (existingNotes.length > 0) { | ||
this.handleFileExistsError(existingNotes); | ||
} | ||
} | ||
|
||
public async insertContentAtEditorCursorPosition(content: string, editor: Editor): Promise<void> { | ||
const note = await this.makeNote(content); | ||
editor.replaceRange(note.content, editor.getCursor()); | ||
} | ||
|
||
private async makeNote(content: string): Promise<Note> { | ||
const parser = await this.parserCreator.createParser(content); | ||
return await parser.prepareNote(content); | ||
} | ||
|
||
private openNote(note: Note): void { | ||
if (this.plugin.settings.openNewNote || this.plugin.settings.openNewNoteInNewTab) { | ||
try { | ||
const file = this.repository.getFileByPath(note.filePath); | ||
this.plugin.app.workspace | ||
.getLeaf(this.plugin.settings.openNewNoteInNewTab ? 'tab' : false) | ||
.openFile(file); | ||
} catch (error) { | ||
console.error(error); | ||
new Notice(`Unable to open ${note.getFullFilename()}`); | ||
} | ||
} | ||
} | ||
|
||
private async handleFileExistsError(notes: Note[]): Promise<void> { | ||
switch (this.plugin.settings.fileExistsStrategy) { | ||
case FileExistsStrategy.Ask: | ||
new FileExistsAsk(this.plugin.app, notes, (strategy, doNotAskAgain) => { | ||
this.handleFileAskModalResponse(strategy, doNotAskAgain, notes); | ||
}).open(); | ||
break; | ||
case FileExistsStrategy.Nothing: | ||
this.handleFileExistsStrategyNothing(notes); | ||
break; | ||
case FileExistsStrategy.AppendToExisting: | ||
this.handleFileExistsStrategyAppend(notes); | ||
break; | ||
} | ||
} | ||
|
||
private async handleFileAskModalResponse( | ||
strategy: FileExistsStrategy, | ||
doNotAskAgain: boolean, | ||
notes: Note[], | ||
): Promise<void> { | ||
switch (strategy) { | ||
case FileExistsStrategy.Nothing: | ||
this.handleFileExistsStrategyNothing(notes); | ||
break; | ||
case FileExistsStrategy.AppendToExisting: | ||
this.handleFileExistsStrategyAppend(notes); | ||
break; | ||
} | ||
|
||
if (doNotAskAgain) { | ||
this.plugin.saveSetting('fileExistsStrategy', strategy as FileExistsStrategy); | ||
} | ||
|
||
if (notes.length === 1) { | ||
this.openNote(notes.shift()); | ||
} | ||
} | ||
|
||
private async handleFileExistsStrategyAppend(notes: Note[]): Promise<void> { | ||
for (const note of notes) { | ||
try { | ||
await this.repository.appendToExistingNote(note); | ||
new Notice(`${note.getFullFilename()} was updated.`); | ||
} catch (error) { | ||
console.error(error); | ||
new Notice(`${note.getFullFilename()} was not updated!`, 0); | ||
} | ||
} | ||
} | ||
|
||
private handleFileExistsStrategyNothing(notes: Note[]): void { | ||
for (const note of notes) { | ||
new Notice(`${note.getFullFilename()} already exists.`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Editor } from 'obsidian'; | ||
import { NoteService } from './NoteService'; | ||
|
||
export class ReadItLaterApi { | ||
constructor(private noteService: NoteService) {} | ||
|
||
/** | ||
* Create single note from provided input. | ||
*/ | ||
public async processContent(content: string): Promise<void> { | ||
this.noteService.createNote(content); | ||
} | ||
|
||
/** | ||
* Create multiple notes from provided input delimited by delimiter defined in plugin settings. | ||
*/ | ||
public async processContentBatch(contentBatch: string): Promise<void> { | ||
this.noteService.createNotesFromBatch(contentBatch); | ||
} | ||
|
||
/** | ||
* Insert processed content from input at current position in editor. | ||
*/ | ||
public async insertContentAtEditorCursorPosition(content: string, editor: Editor): Promise<void> { | ||
this.noteService.insertContentAtEditorCursorPosition(content, editor); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface DropdownEnumOption { | ||
label: string; | ||
option: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { DropdownEnumOption } from './enum'; | ||
|
||
export enum FileExistsStrategy { | ||
AppendToExisting = 'appendToExisting', | ||
Ask = 'ask', | ||
Nothing = 'nothing', | ||
} | ||
|
||
export function getFileExistStrategyOptions(): DropdownEnumOption[] { | ||
return [ | ||
{ label: 'Ask', option: FileExistsStrategy.Ask }, | ||
{ label: 'Nothing', option: FileExistsStrategy.Nothing }, | ||
{ label: 'Append to the existing note', option: FileExistsStrategy.AppendToExisting }, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default class FileExistsError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = 'FileExistsError'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default class FileNotFoundError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = 'FileNotFoundError'; | ||
} | ||
} |
Oops, something went wrong.