tools.umd.js 4.9 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
(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 = {}));
}(this, (function (exports) { 'use strict';

    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 16
    const ACTION_TYPE_ADD_EVENT = 8;
    const ACTION_TYPE_REMOVE_EVENT = 9;
    const ACTION_TYPE_SET_TEXT = 10;
fxy060608's avatar
fxy060608 已提交
17

fxy060608's avatar
fxy060608 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    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 已提交
40 41
    }
    function decodeActions(actions) {
fxy060608's avatar
fxy060608 已提交
42 43 44 45 46
        const [type, dict] = actions[0];
        if (type !== ACTION_TYPE_DICT) {
            return actions;
        }
        const getDict = createGetDict(dict);
fxy060608's avatar
fxy060608 已提交
47 48
        return actions.map((action) => {
            switch (action[0]) {
fxy060608's avatar
fxy060608 已提交
49 50
                case ACTION_TYPE_DICT:
                    return action;
fxy060608's avatar
fxy060608 已提交
51 52 53 54 55
                case ACTION_TYPE_PAGE_CREATE:
                    return decodePageCreateAction(action);
                case ACTION_TYPE_PAGE_CREATED:
                    return decodePageCreatedAction(action);
                case ACTION_TYPE_CREATE:
fxy060608's avatar
fxy060608 已提交
56
                    return decodeCreateAction(action, getDict);
fxy060608's avatar
fxy060608 已提交
57 58 59 60 61
                case ACTION_TYPE_INSERT:
                    return decodeInsertAction(action);
                case ACTION_TYPE_REMOVE:
                    return decodeRemoveAction(action);
                case ACTION_TYPE_SET_ATTRIBUTE:
fxy060608's avatar
fxy060608 已提交
62
                    return decodeSetAttributeAction(action, getDict);
fxy060608's avatar
fxy060608 已提交
63
                case ACTION_TYPE_REMOVE_ATTRIBUTE:
fxy060608's avatar
fxy060608 已提交
64
                    return decodeRemoveAttributeAction(action, getDict);
fxy060608's avatar
fxy060608 已提交
65
                case ACTION_TYPE_ADD_EVENT:
fxy060608's avatar
fxy060608 已提交
66
                    return decodeAddEventAction(action, getDict);
fxy060608's avatar
fxy060608 已提交
67
                case ACTION_TYPE_REMOVE_EVENT:
fxy060608's avatar
fxy060608 已提交
68
                    return decodeRemoveEventAction(action, getDict);
fxy060608's avatar
fxy060608 已提交
69
                case ACTION_TYPE_SET_TEXT:
fxy060608's avatar
fxy060608 已提交
70
                    return decodeSetTextAction(action, getDict);
fxy060608's avatar
fxy060608 已提交
71 72
            }
        });
fxy060608's avatar
fxy060608 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    }
    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);
        }
        if (nodeJson.s) {
            nodeJson.s = getDict(nodeJson.s);
        }
        if (nodeJson.t) {
            nodeJson.t = getDict(nodeJson.t);
        }
        return nodeJson;
    }
fxy060608's avatar
fxy060608 已提交
98
    function decodeCreateAction([, nodeId, nodeName, parentNodeId, refNodeId, nodeJson], getDict) {
fxy060608's avatar
fxy060608 已提交
99 100 101 102 103
        return [
            'create',
            nodeId,
            getDict(nodeName),
            parentNodeId,
fxy060608's avatar
fxy060608 已提交
104
            refNodeId,
fxy060608's avatar
fxy060608 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
            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]];
    }
    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 已提交
133 134
    }

fxy060608's avatar
fxy060608 已提交
135
    exports.createGetDict = createGetDict;
fxy060608's avatar
fxy060608 已提交
136
    exports.decodeActions = decodeActions;
fxy060608's avatar
fxy060608 已提交
137
    exports.decodeNodeJson = decodeNodeJson;
fxy060608's avatar
fxy060608 已提交
138 139 140 141

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

})));