From e3d8edb7fc338ff4e060fe9b3bba8953ebde206e Mon Sep 17 00:00:00 2001 From: Jérôme Herbinet <33763786+Jerome-Herbinet@users.noreply.github.com> Date: Thu, 23 Feb 2023 13:35:31 +0100 Subject: fix: rename 'edit locally' to 'open locally' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace "Edit" by "Open" Signed-off-by: Jérôme Herbinet <33763786+Jerome-Herbinet@users.noreply.github.com> --- apps/files/src/actions/editLocallyAction.spec.ts | 170 ----------------------- apps/files/src/actions/editLocallyAction.ts | 107 -------------- apps/files/src/actions/openLocallyAction.spec.ts | 170 +++++++++++++++++++++++ apps/files/src/actions/openLocallyAction.ts | 107 ++++++++++++++ apps/files/src/init.ts | 2 +- 5 files changed, 278 insertions(+), 278 deletions(-) delete mode 100644 apps/files/src/actions/editLocallyAction.spec.ts delete mode 100644 apps/files/src/actions/editLocallyAction.ts create mode 100644 apps/files/src/actions/openLocallyAction.spec.ts create mode 100644 apps/files/src/actions/openLocallyAction.ts (limited to 'apps/files/src') diff --git a/apps/files/src/actions/editLocallyAction.spec.ts b/apps/files/src/actions/editLocallyAction.spec.ts deleted file mode 100644 index 07ccac5043d..00000000000 --- a/apps/files/src/actions/editLocallyAction.spec.ts +++ /dev/null @@ -1,170 +0,0 @@ -/** - * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors - * SPDX-License-Identifier: AGPL-3.0-or-later - */ -import { File, Permission, View, FileAction } from '@nextcloud/files' -import { beforeAll, beforeEach, describe, expect, test, vi } from 'vitest' - -import axios from '@nextcloud/axios' -import * as nextcloudDialogs from '@nextcloud/dialogs' -import { action } from './editLocallyAction' - -vi.mock('@nextcloud/auth') -vi.mock('@nextcloud/axios') - -const view = { - id: 'files', - name: 'Files', -} as View - -// Mock web root variable -beforeAll(() => { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (window as any)._oc_webroot = ''; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (window as any).OCA = { Viewer: { open: vi.fn() } } -}) - -describe('Edit locally action conditions tests', () => { - test('Default values', () => { - expect(action).toBeInstanceOf(FileAction) - expect(action.id).toBe('edit-locally') - expect(action.displayName([], view)).toBe('Edit locally') - expect(action.iconSvgInline([], view)).toMatch(//) - expect(action.default).toBeUndefined() - expect(action.order).toBe(25) - }) -}) - -describe('Edit locally action enabled tests', () => { - test('Enabled for file with UPDATE permission', () => { - const file = new File({ - id: 1, - source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', - owner: 'admin', - mime: 'text/plain', - permissions: Permission.ALL, - }) - - expect(action.enabled).toBeDefined() - expect(action.enabled!([file], view)).toBe(true) - }) - - test('Disabled for non-dav resources', () => { - const file = new File({ - id: 1, - source: 'https://domain.com/data/foobar.txt', - owner: 'admin', - mime: 'text/plain', - }) - - expect(action.enabled).toBeDefined() - expect(action.enabled!([file], view)).toBe(false) - }) - - test('Disabled if more than one node', () => { - const file1 = new File({ - id: 1, - source: 'https://cloud.domain.com/remote.php/dav/files/admin/foo.txt', - owner: 'admin', - mime: 'text/plain', - permissions: Permission.ALL, - }) - const file2 = new File({ - id: 1, - source: 'https://cloud.domain.com/remote.php/dav/files/admin/bar.txt', - owner: 'admin', - mime: 'text/plain', - permissions: Permission.ALL, - }) - - expect(action.enabled).toBeDefined() - expect(action.enabled!([file1, file2], view)).toBe(false) - }) - - test('Disabled for files', () => { - const file = new File({ - id: 1, - source: 'https://cloud.domain.com/remote.php/dav/files/admin/Foo/', - owner: 'admin', - mime: 'text/plain', - }) - - expect(action.enabled).toBeDefined() - expect(action.enabled!([file], view)).toBe(false) - }) - - test('Disabled without UPDATE permissions', () => { - const file = new File({ - id: 1, - source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', - owner: 'admin', - mime: 'text/plain', - permissions: Permission.READ, - }) - - expect(action.enabled).toBeDefined() - expect(action.enabled!([file], view)).toBe(false) - }) -}) - -describe('Edit locally action execute tests', () => { - let spyShowDialog - beforeEach(() => { - vi.resetAllMocks() - spyShowDialog = vi.spyOn(nextcloudDialogs.Dialog.prototype, 'show') - .mockImplementation(() => Promise.resolve()) - }) - - test('Edit locally opens proper URL', async () => { - vi.spyOn(axios, 'post').mockImplementation(async () => ({ - data: { ocs: { data: { token: 'foobar' } } }, - })) - const showError = vi.spyOn(nextcloudDialogs, 'showError') - const windowOpenSpy = vi.spyOn(window, 'open').mockImplementation(() => null) - - const file = new File({ - id: 1, - source: 'http://nextcloud.local/remote.php/dav/files/admin/foobar.txt', - owner: 'admin', - mime: 'text/plain', - permissions: Permission.UPDATE, - }) - - const exec = await action.exec(file, view, '/') - - expect(spyShowDialog).toBeCalled() - - // Silent action - expect(exec).toBe(null) - expect(axios.post).toBeCalledTimes(1) - expect(axios.post).toBeCalledWith('http://nextcloud.local/ocs/v2.php/apps/files/api/v1/openlocaleditor?format=json', { path: '/foobar.txt' }) - expect(showError).toBeCalledTimes(0) - expect(windowOpenSpy).toBeCalledWith('nc://open/test@nextcloud.local/foobar.txt?token=foobar', '_self') - }) - - test('Edit locally fails and shows error', async () => { - vi.spyOn(axios, 'post').mockImplementation(async () => ({})) - const showError = vi.spyOn(nextcloudDialogs, 'showError') - - const file = new File({ - id: 1, - source: 'http://nextcloud.local/remote.php/dav/files/admin/foobar.txt', - owner: 'admin', - mime: 'text/plain', - permissions: Permission.UPDATE, - }) - - const exec = await action.exec(file, view, '/') - - expect(spyShowDialog).toBeCalled() - - // Silent action - expect(exec).toBe(null) - expect(axios.post).toBeCalledTimes(1) - expect(axios.post).toBeCalledWith('http://nextcloud.local/ocs/v2.php/apps/files/api/v1/openlocaleditor?format=json', { path: '/foobar.txt' }) - expect(showError).toBeCalledTimes(1) - expect(showError).toBeCalledWith('Failed to redirect to client') - expect(window.location.href).toBe('http://nextcloud.local/') - }) -}) diff --git a/apps/files/src/actions/editLocallyAction.ts b/apps/files/src/actions/editLocallyAction.ts deleted file mode 100644 index 72770e9de70..00000000000 --- a/apps/files/src/actions/editLocallyAction.ts +++ /dev/null @@ -1,107 +0,0 @@ -/** - * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors - * SPDX-License-Identifier: AGPL-3.0-or-later - */ -import { encodePath } from '@nextcloud/paths' -import { generateOcsUrl } from '@nextcloud/router' -import { getCurrentUser } from '@nextcloud/auth' -import { FileAction, Permission, type Node } from '@nextcloud/files' -import { showError, DialogBuilder } from '@nextcloud/dialogs' -import { translate as t } from '@nextcloud/l10n' -import axios from '@nextcloud/axios' -import LaptopSvg from '@mdi/svg/svg/laptop.svg?raw' -import IconWeb from '@mdi/svg/svg/web.svg?raw' -import { isPublicShare } from '@nextcloud/sharing/public' - -const confirmLocalEditDialog = ( - localEditCallback: (openingLocally: boolean) => void = () => {}, -) => { - let callbackCalled = false - - return (new DialogBuilder()) - .setName(t('files', 'Edit file locally')) - .setText(t('files', 'The file should now open on your device. If it doesn\'t, please check that you have the desktop app installed.')) - .setButtons([ - { - label: t('files', 'Retry and close'), - type: 'secondary', - callback: () => { - callbackCalled = true - localEditCallback(true) - }, - }, - { - label: t('files', 'Edit online'), - icon: IconWeb, - type: 'primary', - callback: () => { - callbackCalled = true - localEditCallback(false) - }, - }, - ]) - .build() - .show() - .then(() => { - // Ensure the callback is called even if the dialog is dismissed in other ways - if (!callbackCalled) { - localEditCallback(false) - } - }) -} - -const attemptOpenLocalClient = async (path: string) => { - openLocalClient(path) - confirmLocalEditDialog( - (openLocally: boolean) => { - if (!openLocally) { - window.OCA.Viewer.open({ path }) - return - } - openLocalClient(path) - }, - ) -} - -const openLocalClient = async function(path: string) { - const link = generateOcsUrl('apps/files/api/v1') + '/openlocaleditor?format=json' - - try { - const result = await axios.post(link, { path }) - const uid = getCurrentUser()?.uid - let url = `nc://open/${uid}@` + window.location.host + encodePath(path) - url += '?token=' + result.data.ocs.data.token - - window.open(url, '_self') - } catch (error) { - showError(t('files', 'Failed to redirect to client')) - } -} - -export const action = new FileAction({ - id: 'edit-locally', - displayName: () => t('files', 'Edit locally'), - iconSvgInline: () => LaptopSvg, - - // Only works on single files - enabled(nodes: Node[]) { - // Only works on single node - if (nodes.length !== 1) { - return false - } - - // does not work with shares - if (isPublicShare()) { - return false - } - - return (nodes[0].permissions & Permission.UPDATE) !== 0 - }, - - async exec(node: Node) { - attemptOpenLocalClient(node.path) - return null - }, - - order: 25, -}) diff --git a/apps/files/src/actions/openLocallyAction.spec.ts b/apps/files/src/actions/openLocallyAction.spec.ts new file mode 100644 index 00000000000..860bd6233f4 --- /dev/null +++ b/apps/files/src/actions/openLocallyAction.spec.ts @@ -0,0 +1,170 @@ +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +import { File, Permission, View, FileAction } from '@nextcloud/files' +import { beforeAll, beforeEach, describe, expect, test, vi } from 'vitest' + +import axios from '@nextcloud/axios' +import * as nextcloudDialogs from '@nextcloud/dialogs' +import { action } from './openLocallyAction' + +vi.mock('@nextcloud/auth') +vi.mock('@nextcloud/axios') + +const view = { + id: 'files', + name: 'Files', +} as View + +// Mock web root variable +beforeAll(() => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (window as any)._oc_webroot = ''; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (window as any).OCA = { Viewer: { open: vi.fn() } } +}) + +describe('Open locally action conditions tests', () => { + test('Default values', () => { + expect(action).toBeInstanceOf(FileAction) + expect(action.id).toBe('edit-locally') + expect(action.displayName([], view)).toBe('Open locally') + expect(action.iconSvgInline([], view)).toMatch(//) + expect(action.default).toBeUndefined() + expect(action.order).toBe(25) + }) +}) + +describe('Open locally action enabled tests', () => { + test('Enabled for file with UPDATE permission', () => { + const file = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.ALL, + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([file], view)).toBe(true) + }) + + test('Disabled for non-dav resources', () => { + const file = new File({ + id: 1, + source: 'https://domain.com/data/foobar.txt', + owner: 'admin', + mime: 'text/plain', + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([file], view)).toBe(false) + }) + + test('Disabled if more than one node', () => { + const file1 = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foo.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.ALL, + }) + const file2 = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/bar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.ALL, + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([file1, file2], view)).toBe(false) + }) + + test('Disabled for files', () => { + const file = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/Foo/', + owner: 'admin', + mime: 'text/plain', + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([file], view)).toBe(false) + }) + + test('Disabled without UPDATE permissions', () => { + const file = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.READ, + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([file], view)).toBe(false) + }) +}) + +describe('Open locally action execute tests', () => { + let spyShowDialog + beforeEach(() => { + vi.resetAllMocks() + spyShowDialog = vi.spyOn(nextcloudDialogs.Dialog.prototype, 'show') + .mockImplementation(() => Promise.resolve()) + }) + + test('Open locally opens proper URL', async () => { + vi.spyOn(axios, 'post').mockImplementation(async () => ({ + data: { ocs: { data: { token: 'foobar' } } }, + })) + const showError = vi.spyOn(nextcloudDialogs, 'showError') + const windowOpenSpy = vi.spyOn(window, 'open').mockImplementation(() => null) + + const file = new File({ + id: 1, + source: 'http://nextcloud.local/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.UPDATE, + }) + + const exec = await action.exec(file, view, '/') + + expect(spyShowDialog).toBeCalled() + + // Silent action + expect(exec).toBe(null) + expect(axios.post).toBeCalledTimes(1) + expect(axios.post).toBeCalledWith('http://nextcloud.local/ocs/v2.php/apps/files/api/v1/openlocaleditor?format=json', { path: '/foobar.txt' }) + expect(showError).toBeCalledTimes(0) + expect(windowOpenSpy).toBeCalledWith('nc://open/test@nextcloud.local/foobar.txt?token=foobar', '_self') + }) + + test('Open locally fails and shows error', async () => { + vi.spyOn(axios, 'post').mockImplementation(async () => ({})) + const showError = vi.spyOn(nextcloudDialogs, 'showError') + + const file = new File({ + id: 1, + source: 'http://nextcloud.local/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.UPDATE, + }) + + const exec = await action.exec(file, view, '/') + + expect(spyShowDialog).toBeCalled() + + // Silent action + expect(exec).toBe(null) + expect(axios.post).toBeCalledTimes(1) + expect(axios.post).toBeCalledWith('http://nextcloud.local/ocs/v2.php/apps/files/api/v1/openlocaleditor?format=json', { path: '/foobar.txt' }) + expect(showError).toBeCalledTimes(1) + expect(showError).toBeCalledWith('Failed to redirect to client') + expect(window.location.href).toBe('http://nextcloud.local/') + }) +}) diff --git a/apps/files/src/actions/openLocallyAction.ts b/apps/files/src/actions/openLocallyAction.ts new file mode 100644 index 00000000000..a80cf0cbeed --- /dev/null +++ b/apps/files/src/actions/openLocallyAction.ts @@ -0,0 +1,107 @@ +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +import { encodePath } from '@nextcloud/paths' +import { generateOcsUrl } from '@nextcloud/router' +import { getCurrentUser } from '@nextcloud/auth' +import { FileAction, Permission, type Node } from '@nextcloud/files' +import { showError, DialogBuilder } from '@nextcloud/dialogs' +import { translate as t } from '@nextcloud/l10n' +import axios from '@nextcloud/axios' +import LaptopSvg from '@mdi/svg/svg/laptop.svg?raw' +import IconWeb from '@mdi/svg/svg/web.svg?raw' +import { isPublicShare } from '@nextcloud/sharing/public' + +const confirmLocalEditDialog = ( + localEditCallback: (openingLocally: boolean) => void = () => {}, +) => { + let callbackCalled = false + + return (new DialogBuilder()) + .setName(t('files', 'Open file locally')) + .setText(t('files', 'The file should now open on your device. If it doesn\'t, please check that you have the desktop app installed.')) + .setButtons([ + { + label: t('files', 'Retry and close'), + type: 'secondary', + callback: () => { + callbackCalled = true + localEditCallback(true) + }, + }, + { + label: t('files', 'Open online'), + icon: IconWeb, + type: 'primary', + callback: () => { + callbackCalled = true + localEditCallback(false) + }, + }, + ]) + .build() + .show() + .then(() => { + // Ensure the callback is called even if the dialog is dismissed in other ways + if (!callbackCalled) { + localEditCallback(false) + } + }) +} + +const attemptOpenLocalClient = async (path: string) => { + openLocalClient(path) + confirmLocalEditDialog( + (openLocally: boolean) => { + if (!openLocally) { + window.OCA.Viewer.open({ path }) + return + } + openLocalClient(path) + }, + ) +} + +const openLocalClient = async function(path: string) { + const link = generateOcsUrl('apps/files/api/v1') + '/openlocaleditor?format=json' + + try { + const result = await axios.post(link, { path }) + const uid = getCurrentUser()?.uid + let url = `nc://open/${uid}@` + window.location.host + encodePath(path) + url += '?token=' + result.data.ocs.data.token + + window.open(url, '_self') + } catch (error) { + showError(t('files', 'Failed to redirect to client')) + } +} + +export const action = new FileAction({ + id: 'edit-locally', + displayName: () => t('files', 'Open locally'), + iconSvgInline: () => LaptopSvg, + + // Only works on single files + enabled(nodes: Node[]) { + // Only works on single node + if (nodes.length !== 1) { + return false + } + + // does not work with shares + if (isPublicShare()) { + return false + } + + return (nodes[0].permissions & Permission.UPDATE) !== 0 + }, + + async exec(node: Node) { + attemptOpenLocalClient(node.path) + return null + }, + + order: 25, +}) diff --git a/apps/files/src/init.ts b/apps/files/src/init.ts index fd6533c6c23..492ffbb1915 100644 --- a/apps/files/src/init.ts +++ b/apps/files/src/init.ts @@ -6,7 +6,7 @@ import { addNewFileMenuEntry, registerDavProperty, registerFileAction } from '@n import { action as deleteAction } from './actions/deleteAction' import { action as downloadAction } from './actions/downloadAction' -import { action as editLocallyAction } from './actions/editLocallyAction' +import { action as editLocallyAction } from './actions/openLocallyAction.ts' import { action as favoriteAction } from './actions/favoriteAction' import { action as moveOrCopyAction } from './actions/moveOrCopyAction' import { action as openFolderAction } from './actions/openFolderAction' -- cgit v1.2.3