Skip to content

Commit

Permalink
Refactored whole structure to inherit featurs from other libraries (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-divyesh-v authored Oct 25, 2024
1 parent 2a07709 commit 7be885d
Show file tree
Hide file tree
Showing 55 changed files with 3,701 additions and 577 deletions.
30 changes: 21 additions & 9 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
// swift-tools-version: 5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.
// swift-tools-version: 5.9

import PackageDescription

let package = Package(
name: "RichEditorSwiftUI",
platforms: [
//Add supported platforms here
.iOS(.v14),
.iOS(.v15),
.macOS(.v12),
.tvOS(.v15),
.watchOS(.v8),
.visionOS(.v1)
],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "RichEditorSwiftUI",
targets: ["RichEditorSwiftUI"]),
],
dependencies: [],
targets: [
// Targets are the basic building blocks of a pack.age, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "RichEditorSwiftUI"),
name: "RichEditorSwiftUI",
dependencies: [],
resources: [],
swiftSettings: [
.define("macOS", .when(platforms: [.macOS])),
.define("iOS", .when(platforms: [.iOS, .macCatalyst]))
]
),
.testTarget(
name: "RichEditorSwiftUITests",
dependencies: ["RichEditorSwiftUI"]),
dependencies: ["RichEditorSwiftUI"],
swiftSettings: [
.define("macOS", .when(platforms: [.macOS])),
.define("iOS", .when(platforms: [.iOS, .macCatalyst]))
]
)
]
)
14 changes: 8 additions & 6 deletions RichEditorDemo/RichEditorDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 56;
objectVersion = 77;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -126,14 +126,14 @@
};
};
buildConfigurationList = 2A0D57912AD66C7200BCF488 /* Build configuration list for PBXProject "RichEditorDemo" */;
compatibilityVersion = "Xcode 14.0";
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = 2A0D578D2AD66C7200BCF488;
preferredProjectObjectVersion = 77;
productRefGroup = 2A0D57972AD66C7200BCF488 /* Products */;
projectDirPath = "";
projectRoot = "";
Expand Down Expand Up @@ -312,12 +312,13 @@
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.canopas.RichEditorDemo;
PRODUCT_NAME = "$(TARGET_NAME)";
REGISTER_APP_GROUPS = NO;
SDKROOT = auto;
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
SUPPORTED_PLATFORMS = "appletvos appletvsimulator iphoneos iphonesimulator macosx";
SUPPORTS_MACCATALYST = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TARGETED_DEVICE_FAMILY = "1,2,3";
};
name = Debug;
};
Expand Down Expand Up @@ -349,12 +350,13 @@
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.canopas.RichEditorDemo;
PRODUCT_NAME = "$(TARGET_NAME)";
REGISTER_APP_GROUPS = NO;
SDKROOT = auto;
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
SUPPORTED_PLATFORMS = "appletvos appletvsimulator iphoneos iphonesimulator macosx";
SUPPORTS_MACCATALYST = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TARGETED_DEVICE_FAMILY = "1,2,3";
};
name = Release;
};
Expand Down
15 changes: 10 additions & 5 deletions RichEditorDemo/RichEditorDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ struct ContentView: View {
VStack {
EditorToolBarView(state: state)

RichEditor(state: _state)
.textPadding(12)
.cornerRadius(10)
RichTextEditor(
context: _state,
viewConfiguration: { _ in

}
)
.cornerRadius(10)
}
.padding(10)
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
ToolbarItem() {
Button(action: {

print("Exported JSON == \(state.output())")
}, label: {
Image(systemName: "checkmark")
Expand All @@ -49,7 +52,9 @@ struct ContentView: View {
}
.background(colorScheme == .dark ? .black : .gray.opacity(0.07))
.navigationTitle("Rich Editor")
#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
.navigationBarTitleDisplayMode(.inline)
#endif
}
}
}
4 changes: 2 additions & 2 deletions RichEditorDemo/RichEditorDemo/Sample_json.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@
"attributes": {}
},
{
"insert": "Canopas Team.\n\n",
"insert": "Canopas Team.",
"attributes": {
"bold": true,
"italic": true,
"underline": true
}
},
{
"insert": "Thank You! 😊\n\n",
"insert": "\n\nThank You! 😊\n\n",
"attributes": {}
}
]
Expand Down
117 changes: 117 additions & 0 deletions Sources/RichEditorSwiftUI/Actions/RichTextAction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//
// RichTextAction.swift
// RichEditorSwiftUI
//
// Created by Divyesh Vekariya on 21/10/24.
//

import SwiftUI
import Combine

