/**
 * DevExtreme (cjs/ui/html_editor/modules/imageCursor.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.default = void 0;
var _devextremeQuill = _interopRequireDefault(require("devextreme-quill"));
var _base = _interopRequireDefault(require("./base"));
var _events_engine = _interopRequireDefault(require("../../../events/core/events_engine"));
var _index = require("../../../events/utils/index");

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}

function _assertThisInitialized(self) {
    if (void 0 === self) {
        throw new ReferenceError("this hasn't been initialised - super() hasn't been called")
    }
    return self
}

function _inheritsLoose(subClass, superClass) {
    subClass.prototype = Object.create(superClass.prototype);
    subClass.prototype.constructor = subClass;
    _setPrototypeOf(subClass, superClass)
}

function _setPrototypeOf(o, p) {
    _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
        o.__proto__ = p;
        return o
    };
    return _setPrototypeOf(o, p)
}
const MODULE_NAMESPACE = "dxHtmlEditorImageCursor";
const clickEvent = (0, _index.addNamespace)("dxclick", MODULE_NAMESPACE);
let ImageCursorModule = _base.default;
if (_devextremeQuill.default) {
    ImageCursorModule = function(_BaseModule) {
        _inheritsLoose(ImageCursorModule, _BaseModule);

        function ImageCursorModule(quill, options) {
            var _this;
            _this = _BaseModule.call(this, quill, options) || this;
            _this.addCleanCallback(_this.clean.bind(_assertThisInitialized(_this)));
            _this._attachEvents();
            return _this
        }
        var _proto = ImageCursorModule.prototype;
        _proto._attachEvents = function() {
            _events_engine.default.on(this.quill.root, clickEvent, this._clickHandler.bind(this))
        };
        _proto._detachEvents = function() {
            _events_engine.default.off(this.quill.root, clickEvent)
        };
        _proto._clickHandler = function(e) {
            if (this._isAllowedTarget(e.target)) {
                this._adjustSelection(e)
            }
        };
        _proto._isAllowedTarget = function(targetElement) {
            return this._isImage(targetElement)
        };
        _proto._isImage = function(targetElement) {
            return "IMG" === targetElement.tagName.toUpperCase()
        };
        _proto._adjustSelection = function(e) {
            const blot = this.quill.scroll.find(e.target);
            if (blot) {
                const index = blot.offset(this.quill.scroll);
                this.quill.setSelection(index + 1, 0)
            } else {
                this.quill.setSelection(0, 0)
            }
        };
        _proto.clean = function() {
            this._detachEvents()
        };
        return ImageCursorModule
    }(_base.default)
}
var _default = ImageCursorModule;
exports.default = _default;
module.exports = exports.default;
module.exports.default = exports.default;
