-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from kkk669/add-html-encoder-decoder
Add HTML Encoder / Decoder
- Loading branch information
Showing
7 changed files
with
145 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
Sources/DevToysApp/ViewModels/EncodersDecoders/HTMLEncoderDecoderViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Combine | ||
import HTMLEntities | ||
|
||
final class HTMLEncoderDecoderViewModel { | ||
@Published var encodeMode = true | ||
@Published var input = "" | ||
@Published var output = "" | ||
|
||
init() { | ||
self.$input.combineLatest(self.$encodeMode) | ||
.dropFirst() | ||
.map { input, encodeMode in | ||
encodeMode | ||
? Self.encode(input) | ||
: Self.decode(input) | ||
} | ||
.assign(to: &self.$output) | ||
} | ||
|
||
private static func encode(_ input: String) -> String { | ||
input.htmlEscape(useNamedReferences: true) | ||
} | ||
|
||
private static func decode(_ input: String) -> String { | ||
input.htmlUnescape() | ||
} | ||
} | ||
|
||
extension HTMLEncoderDecoderViewModel: ObservableObject {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
Sources/DevToysApp/Views/EncodersDecoders/HTMLEncoderDecoderView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import SwiftUI | ||
|
||
struct HTMLEncoderDecoderView { | ||
@StateObject private var viewModel = HTMLEncoderDecoderViewModel() | ||
|
||
init() { | ||
Task { @MainActor in | ||
UITextView.appearance().backgroundColor = .clear | ||
} | ||
} | ||
} | ||
|
||
extension HTMLEncoderDecoderView: View { | ||
var body: some View { | ||
ToyPage { | ||
ToySection("Configuration") { | ||
ConfigurationRow(systemImage: "arrow.left.arrow.right") { | ||
Text("Conversion") | ||
Text("Select which conversion mode you want to use") | ||
.font(.caption) | ||
.foregroundStyle(.secondary) | ||
} content: { | ||
Picker("", selection: self.$viewModel.encodeMode) { | ||
Text("Encode").tag(true) | ||
Text("Decode").tag(false) | ||
} | ||
} | ||
} | ||
|
||
ToySection("Input") { | ||
PasteButton(text: self.$viewModel.input) | ||
OpenFileButton(text: self.$viewModel.input) | ||
ClearButton(text: self.$viewModel.input) | ||
} content: { | ||
TextEditor(text: self.$viewModel.input) | ||
.disableAutocorrection(true) | ||
.textInputAutocapitalization(.never) | ||
.font(.body.monospaced()) | ||
.background(.regularMaterial) | ||
.cornerRadius(8) | ||
.frame(idealHeight: 200) | ||
} | ||
|
||
ToySection("Output") { | ||
CopyButton(text: self.viewModel.output) | ||
} content: { | ||
TextEditor(text: .constant(self.viewModel.output)) | ||
.disableAutocorrection(true) | ||
.textInputAutocapitalization(.never) | ||
.font(.body.monospaced()) | ||
.background(.regularMaterial) | ||
.cornerRadius(8) | ||
.frame(idealHeight: 200) | ||
} | ||
} | ||
.navigationTitle("HTML Encoder / Decoder") | ||
} | ||
} | ||
|
||
struct HTMLEncoderDecoderView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
HTMLEncoderDecoderView() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters