-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: text toolbar styles combine (#522)
## Description This PR introduces the ability to combine text style ## Type of Change - [ ] Bug fix - [x] New feature - [ ] Breaking change - [ ] Refactoring - [ ] Documentation - [ ] Chore ## Screenshots (if applicable) <img width="180" alt="Screenshot 2025-01-08 at 18 03 13" src="https://github.com/user-attachments/assets/51133318-dca2-44e0-8e56-604af3eeb133" />
- Loading branch information
1 parent
918405c
commit e487ee7
Showing
11 changed files
with
160 additions
and
72 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
91 changes: 91 additions & 0 deletions
91
lib/app/features/feed/views/components/text_editor/utils/quill_style_manager.dart
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,91 @@ | ||
// SPDX-License-Identifier: ice License 1.0 | ||
|
||
import 'package:flutter_quill/flutter_quill.dart'; | ||
|
||
class QuillStyleManager { | ||
QuillStyleManager(this.controller); | ||
final QuillController controller; | ||
|
||
void toggleHeaderStyle(Attribute<dynamic> headerAttribute) { | ||
final currentStyle = controller.getSelectionStyle(); | ||
|
||
final isSameHeaderStyle = | ||
currentStyle.attributes[headerAttribute.key]?.value == headerAttribute.value; | ||
|
||
wipeAllStyles(); | ||
|
||
if (!isSameHeaderStyle) { | ||
controller.formatSelection(headerAttribute); | ||
} | ||
} | ||
|
||
void toggleTextStyle(Attribute<dynamic> textAttribute) { | ||
final currentStyle = controller.getSelectionStyle(); | ||
|
||
final mutuallyExclusiveStyles = [Attribute.bold.key, Attribute.italic.key]; | ||
|
||
final hasHeaderOrLink = currentStyle.attributes.keys.any( | ||
(key) => ['h1', 'h2', 'h3', Attribute.link.key].contains(key), | ||
); | ||
|
||
if (hasHeaderOrLink) { | ||
wipeAllStyles(retainStyles: {Attribute.underline.key}); | ||
controller.formatSelection(textAttribute); | ||
return; | ||
} | ||
|
||
if (mutuallyExclusiveStyles.contains(textAttribute.key)) { | ||
toggleRegularStyle(); | ||
for (final key in mutuallyExclusiveStyles) { | ||
if (currentStyle.attributes.containsKey(key) && key != textAttribute.key) { | ||
final attribute = Attribute.fromKeyValue(key, null); | ||
if (attribute != null) { | ||
controller.formatSelection(Attribute.clone(attribute, null)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (currentStyle.attributes.containsKey(textAttribute.key)) { | ||
controller.formatSelection(Attribute.clone(textAttribute, null)); | ||
} else { | ||
controller.formatSelection(textAttribute); | ||
} | ||
} | ||
|
||
void toggleLinkStyle(String? link) { | ||
if (link == null || link.isEmpty) { | ||
wipeAllStyles(); | ||
controller.formatSelection(const LinkAttribute(null)); | ||
} else { | ||
wipeAllStyles(retainStyles: {Attribute.link.key}); | ||
controller.formatSelection(LinkAttribute(link)); | ||
} | ||
} | ||
|
||
void toggleRegularStyle() { | ||
controller | ||
..formatSelection(Attribute.clone(Attribute.h1, null)) | ||
..formatSelection(Attribute.clone(Attribute.h2, null)) | ||
..formatSelection(Attribute.clone(Attribute.h3, null)); | ||
} | ||
|
||
void wipeAllStyles({Set<String> retainStyles = const {}}) { | ||
final allStyles = { | ||
Attribute.bold.key: Attribute.bold, | ||
Attribute.italic.key: Attribute.italic, | ||
Attribute.underline.key: Attribute.underline, | ||
Attribute.link.key: Attribute.link, | ||
Attribute.h1.key: Attribute.h1, | ||
Attribute.h2.key: Attribute.h2, | ||
Attribute.h3.key: Attribute.h3, | ||
}; | ||
|
||
for (final entry in allStyles.entries) { | ||
if (!retainStyles.contains(entry.key)) { | ||
final attribute = entry.value as Attribute<dynamic>; | ||
controller.formatSelection(Attribute.clone(attribute, null)); | ||
} | ||
} | ||
} | ||
} |
14 changes: 0 additions & 14 deletions
14
lib/app/features/feed/views/components/text_editor/utils/wipe_styles.dart
This file was deleted.
Oops, something went wrong.
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
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
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.