tools.umd.js 5.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4
(function (window, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
    typeof define === 'function' && define.amd ? define(['exports'], factory) :
    (global = typeof globalThis !== 'undefined' ? globalThis : window || self, factory(window.Tools = {}));
fxy060608's avatar
fxy060608 已提交
5
})(this, (function (exports) { 'use strict';
fxy060608's avatar
fxy060608 已提交
6 7 8 9 10 11 12 13

    const ACTION_TYPE_PAGE_CREATE = 1;
    const ACTION_TYPE_PAGE_CREATED = 2;
    const ACTION_TYPE_CREATE = 3;
    const ACTION_TYPE_INSERT = 4;
    const ACTION_TYPE_REMOVE = 5;
    const ACTION_TYPE_SET_ATTRIBUTE = 6;
    const ACTION_TYPE_REMOVE_ATTRIBUTE = 7;
fxy060608's avatar
fxy060608 已提交
14 15
    const ACTION_TYPE_ADD_EVENT = 8;
    const ACTION_TYPE_REMOVE_EVENT = 9;
fxy060608's avatar
fxy060608 已提交
16 17
    const ACTION_TYPE_SET_TEXT = 10;
    const ACTION_TYPE_ADD_WXS_EVENT = 12;
fxy060608's avatar
fxy060608 已提交
18

fxy060608's avatar
fxy060608 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    const ACTION_TYPE_DICT = 0;

    function createGetDict(dict) {
        if (!dict.length) {
            return (v) => v;
        }
        const getDict = (value, includeValue = true) => {
            if (typeof value === 'number') {
                return dict[value];
            }
            const res = {};
            value.forEach(([n, v]) => {
                if (includeValue) {
                    res[getDict(n)] = getDict(v);
                }
                else {
                    res[getDict(n)] = v;
                }
            });
            return res;
        };
        return getDict;
fxy060608's avatar
fxy060608 已提交
41 42
    }
    function decodeActions(actions) {
fxy060608's avatar
fxy060608 已提交
43 44 45 46 47
        const [type, dict] = actions[0];
        if (type !== ACTION_TYPE_DICT) {
            return actions;
        }
        const getDict = createGetDict(dict);
fxy060608's avatar
fxy060608 已提交
48 49
        return actions.map((action) => {
            switch (action[0]) {
fxy060608's avatar
fxy060608 已提交
50 51
                case ACTION_TYPE_DICT:
                    return action;
fxy060608's avatar
fxy060608 已提交
52 53 54 55 56
                case ACTION_TYPE_PAGE_CREATE:
                    return decodePageCreateAction(action);
                case ACTION_TYPE_PAGE_CREATED:
                    return decodePageCreatedAction(action);
                case ACTION_TYPE_CREATE:
fxy060608's avatar
fxy060608 已提交
57
                    return decodeCreateAction(action, getDict);
fxy060608's avatar
fxy060608 已提交
58 59 60 61 62
                case ACTION_TYPE_INSERT:
                    return decodeInsertAction(action);
                case ACTION_TYPE_REMOVE:
                    return decodeRemoveAction(action);
                case ACTION_TYPE_SET_ATTRIBUTE:
fxy060608's avatar
fxy060608 已提交
63
                    return decodeSetAttributeAction(action, getDict);
fxy060608's avatar
fxy060608 已提交
64
                case ACTION_TYPE_REMOVE_ATTRIBUTE:
fxy060608's avatar
fxy060608 已提交
65
                    return decodeRemoveAttributeAction(action, getDict);
fxy060608's avatar
fxy060608 已提交
66
                case ACTION_TYPE_ADD_EVENT:
fxy060608's avatar
fxy060608 已提交
67
                    return decodeAddEventAction(action, getDict);
fxy060608's avatar
fxy060608 已提交
68 69
                case ACTION_TYPE_ADD_WXS_EVENT:
                    return decodeAddWxsEventAction(action, getDict);
fxy060608's avatar
fxy060608 已提交
70
                case ACTION_TYPE_REMOVE_EVENT:
fxy060608's avatar
fxy060608 已提交
71
                    return decodeRemoveEventAction(action, getDict);
fxy060608's avatar
fxy060608 已提交
72
                case ACTION_TYPE_SET_TEXT:
fxy060608's avatar
fxy060608 已提交
73
                    return decodeSetTextAction(action, getDict);
fxy060608's avatar
fxy060608 已提交
74 75
            }
        });
fxy060608's avatar
fxy060608 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    }
    function decodePageCreateAction([, pageCreateData]) {
        return ['pageCreate', pageCreateData];
    }
    function decodePageCreatedAction([]) {
        return ['pageCreated'];
    }
    function decodeNodeJson(getDict, nodeJson) {
        if (!nodeJson) {
            return;
        }
        if (nodeJson.a) {
            nodeJson.a = getDict(nodeJson.a);
        }
        if (nodeJson.e) {
            nodeJson.e = getDict(nodeJson.e, false);
        }
fxy060608's avatar
fxy060608 已提交
93 94 95
        if (nodeJson.w) {
            nodeJson.w = getWxsEventDict(nodeJson.w, getDict);
        }
fxy060608's avatar
fxy060608 已提交
96 97 98 99 100 101 102 103
        if (nodeJson.s) {
            nodeJson.s = getDict(nodeJson.s);
        }
        if (nodeJson.t) {
            nodeJson.t = getDict(nodeJson.t);
        }
        return nodeJson;
    }
fxy060608's avatar
fxy060608 已提交
104 105 106 107 108 109 110
    function getWxsEventDict(w, getDict) {
        const res = {};
        w.forEach(([name, [wxsEvent, flag]]) => {
            res[getDict(name)] = [getDict(wxsEvent), flag];
        });
        return res;
    }
fxy060608's avatar
fxy060608 已提交
111
    function decodeCreateAction([, nodeId, nodeName, parentNodeId, refNodeId, nodeJson], getDict) {
fxy060608's avatar
fxy060608 已提交
112 113 114 115 116
        return [
            'create',
            nodeId,
            getDict(nodeName),
            parentNodeId,
fxy060608's avatar
fxy060608 已提交
117
            refNodeId,
fxy060608's avatar
fxy060608 已提交
118 119 120 121 122 123 124 125 126 127 128 129
            decodeNodeJson(getDict, nodeJson),
        ];
    }
    function decodeInsertAction([, ...action]) {
        return ['insert', ...action];
    }
    function decodeRemoveAction([, ...action]) {
        return ['remove', ...action];
    }
    function decodeAddEventAction([, ...action], getDict) {
        return ['addEvent', action[0], getDict(action[1]), action[2]];
    }
fxy060608's avatar
fxy060608 已提交
130 131 132 133 134 135 136 137 138
    function decodeAddWxsEventAction([, ...action], getDict) {
        return [
            'addWxsEvent',
            action[0],
            getDict(action[1]),
            getDict(action[2]),
            action[3],
        ];
    }
fxy060608's avatar
fxy060608 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    function decodeRemoveEventAction([, ...action], getDict) {
        return ['removeEvent', action[0], getDict(action[1])];
    }
    function decodeSetAttributeAction([, ...action], getDict) {
        return [
            'setAttr',
            action[0],
            getDict(action[1]),
            getDict(action[2]),
        ];
    }
    function decodeRemoveAttributeAction([, ...action], getDict) {
        return ['removeAttr', action[0], getDict(action[1])];
    }
    function decodeSetTextAction([, ...action], getDict) {
        return ['setText', action[0], getDict(action[1])];
fxy060608's avatar
fxy060608 已提交
155 156
    }

fxy060608's avatar
fxy060608 已提交
157
    exports.createGetDict = createGetDict;
fxy060608's avatar
fxy060608 已提交
158
    exports.decodeActions = decodeActions;
fxy060608's avatar
fxy060608 已提交
159
    exports.decodeNodeJson = decodeNodeJson;
fxy060608's avatar
fxy060608 已提交
160 161 162

    Object.defineProperty(exports, '__esModule', { value: true });

fxy060608's avatar
fxy060608 已提交
163
}));