UNPKG

3.82 kBJavaScriptView Raw
1import isElement from '@yelloxing/core.js/isElement';
2import isString from '@yelloxing/core.js/isString';
3import isFunction from '@yelloxing/core.js/isFunction';
4
5import { textWidth, bestLeftNum } from './edit-view/tool';
6
7import { initDom, initView } from './edit-view/init';
8import { updateView, updateCursorPosition } from './edit-view/update';
9import bindEvent from './edit-view/bind';
10
11let 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;/*设置一个tab表示多少个空格*/
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 // 先初始化DOM
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// 挂载辅助方法
102wscode.prototype.$$textWidth = textWidth;
103wscode.prototype.$$bestLeftNum = bestLeftNum;
104
105// 挂载核心方法
106
107wscode.prototype.$$initDom = initDom;
108wscode.prototype.$$initView = initView;
109
110wscode.prototype.$$updateView = updateView;
111wscode.prototype.$$updateCursorPosition = updateCursorPosition;
112
113wscode.prototype.$$bindEvent = bindEvent;
114
115wscode.author = '心叶(yelloxing@gmail.com)';
116
117if (typeof module === "object" && typeof module.exports === "object") {
118 module.exports = wscode;
119} else {
120 window.WSCode = wscode;
121}
\No newline at end of file