@leafer-in/text-editor 1.8.0 → 1.9.0

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.
@@ -1,109 +1,94 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
- var core = require('@leafer-ui/core');
4
- var editor = require('@leafer-in/editor');
3
+ var core = require("@leafer-ui/core");
5
4
 
6
- /******************************************************************************
7
- Copyright (c) Microsoft Corporation.
8
-
9
- Permission to use, copy, modify, and/or distribute this software for any
10
- purpose with or without fee is hereby granted.
11
-
12
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
13
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
15
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
17
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18
- PERFORMANCE OF THIS SOFTWARE.
19
- ***************************************************************************** */
20
- /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
21
-
22
-
23
- function __decorate(decorators, target, key, desc) {
24
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
25
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
26
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
27
- return c > 3 && r && Object.defineProperty(target, key, r), r;
28
- }
29
-
30
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
31
- var e = new Error(message);
32
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
5
+ var editor = require("@leafer-in/editor");
6
+
7
+ function __decorate(decorators, target, key, desc) {
8
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
9
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
10
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
11
+ }
12
+
13
+ typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
14
+ var e = new Error(message);
15
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
33
16
  };
34
17
 
35
18
  const textCaseMap = {
36
- 'none': 'none',
37
- 'title': 'capitalize',
38
- 'upper': 'uppercase',
39
- 'lower': 'lowercase',
40
- 'small-caps': 'small-caps'
19
+ none: "none",
20
+ title: "capitalize",
21
+ upper: "uppercase",
22
+ lower: "lowercase",
23
+ "small-caps": "small-caps"
41
24
  };
25
+
42
26
  const verticalAlignMap = {
43
- 'top': 'flex-start',
44
- 'middle': 'center',
45
- 'bottom': 'flex-end'
27
+ top: "flex-start",
28
+ middle: "center",
29
+ bottom: "flex-end"
46
30
  };
31
+
47
32
  const textDecorationMap = {
48
- 'none': 'none',
49
- 'under': 'underline',
50
- 'delete': 'line-through',
51
- 'under-delete': 'underline line-through'
33
+ none: "none",
34
+ under: "underline",
35
+ delete: "line-through",
36
+ "under-delete": "underline line-through"
52
37
  };
