augment.dart 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. Container(
  112. width: 150,
  113. height: 30,
  114. child: TextField(
  115. controller: _jumpController,
  116. decoration: InputDecoration(
  117. labelText: 'Jump Item',
  118. border: OutlineInputBorder(),
  119. suffixIcon: Icon(Icons.search)),
  120. onSubmitted: (value) {
  121. print("onSubmitted");
  122. if (value.isNotEmpty) {
  123. widget.onJumpToSpan(value);
  124. }
  125. },
  126. ),
  127. ),
  128. //TODO: Make an animation to make the button a textfield
  129. // AnimatedSwitcher(
  130. // duration: animationDuration,
  131. // transitionBuilder: (Widget child, Animation<double> animation) {
  132. // return FadeTransition(opacity: animation, child: child);
  133. // },
  134. // child: _jumpItemHasFocus
  135. // ? Container(
  136. // key: ValueKey('TextField1'),
  137. // width: 150,
  138. // child: TextField(
  139. // focusNode: _JumpItemfocusNode,
  140. // decoration: InputDecoration(
  141. // hintText: 'Enter Text',
  142. // border: OutlineInputBorder(),
  143. // ),
  144. // ),
  145. // )
  146. // : Container(
  147. // key: ValueKey('Button1'),
  148. // child: ElevatedButton(
  149. // onPressed: () => _JumpItemfocusNode.requestFocus(),
  150. // child: Text('Jump Item:'),
  151. // ),
  152. // ),
  153. // ),
  154. SizedBox(width: 8),
  155. Container(
  156. width: 150,
  157. height: 30,
  158. child: TextField(
  159. decoration: InputDecoration(
  160. labelText: 'viewSpecs',
  161. border: OutlineInputBorder(),
  162. suffixIcon: Icon(Icons.style_rounded)),
  163. ),
  164. ),
  165. ElevatedButton(
  166. onPressed: AugmentClasses.handleImages,
  167. child: Text('Filter'),
  168. ),
  169. SizedBox(width: 8),
  170. ElevatedButton(
  171. onPressed: AugmentClasses.handleOpen,
  172. child: Text('Lookup'),
  173. ),
  174. // SizedBox(width: 8),
  175. ElevatedButton(
  176. onPressed: AugmentClasses.handleFind,
  177. child: Text('Create Link'),
  178. ),
  179. ElevatedButton(
  180. onPressed: AugmentClasses.handleFind,
  181. child: Text('Paste Link'),
  182. ),
  183. ],
  184. )
  185. ])
  186. ]);
  187. }
  188. }
  189. class AugmentClasses {
  190. static void handleHome(BuildContext context) {
  191. Navigator.of(context).popUntil((route) => route.isFirst);
  192. }
  193. static void handleReload() {
  194. print("reload");
  195. }
  196. static void handleImages() {
  197. print("Images button pressed");
  198. }
  199. static void handleOpen() {
  200. print("Open button pressed");
  201. }
  202. static void handleFind() {
  203. print("Find button pressed");
  204. }
  205. static void handleStop() {
  206. print("Stop button pressed");
  207. }
  208. static void handleJump(String viewTypeId, String spanId) {
  209. print(spanId);
  210. // final jsCode = '''
  211. // var iframe = document.getElementById("iframe#$viewTypeId").contentWindow;
  212. // var element = iframe.document.getElementById('$spanId');
  213. // if (element) {
  214. // element.scrollIntoView({behavior: "smooth", block: "start"});
  215. // }
  216. // ''';
  217. // final jsCode = '''
  218. // var iframe = document.getElementById("$viewTypeId");
  219. // if (iframe) {
  220. // var iframeWindow = iframe.contentWindow;
  221. // if (iframeWindow && iframeWindow.document) {
  222. // var element = iframeWindow.document.getElementById('$spanId');
  223. // if (element) {
  224. // element.scrollIntoView({behavior: "smooth", block: "start"});
  225. // console.log("Scrolling to element with ID: $spanId");
  226. // } else {
  227. // console.error("Element with ID $spanId not found in iframe.");
  228. // }
  229. // } else {
  230. // console.error("Iframe contentWindow or document is not accessible.");
  231. // }
  232. // } else {
  233. // console.error("Iframe with ID $viewTypeId not found.");
  234. // }
  235. // ''';
  236. // print('jumped'); // For debugging in Dart
  237. // // Execute the JavaScript code
  238. // js.context.callMethod('eval', [jsCode]);
  239. // Select the first iframe on the page
  240. String js_code = '''
  241. var iframe = document.getElementsByTagName('iframe')[0]; // 0 for the first iframe, 1 for the second, etc.
  242. // Check if the iframe is loaded and has content
  243. if (iframe && iframe.contentDocument) {
  244. // Access the document inside the iframe
  245. var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
  246. // Find the element with the specific id inside the iframe
  247. var targetElement = iframeDoc.getElementById("$spanId"); // Replace '36 ' with the actual id of the target element
  248. // If the element exists, scroll to it
  249. if (targetElement) {
  250. targetElement.scrollIntoView();
  251. console.log('Scrolled to element with id "$spanId" inside the iframe.');
  252. } else {
  253. console.log('Element with id "$spanId" not found inside the iframe.');
  254. }
  255. } else {
  256. console.log('Iframe not found or not loaded.');
  257. }
  258. ''';
  259. js.context.callMethod('eval', [js_code]);
  260. }
  261. }