augment.dart 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/widgets.dart';
  4. import 'package:http/http.dart' as http;
  5. import 'dart:convert';
  6. import 'dart:ui_web' as ui;
  7. import 'dart:html' as html;
  8. import 'dart:js' as js;
  9. import 'api_service.dart';
  10. class EmailToolbar extends StatefulWidget {
  11. final Function(String) onJumpToSpan;
  12. final VoidCallback onButtonPressed;
  13. EmailToolbar(
  14. {Key? key, required this.onButtonPressed, required this.onJumpToSpan})
  15. : super(key: key);
  16. @override
  17. _DynamicClassesAugment createState() => _DynamicClassesAugment();
  18. }
  19. class _DynamicClassesAugment extends State<EmailToolbar> {
  20. String selectedClass = 'Class 1';
  21. TextEditingController _jumpController = TextEditingController();
  22. late final FocusNode _JumpItemfocusNode;
  23. late final FocusNode _viewSpecsfocusNode;
  24. bool _jumpItemHasFocus = false;
  25. bool _viewSpecsHasFocus = false;
  26. @override
  27. void initState() {
  28. super.initState();
  29. _JumpItemfocusNode = FocusNode();
  30. _viewSpecsfocusNode = FocusNode();
  31. _JumpItemfocusNode.addListener(() {
  32. setState(() => _jumpItemHasFocus = _JumpItemfocusNode.hasFocus);
  33. });
  34. _viewSpecsfocusNode.addListener(() {
  35. setState(() => _viewSpecsHasFocus = _viewSpecsfocusNode.hasFocus);
  36. });
  37. }
  38. @override
  39. void dispose() {
  40. // _JumpItemfocusNode.dispose();
  41. // _viewSpecsfocusNode.dispose();
  42. _jumpController.dispose();
  43. super.dispose();
  44. }
  45. @override
  46. Widget build(BuildContext context) {
  47. const animationDuration = Duration(milliseconds: 250);
  48. return Column(children: [
  49. Row(
  50. children: [
  51. ElevatedButton(
  52. onPressed: () => AugmentClasses.handleHome(context),
  53. child: Text('Home'),
  54. ),
  55. SizedBox(width: 8),
  56. ElevatedButton(
  57. onPressed: AugmentClasses.handleReload,
  58. child: Text('Reload'),
  59. ),
  60. ElevatedButton(
  61. onPressed: AugmentClasses.handleImages,
  62. child: Text('Images'),
  63. ),
  64. SizedBox(width: 8),
  65. ElevatedButton(
  66. onPressed: AugmentClasses.handleOpen,
  67. child: Text('Open'),
  68. ),
  69. // SizedBox(width: 8),
  70. ElevatedButton(
  71. onPressed: AugmentClasses.handleFind,
  72. child: Text('Find'),
  73. ),
  74. // SizedBox(width: 8),
  75. ElevatedButton(
  76. onPressed: AugmentClasses.handleStop,
  77. child: Text('Stop'),
  78. ),
  79. Spacer(),
  80. PopupMenuButton<String>(
  81. onSelected: (String value) {
  82. setState(() {
  83. selectedClass = value;
  84. print(selectedClass);
  85. });
  86. },
  87. itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
  88. const PopupMenuItem<String>(
  89. value: 'Class 1',
  90. child: Text('Class 1'),
  91. ),
  92. const PopupMenuItem<String>(
  93. value: 'Class 2',
  94. child: Text('Class 2'),
  95. ),
  96. const PopupMenuItem<String>(
  97. value: 'Turbo 3',
  98. child: Text('Turbo 3'),
  99. ),
  100. ],
  101. // child: ElevatedButton(
  102. // onPressed: () {},
  103. child: Text('Options'),
  104. ),
  105. ],
  106. ),
  107. if (selectedClass == 'Class 2')
  108. Stack(children: [
  109. Row(
  110. children: [
  111. ElevatedButton(
  112. onPressed: () => AugmentClasses.JumpButton(context),
  113. child: Text('JumpItem:'),
  114. ),
  115. // SizedBox(
  116. // width: 8,
  117. // ),
  118. Container(
  119. width: 150,
  120. height: 30,
  121. child: TextField(
  122. controller: _jumpController,
  123. decoration: InputDecoration(
  124. labelText: 'Jump Item',
  125. border: OutlineInputBorder(),
  126. suffixIcon: Icon(Icons.search)),
  127. onSubmitted: (value) {
  128. print("onSubmitted");
  129. if (value.isNotEmpty) {
  130. widget.onJumpToSpan(value);
  131. }
  132. },
  133. ),
  134. ),
  135. //TODO: Make an animation to make the button a textfield
  136. // AnimatedSwitcher(
  137. // duration: animationDuration,
  138. // transitionBuilder: (Widget child, Animation<double> animation) {
  139. // return FadeTransition(opacity: animation, child: child);
  140. // },
  141. // child: _jumpItemHasFocus
  142. // ? Container(
  143. // key: ValueKey('TextField1'),
  144. // width: 150,
  145. // child: TextField(
  146. // focusNode: _JumpItemfocusNode,
  147. // decoration: InputDecoration(
  148. // hintText: 'Enter Text',
  149. // border: OutlineInputBorder(),
  150. // ),
  151. // ),
  152. // )
  153. // : Container(
  154. // key: ValueKey('Button1'),
  155. // child: ElevatedButton(
  156. // onPressed: () => _JumpItemfocusNode.requestFocus(),
  157. // child: Text('Jump Item:'),
  158. // ),
  159. // ),
  160. // ),
  161. SizedBox(width: 8),
  162. Container(
  163. width: 150,
  164. height: 30,
  165. child: TextField(
  166. decoration: InputDecoration(
  167. labelText: 'viewSpecs',
  168. border: OutlineInputBorder(),
  169. suffixIcon: Icon(Icons.style_rounded)),
  170. ),
  171. ),
  172. ElevatedButton(
  173. onPressed: AugmentClasses.handleImages,
  174. child: Text('Filter'),
  175. ),
  176. SizedBox(width: 8),
  177. ElevatedButton(
  178. onPressed: AugmentClasses.handleOpen,
  179. child: Text('Lookup'),
  180. ),
  181. // SizedBox(width: 8),
  182. ElevatedButton(
  183. onPressed: AugmentClasses.handleFind,
  184. child: Text('Create Link'),
  185. ),
  186. ElevatedButton(
  187. onPressed: AugmentClasses.handleFind,
  188. child: Text('Paste Link'),
  189. ),
  190. ],
  191. )
  192. ])
  193. ]);
  194. }
  195. }
  196. class AugmentClasses {
  197. static void handleHome(BuildContext context) {
  198. Navigator.of(context).popUntil((route) => route.isFirst);
  199. }
  200. static void handleReload() {
  201. print("reload");
  202. }
  203. static void handleImages() {
  204. print("Images button pressed");
  205. }
  206. static void handleOpen() {
  207. print("Open button pressed");
  208. }
  209. static void handleFind() {
  210. print("Find button pressed");
  211. }
  212. static void handleStop() {
  213. print("Stop button pressed");
  214. }
  215. static void handleJump(String spanId) {
  216. String js_code = '''
  217. var iframe = document.getElementsByTagName('iframe')[0]; // 0 for the first iframe, 1 for the second, etc.
  218. // Check if the iframe is loaded and has content
  219. if (iframe && iframe.contentDocument) {
  220. // Access the document inside the iframe
  221. var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
  222. // Find the element with the specific id inside the iframe
  223. var targetElement = iframeDoc.getElementById("$spanId"); // Replace '36 ' with the actual id of the target element
  224. // If the element exists, scroll to it
  225. if (targetElement) {
  226. targetElement.scrollIntoView();
  227. console.log('Scrolled to element with id "$spanId" inside the iframe.');
  228. } else {
  229. console.log('Element with id "$spanId" not found inside the iframe.');
  230. }
  231. } else {
  232. console.log('Iframe not found or not loaded.');
  233. }
  234. ''';
  235. js.context.callMethod('eval', [js_code]);
  236. }
  237. static void invisibility(String htmlClass) {}
  238. static JumpButton(context) {
  239. showDialog(
  240. context: context,
  241. builder: (context) => AlertDialog(
  242. title: Text('Jump Item:'),
  243. content: Column(
  244. children: [
  245. Text('Jump (to) Item (at)'),
  246. TextField(),
  247. // ElevatedButton(
  248. // onPressed: AugmentClasses.ViewSpecsButton(context),
  249. // child: Text('ViewSpecs'),
  250. // ),
  251. ]
  252. ),
  253. )
  254. );
  255. }
  256. static ViewSpecsButton(context) {
  257. showDialog(
  258. context: context,
  259. builder:(context) => AlertDialog(
  260. title: Text('Viewspecs(short)') ,
  261. )
  262. );
  263. }
  264. }