-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathnewFile.ts
More file actions
51 lines (45 loc) · 1.93 KB
/
Copy pathnewFile.ts
File metadata and controls
51 lines (45 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import FileTreeAlternativePlugin from 'main';
import { stripIndents } from 'common-tags';
import dayjs from 'dayjs';
import { TFile, TFolder, App } from 'obsidian';
import { eventTypes, OZFile } from 'utils/types';
export const openFile = (props: { file: OZFile; app: App; newLeaf: boolean; leafBySplit?: boolean }) => {
const { file, app, newLeaf, leafBySplit } = props;
let fileToOpen = app.vault.getAbstractFileByPath(file.path);
if (!fileToOpen) return;
let leaf = app.workspace.getLeaf(newLeaf);
if (leafBySplit) leaf = app.workspace.createLeafBySplit(leaf, 'vertical');
app.workspace.setActiveLeaf(leaf, {
focus: true,
});
leaf.openFile(fileToOpen as TFile, { eState: { focus: true } });
};
export const getFileCreateString = (params: { plugin: FileTreeAlternativePlugin; fileName: string }): string => {
const { plugin, fileName } = params;
return stripIndents`
${
plugin.settings.createdYaml
? `
---
created: ${dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss')}
---
`
: ''
}
${plugin.settings.fileNameIsHeader ? `# ${fileName}` : ''}
`;
};
export const createNewMarkdownFile = async (plugin: FileTreeAlternativePlugin, folder: TFolder, newFileName: string, content?: string) => {
// @ts-ignore
const newFile = await plugin.app.fileManager.createNewMarkdownFile(folder, newFileName);
if (content && content !== '') await plugin.app.vault.modify(newFile, content);
openFile({ file: newFile, app: plugin.app, newLeaf: false });
let evt = new CustomEvent(eventTypes.activeFileChange, { detail: { filePath: newFile.path } });
window.dispatchEvent(evt);
};
export const openFileInNewTab = (app: App, file: OZFile) => {
openFile({ file: file, app: app, newLeaf: true });
};
export const openFileInNewTabGroup = (app: App, file: OZFile) => {
openFile({ file: file, app: app, newLeaf: false, leafBySplit: true });
};