EditorView.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Bridge.Html5;
  2. using FrontendRouting;
  3. using LearnBridgeNet.Components;
  4. using LearnBridgeNet.Views.Basic;
  5. using System;
  6. using System.Linq;
  7. using System.Reflection;
  8. namespace LearnBridgeNet.Views
  9. {
  10. [FeRoute("/editor.html")]
  11. internal class EditorView : AuthRequiredView
  12. {
  13. private readonly HTMLElement _view;
  14. private readonly HTMLSelectElement _selectTheme;
  15. private readonly HTMLSelectElement _selectMode;
  16. private readonly HTMLInputElement _inputFontSize;
  17. private readonly HTMLLabelElement _labelCharCount;
  18. private HTMLDivElement _editorDom;
  19. private AceEditor _editor;
  20. private readonly Action _updateCharCountDebounced;
  21. public EditorView()
  22. {
  23. //create dom tree
  24. _view = new HTMLDivElement { ClassName = "editor-page" };
  25. _view.AppendChild(new HTMLHeadingElement { TextContent = "Editor Page" });
  26. var selectBar = new HTMLDivElement { ClassName = "select-bar" };
  27. _selectTheme = new HTMLSelectElement();
  28. _selectMode = new HTMLSelectElement();
  29. _inputFontSize = new HTMLInputElement { Type = InputType.Number, Min = "5", Max = "50", Step = "5", Value = "16" };
  30. _labelCharCount = new HTMLLabelElement { TextContent = "0" };
  31. selectBar.AppendChild(new HTMLSpanElement { TextContent = "Theme:" });
  32. selectBar.AppendChild(_selectTheme);
  33. selectBar.AppendChild(new HTMLSpanElement { TextContent = "Mode:" });
  34. selectBar.AppendChild(_selectMode);
  35. selectBar.AppendChild(new HTMLSpanElement { TextContent = "Font Size:" });
  36. selectBar.AppendChild(_inputFontSize);
  37. selectBar.AppendChild(new HTMLSpanElement { TextContent = "Char Count:" });
  38. selectBar.AppendChild(_labelCharCount);
  39. _view.AppendChild(selectBar);
  40. //init data
  41. _selectTheme.SetOptions(typeof(AceEditorTheme).GetFields(BindingFlags.Static | BindingFlags.Public).Select(p => p.GetValue(null).ToString()));
  42. _selectMode.SetOptions(typeof(AceEditorMode).GetFields(BindingFlags.Static | BindingFlags.Public).Select(p => p.GetValue(null).ToString()));
  43. //assoc events
  44. _selectTheme.AddEventListener(EventType.Change, SelectTheme_Changed);
  45. _selectMode.AddEventListener(EventType.Change, SelectMode_Changed);
  46. _inputFontSize.AddEventListener(EventType.Input, InputFontSize_Inputted);
  47. _updateCharCountDebounced = LodashTypeDefine.Debounce<Action>(UpdateCharCount, 100);
  48. }
  49. private void Editor_Change(object sender, EventArgs e)
  50. {
  51. _updateCharCountDebounced();
  52. }
  53. private void UpdateCharCount() => _labelCharCount.TextContent = _editor.Value.Length.ToString("N0");
  54. private void SelectTheme_Changed(Event arg)
  55. {
  56. if (Enum.TryParse(_selectTheme.Value, out AceEditorTheme aet))
  57. _editor.Theme = aet;
  58. }
  59. private void SelectMode_Changed(Event arg)
  60. {
  61. if (Enum.TryParse(_selectMode.Value, out AceEditorMode aem))
  62. _editor.Mode = aem;
  63. }
  64. private void InputFontSize_Inputted(Event arg)
  65. {
  66. if (int.TryParse(_inputFontSize.Value, out var fs))
  67. _editor.FontSize = fs;
  68. }
  69. public override void Enter(Node parent)
  70. {
  71. parent.AppendChild(_view);
  72. if (null != _editor) return;
  73. _editorDom = new HTMLDivElement();
  74. _view.AppendChild(_editorDom);
  75. _editor = new AceEditor(_editorDom);
  76. _editor.AddClass("ace-editor");
  77. _editor.Change += Editor_Change;
  78. _editor.Value = "asdf";
  79. }
  80. public override void Leave()
  81. {
  82. _view.Remove();
  83. }
  84. }
  85. }