Skip to content

Commit

Permalink
Merge pull request #44 from rohanjsh/dev
Browse files Browse the repository at this point in the history
feat: v2.1.0
  • Loading branch information
rohanjsh authored Jan 14, 2024
2 parents 3d723b5 + 71d6da9 commit b0dec34
Show file tree
Hide file tree
Showing 25 changed files with 963 additions and 343 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
pull_request:
branches:
- main
- dev

jobs:
semantic_pull_request:
Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@

# Changelog

# 2.1.0
### New
- Introducing context menus, use `getTypesetContextMenus()` to get the context menus for the text.
- Change default monospace font to `Courier`, as provided by the operating system.

### Breaking
- `TypeSet(inputText: "Foo")` is now `TypeSet("Foo")` to match semantics of `Text("Foo")`

### Other
- Remove `google_fonts` dependency
- 95% test coverage
- Deprecated parser removed

# 2.0.0
### New Features
- **Dynamic Font Sizing:** Font size can now be applied dynamically, enhancing visual hierarchy and readability.
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,33 @@ Utilize the TypeSet widget as easily as you would use the native `Text.rich()` i
import 'package:typeset/typeset.dart';
// Bold Text Example
TypeSet(inputText: 'Hello, *World!*'); // Renders 'World!' in bold
TypeSet('Hello, *World!*'); // Renders 'World!' in bold
// Italic Text Example
TypeSet(inputText: 'Hello, _World!_'); // Renders 'World!' in italic
TypeSet('Hello, _World!_'); // Renders 'World!' in italic
// Strikethrough Text Example
TypeSet(inputText: 'Hello, ~World!~'); // Renders 'World!' with a strikethrough
TypeSet('Hello, ~World!~'); // Renders 'World!' with a strikethrough
// Underline Text Example
TypeSet(inputText: 'Hello #World!#'); // Renders 'World!' underlined
TypeSet('Hello #World!#'); // Renders 'World!' underlined
// Monospace Text Example
TypeSet(inputText: 'Hello, `World!`'); // Renders 'World!' in monospace
TypeSet('Hello, `World!`'); // Renders 'World!' in monospace

// Hyperlink Text Example
TypeSet(inputText: '§google.com|https://google.com§'); // Renders 'google.com' as a clickable link
TypeSet('§google.com|https://google.com§'); // Renders 'google.com' as a clickable link

// Dynamic Font Size Example
TypeSet(inputText: 'Hey, *Hello world<30>*'); // Renders 'Hello world' with font size 30
TypeSet('Hey, *Hello world<30>*'); // Renders 'Hello world' with font size 30
```
TypeSet inherits all properties of the `Text.rich()` widget, allowing for a familiar and versatile configuration experience.

### Use **`¦` (broken bar)** and append with any reserved character to render them.
Example:
```dart
Typeset(inputText: '*Hello ¦* World*');
Typeset('*Hello ¦* World*');
```
## Features 🎨

Expand Down
15 changes: 7 additions & 8 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ Link
height: 12,
),
TypeSet(
inputText:
'→ *TypeSet* _can_ #style# ~everything~ `you need` §with|https://rohanjsh.dev/§ _dynamic<18>_ _font<28>_ _size<25>_',
'→ *TypeSet* _can_ #style# ~everything~ `you need` §with|https://rohanjsh.dev/§ _dynamic<18>_ _font<28>_ _size<25>_',
style: TextStyle(
fontSize: 18,
),
Expand All @@ -110,7 +109,7 @@ Link
height: 12,
),
TypeSet(
inputText: 'Bold:\n→ *Bold Text*',
'Bold:\n→ *Bold Text*',
style: TextStyle(
fontSize: 24,
),
Expand All @@ -119,7 +118,7 @@ Link
height: 20,
),
TypeSet(
inputText: 'Italic:\n→ _Italic Text_ ',
'Italic:\n→ _Italic Text_ ',
style: TextStyle(
fontSize: 24,
),
Expand All @@ -128,7 +127,7 @@ Link
height: 20,
),
TypeSet(
inputText: 'Underline:\n→ #Underline Text#',
'Underline:\n→ #Underline Text#',
style: TextStyle(
fontSize: 24,
),
Expand All @@ -137,7 +136,7 @@ Link
height: 20,
),
TypeSet(
inputText: 'Strikethrough:\n→ ~Strikethrough Text~',
'Strikethrough:\n→ ~Strikethrough Text~',
style: TextStyle(
fontSize: 24,
),
Expand All @@ -146,7 +145,7 @@ Link
height: 20,
),
TypeSet(
inputText: 'Monospace:\n→ `monospace text`',
'Monospace:\n→ `monospace text`',
style: TextStyle(
fontSize: 24,
),
Expand All @@ -157,7 +156,7 @@ Link

//customized link textstyle and recognizer (tap recognizer)
TypeSet(
inputText: 'Link:\n→ §google.com|https://google.com§',
'Link:\n→ §google.com|https://google.com§',
style: TextStyle(
fontSize: 24,
),
Expand Down
11 changes: 9 additions & 2 deletions example/macos/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
PODS:
- FlutterMacOS (1.0.0)
- path_provider_foundation (0.0.1):
- Flutter
- FlutterMacOS
- url_launcher_macos (0.0.1):
- FlutterMacOS

DEPENDENCIES:
- FlutterMacOS (from `Flutter/ephemeral`)
- path_provider_foundation (from `Flutter/ephemeral/.symlinks/plugins/path_provider_foundation/darwin`)
- url_launcher_macos (from `Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos`)

EXTERNAL SOURCES:
FlutterMacOS:
:path: Flutter/ephemeral
path_provider_foundation:
:path: Flutter/ephemeral/.symlinks/plugins/path_provider_foundation/darwin
url_launcher_macos:
:path: Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos

SPEC CHECKSUMS:
FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24
url_launcher_macos: 5335912b679c073563f29d89d33d10d459f95451
path_provider_foundation: c68054786f1b4f3343858c1e1d0caaded73f0be9
url_launcher_macos: d2691c7dd33ed713bf3544850a623080ec693d95

PODFILE CHECKSUM: 353c8bcc5d5b0994e508d035b5431cfe18c1dea7

COCOAPODS: 1.11.3
COCOAPODS: 1.14.3
2 changes: 1 addition & 1 deletion example/macos/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0920;
LastUpgradeCheck = 1300;
LastUpgradeCheck = 1430;
ORGANIZATIONNAME = "";
TargetAttributes = {
33CC10EC2044A3C60003C045 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1300"
LastUpgradeVersion = "1430"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
159 changes: 2 additions & 157 deletions lib/src/core/typeset_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,159 +7,6 @@ import 'package:url_launcher/url_launcher.dart';

///[TypesetParser]
class TypesetParser {
static const _kBoldChar = '*';

static const _kItalicChar = '_';

static const _kStrikeThroughChar = '~';

static const _kUnderlineChar = '//';

static const _kMonoSpaceChar = '`';

static const _kLinkStartChar = '[';

static const _kLinkEndChar = ']';

static const _kLinkUrlSeparator = '|';

static const _kSpace = ' ';

static const _kEmpty = '';

///[parseText] this method will parse the [inputText] and
///return a list of [TextSpan] with the correct styles applied to it
@Deprecated('parseText will be removed in upcoming versions, use parser')
static List<TextSpan> parseText({
required String inputText,
TextStyle? linkStyle,
GestureRecognizer? recognizer,
TextStyle? monospaceStyle,
}) {
final words = inputText.split(_kSpace);
return words.asMap().entries.map<TextSpan>(
(entry) {
final index = entry.key;
final word = entry.value;

if (_isLink(word)) {
return _getLinkSpan(
word: word,
linkStyle: linkStyle,
recognizer: recognizer,
);
}

final style = _getStyle(
word,
monospaceStyle: monospaceStyle,
);
final text = _getText(word, style);
final space = index == words.length - 1 ? _kEmpty : _kSpace;

return TextSpan(
text: '$text$space',
style: style,
);
},
).toList();
}

