Skip to content

Commit 9cbdf28

Browse files
committed
Merge branch 'main' of https://github.com/NotePlan/plugins
2 parents f1ab75a + cc55653 commit 9cbdf28

File tree

4 files changed

+37
-43
lines changed

4 files changed

+37
-43
lines changed

helpers/userInput.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,21 @@ export async function chooseOptionWithModifiers<T, TDefault = T>(
131131

132132
/**
133133
* Show a list of options to the user and return which option they picked (and whether they used a modifier key), optionally with ability to create a new item.
134-
* V2: without the <TDefault> type parameter, using the new CommandBar.showOptions() options from v3.18
134+
* This is a fork of chooseOptionWithModifiers(), without the <TDefault> type parameter, using the new CommandBar.showOptions() options from v3.18
135+
* Note: requires at least v3.18
135136
* @author @jgclark, @dwertheimer based on @nmn chooseOption
136137
*
137138
* @param {string} message - text to display to user
138139
* @param {Array<TCommandBarOptionObject>} options - array of options to display
139140
* @param {TCommandBarOptionObject} additionalCreateNewOption - optional option object to create a new item
140141
* @returns {Promise<TCommandBarResultObject>} - the object that was chosen, plus an index of the chosen option and keyModifiers array. If the user created a new item, the index will be -1.
141142
*/
142-
export async function chooseOptionWithModifiersV2(
143+
export async function chooseDecoratedOptionWithModifiers(
143144
message: string,
144145
options: Array<TCommandBarOptionObject>,
145146
additionalCreateNewOption?: TCommandBarOptionObject,
146147
): Promise<TCommandBarResultObject> {
147-
logDebug('userInput / chooseOptionWithModifiersV2()', `Will showOptions with ${options.length} options & prompt: "${message}"`)
148+
logDebug('userInput / chooseDecoratedOptionWithModifiers()', `Will showOptions with ${options.length} options & prompt: "${message}"`)
148149

149150
// label field is used elsewhere, but @eduardme made showOptions use text instead, so we map it back to label
150151
if (Array.isArray(options) && options.length > 0) {
@@ -158,16 +159,12 @@ export async function chooseOptionWithModifiersV2(
158159
if (additionalCreateNewOption) {
159160
displayOptions.unshift(additionalCreateNewOption)
160161
}
161-
// logDebug('userInput / chooseOptionWithModifiersV2()', `displayOptions: ${displayOptions.length} options`)
162-
// for TEST:
163-
for(let i = 0; i < 15; i++) {
164-
console.log(`- ${i}: ${displayOptions[i].text}`)
165-
}
162+
// logDebug('userInput / chooseDecoratedOptionWithModifiers()', `displayOptions: ${displayOptions.length} options`)
166163

167164
// Use newer CommandBar.showOptions() from v3.18
168165
const result = await CommandBar.showOptions(displayOptions, message, '')
169166
const { index, keyModifiers } = result
170-
clo(result, `chooseOptionWithModifiersV2 chosen result`)
167+
clo(result, `chooseDecoratedOptionWithModifiers chosen result`)
171168
clo(displayOptions[index], `and relevant displayOption`)
172169

173170
// Check if the user selected "Add new item"
@@ -390,7 +387,7 @@ export async function chooseFolder(
390387
if (includeNewFolderOption) {
391388
// Add in the new folder option just for newer CommandBar use
392389
decoratedFolderOptions.unshift(addDecoratedNewFolderOption)
393-
result = await chooseOptionWithModifiersV2(msg, decoratedFolderOptions) // note disabling the add new folder option as we need to handle it with access to folders array
390+
result = await chooseDecoratedOptionWithModifiers(msg, decoratedFolderOptions) // note disabling the add new folder option as we need to handle it with access to folders array
394391
keyModifiers = result.keyModifiers || []
395392
if (keyModifiers.length > 0 && keyModifiers.indexOf('opt') > -1) {
396393
optClickedOnFolder = true
@@ -403,13 +400,15 @@ export async function chooseFolder(
403400
} else if (optClickedOnFolder) {
404401
// i.e. new folder wanted, and parent folder chosen
405402
folder = folders[result.index-1] // to ignore the added new folder option if present
403+
newFolderWanted = true
406404
}
407405
else {
408406
folder = folders[result.index-1] // to ignore the added new folder option if present
409407
// newFolderWanted = value === NEW_FOLDER
410408
}
411409

412-
// Handle new folder creation
410+
// Handle new folder creation, if requested
411+
if (newFolderWanted) {
413412
const newFolderPath = await handleNewFolderCreation(folder, startFolder, includeArchive, includeFolderPath, excludeTeamspaces, forceOriginalCommandBar)
414413
if (newFolderPath) {
415414
folder = newFolderPath
@@ -418,18 +417,13 @@ export async function chooseFolder(
418417
} else {
419418
throw new Error(`Failed to create new folder "${folder}"`)
420419
}
420+
}
421421

422422
} else {
423423
// not including add new folder option
424-
result = await chooseOptionWithModifiersV2(msg, decoratedFolderOptions), //includeNewFolderOption ? addDecoratedNewFolderOption : undefined) // note now wanting to disable the add new folder option as we are handling it specifically here
425-
clo(result, 'chooseFolder chooseOptionWithModifiersV2 result') // ✅
426-
if (result.index === -1) {
427-
// i.e. new folder wanted, but no name given yet
428-
value = ''
429-
newFolderWanted = true
430-
} else {
431-
value = folders[result.index]
432-
}
424+
result = await chooseDecoratedOptionWithModifiers(msg, decoratedFolderOptions)
425+
clo(result, 'chooseFolder chooseDecoratedOptionWithModifiers result') // ✅
426+
value = folders[result.index]
433427
}
434428
} else {
435429
// ✅ for both private + teamspace

jgclark.NoteHelpers/src/index.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
// allow changes in plugin.json to trigger recompilation
1010
import pluginJson from '../plugin.json'
1111

12-
import { JSP, logDebug, logError } from '@helpers/dev'
12+
import { JSP, logDebug, logError, logInfo } from '@helpers/dev'
1313
// import { migrateCommandsIfNecessary } from '@helpers/NPConfiguration'
1414
import { editSettings } from '@helpers/NPSettings'
15-
import { showMessage } from '@helpers/userInput'
15+
import { chooseFolder, showMessage } from '@helpers/userInput'
1616

1717
export { countAndAddDays } from './countDays'
1818
export { indexFolders, updateAllIndexes } from './indexFolders'
@@ -22,7 +22,7 @@ export { renameInconsistentNames } from './lib/commands/renameInconsistentNames'
2222
export { titleToFilename } from './lib/commands/titleToFilename'
2323
export { listPublishedNotes } from './listPublishedNotes'
2424
export { newNote, newNoteFromClipboard, newNoteFromSelection } from './newNote'
25-
export { addTriggerToNote, convertLocalLinksToPluginLinks, addFrontmatterToNote, moveNote, logEditorNoteDetailed, renameNoteFile, trashNote, testChooseFolder } from './noteHelpers'
25+
export { addTriggerToNote, convertLocalLinksToPluginLinks, addFrontmatterToNote, moveNote, logEditorNoteDetailed, renameNoteFile, trashNote } from './noteHelpers'
2626
export {
2727
jumpToDone,
2828
jumpToHeading,
@@ -85,3 +85,20 @@ export async function updateSettings(): Promise<void> {
8585
logError(pluginJson, JSP(error))
8686
}
8787
}
88+
89+
//-----------------------------------------------------------------
90+
91+
/**
92+
* Test the chooseFolder() function
93+
* Call: noteplan://x-callback-url/runPlugin?pluginID=jgclark.NoteHelpers&command=test%3A%20choose%20folder
94+
* @author @jgclark
95+
*/
96+
export async function testChooseFolder(): Promise<void> {
97+
try {
98+
const selectedFolder = await chooseFolder(`TEST: Select a folder`, true, true, '', true, false, false)
99+
logInfo('testChooseFolder', `Selected folder: ${selectedFolder}`)
100+
} catch (err) {
101+
logError('noteHelpers/testChooseFolder', err.message)
102+
await showMessage(err.message)
103+
}
104+
}

jgclark.NoteHelpers/src/noteHelpers.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,6 @@ export async function getSettings(): Promise<any> {
5252

5353
//-----------------------------------------------------------------
5454

55-
/**
56-
* Test the chooseFolder() function
57-
* Call: noteplan://x-callback-url/runPlugin?pluginID=jgclark.NoteHelpers&command=test%3A%20choose%20folder
58-
* @author @jgclark
59-
*/
60-
export async function testChooseFolder(): Promise<void> {
61-
try {
62-
const selectedFolder = await chooseFolder(`TEST: Select a folder`, true, true, '', true, false, true)
63-
logInfo('testChooseFolder', `Selected folder: ${selectedFolder}`)
64-
} catch (err) {
65-
logError('noteHelpers/testChooseFolder', err.message)
66-
await showMessage(err.message)
67-
}
68-
}
69-
70-
//-----------------------------------------------------------------
71-
7255
export async function logEditorNoteDetailed(): Promise<void> {
7356
try {
7457
if (!Editor || !Editor.note) {

np.CallbackURLs/src/NPOpenFolders.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @flow
22

33
import { logDebug, clo } from '../../helpers/dev'
4-
import { showMessage, createFolderRepresentation, chooseOptionWithModifiersV2, chooseOption } from '@helpers/userInput'
4+
import { showMessage, createFolderRepresentation, chooseDecoratedOptionWithModifiers, chooseOption } from '@helpers/userInput'
55
import { getFolderViewData, getFoldersWithNamedViews, getNamedViewsForFolder } from '@helpers/folders'
66
import { getAllTeamspaceIDsAndTitles } from '@helpers/NPTeamspace'
77

@@ -81,7 +81,7 @@ function createFolderOptions(allFolders: $ReadOnlyArray<string>, folderData: Obj
8181
async function selectFolder(folderOptions: Array<Object>): Promise<Object | null> {
8282
clo(folderOptions, `selectFolder: folderOptions`)
8383

84-
const selection = await chooseOptionWithModifiersV2('Choose a folder', folderOptions)
84+
const selection = await chooseDecoratedOptionWithModifiers('Choose a folder', folderOptions)
8585
if (!selection) return null
8686

8787
const selectedFolderObj = folderOptions[selection.index]
@@ -128,7 +128,7 @@ async function selectView(viewOptions: Array<Object>, selectedFolder: string): P
128128
return viewOptions[0].value
129129
}
130130
clo(viewOptions, `selectView viewOptions`)
131-
const responseObj = await chooseOptionWithModifiersV2(`Choose a view for '${selectedFolder}'`, viewOptions)
131+
const responseObj = await chooseDecoratedOptionWithModifiers(`Choose a view for '${selectedFolder}'`, viewOptions)
132132
if (responseObj) {
133133
clo(responseObj, `selectView responseObj`)
134134
return responseObj.value

0 commit comments

Comments
 (0)