Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat(files): add edit locally action
Signed-off-by: John Molakvoæ <[email protected]>
  • Loading branch information
skjnldsv committed May 4, 2023
commit 749481b01fc640c41edaff8aeca8fb449a58bb81
78 changes: 78 additions & 0 deletions apps/files/src/actions/editLocallyAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @copyright Copyright (c) 2023 John Molakvoæ <[email protected]>
*
* @author John Molakvoæ <[email protected]>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import { encodePath } from '@nextcloud/paths'
import { Permission, type Node } from '@nextcloud/files'
import { translate as t } from '@nextcloud/l10n'
import axios from '@nextcloud/axios'
import DevicesSvg from '@mdi/svg/svg/devices.svg?raw'

import { generateOcsUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
import { registerFileAction, FileAction } from '../services/FileAction'
import { showError } from '@nextcloud/dialogs'

const openLocalClient = function(path: string) {
const link = generateOcsUrl('apps/files/api/v1') + '/openlocaleditor?format=json'

axios.post(link, { path })
.then(function(result) {
const scheme = 'nc://'
const command = 'open'
const uid = getCurrentUser()?.uid
let url = scheme + command + '/' + uid + '@' + window.location.host + encodePath(path)
url += '?token=' + result.data.ocs.data.token

window.location.href = url
})
.catch(function() {
showError(t('files', 'Failed to redirect to client'))
})
}

if (!/Android|iPhone|iPad|iPod/i.test(navigator.userAgent)) {
registerFileAction(new FileAction({
id: 'edit-locally',
displayName: () => t('files', 'Edit locally'),
iconSvgInline: () => DevicesSvg,

// Only works on single files
enabled(nodes: Node[]) {
// Only works on single node
if (nodes.length !== 1) {
return false
}

return (nodes[0].permissions & Permission.UPDATE) !== 0
},

async exec(node: Node) {
if (!node.path) {
return false
}
openLocalClient(node.path)
return null
},

default: true,
order: 25,
}))
}
1 change: 1 addition & 0 deletions apps/files/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import './legacy/filelistSearch.js'

import './actions/deleteAction'
import './actions/downloadAction'
import './actions/editLocallyAction'
import './actions/favoriteAction'
import './actions/openFolderAction'
import './actions/sidebarAction'
Expand Down