//*for link
static bool _isLink(String word) {
return word.startsWith(_kLinkStartChar) &&
word.endsWith(_kLinkEndChar) &&
word.contains(_kLinkUrlSeparator) &&
word.length > 3;
}

static TextSpan _getLinkSpan({
required String word,
TextStyle? linkStyle,
GestureRecognizer? recognizer,
}) {
if (word.length < 3 || !word.startsWith('[') || !word.endsWith(']')) {
// The word is not a valid link, return a plain text span.
return TextSpan(text: word);
}
final linkParts =
word.substring(1, word.length - 1).split(_kLinkUrlSeparator);
final linkText = linkParts[0];
final linkUrl = linkParts[1];

return TextSpan(
text: linkText,
style: linkStyle ??
const TextStyle(
color: Colors.blue,
decoration: TextDecoration.underline,
),
recognizer: recognizer ??
(TapGestureRecognizer()
..onTap = () async {
if (await canLaunchUrl(
Uri.parse(linkUrl),
)) {
await launchUrl(
Uri.parse(linkUrl),
);
}
}),
);
}

//* for other styles
static TextStyle _getStyle(String word, {TextStyle? monospaceStyle}) {
final hasBold = word.startsWith(_kBoldChar) &&
word.endsWith(_kBoldChar) &&
word.length > 1;
final hasItalic = word.startsWith(_kItalicChar) &&
word.endsWith(_kItalicChar) &&
word.length > 1;
final hasStrikeThrough = word.startsWith(_kStrikeThroughChar) &&
word.endsWith(_kStrikeThroughChar) &&
word.length > 1;
final hasUnderline = word.startsWith(_kUnderlineChar) &&
word.endsWith(_kUnderlineChar) &&
word.length > 3;
final hasMonoSpace = word.startsWith(_kMonoSpaceChar) &&
word.endsWith(_kMonoSpaceChar) &&
word.length > 1;

if (hasMonoSpace) {
return monospaceStyle ??
GoogleFonts.sourceCodePro(
textStyle: const TextStyle(
fontWeight: FontWeight.normal,
),
);
}

return TextStyle(
fontWeight: hasBold ? FontWeight.bold : null,
fontStyle: hasItalic ? FontStyle.italic : null,
decoration: hasStrikeThrough
? TextDecoration.lineThrough
: hasUnderline
? TextDecoration.underline
: null,
);
}

static String _getText(String word, TextStyle style) {
if (style.fontWeight != null ||
style.fontStyle != null ||
style.decoration != null) {
if (style.decoration == TextDecoration.underline) {
return word.substring(2, word.length - 2);
} else {
return word.substring(1, word.length - 1);
}
} else {
return word;
}
}

/// Parses the input text and generates a list of [TextSpan] objects based on
/// different styles and formatting options.
///
Expand Down Expand Up @@ -266,10 +113,8 @@ class TypesetParser {
TextSpan(
text: justText,
style: monospaceStyle ??
GoogleFonts.sourceCodePro(
textStyle: TextStyle(
fontSize: fontSize,
),
const TextStyle(
fontFamily: 'Courier',
),
),
);
Expand Down
12 changes: 12 additions & 0 deletions lib/src/core/typeset_reserved.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,16 @@ final class TypesetReserved {

/// Regex to identify links
static const fontSizeRegex = r'(.+?)<(\d+)>';

/// all set of reserved characters
static const all = {
escapeLiteral,
boldChar,
italicChar,
strikethroughChar,
monospaceChar,
underlineChar,
linkChar,
linkSplitChar,
};
}
Loading

0 comments on commit b0dec34

Please sign in to comment.