Skip to content

Commit 3b8b21b

Browse files
[SuperEditor][SuperReader] - Added font family attribution, superscript and subscript, and paragraph indentation (Resolves #2107) (#2111)
1 parent 838fcf5 commit 3b8b21b

File tree

11 files changed

+698
-59
lines changed

11 files changed

+698
-59
lines changed

super_editor/example/lib/demos/example_editor/_toolbar.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,26 @@ class _EditorToolbarState extends State<EditorToolbar> {
282282
]);
283283
}
284284

285+
/// Toggles superscript styling for the current selected text.
286+
void _toggleSuperscript() {
287+
widget.editor!.execute([
288+
ToggleTextAttributionsRequest(
289+
documentRange: widget.composer.selection!,
290+
attributions: {superscriptAttribution},
291+
),
292+
]);
293+
}
294+
295+
/// Toggles subscript styling for the current selected text.
296+
void _toggleSubscript() {
297+
widget.editor!.execute([
298+
ToggleTextAttributionsRequest(
299+
documentRange: widget.composer.selection!,
300+
attributions: {subscriptAttribution},
301+
),
302+
]);
303+
}
304+
285305
/// Returns true if the current text selection includes part
286306
/// or all of a single link, returns false if zero links are
287307
/// in the selection or if 2+ links are in the selection.
@@ -571,6 +591,22 @@ class _EditorToolbarState extends State<EditorToolbar> {
571591
tooltip: AppLocalizations.of(context)!.labelStrikethrough,
572592
),
573593
),
594+
Center(
595+
child: IconButton(
596+
onPressed: _toggleSuperscript,
597+
icon: const Icon(Icons.superscript),
598+
splashRadius: 16,
599+
tooltip: AppLocalizations.of(context)!.labelSuperscript,
600+
),
601+
),
602+
Center(
603+
child: IconButton(
604+
onPressed: _toggleSubscript,
605+
icon: const Icon(Icons.subscript),
606+
splashRadius: 16,
607+
tooltip: AppLocalizations.of(context)!.labelSubscript,
608+
),
609+
),
574610
Center(
575611
child: IconButton(
576612
onPressed: _areMultipleLinksSelected() ? null : _onLinkPressed,

super_editor/example/lib/l10n/app_en.arb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"labelBold": "Bold",
44
"labelItalics": "Italics",
55
"labelStrikethrough": "Strikethrough",
6+
"labelSuperscript": "Superscript",
7+
"labelSubscript": "Subscript",
68
"labelLink": "Link",
79
"labelTextAlignment": "Text Alignment",
810
"labelMoreOptions": "More Options (not implemented)",

super_editor/example_docs/lib/editor.dart

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -105,39 +105,6 @@ class _DocsEditorState extends State<DocsEditor> {
105105
}
106106
}
107107

108-
/// Attribution to be used within [AttributedText] to
109-
/// represent an inline span of a font family change.
110-
///
111-
/// Every [FontFamilyAttribution] is considered equivalent so
112-
/// that [AttributedText] prevents multiple [FontFamilyAttribution]s
113-
/// from overlapping.
114-
class FontFamilyAttribution implements Attribution {
115-
FontFamilyAttribution(this.fontFamily);
116-
117-
@override
118-
String get id => fontFamily;
119-
120-
final String fontFamily;
121-
122-
@override
123-
bool canMergeWith(Attribution other) {
124-
return this == other;
125-
}
126-
127-
@override
128-
bool operator ==(Object other) =>
129-
identical(this, other) ||
130-
other is FontFamilyAttribution && runtimeType == other.runtimeType && fontFamily == other.fontFamily;
131-
132-
@override
133-
int get hashCode => fontFamily.hashCode;
134-
135-
@override
136-
String toString() {
137-
return '[FontFamilyAttribution]: $fontFamily';
138-
}
139-
}
140-
141108
// Selection styles when the editor has focus.
142109
const _standardEditorSelectionStyle = defaultSelectionStyle;
143110

super_editor/lib/src/default_editor/attributions.dart

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,35 @@ const underlineAttribution = NamedAttribution('underline');
3838
/// Strikethrough style attribution.
3939
const strikethroughAttribution = NamedAttribution('strikethrough');
4040

41+
/// Superscript style attribution.
42+
const superscriptAttribution = ScriptAttribution.superscript();
43+
44+
/// Subscript style attribution.
45+
const subscriptAttribution = ScriptAttribution.subscript();
46+
4147
/// Code style attribution.
4248
const codeAttribution = NamedAttribution('code');
4349

50+
/// An attribution for superscript and subscript text.
51+
class ScriptAttribution implements Attribution {
52+
static const typeSuper = "superscript";
53+
static const typeSub = "subscript";
54+
55+
const ScriptAttribution.superscript() : type = typeSuper;
56+
57+
const ScriptAttribution.subscript() : type = typeSub;
58+
59+
@override
60+
String get id => "script";
61+
62+
final String type;
63+
64+
@override
65+
bool canMergeWith(Attribution other) {
66+
return other is ScriptAttribution && type == other.type;
67+
}
68+
}
69+
4470
/// Attribution to be used within [AttributedText] to
4571
/// represent an inline span of a text color change.
4672
///
@@ -139,6 +165,39 @@ class FontSizeAttribution implements Attribution {
139165
}
140166
}
141167

168+
/// Attribution that says the text within it should use the given
169+
/// [fontFamily].
170+
///
171+
/// Every [FontFamilyAttribution] is considered equivalent so
172+
/// that [AttributedText] prevents multiple [FontFamilyAttribution]s
173+
/// from overlapping.
174+
class FontFamilyAttribution implements Attribution {
175+
const FontFamilyAttribution(this.fontFamily);
176+
177+
@override
178+
String get id => 'font_family';
179+
180+
final String fontFamily;
181+
182+
@override
183+
bool canMergeWith(Attribution other) {
184+
return this == other;
185+
}
186+
187+
@override
188+
bool operator ==(Object other) =>
189+
identical(this, other) ||
190+
other is FontFamilyAttribution && runtimeType == other.runtimeType && fontFamily == other.fontFamily;
191+
192+
@override
193+
int get hashCode => fontFamily.hashCode;
194+
195+
@override
196+
String toString() {
197+
return '[FontFamilyAttribution]: $fontFamily';
198+
}
199+
}
200+
142201
/// Attribution to be used within [AttributedText] to
143202
/// represent a link.
144203
///

super_editor/lib/src/default_editor/default_document_editor.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,22 @@ final defaultRequestHandlers = List.unmodifiable(<EditRequestHandler>[
134134
alignment: request.alignment,
135135
)
136136
: null,
137+
(request) => request is IndentParagraphRequest
138+
? IndentParagraphCommand(
139+
request.nodeId,
140+
)
141+
: null,
142+
(request) => request is UnIndentParagraphRequest
143+
? UnIndentParagraphCommand(
144+
request.nodeId,
145+
)
146+
: null,
147+
(request) => request is SetParagraphIndentRequest
148+
? SetParagraphIndentCommand(
149+
request.nodeId,
150+
level: request.level,
151+
)
152+
: null,
137153
(request) => request is ChangeParagraphBlockTypeRequest
138154
? ChangeParagraphBlockTypeCommand(
139155
nodeId: request.nodeId,

0 commit comments

Comments
 (0)