/**
This enum defines rich text actions that can be executed on
a rich text editor.

This type also serves as a type namespace for other related
types and views, like ``RichTextAction/Button``.
*/
public enum RichTextAction: Identifiable, Equatable {

/// Copy the currently selected text, if any.
case copy

/// Dismiss any presented software keyboard.
case dismissKeyboard

/// Paste a single image.
// case pasteImage(RichTextInsertion<ImageRepresentable>)
//
// /// Paste multiple images.
// case pasteImages(RichTextInsertion<[ImageRepresentable]>)
//
// /// Paste plain text.
// case pasteText(RichTextInsertion<String>)

/// A print command.
case print

/// Redo the latest undone change.
case redoLatestChange

/// Select a range.
case selectRange(NSRange)

/// Set the text alignment.
case setAlignment(_ alignment: RichTextAlignment)

/// Set the entire attributed string.
case setAttributedString(NSAttributedString)

// Change background color
case setColor(RichTextColor, ColorRepresentable)

// Highlighted renge
case setHighlightedRange(NSRange?)

// Change highlighting style
case setHighlightingStyle(RichTextHighlightingStyle)

/// Set a certain ``RichTextStyle``.
case setStyle(RichTextStyle, Bool)

/// Step the font size.
case stepFontSize(points: Int)

/// Step the indent level.
case stepIndent(points: CGFloat)

/// Step the line spacing.
case stepLineSpacing(points: CGFloat)

/// Step the superscript level.
case stepSuperscript(steps: Int)

/// Toggle a certain style.
case toggleStyle(_ style: RichTextStyle)

/// Undo the latest change.
case undoLatestChange

/// Set HeaderStyle.
case setHeaderStyle(_ style: RichTextStyle, range: NSRange)
}

public extension RichTextAction {

typealias Publisher = PassthroughSubject<Self, Never>

/// The action's unique identifier.
var id: String { UUID().uuidString }

/// The action's standard icon.

}

// MARK: - Aliases

public extension RichTextAction {

/// A name alias for `.redoLatestChange`.
static var redo: RichTextAction { .redoLatestChange }

/// A name alias for `.undoLatestChange`.
static var undo: RichTextAction { .undoLatestChange }
}

public extension CGFloat {

/// The default rich text indent step size.
static var defaultRichTextIntentStepSize: CGFloat = 30.0
}

public extension UInt {

/// The default rich text indent step size.
static var defaultRichTextIntentStepSize: UInt = 30
}

90 changes: 90 additions & 0 deletions Sources/RichEditorSwiftUI/Alignment/RichTextAlignment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// RichTextAlignment.swift
// RichEditorSwiftUI
//
// Created by Divyesh Vekariya on 21/10/24.
//

import SwiftUI

/**
This enum defines supported rich text alignments, like left,
right, center, and justified.
*/
public enum RichTextAlignment: String, CaseIterable, Codable, Equatable, Identifiable {

/**
Initialize a rich text alignment with a native alignment.

- Parameters:
- alignment: The native alignment to use.
*/
public init(_ alignment: NSTextAlignment) {
switch alignment {
case .left: self = .left
case .right: self = .right
case .center: self = .center
case .justified: self = .justified
default: self = .left
}
}

/// Left text alignment.
case left

/// Center text alignment.
case center

/// Justified text alignment.
case justified

/// Right text alignment.
case right
}

public extension Collection where Element == RichTextAlignment {

static var all: [Element] { RichTextAlignment.allCases }
}

public extension RichTextAlignment {

/// The unique alignment ID.
var id: String { rawValue }

/// The standard icon to use for the alignment.
// var icon: Image { nativeAlignment.icon }

/// The standard title to use for the alignment.
// var title: String { nativeAlignment.title }

/// The standard title key to use for the alignment.
// var titleKey: RTKL10n { nativeAlignment.titleKey }

/// The native alignment of the alignment.
var nativeAlignment: NSTextAlignment {
switch self {
case .left: .left
case .right: .right
case .center: .center
case .justified: .justified
}
}
}

//extension NSTextAlignment: RichTextLabelValue {}

public extension NSTextAlignment {

// /// The standard icon to use for the alignment.
// var icon: Image {
// switch self {
// case .left: .richTextAlignmentLeft
// case .right: .richTextAlignmentRight
// case .center: .richTextAlignmentCenter
// case .justified: .richTextAlignmentJustified
// default: .richTextAlignmentLeft
// }
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private extension RichTextAttributeWriter {
let paragraphStyle = NSMutableParagraphStyle()

paragraphStyle.alignment = .left
let listItem = NSTextList(markerFormat: listType.getMarkerFormat(), options: 0)
let listItem = TextList(markerFormat: listType.getMarkerFormat(), options: 0)

if paragraphStyle.textLists.isEmpty && newValue {
paragraphStyle.textLists.append(listItem)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

public extension NSMutableAttributedString {

/**
Set a rich text style at a certain range.

Expand Down Expand Up @@ -37,7 +37,7 @@ public extension NSMutableAttributedString {
let attributeValue = newValue ? 1 : 0
if style == .underline { return setRichTextAttribute(.underlineStyle, to: attributeValue, at: range) }
if style == .strikethrough { return setRichTextAttribute(.strikethroughStyle, to: attributeValue, at: range) }
guard let font = richTextFont(at: range) else { return }
let font = richTextFont(at: range) ?? .standardRichTextFont
let styles = richTextStyles(at: range)
let shouldAdd = newValue && !styles.hasStyle(style)
let shouldRemove = !newValue && styles.hasStyle(style)
Expand Down
Loading

0 comments on commit 7be885d

Please sign in to comment.