Skip to content

Commit 9759a49

Browse files
feat(create): Prompt for filename during creation
AI-assistant: Claude Code v2.1.138 (Claude Sonnet 4.6) Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
1 parent 014ae8e commit 9759a49

2 files changed

Lines changed: 96 additions & 3 deletions

File tree

‎src/__tests__/mindmap.spec.js‎

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6-
import { describe, it, expect, vi, beforeEach } from 'vitest'
6+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
77

88
import FilesMindMap from '../mindmap.js'
99
import { showMessage as showToast } from '@nextcloud/dialogs'
@@ -21,6 +21,7 @@ vi.mock('@nextcloud/router', () => ({
2121

2222
vi.mock('@nextcloud/dialogs', () => ({
2323
showMessage: vi.fn(() => ({ hideToast: vi.fn() })),
24+
prompt: vi.fn(),
2425
}))
2526

2627
vi.mock('@nextcloud/auth', () => ({
@@ -450,4 +451,83 @@ describe('FilesMindMap', () => {
450451
expect(FilesMindMap._file.supportedWrite).toBe(false)
451452
})
452453
})
454+
455+
// ─── registerNewFileMenuPlugin handler ────────────────────────────────────
456+
457+
describe('registerNewFileMenuPlugin handler', () => {
458+
let handler
459+
let context
460+
let prompt
461+
462+
beforeEach(async () => {
463+
;({ prompt } = await import('@nextcloud/dialogs'))
464+
465+
const { addNewFileMenuEntry: legacyAdd } = await import('@nextcloud/files-legacy')
466+
const { emit } = await import('@nextcloud/event-bus')
467+
468+
FilesMindMap.registerNewFileMenuPlugin()
469+
handler = legacyAdd.mock.calls[legacyAdd.mock.calls.length - 1][0].handler
470+
471+
context = {
472+
source: 'https://nc.example/remote.php/dav/files/testuser/docs',
473+
encodedSource: 'https://nc.example/remote.php/dav/files/testuser/docs',
474+
permissions: 4,
475+
root: '/files/testuser',
476+
}
477+
478+
axios.mockResolvedValue({
479+
headers: { 'oc-fileid': '42' },
480+
})
481+
482+
global.OCA = { Viewer: { openWith: vi.fn() } }
483+
})
484+
485+
afterEach(() => {
486+
delete global.OCA
487+
})
488+
489+
it('creates the file with the user-supplied name', async () => {
490+
prompt.mockResolvedValue('My map.km')
491+
492+
await handler(context, [])
493+
494+
expect(axios).toHaveBeenCalledWith(expect.objectContaining({
495+
method: 'PUT',
496+
url: expect.stringContaining('My%20map.km'),
497+
}))
498+
})
499+
500+
it('does not create the file when the dialog is cancelled', async () => {
501+
prompt.mockResolvedValue(null)
502+
503+
await handler(context, [])
504+
505+
expect(axios).not.toHaveBeenCalled()
506+
})
507+
508+
it('falls back to the default name when the user submits an empty string', async () => {
509+
prompt.mockResolvedValue('')
510+
511+
await handler(context, [])
512+
513+
expect(axios).toHaveBeenCalledWith(expect.objectContaining({
514+
method: 'PUT',
515+
url: expect.stringContaining('New%20mind%20map.km'),
516+
}))
517+
})
518+
519+
it('appends a suffix via getUniqueName when the name already exists', async () => {
520+
const { getUniqueName } = await import('@nextcloud/files')
521+
getUniqueName.mockReturnValueOnce('My map (2).km')
522+
prompt.mockResolvedValue('My map.km')
523+
524+
const content = [{ basename: 'My map.km' }]
525+
await handler(context, content)
526+
527+
expect(getUniqueName).toHaveBeenCalledWith('My map.km', ['My map.km'])
528+
expect(axios).toHaveBeenCalledWith(expect.objectContaining({
529+
url: expect.stringContaining('My%20map%20(2).km'),
530+
}))
531+
})
532+
})
453533
})

‎src/mindmap.js‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { dirname } from '@nextcloud/paths'
2929
import { isPublicShare } from '@nextcloud/sharing/public'
3030
import { translate as t } from '@nextcloud/l10n'
3131
import { generateUrl } from '@nextcloud/router'
32-
import { showMessage as showToast } from '@nextcloud/dialogs'
32+
import { showMessage as showToast, prompt } from '@nextcloud/dialogs'
3333

3434
import util from './util.js'
3535
import km from './plugins/km.js'
@@ -242,7 +242,20 @@ const FilesMindMap = {
242242
},
243243
async handler(context, content) {
244244
const contentNames = content.map((node) => node.basename)
245-
const fileName = getUniqueName(t('files_mindmap', 'New mind map.km'), contentNames)
245+
const defaultName = t('files_mindmap', 'New mind map.km')
246+
247+
const userInput = await prompt(
248+
t('files_mindmap', 'New mind map'),
249+
t('files_mindmap', 'Enter the name for the new mind map'),
250+
defaultName,
251+
)
252+
253+
if (userInput === null) {
254+
return
255+
}
256+
257+
const enteredName = userInput.trim() || defaultName
258+
const fileName = getUniqueName(enteredName, contentNames)
246259
const source = context.encodedSource + '/' + encodeURIComponent(fileName)
247260

248261
const response = await axios({

0 commit comments

Comments
 (0)