1 | import isElement from '@yelloxing/core.js/isElement';
|
2 | import isString from '@yelloxing/core.js/isString';
|
3 | import isFunction from '@yelloxing/core.js/isFunction';
|
4 |
|
5 | import { textWidth, bestLeftNum } from './edit-view/tool';
|
6 |
|
7 | import { initDom, initView } from './edit-view/init';
|
8 | import { updateView, updateCursorPosition } from './edit-view/update';
|
9 | import bindEvent from './edit-view/bind';
|
10 |
|
11 | let wscode = function (options) {
|
12 |
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | if (isElement(options.el)) {
|
25 |
|
26 | this._el = options.el;
|
27 |
|
28 |
|
29 | options.color = options.color || {};
|
30 | this._colorBackground = options.color.background || "#d6d6e4";
|
31 | this._colorText = options.color.text || "#000000";
|
32 | this._colorNumber = options.color.number || "#888484";
|
33 | this._colorEdit = options.color.edit || "#eaeaf1";
|
34 | this._colorCursor = options.color.cursor || "#ff0000";
|
35 | this._fontFamily = options["font-family"] || "新宋体";
|
36 | this._fontWeight = options["font-weight"] || 600;
|
37 | this._tabSpace = options.tabSpace || 4;
|
38 |
|
39 |
|
40 | this._contentArray = isString(options.content) ? (options.content + "").split("\n") : [""];
|
41 |
|
42 |
|
43 | this.$shader = isFunction(options.shader) ? options.shader : () => {
|
44 | let resultData = [];
|
45 | this._contentArray.forEach(text => { resultData.push([{ content: text, color: this._colorText }]); });
|
46 | return resultData;
|
47 | };
|
48 |
|
49 |
|
50 | this.$format = isFunction(options.format) ? options.format : textString => textString;
|
51 |
|
52 | } else {
|
53 |
|
54 |
|
55 | throw new Error('options.el is not a element!');
|
56 | }
|
57 |
|
58 |
|
59 | this.$$initDom();
|
60 |
|
61 |
|
62 | this.__needUpdate = true;
|
63 | this.__lineNum = this._contentArray.length - 1;
|
64 | this.__leftNum = this._contentArray[this.__lineNum].length;
|
65 | this.__formatData = this.$shader(this._contentArray.join('\n'));
|
66 |
|
67 |
|
68 | this.$$initView();
|
69 |
|
70 |
|
71 | this.$$updateView();
|
72 |
|
73 |
|
74 | this.$$bindEvent();
|
75 |
|
76 | this.valueOf = () => {
|
77 | return this._contentArray.join('\n');
|
78 | };
|
79 |
|
80 | this.format = () => {
|
81 |
|
82 |
|
83 | this._contentArray = this.$format(this._contentArray.join('\n')).split('\n');
|
84 |
|
85 | this.__lineNum = this._contentArray.length - 1;
|
86 | this.__leftNum = this._contentArray[this.__lineNum].length;
|
87 |
|
88 |
|
89 | this.__formatData = this.$shader(this._contentArray.join('\n'));
|
90 |
|
91 |
|
92 | this.$$updateView();
|
93 |
|
94 |
|
95 | this.$$initView();
|
96 |
|
97 | };
|
98 |
|
99 | };
|
100 |
|
101 |
|
102 | wscode.prototype.$$textWidth = textWidth;
|
103 | wscode.prototype.$$bestLeftNum = bestLeftNum;
|
104 |
|
105 |
|
106 |
|
107 | wscode.prototype.$$initDom = initDom;
|
108 | wscode.prototype.$$initView = initView;
|
109 |
|
110 | wscode.prototype.$$updateView = updateView;
|
111 | wscode.prototype.$$updateCursorPosition = updateCursorPosition;
|
112 |
|
113 | wscode.prototype.$$bindEvent = bindEvent;
|
114 |
|
115 | wscode.author = '心叶(yelloxing@gmail.com)';
|
116 |
|
117 | if (typeof module === "object" && typeof module.exports === "object") {
|
118 | module.exports = wscode;
|
119 | } else {
|
120 | window.WSCode = wscode;
|
121 | } |
\ | No newline at end of file |