Skip to content

Commit

Permalink
feat: search
Browse files Browse the repository at this point in the history
close #90
  • Loading branch information
Vinzent03 committed Nov 25, 2022
1 parent e1079a3 commit 52a2f4c
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ You can for example
- [create files](https://vinzent03.github.io/obsidian-advanced-uri/actions/writing)
- [open workspaces](https://vinzent03.github.io/obsidian-advanced-uri/actions/navigation)
- [navigate to headings/blocks](https://vinzent03.github.io/obsidian-advanced-uri/actions/navigation)
- [automated search and replace in a file](https://vinzent03.github.io/obsidian-advanced-uri/actions/search_replace)
- [automated search and replace in a file](https://vinzent03.github.io/obsidian-advanced-uri/actions/search)

Please read the [documentation](https://vinzent03.github.io/obsidian-advanced-uri) for a detailed explanation.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@
sidebar_position: 4
---

# Search and Replace
# Search

## Per File Search

| / | parameters | explanation |
| --------------------- | ------------------------- | ----------------------------------------- |
| Normal | search | Searches for `search` in the current file |
| Normal with open file | search, <identification\> | Opens specific file and searches `search` |

## Per File Search and Replace

| / | parameters | explanation |
| ------ | --------------------------------------- | ---------------------------------------------------------------------------- |
| Normal | search, replace | Replaces every occurrence of `search` with `replace` in the current file |
| Normal | search, replace, <identification\> | Replaces every occurrence of `search` with `replace` in file |
| RegEx | searchregex, replace | Uses `searchregex` to replace every match with `replace` in the current file |
| RegEx | searchregex, replace, <identification\> | Uses `searchregex` to replace every match with `replace` in file |
| RegEx | searchregex, replace, <identification\> | Uses `searchregex` to replace every match with `replace` in file |
2 changes: 1 addition & 1 deletion docs/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ You can for example
- [create files](actions/writing.md)
- [open workspaces](actions/navigation.md)
- [navigate to headings/blocks](actions/navigation.md)
- [automated search and replace in a file](actions/search_replace.md)
- [automated search and replace in a file](actions/search.md)

## Motivation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ slug: /
- [创建文件](actions/writing.md)
- [打开工作区](actions/navigation.md)
- [定位到标题/区块](actions/navigation.md)
- [文件内自动搜索替换](actions/search_replace.md)
- [文件内自动搜索替换](actions/search.md)

## 开发目的

Expand Down
16 changes: 15 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { base64ToArrayBuffer, CachedMetadata, MarkdownView, normalizePath, Notice, parseFrontMatterAliases, parseFrontMatterEntry, Plugin, TAbstractFile, TFile, TFolder } from "obsidian";
import { base64ToArrayBuffer, CachedMetadata, FileView, MarkdownView, normalizePath, Notice, parseFrontMatterAliases, parseFrontMatterEntry, Plugin, TAbstractFile, TFile, TFolder } from "obsidian";
import { stripMD } from "obsidian-community-lib";
import { appHasDailyNotesPluginLoaded, createDailyNote, getAllDailyNotes, getDailyNote } from "obsidian-daily-notes-interface";
import { v4 as uuidv4 } from 'uuid';
Expand Down Expand Up @@ -182,6 +182,9 @@ export default class AdvancedURI extends Plugin {
} else if ((parameters.search || parameters.searchregex) && parameters.replace != undefined) {
this.handleSearchAndReplace(parameters);

} else if (parameters.search) {
this.handleSearch(parameters);

} else if (parameters.filepath) {
this.handleOpen(parameters);

Expand Down Expand Up @@ -432,6 +435,17 @@ export default class AdvancedURI extends Plugin {
}
}

async handleSearch(parameters: Parameters) {
if (parameters.filepath) {
await this.open({ file: parameters.filepath, parameters: parameters });
}
const view = this.app.workspace.getActiveViewOfType(FileView);
view.currentMode.showSearch();
const search = view.currentMode.search;
search.searchInputEl.value = parameters.search;
search.searchInputEl.dispatchEvent(new Event("input"));
}

async handleWrite(parameters: Parameters, createdDailyNote: boolean = false) {
let file: TAbstractFile | null;
if (parameters.filepath) {
Expand Down
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ declare module 'obsidian' {
}
interface View {
file: TFile;

}
interface FileView {
currentMode: {
showSearch(): void;
search: {
searchInputEl: HTMLInputElement;
};
};
}
interface WorkspaceLeaf {
width: number;
Expand Down

0 comments on commit 52a2f4c

Please sign in to comment.