Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 0 additions & 69 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"@nextcloud/dialogs": "^7.5.0",
"@nextcloud/event-bus": "^3.1.0",
"@nextcloud/files": "^4.1.0",
"@nextcloud/files-legacy": "npm:@nextcloud/files@^3.12.2",
"@nextcloud/l10n": "^3.0.0",
"@nextcloud/logger": "^3.0.0",
"@nextcloud/paths": "^3.1.0",
Expand Down
41 changes: 41 additions & 0 deletions src/__tests__/legacyFileAction.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { afterEach, describe, expect, it, vi } from 'vitest'
import { registerLegacyFileAction } from '../legacyFileAction.js'

vi.mock('@nextcloud/viewer', () => ({ getViewer: vi.fn() }))

describe('registerLegacyFileAction', () => {
afterEach(() => {
delete window._nc_fileactions
})

it('registers one hidden default action in the files 3 registry', () => {
registerLegacyFileAction()
registerLegacyFileAction()

expect(window._nc_fileactions).toHaveLength(1)
expect(window._nc_fileactions[0]).toMatchObject({ id: 'file_mindmap', default: 'hidden' })
})

it('keeps actions registered before it', () => {
window._nc_fileactions = [{ id: 'other' }]
registerLegacyFileAction()

expect(window._nc_fileactions.map((action) => action.id)).toEqual(['other', 'file_mindmap'])
})

it('is enabled for a single readable mind map only', () => {
registerLegacyFileAction()
const { enabled } = window._nc_fileactions[0]
const km = { mime: 'application/km', permissions: 1 }

expect(enabled([km])).toBe(true)
expect(enabled([{ ...km, permissions: 0 }])).toBe(false)
expect(enabled([{ ...km, mime: 'text/plain' }])).toBe(false)
expect(enabled([km, km])).toBe(false)
})
})
55 changes: 36 additions & 19 deletions src/legacyFileAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,44 @@
*/

import SvgPencil from '@mdi/svg/svg/pencil.svg?raw'
import { DefaultType, FileAction, Permission, registerFileAction } from '@nextcloud/files-legacy'
import { translate as t } from '@nextcloud/l10n'
import { getViewer } from '@nextcloud/viewer'
import { HANDLER_ID, MIMES } from './constants.js'
import logger from './logger.js'

registerFileAction(new FileAction({
id: 'file_mindmap',
displayName: () => t('files_mindmap', 'Edit'),
iconSvgInline: () => SvgPencil,
enabled: (nodes) => nodes.length === 1 && MIMES.includes(nodes[0].mime) && (nodes[0].permissions & Permission.READ) !== 0,
async exec(node) {
try {
await getViewer().open([node], node, {}, HANDLER_ID)
return true
} catch (error) {
logger.error('Could not open the mind map', { error })
const { showError } = await import('@nextcloud/dialogs')
showError(error.message)
return false
}
},
default: DefaultType.HIDDEN,
}))
const ACTION_ID = 'file_mindmap'
const PERMISSION_READ = 1

/**
* Register the hidden default action that opens mind maps from the Files app of Nextcloud 32.
*
* Files 32 looks up the default action once when it opens a file from the URL, as on a public
* file share, so this has to run synchronously in the init script. The action is a plain object
* in the `window._nc_fileactions` registry of `@nextcloud/files` 3, whose `FileAction` only
* wraps these properties, so that library does not load on every page.
*/
export function registerLegacyFileAction() {
window._nc_fileactions ??= []
if (window._nc_fileactions.some((action) => action.id === ACTION_ID)) {
return
}

window._nc_fileactions.push({
id: ACTION_ID,
displayName: () => t('files_mindmap', 'Edit'),
iconSvgInline: () => SvgPencil,
enabled: (nodes) => nodes.length === 1 && MIMES.includes(nodes[0].mime) && (nodes[0].permissions & PERMISSION_READ) !== 0,
async exec(node) {
try {
await getViewer().open([node], node, {}, HANDLER_ID)
return true
} catch (error) {
logger.error('Could not open the mind map', { error })
const { showError } = await import('@nextcloud/dialogs')
showError(error.message)
return false
}
},
default: 'hidden',
})
}
3 changes: 2 additions & 1 deletion src/mindmapviewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { translate as t } from '@nextcloud/l10n'
import { registerHandler } from '@nextcloud/viewer'
import { HANDLER_ID, MIMES, TAG_NAME } from './constants.js'
import { registerLegacyFileAction } from './legacyFileAction.js'
import MindMap from './views/MindMap.js'

const version = Number.parseInt((window.OC?.config?.version ?? '0').split('.')[0])
Expand All @@ -25,5 +26,5 @@ registerHandler({

// From 33 on the viewer registers the actions for its handlers itself
if (version < 33) {
import('./legacyFileAction.js')
registerLegacyFileAction()
}
Loading