@hailin-zheng/editor-core 1.1.4 → 1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -2,6 +2,7 @@ import { nanoid } from 'nanoid';
2
2
  import moment from 'moment';
3
3
  import * as acor from 'acorn';
4
4
  import { generate } from 'astring';
5
+ import * as estraverse from 'estraverse';
5
6
  import bwipjs from 'bwip-js';
6
7
 
7
8
  /**
@@ -2680,814 +2681,9 @@ class DataDecorateRenderObject extends LeafRenderObject {
2680
2681
  }
2681
2682
  }
2682
2683
 
2683
- /*
2684
- Copyright (C) 2012-2013 Yusuke Suzuki <utatane.tea@gmail.com>
2685
- Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
2686
-
2687
- Redistribution and use in source and binary forms, with or without
2688
- modification, are permitted provided that the following conditions are met:
2689
-
2690
- * Redistributions of source code must retain the above copyright
2691
- notice, this list of conditions and the following disclaimer.
2692
- * Redistributions in binary form must reproduce the above copyright
2693
- notice, this list of conditions and the following disclaimer in the
2694
- documentation and/or other materials provided with the distribution.
2695
-
2696
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2697
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2698
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2699
- ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
2700
- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2701
- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2702
- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2703
- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2704
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2705
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2706
- */
2707
- /*jslint vars:false, bitwise:true*/
2708
- /*jshint indent:4*/
2709
- /*global exports:true*/
2710
- (function clone(exports) {
2711
-
2712
- var Syntax,
2713
- VisitorOption,
2714
- VisitorKeys,
2715
- BREAK,
2716
- SKIP,
2717
- REMOVE;
2718
-
2719
- function deepCopy(obj) {
2720
- var ret = {}, key, val;
2721
- for (key in obj) {
2722
- if (obj.hasOwnProperty(key)) {
2723
- val = obj[key];
2724
- if (typeof val === 'object' && val !== null) {
2725
- ret[key] = deepCopy(val);
2726
- } else {
2727
- ret[key] = val;
2728
- }
2729
- }
2730
- }
2731
- return ret;
2732
- }
2733
-
2734
- // based on LLVM libc++ upper_bound / lower_bound
2735
- // MIT License
2736
-
2737
- function upperBound(array, func) {
2738
- var diff, len, i, current;
2739
-
2740
- len = array.length;
2741
- i = 0;
2742
-
2743
- while (len) {
2744
- diff = len >>> 1;
2745
- current = i + diff;
2746
- if (func(array[current])) {
2747
- len = diff;
2748
- } else {
2749
- i = current + 1;
2750
- len -= diff + 1;
2751
- }
2752
- }
2753
- return i;
2754
- }
2755
-
2756
- Syntax = {
2757
- AssignmentExpression: 'AssignmentExpression',
2758
- AssignmentPattern: 'AssignmentPattern',
2759
- ArrayExpression: 'ArrayExpression',
2760
- ArrayPattern: 'ArrayPattern',
2761
- ArrowFunctionExpression: 'ArrowFunctionExpression',
2762
- AwaitExpression: 'AwaitExpression', // CAUTION: It's deferred to ES7.
2763
- BlockStatement: 'BlockStatement',
2764
- BinaryExpression: 'BinaryExpression',
2765
- BreakStatement: 'BreakStatement',
2766
- CallExpression: 'CallExpression',
2767
- CatchClause: 'CatchClause',
2768
- ChainExpression: 'ChainExpression',
2769
- ClassBody: 'ClassBody',
2770
- ClassDeclaration: 'ClassDeclaration',
2771
- ClassExpression: 'ClassExpression',
2772
- ComprehensionBlock: 'ComprehensionBlock', // CAUTION: It's deferred to ES7.
2773
- ComprehensionExpression: 'ComprehensionExpression', // CAUTION: It's deferred to ES7.
2774
- ConditionalExpression: 'ConditionalExpression',
2775
- ContinueStatement: 'ContinueStatement',
2776
- DebuggerStatement: 'DebuggerStatement',
2777
- DirectiveStatement: 'DirectiveStatement',
2778
- DoWhileStatement: 'DoWhileStatement',
2779
- EmptyStatement: 'EmptyStatement',
2780
- ExportAllDeclaration: 'ExportAllDeclaration',
2781
- ExportDefaultDeclaration: 'ExportDefaultDeclaration',
2782
- ExportNamedDeclaration: 'ExportNamedDeclaration',
2783
- ExportSpecifier: 'ExportSpecifier',
2784
- ExpressionStatement: 'ExpressionStatement',
2785
- ForStatement: 'ForStatement',
2786
- ForInStatement: 'ForInStatement',
2787
- ForOfStatement: 'ForOfStatement',
2788
- FunctionDeclaration: 'FunctionDeclaration',
2789
- FunctionExpression: 'FunctionExpression',
2790
- GeneratorExpression: 'GeneratorExpression', // CAUTION: It's deferred to ES7.
2791
- Identifier: 'Identifier',
2792
- IfStatement: 'IfStatement',
2793
- ImportExpression: 'ImportExpression',
2794
- ImportDeclaration: 'ImportDeclaration',
2795
- ImportDefaultSpecifier: 'ImportDefaultSpecifier',
2796
- ImportNamespaceSpecifier: 'ImportNamespaceSpecifier',
2797
- ImportSpecifier: 'ImportSpecifier',
2798
- Literal: 'Literal',
2799
- LabeledStatement: 'LabeledStatement',
2800
- LogicalExpression: 'LogicalExpression',
2801
- MemberExpression: 'MemberExpression',
2802
- MetaProperty: 'MetaProperty',
2803
- MethodDefinition: 'MethodDefinition',
2804
- ModuleSpecifier: 'ModuleSpecifier',
2805
- NewExpression: 'NewExpression',
2806
- ObjectExpression: 'ObjectExpression',
2807
- ObjectPattern: 'ObjectPattern',
2808
- PrivateIdentifier: 'PrivateIdentifier',
2809
- Program: 'Program',
2810
- Property: 'Property',
2811
- PropertyDefinition: 'PropertyDefinition',
2812
- RestElement: 'RestElement',
2813
- ReturnStatement: 'ReturnStatement',
2814
- SequenceExpression: 'SequenceExpression',
2815
- SpreadElement: 'SpreadElement',
2816
- Super: 'Super',
2817
- SwitchStatement: 'SwitchStatement',
2818
- SwitchCase: 'SwitchCase',
2819
- TaggedTemplateExpression: 'TaggedTemplateExpression',
2820
- TemplateElement: 'TemplateElement',
2821
- TemplateLiteral: 'TemplateLiteral',
2822
- ThisExpression: 'ThisExpression',
2823
- ThrowStatement: 'ThrowStatement',
2824
- TryStatement: 'TryStatement',
2825
- UnaryExpression: 'UnaryExpression',
2826
- UpdateExpression: 'UpdateExpression',
2827
- VariableDeclaration: 'VariableDeclaration',
2828
- VariableDeclarator: 'VariableDeclarator',
2829
- WhileStatement: 'WhileStatement',
2830
- WithStatement: 'WithStatement',
2831
- YieldExpression: 'YieldExpression'
2832
- };
2833
-
2834
- VisitorKeys = {
2835
- AssignmentExpression: ['left', 'right'],
2836
- AssignmentPattern: ['left', 'right'],
2837
- ArrayExpression: ['elements'],
2838
- ArrayPattern: ['elements'],
2839
- ArrowFunctionExpression: ['params', 'body'],
2840
- AwaitExpression: ['argument'], // CAUTION: It's deferred to ES7.
2841
- BlockStatement: ['body'],
2842
- BinaryExpression: ['left', 'right'],
2843
- BreakStatement: ['label'],
2844
- CallExpression: ['callee', 'arguments'],
2845
- CatchClause: ['param', 'body'],
2846
- ChainExpression: ['expression'],
2847
- ClassBody: ['body'],
2848
- ClassDeclaration: ['id', 'superClass', 'body'],
2849
- ClassExpression: ['id', 'superClass', 'body'],
2850
- ComprehensionBlock: ['left', 'right'], // CAUTION: It's deferred to ES7.
2851
- ComprehensionExpression: ['blocks', 'filter', 'body'], // CAUTION: It's deferred to ES7.
2852
- ConditionalExpression: ['test', 'consequent', 'alternate'],
2853
- ContinueStatement: ['label'],
2854
- DebuggerStatement: [],
2855
- DirectiveStatement: [],
2856
- DoWhileStatement: ['body', 'test'],
2857
- EmptyStatement: [],
2858
- ExportAllDeclaration: ['source'],
2859
- ExportDefaultDeclaration: ['declaration'],
2860
- ExportNamedDeclaration: ['declaration', 'specifiers', 'source'],
2861
- ExportSpecifier: ['exported', 'local'],
2862
- ExpressionStatement: ['expression'],
2863
- ForStatement: ['init', 'test', 'update', 'body'],
2864
- ForInStatement: ['left', 'right', 'body'],
2865
- ForOfStatement: ['left', 'right', 'body'],
2866
- FunctionDeclaration: ['id', 'params', 'body'],
2867
- FunctionExpression: ['id', 'params', 'body'],
2868
- GeneratorExpression: ['blocks', 'filter', 'body'], // CAUTION: It's deferred to ES7.
2869
- Identifier: [],
2870
- IfStatement: ['test', 'consequent', 'alternate'],
2871
- ImportExpression: ['source'],
2872
- ImportDeclaration: ['specifiers', 'source'],
2873
- ImportDefaultSpecifier: ['local'],
2874
- ImportNamespaceSpecifier: ['local'],
2875
- ImportSpecifier: ['imported', 'local'],
2876
- Literal: [],
2877
- LabeledStatement: ['label', 'body'],
2878
- LogicalExpression: ['left', 'right'],
2879
- MemberExpression: ['object', 'property'],
2880
- MetaProperty: ['meta', 'property'],
2881
- MethodDefinition: ['key', 'value'],
2882
- ModuleSpecifier: [],
2883
- NewExpression: ['callee', 'arguments'],
2884
- ObjectExpression: ['properties'],
2885
- ObjectPattern: ['properties'],
2886
- PrivateIdentifier: [],
2887
- Program: ['body'],
2888
- Property: ['key', 'value'],
2889
- PropertyDefinition: ['key', 'value'],
2890
- RestElement: [ 'argument' ],
2891
- ReturnStatement: ['argument'],
2892
- SequenceExpression: ['expressions'],
2893
- SpreadElement: ['argument'],
2894
- Super: [],
2895
- SwitchStatement: ['discriminant', 'cases'],
2896
- SwitchCase: ['test', 'consequent'],
2897
- TaggedTemplateExpression: ['tag', 'quasi'],
2898
- TemplateElement: [],
2899
- TemplateLiteral: ['quasis', 'expressions'],
2900
- ThisExpression: [],
2901
- ThrowStatement: ['argument'],
2902
- TryStatement: ['block', 'handler', 'finalizer'],
2903
- UnaryExpression: ['argument'],
2904
- UpdateExpression: ['argument'],
2905
- VariableDeclaration: ['declarations'],
2906
- VariableDeclarator: ['id', 'init'],
2907
- WhileStatement: ['test', 'body'],
2908
- WithStatement: ['object', 'body'],
2909
- YieldExpression: ['argument']
2910
- };
2911
-
2912
- // unique id
2913
- BREAK = {};
2914
- SKIP = {};
2915
- REMOVE = {};
2916
-
2917
- VisitorOption = {
2918
- Break: BREAK,
2919
- Skip: SKIP,
2920
- Remove: REMOVE
2921
- };
2922
-
2923
- function Reference(parent, key) {
2924
- this.parent = parent;
2925
- this.key = key;
2926
- }
2927
-
2928
- Reference.prototype.replace = function replace(node) {
2929
- this.parent[this.key] = node;
2930
- };
2931
-
2932
- Reference.prototype.remove = function remove() {
2933
- if (Array.isArray(this.parent)) {
2934
- this.parent.splice(this.key, 1);
2935
- return true;
2936
- } else {
2937
- this.replace(null);
2938
- return false;
2939
- }
2940
- };
2941
-
2942
- function Element(node, path, wrap, ref) {
2943
- this.node = node;
2944
- this.path = path;
2945
- this.wrap = wrap;
2946
- this.ref = ref;
2947
- }
2948
-
2949
- function Controller() { }
2950
-
2951
- // API:
2952
- // return property path array from root to current node
2953
- Controller.prototype.path = function path() {
2954
- var i, iz, j, jz, result, element;
2955
-
2956
- function addToPath(result, path) {
2957
- if (Array.isArray(path)) {
2958
- for (j = 0, jz = path.length; j < jz; ++j) {
2959
- result.push(path[j]);
2960
- }
2961
- } else {
2962
- result.push(path);
2963
- }
2964
- }
2965
-
2966
- // root node
2967
- if (!this.__current.path) {
2968
- return null;
2969
- }
2970
-
2971
- // first node is sentinel, second node is root element
2972
- result = [];
2973
- for (i = 2, iz = this.__leavelist.length; i < iz; ++i) {
2974
- element = this.__leavelist[i];
2975
- addToPath(result, element.path);
2976
- }
2977
- addToPath(result, this.__current.path);
2978
- return result;
2979
- };
2980
-
2981
- // API:
2982
- // return type of current node
2983
- Controller.prototype.type = function () {
2984
- var node = this.current();
2985
- return node.type || this.__current.wrap;
2986
- };
2987
-
2988
- // API:
2989
- // return array of parent elements
2990
- Controller.prototype.parents = function parents() {
2991
- var i, iz, result;
2992
-
2993
- // first node is sentinel
2994
- result = [];
2995
- for (i = 1, iz = this.__leavelist.length; i < iz; ++i) {
2996
- result.push(this.__leavelist[i].node);
2997
- }
2998
-
2999
- return result;
3000
- };
3001
-
3002
- // API:
3003
- // return current node
3004
- Controller.prototype.current = function current() {
3005
- return this.__current.node;
3006
- };
3007
-
3008
- Controller.prototype.__execute = function __execute(callback, element) {
3009
- var previous, result;
3010
-
3011
- result = undefined;
3012
-
3013
- previous = this.__current;
3014
- this.__current = element;
3015
- this.__state = null;
3016
- if (callback) {
3017
- result = callback.call(this, element.node, this.__leavelist[this.__leavelist.length - 1].node);
3018
- }
3019
- this.__current = previous;
3020
-
3021
- return result;
3022
- };
3023
-
3024
- // API:
3025
- // notify control skip / break
3026
- Controller.prototype.notify = function notify(flag) {
3027
- this.__state = flag;
3028
- };
3029
-
3030
- // API:
3031
- // skip child nodes of current node
3032
- Controller.prototype.skip = function () {
3033
- this.notify(SKIP);
3034
- };
3035
-
3036
- // API:
3037
- // break traversals
3038
- Controller.prototype['break'] = function () {
3039
- this.notify(BREAK);
3040
- };
3041
-
3042
- // API:
3043
- // remove node
3044
- Controller.prototype.remove = function () {
3045
- this.notify(REMOVE);
3046
- };
3047
-
3048
- Controller.prototype.__initialize = function(root, visitor) {
3049
- this.visitor = visitor;
3050
- this.root = root;
3051
- this.__worklist = [];
3052
- this.__leavelist = [];
3053
- this.__current = null;
3054
- this.__state = null;
3055
- this.__fallback = null;
3056
- if (visitor.fallback === 'iteration') {
3057
- this.__fallback = Object.keys;
3058
- } else if (typeof visitor.fallback === 'function') {
3059
- this.__fallback = visitor.fallback;
3060
- }
3061
-
3062
- this.__keys = VisitorKeys;
3063
- if (visitor.keys) {
3064
- this.__keys = Object.assign(Object.create(this.__keys), visitor.keys);
3065
- }
3066
- };
3067
-
3068
- function isNode(node) {
3069
- if (node == null) {
3070
- return false;
3071
- }
3072
- return typeof node === 'object' && typeof node.type === 'string';
3073
- }
3074
-
3075
- function isProperty(nodeType, key) {
3076
- return (nodeType === Syntax.ObjectExpression || nodeType === Syntax.ObjectPattern) && 'properties' === key;
3077
- }
3078
-
3079
- function candidateExistsInLeaveList(leavelist, candidate) {
3080
- for (var i = leavelist.length - 1; i >= 0; --i) {
3081
- if (leavelist[i].node === candidate) {
3082
- return true;
3083
- }
3084
- }
3085
- return false;
3086
- }
3087
-
3088
- Controller.prototype.traverse = function traverse(root, visitor) {
3089
- var worklist,
3090
- leavelist,
3091
- element,
3092
- node,
3093
- nodeType,
3094
- ret,
3095
- key,
3096
- current,
3097
- current2,
3098
- candidates,
3099
- candidate,
3100
- sentinel;
3101
-
3102
- this.__initialize(root, visitor);
3103
-
3104
- sentinel = {};
3105
-
3106
- // reference
3107
- worklist = this.__worklist;
3108
- leavelist = this.__leavelist;
3109
-
3110
- // initialize
3111
- worklist.push(new Element(root, null, null, null));
3112
- leavelist.push(new Element(null, null, null, null));
3113
-
3114
- while (worklist.length) {
3115
- element = worklist.pop();
3116
-
3117
- if (element === sentinel) {
3118
- element = leavelist.pop();
3119
-
3120
- ret = this.__execute(visitor.leave, element);
3121
-
3122
- if (this.__state === BREAK || ret === BREAK) {
3123
- return;
3124
- }
3125
- continue;
3126
- }
3127
-
3128
- if (element.node) {
3129
-
3130
- ret = this.__execute(visitor.enter, element);
3131
-
3132
- if (this.__state === BREAK || ret === BREAK) {
3133
- return;
3134
- }
3135
-
3136
- worklist.push(sentinel);
3137
- leavelist.push(element);
3138
-
3139
- if (this.__state === SKIP || ret === SKIP) {
3140
- continue;
3141
- }
3142
-
3143
- node = element.node;
3144
- nodeType = node.type || element.wrap;
3145
- candidates = this.__keys[nodeType];
3146
- if (!candidates) {
3147
- if (this.__fallback) {
3148
- candidates = this.__fallback(node);
3149
- } else {
3150
- throw new Error('Unknown node type ' + nodeType + '.');
3151
- }
3152
- }
3153
-
3154
- current = candidates.length;
3155
- while ((current -= 1) >= 0) {
3156
- key = candidates[current];
3157
- candidate = node[key];
3158
- if (!candidate) {
3159
- continue;
3160
- }
3161
-
3162
- if (Array.isArray(candidate)) {
3163
- current2 = candidate.length;
3164
- while ((current2 -= 1) >= 0) {
3165
- if (!candidate[current2]) {
3166
- continue;
3167
- }
3168
-
3169
- if (candidateExistsInLeaveList(leavelist, candidate[current2])) {
3170
- continue;
3171
- }
3172
-
3173
- if (isProperty(nodeType, candidates[current])) {
3174
- element = new Element(candidate[current2], [key, current2], 'Property', null);
3175
- } else if (isNode(candidate[current2])) {
3176
- element = new Element(candidate[current2], [key, current2], null, null);
3177
- } else {
3178
- continue;
3179
- }
3180
- worklist.push(element);
3181
- }
3182
- } else if (isNode(candidate)) {
3183
- if (candidateExistsInLeaveList(leavelist, candidate)) {
3184
- continue;
3185
- }
3186
-
3187
- worklist.push(new Element(candidate, key, null, null));
3188
- }
3189
- }
3190
- }
3191
- }
3192
- };
3193
-
3194
- Controller.prototype.replace = function replace(root, visitor) {
3195
- var worklist,
3196
- leavelist,
3197
- node,
3198
- nodeType,
3199
- target,
3200
- element,
3201
- current,
3202
- current2,
3203
- candidates,
3204
- candidate,
3205
- sentinel,
3206
- outer,
3207
- key;
3208
-
3209
- function removeElem(element) {
3210
- var i,
3211
- key,
3212
- nextElem,
3213
- parent;
3214
-
3215
- if (element.ref.remove()) {
3216
- // When the reference is an element of an array.
3217
- key = element.ref.key;
3218
- parent = element.ref.parent;
3219
-
3220
- // If removed from array, then decrease following items' keys.
3221
- i = worklist.length;
3222
- while (i--) {
3223
- nextElem = worklist[i];
3224
- if (nextElem.ref && nextElem.ref.parent === parent) {
3225
- if (nextElem.ref.key < key) {
3226
- break;
3227
- }
3228
- --nextElem.ref.key;
3229
- }
3230
- }
3231
- }
3232
- }
3233
-
3234
- this.__initialize(root, visitor);
3235
-
3236
- sentinel = {};
3237
-
3238
- // reference
3239
- worklist = this.__worklist;
3240
- leavelist = this.__leavelist;
3241
-
3242
- // initialize
3243
- outer = {
3244
- root: root
3245
- };
3246
- element = new Element(root, null, null, new Reference(outer, 'root'));
3247
- worklist.push(element);
3248
- leavelist.push(element);
3249
-
3250
- while (worklist.length) {
3251
- element = worklist.pop();
3252
-
3253
- if (element === sentinel) {
3254
- element = leavelist.pop();
3255
-
3256
- target = this.__execute(visitor.leave, element);
3257
-
3258
- // node may be replaced with null,
3259
- // so distinguish between undefined and null in this place
3260
- if (target !== undefined && target !== BREAK && target !== SKIP && target !== REMOVE) {
3261
- // replace
3262
- element.ref.replace(target);
3263
- }
3264
-
3265
- if (this.__state === REMOVE || target === REMOVE) {
3266
- removeElem(element);
3267
- }
3268
-
3269
- if (this.__state === BREAK || target === BREAK) {
3270
- return outer.root;
3271
- }
3272
- continue;
3273
- }
3274
-
3275
- target = this.__execute(visitor.enter, element);
3276
-
3277
- // node may be replaced with null,
3278
- // so distinguish between undefined and null in this place
3279
- if (target !== undefined && target !== BREAK && target !== SKIP && target !== REMOVE) {
3280
- // replace
3281
- element.ref.replace(target);
3282
- element.node = target;
3283
- }
3284
-
3285
- if (this.__state === REMOVE || target === REMOVE) {
3286
- removeElem(element);
3287
- element.node = null;
3288
- }
3289
-
3290
- if (this.__state === BREAK || target === BREAK) {
3291
- return outer.root;
3292
- }
3293
-
3294
- // node may be null
3295
- node = element.node;
3296
- if (!node) {
3297
- continue;
3298
- }
3299
-
3300
- worklist.push(sentinel);
3301
- leavelist.push(element);
3302
-
3303
- if (this.__state === SKIP || target === SKIP) {
3304
- continue;
3305
- }
3306
-
3307
- nodeType = node.type || element.wrap;
3308
- candidates = this.__keys[nodeType];
3309
- if (!candidates) {
3310
- if (this.__fallback) {
3311
- candidates = this.__fallback(node);
3312
- } else {
3313
- throw new Error('Unknown node type ' + nodeType + '.');
3314
- }
3315
- }
3316
-
3317
- current = candidates.length;
3318
- while ((current -= 1) >= 0) {
3319
- key = candidates[current];
3320
- candidate = node[key];
3321
- if (!candidate) {
3322
- continue;
3323
- }
3324
-
3325
- if (Array.isArray(candidate)) {
3326
- current2 = candidate.length;
3327
- while ((current2 -= 1) >= 0) {
3328
- if (!candidate[current2]) {
3329
- continue;
3330
- }
3331
- if (isProperty(nodeType, candidates[current])) {
3332
- element = new Element(candidate[current2], [key, current2], 'Property', new Reference(candidate, current2));
3333
- } else if (isNode(candidate[current2])) {
3334
- element = new Element(candidate[current2], [key, current2], null, new Reference(candidate, current2));
3335
- } else {
3336
- continue;
3337
- }
3338
- worklist.push(element);
3339
- }
3340
- } else if (isNode(candidate)) {
3341
- worklist.push(new Element(candidate, key, null, new Reference(node, key)));
3342
- }
3343
- }
3344
- }
3345
-
3346
- return outer.root;
3347
- };
3348
-
3349
- function traverse(root, visitor) {
3350
- var controller = new Controller();
3351
- return controller.traverse(root, visitor);
3352
- }
3353
-
3354
- function replace(root, visitor) {
3355
- var controller = new Controller();
3356
- return controller.replace(root, visitor);
3357
- }
3358
-
3359
- function extendCommentRange(comment, tokens) {
3360
- var target;
3361
-
3362
- target = upperBound(tokens, function search(token) {
3363
- return token.range[0] > comment.range[0];
3364
- });
3365
-
3366
- comment.extendedRange = [comment.range[0], comment.range[1]];
3367
-
3368
- if (target !== tokens.length) {
3369
- comment.extendedRange[1] = tokens[target].range[0];
3370
- }
3371
-
3372
- target -= 1;
3373
- if (target >= 0) {
3374
- comment.extendedRange[0] = tokens[target].range[1];
3375
- }
3376
-
3377
- return comment;
3378
- }
3379
-
3380
- function attachComments(tree, providedComments, tokens) {
3381
- // At first, we should calculate extended comment ranges.
3382
- var comments = [], comment, len, i, cursor;
3383
-
3384
- if (!tree.range) {
3385
- throw new Error('attachComments needs range information');
3386
- }
3387
-
3388
- // tokens array is empty, we attach comments to tree as 'leadingComments'
3389
- if (!tokens.length) {
3390
- if (providedComments.length) {
3391
- for (i = 0, len = providedComments.length; i < len; i += 1) {
3392
- comment = deepCopy(providedComments[i]);
3393
- comment.extendedRange = [0, tree.range[0]];
3394
- comments.push(comment);
3395
- }
3396
- tree.leadingComments = comments;
3397
- }
3398
- return tree;
3399
- }
3400
-
3401
- for (i = 0, len = providedComments.length; i < len; i += 1) {
3402
- comments.push(extendCommentRange(deepCopy(providedComments[i]), tokens));
3403
- }
3404
-
3405
- // This is based on John Freeman's implementation.
3406
- cursor = 0;
3407
- traverse(tree, {
3408
- enter: function (node) {
3409
- var comment;
3410
-
3411
- while (cursor < comments.length) {
3412
- comment = comments[cursor];
3413
- if (comment.extendedRange[1] > node.range[0]) {
3414
- break;
3415
- }
3416
-
3417
- if (comment.extendedRange[1] === node.range[0]) {
3418
- if (!node.leadingComments) {
3419
- node.leadingComments = [];
3420
- }
3421
- node.leadingComments.push(comment);
3422
- comments.splice(cursor, 1);
3423
- } else {
3424
- cursor += 1;
3425
- }
3426
- }
3427
-
3428
- // already out of owned node
3429
- if (cursor === comments.length) {
3430
- return VisitorOption.Break;
3431
- }
3432
-
3433
- if (comments[cursor].extendedRange[0] > node.range[1]) {
3434
- return VisitorOption.Skip;
3435
- }
3436
- }
3437
- });
3438
-
3439
- cursor = 0;
3440
- traverse(tree, {
3441
- leave: function (node) {
3442
- var comment;
3443
-
3444
- while (cursor < comments.length) {
3445
- comment = comments[cursor];
3446
- if (node.range[1] < comment.extendedRange[0]) {
3447
- break;
3448
- }
3449
-
3450
- if (node.range[1] === comment.extendedRange[0]) {
3451
- if (!node.trailingComments) {
3452
- node.trailingComments = [];
3453
- }
3454
- node.trailingComments.push(comment);
3455
- comments.splice(cursor, 1);
3456
- } else {
3457
- cursor += 1;
3458
- }
3459
- }
3460
-
3461
- // already out of owned node
3462
- if (cursor === comments.length) {
3463
- return VisitorOption.Break;
3464
- }
3465
-
3466
- if (comments[cursor].extendedRange[0] > node.range[1]) {
3467
- return VisitorOption.Skip;
3468
- }
3469
- }
3470
- });
3471
-
3472
- return tree;
3473
- }
3474
-
3475
- exports.Syntax = Syntax;
3476
- exports.traverse = traverse;
3477
- exports.replace = replace;
3478
- exports.attachComments = attachComments;
3479
- exports.VisitorKeys = VisitorKeys;
3480
- exports.VisitorOption = VisitorOption;
3481
- exports.Controller = Controller;
3482
- exports.cloneEnvironment = function () { return clone({}); };
3483
-
3484
- return exports;
3485
- }(exports));
3486
- /* vim: set sw=4 ts=4 et tw=80 : */
3487
-
3488
2684
  function parser(code) {
3489
2685
  const node = acor.parse(code, { ecmaVersion: 'latest' });
3490
- undefined(node, {
2686
+ estraverse.traverse(node, {
3491
2687
  enter: (child, parent) => {
3492
2688
  if (child.type === 'Identifier') {
3493
2689
  const identifierName = child['name'];
@@ -3497,7 +2693,7 @@ function parser(code) {
3497
2693
  }
3498
2694
  }
3499
2695
  });
3500
- undefined(node, {
2696
+ estraverse.replace(node, {
3501
2697
  leave: (child) => {
3502
2698
  //函数调用
3503
2699
  if (child.type == 'CallExpression') {
@@ -5036,7 +4232,6 @@ class TableUtil {
5036
4232
  if (!ss.startControl) {
5037
4233
  return;
5038
4234
  }
5039
- ss.clear();
5040
4235
  const startCell = ElementUtil.getParentByType(ss.startControl, TableCellElement);
5041
4236
  if (!startCell?.parent) {
5042
4237
  throw new Error('parent is null');
@@ -5047,6 +4242,7 @@ class TableUtil {
5047
4242
  if (tb.length === 0) {
5048
4243
  tb.remove();
5049
4244
  }
4245
+ ss.clear();
5050
4246
  }
5051
4247
  /**
5052
4248
  * 移除光标所在的当前列
@@ -11968,6 +11164,7 @@ class ParagraphMeasure {
11968
11164
  */
11969
11165
  measureParagraph(p, limitWidth) {
11970
11166
  ElementUtil.fixParagraphContent(p);
11167
+ this.verifyPara(p, limitWidth);
11971
11168
  p.cacheRender = null;
11972
11169
  const paraModels = [];
11973
11170
  let currRenderLine;
@@ -12088,6 +11285,17 @@ class ParagraphMeasure {
12088
11285
  }
12089
11286
  return paraRenders;
12090
11287
  }
11288
+ /**
11289
+ * 校验当前段落属性
11290
+ */
11291
+ verifyPara(p, limitWidth) {
11292
+ if (p.props.indent > 0 && p.props.indent + 20 > limitWidth) {
11293
+ p.props.indent = limitWidth - 20;
11294
+ }
11295
+ if (p.props.hanging > 0 && p.props.hanging + 20 > limitWidth) {
11296
+ p.props.hanging = limitWidth - 20;
11297
+ }
11298
+ }
12091
11299
  /**
12092
11300
  * 获取段落行布局横向坐标起始位置,被段落text-align影响
12093
11301
  */
@@ -17924,6 +17132,10 @@ var ModifyFlag;
17924
17132
  class NodeCore {
17925
17133
  enableClip = true;
17926
17134
  pointEvent = true;
17135
+ allowHitTest = false;
17136
+ hitTest(hitPos, currPos) {
17137
+ return false;
17138
+ }
17927
17139
  get enable() {
17928
17140
  return this._enable;
17929
17141
  }
@@ -18253,6 +17465,9 @@ class NodeItems extends NodeCore {
18253
17465
  eventMap = new WeakMap();
18254
17466
  absoluteItems = false;
18255
17467
  containerPropName;
17468
+ constructor() {
17469
+ super();
17470
+ }
18256
17471
  addChild(control, index = -1) {
18257
17472
  if (this.containerPropName) {
18258
17473
  this[this.containerPropName].addChild(control, index);
@@ -18324,6 +17539,33 @@ class NodeItems extends NodeCore {
18324
17539
  return this.controls.length;
18325
17540
  }
18326
17541
  }
17542
+ /**
17543
+ * 子内容采用绝对定位的容器
17544
+ */
17545
+ class AbsolutePanel extends NodeItems {
17546
+ constructor() {
17547
+ super();
17548
+ this.absoluteItems = true;
17549
+ }
17550
+ measureOverride(e, availableSize) {
17551
+ this.controls.forEach(item => {
17552
+ item.measure(e, availableSize);
17553
+ });
17554
+ return super.measureOverride(e, availableSize);
17555
+ }
17556
+ arrangeOverride(e, finalSize) {
17557
+ this.controls.forEach(item => {
17558
+ const itemRect = {
17559
+ x: item.x,
17560
+ y: item.y,
17561
+ width: item.desiredSize.width,
17562
+ height: item.desiredSize.height
17563
+ };
17564
+ item.arrange(e, itemRect);
17565
+ });
17566
+ return super.arrangeOverride(e, finalSize);
17567
+ }
17568
+ }
18327
17569
  let currentActiveAppContext = null;
18328
17570
  function getCurrentActiveAppContext() {
18329
17571
  return currentActiveAppContext;
@@ -18441,7 +17683,7 @@ function drawBorderline(node, renderCtx) {
18441
17683
  if (!node.border) {
18442
17684
  return;
18443
17685
  }
18444
- let { x, y, finalRect: { width, height } } = node;
17686
+ let { finalRect: { x, y, width, height } } = node;
18445
17687
  const ctx = renderCtx.ctx;
18446
17688
  ctx.save();
18447
17689
  ctx.fillStyle = node.borderColor;
@@ -19005,7 +18247,7 @@ class NodeEvent {
19005
18247
  };
19006
18248
  mouseleaveRenders.forEach(item => {
19007
18249
  onMouseoutEvent.target = item;
19008
- invokeEvent(onMouseoutEvent, 'mouseleave', item, false);
18250
+ invokeEvent(onMouseoutEvent, 'mouseout', item, false);
19009
18251
  });
19010
18252
  const onMouseoverEvent = {
19011
18253
  pos: this.appState.pos,
@@ -19171,7 +18413,8 @@ class NodeEvent {
19171
18413
  width: width,
19172
18414
  height: height
19173
18415
  };
19174
- if (isInsideRectByPosition(currRect, hitPos)) {
18416
+ const hitTest = node.allowHitTest ? node.hitTest(hitPos, currRect) : isInsideRectByPosition(currRect, hitPos);
18417
+ if (hitTest) {
19175
18418
  //有边框和内边距,所以在计算子节点的时候,需要考虑
19176
18419
  const innerPos = {
19177
18420
  x: currRect.x + node.border + node.padding,
@@ -19333,6 +18576,13 @@ class ViewPaint {
19333
18576
  this.drawHoriLine(x, y, width, textProps.color, 1);
19334
18577
  }
19335
18578
  }
18579
+ drawText2(text, textProps, x, y) {
18580
+ this.ctx.save();
18581
+ this.ctx.fillStyle = textProps.color;
18582
+ this.ctx.font = `${textProps.fontSize}px ${textProps.fontName}`;
18583
+ this.ctx.fillText(text, x, y + textProps.fontSize / 7);
18584
+ this.ctx.restore();
18585
+ }
19336
18586
  fillCircular(x, y, r, color = 'black') {
19337
18587
  this.ctx.save();
19338
18588
  this.ctx.beginPath();
@@ -19347,6 +18597,10 @@ class ViewPaint {
19347
18597
  const textMeasure = this.ctx.measureText(text);
19348
18598
  return { width: textMeasure.width, height: textProps.fontSize };
19349
18599
  }
18600
+ measureText2(text, font) {
18601
+ this.ctx.font = font.fontSize + 'px ' + font.fontName;
18602
+ return this.ctx.measureText(text).width;
18603
+ }
19350
18604
  measureTextUnits(units, textProps) {
19351
18605
  this.ctx.font = textProps.getFont();
19352
18606
  const letterSpace = textProps.letterSpace ?? 0;
@@ -19763,6 +19017,11 @@ class SurfaceView extends NodeItems {
19763
19017
  * 如果有默认宽度限制,而且支持换行显示,则内容高度等于多行累计高度
19764
19018
  */
19765
19019
  class LabelNode extends TextBase {
19020
+ constructor() {
19021
+ super();
19022
+ this.border = 1;
19023
+ this.borderColor = '#000';
19024
+ }
19766
19025
  _textWrapping = 'no';
19767
19026
  get textWrapping() {
19768
19027
  return this._textWrapping;
@@ -19772,34 +19031,54 @@ class LabelNode extends TextBase {
19772
19031
  this._textWrapping = value;
19773
19032
  }
19774
19033
  cacheTextRect;
19775
- textUnits;
19034
+ //private textUnits!: Array<TextUnits>;
19035
+ lines = [];
19776
19036
  measureOverride(e, availableSize) {
19777
- const textProps = this.getTextProps();
19778
- this.textUnits = this.text.split('').map(char => ({ char, actualSize: 0, sourceSize: 0 }));
19779
- e.render.measureTextUnits(this.textUnits, textProps);
19037
+ this.getTextProps();
19038
+ this.lines = this.getMuiltLines(this.text, e);
19039
+ // this.textUnits = this.text.split('').map<TextUnits>(char => ({char, actualSize: 0, sourceSize: 0}));
19040
+ // e.render.measureTextUnits(this.textUnits, textProps);
19780
19041
  //没有宽度限制,测量横向排列的尺寸大小,此时不需要考虑换行
19781
- if (Number.isNaN(this._width)) {
19782
- return {
19783
- width: this.textUnits.reduce((prev, curr) => {
19784
- return prev + curr.sourceSize;
19785
- }, 0), height: this.fontSize
19786
- };
19787
- }
19788
- if (this._textWrapping === 'no') {
19789
- return { width: this.getPercentWidth(availableSize.width), height: this.fontSize };
19790
- }
19791
- return this.measureTextRect(this.textUnits, {
19042
+ // if (Number.isNaN(this._width)) {
19043
+ // // return {
19044
+ // // width: this.lines[0].textUnits.reduce((prev, curr) => {
19045
+ // // return prev + curr.sourceSize
19046
+ // // }, 0), height: this.fontSize
19047
+ // // };
19048
+ // return this.measureTextRect(this.lines, {
19049
+ // width: availableSize.width,
19050
+ // height: this.getPercentHeight(availableSize.height)
19051
+ // });
19052
+ // }
19053
+ // if (this._textWrapping === 'no') {
19054
+ // return {width: this.getPercentWidth(availableSize.width), height: this.fontSize};
19055
+ // }
19056
+ return this.measureTextRect(this.lines, {
19792
19057
  width: availableSize.width,
19793
19058
  height: this.getPercentHeight(availableSize.height)
19794
19059
  });
19795
19060
  }
19796
- arrangeOverride(e, finalSize) {
19797
- if (!Number.isNaN(this._width) && this._textWrapping === 'wrap') {
19798
- this.measureTextRect(this.textUnits, {
19799
- width: finalSize.width,
19800
- height: finalSize.height
19801
- });
19061
+ getMuiltLines(text, e) {
19062
+ const lineRects = [];
19063
+ const lines = this.text.split('\r\n');
19064
+ for (let i = 0; i < lines.length; i++) {
19065
+ const units = lines[i].split('').map(char => ({ char, actualSize: 0, sourceSize: 0 }));
19066
+ e.render.measureTextUnits(units, this.getTextProps());
19067
+ lineRects.push({ textUnits: units });
19802
19068
  }
19069
+ return lineRects;
19070
+ }
19071
+ arrangeOverride(e, finalSize) {
19072
+ // if (!Number.isNaN(this._width) && this._textWrapping === 'wrap') {
19073
+ // this.measureTextRect(this.lines, {
19074
+ // width: finalSize.width,
19075
+ // height: finalSize.height
19076
+ // });
19077
+ // }
19078
+ this.measureTextRect(this.lines, {
19079
+ width: finalSize.width,
19080
+ height: finalSize.height
19081
+ });
19803
19082
  return super.arrangeOverride(e, finalSize);
19804
19083
  }
19805
19084
  getTextProps() {
@@ -19809,54 +19088,56 @@ class LabelNode extends TextBase {
19809
19088
  textProps.color = this.color ?? '#000';
19810
19089
  return textProps;
19811
19090
  }
19812
- measureTextRect(textUnits, availableSize, final = false) {
19813
- const sumWidth = textUnits.reduce((prev, curr) => {
19814
- return curr.sourceSize + prev;
19815
- }, 0);
19091
+ measureTextRect(lines, availableSize, final = false) {
19816
19092
  //总宽度大于可用宽度,换行计算
19817
19093
  const cacheLines = [];
19818
19094
  const { width: limitWidth, height: limitHeight } = availableSize;
19819
- let line = { lineUnits: [], x: 0, y: 0, width: 0, height: 0 };
19820
- cacheLines.push(line);
19821
- for (let i = 0; i < textUnits.length; i++) {
19822
- const unit = textUnits[i];
19823
- //至少每行一个字符
19824
- if (line.lineUnits.length > 0 && line.width + unit.sourceSize > limitWidth) {
19825
- line = { lineUnits: [], x: 0, y: 0, width: 0, height: 0 };
19826
- cacheLines.push(line);
19827
- }
19828
- line.lineUnits.push(unit);
19829
- line.width += unit.sourceSize;
19830
- line.height = this.fontSize;
19095
+ for (let i = 0; i < lines.length; i++) {
19096
+ const lineText = lines[i];
19097
+ let line = { lineUnits: [], x: 0, y: 0, width: 0, height: 0 };
19098
+ cacheLines.push(line);
19099
+ for (let j = 0; j < lineText.textUnits.length; j++) {
19100
+ const unit = lineText.textUnits[j];
19101
+ //至少每行一个字符
19102
+ if (line.lineUnits.length > 0 && line.width + unit.sourceSize > limitWidth) {
19103
+ line = { lineUnits: [], x: 0, y: 0, width: 0, height: 0 };
19104
+ cacheLines.push(line);
19105
+ }
19106
+ line.lineUnits.push(unit);
19107
+ line.width += unit.sourceSize;
19108
+ line.height = this.fontSize;
19109
+ }
19831
19110
  }
19832
19111
  let contentHeight = 0;
19112
+ let maxLineWidth = 0;
19833
19113
  cacheLines.forEach(line => {
19834
19114
  line.y = contentHeight;
19835
19115
  contentHeight += line.height;
19116
+ maxLineWidth = Math.max(line.width, maxLineWidth);
19836
19117
  });
19837
19118
  //期望高度
19838
19119
  let desiredHeight = final ? limitHeight : contentHeight;
19839
19120
  this.cacheTextRect = {
19840
19121
  lines: cacheLines,
19841
- width: sumWidth,
19122
+ width: maxLineWidth,
19842
19123
  height: contentHeight,
19843
19124
  desiredHeight,
19844
19125
  desiredWidth: limitWidth
19845
19126
  };
19846
19127
  return {
19847
- width: limitWidth,
19128
+ width: maxLineWidth,
19848
19129
  height: desiredHeight
19849
19130
  };
19850
19131
  }
19851
19132
  render(e) {
19852
- const textProp = new TextProps();
19853
- textProp.fontName = this.fontName;
19854
- textProp.fontSize = this.fontSize;
19855
- textProp.color = this.color;
19856
- if (this.textWrapping === 'no' || Number.isNaN(this._width)) {
19857
- e.render.drawText(this.text, textProp, 0, 0, this.finalRect.width, textProp.fontSize);
19858
- return;
19859
- }
19133
+ // const textProp = new TextProps();
19134
+ // textProp.fontName = this.fontName;
19135
+ // textProp.fontSize = this.fontSize;
19136
+ // textProp.color = this.color;
19137
+ // if (this.textWrapping === 'no' || Number.isNaN(this._width)) {
19138
+ // e.render.drawText(this.text, textProp, 0, 0, this.finalRect.width, textProp.fontSize);
19139
+ // return;
19140
+ // }
19860
19141
  for (let i = 0; i < this.cacheTextRect.lines.length; i++) {
19861
19142
  const line = this.cacheTextRect.lines[i];
19862
19143
  this.drawLine(e, line);
@@ -19872,7 +19153,7 @@ class LabelNode extends TextBase {
19872
19153
  render.tran(() => {
19873
19154
  const textProps = this.getTextProps();
19874
19155
  render.ctx.font = textProps.getFont();
19875
- render.drawText(line.lineUnits.join(''), textProps, 0, line.y, line.width, line.height);
19156
+ render.drawText(line.lineUnits.map(item => item.char).join(''), textProps, 0, line.y, line.width, line.height);
19876
19157
  });
19877
19158
  }
19878
19159
  }
@@ -20122,8 +19403,8 @@ class ScrollView extends NodeItems {
20122
19403
  });
20123
19404
  this.addEventListener('wheel', evt => {
20124
19405
  const { deltaY, deltaX } = evt;
20125
- this.horBar.updateScrollByCurrent(deltaX / 10, deltaY / 10);
20126
- this.verBar.updateScrollByCurrent(deltaX / 10, deltaY / 10);
19406
+ this.horBar.updateScrollByCurrent(deltaX, deltaY);
19407
+ this.verBar.updateScrollByCurrent(deltaX, deltaY);
20127
19408
  });
20128
19409
  }
20129
19410
  scrollTo(x, y) {
@@ -20842,7 +20123,7 @@ function pointInPoly(pt, poly) {
20842
20123
  * 1.在单页模式下,文档最小宽度为单个文档宽度+合适的外边距
20843
20124
  * 2.在多页模式下,文档最小宽度为单个文档宽度+合适的外边距
20844
20125
  */
20845
- class CanvasTextEditor extends NodeItems {
20126
+ class CanvasTextEditor extends AbsolutePanel {
20846
20127
  editCanvas;
20847
20128
  editInput;
20848
20129
  contentCtx;
@@ -21769,6 +21050,13 @@ class CanvasTextEditor extends NodeItems {
21769
21050
  const win = new Window();
21770
21051
  win.width = 1000;
21771
21052
  win.height = 800;
21053
+ //win.content.addChild(timelineScrollbar)
21054
+ const rule2 = new RuleControl(this.docCtx);
21055
+ this.rule = rule2;
21056
+ rule2.width = 700;
21057
+ rule2.height = 30;
21058
+ rule2.x = 20;
21059
+ rule2.y = 500;
21772
21060
  const surface = new SurfaceView(this.editCanvas, this.editInput);
21773
21061
  this.surfaceView = surface;
21774
21062
  surface.width = 1000;
@@ -21780,12 +21068,6 @@ class CanvasTextEditor extends NodeItems {
21780
21068
  this.resetViewer();
21781
21069
  });
21782
21070
  surface.addChild(scrollView);
21783
- const rule2 = new RuleControl(this.docCtx);
21784
- this.rule = rule2;
21785
- rule2.width = 700;
21786
- rule2.height = 30;
21787
- rule2.x = 20;
21788
- rule2.y = 500;
21789
21071
  surface.addChild(rule2);
21790
21072
  //surface.addChild(win);
21791
21073
  surface.start();
@@ -21860,24 +21142,6 @@ class CanvasTextEditor extends NodeItems {
21860
21142
  appCtx.root.setInputPosition(caretPos);
21861
21143
  }
21862
21144
  }
21863
- measureOverride(e, availableSize) {
21864
- this.controls.forEach(item => {
21865
- item.measure(e, availableSize);
21866
- });
21867
- return super.measureOverride(e, availableSize);
21868
- }
21869
- arrangeOverride(e, finalSize) {
21870
- this.controls.forEach(item => {
21871
- const itemRect = {
21872
- x: item.x,
21873
- y: item.y,
21874
- width: item.desiredSize.width,
21875
- height: item.desiredSize.height
21876
- };
21877
- item.arrange(e, itemRect);
21878
- });
21879
- return super.arrangeOverride(e, finalSize);
21880
- }
21881
21145
  }
21882
21146
 
21883
21147
  /**