UNPKG

6.08 kBJavaScriptView Raw
1
2import xhtml from './xhtml';
3
4import isElement from '@yelloxing/core.js/isElement';
5import isString from '@yelloxing/core.js/isString';
6import isFunction from '@yelloxing/core.js/isFunction';
7
8// 核心方法和工具方法
9
10import { textWidth, bestLeftNum, calcCanvasXY, selectIsNotBlank, toTemplate } from './edit-view/tool';
11
12import { initDom, initView } from './edit-view/init';
13import { updateView, updateSelectView, updateCursorPosition, updateCanvasSize, cancelSelect, deleteSelect } from './edit-view/update';
14import bindEvent from './edit-view/bind';
15import diff from './edit-view/diff';
16
17import filterText from './edit-view/filter';
18
19let wscode = function (options) {
20
21 if (!(this instanceof wscode)) {
22 throw new Error('WSCode is a constructor and should be called with the `new` keyword');
23 }
24
25 /**
26 *
27 * [格式化配置]
28 *
29 * 所有的配置校验和默认值设置等都应该在这里进行
30 * 经过这里处理以后,后续不需要再进行校验了
31 * 因此这里的内容的更改一定要慎重
32 *
33 */
34
35 // 编辑器挂载点
36 if (isElement(options.el)) {
37
38 // 着色器
39 let shader = () => {
40 let resultData = [];
41 this._contentArray.forEach(text => { resultData.push([{ content: text, color: this._colorText }]); });
42 return resultData;
43 };
44
45 // 格式化
46 let format = textString => textString;
47
48 this._el = options.el;
49
50 // 公共配置
51 options.color = options.color || {};
52 this._colorBackground = options.color.background || "#d6d6e4"; /*编辑器背景*/
53 this._colorText = options.color.text || "#000000"; /*普通文本颜色*/
54 this._colorNumber = options.color.number || "#888484"; /*行号颜色*/
55 this._colorEdit = options.color.edit || "#eaeaf1"; /*编辑行颜色*/
56 this._colorCursor = options.color.cursor || "#ff0000"; /*光标颜色*/
57 this._colorSelect = options.color.select || "#6c6cf1"; /*选择背景*/
58 this._fontFamily = options["font-family"] || "新宋体"; /*字体*/
59 this._fontWeight = options["font-weight"] || 600;/*字重*/
60 this._tabSpace = options.tabSpace || 4;/*设置一个tab表示多少个空格*/
61 this._readonly = options.readonly || false;/*是否只读*/
62 this._noLineNumber = options.noLineNumber || false;/*是否隐藏行号*/
63
64 // 文本
65 this._contentArray = isString(options.content) ? (this.$$filterText(options.content) + "").split("\n") : [""];
66
67 // 着色方法
68 this.$shader = isFunction(options.shader) ? options.shader : shader;
69
70 // 格式化方法
71 this.$format = isFunction(options.format) ? options.format : format;
72
73 // 辅助输入
74 this.$input = isFunction(options.input) ? options.input : null;
75
76 } else {
77
78 // 挂载点是必须的,一定要有
79 throw new Error('options.el is not a element!');
80 }
81
82 // 先初始化DOM
83 this.$$initDom();
84
85 // 初始化控制变量
86 this.__needUpdate = true;
87 this.__lineNum = this._contentArray.length - 1;
88 this.__leftNum = this._contentArray[this.__lineNum].length;
89 this.__cursor1 = this.__cursor2 = { leftNum: 0, lineNum: 0 };
90 this.__formatData = this.$$diff(this.$shader(this._contentArray.join('\n')));
91
92 // 初始化视图
93 this.$$initView();
94
95 // 更新视图
96 this.$$updateView();
97
98 // 绑定操作
99 this.$$bindEvent();
100
101 this.__updated__ = () => { };
102 // 编辑器管理的文本发生改变后会主动触发callback方法
103 this.updated = callback => {
104 this.__updated__ = callback;
105 };
106
107 // 获取当前编辑器代码
108 this.valueOf = () => {
109 return this._contentArray.join('\n');
110 };
111
112 // 在当前光标位置输入新的内容
113 this.input = (content = "", cursor = 0, number = 0) => {
114
115 // 先删除多余的内容
116
117 if (cursor != 0) {
118
119 if (number != 0) {
120 this._contentArray[this.__lineNum] =
121 this._contentArray[this.__lineNum].substring(0, this.__leftNum + cursor) +
122 this._contentArray[this.__lineNum].substring(this.__leftNum + cursor + number);
123 }
124
125 // 修改光标位置
126 this.__leftNum += cursor;
127
128 }
129
130 // 输入以触发更新
131 this.__focusDOM.value = content;
132 xhtml.trigger(this.__focusDOM, 'input');
133 this.__focusDOM.focus();
134
135 };
136
137 // 格式化代码
138 this.format = () => {
139
140 // 格式化内容
141 this._contentArray = this.$format(this._contentArray.join('\n'), this._tabSpace).split('\n');
142
143 this.__lineNum = this._contentArray.length - 1;
144 this.__leftNum = this._contentArray[this.__lineNum].length;
145
146 // 着色
147 this.__formatData = this.$$diff(this.$shader(this._contentArray.join('\n')));
148
149 // 更新视图
150 this.$$updateView();
151
152 // 更新光标位置
153 this.$$initView();
154
155 };
156
157};
158
159// 挂载辅助方法
160wscode.prototype.$$textWidth = textWidth;
161wscode.prototype.$$bestLeftNum = bestLeftNum;
162wscode.prototype.$$calcCanvasXY = calcCanvasXY;
163wscode.prototype.$$selectIsNotBlank = selectIsNotBlank;
164wscode.prototype.$$filterText = filterText;
165wscode.prototype.$$toTemplate = toTemplate;
166
167// 挂载核心方法
168
169wscode.prototype.$$initDom = initDom;
170wscode.prototype.$$initView = initView;
171
172wscode.prototype.$$updateView = updateView;
173wscode.prototype.$$updateSelectView = updateSelectView;
174wscode.prototype.$$updateCursorPosition = updateCursorPosition;
175wscode.prototype.$$updateCanvasSize = updateCanvasSize;
176wscode.prototype.$$cancelSelect = cancelSelect;
177wscode.prototype.$$deleteSelect = deleteSelect;
178
179wscode.prototype.$$bindEvent = bindEvent;
180
181// 性能优化系列方法
182
183wscode.prototype.$$diff = diff;
184
185wscode.author = '心叶(yelloxing@gmail.com)';
186
187if (typeof module === "object" && typeof module.exports === "object") {
188 module.exports = wscode;
189} else {
190 window.WSCode = wscode;
191}
\No newline at end of file