/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

import { __extends, __assign } from 'tslib';
import React__default from 'react';
import isEqual from 'lodash/isEqual';
import FroalaEditor from 'froala-editor';
export { default as FroalaEditor } from 'froala-editor';
import Froala from 'froala-editor/js/froala_editor.min.js';
import 'froala-editor/js/plugins/align.min';
import 'froala-editor/js/plugins/colors.min';
import 'froala-editor/js/plugins/char_counter.min';
import 'froala-editor/js/plugins/code_view.min';
import 'froala-editor/js/plugins/draggable.min';
import 'froala-editor/js/plugins/entities.min';
import 'froala-editor/js/plugins/font_family.min';
import 'froala-editor/js/plugins/font_size.min';
import 'froala-editor/js/plugins/forms.min';
import 'froala-editor/js/plugins/fullscreen.min';
import 'froala-editor/js/plugins/help.min';
import 'froala-editor/js/plugins/image.min';
import 'froala-editor/js/plugins/inline_class.min';
import 'froala-editor/js/plugins/inline_style.min';
import 'froala-editor/js/plugins/line_breaker.min';
import 'froala-editor/js/plugins/line_height.min';
import 'froala-editor/js/plugins/link.min';
import 'froala-editor/js/plugins/lists.min';
import 'froala-editor/js/plugins/paragraph_format.min';
import 'froala-editor/js/plugins/paragraph_style.min';
import 'froala-editor/js/plugins/print.min';
import 'froala-editor/js/plugins/quick_insert.min';
import 'froala-editor/js/plugins/quote.min';
import 'froala-editor/js/plugins/save.min';
import 'froala-editor/js/plugins/special_characters.min';
import 'froala-editor/js/plugins/table.min';
import 'froala-editor/js/plugins/url.min';
import 'froala-editor/js/plugins/video.min';
import 'froala-editor/js/plugins/word_paste.min';
import 'froala-editor/js/languages/zh_cn.js';

/**
 * @file RichText
 * @description
 * @author fex
 */
