PostGeneratorComponents.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. import AppKit
  2. import SwiftUI
  3. import UniformTypeIdentifiers
  4. // MARK: - Page Boundary
  5. struct PostPageBoundary<Content: View>: View {
  6. @ViewBuilder let content: Content
  7. var body: some View {
  8. content
  9. .padding(20)
  10. .background(
  11. RoundedRectangle(cornerRadius: AppTheme.cornerRadius)
  12. .fill(AppTheme.panelBackground)
  13. )
  14. .overlay(
  15. RoundedRectangle(cornerRadius: AppTheme.cornerRadius)
  16. .stroke(AppTheme.cardBorder, lineWidth: 1)
  17. )
  18. }
  19. }
  20. // MARK: - Shared Form Components
  21. struct PostFormLabel: View {
  22. let text: String
  23. var isRequired: Bool = false
  24. var body: some View {
  25. HStack(spacing: 2) {
  26. Text(text)
  27. .font(.system(size: 10, weight: .medium))
  28. .foregroundStyle(AppTheme.textTertiary)
  29. if isRequired {
  30. Text("*")
  31. .font(.system(size: 10, weight: .semibold))
  32. .foregroundStyle(Color(hex: 0xF87171))
  33. }
  34. }
  35. }
  36. }
  37. struct PostFormCard<Content: View>: View {
  38. let title: String
  39. var subtitle: String?
  40. @ViewBuilder let content: Content
  41. var body: some View {
  42. VStack(alignment: .leading, spacing: 12) {
  43. VStack(alignment: .leading, spacing: 2) {
  44. Text(title)
  45. .font(.system(size: 12, weight: .semibold))
  46. .foregroundStyle(AppTheme.textPrimary)
  47. if let subtitle {
  48. Text(subtitle)
  49. .font(.system(size: 10))
  50. .foregroundStyle(AppTheme.textSecondary)
  51. }
  52. }
  53. content
  54. }
  55. .padding(14)
  56. .frame(maxWidth: .infinity, alignment: .leading)
  57. .background(
  58. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  59. .fill(AppTheme.cardBackground)
  60. .overlay(
  61. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  62. .stroke(AppTheme.cardBorder, lineWidth: 1)
  63. )
  64. )
  65. }
  66. }
  67. struct PostFormField: View {
  68. let label: String
  69. var placeholder: String = ""
  70. @Binding var text: String
  71. var prefix: String?
  72. var isRequired: Bool = false
  73. var body: some View {
  74. VStack(alignment: .leading, spacing: 6) {
  75. PostFormLabel(text: label, isRequired: isRequired)
  76. HStack(spacing: 0) {
  77. if let prefix {
  78. Text(prefix)
  79. .font(.system(size: 12, weight: .medium))
  80. .foregroundStyle(AppTheme.textSecondary)
  81. .padding(.leading, 10)
  82. }
  83. TextField(placeholder, text: $text)
  84. .textFieldStyle(.plain)
  85. .font(.system(size: 12))
  86. .foregroundStyle(AppTheme.textPrimary)
  87. .padding(.horizontal, prefix == nil ? 10 : 4)
  88. .padding(.vertical, 8)
  89. }
  90. .background(AppTheme.panelBackground)
  91. .clipShape(RoundedRectangle(cornerRadius: 8))
  92. .overlay(
  93. RoundedRectangle(cornerRadius: 8)
  94. .stroke(AppTheme.cardBorder, lineWidth: 1)
  95. )
  96. }
  97. }
  98. }
  99. struct PostFormTextEditor: View {
  100. let label: String
  101. var placeholder: String = ""
  102. @Binding var text: String
  103. var minHeight: CGFloat = 120
  104. var isRequired: Bool = false
  105. var body: some View {
  106. VStack(alignment: .leading, spacing: 6) {
  107. PostFormLabel(text: label, isRequired: isRequired)
  108. ThinCaretTextEditor(
  109. text: $text,
  110. placeholder: placeholder
  111. )
  112. .frame(height: minHeight)
  113. .background(AppTheme.panelBackground)
  114. .clipShape(RoundedRectangle(cornerRadius: 8))
  115. .overlay(
  116. RoundedRectangle(cornerRadius: 8)
  117. .stroke(AppTheme.cardBorder, lineWidth: 1)
  118. )
  119. }
  120. }
  121. }
  122. struct PostTypePicker: View {
  123. @Binding var selectedType: RedditPostType
  124. var body: some View {
  125. LazyVGrid(
  126. columns: [GridItem(.flexible()), GridItem(.flexible())],
  127. spacing: 8
  128. ) {
  129. ForEach(RedditPostType.allCases) { type in
  130. PostTypeButton(type: type, isSelected: selectedType == type) {
  131. selectedType = type
  132. }
  133. }
  134. }
  135. }
  136. }
  137. private struct PostTypeButton: View {
  138. let type: RedditPostType
  139. let isSelected: Bool
  140. let action: () -> Void
  141. var body: some View {
  142. Button(action: action) {
  143. HStack(spacing: 8) {
  144. Image(systemName: type.systemImage)
  145. .font(.system(size: 12, weight: .semibold))
  146. .foregroundStyle(isSelected ? AppTheme.accentPurpleLight : AppTheme.textSecondary)
  147. .frame(width: 16)
  148. VStack(alignment: .leading, spacing: 1) {
  149. Text(type.title)
  150. .font(.system(size: 11, weight: .semibold))
  151. .foregroundStyle(AppTheme.textPrimary)
  152. Text(type.subtitle)
  153. .font(.system(size: 9))
  154. .foregroundStyle(AppTheme.textTertiary)
  155. .lineLimit(1)
  156. }
  157. Spacer(minLength: 0)
  158. }
  159. .padding(.horizontal, 10)
  160. .padding(.vertical, 8)
  161. .background(
  162. RoundedRectangle(cornerRadius: 8)
  163. .fill(isSelected ? AppTheme.accentPurple.opacity(0.15) : AppTheme.panelBackground)
  164. )
  165. .overlay(
  166. RoundedRectangle(cornerRadius: 8)
  167. .stroke(
  168. isSelected ? AppTheme.accentPurple.opacity(0.5) : AppTheme.cardBorder,
  169. lineWidth: 1
  170. )
  171. )
  172. }
  173. .buttonStyle(AppPlainButtonStyle())
  174. .hoverOverlay(cornerRadius: 8)
  175. }
  176. }
  177. struct PostTagToggle: View {
  178. let title: String
  179. let systemImage: String
  180. let activeColor: Color
  181. @Binding var isOn: Bool
  182. var body: some View {
  183. Button {
  184. isOn.toggle()
  185. } label: {
  186. HStack(spacing: 5) {
  187. Image(systemName: systemImage)
  188. .font(.system(size: 10, weight: .semibold))
  189. Text(title)
  190. .font(.system(size: 10, weight: .semibold))
  191. }
  192. .frame(maxWidth: .infinity)
  193. .foregroundStyle(isOn ? activeColor : AppTheme.textSecondary)
  194. .padding(.horizontal, 10)
  195. .padding(.vertical, 7)
  196. .background(
  197. RoundedRectangle(cornerRadius: 6)
  198. .fill(isOn ? activeColor.opacity(0.15) : AppTheme.panelBackground)
  199. )
  200. .overlay(
  201. RoundedRectangle(cornerRadius: 6)
  202. .stroke(isOn ? activeColor.opacity(0.4) : AppTheme.cardBorder, lineWidth: 1)
  203. )
  204. }
  205. .buttonStyle(AppPlainButtonStyle())
  206. .hoverOverlay(cornerRadius: 8)
  207. }
  208. }
  209. struct TonePicker: View {
  210. @Binding var selectedTone: PostTone
  211. var body: some View {
  212. ScrollView(.horizontal, showsIndicators: false) {
  213. HStack(spacing: 6) {
  214. ForEach(PostTone.allCases) { tone in
  215. Button {
  216. selectedTone = tone
  217. } label: {
  218. Text(tone.title)
  219. .font(.system(size: 10, weight: .semibold))
  220. .foregroundStyle(selectedTone == tone ? .white : AppTheme.textSecondary)
  221. .padding(.horizontal, 10)
  222. .padding(.vertical, 6)
  223. .background(
  224. Capsule()
  225. .fill(selectedTone == tone ? AppTheme.accentPurple : AppTheme.panelBackground)
  226. )
  227. .overlay(
  228. Capsule()
  229. .stroke(
  230. selectedTone == tone ? AppTheme.accentPurple.opacity(0.5) : AppTheme.cardBorder,
  231. lineWidth: 1
  232. )
  233. )
  234. }
  235. .buttonStyle(AppPlainButtonStyle())
  236. .hoverOverlay(cornerRadius: 50)
  237. }
  238. }
  239. }
  240. }
  241. }
  242. struct PostGeneratorMessageBanner: View {
  243. let message: String
  244. let isError: Bool
  245. var body: some View {
  246. HStack(spacing: 8) {
  247. Image(systemName: isError ? "exclamationmark.circle.fill" : "checkmark.circle.fill")
  248. .font(.system(size: 12))
  249. Text(message)
  250. .font(.system(size: 11))
  251. }
  252. .foregroundStyle(isError ? Color(hex: 0xF87171) : AppTheme.accentGreen)
  253. .padding(.horizontal, 12)
  254. .padding(.vertical, 8)
  255. .frame(maxWidth: .infinity, alignment: .leading)
  256. .background(
  257. RoundedRectangle(cornerRadius: 8)
  258. .fill((isError ? Color(hex: 0xF87171) : AppTheme.accentGreen).opacity(0.1))
  259. )
  260. }
  261. }
  262. // MARK: - Markdown
  263. struct PostMarkdownText: View {
  264. let text: String
  265. var fontSize: CGFloat = 12
  266. var color: Color = AppTheme.textSecondary
  267. var lineSpacing: CGFloat = 3
  268. var body: some View {
  269. if let attributed = try? AttributedString(
  270. markdown: text,
  271. options: AttributedString.MarkdownParsingOptions(interpretedSyntax: .inlineOnlyPreservingWhitespace)
  272. ) {
  273. Text(attributed)
  274. .font(.system(size: fontSize))
  275. .foregroundStyle(color)
  276. .lineSpacing(lineSpacing)
  277. .fixedSize(horizontal: false, vertical: true)
  278. .textSelection(.enabled)
  279. } else {
  280. Text(text)
  281. .font(.system(size: fontSize))
  282. .foregroundStyle(color)
  283. .lineSpacing(lineSpacing)
  284. .fixedSize(horizontal: false, vertical: true)
  285. .textSelection(.enabled)
  286. }
  287. }
  288. }
  289. // MARK: - Reddit Preview Card
  290. struct RedditPostPreviewCard: View {
  291. let draft: PostDraft
  292. let formattedSubreddit: String
  293. var body: some View {
  294. VStack(alignment: .leading, spacing: 0) {
  295. previewHeader
  296. previewContent
  297. }
  298. .background(AppTheme.panelBackground)
  299. .clipShape(RoundedRectangle(cornerRadius: AppTheme.cornerRadius))
  300. .overlay(
  301. RoundedRectangle(cornerRadius: AppTheme.cornerRadius)
  302. .stroke(AppTheme.cardBorder, lineWidth: 1)
  303. )
  304. }
  305. private var previewHeader: some View {
  306. HStack(spacing: 8) {
  307. Circle()
  308. .fill(
  309. LinearGradient(
  310. colors: [AppTheme.accentOrange, AppTheme.accentOrange.opacity(0.7)],
  311. startPoint: .topLeading,
  312. endPoint: .bottomTrailing
  313. )
  314. )
  315. .frame(width: 28, height: 28)
  316. .overlay {
  317. Image(systemName: "person.fill")
  318. .font(.system(size: 12))
  319. .foregroundStyle(.white)
  320. }
  321. VStack(alignment: .leading, spacing: 1) {
  322. HStack(spacing: 4) {
  323. Text(formattedSubreddit)
  324. .font(.system(size: 11, weight: .semibold))
  325. .foregroundStyle(AppTheme.textPrimary)
  326. if !draft.flair.isEmpty {
  327. Text(draft.flair)
  328. .font(.system(size: 9, weight: .semibold))
  329. .foregroundStyle(AppTheme.accentBlue)
  330. .padding(.horizontal, 6)
  331. .padding(.vertical, 2)
  332. .background(AppTheme.accentBlue.opacity(0.15))
  333. .clipShape(Capsule())
  334. }
  335. }
  336. Text("u/ReddoraUser · just now")
  337. .font(.system(size: 9))
  338. .foregroundStyle(AppTheme.textTertiary)
  339. }
  340. Spacer()
  341. previewTags
  342. }
  343. .padding(.horizontal, 14)
  344. .padding(.top, 14)
  345. .padding(.bottom, 10)
  346. }
  347. @ViewBuilder
  348. private var previewTags: some View {
  349. HStack(spacing: 4) {
  350. if draft.isNSFW {
  351. previewTag("NSFW", color: Color(hex: 0xEF4444))
  352. }
  353. if draft.isSpoiler {
  354. previewTag("Spoiler", color: AppTheme.textSecondary)
  355. }
  356. if draft.isOC {
  357. previewTag("OC", color: AppTheme.accentGreen)
  358. }
  359. }
  360. }
  361. private func previewTag(_ text: String, color: Color) -> some View {
  362. Text(text)
  363. .font(.system(size: 8, weight: .bold))
  364. .foregroundStyle(color)
  365. .padding(.horizontal, 5)
  366. .padding(.vertical, 2)
  367. .background(color.opacity(0.15))
  368. .clipShape(RoundedRectangle(cornerRadius: 3))
  369. }
  370. @ViewBuilder
  371. private var previewContent: some View {
  372. VStack(alignment: .leading, spacing: 10) {
  373. Text(draft.title.isEmpty ? "Your post title will appear here" : draft.title)
  374. .font(.system(size: 14, weight: .semibold))
  375. .foregroundStyle(draft.title.isEmpty ? AppTheme.textTertiary : AppTheme.textPrimary)
  376. .fixedSize(horizontal: false, vertical: true)
  377. switch draft.postType {
  378. case .text:
  379. textPreview
  380. case .image:
  381. imagePreview
  382. case .link:
  383. linkPreview
  384. case .video:
  385. videoPreview
  386. case .poll:
  387. pollPreview
  388. }
  389. }
  390. .padding(.horizontal, 14)
  391. .padding(.bottom, 14)
  392. }
  393. @ViewBuilder
  394. private var textPreview: some View {
  395. if !draft.body.isEmpty {
  396. PostMarkdownText(text: draft.body)
  397. }
  398. }
  399. @ViewBuilder
  400. private var imagePreview: some View {
  401. if let url = draft.imageFileURL, let nsImage = NSImage(contentsOf: url) {
  402. Image(nsImage: nsImage)
  403. .resizable()
  404. .scaledToFit()
  405. .frame(maxWidth: .infinity, maxHeight: 200)
  406. .frame(height: 200)
  407. .clipped()
  408. .clipShape(RoundedRectangle(cornerRadius: 8))
  409. } else {
  410. imagePlaceholder(icon: "photo", label: "Image preview")
  411. }
  412. if !draft.body.isEmpty {
  413. PostMarkdownText(text: draft.body, fontSize: 11)
  414. }
  415. }
  416. @ViewBuilder
  417. private var linkPreview: some View {
  418. linkCard(
  419. domain: linkDomain,
  420. title: draft.linkURL.isEmpty ? "Link preview" : draft.title,
  421. url: draft.linkURL
  422. )
  423. if !draft.body.isEmpty {
  424. PostMarkdownText(text: draft.body, fontSize: 11)
  425. }
  426. }
  427. @ViewBuilder
  428. private var videoPreview: some View {
  429. if draft.videoURL.isEmpty {
  430. imagePlaceholder(icon: "play.rectangle", label: "Video preview")
  431. } else {
  432. linkCard(
  433. domain: videoDomain,
  434. title: draft.title.isEmpty ? "Video link" : draft.title,
  435. url: draft.videoURL
  436. )
  437. }
  438. if !draft.body.isEmpty {
  439. PostMarkdownText(text: draft.body, fontSize: 11)
  440. }
  441. }
  442. @ViewBuilder
  443. private var pollPreview: some View {
  444. if !draft.body.isEmpty {
  445. PostMarkdownText(text: draft.body, fontSize: 11)
  446. }
  447. VStack(spacing: 6) {
  448. ForEach(draft.pollOptions) { option in
  449. HStack {
  450. Text(option.text.isEmpty ? "Poll option" : option.text)
  451. .font(.system(size: 11))
  452. .foregroundStyle(option.text.isEmpty ? AppTheme.textTertiary : AppTheme.textPrimary)
  453. Spacer()
  454. Circle()
  455. .stroke(AppTheme.cardBorder, lineWidth: 1.5)
  456. .frame(width: 14, height: 14)
  457. }
  458. .padding(.horizontal, 10)
  459. .padding(.vertical, 8)
  460. .background(AppTheme.cardBackground)
  461. .clipShape(RoundedRectangle(cornerRadius: 6))
  462. }
  463. }
  464. Text("Poll ends in \(draft.pollDuration.label)")
  465. .font(.system(size: 9))
  466. .foregroundStyle(AppTheme.textTertiary)
  467. }
  468. private func imagePlaceholder(icon: String, label: String) -> some View {
  469. RoundedRectangle(cornerRadius: 8)
  470. .fill(AppTheme.cardBackground)
  471. .frame(height: 140)
  472. .overlay {
  473. VStack(spacing: 6) {
  474. Image(systemName: icon)
  475. .font(.system(size: 24))
  476. .foregroundStyle(AppTheme.textTertiary)
  477. Text(label)
  478. .font(.system(size: 10))
  479. .foregroundStyle(AppTheme.textTertiary)
  480. }
  481. }
  482. }
  483. private func linkCard(domain: String, title: String, url: String) -> some View {
  484. HStack(spacing: 10) {
  485. RoundedRectangle(cornerRadius: 6)
  486. .fill(AppTheme.cardBackground)
  487. .frame(width: 60, height: 60)
  488. .overlay {
  489. Image(systemName: "link")
  490. .font(.system(size: 18))
  491. .foregroundStyle(AppTheme.textTertiary)
  492. }
  493. VStack(alignment: .leading, spacing: 3) {
  494. Text(domain.isEmpty ? "example.com" : domain)
  495. .font(.system(size: 9))
  496. .foregroundStyle(AppTheme.textTertiary)
  497. Text(title)
  498. .font(.system(size: 11, weight: .medium))
  499. .foregroundStyle(AppTheme.textPrimary)
  500. .lineLimit(2)
  501. }
  502. Spacer(minLength: 0)
  503. }
  504. .padding(10)
  505. .background(AppTheme.cardBackground)
  506. .clipShape(RoundedRectangle(cornerRadius: 8))
  507. .overlay(
  508. RoundedRectangle(cornerRadius: 8)
  509. .stroke(AppTheme.cardBorder, lineWidth: 1)
  510. )
  511. }
  512. private var linkDomain: String {
  513. guard let url = URL(string: draft.linkURL), let host = url.host else { return "" }
  514. return host
  515. }
  516. private var videoDomain: String {
  517. guard let url = URL(string: draft.videoURL), let host = url.host else { return "" }
  518. return host
  519. }
  520. }