Skip to content

Commit 4ee9150

Browse files
committed
chore(example): Improve romanization feedback to end user
1 parent 7aa4d5f commit 4ee9150

3 files changed

Lines changed: 55 additions & 32 deletions

File tree

example/lib/app.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class App extends StatelessComponent {
3636
.text('Dart Romanize'),
3737
p(
3838
styles: Styles(
39-
margin: Spacing.only(top: Unit.pixels(8)),
39+
margin: Spacing.all(Unit.pixels(8)),
4040
color: Color('#6b7280'),
4141
fontSize: Unit.rem(1),
4242
),

example/lib/main.dart

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ void main() async {
99
_chinese();
1010
_hebrew();
1111
_multiLanguages();
12+
_analyze();
1213
}
1314

1415
void _korean() {
@@ -88,11 +89,10 @@ void _hebrew() {
8889
print('Hebrew Romanization: \n$hebrewOutput');
8990
}
9091

91-
void _multiLanguages() {
92-
final multiLanguagesText = '''Mixed Script Stress Test:
92+
const multiLanguagesText = '''Mixed Script Stress Test:
9393
-------------------------
9494
1. CJK Ambiguity (Should detect Chinese vs Japanese context):
95-
中文 (Chinese) vs 日本語 (Japanese)
95+
中文 (Chinese) vs 日本語 (Japanese will likely fail)
9696
你好世界 (Hello World - CN) mixed with こんにちは (Hello - JP)
9797
東京 (Tokyo - JP/CN chars) vs 北京 (Beijing - CN)
9898
@@ -121,6 +121,18 @@ void _multiLanguages() {
121121
And finally Hebrew: ולבסוף עברית.
122122
123123
End of Stress Test.''';
124+
void _multiLanguages() {
124125
final multiLanguagesOutput = TextRomanizer.romanize(multiLanguagesText);
125126
print('Multi Languages Romanization: \n$multiLanguagesOutput');
126127
}
128+
129+
void _analyze() {
130+
final output = TextRomanizer.analyze(multiLanguagesText)
131+
.map(
132+
(e) {
133+
return e.romanizedText;
134+
},
135+
)
136+
.join('');
137+
print('Analyze Romanization: \n$output');
138+
}

example/lib/pages/home.dart

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:example/main.client.dart';
2+
import 'package:example/main.dart';
23
import 'package:jaspr/dom.dart';
34
import 'package:jaspr/jaspr.dart';
45
import 'package:romanize/romanize.dart';
@@ -12,8 +13,8 @@ class Home extends StatefulComponent {
1213
}
1314

1415
class _HomeState extends State<Home> {
15-
String _text = '안녕하세요, 만나서 반갑습니다.';
16-
late List<RomanizedText> _romanized = TextRomanizer.analyze(_text);
16+
String _text = multiLanguagesText;
17+
late Future<List<RomanizedText>> _romanized = service.convert(_text);
1718

1819
void _syncScroll(web.Event event, String otherId) {
1920
final source = event.target as web.HTMLElement;
@@ -70,10 +71,10 @@ class _HomeState extends State<Home> {
7071
final sectionStyle = Styles(
7172
display: Display.flex,
7273
height: Unit.percent(70),
73-
minWidth: Unit.pixels(300),
74+
minWidth: Unit.pixels(200),
7475
flexDirection: FlexDirection.column,
7576
gap: Gap.all(Unit.pixels(12)),
76-
flex: Flex(grow: 1, shrink: 0, basis: Unit.pixels(400)),
77+
flex: Flex(grow: 1, shrink: 0, basis: Unit.pixels(200)),
7778
);
7879

7980
final labelStyle = Styles(
@@ -100,9 +101,10 @@ class _HomeState extends State<Home> {
100101
},
101102
placeholder: 'Type your text here...',
102103
onInput: (text) async {
103-
_text = text;
104-
_romanized = await service.convert(text);
105-
if (mounted) setState(() {});
104+
setState(() {
105+
_text = text;
106+
_romanized = service.convert(text);
107+
});
106108
},
107109
required: true,
108110
spellCheck: SpellCheck.isFalse,
@@ -114,30 +116,39 @@ class _HomeState extends State<Home> {
114116
]),
115117
div(styles: sectionStyle, [
116118
h3(styles: labelStyle, [.text('Output')]),
117-
div(
118-
id: 'output-area',
119-
events: {
120-
'scroll': (e) => _syncScroll(e, 'input-area'),
121-
},
122-
styles: textareaStyle.combine(
123-
Styles(
124-
overflow: Overflow.auto,
125-
whiteSpace: WhiteSpace.preWrap,
126-
backgroundColor: Color('#f9fafb'),
127-
raw: {
128-
'word-break': 'break-word',
119+
FutureBuilder<List<RomanizedText>>(
120+
future: _romanized,
121+
builder: (context, snapshot) {
122+
return div(
123+
id: 'output-area',
124+
events: {
125+
'scroll': (e) => _syncScroll(e, 'input-area'),
129126
},
130-
),
131-
),
132-
_romanized.map((item) {
133-
return span(
134-
styles: Styles(
135-
color: _getColorForLanguage(item.language),
136-
fontWeight: FontWeight.w500,
127+
styles: textareaStyle.combine(
128+
Styles(
129+
overflow: Overflow.auto,
130+
whiteSpace: WhiteSpace.preWrap,
131+
backgroundColor: Color('#f9fafb'),
132+
raw: {
133+
'word-break': 'break-word',
134+
},
135+
),
137136
),
138-
[.text(item.romanizedText)],
137+
snapshot.hasData
138+
? snapshot.data!.map((item) {
139+
return span(
140+
styles: Styles(
141+
color: _getColorForLanguage(item.language),
142+
fontWeight: FontWeight.w500,
143+
),
144+
[.text(item.romanizedText)],
145+
);
146+
}).toList()
147+
: [
148+
.text('Loading resources...'),
149+
],
139150
);
140-
}).toList(),
151+
},
141152
),
142153
]),
143154
],

0 commit comments

Comments
 (0)