// 代码来源于：https://github.com/froala/react-froala-wysiwyg/blob/master/lib/FroalaEditorFunctionality.jsx
// 改动原因是model 同步有些问题，有时候不更新，所以基于官方代码改造一下。
// 目前发现的问题是，如果 model 数据修改，如果此时 editor 还没有初始化完成则不会同步成功
var FroalaEditorComponent = /** @class */ (function (_super) {
    __extends(FroalaEditorComponent, _super);
    function FroalaEditorComponent(props) {
        var _this = _super.call(this, props) || this;
        _this.listeningEvents = [];
        _this.element = null;
        _this.editor = null;
        _this.config = {
            immediateReactModelUpdate: false,
            reactIgnoreAttrs: null
        };
        _this.editorInitialized = false;
        _this.INNER_HTML_ATTR = 'innerHTML';
        _this.oldModel = null;
        return _this;
    }
    // After first time render.
    FroalaEditorComponent.prototype.componentDidMount = function () {
        this.createEditor();
    };
    FroalaEditorComponent.prototype.componentWillUnmount = function () {
        this.destroyEditor();
    };
    FroalaEditorComponent.prototype.componentDidUpdate = function (prevProps) {
        if (!isEqual(this.props.config, prevProps.config)) {
            this.destroyEditor();
            this.createEditor();
            return;
        }
        if (JSON.stringify(this.oldModel) == JSON.stringify(this.props.model)) {
            return;
        }
        this.setContent();
    };
    // Return cloned object
    FroalaEditorComponent.prototype.clone = function (item) {
        var me = this;
        if (!item) {
            return item;
        } // null, undefined values check
        var types = [Number, String, Boolean], result;
        // normalizing primitives if someone did new String('aaa'), or new Number('444');
        types.forEach(function (type) {
            if (item instanceof type) {
                result = type(item);
            }
        });
        if (typeof result == 'undefined') {
            if (Object.prototype.toString.call(item) === '[object Array]') {
                result = [];
                item.forEach(function (child, index, array) {
                    result[index] = me.clone(child);
                });
            }
            else if (typeof item == 'object') {
                // testing that this is DOM
                if (item.nodeType && typeof item.cloneNode == 'function') {
                    result = item.cloneNode(true);
                }
                else if (!item.prototype) {
                    // check that this is a literal
                    if (item instanceof Date) {
                        result = new Date(item);
                    }
                    else {
                        // it is an object literal
                        result = {};
                        for (var i in item) {
                            result[i] = me.clone(item[i]);
                        }
                    }
                }
                else {
                    {
                        result = item;
                    }
                }
            }
            else {
                result = item;
            }
        }
        return result;
    };
    FroalaEditorComponent.prototype.createEditor = function () {
        var _this = this;
        if (this.editorInitialized) {
            return;
        }
        this.config = this.clone(this.props.config || this.config);
        this.config = __assign({}, this.config);
        this.element = this.el;
        if (this.props.model) {
            this.element.innerHTML = this.props.model;
        }
        this.setContent();
        // Default initialized.
        this.registerEvent('initialized', this.config.events && this.config.events.initialized);
        // Check if events are set.
        if (!this.config.events)
            this.config.events = {};
        this.config.events.initialized = function () {
            _this.editorInitialized = true;
            _this.initListeners();
        };
        this.editor = new FroalaEditor(this.element, this.config);
    };
    FroalaEditorComponent.prototype.setContent = function () {
        var _this = this;
        if (this.props.model || this.props.model == '') {
            this.oldModel = this.props.model;
            if (this.editorInitialized) {
                this.setNormalTagContent();
            }
            else {
                if (!this._initEvents)
                    this._initEvents = [];
                this._initEvents.push(function () { return _this.setNormalTagContent(); });
            }
        }
    };
    FroalaEditorComponent.prototype.setNormalTagContent = function () {
        var self = this;
        self.editor.html && self.editor.html.set(self.props.model || '');
        if (self.editorInitialized && self.editor.undo) {
            //This will reset the undo stack everytime the model changes externally. Can we fix this?
            self.editor.undo.reset();
            self.editor.undo.saveStep();
        }
    };
    FroalaEditorComponent.prototype.destroyEditor = function () {
        if (this.element) {
            this.editor.destroy && this.editor.destroy();
            this.listeningEvents.length = 0;
            this.element = null;
            this.editorInitialized = false;
            this._initEvents = [];
        }
    };
    FroalaEditorComponent.prototype.getEditor = function () {
        if (this.element) {
            return this.editor;
        }
        return null;
    };
    FroalaEditorComponent.prototype.updateModel = function () {
        if (!this.props.onModelChange) {
            return;
        }
        var modelContent = '';
        var returnedHtml = this.editor.html.get();
        if (typeof returnedHtml === 'string') {
            modelContent = returnedHtml;
        }
        if (this.oldModel !== modelContent) {
            this.oldModel = modelContent;
            this.props.onModelChange(modelContent);
        }
    };
    FroalaEditorComponent.prototype.initListeners = function () {
        var self = this;
        // bind contentChange and keyup event to froalaModel
        this.editor.events.on('contentChanged', function () {
            self.updateModel();
        });
        if (this.config.immediateReactModelUpdate) {
            this.editor.events.on('keyup', function () {
                self.updateModel();
            });
        }
        // Call init events.
        if (this._initEvents) {
            for (var i = 0; i < this._initEvents.length; i++) {
                this._initEvents[i].call(this.editor);
            }
        }
    };
    // register event on jquery editor element
    FroalaEditorComponent.prototype.registerEvent = function (eventName, callback) {
        if (!eventName || !callback) {
            return;
        }
        if (eventName == 'initialized') {
            if (!this._initEvents)
                this._initEvents = [];
            this._initEvents.push(callback);
        }
        else {
            if (!this.config.events) {
                this.config.events = {};
            }
            this.config.events[eventName] = callback;
        }
    };
    FroalaEditorComponent.prototype.render = function () {
        var _this = this;
        return (React__default.createElement("textarea", { ref: function (el) { return (_this.el = el); } }, this.props.children));
    };
    return FroalaEditorComponent;
}(React__default.Component));
var default_1 = /** @class */ (function (_super) {
    __extends(default_1, _super);
    function default_1(props) {
        var _this = _super.call(this, props) || this;
        Froala.VIDEO_PROVIDERS = [
            {
                test_regex: /.*/,
                url_regex: '',
                url_text: '',
                html: '<span class="fr-video fr-dvb fr-draggable" contenteditable="false" draggable="true"><video class="fr-draggable" controls="" data-msg="ok" data-status="0" src="{url}" style="width: 600px;"></video></span>'
            }
        ];
        return _this;
    }
    default_1.prototype.render = function () {
        return (React__default.createElement(FroalaEditorComponent, { config: this.props.config, model: this.props.model, onModelChange: this.props.onModelChange }));
    };
    return default_1;
}(React__default.Component));

export { default_1 as default };
