File size: 4,004 Bytes
0070fce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import { ExtraPathType, addExtraPath, aliasExtraPath, removeExtraPath } from '@/api/db'
import { globalEvents } from '@/util'
import { Input, Modal, RadioButton, RadioGroup, message, Button } from 'ant-design-vue'
import { open } from '@tauri-apps/api/dialog'
import { checkPathExists } from '@/api'
import { h, ref } from 'vue'
import { t } from '@/i18n'
import { useGlobalStore } from '@/store/useGlobalStore'
import { isTauri } from '@/util/env'



export const addToExtraPath = async (initType: ExtraPathType, initPath?: string) => {
  const g = useGlobalStore()
  const path = ref(initPath ?? '')

  const type = ref(initType)
  const openToSelectPath = async () => {
    const ret = await open({ directory: true, defaultPath: initPath })
    if (typeof ret === 'string') {
      path.value = ret
    } else {
      return
    }
  }
  path.value = await new Promise<string>((resolve) => {
    Modal.confirm({
      title: t('inputTargetFolderPath'),
      width: '800px',
      content: () => {
        return h('div', [
          g.conf?.enable_access_control ? h('a', {
            style: {
              'word-break': 'break-all',
              'margin-bottom': '4px',
              display: 'block'
            },
            target: '_blank',
            href: 'https://github.com/zanllp/sd-webui-infinite-image-browsing/issues/518'
          }, 'Please open this link first (Access Control mode only)') : '',
          isTauri ? h(Button, { onClick: openToSelectPath, style: {  margin: '4px 0' } } , t('selectFolder') ): '',
          h(Input, {
            value: path.value,
            'onUpdate:value': (v: string) => (path.value = v)
          }),
          h('div', [
            h('span', t('type')+': '),
            h(RadioGroup, {
              value: type.value,
              'onUpdate:value': (v: ExtraPathType) => (type.value = v),
              buttonStyle: 'solid',
              style: { margin: '16px 0 32px' }
            }, [
              h(RadioButton, { value: 'walk' }, 'Walk'),
              h(RadioButton, { value: 'scanned' }, 'Normal'),
              h(RadioButton, { value: 'scanned-fixed' }, 'Fixed')
            ])
          ]),
          h('p', 'Walk: '+ t('walkModeDoc')),
          h('p', 'Normal: '+ t('normalModelDoc')),
          h('p', 'Fixed: '+ t('fixedModeDoc'))
        ])
      },
      async onOk () {
        if (!path.value) {
          message.error(t('pathIsEmpty'))
          throw new Error('pathIsEmpty')
        }
        const res = await checkPathExists([path.value])
        if (res[path.value]) {
          resolve(path.value)
        } else {
          message.error(t('pathDoesNotExist'))
        }
      }
    })
  })
  Modal.confirm({
    content: t('confirmToAddToExtraPath'),
    async onOk () {
      await addExtraPath({ types: [type.value], path: path.value })
      message.success(t('addCompleted'))
      globalEvents.emit('searchIndexExpired')
      globalEvents.emit('updateGlobalSetting')
    }
  })
}

export const onRemoveExtraPathClick = (path: string, type: ExtraPathType) => {
  Modal.confirm({
    content: t('confirmDelete'),
    closable: true,
    async onOk () {
      await removeExtraPath({ types: [type], path })
      message.success(t('removeCompleted'))
      globalEvents.emit('searchIndexExpired')
      globalEvents.emit('updateGlobalSetting')
    }
  })
}

export const onAliasExtraPathClick = (path: string) => {
  const alias = ref('')
  Modal.confirm({
    title: t('inputAlias'),
    content: () => {
      return h('div', [
        h('div', {
          style: {
            'word-break': 'break-all',
            'margin-bottom': '4px'
          }
        }, 'Path: ' + path),
        h(Input, {
          value: alias.value,
          'onUpdate:value': (v: string) => (alias.value = v)
        })]
      )
    },
    async onOk () {
      await aliasExtraPath({ alias: alias.value, path })
      message.success(t('addAliasCompleted'))
      globalEvents.emit('updateGlobalSetting')
    }
  })
}