<repo_name>Seanxwy/TinyMCE.Blazor<file_sep>/src/TinyMCE.Blazor/Components/Editor.razor.Method.cs
﻿using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System.Threading.Tasks;

namespace TinyMCE.Blazor
{
    public partial class Editor : ComponentBase
    {
        [Inject] private IJSRuntime Js { get; set; }

        public async Task CreateTinyMCE()
        {
            await Js.InvokeVoidAsync("TinyMCEBlazor.createTinyMCE", _ref, DotNetObjectReference.Create(this), this.Id, this.Value, this.TinyMCEOption, this.Width, this.Height);
        }

        public ValueTask<string> GetValue()
        {
            return Js.InvokeAsync<string>("TinyMCEBlazor.getValue", _ref, this.Id);
        }

        public ValueTask<string> GetHTML()
        {
            return Js.InvokeAsync<string>("TinyMCEBlazor.getHTML", _ref, this.Id);
        }

        public async Task SetValue(string value)
        {
            await Js.InvokeVoidAsync("TinyMCEBlazor.setValue", _ref, this.Id, value);
        }

        public async Task InsertValue(string value)
        {
            await Js.InvokeVoidAsync("TinyMCEBlazor.insertValue", _ref, this.Id, value);
        }

        public async Task Destroy()
        {
            await Js.InvokeVoidAsync("TinyMCEBlazor.destroy", _ref);
        }

        public async Task SetHeight(string value, bool stop = false)
        {
            await Js.InvokeVoidAsync("TinyMCEBlazor.setHeight", _ref, value, stop);
        }
    }
}<file_sep>/src/TinyMCE.Blazor/Components/Editor.razor.cs
﻿using Microsoft.AspNetCore.Components;
using System.Threading.Tasks;
using System.Collections.Generic;
using System;
using TinyMCE.Blazor.Models;

namespace TinyMCE.Blazor
{
    public partial class Editor : ComponentBase, IDisposable
    {
        /// <summary>
        /// Markdown Content
        /// </summary>
        [Parameter]
        public string Value
        {
            get => _value;
            set
            {
                if (_value != value)
                {
                    _value = value;
                    _waitingUpdate = true;
                }
            }
        }

        [Parameter] public EventCallback<string> ValueChanged { get; set; }

        /// <summary>
        /// Html Content
        /// </summary>
        [Parameter]
        public string Html { get; set; }

        [Parameter]
        public string Id { get; set; } = null;

        [Parameter] public EventCallback<string> HtmlChanged { get; set; }

        [Parameter]
        public TinyMCEOption TinyMCEOption { get; set; } = new TinyMCEOption();


        [Parameter]
        public string Height {
            get => _heightValue; 
            set {
                SetHeight(value);
                _heightValue = value;
            }
        }

        [Parameter]
        public string Width { get; set; }

        private ElementReference _ref;

        private bool _editorRendered = false;
        private bool _waitingUpdate = false;
        private string _value, _heightValue = "500";

        private bool _afterFirstRender = false;

        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            await base.OnAfterRenderAsync(firstRender);

            if (firstRender)
            {
                //Console.WriteLine("edit is first render");
                await CreateTinyMCE();
            }
            
        }

        protected override async Task OnInitializedAsync()
        {
            //Console.WriteLine("edit is initialized");
            await base.OnInitializedAsync();
            this.Id = Id ?? Guid.NewGuid().ToString();
        }

        protected override async Task OnParametersSetAsync()
        {
            //Console.WriteLine("edit is on parameters");
            await base.OnParametersSetAsync();
            if (_waitingUpdate && _editorRendered)
            {
                _waitingUpdate = false;
                await SetValue(_value);
                //Console.WriteLine("edit is on parameters set value");
            }
        }

        public async ValueTask<string> GetValueAsync()
        {
            if (_editorRendered)
            {
                return await GetValue();
            }

            return string.Empty;
        }

        public void Dispose()
        {
            //Console.WriteLine("edit is dispose");
            Destroy();
        }
    }
}
<file_sep>/README.md
# TinyMCE.Blazor
 
<file_sep>/src/TinyMCE.Blazor/Models/TinyMCEOption.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TinyMCE.Blazor.Models
{
    public class TinyMCEOption
    {
        public bool InlineMode { get; set; }

        public string Plugins { get; set; } = "paste autolink link wordcount";

        public string Toolbar { get; set; } = "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | outdent indent | link";

        public bool ShowMenuBar { get; set; } = false;

        public bool PasteAsText { get; set; }

        public bool PasteDataImage { get; set; }
    }
}
