augment.dart 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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: 50,
  120. height: 30,
  121. child: TextField(
  122. // controller: _jumpController,
  123. decoration: InputDecoration(
  124. border: OutlineInputBorder(),
  125. // suffixIcon: Icon(Icons.search)
  126. ),
  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. ElevatedButton(
  163. onPressed: () => AugmentClasses.ViewSpecsButton(context),
  164. child: Text('ViewSpecs:')),
  165. Container(
  166. width: 50,
  167. height: 30,
  168. child: TextField(
  169. decoration: InputDecoration(
  170. labelText: '',
  171. border: OutlineInputBorder(),
  172. // suffixIcon: Icon(Icons.style_rounded)
  173. ),
  174. ),
  175. ),
  176. ElevatedButton(
  177. onPressed: AugmentClasses.handleImages,
  178. child: Text('Filter'),
  179. ),
  180. SizedBox(width: 8),
  181. ElevatedButton(
  182. onPressed: AugmentClasses.handleOpen,
  183. child: Text('Lookup'),
  184. ),
  185. // SizedBox(width: 8),
  186. ElevatedButton(
  187. onPressed: AugmentClasses.handleFind,
  188. child: Text('Create Link'),
  189. ),
  190. ElevatedButton(
  191. onPressed: AugmentClasses.handleFind,
  192. child: Text('Paste Link'),
  193. ),
  194. ],
  195. )
  196. ])
  197. ]);
  198. }
  199. }
  200. class AugmentClasses {
  201. static void handleHome(BuildContext context) {
  202. Navigator.of(context).popUntil((route) => route.isFirst);
  203. }
  204. static void handleReload() {
  205. print("reload");
  206. }
  207. static void handleImages() {
  208. print("Images button pressed");
  209. }
  210. static void handleOpen() {
  211. print("Open button pressed");
  212. }
  213. static void handleFind() {
  214. print("Find button pressed");
  215. }
  216. static void handleStop() {
  217. print("Stop button pressed");
  218. }
  219. static void handleJump(String spanId) {
  220. String js_code = '''
  221. var iframe = document.getElementsByTagName('iframe')[0]; // 0 for the first iframe, 1 for the second, etc.
  222. // Check if the iframe is loaded and has content
  223. if (iframe && iframe.contentDocument) {
  224. // Access the document inside the iframe
  225. var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
  226. // Find the element with the specific id inside the iframe
  227. var targetElement = iframeDoc.getElementById("$spanId"); // Replace '36 ' with the actual id of the target element
  228. // If the element exists, scroll to it
  229. if (targetElement) {
  230. targetElement.scrollIntoView();
  231. console.log('Scrolled to element with id "$spanId" inside the iframe.');
  232. } else {
  233. console.log('Element with id "$spanId" not found inside the iframe.');
  234. }
  235. } else {
  236. console.log('Iframe not found or not loaded.');
  237. }
  238. ''';
  239. js.context.callMethod('eval', [js_code]);
  240. }
  241. static void invisibility(String htmlClass) {}
  242. static Future<void> JumpButton(BuildContext context) async {
  243. // FocusNode textFieldFocusNode = FocusNode();
  244. AugmentClasses.disableIframePointerEvents();
  245. await showDialog(
  246. barrierDismissible: true,
  247. // barrierColor: Colors.yellow,
  248. context: context,
  249. builder: (context) => AlertDialog(
  250. title: Text('Jump Item:'),
  251. content: Container(
  252. width: 200,
  253. height: 120,
  254. child: Column(
  255. mainAxisSize: MainAxisSize.min,
  256. children: [
  257. Text('Jump (to) Item (at)'),
  258. SizedBox(height: 10),
  259. TextField(
  260. autofocus: true,
  261. decoration: InputDecoration(
  262. labelText: '...',
  263. border: OutlineInputBorder(),
  264. suffixIcon: Icon(Icons.search)),
  265. onSubmitted: (value) {
  266. print("onSubmitted: $value");
  267. if (value.isNotEmpty) {
  268. handleJump(value);
  269. Navigator.of(context).pop();
  270. }
  271. },
  272. ),
  273. ],
  274. ),
  275. ),
  276. actions: [
  277. TextButton(
  278. onPressed: () {
  279. Navigator.of(context).pop();
  280. // print('close pressed');
  281. },
  282. child: Text('close'),
  283. ),
  284. ElevatedButton(
  285. onPressed: () => ViewSpecsButton(context),
  286. child: Text('viewspecs'))
  287. ],
  288. ),
  289. ).then((_) {
  290. AugmentClasses.enableIframePointerEvents();
  291. });
  292. }
  293. static Future<void> ViewSpecsButton(context) async {
  294. //TODO: finish it
  295. bool blankLines = false;
  296. bool numbering = false;
  297. bool statementSignatures = false;
  298. AugmentClasses.disableIframePointerEvents();
  299. await showDialog(
  300. context: context,
  301. builder: (context) => Container(
  302. height: 150,
  303. width: 300,
  304. child: AlertDialog(
  305. title: Text('Viewspecs(short)'),
  306. content: Container(
  307. width: 400, // Set the width to simulate the Windows style
  308. child: Column(
  309. mainAxisSize: MainAxisSize.min,
  310. crossAxisAlignment: CrossAxisAlignment.start,
  311. children: [
  312. Row(
  313. children: [
  314. // First section: Checkboxes for "Show"
  315. Expanded(
  316. child: Column(
  317. crossAxisAlignment: CrossAxisAlignment.start,
  318. children: [
  319. Text('Show'),
  320. Row(
  321. children: [
  322. Text("y z"),
  323. Checkbox(
  324. value: blankLines,
  325. onChanged: (bool? value) {
  326. blankLines = value!;
  327. },
  328. ),
  329. Text('Blank lines'),
  330. ],
  331. ),
  332. Row(
  333. children: [
  334. Text('m n'),
  335. Checkbox(
  336. value: numbering,
  337. onChanged: (bool? value) {
  338. numbering = value!;
  339. },
  340. ),
  341. Text('Numbering'),
  342. ],
  343. ),
  344. Row(
  345. children: [
  346. Text('K L'),
  347. Checkbox(
  348. value: statementSignatures,
  349. onChanged: (bool? value) {
  350. statementSignatures = value!;
  351. },
  352. ),
  353. Text('Statement signatures'),
  354. ],
  355. ),
  356. ],
  357. ),
  358. ),
  359. // Second section: Numeric input for Outline, Levels, and Lines
  360. Expanded(
  361. child: Column(
  362. crossAxisAlignment: CrossAxisAlignment.start,
  363. children: [
  364. Text('Outline'),
  365. Row(
  366. children: [
  367. Text('Levels'),
  368. SizedBox(width: 10),
  369. Container(
  370. width: 40,
  371. child: TextField(
  372. decoration: InputDecoration(
  373. isDense: true,
  374. border: OutlineInputBorder(),
  375. ),
  376. keyboardType: TextInputType.number,
  377. ),
  378. ),
  379. ],
  380. ),
  381. Row(
  382. children: [
  383. Text('Lines'),
  384. SizedBox(width: 10),
  385. Container(
  386. width: 40,
  387. child: TextField(
  388. decoration: InputDecoration(
  389. isDense: true,
  390. border: OutlineInputBorder(),
  391. ),
  392. keyboardType: TextInputType.number,
  393. ),
  394. ),
  395. ],
  396. ),
  397. ],
  398. ),
  399. ),
  400. ],
  401. ),
  402. SizedBox(height: 20),
  403. Row(
  404. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  405. children: [
  406. ElevatedButton(onPressed: () {}, child: Text('OK')),
  407. ElevatedButton(
  408. onPressed: () {
  409. Navigator.of(context).pop();
  410. },
  411. child: Text('Cancel')),
  412. ElevatedButton(
  413. onPressed: () {}, child: Text('Reset')),
  414. ElevatedButton(onPressed: () {}, child: Text('Help')),
  415. ],
  416. ),
  417. ],
  418. ),
  419. ),
  420. ),
  421. )).then((_) {
  422. AugmentClasses.enableIframePointerEvents();
  423. });
  424. }
  425. static void disableIframePointerEvents() {
  426. final iframes = html.document.getElementsByTagName('iframe');
  427. for (var iframe in iframes) {
  428. if (iframe is html.Element) {
  429. iframe.style.pointerEvents = 'none'; // Disable pointer events
  430. }
  431. }
  432. }
  433. static void enableIframePointerEvents() {
  434. final iframes = html.document.getElementsByTagName('iframe');
  435. for (var iframe in iframes) {
  436. if (iframe is html.Element) {
  437. iframe.style.pointerEvents = 'auto'; // Re-enable pointer events
  438. }
  439. }
  440. }
  441. }