Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add superscript and subscript #291

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/fleather/lib/src/widgets/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ List<String> _toggleableStyleKeys = [
ParchmentAttribute.inlineCode.key,
ParchmentAttribute.backgroundColor.key,
ParchmentAttribute.foregroundColor.key,
ParchmentAttribute.script.key,
];

class FleatherController extends ChangeNotifier {
Expand Down
22 changes: 22 additions & 0 deletions packages/fleather/lib/src/widgets/editor_toolbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ class FleatherToolbar extends StatefulWidget implements PreferredSizeWidget {
bool hideBackgroundColor = false,
bool hideForegroundColor = false,
bool hideInlineCode = false,
bool hideScript = false,
bool hideHeadingStyle = false,
bool hideIndentation = false,
bool hideListNumbers = false,
Expand Down Expand Up @@ -970,6 +971,27 @@ class FleatherToolbar extends StatefulWidget implements PreferredSizeWidget {

/// ################################################################

Visibility(
visible: !hideScript,
child: ToggleStyleButton(
attribute: ParchmentAttribute.superscript,
icon: Icons.superscript,
controller: controller,
)),
Visibility(
visible: !hideScript,
child: ToggleStyleButton(
attribute: ParchmentAttribute.subscript,
icon: Icons.subscript,
controller: controller,
)),
Visibility(
visible: !hideScript,
child: VerticalDivider(
indent: 16, endIndent: 16, color: Colors.grey.shade400)),

/// ################################################################

Visibility(
visible: !hideDirection,
child: ToggleStyleButton(
Expand Down
6 changes: 6 additions & 0 deletions packages/fleather/lib/src/widgets/text_line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@
result = result.copyWith(color: Color(foregroundColor.value!));
}
}
if (nodeStyle.containsSame(ParchmentAttribute.superscript)) {
result = _mergeTextStyleWithDecoration(result, theme.superscript);

Check warning on line 317 in packages/fleather/lib/src/widgets/text_line.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/text_line.dart#L317

Added line #L317 was not covered by tests
} else if (nodeStyle.containsSame(ParchmentAttribute.subscript)) {
result = _mergeTextStyleWithDecoration(result, theme.subscript);

Check warning on line 319 in packages/fleather/lib/src/widgets/text_line.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/text_line.dart#L319

Added line #L319 was not covered by tests
}

return result;
}

Expand Down
16 changes: 16 additions & 0 deletions packages/fleather/lib/src/widgets/theme.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:parchment/parchment.dart';

Expand Down Expand Up @@ -74,6 +76,12 @@ class FleatherThemeData {
/// Style of links in text.
final TextStyle link;

/// Style of superscript text.
final TextStyle superscript;

/// Style of subscript text.
final TextStyle subscript;

/// Default style theme for regular paragraphs of text.
final TextBlockTheme paragraph; // spacing: top: 6, bottom: 10

Expand Down Expand Up @@ -121,6 +129,8 @@ class FleatherThemeData {
required this.lists,
required this.quote,
required this.code,
required this.superscript,
required this.subscript,
});

factory FleatherThemeData.fallback(BuildContext context) {
Expand Down Expand Up @@ -262,6 +272,8 @@ class FleatherThemeData {
borderRadius: BorderRadius.circular(2),
),
),
superscript: const TextStyle(fontFeatures: [FontFeature.superscripts()]),
subscript: const TextStyle(fontFeatures: [FontFeature.subscripts()]),
);
}

