File size: 34,874 Bytes
b173115 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 |
import { useGlobalStore, type FileTransferTabPane, type Shortcut, type TagSearchTabPane, FuzzySearchTabPane } from '@/store/useGlobalStore'
import { useImgSliStore } from '@/store/useImgSli'
import { onLongPress, useElementSize, useMouseInElement } from '@vueuse/core'
import { ref, computed, watch, onMounted, h, reactive } from 'vue'
import { genInfoCompleted, getImageGenerationInfo, openFolder, setImgPath } from '@/api'
import {
useWatchDocument,
ok,
createTypedShareStateHook,
delay,
typedEventEmitter
} from 'vue3-ts-util'
import {
createReactiveQueue,
isImageFile,
copy2clipboardI18n,
useGlobalEventListen,
makeAsyncFunctionSingle,
globalEvents
} from '@/util'
import { getTargetFolderFiles, type FileNodeInfo, deleteFiles, moveFiles, copyFiles } from '@/api/files'
import { sortFiles, sortMethodConv } from './fileSort'
import { cloneDeep, debounce, last, range, uniqBy, uniqueId } from 'lodash-es'
import * as Path from '@/util/path'
import type Progress from 'nprogress'
// @ts-ignore
import NProgress from 'multi-nprogress'
import { Button, Checkbox, Modal, message } from 'ant-design-vue'
import type { MenuInfo } from 'ant-design-vue/lib/menu/src/interface'
import { t } from '@/i18n'
import { DatabaseOutlined } from '@/icon'
import { addExtraPath, batchUpdateImageTag, removeExtraPath, toggleCustomTagToImg } from '@/api/db'
import { FileTransferData, downloadFiles, getFileTransferDataFromDragEvent, isMediaFile, toRawFileUrl } from '@/util/file'
import { getShortcutStrFromEvent } from '@/util/shortcut'
import { openCreateFlodersModal, MultiSelectTips } from '@/components/functionalCallableComp'
import { useTagStore } from '@/store/useTagStore'
import { useBatchDownloadStore } from '@/store/useBatchDownloadStore'
import { Walker } from './walker'
export const stackCache = new Map<string, Page[]>()
const global = useGlobalStore()
const batchDownload = useBatchDownloadStore()
const tagStore = useTagStore()
const sli = useImgSliStore()
const imgTransferBus = new BroadcastChannel('iib-image-transfer-bus')
export const { eventEmitter: events, useEventListen } = typedEventEmitter<{
removeFiles (_: { paths: string[]; loc: string }): void
addFiles (_: { files: FileNodeInfo[]; loc: string }): void
}>()
export interface Scroller {
$_startIndex: number
$_endIndex: number
scrollToItem (idx: number): void
}
export const { useHookShareState } = createTypedShareStateHook(
(_inst, { images }) => {
const props = ref<Props>({ tabIdx: -1, paneIdx: -1 })
const currPage = computed(() => last(stack.value))
const stack = ref<Page[]>([])
const basePath = computed(() =>
stack.value.map((v) => v.curr).slice(global.conf?.is_win ? 1 : 0)
)
const currLocation = computed(() => Path.join(...basePath.value))
const sortMethod = ref(global.defaultSortingMethod)
const walker = ref(props.value.walkModePath ? new Walker(props.value.walkModePath, sortMethod.value) : undefined)
watch([() => props.value.walkModePath, sortMethod], () => {
walker.value = props.value.walkModePath ? new Walker(props.value.walkModePath, sortMethod.value) : undefined
})
const deletedFiles = reactive(new Set<string>())
watch(currPage, () => deletedFiles.clear())
const sortedFiles = computed(() => {
if (images.value) {
return images.value
}
if (walker.value) {
return walker.value.images.filter(v => !deletedFiles.has(v.fullpath))
}
if (!currPage.value) {
return []
}
const files = currPage.value?.files ?? []
const method = sortMethod.value
const filter = (files: FileNodeInfo[]) =>
global.onlyFoldersAndImages
? files.filter((file) => file.type === 'dir' || isMediaFile(file.name))
: files
return sortFiles(filter(files), method).filter(v => !deletedFiles.has(v.fullpath))
})
const multiSelectedIdxs = ref([] as number[])
const previewIdx = ref(-1)
const canLoadNext = computed(() => walker.value ? !walker.value.isCompleted : false)
const spinning = ref(false)
const previewing = ref(false)
const getPane = () => {
return global.tabList?.[props.value.tabIdx]?.panes?.[props.value.paneIdx] as FileTransferTabPane
}
const events = typedEventEmitter<{
loadNextDir (isFullscreenPreview?: boolean): Promise<void>
refresh (): Promise<void>
selectAll (): void
}>()
events.useEventListen('selectAll', () => {
console.log(`select all 0 -> ${sortedFiles.value.length}`)
multiSelectedIdxs.value = range(0, sortedFiles.value.length)
})
return {
previewing,
spinning,
canLoadNext,
multiSelectedIdxs,
previewIdx,
basePath,
currLocation,
currPage,
stack,
sortMethod,
sortedFiles,
scroller: ref<Scroller>(),
stackViewEl: ref<HTMLDivElement>(),
props,
getPane,
walker,
deletedFiles,
...events
}
},
() => ({ images: ref<FileNodeInfo[]>() })
)
export interface Props {
tabIdx: number
paneIdx: number
path?: string
walkModePath?: string
}
export interface Page {
files: FileNodeInfo[]
curr: string
}
/**
* 全屏查看
* @param props
* @returns
*/
export function usePreview () {
const {
previewIdx,
eventEmitter,
canLoadNext,
previewing,
sortedFiles: files,
scroller,
props
} = useHookShareState().toRefs()
const { state } = useHookShareState()
let waitScrollTo = null as number | null
const onPreviewVisibleChange = (v: boolean, lv: boolean) => {
previewing.value = v
if (waitScrollTo != null && !v && lv) {
// 关闭预览时滚动过去
scroller.value?.scrollToItem(waitScrollTo)
waitScrollTo = null
}
}
const loadNextIfNeeded = () => {
if (props.value.walkModePath) {
if (!canPreview('next') && canLoadNext) {
message.info(t('loadingNextFolder'))
eventEmitter.value.emit('loadNextDir', true) // 如果在全屏查看时外面scroller可能还停留在很久之前,使用全屏查看的索引
}
}
}
useWatchDocument('keydown', (e) => {
if (previewing.value) {
let next = previewIdx.value
if (['ArrowDown', 'ArrowRight'].includes(e.key)) {
next++
while (files.value[next] && !isImageFile(files.value[next].name)) {
next++
}
} else if (['ArrowUp', 'ArrowLeft'].includes(e.key)) {
next--
while (files.value[next] && !isImageFile(files.value[next].name)) {
next--
}
}
if (isImageFile(files.value[next]?.name) ?? '') {
previewIdx.value = next
const s = scroller.value
if (s && !(next >= s.$_startIndex && next <= s.$_endIndex)) {
waitScrollTo = next // 关闭预览时滚动过去
}
}
loadNextIfNeeded()
}
})
const previewImgMove = (type: 'next' | 'prev') => {
let next = previewIdx.value
if (type === 'next') {
next++
while (files.value[next] && !isImageFile(files.value[next].name)) {
next++
}
} else if (type === 'prev') {
next--
while (files.value[next] && !isImageFile(files.value[next].name)) {
next--
}
}
if (isImageFile(files.value[next]?.name) ?? '') {
previewIdx.value = next
const s = scroller.value
if (s && !(next >= s.$_startIndex && next <= s.$_endIndex)) {
waitScrollTo = next // 关闭预览时滚动过去
}
}
loadNextIfNeeded()
}
const canPreview = (type: 'next' | 'prev') => {
let next = previewIdx.value
if (type === 'next') {
next++
while (files.value[next] && !isImageFile(files.value[next].name)) {
next++
}
} else if (type === 'prev') {
next--
while (files.value[next] && !isImageFile(files.value[next].name)) {
next--
}
}
return isImageFile(files.value[next]?.name) ?? ''
}
useEventListen('removeFiles', async () => {
if (previewing.value && !state.sortedFiles[previewIdx.value]) {
message.info(t('manualExitFullScreen'), 5)
await delay(500)
; (
document.querySelector(
'.ant-image-preview-operations-operation .anticon-close'
) as HTMLDivElement
)?.click()
previewIdx.value = -1
}
})
return {
previewIdx,
onPreviewVisibleChange,
previewing,
previewImgMove,
canPreview
}
}
/**
* 路径栏相关
*/
export function useLocation () {
const np = ref<Progress.NProgress>()
const {
scroller,
stackViewEl,
stack,
currPage,
currLocation,
useEventListen,
eventEmitter,
getPane,
props,
deletedFiles,
walker,
sortedFiles
} = useHookShareState().toRefs()
watch(
() => stack.value.length,
debounce((v, lv) => {
if (v !== lv) {
scroller.value?.scrollToItem(0)
}
}, 300)
)
const handleWalkModeTo = async (path: string) => {
await to(path)
if (props.value.walkModePath) {
await delay()
await walker.value?.reset()
eventEmitter.value.emit('loadNextDir')
}
}
onMounted(async () => {
if (!stack.value.length) {
// 有传入stack时直接使用传入的
const resp = await getTargetFolderFiles('/')
stack.value.push({
files: resp.files,
curr: '/'
})
}
np.value = new NProgress()
np.value!.configure({ parent: stackViewEl.value as any })
if (props.value.path && props.value.path !== '/') {
await handleWalkModeTo(props.value.walkModePath ?? props.value.path)
} else {
global.conf?.home && to(global.conf.home)
}
})
watch(
currLocation,
debounce((loc) => {
const pane = getPane.value()
if (!pane) {
return
}
pane.path = loc
const filename = pane.path!.split('/').pop()
const getTitle = () => {
if (!props.value.walkModePath) {
const np = Path.normalize(loc)
for (const [k, v] of Object.entries(global.pathAliasMap)) {
if (np.startsWith(v)) {
return np.replace(v, k)
}
}
return filename
}
return (
'Walk: ' +
(global.quickMovePaths.find((v) => v.dir === pane.walkModePath)?.zh ?? filename)
)
}
const title = getTitle()
pane.name = h('div', { style: 'display:flex;align-items:center' }, [
h(DatabaseOutlined),
h('span', { class: 'line-clamp-1', style: 'max-width: 256px' }, title)
])
pane.nameFallbackStr = title
global.recent = global.recent.filter((v) => v.key !== pane.key)
global.recent.unshift({ path: loc, key: pane.key })
if (global.recent.length > 20) {
global.recent = global.recent.slice(0, 20)
}
}, 300)
)
const copyLocation = () => copy2clipboardI18n(currLocation.value)
const openNext = async (file: FileNodeInfo) => {
if (file.type !== 'dir') {
return
}
try {
np.value?.start()
const { files } = await getTargetFolderFiles(file.fullpath)
stack.value.push({
files,
curr: file.name
})
} finally {
np.value?.done()
}
}
const back = (idx: number) => {
while (idx < stack.value.length - 1) {
stack.value.pop()
}
}
const isDirNameEqual = (a: string, b: string) => {
ok(global.conf, 'global.conf load failed')
if (global.conf.is_win) {
// window下忽略
return a.toLowerCase() == b.toLowerCase()
}
return a == b
}
const to = async (dir: string) => {
const backup = stack.value.slice()
try {
if (!Path.isAbsolute(dir)) {
// 相对路径
dir = Path.join(global.conf?.sd_cwd ?? '/', dir)
}
const frags = Path.splitPath(dir)
const currPaths = stack.value.map((v) => v.curr)
currPaths.shift() // 是 /
while (currPaths[0] && frags[0]) {
if (!isDirNameEqual(currPaths[0], frags[0])) {
break
} else {
currPaths.shift()
frags.shift()
}
}
for (let index = 0; index < currPaths.length; index++) {
stack.value.pop()
}
if (!frags.length) {
return refresh()
}
for (const frag of frags) {
const target = currPage.value?.files.find((v) => isDirNameEqual(v.name, frag))
if (!target) {
console.error({ frags, frag, stack: cloneDeep(stack.value) })
throw new Error(`${frag} not found`)
}
await openNext(target)
}
} catch (error) {
message.error(t('moveFailedCheckPath') + (error instanceof Error ? error.message : ''))
console.error(dir, Path.splitPath(dir), currPage.value)
stack.value = backup
throw error
}
}
const refresh = makeAsyncFunctionSingle(async () => {
try {
np.value?.start()
if (walker.value) {
await walker.value.reset()
eventEmitter.value.emit('loadNextDir')
} else {
const { files } = await getTargetFolderFiles(
stack.value.length === 1 ? '/' : currLocation.value
)
last(stack.value)!.files = files
}
deletedFiles.value.clear()
scroller.value?.scrollToItem(0)
message.success(t('refreshCompleted'))
} finally {
np.value?.done()
}
})
useGlobalEventListen(
'returnToIIB',
makeAsyncFunctionSingle(async () => {
if (!props.value.walkModePath) {
try {
np.value?.start()
const { files } = await getTargetFolderFiles(
stack.value.length === 1 ? '/' : currLocation.value
)
const currFiles = last(stack.value)!.files
if (currFiles.map((v) => v.date).join() !== files.map((v) => v.date).join()) {
last(stack.value)!.files = files
message.success(t('autoUpdate'))
}
} finally {
np.value?.done()
}
}
})
)
useEventListen.value('refresh', refresh)
const quickMoveTo = (path: string) => {
if (props.value.walkModePath) {
getPane.value().walkModePath = path
}
handleWalkModeTo(path)
}
const normalizedScandPath = computed(() => {
return global.quickMovePaths.map((v) => ({ ...v, path: Path.normalize(v.dir) }))
})
const searchPathInfo = computed(() => {
const c = Path.normalize(currLocation.value)
const path = normalizedScandPath.value.find((v) => v.path === c)
return path
})
const addToSearchScanPathAndQuickMove = async () => {
const path = searchPathInfo.value
if (path) {
if (!path.can_delete) {
return
}
await removeExtraPath({ path: currLocation.value, type: 'scanned' })
message.success(t('removeCompleted'))
} else {
await addExtraPath({ path: currLocation.value, type: 'scanned' })
message.success(t('addCompleted'))
}
globalEvents.emit('searchIndexExpired')
globalEvents.emit('updateGlobalSetting')
}
const isLocationEditing = ref(false)
const locInputValue = ref(currLocation.value)
const onEditBtnClick = () => {
isLocationEditing.value = true
locInputValue.value = currLocation.value
}
const onLocEditEnter = async () => {
await to(locInputValue.value)
isLocationEditing.value = false
}
useWatchDocument('click', (e) => {
if (!(e.target as HTMLElement)?.className?.includes?.('ant-input')) {
isLocationEditing.value = false
}
})
const share = () => {
const loc = parent.location
const baseUrl = loc.href.substring(0, loc.href.length - loc.search.length)
const params = new URLSearchParams(loc.search)
params.set('action', 'open')
if (walker.value) {
params.set('walk', '1')
}
params.set('path', currLocation.value)
const url = `${baseUrl}?${params.toString()}`
copy2clipboardI18n(url, t('copyLocationUrlSuccessMsg'))
}
const searchInCurrentDir = (type: (TagSearchTabPane|FuzzySearchTabPane)['type'] = 'tag-search') => {
const tab = global.tabList[props.value.tabIdx]
const pane = {
type,
key: uniqueId(),
searchScope: currLocation.value,
name: t(type === 'tag-search' ? 'imgSearch' : 'fuzzy-search'),
}
tab.panes.push(pane)
tab.key = pane.key
}
const selectAll = () => eventEmitter.value.emit('selectAll')
const onCreateFloderBtnClick = async () => {
await openCreateFlodersModal(currLocation.value)
await refresh()
}
const onWalkBtnClick = () => {
const path = currLocation.value
stackCache.set(path, stack.value)
const tab = global.tabList[props.value.tabIdx]
const pane: FileTransferTabPane = {
type: 'local',
key: uniqueId(),
path: path,
name: t('local'),
stackKey: path,
walkModePath: path
}
tab.panes.push(pane)
tab.key = pane.key
}
const showWalkButton = computed(() => !walker.value && sortedFiles.value.some(v => v.type === 'dir'))
return {
locInputValue,
isLocationEditing,
onLocEditEnter,
onEditBtnClick,
addToSearchScanPathAndQuickMove,
searchPathInfo,
refresh,
copyLocation,
back,
openNext,
currPage,
currLocation,
to,
stack,
scroller,
share,
selectAll,
quickMoveTo,
onCreateFloderBtnClick,
onWalkBtnClick,
showWalkButton,
searchInCurrentDir
}
}
export function useFilesDisplay ({ fetchNext }: {fetchNext?: () => Promise<any>} = { }) {
const {
scroller,
sortedFiles,
sortMethod,
currLocation,
stackViewEl,
canLoadNext,
previewIdx,
props,
walker
} = useHookShareState().toRefs()
const { state } = useHookShareState()
const moreActionsDropdownShow = ref(false)
const cellWidth = ref(global.defaultGridCellWidth)
const gridSize = computed(() => cellWidth.value + 16) // margin 8
const profileHeight = 44
const { width } = useElementSize(stackViewEl)
const gridItems = computed(() => ~~(width.value / gridSize.value))
const itemSize = computed(() => {
const second = gridSize.value
const first = second + (cellWidth.value <= 160 ? 0 : profileHeight)
return {
first,
second
}
})
const loadNextDirLoading = ref(false)
const loadNextDir = async () => {
if (loadNextDirLoading.value || !props.value.walkModePath || !canLoadNext.value) {
return
}
try {
loadNextDirLoading.value = true
await walker.value?.next()
} finally {
loadNextDirLoading.value = false
}
}
// 填充够一页,直到不行为止
const fetchDataUntilViewFilled = async (isFullScreenPreview = false) => {
const s = scroller.value
const currIdx = () => (isFullScreenPreview ? previewIdx.value : s?.$_endIndex ?? 0)
const needLoad = () => {
const len = sortedFiles.value.length
const preload = 50
if (!len) {
return true
}
if (fetchNext) {
return currIdx() > len - preload
}
return currIdx() > len - preload && canLoadNext.value // canLoadNext 是walker的,表示加载完成
}
while (needLoad()) {
await delay(30)
const ret = await (fetchNext ?? loadNextDir)()
if (typeof ret === 'boolean' && !ret) {
return // 返回false同样表示加载完成
}
}
}
state.useEventListen('loadNextDir', fetchDataUntilViewFilled)
const updateViewingImagesTag = () => {
const s = scroller.value
if (s) {
const paths = sortedFiles.value.slice(Math.max(s.$_startIndex - 10, 0), s.$_endIndex + 10)
.filter(v => v.is_under_scanned_path && isImageFile(v.name))
.map(v => v.fullpath)
tagStore.fetchImageTags(paths)
}
}
watch(currLocation, debounce(updateViewingImagesTag, 150))
const onScroll = debounce(async () => {
await fetchDataUntilViewFilled()
updateViewingImagesTag()
}, 150)
return {
gridItems,
sortedFiles,
sortMethodConv,
moreActionsDropdownShow,
gridSize,
sortMethod,
onScroll,
loadNextDir,
loadNextDirLoading,
canLoadNext,
itemSize,
cellWidth
}
}
export function useFileTransfer () {
const { currLocation, sortedFiles, currPage, multiSelectedIdxs, eventEmitter, walker } =
useHookShareState().toRefs()
const recover = () => {
multiSelectedIdxs.value = []
}
useWatchDocument('click', recover)
useWatchDocument('blur', recover)
watch(currPage, recover)
const onFileDragStart = (e: DragEvent, idx: number) => {
const file = cloneDeep(sortedFiles.value[idx])
sli.fileDragging = true
console.log('onFileDragStart set drag file ', e, idx, file)
const files = [file]
let includeDir = file.type === 'dir'
if (multiSelectedIdxs.value.includes(idx)) {
const selectedFiles = multiSelectedIdxs.value.map((idx) => sortedFiles.value[idx])
files.push(...selectedFiles)
includeDir = selectedFiles.some((v) => v.type === 'dir')
}
const data: FileTransferData = {
includeDir,
loc: currLocation.value || 'search-result',
path: uniqBy(files, 'fullpath').map((f) => f.fullpath),
nodes: uniqBy(files, 'fullpath'),
__id: 'FileTransferData'
}
e.dataTransfer!.setData('text/plain', JSON.stringify(data))
}
const onFileDragEnd = () => {
sli.fileDragging = false
}
const onDrop = async (e: DragEvent) => {
if (walker.value) {
return
}
const data = getFileTransferDataFromDragEvent(e)
if (!data) {
return
}
const toPath = currLocation.value
if (data.loc === toPath) {
return
}
const q = createReactiveQueue()
const onCopyBtnClick = async () => q.pushAction(async () => {
await copyFiles(data.path, toPath)
eventEmitter.value.emit('refresh')
Modal.destroyAll()
})
const onMoveBtnClick = () => q.pushAction(async () => {
await moveFiles(data.path, toPath)
events.emit('removeFiles', { paths: data.path, loc: data.loc })
eventEmitter.value.emit('refresh')
Modal.destroyAll()
})
Modal.confirm({
title: t('confirm') + '?',
width: '60vw',
content: () => <div>
<div>
{`${t('moveSelectedFilesTo')} ${toPath}`}
<ol style={{ maxHeight: '50vh', overflow: 'auto' }}>
{data.path.map((v) => <li>{v.split(/[/\\]/).pop()}</li>)}
</ol>
</div>
<MultiSelectTips />
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'flex-end' }} class="actions">
<Button onClick={Modal.destroyAll}>{t('cancel')}</Button>
<Button type="primary" loading={!q.isIdle} onClick={onCopyBtnClick}>{t('copy')}</Button>
<Button type="primary" loading={!q.isIdle} onClick={onMoveBtnClick}>{t('move')}</Button>
</div>
</div>,
maskClosable: true,
wrapClassName: 'hidden-antd-btns-modal'
})
}
return {
onFileDragStart,
onDrop,
multiSelectedIdxs,
onFileDragEnd
}
}
export function useFileItemActions (
{ openNext }: { openNext: (file: FileNodeInfo) => Promise<void> }
) {
const showGenInfo = ref(false)
const imageGenInfo = ref('')
const {
sortedFiles,
previewIdx,
multiSelectedIdxs,
stack,
currLocation,
spinning,
previewing,
stackViewEl,
eventEmitter,
props,
deletedFiles
} = useHookShareState().toRefs()
const nor = Path.normalize
useEventListen('removeFiles', ({ paths, loc }) => {
if (nor(loc) !== nor(currLocation.value)) {
return
}
const top = last(stack.value)
if (!top) {
return
}
paths.forEach(path => deletedFiles.value.add(path))
paths.filter(isImageFile).forEach(path => deletedFiles.value.add(path.replace(/\.\w+$/, '.txt')))
})
useEventListen('addFiles', ({ files, loc }) => {
if (nor(loc) !== nor(currLocation.value)) {
return
}
const top = last(stack.value)
if (!top) {
return
}
top.files.unshift(...files)
})
const q = createReactiveQueue()
const onFileItemClick = async (e: MouseEvent, file: FileNodeInfo, idx: number) => {
previewIdx.value = idx
global.fullscreenPreviewInitialUrl = toRawFileUrl(file)
const idxInSelected = multiSelectedIdxs.value.indexOf(idx)
if (e.shiftKey) {
if (idxInSelected !== -1) {
multiSelectedIdxs.value.splice(idxInSelected, 1)
} else {
multiSelectedIdxs.value.push(idx)
multiSelectedIdxs.value.sort((a, b) => a - b)
const first = multiSelectedIdxs.value[0]
const last = multiSelectedIdxs.value[multiSelectedIdxs.value.length - 1]
multiSelectedIdxs.value = range(first, last + 1)
}
e.stopPropagation()
} else if (e.ctrlKey || e.metaKey) {
if (idxInSelected !== -1) {
multiSelectedIdxs.value.splice(idxInSelected, 1)
} else {
multiSelectedIdxs.value.push(idx)
}
e.stopPropagation()
} else {
await openNext(file)
}
}
const onContextMenuClick = async (e: MenuInfo, file: FileNodeInfo, idx: number) => {
const url = toRawFileUrl(file)
const path = currLocation.value
const preset = { IIB_container_id: parent.IIB_container_id }
/**
* 获取选中的图片信息
* 选中的图片信息数组
*/
const getSelectedImg = () => {
let selectedFiles: FileNodeInfo[] = []
if (multiSelectedIdxs.value.includes(idx)) {
// 如果索引已被选中,则获取所有已选中的图片信息
selectedFiles = multiSelectedIdxs.value.map((idx) => sortedFiles.value[idx])
} else {
// 否则,只获取当前图片信息
selectedFiles.push(file)
}
return selectedFiles
}
const copyImgTo = async (tab: ['txt2img', 'img2img', 'inpaint', 'extras'][number]) => {
if (spinning.value) {
return
}
try {
spinning.value = true
await setImgPath(file.fullpath) // 设置图像路径
imgTransferBus.postMessage({ ...preset, event: 'click_hidden_button', btnEleId: 'iib_hidden_img_update_trigger' }) // 触发图像组件更新
// ok(await genInfoCompleted(), 'genInfoCompleted timeout') // 等待消息生成完成
await genInfoCompleted() // 等待消息生成完成
imgTransferBus.postMessage({ ...preset, event: 'click_hidden_button', btnEleId: `iib_hidden_tab_${tab}` }) // 触发粘贴
} catch (error) {
console.error(error)
message.error('发送图像失败,请携带console的错误消息找开发者')
} finally {
spinning.value = false
}
}
const key = `${e.key}`
if (key.startsWith('toggle-tag-')) {
const tagId = +key.split('toggle-tag-')[1]
const { is_remove } = await toggleCustomTagToImg({ tag_id: tagId, img_path: file.fullpath })
const tag = global.conf?.all_custom_tags.find((v) => v.id === tagId)?.name!
await tagStore.refreshTags([file.fullpath])
message.success(t(is_remove ? 'removedTagFromImage' : 'addedTagToImage', { tag }))
return
} else if (key.startsWith('batch-add-tag-') || key.startsWith('batch-remove-tag-')) {
const tagId = +key.split('-tag-')[1]
const action = key.includes('add') ? 'add' : 'remove'
const paths = getSelectedImg().map(v => v.fullpath)
await batchUpdateImageTag({
tag_id: tagId,
img_paths: paths,
action
})
await tagStore.refreshTags(paths)
message.success(t(action === 'add' ? 'addCompleted' : 'removeCompleted'))
return
}
switch (e.key) {
case 'previewInNewWindow':
return window.open(url)
case 'download':{
const selectedFiles = getSelectedImg()
downloadFiles(selectedFiles.map(file => toRawFileUrl(file, true)))
break
}
case 'copyPreviewUrl': {
return copy2clipboardI18n(parent.document.location.origin + url)
}
case 'send2txt2img':
return copyImgTo('txt2img')
case 'send2img2img':
return copyImgTo('img2img')
case 'send2inpaint':
return copyImgTo('inpaint')
case 'send2extras':
return copyImgTo('extras')
case 'send2savedDir': {
const dir = global.quickMovePaths.find((v) => v.key === 'outdir_save')
if (!dir) {
return message.error(t('unknownSavedDir'))
}
const absolutePath = Path.normalizeRelativePathToAbsolute(dir.dir, global.conf?.sd_cwd!)
const selectedImg = getSelectedImg()
await moveFiles(
selectedImg.map((v) => v.fullpath),
absolutePath,
true
)
events.emit('removeFiles', {
paths: selectedImg.map((v) => v.fullpath),
loc: currLocation.value
})
events.emit('addFiles', { files: selectedImg, loc: absolutePath })
break
}
case 'send2controlnet-img2img':
case 'send2controlnet-txt2img': {
const type = e.key.split('-')[1] as 'img2img' | 'txt2img'
imgTransferBus.postMessage({ ...preset, event: 'send_to_control_net', type, url: toRawFileUrl(file) })
break
}
case 'send2outpaint': {
imageGenInfo.value = await q.pushAction(() => getImageGenerationInfo(file.fullpath)).res
const [prompt, negPrompt] = (imageGenInfo.value || '').split('\n')
imgTransferBus.postMessage({
...preset,
event: 'send_to_outpaint',
url: toRawFileUrl(file),
prompt,
negPrompt: negPrompt.slice('Negative prompt: '.length)
})
break
}
case 'openWithWalkMode': {
stackCache.set(path, stack.value)
const tab = global.tabList[props.value.tabIdx]
const pane: FileTransferTabPane = {
type: 'local',
key: uniqueId(),
path: file.fullpath,
name: t('local'),
stackKey: path,
walkModePath: file.fullpath
}
tab.panes.push(pane)
tab.key = pane.key
break
}
case 'openInNewTab': {
stackCache.set(path, stack.value)
const tab = global.tabList[props.value.tabIdx]
const pane: FileTransferTabPane = {
type: 'local',
key: uniqueId(),
path: file.fullpath,
name: t('local'),
stackKey: path
}
tab.panes.push(pane)
tab.key = pane.key
break
}
case 'openOnTheRight': {
stackCache.set(path, stack.value)
let tab = global.tabList[props.value.tabIdx + 1]
if (!tab) {
tab = { panes: [], key: '', id: uniqueId() }
global.tabList[props.value.tabIdx + 1] = tab
}
const pane: FileTransferTabPane = {
type: 'local',
key: uniqueId(),
path: file.fullpath,
name: t('local'),
stackKey: path
}
tab.panes.push(pane)
tab.key = pane.key
break
}
case 'send2BatchDownload': {
batchDownload.addFiles(getSelectedImg())
break
}
case 'viewGenInfo': {
showGenInfo.value = true
imageGenInfo.value = await q.pushAction(() => getImageGenerationInfo(file.fullpath)).res
break
}
case 'openWithLocalFileBrowser': {
await openFolder(file.fullpath)
break
}
case 'deleteFiles': {
const selectedFiles = getSelectedImg()
const removeFile = async () => {
const paths = selectedFiles.map((v) => v.fullpath)
await deleteFiles(paths)
message.success(t('deleteSuccess'))
events.emit('removeFiles', { paths: paths, loc: currLocation.value })
}
if (selectedFiles.length === 1 && global.ignoredConfirmActions.deleteOneOnly) {
return removeFile()
}
await new Promise<void>((resolve) => {
Modal.confirm({
title: t('confirmDelete'),
maskClosable: true,
width: '60vw',
content:() =>
<div>
<ol style={{ maxHeight: '50vh', overflow: 'auto' }}>
{selectedFiles.map((v) => <li>{v.fullpath.split(/[/\\]/).pop()}</li>)}
</ol>
<MultiSelectTips />
<Checkbox v-model:checked={global.ignoredConfirmActions.deleteOneOnly}>{t('deleteOneOnlySkipConfirm')} ({t('resetOnGlobalSettingsPage')})</Checkbox>
</div>,
async onOk () {
await removeFile()
resolve()
}
})
})
break
}
}
return {}
}
const { isOutside } = useMouseInElement(stackViewEl)
useWatchDocument('keydown', (e) => {
const keysStr = getShortcutStrFromEvent(e)
if (previewing.value) {
const action = Object.entries(global.shortcut).find(
(v) => v[1] === keysStr && v[1]
)?.[0] as keyof Shortcut
if (action) {
e.stopPropagation()
e.preventDefault()
const idx = previewIdx.value
const file = sortedFiles.value[idx]
switch (action) {
case 'delete': {
if (toRawFileUrl(file) === global.fullscreenPreviewInitialUrl) {
return message.warn(t('fullscreenRestriction'))
}
return onContextMenuClick({ key: 'deleteFiles' } as MenuInfo, file, idx)
}
case 'download': {
return onContextMenuClick({ key: 'download' } as MenuInfo, file, idx)
}
default: {
const name = /^toggle_tag_(.*)$/.exec(action)?.[1]
const tag = global.conf?.all_custom_tags.find((v) => v.name === name)
if (!tag) return
return onContextMenuClick({ key: `toggle-tag-${tag.id}` } as MenuInfo, file, idx)
}
}
}
} else if (!isOutside.value && ['Ctrl + KeyA', 'Cmd + KeyA'].includes(keysStr)) {
e.preventDefault()
e.stopPropagation()
eventEmitter.value.emit('selectAll')
}
})
return {
onFileItemClick,
onContextMenuClick,
showGenInfo,
imageGenInfo,
q
}
}
/**
* 针对移动端端操作优化,使用长按提到右键
*/
export const useMobileOptimization = () => {
const { stackViewEl } = useHookShareState().toRefs()
const showMenuIdx = ref(-1)
onLongPress(stackViewEl, (e) => {
let fileEl = e.target as HTMLDivElement
while (fileEl.parentElement) {
fileEl = fileEl.parentElement as any
if (fileEl.tagName.toLowerCase() === 'li' && fileEl.classList.contains('file-item-trigger')) {
const idx = fileEl.dataset?.idx
if (idx && Number.isSafeInteger(+idx)) {
showMenuIdx.value = +idx
}
return
}
}
})
return {
showMenuIdx
}
}
|