38
+
53
39
  function updateStyle(textDom, text, textScale) {
54
- const { style } = textDom;
55
- const { fill, padding, textWrap, textOverflow, textDecoration } = text;
40
+ const {style: style} = textDom;
41
+ const {fill: fill, padding: padding, textWrap: textWrap, textOverflow: textOverflow, textDecoration: textDecoration} = text;
56
42
  style.fontFamily = text.fontFamily;
57
- style.fontSize = text.fontSize * textScale + 'px';
43
+ style.fontSize = text.fontSize * textScale + "px";
58
44
  setFill(style, fill);
59
- style.fontStyle = text.italic ? 'italic' : 'normal';
45
+ style.fontStyle = text.italic ? "italic" : "normal";
60
46
  style.fontWeight = text.fontWeight;
61
47
  let decorationType;
62
- if (typeof textDecoration === 'object') {
48
+ if (core.isObject(textDecoration)) {
63
49
  decorationType = textDecoration.type;
64
- if (textDecoration.color)
65
- style.textDecorationColor = core.ColorConvert.string(textDecoration.color);
66
- }
67
- else {
50
+ if (textDecoration.color) style.textDecorationColor = core.ColorConvert.string(textDecoration.color);
51
+ } else {
68
52
  decorationType = textDecoration;
69
53
  }
70
54
  style.textDecoration = textDecorationMap[decorationType];
71
55
  style.textTransform = textCaseMap[text.textCase];
72
- style.textAlign = text.textAlign === 'both' ? 'justify' : text.textAlign;
73
- style.display = 'flex';
74
- style.flexDirection = 'column';
56
+ style.textAlign = text.textAlign === "both" ? "justify" : text.textAlign;
57
+ style.display = "flex";
58
+ style.flexDirection = "column";
75
59
  style.justifyContent = verticalAlignMap[text.verticalAlign];
76
- style.lineHeight = (text.__.__lineHeight || 0) * textScale + 'px';
77
- style.letterSpacing = (text.__.__letterSpacing || 0) * textScale + 'px';
78
- style.whiteSpace = (textWrap === 'none' || text.__.__autoWidth) ? 'nowrap' : 'normal';
79
- style.wordBreak = textWrap === 'break' ? 'break-all' : 'normal';
80
- style.textIndent = (text.paraIndent || 0) * textScale + 'px';
81
- style.padding = padding instanceof Array ? padding.map(item => item * textScale + 'px').join(' ') : (padding || 0) * textScale + 'px';
82
- style.textOverflow = textOverflow === 'show' ? '' : (textOverflow === 'hide' ? 'clip' : textOverflow);
60
+ style.lineHeight = (text.__.__lineHeight || 0) * textScale + "px";
61
+ style.letterSpacing = (text.__.__letterSpacing || 0) * textScale + "px";
62
+ style.whiteSpace = textWrap === "none" || text.__.__autoWidth ? "nowrap" : "normal";
63
+ style.wordBreak = textWrap === "break" ? "break-all" : "normal";
64
+ style.textIndent = (text.paraIndent || 0) * textScale + "px";
65
+ style.padding = core.isArray(padding) ? padding.map(item => item * textScale + "px").join(" ") : (padding || 0) * textScale + "px";
66
+ style.textOverflow = textOverflow === "show" ? "" : textOverflow === "hide" ? "clip" : textOverflow;
83
67
  }
68
+
84
69
  function setFill(style, fill) {
85
- let color = 'black';
86
- if (fill instanceof Array)
87
- fill = fill[0];
88
- if (typeof fill === 'object') {
70
+ let color = "black";
71
+ if (core.isArray(fill)) fill = fill[0];
72
+ if (core.isObject(fill)) {
89
73
  switch (fill.type) {
90
- case 'solid':
91
- color = core.ColorConvert.string(fill.color);
92
- break;
93
- case 'image':
94
- break;
95
- case 'linear':
96
- case 'radial':
97
- case 'angular':
98
- const stop = fill.stops[0];
99
- color = core.ColorConvert.string(typeof stop === 'string' ? stop : stop.color);
100
- break;
101
- default:
102
- if (fill.r !== undefined)
103
- color = core.ColorConvert.string(fill);
74
+ case "solid":
75
+ color = core.ColorConvert.string(fill.color);
76
+ break;
77
+
78
+ case "image":
79
+ break;
80
+
81
+ case "linear":
82
+ case "radial":
83
+ case "angular":
84
+ const stop = fill.stops[0];
85
+ color = core.ColorConvert.string(core.isString(stop) ? stop : stop.color);
86
+ break;
87
+
88
+ default:
89
+ if (!core.isUndefined(fill.r)) color = core.ColorConvert.string(fill);
104
90
  }
105
- }
106
- else {
91
+ } else {
107
92
  color = fill;
108
93
  }
109
94
  style.color = color;
@@ -117,36 +102,34 @@ exports.TextEditor = class TextEditor extends editor.InnerEditor {
117
102
  };
118
103
  this.eventIds = [];
119
104
  }
120
- get tag() { return 'TextEditor'; }
105
+ get tag() {
106
+ return "TextEditor";
107
+ }
121
108
  onLoad() {
122
- const { editor } = this;
123
- const { config } = editor.app;
109
+ const {editor: editor} = this;
110
+ const {config: config} = editor.app;
124
111
  const text = this.editTarget;
125
112
  text.textEditing = true;
126
113
  this.isHTMLText = !(text instanceof core.Text);
127
114
  this._keyEvent = config.keyEvent;
128
115
  config.keyEvent = false;
129
- const div = this.editDom = document.createElement('div');
130
- const { style } = div;
131
- div.contentEditable = 'true';
132
- style.position = 'fixed';
133
- style.transformOrigin = 'left top';
134
- style.boxSizing = 'border-box';
116
+ const div = this.editDom = document.createElement("div");
117
+ const {style: style} = div;
118
+ div.contentEditable = "true";
119
+ style.position = "fixed";
120
+ style.transformOrigin = "left top";
121
+ style.boxSizing = "border-box";
135
122
  this.isHTMLText ? div.innerHTML = String(text.text) : div.innerText = String(text.text);
136
- const { view } = editor.app;
123
+ const {view: view} = editor.app;
137
124
  (this.inBody = view instanceof HTMLCanvasElement) ? document.body.appendChild(div) : view.appendChild(div);
138
- this.eventIds = [
139
- editor.app.on_(core.PointerEvent.DOWN, (e) => {
140
- let { target } = e.origin, find;
141
- while (target) {
142
- if (target === div)
143
- find = true;
144
- target = target.parentElement;
145
- }
146
- if (!find)
147
- editor.closeInnerEditor();
148
- })
149
- ];
125
+ this.eventIds = [ editor.app.on_(core.PointerEvent.DOWN, e => {
126
+ let {target: target} = e.origin, find;
127
+ while (target) {
128
+ if (target === div) find = true;
129
+ target = target.parentElement;
130
+ }
131
+ if (!find) editor.closeInnerEditor();
132
+ }) ];
150
133
  this.onFocus = this.onFocus.bind(this);
151
134
  this.onInput = this.onInput.bind(this);
152
135
  this.onPaste = this.onPaste.bind(this);
@@ -155,14 +138,13 @@ exports.TextEditor = class TextEditor extends editor.InnerEditor {
155
138
  div.addEventListener("focus", this.onFocus);
156
139
  div.addEventListener("input", this.onInput);
157
140
  div.addEventListener("paste", this.onPaste);
158
- window.addEventListener('keydown', this.onKeydown);
159
- window.addEventListener('scroll', this.onUpdate);
141
+ window.addEventListener("keydown", this.onKeydown);
142
+ window.addEventListener("scroll", this.onUpdate);
160
143
  const selection = window.getSelection();
161
144
  const range = document.createRange();
162
145
  if (this.config.selectAll) {
163
146
  range.selectNodeContents(div);
164
- }
165
- else {
147
+ } else {
166
148
  const node = div.childNodes[0];
167
149
  range.setStartAfter(node);
168
150
  range.setEndAfter(node);
@@ -172,19 +154,18 @@ exports.TextEditor = class TextEditor extends editor.InnerEditor {
172
154
  selection.addRange(range);
173
155
  }
174
156
  onInput() {
175
- const { editDom } = this;
157
+ const {editDom: editDom} = this;
176
158
  this.editTarget.text = this.isHTMLText ? editDom.innerHTML : editDom.innerText;
177
159
  }
178
160
  onFocus() {
179
- this.editDom.style.outline = 'none';
161
+ this.editDom.style.outline = "none";
180
162
  }
181
163
  onKeydown(e) {
182
- if (e.code === 'Escape')
183
- this.editor.closeInnerEditor();
184
- if (e.key === 'Enter') {
164
+ if (e.code === "Escape") this.editor.closeInnerEditor();
165
+ if (e.key === "Enter") {
185
166
  e.preventDefault();
186
- const br = document.createElement('br');
187
- const zwsp = document.createTextNode('\u200B');
167
+ const br = document.createElement("br");
168
+ const zwsp = document.createTextNode("​");
188
169
  const selection = window.getSelection();
189
170
  const range = selection.getRangeAt(0);
190
171
  range.deleteContents();
@@ -196,23 +177,19 @@ exports.TextEditor = class TextEditor extends editor.InnerEditor {
196
177
  }
197
178
  }
198
179
  onPaste(event) {
199
- if (this.isHTMLText)
200
- return;
180
+ if (this.isHTMLText) return;
201
181
  event.preventDefault();
202
182
  const clipboardData = event.clipboardData;
203
- if (!clipboardData)
204
- return;
205
- let text = clipboardData.getData('text/plain').replace(/\r\n?/g, '\n');
183
+ if (!clipboardData) return;
184
+ let text = clipboardData.getData("text/plain").replace(/\r\n?/g, "\n");
206
185
  const selection = window.getSelection();
207
- if (!selection || !selection.rangeCount)
208
- return;
186
+ if (!selection || !selection.rangeCount) return;
209
187
  const range = selection.getRangeAt(0);
210
188
  range.deleteContents();
211
- const lines = text.split('\n');
189
+ const lines = text.split("\n");
212
190
  const fragment = document.createDocumentFragment();
213
191
  lines.forEach((line, index) => {
214
- if (index > 0)
215
- fragment.appendChild(document.createElement('br'));
192
+ if (index > 0) fragment.appendChild(document.createElement("br"));
216
193
  fragment.appendChild(document.createTextNode(line));
217
194
  });
218
195
  range.insertNode(fragment);
@@ -222,27 +199,28 @@ exports.TextEditor = class TextEditor extends editor.InnerEditor {
222
199
  this.onInput();
223
200
  }
224
201
  onUpdate() {
225
- const { editTarget: text } = this;
202
+ const {editTarget: text} = this;
226
203
  let textScale = 1;
227
204
  if (!this.isHTMLText) {
228
- const { scaleX, scaleY } = text.worldTransform;
205
+ const {scaleX: scaleX, scaleY: scaleY} = text.worldTransform;
229
206
  textScale = Math.max(Math.abs(scaleX), Math.abs(scaleY));
230
207
  const fontSize = text.fontSize * textScale;
231
- if (fontSize < 12)
232
- textScale *= 12 / text.fontSize;
208
+ if (fontSize < 12) textScale *= 12 / text.fontSize;
233
209
  }
234
210
  this.textScale = textScale;
235
- let { width, height } = text, offsetX = 0, offsetY = 0;
211
+ let {width: width, height: height} = text, offsetX = 0, offsetY = 0;
236
212
  width *= textScale, height *= textScale;
237
213
  const data = text.__;
238
214
  if (data.__autoWidth) {
239
215
  width += 20;
240
216
  if (data.autoSizeAlign) {
241
217
  switch (data.textAlign) {
242
- case 'center':
243
- offsetX = -width / 2;
244
- break;
245
- case 'right': offsetX = -width;
218
+ case "center":
219
+ offsetX = -width / 2;
220
+ break;
221
+
222
+ case "right":
223
+ offsetX = -width;
246
224
  }
247
225
  }
248
226
  }
@@ -250,45 +228,44 @@ exports.TextEditor = class TextEditor extends editor.InnerEditor {
250
228
  height += 20;
251
229
  if (data.autoSizeAlign) {
252
230
  switch (data.verticalAlign) {
253
- case 'middle':
254
- offsetY = -height / 2;
255
- break;
256
- case 'bottom': offsetY = -height;
231
+ case "middle":
232
+ offsetY = -height / 2;
233
+ break;
234
+
235
+ case "bottom":
236
+ offsetY = -height;
257
237
  }
258
238
  }
259
239
  }
260
- const { x, y } = this.inBody ? text.app.clientBounds : text.app.tree.clientBounds;
261
- const { a, b, c, d, e, f } = new core.Matrix(text.worldTransform).scale(1 / textScale).translateInner(offsetX, offsetY);
262
- const { style } = this.editDom;
240
+ const {x: x, y: y} = this.inBody ? text.app.clientBounds : text.app.tree.clientBounds;
241
+ const {a: a, b: b, c: c, d: d, e: e, f: f} = new core.Matrix(text.worldTransform).scale(1 / textScale).translateInner(offsetX, offsetY);
242
+ const {style: style} = this.editDom;
263
243
  style.transform = `matrix(${a},${b},${c},${d},${e},${f})`;
264
- style.left = x + 'px';
265
- style.top = y + 'px';
266
- style.width = width + 'px';
267
- style.height = height + 'px';
244
+ style.left = x + "px";
245
+ style.top = y + "px";
246
+ style.width = width + "px";
247
+ style.height = height + "px";
268
248
  this.isHTMLText || updateStyle(this.editDom, text, textScale);
269
249
  }
270
250
  onUnload() {
271
- const { editTarget: text, editor, editDom: dom } = this;
251
+ const {editTarget: text, editor: editor, editDom: dom} = this;
272
252
  if (text) {
273
253
  this.onInput();
274
- if (text.text === '\n')
275
- text.text = '';
254
+ if (text.text === "\n") text.text = "";
276
255
  text.textEditing = undefined;
277
- if (editor.app)
278
- editor.app.config.keyEvent = this._keyEvent;
256
+ if (editor.app) editor.app.config.keyEvent = this._keyEvent;
279
257
  editor.off_(this.eventIds);
280
258
  dom.removeEventListener("focus", this.onFocus);
281
259
  dom.removeEventListener("input", this.onInput);
282
260
  dom.removeEventListener("paste", this.onPaste);
283
- window.removeEventListener('keydown', this.onKeydown);
284
- window.removeEventListener('scroll', this.onUpdate);
261
+ window.removeEventListener("keydown", this.onKeydown);
262
+ window.removeEventListener("scroll", this.onUpdate);
285
263
  dom.remove();
286
264
  this.editDom = this.eventIds = undefined;
287
265
  }
288
266
  }
289
267
  };
290
- exports.TextEditor = __decorate([
291
- editor.registerInnerEditor()
292
- ], exports.TextEditor);
293
268
 
294
- core.Plugin.add('text-editor', 'editor');
269
+ exports.TextEditor = __decorate([ editor.registerInnerEditor() ], exports.TextEditor);
270
+
271
+ core.Plugin.add("text-editor", "editor");