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);
}
}
}