forked from overtake/TelegramSwift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatInterfaceInputContext.swift
More file actions
244 lines (201 loc) · 9.5 KB
/
Copy pathChatInterfaceInputContext.swift
File metadata and controls
244 lines (201 loc) · 9.5 KB
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
//
// ChatInterfaceInputContext.swift
// TelegramMac
//
// Created by keepcoder on 22/11/2016.
// Copyright © 2016 Telegram. All rights reserved.
//
import Cocoa
import TelegramCoreMac
import PostboxMac
struct PossibleContextQueryTypes: OptionSet {
var rawValue: Int32
init() {
self.rawValue = 0
}
init(rawValue: Int32) {
self.rawValue = rawValue
}
static let hashtag = PossibleContextQueryTypes(rawValue: (1 << 0))
static let mention = PossibleContextQueryTypes(rawValue: (1 << 1))
static let command = PossibleContextQueryTypes(rawValue: (1 << 2))
static let contextRequest = PossibleContextQueryTypes(rawValue: (1 << 3))
static let stickers = PossibleContextQueryTypes(rawValue: (1 << 4))
static let emoji = PossibleContextQueryTypes(rawValue: (1 << 5))
static let emojiFast = PossibleContextQueryTypes(rawValue: (1 << 6))
}
private func makeScalar(_ c: Character) -> Character {
return c
//return c.utf16[c.utf16.startIndex]
}
private let spaceScalar = makeScalar(" ")
private let newlineScalar = makeScalar("\n")
private let hashScalar = makeScalar("#")
private let atScalar = makeScalar("@")
private let slashScalar = makeScalar("/")
private let emojiScalar = makeScalar(":")
private let alphanumerics = CharacterSet.alphanumerics
func textInputStateContextQueryRangeAndType(_ inputState: ChatTextInputState, includeContext: Bool = true) -> (Range<String.Index>, PossibleContextQueryTypes, Range<String.Index>?)? {
let inputText = inputState.inputText
if !inputText.isEmpty {
if inputText.hasPrefix("@") && inputText != "@" {
let startIndex = inputText.index(after: inputText.startIndex)
var index = startIndex
var contextAddressRange: Range<String.Index>?
while true {
if index == inputText.endIndex {
break
}
let c = inputText[index]
if c == " " {
if index != startIndex {
contextAddressRange = startIndex ..< index
index = inputText.index(after: index)
}
break
} else {
if !((c >= "a" && c <= "z") || (c >= "A" && c <= "Z") || (c >= "0" && c <= "9") || c == "_") {
break
}
}
if index == inputText.endIndex {
break
} else {
index = inputText.index(after: index)
}
}
if let contextAddressRange = contextAddressRange, includeContext {
return (contextAddressRange, [.contextRequest], index ..< inputText.endIndex)
}
}
if inputText.isSingleEmoji {
var inputText = inputText
if inputText.canHaveSkinToneModifier {
inputText = inputText.emojiUnmodified
}
return (inputText.startIndex ..< inputText.endIndex, [.stickers], nil)
}
let maxUtfIndex = inputText.utf16.index(inputText.utf16.startIndex, offsetBy: inputState.selectionRange.lowerBound)
guard let maxIndex = maxUtfIndex.samePosition(in: inputText) else {
return nil
}
if maxIndex == inputText.startIndex {
return nil
}
var index = inputText.index(before: maxIndex)
var possibleQueryRange: Range<String.Index>?
var possibleTypes = PossibleContextQueryTypes([.command, .mention, .emoji, .hashtag, .emojiFast])
//var possibleTypes = PossibleContextQueryTypes([.command, .mention])
func check() {
if inputText.startIndex != inputText.index(before: index) {
let prev = inputText.index(before: inputText.index(before: index))
let scalars:CharacterSet = CharacterSet.alphanumerics
if let scalar = inputText[prev].unicodeScalars.first, scalars.contains(scalar) && inputText[prev] != newlineScalar {
possibleTypes = []
}
switch possibleTypes {
case .emoji:
if index != inputText.endIndex {
if let scalar = inputText[index].unicodeScalars.first {
if !scalars.contains(scalar) {
possibleTypes = []
}
} else {
possibleTypes = []
}
} else {
possibleTypes = []
}
default:
break
}
}
}
var definedType = false
var characterSet = CharacterSet.alphanumerics
characterSet.insert(hashScalar.unicodeScalars.first!)
characterSet.insert(atScalar.unicodeScalars.first!)
characterSet.insert(slashScalar.unicodeScalars.first!)
characterSet.insert(emojiScalar.unicodeScalars.first!)
while true {
let c = inputText[index]
//if index == inputText.startIndex {
//|| (inputText[inputText.index(before: index)] == spaceScalar || inputText[inputText.index(before: index)] == newlineScalar)
if !characterSet.contains(c.unicodeScalars.first!) {
possibleTypes = []
} else if c == hashScalar {
possibleTypes = possibleTypes.intersection([.hashtag])
definedType = true
index = inputText.index(after: index)
possibleQueryRange = index ..< maxIndex
check()
break
} else if c == atScalar {
possibleTypes = possibleTypes.intersection([.mention])
definedType = true
index = inputText.index(after: index)
possibleQueryRange = index ..< maxIndex
check()
break
} else if c == slashScalar, inputText.startIndex == index {
possibleTypes = possibleTypes.intersection([.command])
definedType = true
index = inputText.index(after: index)
possibleQueryRange = index ..< maxIndex
check()
break
} else if c == emojiScalar {
possibleTypes = possibleTypes.intersection([.emoji])
definedType = true
index = inputText.index(after: index)
possibleQueryRange = index ..< maxIndex
check()
break
}
// }
if index == inputText.startIndex {
break
} else {
index = inputText.index(before: index)
possibleQueryRange = index ..< maxIndex
}
}
if inputText.trimmingCharacters(in: CharacterSet.alphanumerics).isEmpty, !inputText.isEmpty {
possibleTypes = possibleTypes.intersection([.emojiFast])
definedType = true
possibleQueryRange = index ..< maxIndex
}
if let possibleQueryRange = possibleQueryRange, definedType && !possibleTypes.isEmpty {
return (possibleQueryRange, possibleTypes, nil)
}
}
return nil
}
func inputContextQueryForChatPresentationIntefaceState(_ chatPresentationInterfaceState: ChatPresentationInterfaceState, includeContext: Bool) -> ChatPresentationInputQuery {
let inputState = chatPresentationInterfaceState.effectiveInput
if let (possibleQueryRange, possibleTypes, additionalStringRange) = textInputStateContextQueryRangeAndType(inputState, includeContext: includeContext) {
if chatPresentationInterfaceState.state == .editing && (possibleTypes != [.contextRequest] && possibleTypes != [.mention] && possibleTypes != [.emoji]) {
return .none
}
let query = String(inputState.inputText[possibleQueryRange]) //.substring(with: possibleQueryRange)
if possibleTypes == [.hashtag] {
return .hashtag(query)
} else if possibleTypes == [.mention] {
return .mention(query: query, includeRecent: inputState.inputText.startIndex == inputState.inputText.index(before: possibleQueryRange.lowerBound) && chatPresentationInterfaceState.state == .normal)
} else if possibleTypes == [.command] {
return .command(query)
} else if possibleTypes == [.contextRequest], let additionalStringRange = additionalStringRange {
let additionalString = String(inputState.inputText[additionalStringRange])
return .contextRequest(addressName: query, query: additionalString)
} else if possibleTypes == [.stickers] {
return .stickers(query)
} else if possibleTypes == [.emoji] {
return .emoji(query, firstWord: false)
} else if possibleTypes == [.emojiFast] {
return .emoji(query, firstWord: true)
}
return .none
} else {
return .none
}
}