about summary refs log tree commit diff
path: root/src/view/com/composer/photos/Gallery.tsx
blob: e2d95b2a4a089fbde6c9b29b350d3ab5fc0e615f (plain) (blame)
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
import React, {useCallback} from 'react'
import {ImageStyle, Keyboard} from 'react-native'
import {GalleryModel} from 'state/models/media/gallery'
import {observer} from 'mobx-react-lite'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {colors} from 'lib/styles'
import {StyleSheet, TouchableOpacity, View} from 'react-native'
import {ImageModel} from 'state/models/media/image'
import {Image} from 'expo-image'
import {Text} from 'view/com/util/text/Text'
import {isDesktopWeb} from 'platform/detection'

interface Props {
  gallery: GalleryModel
}

export const Gallery = observer(function ({gallery}: Props) {
  const getImageStyle = useCallback(() => {
    let side: number

    if (gallery.size === 1) {
      side = 250
    } else {
      side = (isDesktopWeb ? 560 : 350) / gallery.size
    }

    return {
      height: side,
      width: side,
    }
  }, [gallery])

  const imageStyle = getImageStyle()
  const handleAddImageAltText = useCallback(
    (image: ImageModel) => {
      Keyboard.dismiss()
      gallery.setAltText(image)
    },
    [gallery],
  )
  const handleRemovePhoto = useCallback(
    (image: ImageModel) => {
      gallery.remove(image)
    },
    [gallery],
  )

  const handleEditPhoto = useCallback(
    (image: ImageModel) => {
      gallery.crop(image)
    },
    [gallery],
  )

  const isOverflow = !isDesktopWeb && gallery.size > 2

  const imageControlLabelStyle = {
    borderRadius: 5,
    paddingHorizontal: 10,
    position: 'absolute' as const,
    width: 46,
    zIndex: 1,
    ...(isOverflow
      ? {
          left: 4,
          bottom: 4,
        }
      : isDesktopWeb && gallery.size < 3
      ? {
          left: 8,
          top: 8,
        }
      : {
          left: 4,
          top: 4,
        }),
  }

  const imageControlsSubgroupStyle = {
    display: 'flex' as const,
    flexDirection: 'row' as const,
    position: 'absolute' as const,
    ...(isOverflow
      ? {
          top: 4,
          right: 4,
          gap: 4,
        }
      : isDesktopWeb && gallery.size < 3
      ? {
          top: 8,
          right: 8,
          gap: 8,
        }
      : {
          top: 4,
          right: 4,
          gap: 4,
        }),
    zIndex: 1,
  }

  return !gallery.isEmpty ? (
    <View testID="selectedPhotosView" style={styles.gallery}>
      {gallery.images.map(image =>
        image.compressed !== undefined ? (
          <View key={`selected-image-${image.path}`} style={[imageStyle]}>
            <TouchableOpacity
              testID="altTextButton"
              accessibilityRole="button"
              accessibilityLabel="Add alt text"
              accessibilityHint="Opens modal for inputting image alt text"
              onPress={() => {
                handleAddImageAltText(image)
              }}
              style={[styles.imageControl, imageControlLabelStyle]}>
              <Text style={styles.imageControlTextContent}>ALT</Text>
            </TouchableOpacity>
            <View style={imageControlsSubgroupStyle}>
              <TouchableOpacity
                testID="cropPhotoButton"
                accessibilityRole="button"
                accessibilityLabel="Crop image"
                accessibilityHint="Opens modal for cropping image"
                onPress={() => {
                  handleEditPhoto(image)
                }}
                style={styles.imageControl}>
                <FontAwesomeIcon
                  icon="pen"
                  size={12}
                  style={{color: colors.white}}
                />
              </TouchableOpacity>
              <TouchableOpacity
                testID="removePhotoButton"
                accessibilityRole="button"
                accessibilityLabel="Remove image"
                accessibilityHint=""
                onPress={() => handleRemovePhoto(image)}
                style={styles.imageControl}>
                <FontAwesomeIcon
                  icon="xmark"
                  size={16}
                  style={{color: colors.white}}
                />
              </TouchableOpacity>
            </View>

            <Image
              testID="selectedPhotoImage"
              style={[styles.image, imageStyle] as ImageStyle}
              source={{
                uri: image.compressed.path,
              }}
              accessible={true}
              accessibilityIgnoresInvertColors
            />
          </View>
        ) : null,
      )}
    </View>
  ) : null
})

const styles = StyleSheet.create({
  gallery: {
    flex: 1,
    flexDirection: 'row',
    gap: 8,
    marginTop: 16,
  },
  image: {
    resizeMode: 'cover',
    borderRadius: 8,
  },
  imageControl: {
    width: 24,
    height: 24,
    borderRadius: 12,
    backgroundColor: 'rgba(0, 0, 0, 0.75)',
    borderWidth: 0.5,
    alignItems: 'center',
    justifyContent: 'center',
  },
  imageControlTextContent: {
    color: 'white',
    fontSize: 12,
    fontWeight: 'bold',
    letterSpacing: 1,
  },
})