@@ -14,12 +14,16 @@ import {
1414 Plugin ,
1515 PluginSettingTab ,
1616 Setting ,
17- SuggestModal ,
1817 TFile ,
1918 WorkspaceLeaf ,
2019 TAbstractFile ,
2120 type MarkdownFileInfo ,
2221} from "obsidian" ;
22+ import { createObsidianUi , type UiInteractions } from "@vrtmrz/obsidian-plugin-kit/ui" ;
23+ import {
24+ createObsidianVaultTextAccess ,
25+ type VaultTextAccess ,
26+ } from "@vrtmrz/obsidian-plugin-kit/vault" ;
2327
2428import {
2529 DEFAULT_SETTINGS ,
@@ -56,7 +60,11 @@ import {
5660 trimPrefix ,
5761 uniqueCaseIntensive
5862} from "./util" ;
59- import { renderTagFolderTemplateVariables } from "./new-note-template" ;
63+ import {
64+ chooseNewNoteTemplate ,
65+ populateNewNote ,
66+ type NewNoteTemplateChoice ,
67+ } from "./new-note-workflow" ;
6068import { ScrollView } from "./ScrollView" ;
6169import { TagFolderView } from "./TagFolderView" ;
6270import { TagFolderList } from "./TagFolderList" ;
@@ -100,8 +108,6 @@ function getCompareMethodItems(settings: TagFolderSettings) {
100108 }
101109}
102110
103- type NewNoteTemplateChoice = TFile ;
104-
105111function getCoreTemplatesFolder ( app : App ) : string | null {
106112 const internalPlugins = ( app as App & {
107113 internalPlugins ?: {
@@ -135,44 +141,6 @@ function getTemplateFiles(app: App) {
135141 return templates . sort ( ( a , b ) => compare ( a . path , b . path ) ) ;
136142}
137143
138- class NewNoteTemplateSuggestModal extends SuggestModal < NewNoteTemplateChoice > {
139- private callback ?: ( template : NewNoteTemplateChoice | false ) => void ;
140- private templates : TFile [ ] ;
141-
142- constructor ( app : App , templates : TFile [ ] , callback : ( template : NewNoteTemplateChoice | false ) => void ) {
143- super ( app ) ;
144- this . templates = templates ;
145- this . callback = callback ;
146- this . setPlaceholder ( "Type to search templates..." ) ;
147- }
148-
149- getSuggestions ( query : string ) : NewNoteTemplateChoice [ ] {
150- const normalizedQuery = query . toLowerCase ( ) ;
151- return this . templates . filter ( ( file ) =>
152- file . path . toLowerCase ( ) . contains ( normalizedQuery )
153- ) ;
154- }
155-
156- renderSuggestion ( template : NewNoteTemplateChoice , el : HTMLElement ) {
157- el . createDiv ( { text : template . basename } ) ;
158- el . createDiv ( { text : template . path , cls : "suggestion-note" } ) ;
159- }
160-
161- onChooseSuggestion ( template : NewNoteTemplateChoice ) {
162- this . callback ?.( template ) ;
163- this . callback = undefined ;
164- }
165-
166- onClose ( ) : void {
167- window . setTimeout ( ( ) => {
168- if ( this . callback ) {
169- this . callback ( false ) ;
170- this . callback = undefined ;
171- }
172- } , 100 ) ;
173- }
174- }
175-
176144class NewNoteTemplateInputSuggest extends AbstractInputSuggest < TFile > {
177145 private callback : ( template : TFile ) => void ;
178146
@@ -201,20 +169,19 @@ class NewNoteTemplateInputSuggest extends AbstractInputSuggest<TFile> {
201169 }
202170}
203171
204- function askNewNoteTemplate ( app : App ) : Promise < NewNoteTemplateChoice | false > {
205- return new Promise ( ( resolve ) => {
206- const templates = getTemplateFiles ( app ) ;
207- if ( templates . length == 0 ) {
208- new Notice ( "No templates found" ) ;
209- resolve ( false ) ;
210- return ;
211- }
212- const modal = new NewNoteTemplateSuggestModal ( app , templates , resolve ) ;
213- modal . open ( ) ;
214- } ) ;
172+ async function askNewNoteTemplate ( ui : UiInteractions , app : App ) : Promise < NewNoteTemplateChoice | null > {
173+ const templates = getTemplateFiles ( app ) . map ( ( file ) => ( {
174+ path : file . path ,
175+ name : file . basename ,
176+ } ) ) ;
177+ if ( templates . length == 0 ) {
178+ new Notice ( "No templates found" ) ;
179+ return null ;
180+ }
181+ return await chooseNewNoteTemplate ( ui , templates ) ;
215182}
216183
217- function getConfiguredNewNoteTemplate ( app : App , templatePath : string ) : TFile | null {
184+ function getConfiguredNewNoteTemplate ( app : App , templatePath : string ) : NewNoteTemplateChoice | null {
218185 const inputPath = normalizeNewNoteTemplatePath ( templatePath ) ;
219186 if ( inputPath == "" ) return null ;
220187
@@ -240,7 +207,7 @@ function getConfiguredNewNoteTemplate(app: App, templatePath: string): TFile | n
240207 return null ;
241208 }
242209
243- return file ;
210+ return { path : file . path , name : file . basename } ;
244211}
245212
246213function normalizeNewNoteTemplatePath ( templatePath : string ) {
@@ -260,6 +227,8 @@ function onElement<T extends HTMLElement | Document>(el: T, event: string, selec
260227
261228export default class TagFolderPlugin extends Plugin {
262229 settings : TagFolderSettings = { ...DEFAULT_SETTINGS } ;
230+ ui ! : UiInteractions ;
231+ vaultText ! : VaultTextAccess ;
263232
264233 // Folder opening status.
265234 expandedFolders : string [ ] = [ "root" ] ;
@@ -367,6 +336,8 @@ export default class TagFolderPlugin extends Plugin {
367336
368337 async onload ( ) {
369338 await this . loadSettings ( ) ;
339+ this . ui = createObsidianUi ( this . app ) ;
340+ this . vaultText = createObsidianVaultTextAccess ( this . app . vault ) ;
370341 this . hoverPreview = this . hoverPreview . bind ( this ) ;
371342 this . modifyFile = this . modifyFile . bind ( this ) ;
372343 this . setSearchString = this . setSearchString . bind ( this ) ;
@@ -1402,31 +1373,28 @@ export default class TagFolderPlugin extends Plugin {
14021373 const selectedTemplate = configuredTemplatePath == ""
14031374 ? null
14041375 : ( getConfiguredNewNoteTemplate ( this . app , configuredTemplatePath )
1405- ?? await askNewNoteTemplate ( this . app ) ) ;
1376+ ?? await askNewNoteTemplate ( this . ui , this . app ) ) ;
14061377
14071378 //@ts -ignore
14081379 const ww = await this . app . fileManager . createAndOpenMarkdownFile ( ) ;
14091380 if ( ! ( ww instanceof TFile ) ) return ;
1410- if ( selectedTemplate != null && selectedTemplate !== false ) {
1411- const template = await this . app . vault . read ( selectedTemplate ) ;
1412- const renderedTemplate = renderTagFolderTemplateVariables ( template , expandedTagsAll , expandedTags ) ;
1413- if ( renderedTemplate . trim ( ) != "" ) {
1414- await this . app . vault . modify ( ww , renderedTemplate ) ;
1415- }
1416- return ;
1417- }
1418-
1419- if ( this . settings . useFrontmatterTagsForNewNotes ) {
1420- await this . app . fileManager . processFrontMatter ( ww , ( matter ) => {
1421- matter . tags = matter . tags ?? [ ] ;
1422- matter . tags = expandedTagsAll
1423- . filter ( e => ! isSpecialTag ( e ) )
1424- . filter ( e => matter . tags . indexOf ( e ) < 0 )
1425- . concat ( matter . tags ) ;
1426- } ) ;
1427- } else {
1428- await this . app . vault . append ( ww , expandedTags ) ;
1429- }
1381+ await populateNewNote ( {
1382+ vault : this . vaultText ,
1383+ notePath : ww . path ,
1384+ template : selectedTemplate ,
1385+ expandedTagsAll,
1386+ expandedTags,
1387+ frontmatterTags : expandedTagsAll . filter ( e => ! isSpecialTag ( e ) ) ,
1388+ useFrontmatterTags : this . settings . useFrontmatterTagsForNewNotes ,
1389+ applyFrontmatterTags : async ( frontmatterTags ) => {
1390+ await this . app . fileManager . processFrontMatter ( ww , ( matter ) => {
1391+ matter . tags = matter . tags ?? [ ] ;
1392+ matter . tags = frontmatterTags
1393+ . filter ( e => matter . tags . indexOf ( e ) < 0 )
1394+ . concat ( matter . tags ) ;
1395+ } ) ;
1396+ } ,
1397+ } ) ;
14301398 }
14311399}
14321400
0 commit comments