<template>
    <view :class="'_root ' + (selectable ? '_select' : '')" :style="containerStyle">
        <slot v-if="!nodes[0]" />
        <node id="_root" :childs="nodes" :opts="[lazyLoad, loadingImg, errorImg, showImgMenu, selectable]" @add.stop.prevent="addFun($event, { tagId: '_root' })" />
    </view>
</template>

<script>
'use strict';
import node from './node/node';
function e(t) {
    '@babel/helpers - typeof';

    return (e =
        'function' == typeof Symbol && 'symbol' == typeof Symbol.iterator
            ? function (e) {
                  return typeof e;
              }
            : function (e) {
                  return e && 'function' == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? 'symbol' : typeof e;
              })(t);
}
function t(e, t, o) {
    (t = n(t)) in e
        ? Object.defineProperty(e, t, {
              value: o,
              enumerable: true,
              configurable: true,
              writable: true
          })
        : (e[t] = o);
    return e;
}
function n(t) {
    var n = o(t, 'string');
    return 'symbol' == e(n) ? n : n + '';
}
function o(t, n) {
    if ('object' != e(t) || !t) {
        return t;
    }
    var o = t[Symbol.toPrimitive];
    if (void 0 !== o) {
        var i = o.call(t, n || 'default');
        if ('object' != e(i)) {
            return i;
        }
        throw new TypeError('@@toPrimitive must return a primitive value.');
    }
    return ('string' === n ? String : Number)(t);
}
/*!
 * mp-html v2.5.0
 * https://github.com/jin-yufeng/mp-html
 *
 * Released under the MIT license
 * Author: Jin Yufeng
 */
var i = require('./parser');
var r = [];
export default {
    components: {
        node
    },
    data() {
        return {
            nodes: []
        };
    },
    props: {
        containerStyle: String,
        content: {
            type: String,
            default: ''
        },
        copyLink: {
            type: Boolean,
            default: true
        },
        domain: String,
        errorImg: String,
        lazyLoad: Boolean,
        loadingImg: String,
        pauseVideo: {
            type: Boolean,
            default: true
        },
        previewImg: {
            type: null,
            default: true
        },
        scrollTable: Boolean,
        selectable: null,
        setTitle: {
            type: Boolean,
            default: true
        },
        showImgMenu: {
            type: Boolean,
            default: true
        },
        tagStyle: Object,
        useAnchor: null
    },
    created: function () {
        this.plugins = [];
        for (var e = r.length; e--; ) {
            this.plugins.push(new r[e](this));
        }
    },
    destroyed: function () {
        this.hookFun('onDetached');
    },
    methods: {
        inFun: function (e, t, n) {
            if (e && t && n) {
                this._in = {
                    page: e,
                    selector: t,
                    scrollTop: n
                };
            }
        },
        navigateTo: function (e, n) {
            var that = this;
            return new Promise(function (i, r) {
                if (!that.useAnchor) {
                    return void r(Error('Anchor is disabled'));
                }
                var a = uni
                    .createSelectorQuery()
                    .in(that._in ? that._in.page : that)
                    .select((that._in ? that._in.selector : '._root') + (e ? ''.concat('>>>', '#').concat(e) : ''))
                    .boundingClientRect();
                that._in ? a.select(that._in.selector).scrollOffset().select(that._in.selector).boundingClientRect() : a.selectViewport().scrollOffset();
                a.exec(function (e) {
                    if (!e[0]) {
                        return void r(Error('Label not found'));
                    }
                    var a = e[1].scrollTop + e[0].top - (e[2] ? e[2].top : 0) + (n || parseInt(that.useAnchor) || 0);
                    that._in
                        ? that._in.page.setData(t({}, that._in.scrollTop, a))
                        : uni.pageScrollTo({
                              scrollTop: a,
                              duration: 300
                          });
                    i();
                });
            });
        },
        getText: function (e) {
            var t = '';
            (function e(n) {
                for (var o = 0; o < n.length; o++) {
                    var i = n[o];
                    if ('text' === i.type) t += i.text.replace(/&amp;/g, '&');
                    else if ('br' === i.name) {
                        t += '\n';
                    } else {
                        var r = 'p' === i.name || 'div' === i.name || 'tr' === i.name || 'li' === i.name || ('h' === i.name[0] && i.name[1] > '0' && i.name[1] < '7');
                        if (r && t && '\n' !== t[t.length - 1]) {
                            t += '\n';
                        }
                        if (i.children) {
                            e(i.children);
                        }
                        r && '\n' !== t[t.length - 1] ? (t += '\n') : ('td' !== i.name && 'th' !== i.name) || (t += '\t');
                    }
                }
            })(e || this.nodes);
            return t;
        },
        getRect: function () {
            var that = this;
            return new Promise(function (t, n) {
                uni.createSelectorQuery()
                    .in(that)
                    .select('._root')
                    .boundingClientRect()
                    .exec(function (e) {
                        return e[0] ? t(e[0]) : n(Error('Root label not found'));
                    });
            });
        },
        pauseMedia: function () {
            for (var e = (this._videos || []).length; e--; ) {
                this._videos[e].pause();
            }
        },
        setPlaybackRate: function (e) {
            this.playbackRate = e;
            for (var t = (this._videos || []).length; t--; ) {
                this._videos[t].playbackRate(e);
            }
        },
        setContent: function (e, t) {
            var that = this;
            (this.imgList && t) || (this.imgList = []);
            this._videos = [];
            var o = {};
            var r = new i(this).parse(e);
            if (t) {
                for (var a = this.nodes.length, s = r.length; s--; ) {
                    o['nodes['.concat(a + s, ']')] = r[s];
                }
            } else {
                o.nodes = r;
            }
            this.setData(o, function () {
                that.hookFun('onLoad');
                that.$emit('load');
            });
            if (this.lazyLoad || this.imgList._unloadimgs < this.imgList.length / 2) {
                var l = 0;
                var c = function (e) {
                    (e && e.height) || (e = {});
                    e.height === l
                        ? that.$emit('ready', {
                              detail: e
                          })
                        : ((l = e.height),
                          setTimeout(function () {
                              that.getRect().then(c).catch(c);
                          }, 350));
                };
                this.getRect().then(c).catch(c);
            } else {
                this.imgList._unloadimgs ||
                    this.getRect()
                        .then(function (e) {
                            that.$emit('ready', {
                                detail: e
                            });
                        })
                        .catch(function () {
                            that.$emit('ready', {
                                detail: {}
                            });
                        });
            }
        },
        hookFun: function (e) {
            for (var t = r.length; t--; ) {
                if (this.plugins[t][e]) {
                    this.plugins[t][e]();
                }
            }
        },
        addFun: function (e, _dataset) {
            /* ---处理dataset begin--- */
            this.handleDataset(e, _dataset);
            /* ---处理dataset end--- */
            e.detail.root = this;
        }
    },
    watch: {
        content: {
            handler: function (e) {
                this.setContent(e);
            },

            immediate: true
        }
    }
};
</script>
<style>
._root {
    /* padding: 1px 0;
  overflow-x: auto;
  overflow-y: hidden; */
}

._select {
    -webkit-user-select: text;
    user-select: text;
}
</style>
