-
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.
- Loading branch information
1 parent
9e4cbdf
commit 24576b6
Showing
12 changed files
with
286 additions
and
22 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
33 changes: 33 additions & 0 deletions
33
Sources/RichEditorSwiftUI/Components/RichTextViewComponent+Alignment.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,33 @@ | ||
// | ||
// RichTextViewComponent+Alignment.swift | ||
// RichEditorSwiftUI | ||
// | ||
// Created by Divyesh Vekariya on 25/11/24. | ||
// | ||
|
||
import Foundation | ||
|
||
#if canImport(UIKit) | ||
import UIKit | ||
#elseif canImport(AppKit) && !targetEnvironment(macCatalyst) | ||
import AppKit | ||
#endif | ||
|
||
public extension RichTextViewComponent { | ||
|
||
/// Get the text alignment. | ||
var richTextAlignment: RichTextAlignment? { | ||
guard let style = richTextParagraphStyle else { return nil } | ||
return RichTextAlignment(style.alignment) | ||
} | ||
|
||
/// Set the text alignment. | ||
func setRichTextAlignment(_ alignment: RichTextAlignment) { | ||
if richTextAlignment == alignment { return } | ||
let style = NSMutableParagraphStyle( | ||
from: richTextParagraphStyle, | ||
alignment: alignment | ||
) | ||
setRichTextParagraphStyle(style) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
Sources/RichEditorSwiftUI/Components/RichTextViewComponent+Paragraph.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,39 @@ | ||
// | ||
// RichTextViewComponent+Paragraph.swift | ||
// RichEditorSwiftUI | ||
// | ||
// Created by Divyesh Vekariya on 25/11/24. | ||
// | ||
|
||
import Foundation | ||
|
||
#if canImport(UIKit) | ||
import UIKit | ||
#endif | ||
|
||
#if canImport(AppKit) && !targetEnvironment(macCatalyst) | ||
import AppKit | ||
#endif | ||
|
||
public extension RichTextViewComponent { | ||
|
||
/// Get the paragraph style. | ||
var richTextParagraphStyle: NSMutableParagraphStyle? { | ||
richTextAttribute(.paragraphStyle) | ||
} | ||
|
||
/// Set the paragraph style. | ||
/// | ||
/// > Todo: The function currently can't handle multiple | ||
/// selected paragraphs. If many paragraphs are selected, | ||
/// it will only affect the first one. | ||
func setRichTextParagraphStyle(_ style: NSParagraphStyle) { | ||
let range = lineRange(for: selectedRange) | ||
guard range.length > 0 else { return } | ||
#if os(watchOS) | ||
setRichTextAttribute(.paragraphStyle, to: style, at: range) | ||
#else | ||
textStorageWrapper?.addAttribute(.paragraphStyle, value: style, range: range) | ||
#endif | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
Sources/RichEditorSwiftUI/Components/RichTextViewComponent+Ranges.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,58 @@ | ||
// | ||
// RichTextViewComponent+Ranges.swift | ||
// RichTextKit | ||
// | ||
// Created by Dominik Bucher | ||
// | ||
|
||
import Foundation | ||
|
||
extension RichTextViewComponent { | ||
|
||
var notFoundRange: NSRange { | ||
.init(location: NSNotFound, length: 0) | ||
} | ||
|
||
/// Get the line range at a certain text location. | ||
func lineRange(at location: Int) -> NSRange { | ||
#if os(watchOS) | ||
return notFoundRange | ||
#else | ||
guard | ||
let manager = layoutManagerWrapper, | ||
let storage = textStorageWrapper | ||
else { return NSRange(location: NSNotFound, length: 0) } | ||
let string = storage.string as NSString | ||
let locationRange = NSRange(location: location, length: 0) | ||
let lineRange = string.lineRange(for: locationRange) | ||
return manager.characterRange(forGlyphRange: lineRange, actualGlyphRange: nil) | ||
#endif | ||
} | ||
|
||
/// Get the line range for a certain text range. | ||
func lineRange(for range: NSRange) -> NSRange { | ||
#if os(watchOS) | ||
return notFoundRange | ||
#else | ||
// Use the location-based logic if range is empty | ||
if range.length == 0 { | ||
return lineRange(at: range.location) | ||
} | ||
|
||
guard let manager = layoutManagerWrapper else { | ||
return NSRange(location: NSNotFound, length: 0) | ||
} | ||
|
||
var lineRange = NSRange(location: NSNotFound, length: 0) | ||
manager.enumerateLineFragments( | ||
forGlyphRange: range | ||
) { (_, _, _, glyphRange, stop) in | ||
lineRange = glyphRange | ||
stop.pointee = true | ||
} | ||
|
||
// Convert glyph range to character range | ||
return manager.characterRange(forGlyphRange: lineRange, actualGlyphRange: nil) | ||
#endif | ||
} | ||
} |
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
Oops, something went wrong.