Skip to content

Commit 221e0dc

Browse files
committed
refactor(example): Colored romanized output
1 parent 0c55d88 commit 221e0dc

2 files changed

Lines changed: 70 additions & 23 deletions

File tree

example/lib/main.client.dart

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,31 @@ import 'app.dart';
1010

1111
@pragma('vm:entry-point')
1212
@isolateManagerWorker
13-
Future<String> romanize(String text) async {
13+
Future<List<Object?>> romanize(String text) async {
1414
await TextRomanizer.ensureInitialized();
15-
return TextRomanizer.romanize(text);
15+
if (text.isEmpty) {
16+
return <Map<String, String>>[];
17+
}
18+
return TextRomanizer.analyze(text).map<Map<String, String>>((text) {
19+
return {
20+
'rawText': text.rawText,
21+
'language': text.language,
22+
'romanizedText': text.romanizedText,
23+
};
24+
}).toList();
1625
}
1726

1827
final service = RomanizationService();
1928
void main() async {
2029
await service.init();
30+
// Warm up the isolate.
31+
service.convert('');
2132

2233
runApp(App());
2334
}
2435

2536
class RomanizationService {
26-
late final IsolateManager<String, String> _manager;
37+
late final IsolateManager<List<Object?>, String> _manager;
2738

2839
Future<void> init() async {
2940
_manager = IsolateManager.create(
@@ -35,7 +46,13 @@ class RomanizationService {
3546
await _manager.start();
3647
}
3748

38-
Future<String> convert(String text) async {
39-
return await _manager.compute(text);
49+
Future<List<RomanizedText>> convert(String text) async {
50+
return (await _manager.compute(text) as List).cast<Map<Object?, Object?>>().map((e) {
51+
return RomanizedText(
52+
rawText: e['rawText'] as String,
53+
language: e['language'] as String,
54+
romanizedText: e['romanizedText'] as String,
55+
);
56+
}).toList();
4057
}
4158
}

example/lib/pages/home.dart

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,40 @@ class Home extends StatefulComponent {
1313

1414
class _HomeState extends State<Home> {
1515
String _text = '안녕하세요, 만나서 반갑습니다.';
16-
late String _romanized = TextRomanizer.romanize(_text);
16+
late List<RomanizedText> _romanized = TextRomanizer.analyze(_text);
1717

1818
void _syncScroll(web.Event event, String otherId) {
19-
final source = event.target as web.HTMLTextAreaElement;
20-
final other = web.document.getElementById(otherId) as web.HTMLTextAreaElement?;
21-
if (other != null && other.scrollTop != source.scrollTop) {
19+
final source = event.target as web.HTMLElement;
20+
final other = web.document.getElementById(otherId) as web.HTMLElement?;
21+
if (other != null) {
2222
other.scrollTop = source.scrollTop;
2323
}
2424
}
2525

26+
Color _getColorForLanguage(String languageCode) {
27+
switch (languageCode) {
28+
case 'korean':
29+
return Color('#3B82F6'); // Blue
30+
case 'japanese':
31+
return Color('#EF4444'); // Red
32+
case 'chinese':
33+
return Color('#F59E0B'); // Amber
34+
case 'arabic':
35+
return Color('#10B981'); // Green
36+
case 'hebrew':
37+
return Color('#EC4899'); // Pink
38+
case 'cyrillic':
39+
return Color('#8B5CF6'); // Purple
40+
default:
41+
return Color('#6B7280'); // Neutral Gray
42+
}
43+
}
44+
2645
@override
2746
Component build(BuildContext context) {
2847
final textareaStyle = Styles(
2948
width: Unit.percent(100),
30-
height: Unit.percent(100),
49+
height: Unit.vh(60),
3150
padding: Spacing.all(Unit.pixels(16)),
3251
boxSizing: BoxSizing.borderBox,
3352
border: Border.all(color: Color('#d1d5db')),
@@ -39,7 +58,9 @@ class _HomeState extends State<Home> {
3958
offsetX: Unit.zero,
4059
offsetY: Unit.pixels(1),
4160
),
61+
fontFamily: FontFamily('inherit'),
4262
fontSize: Unit.rem(1),
63+
lineHeight: Unit.rem(1.5),
4364
backgroundColor: Colors.white,
4465
raw: {
4566
'resize': 'none',
@@ -48,9 +69,11 @@ class _HomeState extends State<Home> {
4869

4970
final sectionStyle = Styles(
5071
display: Display.flex,
72+
height: Unit.percent(70),
73+
minWidth: Unit.pixels(300),
5174
flexDirection: FlexDirection.column,
5275
gap: Gap.all(Unit.pixels(12)),
53-
flex: Flex(grow: 1, shrink: 1),
76+
flex: Flex(grow: 1, shrink: 0, basis: Unit.pixels(400)),
5477
);
5578

5679
final labelStyle = Styles(
@@ -79,35 +102,42 @@ class _HomeState extends State<Home> {
79102
onInput: (text) async {
80103
_text = text;
81104
_romanized = await service.convert(text);
82-
if (mounted) {
83-
setState(() {});
84-
}
105+
if (mounted) setState(() {});
85106
},
86-
rows: 15,
87107
required: true,
88108
spellCheck: SpellCheck.isFalse,
89-
styles: textareaStyle,
109+
styles: textareaStyle.combine(
110+
Styles(raw: {'resize': 'none'}),
111+
),
90112
[.text(_text)],
91113
),
92114
]),
93115
div(styles: sectionStyle, [
94116
h3(styles: labelStyle, [.text('Output')]),
95-
textarea(
117+
div(
96118
id: 'output-area',
97119
events: {
98120
'scroll': (e) => _syncScroll(e, 'input-area'),
99121
},
100-
placeholder: 'Type your text here...',
101-
rows: 15,
102-
readonly: true,
103-
spellCheck: SpellCheck.isFalse,
104122
styles: textareaStyle.combine(
105123
Styles(
106-
color: Color('#4b5563'),
124+
overflow: Overflow.auto,
125+
whiteSpace: WhiteSpace.preWrap,
107126
backgroundColor: Color('#f9fafb'),
127+
raw: {
128+
'word-break': 'break-word',
129+
},
108130
),
109131
),
110-
[.text(_romanized)],
132+
_romanized.map((item) {
133+
return span(
134+
styles: Styles(
135+
color: _getColorForLanguage(item.language),
136+
fontWeight: FontWeight.w500,
137+
),
138+
[.text(item.romanizedText)],
139+
);
140+
}).toList(),
111141
),
112142
]),
113143
],

0 commit comments

Comments
 (0)