-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wikimedia Commons field hack, and releasing 3.1
- Loading branch information
Showing
6 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import 'package:every_door/constants.dart'; | ||
import 'package:every_door/models/amenity.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:every_door/models/field.dart'; | ||
|
||
class WikiCommonsPresetField extends PresetField { | ||
const WikiCommonsPresetField({ | ||
String? key, | ||
String? label, | ||
IconData? icon, | ||
String? placeholder, | ||
FieldPrerequisite? prerequisite, | ||
}) : super( | ||
key: key ?? 'wikimedia_commons', | ||
label: label ?? 'Wikimedia Commons', | ||
icon: icon, | ||
placeholder: placeholder, | ||
prerequisite: prerequisite); | ||
|
||
@override | ||
Widget buildWidget(OsmChange element) => WikiCommonsInputField(this, element); | ||
} | ||
|
||
class WikiCommonsInputField extends StatefulWidget { | ||
final WikiCommonsPresetField field; | ||
final OsmChange element; | ||
|
||
const WikiCommonsInputField(this.field, this.element); | ||
|
||
@override | ||
State createState() => _WikiCommonsInputFieldState(); | ||
} | ||
|
||
class _WikiCommonsInputFieldState extends State<WikiCommonsInputField> { | ||
late final TextEditingController _controller; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_controller = | ||
TextEditingController(text: widget.element[widget.field.key] ?? ''); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_controller.dispose(); | ||
super.dispose(); | ||
} | ||
|
||
final kReMarkup = RegExp(r'^\s*\[\[([^|\]]+)(?:\|.*)?\]\]\s*$'); | ||
final kReURL = RegExp(r'^.*(https://commons\S+(?:File|Category)\S+\.[a-z]+)\s*$'); | ||
|
||
String _removeWikiMarkup(String value) { | ||
final match = kReMarkup.matchAsPrefix(value); | ||
if (match != null) return match.group(1)!; | ||
final urlMatch = kReURL.firstMatch(value); | ||
if (urlMatch != null) return urlMatch.group(1)!; | ||
return value; | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final value = widget.element[widget.field.key] ?? ''; | ||
if (value != _controller.text.trim()) { | ||
// Hopefully that's not the time when we type a letter in the field. | ||
// TODO: only update when page is back from inactive? | ||
_controller.text = value; | ||
} | ||
|
||
return Padding( | ||
padding: EdgeInsets.only(right: 10.0), | ||
child: TextField( | ||
controller: _controller, | ||
keyboardType: TextInputType.url, | ||
decoration: InputDecoration( | ||
hintText: widget.field.placeholder, | ||
labelText: widget.field.icon != null ? widget.field.label : null, | ||
), | ||
style: kFieldTextStyle, | ||
maxLength: value.length > 200 ? 255 : null, | ||
onChanged: (value) { | ||
// On every keypress, since the focus can change at any minute. | ||
setState(() { | ||
widget.element[widget.field.key] = _removeWikiMarkup(value.trim()); | ||
}); | ||
}, | ||
), | ||
); | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters