"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RenderWrapper = exports.DATA_SET_COMMAND = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importStar(require("react"));
require("./styles.less");
exports.DATA_SET_COMMAND = 'data-command';
const RenderWrapper = (props) => {
    const ref = (0, react_1.useRef)();
    const { html, opener } = props;
    (0, react_1.useEffect)(() => {
        if (ref && ref.current) {
            ref.current.addEventListener('click', listenClick);
        }
        return () => {
            if (ref && ref.current) {
                ref.current.removeEventListener('click', listenClick);
            }
        };
    }, []);
    /**
     * 拦截 a 标签的点击事件，触发 commands
     */
    const listenClick = (event) => {
        let target = event.target;
        if (target.className.includes('codicon')) {
            target = target.parentNode;
        }
        if (target.tagName.toLowerCase() === 'a' && target.hasAttribute(exports.DATA_SET_COMMAND)) {
            const dataCommand = target.getAttribute(exports.DATA_SET_COMMAND);
            if (dataCommand && opener) {
                opener.open(dataCommand);
            }
        }
    };
    return (react_1.default.createElement("div", { className: 'kt-md-renderer-wrap', dangerouslySetInnerHTML: { __html: html }, ref: ref }));
};
exports.RenderWrapper = RenderWrapper;
//# sourceMappingURL=render.js.map