-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[full-ci] Enhancement: Implement Copy/Move Conflict Dialog (#6994)
Introduce conflict resolve dialog for move Co-authored-by: Pascal Wengerter <[email protected]>
- Loading branch information
1 parent
df0540a
commit 7759ce4
Showing
13 changed files
with
459 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Enhancement: Copy/Move conflict dialog | ||
|
||
We've added a conflict dialog for moving resources via drag&drop in the files list | ||
|
||
https://github.com/owncloud/web/pull/6994 | ||
https://github.com/owncloud/web/issues/6996 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
import { Resource } from './index' | ||
import { join } from 'path' | ||
|
||
export enum ResolveStrategy { | ||
SKIP, | ||
REPLACE, | ||
KEEP_BOTH | ||
} | ||
export interface ResolveConflict { | ||
strategy: ResolveStrategy | ||
doForAllConflicts: boolean | ||
} | ||
export interface FileConflict { | ||
resource: Resource | ||
strategy?: ResolveStrategy | ||
} | ||
|
||
export const resolveFileExists = ( | ||
createModal, | ||
hideModal, | ||
resource, | ||
conflictCount, | ||
$gettext, | ||
$gettextInterpolate, | ||
isSingleConflict | ||
) => { | ||
return new Promise<ResolveConflict>((resolve) => { | ||
let doForAllConflicts = false | ||
const modal = { | ||
variation: 'danger', | ||
title: $gettext('File already exists'), | ||
message: $gettextInterpolate( | ||
$gettext('Resource with name %{name} already exists.'), | ||
{ name: resource.name }, | ||
true | ||
), | ||
cancelText: $gettext('Skip'), | ||
confirmText: $gettext('Keep both'), | ||
buttonSecondaryText: $gettext('Replace'), | ||
checkboxLabel: isSingleConflict | ||
? '' | ||
: $gettextInterpolate( | ||
$gettext('Do this for all %{count} conflicts'), | ||
{ count: conflictCount }, | ||
true | ||
), | ||
onCheckboxValueChanged: (value) => { | ||
doForAllConflicts = value | ||
}, | ||
onCancel: () => { | ||
hideModal() | ||
resolve({ strategy: ResolveStrategy.SKIP, doForAllConflicts } as ResolveConflict) | ||
}, | ||
onConfirmSecondary: () => { | ||
hideModal() | ||
const strategy = ResolveStrategy.REPLACE | ||
resolve({ strategy, doForAllConflicts } as ResolveConflict) | ||
}, | ||
onConfirm: () => { | ||
hideModal() | ||
resolve({ strategy: ResolveStrategy.KEEP_BOTH, doForAllConflicts } as ResolveConflict) | ||
} | ||
} | ||
createModal(modal) | ||
}) | ||
} | ||
export const resolveAllConflicts = async ( | ||
resourcesToMove, | ||
targetFolder, | ||
client, | ||
createModal, | ||
hideModal, | ||
$gettext, | ||
$gettextInterpolate, | ||
resolveFileExistsMethod | ||
) => { | ||
// if we implement MERGE, we need to use 'infinity' instead of 1 | ||
const targetFolderItems = await client.files.list(targetFolder.webDavPath, 1) | ||
const targetPath = targetFolder.path | ||
const index = targetFolder.webDavPath.lastIndexOf(targetPath) | ||
const webDavPrefix = targetFolder.webDavPath.substring(0, index) | ||
|
||
// Collect all conflicting resources | ||
const allConflicts = [] | ||
for (const resource of resourcesToMove) { | ||
const potentialTargetWebDavPath = join(webDavPrefix, targetFolder.path, resource.name) | ||
const exists = targetFolderItems.some((e) => e.name === potentialTargetWebDavPath) | ||
if (exists) { | ||
allConflicts.push({ | ||
resource, | ||
strategy: null | ||
} as FileConflict) | ||
} | ||
} | ||
let count = 0 | ||
let doForAllConflicts = false | ||
let doForAllConflictsStrategy = null | ||
const resolvedConflicts = [] | ||
for (const conflict of allConflicts) { | ||
// Resolve conflicts accordingly | ||
if (doForAllConflicts) { | ||
conflict.strategy = doForAllConflictsStrategy | ||
resolvedConflicts.push(conflict) | ||
continue | ||
} | ||
|
||
// Resolve next conflict | ||
const conflictsLeft = allConflicts.length - count | ||
const result: ResolveConflict = await resolveFileExistsMethod( | ||
createModal, | ||
hideModal, | ||
conflict.resource, | ||
conflictsLeft, | ||
$gettext, | ||
$gettextInterpolate, | ||
allConflicts.length === 1 | ||
) | ||
conflict.strategy = result.strategy | ||
resolvedConflicts.push(conflict) | ||
count += 1 | ||
|
||
// User checked 'do for all x conflicts' | ||
if (!result.doForAllConflicts) continue | ||
doForAllConflicts = true | ||
doForAllConflictsStrategy = result.strategy | ||
} | ||
return resolvedConflicts | ||
} | ||
export const showResultMessage = async ( | ||
errors, | ||
movedResources, | ||
showMessage, | ||
$gettext, | ||
$gettextInterpolate, | ||
$ngettext | ||
) => { | ||
if (errors.length === 0) { | ||
const count = movedResources.length | ||
const ntitle = $ngettext( | ||
'%{count} item was moved successfully', | ||
'%{count} items were moved successfully', | ||
count | ||
) | ||
const title = $gettextInterpolate(ntitle, { count }, true) | ||
showMessage({ | ||
title, | ||
status: 'success' | ||
}) | ||
return | ||
} | ||
let title = $gettextInterpolate( | ||
$gettext('Failed to move %{count} resources'), | ||
{ count: errors.length }, | ||
true | ||
) | ||
if (errors.length === 1) { | ||
title = $gettextInterpolate( | ||
$gettext('Failed to move "%{name}"'), | ||
{ name: errors[0]?.resourceName }, | ||
true | ||
) | ||
} | ||
showMessage({ | ||
title, | ||
status: 'danger' | ||
}) | ||
} | ||
export const move = async ( | ||
resourcesToMove, | ||
targetFolder, | ||
client, | ||
createModal, | ||
hideModal, | ||
showMessage, | ||
$gettext, | ||
$gettextInterpolate, | ||
$ngettext | ||
) => { | ||
const errors = [] | ||
const resolvedConflicts = await resolveAllConflicts( | ||
resourcesToMove, | ||
targetFolder, | ||
client, | ||
createModal, | ||
hideModal, | ||
$gettext, | ||
$gettextInterpolate, | ||
resolveFileExists | ||
) | ||
const movedResources = [] | ||
|
||
for (const resource of resourcesToMove) { | ||
const hasConflict = resolvedConflicts.some((e) => e.resource.id === resource.id) | ||
let targetName = resource.name | ||
let overwriteTarget = false | ||
if (hasConflict) { | ||
const resolveStrategy = resolvedConflicts.find((e) => e.resource.id === resource.id)?.strategy | ||
if (resolveStrategy === ResolveStrategy.SKIP) { | ||
continue | ||
} | ||
if (resolveStrategy === ResolveStrategy.REPLACE) { | ||
overwriteTarget = true | ||
} | ||
if (resolveStrategy === ResolveStrategy.KEEP_BOTH) { | ||
targetName = $gettextInterpolate($gettext('%{name} copy'), { name: resource.name }, true) | ||
} | ||
} | ||
try { | ||
await client.files.move( | ||
resource.webDavPath, | ||
join(targetFolder.webDavPath, targetName), | ||
overwriteTarget | ||
) | ||
movedResources.push(resource) | ||
} catch (error) { | ||
console.error(error) | ||
error.resourceName = resource.name | ||
errors.push(error) | ||
} | ||
} | ||
showResultMessage(errors, movedResources, showMessage, $gettext, $gettextInterpolate, $ngettext) | ||
return movedResources | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.