123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- using Bridge.Html5;
- using Retyped;
- using System;
- using Bridge;
- using static Retyped.ace;
- namespace LearnBridgeNet.Components
- {
- public class AceEditor
- {
- private readonly HTMLDivElement _dom;
- private readonly AceAjax.Editor _aceInstance;
- private AceEditorTheme _theme;
- private AceEditorMode _mode;
- private int? _width;
- private int? _height;
- private CssUnit _widthUnit = CssUnit.Pixel;
- private CssUnit _heightUnit = CssUnit.Pixel;
- private int _fontSize;
- private CssUnit _fontSizeUnit;
- [Init(InitPosition.Top)]
- public static void Patch()
- {
- //A pacth of retyped.ace type define
- /*@
- if (window.AceAjax === undefined) window.AceAjax = {};
- if (window.AceAjax.Editor === undefined) window.AceAjax.Editor = function () { };
- */
- }
- public AceEditor(HTMLDivElement dom)
- {
- DebugSupport.Add(this);
- _dom = dom;
- // ReSharper disable once SuspiciousTypeConversion.Global
- _aceInstance = ace2.edit((dom.HTMLDivElement)(object)dom);
- SizetUnit = FontSizeUnit = CssUnit.Pixel;
- Width = 800;
- Height = 600;
- FontSize = 16;
- _aceInstance.resize();
- Theme = AceEditorTheme.Chrome;
- Mode = AceEditorMode.Markdown;
- _aceInstance.on("change", o =>
- {
- OnChange();
- return null;
- });
- }
- public event EventHandler Change;
- public string Value
- {
- get => _aceInstance.getValue();
- set => _aceInstance.setValue(value);
- }
- public void AddClass(string name) => _dom.ClassList.Add(name);
- public void RemoveClass(string name) => _dom.ClassList.Remove(name);
- public AceEditorTheme Theme { set => _aceInstance.setTheme("ace/theme/" + (_theme = value)); get => _theme; }
- public AceEditorMode Mode { set => _aceInstance.session.setMode("ace/mode/" + (_mode = value)); get => _mode; }
- public int? Width
- {
- get => _width;
- set
- {
- if (null == value)
- {
- _width = null;
- _dom.Style.Width = null;
- return;
- }
- _dom.Style.Width = (_width = value).Value + _widthUnit.ToString();
- }
- }
- public CssUnit WidthUnit
- {
- get => _widthUnit;
- set
- {
- _widthUnit = value;
- _dom.Style.Width = null == _width ? null : _width.Value + _widthUnit.ToString();
- }
- }
- public int? Height
- {
- get => _height;
- set
- {
- if (null == value)
- {
- _width = null;
- _dom.Style.Height = null;
- return;
- }
- _dom.Style.Height = (_height = value).Value + _heightUnit.ToString();
- }
- }
- public CssUnit HeightUnit
- {
- get => _heightUnit;
- set
- {
- _heightUnit = value;
- _dom.Style.Width = null == _height ? null : _height.Value + _heightUnit.ToString();
- }
- }
- public CssUnit? SizetUnit
- {
- get => _widthUnit == _heightUnit ? (CssUnit?)_widthUnit : null;
- set
- {
- if (null == value) return;
- _heightUnit = value.Value;
- _widthUnit = value.Value;
- _dom.Style.Width = null == _width ? null : _width.Value + _widthUnit.ToString();
- _dom.Style.Width = null == _height ? null : _height.Value + _heightUnit.ToString();
- }
- }
- public int FontSize { set => _aceInstance.setFontSize((_fontSize = value) + _fontSizeUnit.ToString()); get => _fontSize; }
- public CssUnit FontSizeUnit { set => _aceInstance.setFontSize(_fontSize + (_fontSizeUnit = value).ToString()); get => _fontSizeUnit; }
- protected virtual void OnChange()
- {
- Change?.Invoke(this, EventArgs.Empty);
- }
- }
- }
|