Skip to content

Commit 376d235

Browse files
author
王一帆
committed
add search-results-command
1 parent 5865be6 commit 376d235

File tree

8 files changed

+161
-6
lines changed

8 files changed

+161
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This plugin for Obsidian deals multi notes from search results、current file、
88

99
## Features
1010

11+
- Add Search-Results-Command,can deal with search results directly---**New in v1.7.8**
1112
- Add Multi-File to target Canvas --- **New in v1.7.0**
1213
- Add text to target Canvas---**New in v1.7.6**
1314
- Add Dataview Task to target Canvas---**New in v1.7.7**

README_zh.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
## 功能
88

9+
- 添加针对搜索结果的命令,可直接针对搜索结果进行操作---**v1.7.8新增**
910
- 批量添加目标文档到指定Canvas---**v1.7.0新增**
1011
- 添加文本到指定Canvas---**v1.7.6新增**
1112
- 添加Dataview Task到指定Canvas---**v1.7.7新增**

main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Plugin, App, PluginSettingTab, Setting } from 'obsidian';
22
import { ClipboardCommand } from 'src/command/clipboard-command';
33
import { CurrentFileCommand } from 'src/command/current-file-command';
44
import { DataviewCommand } from 'src/command/dataview-command';
5+
import { SearchCommand } from 'src/command/search-command';
56
import * as internal from 'stream';
67

78
export default class FileCookerPlugin extends Plugin {
@@ -14,6 +15,7 @@ export default class FileCookerPlugin extends Plugin {
1415
new CurrentFileCommand(this).regist();
1516
new ClipboardCommand(this).regist();
1617
new DataviewCommand(this).regist();
18+
new SearchCommand(this).regist();
1719

1820
// This adds a settings tab so the user can configure various aspects of the plugin
1921
this.addSettingTab(new FileCookerSettingTab(this.app, this));

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "obsidian-file-cooker",
33
"name": "File Cooker",
4-
"version": "1.7.7",
4+
"version": "1.7.8",
55
"minAppVersion": "1.2.0",
66
"description": "Deal multi notes from Search results、current file、Dataview query string...",
77
"author": "iuian",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-file-cooker",
3-
"version": "1.7.7",
3+
"version": "1.7.8",
44
"description": "Deal batch notes from Search results、current file、Dataview query string...",
55
"main": "main.js",
66
"scripts": {

src/command/clipboard-command.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ export class ClipboardCommand implements Command {
108108
id: "Add-content-to",
109109
name: "Add content in clipboard to canvas ...",
110110
callback: () => {
111-
let actionFunc = (path: string): Action => { return new AddToCanvasAction(this.plugin.app, path); };
112-
new ChooseCanvasModal(this.plugin.app, new ClipboardContentReader(this.plugin, actionFunc)).open();
111+
new ChooseCanvasModal(this.plugin.app, new ClipboardContentReader(this.plugin)).open();
113112
}
114113
});
115114
}
@@ -119,8 +118,7 @@ export class ClipboardCommand implements Command {
119118
id: "add-files-in-clipboard-to-canvas",
120119
name: "Add files in clipboard to target canvas ...",
121120
callback: () => {
122-
let actionFunc = (path: string): Action => { return new AddToCanvasAction(this.plugin.app, path); };
123-
new ChooseCanvasModal(this.plugin.app, new ClipboardReader(this.plugin, actionFunc)).open();
121+
new ChooseCanvasModal(this.plugin.app, new ClipboardReader(this.plugin,)).open();
124122
}
125123
});
126124
}

