Skip to content

Commit ec8a193

Browse files
authored
markdown: example improvements (#2361)
1 parent 29ca875 commit ec8a193

1 file changed

Lines changed: 12 additions & 23 deletions

File tree

pkgs/markdown/example/app.dart

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ final extensionSets = {
3939
'gfm-radio': md.ExtensionSet.gitHubWeb,
4040
};
4141

42+
final _radioGroups = [basicRadio, commonmarkRadio, gfmRadio];
43+
4244
void main() {
4345
versionSpan.textContent = 'v${md.version}';
44-
markdownInput.onKeyUp.listen(_renderMarkdown);
46+
markdownInput.onInput.listen(_renderMarkdown);
4547

4648
final savedMarkdown = window.localStorage.getItem('markdown');
4749

@@ -88,11 +90,14 @@ void _renderMarkdown([Event? event]) {
8890

8991
void _typeItOut(String msg, int pos) {
9092
late Timer timer;
91-
markdownInput.onKeyUp.listen((_) {
93+
late StreamSubscription<Event> sub;
94+
sub = markdownInput.onInput.listen((_) {
9295
timer.cancel();
96+
sub.cancel();
9397
});
9498
void addCharacter() {
9599
if (pos > msg.length) {
100+
sub.cancel();
96101
return;
97102
}
98103
markdownInput.value = msg.substring(0, pos);
@@ -107,23 +112,13 @@ void _typeItOut(String msg, int pos) {
107112

108113
void _switchFlavor(Event e) {
109114
final target = e.currentTarget as HTMLElement;
110-
if (target.attributes.getNamedItem('checked') == null) {
111-
if (basicRadio != target) {
112-
basicRadio.attributes.safeRemove('checked');
113-
basicRadio.querySelector('.glyph')!.textContent =
114-
'radio_button_unchecked';
115-
}
116-
if (commonmarkRadio != target) {
117-
commonmarkRadio.attributes.safeRemove('checked');
118-
commonmarkRadio.querySelector('.glyph')!.textContent =
119-
'radio_button_unchecked';
120-
}
121-
if (gfmRadio != target) {
122-
gfmRadio.attributes.safeRemove('checked');
123-
gfmRadio.querySelector('.glyph')!.textContent = 'radio_button_unchecked';
115+
if (target.getAttribute('checked') == null) {
116+
for (final radio in _radioGroups.where((r) => r != target)) {
117+
radio.removeAttribute('checked');
118+
radio.querySelector('.glyph')!.textContent = 'radio_button_unchecked';
124119
}
125120

126-
target.attributes.getNamedItem('checked')?.value = '';
121+
target.setAttribute('checked', '');
127122
target.querySelector('.glyph')!.textContent = 'radio_button_checked';
128123
extensionSet = extensionSets[target.id];
129124
_renderMarkdown();
@@ -134,12 +129,6 @@ extension on NodeList {
134129
List<Node> get items => [for (var i = 0; i < length; i++) item(i)!];
135130
}
136131

137-
extension on NamedNodeMap {
138-
void safeRemove(String qualifiedName) {
139-
if (getNamedItem(qualifiedName) != null) removeNamedItem(qualifiedName);
140-
}
141-
}
142-
143132
extension on HTMLDivElement {
144133
// The default implementation allows `JSAny` to support trusted types. We only
145134
// use `String`s, so prefer this to avoid manual conversions.

0 commit comments

Comments
 (0)