Expand All @@ -271,6 +283,8 @@ class FleatherThemeData {
TextStyle? underline,
TextStyle? strikethrough,
TextStyle? link,
TextStyle? superscript,
TextStyle? subscript,
InlineCodeThemeData? inlineCode,
TextBlockTheme? paragraph,
TextBlockTheme? heading1,
Expand Down Expand Up @@ -300,6 +314,8 @@ class FleatherThemeData {
lists: lists ?? this.lists,
quote: quote ?? this.quote,
code: code ?? this.code,
superscript: superscript ?? this.superscript,
subscript: subscript ?? this.subscript,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,32 +106,35 @@ void main() {
when(() => editorState.textEditingValue)
.thenReturn(initialTextEditingValue);
when(() => editorState.themeData).thenReturn(FleatherThemeData(
bold: const TextStyle(),
italic: const TextStyle(),
underline: const TextStyle(),
strikethrough: const TextStyle(),
inlineCode: InlineCodeThemeData(style: const TextStyle()),
link: const TextStyle(),
paragraph: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
heading1: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
heading2: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
heading3: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
heading4: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
heading5: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
heading6: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
lists: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
quote: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
code: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing())));
bold: const TextStyle(),
italic: const TextStyle(),
underline: const TextStyle(),
strikethrough: const TextStyle(),
inlineCode: InlineCodeThemeData(style: const TextStyle()),
link: const TextStyle(),
paragraph: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
heading1: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
heading2: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
heading3: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
heading4: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
heading5: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
heading6: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
lists: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
quote: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
code: TextBlockTheme(
style: const TextStyle(), spacing: const VerticalSpacing()),
superscript: const TextStyle(),
subscript: const TextStyle(),
));
when(() => rawEditor.controller).thenReturn(controller);
when(() => rawEditor.readOnly).thenReturn(false);
when(() => rawEditor.keyboardAppearance).thenReturn(Brightness.light);
Expand Down
25 changes: 25 additions & 0 deletions packages/parchment/lib/src/document/attributes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
ParchmentAttribute.direction.key: ParchmentAttribute.direction,
ParchmentAttribute.alignment.key: ParchmentAttribute.alignment,
ParchmentAttribute.indent.key: ParchmentAttribute.indent,
ParchmentAttribute.script.key: ParchmentAttribute.script,
};

// Inline attributes
Expand Down Expand Up @@ -128,6 +129,16 @@
// ignore: const_eval_throws_exception
static const link = LinkAttributeBuilder._();

/// Script style attribute.
// ignore: const_eval_throws_exception
static const script = _ScriptAttributeBuilder._();

/// Alias for [ParchmentAttribute.script.superscript].
static ParchmentAttribute<String> get superscript => script.superscript;

Check warning on line 137 in packages/parchment/lib/src/document/attributes.dart

View check run for this annotation

Codecov / codecov/patch

packages/parchment/lib/src/document/attributes.dart#L137

Added line #L137 was not covered by tests

/// Alias for [ParchmentAttribute.script.subscript].
static ParchmentAttribute<String> get subscript => script.subscript;

Check warning on line 140 in packages/parchment/lib/src/document/attributes.dart

View check run for this annotation

Codecov / codecov/patch

packages/parchment/lib/src/document/attributes.dart#L140

Added line #L140 was not covered by tests

// Line attributes

/// Heading style attribute.
Expand Down Expand Up @@ -433,6 +444,20 @@
: super._('c', ParchmentAttributeScope.inline, true);
}

/// Builder for super/sub script style attributes.
class _ScriptAttributeBuilder extends ParchmentAttributeBuilder<String> {
const _ScriptAttributeBuilder._()
: super._('script', ParchmentAttributeScope.inline);

Check warning on line 450 in packages/parchment/lib/src/document/attributes.dart

View check run for this annotation

Codecov / codecov/patch

packages/parchment/lib/src/document/attributes.dart#L450

Added line #L450 was not covered by tests

/// Creates a subscript attribute.
ParchmentAttribute<String> get superscript =>
ParchmentAttribute<String>._(key, scope, 'super');

Check warning on line 454 in packages/parchment/lib/src/document/attributes.dart

View check run for this annotation

Codecov / codecov/patch

packages/parchment/lib/src/document/attributes.dart#L453-L454

Added lines #L453 - L454 were not covered by tests

/// Creates a superscript attribute.
ParchmentAttribute<String> get subscript =>
ParchmentAttribute<String>._(key, scope, 'sub');

Check warning on line 458 in packages/parchment/lib/src/document/attributes.dart

View check run for this annotation

Codecov / codecov/patch

packages/parchment/lib/src/document/attributes.dart#L457-L458

Added lines #L457 - L458 were not covered by tests
}

/// Builder for color-based style attributes.
///
/// Useful in scenarios when a color attribute value is not known upfront.
Expand Down
Loading