src/command/search-command.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import FileCookerPlugin from 'main';
2+
import { Action } from 'src/action/action';
3+
import { AddToCanvasAction } from 'src/action/add-to-canvas-action';
4+
import { DeleteAction } from 'src/action/delete-action';
5+
import { EditFrontMatterAction } from 'src/action/edit-front-matter-action';
6+
import { MoveAction } from 'src/action/move-action';
7+
import { RenameAction } from 'src/action/rename-action';
8+
import { SyncFlomoAction } from 'src/action/sync-flomo-action';
9+
import { ChooseCanvasModal } from 'src/modal/choose-canvas-modal';
10+
import { ChooseFileModal } from 'src/modal/choose-file-modal';
11+
import { ChooseFolderModal } from 'src/modal/choose-folder-modal';
12+
import { SearchResultsReader } from 'src/reader/search-results-reader';
13+
import { Command } from './command';
14+
15+
export class SearchCommand implements Command {
16+
17+
plugin: FileCookerPlugin;
18+
19+
constructor(plugin: FileCookerPlugin) {
20+
this.plugin = plugin;
21+
}
22+
23+
regist(): void {
24+
this.registMoveFile();
25+
this.registSyncFlomo();
26+
this.registMergeFile();
27+
this.registDeleteFile();
28+
this.registEditProp();
29+
this.registRenameFile();
30+
// Canvas
31+
this.registAddFile2Canvas();
32+
}
33+
34+
private registRenameFile() {
35+
this.plugin.addCommand({
36+
id: 'rename-files-in-searchresults',
37+
name: 'Rename files in searchresults ...',
38+
callback: () => {
39+
new SearchResultsReader(this.plugin).read(new RenameAction(this.plugin.app));
40+
}
41+
});
42+
}
43+
44+
/**
45+
* Edit Front Matter
46+
* https://github.com/lijyze/obsidian-state-switcher/blob/d0a80081b0fcc1b899eed2e3d7e834c2d5703875/src/util.ts#L42
47+
*/
48+
private registEditProp() {
49+
let metaedit = this.plugin.app.plugins.plugins["metaedit"];
50+
51+
this.plugin.addCommand({
52+
id: 'edit-front-matter-in-searchresults-files',
53+
name: 'Edit Front Matter in searchresults files ...',
54+
checkCallback: (checking: boolean) => {
55+
if (!checking) {
56+
new SearchResultsReader(this.plugin).read(new EditFrontMatterAction(this.plugin.app));
57+
}
58+
return metaedit != null;
59+
}
60+
});
61+
}
62+
63+
private registDeleteFile() {
64+
this.plugin.addCommand({
65+
id: 'delete-files-in-searchresults',
66+
name: 'Delete files in searchresults!',
67+
callback: () => {
68+
new SearchResultsReader(this.plugin).read(new DeleteAction(this.plugin.app));
69+
}
70+
});
71+
}
72+
73+
private registMergeFile() {
74+
this.plugin.addCommand({
75+
id: 'merge-files-in-searchresults-to',
76+
name: 'Merge files in searchresults to ...',
77+
callback: () => {
78+
new ChooseFileModal(this.plugin.app, new SearchResultsReader(this.plugin)).open();
79+
}
80+
});
81+
}
82+
83+
private registSyncFlomo() {
84+
this.plugin.addCommand({
85+
id: 'sync-files-in-searchresults-to-flomo',
86+
name: 'Sync files in searchresults to flomo ...',
87+
callback: () => {
88+
new SearchResultsReader(this.plugin).read(new SyncFlomoAction(this.plugin));
89+
}
90+
});
91+
}
92+
93+
private registMoveFile() {
94+
this.plugin.addCommand({
95+
id: 'move-files-in-searchresults-to',
96+
name: 'Move files in searchresults to ...',
97+
callback: () => {
98+
let actionFunc = (path: string): Action => { return new MoveAction(this.plugin.app, path); };
99+
new ChooseFolderModal(this.plugin.app, new SearchResultsReader(this.plugin), actionFunc).open();
100+
}
101+
});
102+
}
103+
104+
private registAddFile2Canvas() {
105+
this.plugin.addCommand({
106+
id: "add-files-in-searchresults-to-canvas",
107+
name: "Add files in searchresults to target canvas ...",
108+
callback: () => {
109+
new ChooseCanvasModal(this.plugin.app, new SearchResultsReader(this.plugin)).open();
110+
}
111+
});
112+
}
113+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import FileCookerPlugin from "main";
2+
import { App, Notice, TAbstractFile } from "obsidian";
3+
import { Action } from "src/action/action";
4+
import hasMarkdownSuffix, { hasCanvasSuffix } from "src/utils/file-type-util";
5+
import { ReadInfo } from "./read-info";
6+
import { Readable } from "./readable";
7+
8+
9+
export class SearchResultsReader implements Readable {
10+
11+
plugin: FileCookerPlugin;
12+
app: App;
13+
14+
constructor(plugin: FileCookerPlugin) {
15+
this.plugin = plugin;
16+
this.app = plugin.app;
17+
}
18+
19+
read(action: Action): void {
20+
let readInfo = new ReadInfo(this.plugin.settings.limit);
21+
22+
let results = this.app.workspace.getLeavesOfType('search')[0].view.dom.resultDomLookup;
23+
24+
if (results.size == 0) {
25+
new Notice("Has no search results!Please search first!");
26+
} else {
27+
let arr = [...results.keys()];
28+
arr.forEach(k => {
29+
let ff = this.app.vault.getAbstractFileByPath(k.path);
30+
if (ff != null) {
31+
readInfo.addFile(ff);
32+
} else {
33+
console.log("can not find file :" + k.path);
34+
}
35+
});
36+
action.act(readInfo.getModels());
37+
}
38+
}
39+
40+
}

0 commit comments

Comments
 (0)