@leafer-in/text-editor 1.0.0 → 1.0.2
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/dist/text-editor.cjs +213 -1
- package/dist/text-editor.esm.js +213 -1
- package/dist/text-editor.esm.min.js +1 -1
- package/dist/text-editor.js +216 -1
- package/dist/text-editor.min.cjs +1 -1
- package/dist/text-editor.min.js +1 -1
- package/package.json +13 -6
- package/src/TextEditor.ts +152 -0
- package/src/index.ts +1 -0
- package/src/updateStyle.ts +81 -0
- package/types/index.d.ts +2 -0
package/dist/text-editor.cjs
CHANGED
|
@@ -1 +1,213 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@leafer-ui/core');
|
|
4
|
+
var editor = require('@leafer-in/editor');
|
|
5
|
+
|
|
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 */
|
|
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;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const textCaseMap = {
|
|
36
|
+
'none': 'none',
|
|
37
|
+
'title': 'capitalize',
|
|
38
|
+
'upper': 'uppercase',
|
|
39
|
+
'lower': 'lowercase',
|
|
40
|
+
'small-caps': 'small-caps'
|
|
41
|
+
};
|
|
42
|
+
const verticalAlignMap = {
|
|
43
|
+
'top': 'flex-start',
|
|
44
|
+
'middle': 'center',
|
|
45
|
+
'bottom': 'flex-end'
|
|
46
|
+
};
|
|
47
|
+
function updateStyle(textDom, text, textScale) {
|
|
48
|
+
const { style } = textDom;
|
|
49
|
+
const { fill, padding, textWrap, textOverflow, textDecoration } = text;
|
|
50
|
+
style.fontFamily = text.fontFamily;
|
|
51
|
+
style.fontSize = text.fontSize * textScale + 'px';
|
|
52
|
+
setFill(style, fill);
|
|
53
|
+
style.fontStyle = text.italic ? 'italic' : 'normal';
|
|
54
|
+
style.fontWeight = text.fontWeight;
|
|
55
|
+
style.textDecoration = textDecoration === 'delete' ? 'line-through' : textDecoration;
|
|
56
|
+
style.textTransform = textCaseMap[text.textCase];
|
|
57
|
+
style.textAlign = text.textAlign;
|
|
58
|
+
style.display = 'flex';
|
|
59
|
+
style.flexDirection = 'column';
|
|
60
|
+
style.justifyContent = verticalAlignMap[text.verticalAlign];
|
|
61
|
+
style.lineHeight = (text.__.__lineHeight || 0) * textScale + 'px';
|
|
62
|
+
style.letterSpacing = (text.__.__letterSpacing || 0) * textScale + 'px';
|
|
63
|
+
if (textWrap === 'none') {
|
|
64
|
+
style.whiteSpace = 'nowrap';
|
|
65
|
+
}
|
|
66
|
+
else if (textWrap === 'break') {
|
|
67
|
+
style.wordBreak = 'break-all';
|
|
68
|
+
}
|
|
69
|
+
style.textIndent = (text.paraIndent || 0) * textScale + 'px';
|
|
70
|
+
style.padding = padding instanceof Array ? padding.map(item => item * textScale + 'px').join(' ') : (padding || 0) * textScale + 'px';
|
|
71
|
+
style.textOverflow = textOverflow === 'show' ? '' : (textOverflow === 'hide' ? 'clip' : textOverflow);
|
|
72
|
+
}
|
|
73
|
+
function setFill(style, fill) {
|
|
74
|
+
let color = 'black';
|
|
75
|
+
if (fill instanceof Array)
|
|
76
|
+
fill = fill[0];
|
|
77
|
+
if (typeof fill === 'object') {
|
|
78
|
+
switch (fill.type) {
|
|
79
|
+
case 'solid':
|
|
80
|
+
color = core.ColorConvert.string(fill.color);
|
|
81
|
+
break;
|
|
82
|
+
case 'image':
|
|
83
|
+
break;
|
|
84
|
+
case 'linear':
|
|
85
|
+
case 'radial':
|
|
86
|
+
case 'angular':
|
|
87
|
+
const stop = fill.stops[0];
|
|
88
|
+
color = core.ColorConvert.string(typeof stop === 'string' ? stop : stop.color);
|
|
89
|
+
break;
|
|
90
|
+
default:
|
|
91
|
+
if (fill.r !== undefined)
|
|
92
|
+
color = core.ColorConvert.string(fill);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
color = fill;
|
|
97
|
+
}
|
|
98
|
+
style.color = color;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
exports.TextEditor = class TextEditor extends editor.InnerEditor {
|
|
102
|
+
constructor() {
|
|
103
|
+
super(...arguments);
|
|
104
|
+
this.config = {
|
|
105
|
+
selectAll: true
|
|
106
|
+
};
|
|
107
|
+
this.eventIds = [];
|
|
108
|
+
}
|
|
109
|
+
get tag() { return 'TextEditor'; }
|
|
110
|
+
onLoad() {
|
|
111
|
+
const { editor } = this;
|
|
112
|
+
const { config } = editor.app;
|
|
113
|
+
const text = this.editTarget;
|
|
114
|
+
text.visible = false;
|
|
115
|
+
this.isHTMLText = !(text instanceof core.Text);
|
|
116
|
+
this._keyEvent = config.keyEvent;
|
|
117
|
+
config.keyEvent = false;
|
|
118
|
+
const div = this.editDom = document.createElement('div');
|
|
119
|
+
const { style } = div;
|
|
120
|
+
div.contentEditable = 'true';
|
|
121
|
+
style.position = 'fixed';
|
|
122
|
+
style.transformOrigin = 'left top';
|
|
123
|
+
style.boxSizing = 'border-box';
|
|
124
|
+
if (this.isHTMLText) {
|
|
125
|
+
div.innerHTML = text.text;
|
|
126
|
+
this.textScale = 1;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
div.innerText = text.text;
|
|
130
|
+
const { scaleX, scaleY } = text.worldTransform;
|
|
131
|
+
this.textScale = Math.max(Math.abs(scaleX), Math.abs(scaleY));
|
|
132
|
+
const fontSize = text.fontSize * this.textScale;
|
|
133
|
+
if (fontSize < 12)
|
|
134
|
+
this.textScale *= 12 / fontSize;
|
|
135
|
+
}
|
|
136
|
+
const { view } = editor.app;
|
|
137
|
+
(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
|
+
];
|
|
150
|
+
this.onFocus = this.onFocus.bind(this);
|
|
151
|
+
this.onInput = this.onInput.bind(this);
|
|
152
|
+
this.onUpdate = this.onUpdate.bind(this);
|
|
153
|
+
this.onEscape = this.onEscape.bind(this);
|
|
154
|
+
div.addEventListener("focus", this.onFocus);
|
|
155
|
+
div.addEventListener("input", this.onInput);
|
|
156
|
+
window.addEventListener('keydown', this.onEscape);
|
|
157
|
+
window.addEventListener('scroll', this.onUpdate);
|
|
158
|
+
const selection = window.getSelection();
|
|
159
|
+
const range = document.createRange();
|
|
160
|
+
if (this.config.selectAll) {
|
|
161
|
+
range.selectNodeContents(div);
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
const node = div.childNodes[0];
|
|
165
|
+
range.setStartAfter(node);
|
|
166
|
+
range.setEndAfter(node);
|
|
167
|
+
range.collapse(true);
|
|
168
|
+
}
|
|
169
|
+
selection.removeAllRanges();
|
|
170
|
+
selection.addRange(range);
|
|
171
|
+
}
|
|
172
|
+
onInput() {
|
|
173
|
+
const { editDom } = this;
|
|
174
|
+
this.editTarget.text = this.isHTMLText ? editDom.innerHTML : editDom.innerText.replace(/\n\n/, '\n');
|
|
175
|
+
}
|
|
176
|
+
onFocus() {
|
|
177
|
+
this.editDom.style.outline = 'none';
|
|
178
|
+
}
|
|
179
|
+
onEscape(e) {
|
|
180
|
+
if (e.code === 'Escape')
|
|
181
|
+
this.editor.closeInnerEditor();
|
|
182
|
+
}
|
|
183
|
+
onUpdate() {
|
|
184
|
+
const { editTarget: text, textScale } = this;
|
|
185
|
+
const { style } = this.editDom;
|
|
186
|
+
const { x, y } = this.inBody ? text.app.clientBounds : text.app.tree.clientBounds;
|
|
187
|
+
const { a, b, c, d, e, f } = new core.Matrix(text.worldTransform).scale(1 / textScale);
|
|
188
|
+
style.transform = `matrix(${a},${b},${c},${d},${e},${f})`;
|
|
189
|
+
style.left = x - window.scrollX + 'px';
|
|
190
|
+
style.top = y - window.scrollY + 'px';
|
|
191
|
+
style.width = text.width * textScale + (text.__.__autoWidth ? 20 : 0) + 'px';
|
|
192
|
+
style.height = text.height * textScale + (text.__.__autoHeight ? 20 : 0) + 'px';
|
|
193
|
+
this.isHTMLText || updateStyle(this.editDom, text, this.textScale);
|
|
194
|
+
}
|
|
195
|
+
onUnload() {
|
|
196
|
+
const { editTarget: text, editor, editDom: dom } = this;
|
|
197
|
+
if (text) {
|
|
198
|
+
this.onInput();
|
|
199
|
+
text.visible = true;
|
|
200
|
+
editor.app.config.keyEvent = this._keyEvent;
|
|
201
|
+
editor.off_(this.eventIds);
|
|
202
|
+
dom.removeEventListener("focus", this.onFocus);
|
|
203
|
+
dom.removeEventListener("input", this.onInput);
|
|
204
|
+
window.removeEventListener('keydown', this.onEscape);
|
|
205
|
+
window.removeEventListener('scroll', this.onUpdate);
|
|
206
|
+
dom.remove();
|
|
207
|
+
this.editDom = this.eventIds = undefined;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
exports.TextEditor = __decorate([
|
|
212
|
+
editor.registerInnerEditor()
|
|
213
|
+
], exports.TextEditor);
|
package/dist/text-editor.esm.js
CHANGED
|
@@ -1 +1,213 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import { ColorConvert, Text, PointerEvent, Matrix } from '@leafer-ui/core';
|
|
2
|
+
import { registerInnerEditor, InnerEditor } from '@leafer-in/editor';
|
|
3
|
+
|
|
4
|
+
/******************************************************************************
|
|
5
|
+
Copyright (c) Microsoft Corporation.
|
|
6
|
+
|
|
7
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
8
|
+
purpose with or without fee is hereby granted.
|
|
9
|
+
|
|
10
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
11
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
12
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
13
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
14
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
15
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
16
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
17
|
+
***************************************************************************** */
|
|
18
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
function __decorate(decorators, target, key, desc) {
|
|
22
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
23
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
24
|
+
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;
|
|
25
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
29
|
+
var e = new Error(message);
|
|
30
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const textCaseMap = {
|
|
34
|
+
'none': 'none',
|
|
35
|
+
'title': 'capitalize',
|
|
36
|
+
'upper': 'uppercase',
|
|
37
|
+
'lower': 'lowercase',
|
|
38
|
+
'small-caps': 'small-caps'
|
|
39
|
+
};
|
|
40
|
+
const verticalAlignMap = {
|
|
41
|
+
'top': 'flex-start',
|
|
42
|
+
'middle': 'center',
|
|
43
|
+
'bottom': 'flex-end'
|
|
44
|
+
};
|
|
45
|
+
function updateStyle(textDom, text, textScale) {
|
|
46
|
+
const { style } = textDom;
|
|
47
|
+
const { fill, padding, textWrap, textOverflow, textDecoration } = text;
|
|
48
|
+
style.fontFamily = text.fontFamily;
|
|
49
|
+
style.fontSize = text.fontSize * textScale + 'px';
|
|
50
|
+
setFill(style, fill);
|
|
51
|
+
style.fontStyle = text.italic ? 'italic' : 'normal';
|
|
52
|
+
style.fontWeight = text.fontWeight;
|
|
53
|
+
style.textDecoration = textDecoration === 'delete' ? 'line-through' : textDecoration;
|
|
54
|
+
style.textTransform = textCaseMap[text.textCase];
|
|
55
|
+
style.textAlign = text.textAlign;
|
|
56
|
+
style.display = 'flex';
|
|
57
|
+
style.flexDirection = 'column';
|
|
58
|
+
style.justifyContent = verticalAlignMap[text.verticalAlign];
|
|
59
|
+
style.lineHeight = (text.__.__lineHeight || 0) * textScale + 'px';
|
|
60
|
+
style.letterSpacing = (text.__.__letterSpacing || 0) * textScale + 'px';
|
|
61
|
+
if (textWrap === 'none') {
|
|
62
|
+
style.whiteSpace = 'nowrap';
|
|
63
|
+
}
|
|
64
|
+
else if (textWrap === 'break') {
|
|
65
|
+
style.wordBreak = 'break-all';
|
|
66
|
+
}
|
|
67
|
+
style.textIndent = (text.paraIndent || 0) * textScale + 'px';
|
|
68
|
+
style.padding = padding instanceof Array ? padding.map(item => item * textScale + 'px').join(' ') : (padding || 0) * textScale + 'px';
|
|
69
|
+
style.textOverflow = textOverflow === 'show' ? '' : (textOverflow === 'hide' ? 'clip' : textOverflow);
|
|
70
|
+
}
|
|
71
|
+
function setFill(style, fill) {
|
|
72
|
+
let color = 'black';
|
|
73
|
+
if (fill instanceof Array)
|
|
74
|
+
fill = fill[0];
|
|
75
|
+
if (typeof fill === 'object') {
|
|
76
|
+
switch (fill.type) {
|
|
77
|
+
case 'solid':
|
|
78
|
+
color = ColorConvert.string(fill.color);
|
|
79
|
+
break;
|
|
80
|
+
case 'image':
|
|
81
|
+
break;
|
|
82
|
+
case 'linear':
|
|
83
|
+
case 'radial':
|
|
84
|
+
case 'angular':
|
|
85
|
+
const stop = fill.stops[0];
|
|
86
|
+
color = ColorConvert.string(typeof stop === 'string' ? stop : stop.color);
|
|
87
|
+
break;
|
|
88
|
+
default:
|
|
89
|
+
if (fill.r !== undefined)
|
|
90
|
+
color = ColorConvert.string(fill);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
color = fill;
|
|
95
|
+
}
|
|
96
|
+
style.color = color;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
let TextEditor = class TextEditor extends InnerEditor {
|
|
100
|
+
constructor() {
|
|
101
|
+
super(...arguments);
|
|
102
|
+
this.config = {
|
|
103
|
+
selectAll: true
|
|
104
|
+
};
|
|
105
|
+
this.eventIds = [];
|
|
106
|
+
}
|
|
107
|
+
get tag() { return 'TextEditor'; }
|
|
108
|
+
onLoad() {
|
|
109
|
+
const { editor } = this;
|
|
110
|
+
const { config } = editor.app;
|
|
111
|
+
const text = this.editTarget;
|
|
112
|
+
text.visible = false;
|
|
113
|
+
this.isHTMLText = !(text instanceof Text);
|
|
114
|
+
this._keyEvent = config.keyEvent;
|
|
115
|
+
config.keyEvent = false;
|
|
116
|
+
const div = this.editDom = document.createElement('div');
|
|
117
|
+
const { style } = div;
|
|
118
|
+
div.contentEditable = 'true';
|
|
119
|
+
style.position = 'fixed';
|
|
120
|
+
style.transformOrigin = 'left top';
|
|
121
|
+
style.boxSizing = 'border-box';
|
|
122
|
+
if (this.isHTMLText) {
|
|
123
|
+
div.innerHTML = text.text;
|
|
124
|
+
this.textScale = 1;
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
div.innerText = text.text;
|
|
128
|
+
const { scaleX, scaleY } = text.worldTransform;
|
|
129
|
+
this.textScale = Math.max(Math.abs(scaleX), Math.abs(scaleY));
|
|
130
|
+
const fontSize = text.fontSize * this.textScale;
|
|
131
|
+
if (fontSize < 12)
|
|
132
|
+
this.textScale *= 12 / fontSize;
|
|
133
|
+
}
|
|
134
|
+
const { view } = editor.app;
|
|
135
|
+
(this.inBody = view instanceof HTMLCanvasElement) ? document.body.appendChild(div) : view.appendChild(div);
|
|
136
|
+
this.eventIds = [
|
|
137
|
+
editor.app.on_(PointerEvent.DOWN, (e) => {
|
|
138
|
+
let { target } = e.origin, find;
|
|
139
|
+
while (target) {
|
|
140
|
+
if (target === div)
|
|
141
|
+
find = true;
|
|
142
|
+
target = target.parentElement;
|
|
143
|
+
}
|
|
144
|
+
if (!find)
|
|
145
|
+
editor.closeInnerEditor();
|
|
146
|
+
})
|
|
147
|
+
];
|
|
148
|
+
this.onFocus = this.onFocus.bind(this);
|
|
149
|
+
this.onInput = this.onInput.bind(this);
|
|
150
|
+
this.onUpdate = this.onUpdate.bind(this);
|
|
151
|
+
this.onEscape = this.onEscape.bind(this);
|
|
152
|
+
div.addEventListener("focus", this.onFocus);
|
|
153
|
+
div.addEventListener("input", this.onInput);
|
|
154
|
+
window.addEventListener('keydown', this.onEscape);
|
|
155
|
+
window.addEventListener('scroll', this.onUpdate);
|
|
156
|
+
const selection = window.getSelection();
|
|
157
|
+
const range = document.createRange();
|
|
158
|
+
if (this.config.selectAll) {
|
|
159
|
+
range.selectNodeContents(div);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
const node = div.childNodes[0];
|
|
163
|
+
range.setStartAfter(node);
|
|
164
|
+
range.setEndAfter(node);
|
|
165
|
+
range.collapse(true);
|
|
166
|
+
}
|
|
167
|
+
selection.removeAllRanges();
|
|
168
|
+
selection.addRange(range);
|
|
169
|
+
}
|
|
170
|
+
onInput() {
|
|
171
|
+
const { editDom } = this;
|
|
172
|
+
this.editTarget.text = this.isHTMLText ? editDom.innerHTML : editDom.innerText.replace(/\n\n/, '\n');
|
|
173
|
+
}
|
|
174
|
+
onFocus() {
|
|
175
|
+
this.editDom.style.outline = 'none';
|
|
176
|
+
}
|
|
177
|
+
onEscape(e) {
|
|
178
|
+
if (e.code === 'Escape')
|
|
179
|
+
this.editor.closeInnerEditor();
|
|
180
|
+
}
|
|
181
|
+
onUpdate() {
|
|
182
|
+
const { editTarget: text, textScale } = this;
|
|
183
|
+
const { style } = this.editDom;
|
|
184
|
+
const { x, y } = this.inBody ? text.app.clientBounds : text.app.tree.clientBounds;
|
|
185
|
+
const { a, b, c, d, e, f } = new Matrix(text.worldTransform).scale(1 / textScale);
|
|
186
|
+
style.transform = `matrix(${a},${b},${c},${d},${e},${f})`;
|
|
187
|
+
style.left = x - window.scrollX + 'px';
|
|
188
|
+
style.top = y - window.scrollY + 'px';
|
|
189
|
+
style.width = text.width * textScale + (text.__.__autoWidth ? 20 : 0) + 'px';
|
|
190
|
+
style.height = text.height * textScale + (text.__.__autoHeight ? 20 : 0) + 'px';
|
|
191
|
+
this.isHTMLText || updateStyle(this.editDom, text, this.textScale);
|
|
192
|
+
}
|
|
193
|
+
onUnload() {
|
|
194
|
+
const { editTarget: text, editor, editDom: dom } = this;
|
|
195
|
+
if (text) {
|
|
196
|
+
this.onInput();
|
|
197
|
+
text.visible = true;
|
|
198
|
+
editor.app.config.keyEvent = this._keyEvent;
|
|
199
|
+
editor.off_(this.eventIds);
|
|
200
|
+
dom.removeEventListener("focus", this.onFocus);
|
|
201
|
+
dom.removeEventListener("input", this.onInput);
|
|
202
|
+
window.removeEventListener('keydown', this.onEscape);
|
|
203
|
+
window.removeEventListener('scroll', this.onUpdate);
|
|
204
|
+
dom.remove();
|
|
205
|
+
this.editDom = this.eventIds = undefined;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
TextEditor = __decorate([
|
|
210
|
+
registerInnerEditor()
|
|
211
|
+
], TextEditor);
|
|
212
|
+
|
|
213
|
+
export { TextEditor };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{ColorConvert as e,Text as t,PointerEvent as n,Matrix as i}from"@leafer-ui/core";import{registerInnerEditor as o,InnerEditor as s}from"@leafer-in/editor";"function"==typeof SuppressedError&&SuppressedError;const r={none:"none",title:"capitalize",upper:"uppercase",lower:"lowercase","small-caps":"small-caps"},a={top:"flex-start",middle:"center",bottom:"flex-end"};function l(t,n,i){const{style:o}=t,{fill:s,padding:l,textWrap:c,textOverflow:d,textDecoration:p}=n;o.fontFamily=n.fontFamily,o.fontSize=n.fontSize*i+"px",function(t,n){let i="black";n instanceof Array&&(n=n[0]);if("object"==typeof n)switch(n.type){case"solid":i=e.string(n.color);break;case"image":break;case"linear":case"radial":case"angular":const t=n.stops[0];i=e.string("string"==typeof t?t:t.color);break;default:void 0!==n.r&&(i=e.string(n))}else i=n;t.color=i}(o,s),o.fontStyle=n.italic?"italic":"normal",o.fontWeight=n.fontWeight,o.textDecoration="delete"===p?"line-through":p,o.textTransform=r[n.textCase],o.textAlign=n.textAlign,o.display="flex",o.flexDirection="column",o.justifyContent=a[n.verticalAlign],o.lineHeight=(n.__.__lineHeight||0)*i+"px",o.letterSpacing=(n.__.__letterSpacing||0)*i+"px","none"===c?o.whiteSpace="nowrap":"break"===c&&(o.wordBreak="break-all"),o.textIndent=(n.paraIndent||0)*i+"px",o.padding=l instanceof Array?l.map((e=>e*i+"px")).join(" "):(l||0)*i+"px",o.textOverflow="show"===d?"":"hide"===d?"clip":d}let c=class extends s{constructor(){super(...arguments),this.config={selectAll:!0},this.eventIds=[]}get tag(){return"TextEditor"}onLoad(){const{editor:e}=this,{config:i}=e.app,o=this.editTarget;o.visible=!1,this.isHTMLText=!(o instanceof t),this._keyEvent=i.keyEvent,i.keyEvent=!1;const s=this.editDom=document.createElement("div"),{style:r}=s;if(s.contentEditable="true",r.position="fixed",r.transformOrigin="left top",r.boxSizing="border-box",this.isHTMLText)s.innerHTML=o.text,this.textScale=1;else{s.innerText=o.text;const{scaleX:e,scaleY:t}=o.worldTransform;this.textScale=Math.max(Math.abs(e),Math.abs(t));const n=o.fontSize*this.textScale;n<12&&(this.textScale*=12/n)}const{view:a}=e.app;(this.inBody=a instanceof HTMLCanvasElement)?document.body.appendChild(s):a.appendChild(s),this.eventIds=[e.app.on_(n.DOWN,(t=>{let n,{target:i}=t.origin;for(;i;)i===s&&(n=!0),i=i.parentElement;n||e.closeInnerEditor()}))],this.onFocus=this.onFocus.bind(this),this.onInput=this.onInput.bind(this),this.onUpdate=this.onUpdate.bind(this),this.onEscape=this.onEscape.bind(this),s.addEventListener("focus",this.onFocus),s.addEventListener("input",this.onInput),window.addEventListener("keydown",this.onEscape),window.addEventListener("scroll",this.onUpdate);const l=window.getSelection(),c=document.createRange();if(this.config.selectAll)c.selectNodeContents(s);else{const e=s.childNodes[0];c.setStartAfter(e),c.setEndAfter(e),c.collapse(!0)}l.removeAllRanges(),l.addRange(c)}onInput(){const{editDom:e}=this;this.editTarget.text=this.isHTMLText?e.innerHTML:e.innerText.replace(/\n\n/,"\n")}onFocus(){this.editDom.style.outline="none"}onEscape(e){"Escape"===e.code&&this.editor.closeInnerEditor()}onUpdate(){const{editTarget:e,textScale:t}=this,{style:n}=this.editDom,{x:o,y:s}=this.inBody?e.app.clientBounds:e.app.tree.clientBounds,{a:r,b:a,c:c,d:d,e:p,f:h}=new i(e.worldTransform).scale(1/t);n.transform=`matrix(${r},${a},${c},${d},${p},${h})`,n.left=o-window.scrollX+"px",n.top=s-window.scrollY+"px",n.width=e.width*t+(e.__.__autoWidth?20:0)+"px",n.height=e.height*t+(e.__.__autoHeight?20:0)+"px",this.isHTMLText||l(this.editDom,e,this.textScale)}onUnload(){const{editTarget:e,editor:t,editDom:n}=this;e&&(this.onInput(),e.visible=!0,t.app.config.keyEvent=this._keyEvent,t.off_(this.eventIds),n.removeEventListener("focus",this.onFocus),n.removeEventListener("input",this.onInput),window.removeEventListener("keydown",this.onEscape),window.removeEventListener("scroll",this.onUpdate),n.remove(),this.editDom=this.eventIds=void 0)}};c=function(e,t,n,i){var o,s=arguments.length,r=s<3?t:null===i?i=Object.getOwnPropertyDescriptor(t,n):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(e,t,n,i);else for(var a=e.length-1;a>=0;a--)(o=e[a])&&(r=(s<3?o(r):s>3?o(t,n,r):o(t,n))||r);return s>3&&r&&Object.defineProperty(t,n,r),r}([o()],c);export{c as TextEditor};
|
package/dist/text-editor.js
CHANGED
|
@@ -1 +1,216 @@
|
|
|
1
|
-
this.LeaferIN
|
|
1
|
+
this.LeaferIN = this.LeaferIN || {};
|
|
2
|
+
this.LeaferIN.textEditor = (function (exports, core, editor) {
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/******************************************************************************
|
|
6
|
+
Copyright (c) Microsoft Corporation.
|
|
7
|
+
|
|
8
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
9
|
+
purpose with or without fee is hereby granted.
|
|
10
|
+
|
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
12
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
13
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
14
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
15
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
16
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
17
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
18
|
+
***************************************************************************** */
|
|
19
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
function __decorate(decorators, target, key, desc) {
|
|
23
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
24
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
25
|
+
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;
|
|
26
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
30
|
+
var e = new Error(message);
|
|
31
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const textCaseMap = {
|
|
35
|
+
'none': 'none',
|
|
36
|
+
'title': 'capitalize',
|
|
37
|
+
'upper': 'uppercase',
|
|
38
|
+
'lower': 'lowercase',
|
|
39
|
+
'small-caps': 'small-caps'
|
|
40
|
+
};
|
|
41
|
+
const verticalAlignMap = {
|
|
42
|
+
'top': 'flex-start',
|
|
43
|
+
'middle': 'center',
|
|
44
|
+
'bottom': 'flex-end'
|
|
45
|
+
};
|
|
46
|
+
function updateStyle(textDom, text, textScale) {
|
|
47
|
+
const { style } = textDom;
|
|
48
|
+
const { fill, padding, textWrap, textOverflow, textDecoration } = text;
|
|
49
|
+
style.fontFamily = text.fontFamily;
|
|
50
|
+
style.fontSize = text.fontSize * textScale + 'px';
|
|
51
|
+
setFill(style, fill);
|
|
52
|
+
style.fontStyle = text.italic ? 'italic' : 'normal';
|
|
53
|
+
style.fontWeight = text.fontWeight;
|
|
54
|
+
style.textDecoration = textDecoration === 'delete' ? 'line-through' : textDecoration;
|
|
55
|
+
style.textTransform = textCaseMap[text.textCase];
|
|
56
|
+
style.textAlign = text.textAlign;
|
|
57
|
+
style.display = 'flex';
|
|
58
|
+
style.flexDirection = 'column';
|
|
59
|
+
style.justifyContent = verticalAlignMap[text.verticalAlign];
|
|
60
|
+
style.lineHeight = (text.__.__lineHeight || 0) * textScale + 'px';
|
|
61
|
+
style.letterSpacing = (text.__.__letterSpacing || 0) * textScale + 'px';
|
|
62
|
+
if (textWrap === 'none') {
|
|
63
|
+
style.whiteSpace = 'nowrap';
|
|
64
|
+
}
|
|
65
|
+
else if (textWrap === 'break') {
|
|
66
|
+
style.wordBreak = 'break-all';
|
|
67
|
+
}
|
|
68
|
+
style.textIndent = (text.paraIndent || 0) * textScale + 'px';
|
|
69
|
+
style.padding = padding instanceof Array ? padding.map(item => item * textScale + 'px').join(' ') : (padding || 0) * textScale + 'px';
|
|
70
|
+
style.textOverflow = textOverflow === 'show' ? '' : (textOverflow === 'hide' ? 'clip' : textOverflow);
|
|
71
|
+
}
|
|
72
|
+
function setFill(style, fill) {
|
|
73
|
+
let color = 'black';
|
|
74
|
+
if (fill instanceof Array)
|
|
75
|
+
fill = fill[0];
|
|
76
|
+
if (typeof fill === 'object') {
|
|
77
|
+
switch (fill.type) {
|
|
78
|
+
case 'solid':
|
|
79
|
+
color = core.ColorConvert.string(fill.color);
|
|
80
|
+
break;
|
|
81
|
+
case 'image':
|
|
82
|
+
break;
|
|
83
|
+
case 'linear':
|
|
84
|
+
case 'radial':
|
|
85
|
+
case 'angular':
|
|
86
|
+
const stop = fill.stops[0];
|
|
87
|
+
color = core.ColorConvert.string(typeof stop === 'string' ? stop : stop.color);
|
|
88
|
+
break;
|
|
89
|
+
default:
|
|
90
|
+
if (fill.r !== undefined)
|
|
91
|
+
color = core.ColorConvert.string(fill);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
color = fill;
|
|
96
|
+
}
|
|
97
|
+
style.color = color;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
exports.TextEditor = class TextEditor extends editor.InnerEditor {
|
|
101
|
+
constructor() {
|
|
102
|
+
super(...arguments);
|
|
103
|
+
this.config = {
|
|
104
|
+
selectAll: true
|
|
105
|
+
};
|
|
106
|
+
this.eventIds = [];
|
|
107
|
+
}
|
|
108
|
+
get tag() { return 'TextEditor'; }
|
|
109
|
+
onLoad() {
|
|
110
|
+
const { editor } = this;
|
|
111
|
+
const { config } = editor.app;
|
|
112
|
+
const text = this.editTarget;
|
|
113
|
+
text.visible = false;
|
|
114
|
+
this.isHTMLText = !(text instanceof core.Text);
|
|
115
|
+
this._keyEvent = config.keyEvent;
|
|
116
|
+
config.keyEvent = false;
|
|
117
|
+
const div = this.editDom = document.createElement('div');
|
|
118
|
+
const { style } = div;
|
|
119
|
+
div.contentEditable = 'true';
|
|
120
|
+
style.position = 'fixed';
|
|
121
|
+
style.transformOrigin = 'left top';
|
|
122
|
+
style.boxSizing = 'border-box';
|
|
123
|
+
if (this.isHTMLText) {
|
|
124
|
+
div.innerHTML = text.text;
|
|
125
|
+
this.textScale = 1;
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
div.innerText = text.text;
|
|
129
|
+
const { scaleX, scaleY } = text.worldTransform;
|
|
130
|
+
this.textScale = Math.max(Math.abs(scaleX), Math.abs(scaleY));
|
|
131
|
+
const fontSize = text.fontSize * this.textScale;
|
|
132
|
+
if (fontSize < 12)
|
|
133
|
+
this.textScale *= 12 / fontSize;
|
|
134
|
+
}
|
|
135
|
+
const { view } = editor.app;
|
|
136
|
+
(this.inBody = view instanceof HTMLCanvasElement) ? document.body.appendChild(div) : view.appendChild(div);
|
|
137
|
+
this.eventIds = [
|
|
138
|
+
editor.app.on_(core.PointerEvent.DOWN, (e) => {
|
|
139
|
+
let { target } = e.origin, find;
|
|
140
|
+
while (target) {
|
|
141
|
+
if (target === div)
|
|
142
|
+
find = true;
|
|
143
|
+
target = target.parentElement;
|
|
144
|
+
}
|
|
145
|
+
if (!find)
|
|
146
|
+
editor.closeInnerEditor();
|
|
147
|
+
})
|
|
148
|
+
];
|
|
149
|
+
this.onFocus = this.onFocus.bind(this);
|
|
150
|
+
this.onInput = this.onInput.bind(this);
|
|
151
|
+
this.onUpdate = this.onUpdate.bind(this);
|
|
152
|
+
this.onEscape = this.onEscape.bind(this);
|
|
153
|
+
div.addEventListener("focus", this.onFocus);
|
|
154
|
+
div.addEventListener("input", this.onInput);
|
|
155
|
+
window.addEventListener('keydown', this.onEscape);
|
|
156
|
+
window.addEventListener('scroll', this.onUpdate);
|
|
157
|
+
const selection = window.getSelection();
|
|
158
|
+
const range = document.createRange();
|
|
159
|
+
if (this.config.selectAll) {
|
|
160
|
+
range.selectNodeContents(div);
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
const node = div.childNodes[0];
|
|
164
|
+
range.setStartAfter(node);
|
|
165
|
+
range.setEndAfter(node);
|
|
166
|
+
range.collapse(true);
|
|
167
|
+
}
|
|
168
|
+
selection.removeAllRanges();
|
|
169
|
+
selection.addRange(range);
|
|
170
|
+
}
|
|
171
|
+
onInput() {
|
|
172
|
+
const { editDom } = this;
|
|
173
|
+
this.editTarget.text = this.isHTMLText ? editDom.innerHTML : editDom.innerText.replace(/\n\n/, '\n');
|
|
174
|
+
}
|
|
175
|
+
onFocus() {
|
|
176
|
+
this.editDom.style.outline = 'none';
|
|
177
|
+
}
|
|
178
|
+
onEscape(e) {
|
|
179
|
+
if (e.code === 'Escape')
|
|
180
|
+
this.editor.closeInnerEditor();
|
|
181
|
+
}
|
|
182
|
+
onUpdate() {
|
|
183
|
+
const { editTarget: text, textScale } = this;
|
|
184
|
+
const { style } = this.editDom;
|
|
185
|
+
const { x, y } = this.inBody ? text.app.clientBounds : text.app.tree.clientBounds;
|
|
186
|
+
const { a, b, c, d, e, f } = new core.Matrix(text.worldTransform).scale(1 / textScale);
|
|
187
|
+
style.transform = `matrix(${a},${b},${c},${d},${e},${f})`;
|
|
188
|
+
style.left = x - window.scrollX + 'px';
|
|
189
|
+
style.top = y - window.scrollY + 'px';
|
|
190
|
+
style.width = text.width * textScale + (text.__.__autoWidth ? 20 : 0) + 'px';
|
|
191
|
+
style.height = text.height * textScale + (text.__.__autoHeight ? 20 : 0) + 'px';
|
|
192
|
+
this.isHTMLText || updateStyle(this.editDom, text, this.textScale);
|
|
193
|
+
}
|
|
194
|
+
onUnload() {
|
|
195
|
+
const { editTarget: text, editor, editDom: dom } = this;
|
|
196
|
+
if (text) {
|
|
197
|
+
this.onInput();
|
|
198
|
+
text.visible = true;
|
|
199
|
+
editor.app.config.keyEvent = this._keyEvent;
|
|
200
|
+
editor.off_(this.eventIds);
|
|
201
|
+
dom.removeEventListener("focus", this.onFocus);
|
|
202
|
+
dom.removeEventListener("input", this.onInput);
|
|
203
|
+
window.removeEventListener('keydown', this.onEscape);
|
|
204
|
+
window.removeEventListener('scroll', this.onUpdate);
|
|
205
|
+
dom.remove();
|
|
206
|
+
this.editDom = this.eventIds = undefined;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
exports.TextEditor = __decorate([
|
|
211
|
+
editor.registerInnerEditor()
|
|
212
|
+
], exports.TextEditor);
|
|
213
|
+
|
|
214
|
+
return exports;
|
|
215
|
+
|
|
216
|
+
})({}, LeaferUI, LeaferIN.editor);
|
package/dist/text-editor.min.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("@leafer-ui/core"),t=require("@leafer-in/editor");"function"==typeof SuppressedError&&SuppressedError;const n={none:"none",title:"capitalize",upper:"uppercase",lower:"lowercase","small-caps":"small-caps"},
|
|
1
|
+
"use strict";var e=require("@leafer-ui/core"),t=require("@leafer-in/editor");"function"==typeof SuppressedError&&SuppressedError;const n={none:"none",title:"capitalize",upper:"uppercase",lower:"lowercase","small-caps":"small-caps"},i={top:"flex-start",middle:"center",bottom:"flex-end"};function o(t,o,s){const{style:r}=t,{fill:a,padding:l,textWrap:c,textOverflow:d,textDecoration:p}=o;r.fontFamily=o.fontFamily,r.fontSize=o.fontSize*s+"px",function(t,n){let i="black";n instanceof Array&&(n=n[0]);if("object"==typeof n)switch(n.type){case"solid":i=e.ColorConvert.string(n.color);break;case"image":break;case"linear":case"radial":case"angular":const t=n.stops[0];i=e.ColorConvert.string("string"==typeof t?t:t.color);break;default:void 0!==n.r&&(i=e.ColorConvert.string(n))}else i=n;t.color=i}(r,a),r.fontStyle=o.italic?"italic":"normal",r.fontWeight=o.fontWeight,r.textDecoration="delete"===p?"line-through":p,r.textTransform=n[o.textCase],r.textAlign=o.textAlign,r.display="flex",r.flexDirection="column",r.justifyContent=i[o.verticalAlign],r.lineHeight=(o.__.__lineHeight||0)*s+"px",r.letterSpacing=(o.__.__letterSpacing||0)*s+"px","none"===c?r.whiteSpace="nowrap":"break"===c&&(r.wordBreak="break-all"),r.textIndent=(o.paraIndent||0)*s+"px",r.padding=l instanceof Array?l.map((e=>e*s+"px")).join(" "):(l||0)*s+"px",r.textOverflow="show"===d?"":"hide"===d?"clip":d}exports.TextEditor=class extends t.InnerEditor{constructor(){super(...arguments),this.config={selectAll:!0},this.eventIds=[]}get tag(){return"TextEditor"}onLoad(){const{editor:t}=this,{config:n}=t.app,i=this.editTarget;i.visible=!1,this.isHTMLText=!(i instanceof e.Text),this._keyEvent=n.keyEvent,n.keyEvent=!1;const o=this.editDom=document.createElement("div"),{style:s}=o;if(o.contentEditable="true",s.position="fixed",s.transformOrigin="left top",s.boxSizing="border-box",this.isHTMLText)o.innerHTML=i.text,this.textScale=1;else{o.innerText=i.text;const{scaleX:e,scaleY:t}=i.worldTransform;this.textScale=Math.max(Math.abs(e),Math.abs(t));const n=i.fontSize*this.textScale;n<12&&(this.textScale*=12/n)}const{view:r}=t.app;(this.inBody=r instanceof HTMLCanvasElement)?document.body.appendChild(o):r.appendChild(o),this.eventIds=[t.app.on_(e.PointerEvent.DOWN,(e=>{let n,{target:i}=e.origin;for(;i;)i===o&&(n=!0),i=i.parentElement;n||t.closeInnerEditor()}))],this.onFocus=this.onFocus.bind(this),this.onInput=this.onInput.bind(this),this.onUpdate=this.onUpdate.bind(this),this.onEscape=this.onEscape.bind(this),o.addEventListener("focus",this.onFocus),o.addEventListener("input",this.onInput),window.addEventListener("keydown",this.onEscape),window.addEventListener("scroll",this.onUpdate);const a=window.getSelection(),l=document.createRange();if(this.config.selectAll)l.selectNodeContents(o);else{const e=o.childNodes[0];l.setStartAfter(e),l.setEndAfter(e),l.collapse(!0)}a.removeAllRanges(),a.addRange(l)}onInput(){const{editDom:e}=this;this.editTarget.text=this.isHTMLText?e.innerHTML:e.innerText.replace(/\n\n/,"\n")}onFocus(){this.editDom.style.outline="none"}onEscape(e){"Escape"===e.code&&this.editor.closeInnerEditor()}onUpdate(){const{editTarget:t,textScale:n}=this,{style:i}=this.editDom,{x:s,y:r}=this.inBody?t.app.clientBounds:t.app.tree.clientBounds,{a:a,b:l,c:c,d:d,e:p,f:h}=new e.Matrix(t.worldTransform).scale(1/n);i.transform=`matrix(${a},${l},${c},${d},${p},${h})`,i.left=s-window.scrollX+"px",i.top=r-window.scrollY+"px",i.width=t.width*n+(t.__.__autoWidth?20:0)+"px",i.height=t.height*n+(t.__.__autoHeight?20:0)+"px",this.isHTMLText||o(this.editDom,t,this.textScale)}onUnload(){const{editTarget:e,editor:t,editDom:n}=this;e&&(this.onInput(),e.visible=!0,t.app.config.keyEvent=this._keyEvent,t.off_(this.eventIds),n.removeEventListener("focus",this.onFocus),n.removeEventListener("input",this.onInput),window.removeEventListener("keydown",this.onEscape),window.removeEventListener("scroll",this.onUpdate),n.remove(),this.editDom=this.eventIds=void 0)}},exports.TextEditor=function(e,t,n,i){var o,s=arguments.length,r=s<3?t:null===i?i=Object.getOwnPropertyDescriptor(t,n):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(e,t,n,i);else for(var a=e.length-1;a>=0;a--)(o=e[a])&&(r=(s<3?o(r):s>3?o(t,n,r):o(t,n))||r);return s>3&&r&&Object.defineProperty(t,n,r),r}([t.registerInnerEditor()],exports.TextEditor);
|
package/dist/text-editor.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
this.LeaferIN=this.LeaferIN||{},this.LeaferIN.textEditor=function(e,
|
|
1
|
+
this.LeaferIN=this.LeaferIN||{},this.LeaferIN.textEditor=function(t,e,n){"use strict";"function"==typeof SuppressedError&&SuppressedError;const i={none:"none",title:"capitalize",upper:"uppercase",lower:"lowercase","small-caps":"small-caps"},o={top:"flex-start",middle:"center",bottom:"flex-end"};function s(t,n,s){const{style:r}=t,{fill:a,padding:l,textWrap:c,textOverflow:d,textDecoration:p}=n;r.fontFamily=n.fontFamily,r.fontSize=n.fontSize*s+"px",function(t,n){let i="black";n instanceof Array&&(n=n[0]);if("object"==typeof n)switch(n.type){case"solid":i=e.ColorConvert.string(n.color);break;case"image":break;case"linear":case"radial":case"angular":const t=n.stops[0];i=e.ColorConvert.string("string"==typeof t?t:t.color);break;default:void 0!==n.r&&(i=e.ColorConvert.string(n))}else i=n;t.color=i}(r,a),r.fontStyle=n.italic?"italic":"normal",r.fontWeight=n.fontWeight,r.textDecoration="delete"===p?"line-through":p,r.textTransform=i[n.textCase],r.textAlign=n.textAlign,r.display="flex",r.flexDirection="column",r.justifyContent=o[n.verticalAlign],r.lineHeight=(n.__.__lineHeight||0)*s+"px",r.letterSpacing=(n.__.__letterSpacing||0)*s+"px","none"===c?r.whiteSpace="nowrap":"break"===c&&(r.wordBreak="break-all"),r.textIndent=(n.paraIndent||0)*s+"px",r.padding=l instanceof Array?l.map((t=>t*s+"px")).join(" "):(l||0)*s+"px",r.textOverflow="show"===d?"":"hide"===d?"clip":d}return t.TextEditor=class extends n.InnerEditor{constructor(){super(...arguments),this.config={selectAll:!0},this.eventIds=[]}get tag(){return"TextEditor"}onLoad(){const{editor:t}=this,{config:n}=t.app,i=this.editTarget;i.visible=!1,this.isHTMLText=!(i instanceof e.Text),this._keyEvent=n.keyEvent,n.keyEvent=!1;const o=this.editDom=document.createElement("div"),{style:s}=o;if(o.contentEditable="true",s.position="fixed",s.transformOrigin="left top",s.boxSizing="border-box",this.isHTMLText)o.innerHTML=i.text,this.textScale=1;else{o.innerText=i.text;const{scaleX:t,scaleY:e}=i.worldTransform;this.textScale=Math.max(Math.abs(t),Math.abs(e));const n=i.fontSize*this.textScale;n<12&&(this.textScale*=12/n)}const{view:r}=t.app;(this.inBody=r instanceof HTMLCanvasElement)?document.body.appendChild(o):r.appendChild(o),this.eventIds=[t.app.on_(e.PointerEvent.DOWN,(e=>{let n,{target:i}=e.origin;for(;i;)i===o&&(n=!0),i=i.parentElement;n||t.closeInnerEditor()}))],this.onFocus=this.onFocus.bind(this),this.onInput=this.onInput.bind(this),this.onUpdate=this.onUpdate.bind(this),this.onEscape=this.onEscape.bind(this),o.addEventListener("focus",this.onFocus),o.addEventListener("input",this.onInput),window.addEventListener("keydown",this.onEscape),window.addEventListener("scroll",this.onUpdate);const a=window.getSelection(),l=document.createRange();if(this.config.selectAll)l.selectNodeContents(o);else{const t=o.childNodes[0];l.setStartAfter(t),l.setEndAfter(t),l.collapse(!0)}a.removeAllRanges(),a.addRange(l)}onInput(){const{editDom:t}=this;this.editTarget.text=this.isHTMLText?t.innerHTML:t.innerText.replace(/\n\n/,"\n")}onFocus(){this.editDom.style.outline="none"}onEscape(t){"Escape"===t.code&&this.editor.closeInnerEditor()}onUpdate(){const{editTarget:t,textScale:n}=this,{style:i}=this.editDom,{x:o,y:r}=this.inBody?t.app.clientBounds:t.app.tree.clientBounds,{a:a,b:l,c:c,d:d,e:p,f:h}=new e.Matrix(t.worldTransform).scale(1/n);i.transform=`matrix(${a},${l},${c},${d},${p},${h})`,i.left=o-window.scrollX+"px",i.top=r-window.scrollY+"px",i.width=t.width*n+(t.__.__autoWidth?20:0)+"px",i.height=t.height*n+(t.__.__autoHeight?20:0)+"px",this.isHTMLText||s(this.editDom,t,this.textScale)}onUnload(){const{editTarget:t,editor:e,editDom:n}=this;t&&(this.onInput(),t.visible=!0,e.app.config.keyEvent=this._keyEvent,e.off_(this.eventIds),n.removeEventListener("focus",this.onFocus),n.removeEventListener("input",this.onInput),window.removeEventListener("keydown",this.onEscape),window.removeEventListener("scroll",this.onUpdate),n.remove(),this.editDom=this.eventIds=void 0)}},t.TextEditor=function(t,e,n,i){var o,s=arguments.length,r=s<3?e:null===i?i=Object.getOwnPropertyDescriptor(e,n):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(t,e,n,i);else for(var a=t.length-1;a>=0;a--)(o=t[a])&&(r=(s<3?o(r):s>3?o(e,n,r):o(e,n))||r);return s>3&&r&&Object.defineProperty(e,n,r),r}([n.registerInnerEditor()],t.TextEditor),t}({},LeaferUI,LeaferIN.editor);
|
package/package.json
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer-in/text-editor",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "@leafer-in/text-editor",
|
|
5
5
|
"author": "Chao (Leafer) Wan",
|
|
6
6
|
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
7
8
|
"main": "dist/text-editor.esm.js",
|
|
9
|
+
"exports": {
|
|
10
|
+
"import": "./dist/text-editor.esm.js",
|
|
11
|
+
"require": "./dist/text-editor.cjs",
|
|
12
|
+
"types": "./types/index.d.ts"
|
|
13
|
+
},
|
|
14
|
+
"types": "types/index.d.ts",
|
|
8
15
|
"unpkg": "dist/text-editor.js",
|
|
9
16
|
"jsdelivr": "dist/text-editor.js",
|
|
10
|
-
"types": "types/index.d.ts",
|
|
11
17
|
"files": [
|
|
18
|
+
"src",
|
|
12
19
|
"types",
|
|
13
20
|
"dist"
|
|
14
21
|
],
|
|
@@ -27,9 +34,9 @@
|
|
|
27
34
|
"leaferjs"
|
|
28
35
|
],
|
|
29
36
|
"dependencies": {
|
|
30
|
-
"@leafer-ui/core": "1.0.
|
|
31
|
-
"@leafer-in/editor": "1.0.
|
|
32
|
-
"@leafer-ui/interface": "1.0.
|
|
33
|
-
"@leafer-in/interface": "1.0.
|
|
37
|
+
"@leafer-ui/core": "^1.0.2",
|
|
38
|
+
"@leafer-in/editor": "^1.0.2",
|
|
39
|
+
"@leafer-ui/interface": "^1.0.2",
|
|
40
|
+
"@leafer-in/interface": "^1.0.2"
|
|
34
41
|
}
|
|
35
42
|
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { IText, IEventListenerId } from '@leafer-in/interface'
|
|
2
|
+
import { Matrix, PointerEvent, Text } from '@leafer-ui/core'
|
|
3
|
+
import { InnerEditor, registerInnerEditor } from '@leafer-in/editor'
|
|
4
|
+
import { updateStyle } from './updateStyle'
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@registerInnerEditor()
|
|
8
|
+
export class TextEditor extends InnerEditor {
|
|
9
|
+
|
|
10
|
+
public get tag() { return 'TextEditor' }
|
|
11
|
+
declare public editTarget: IText
|
|
12
|
+
|
|
13
|
+
public editDom: HTMLDivElement
|
|
14
|
+
public textScale: number
|
|
15
|
+
|
|
16
|
+
public config = {
|
|
17
|
+
selectAll: true
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public eventIds: IEventListenerId[] = []
|
|
21
|
+
|
|
22
|
+
protected inBody: boolean
|
|
23
|
+
protected isHTMLText: boolean
|
|
24
|
+
protected _keyEvent: boolean
|
|
25
|
+
|
|
26
|
+
public onLoad(): void {
|
|
27
|
+
const { editor } = this
|
|
28
|
+
const { config } = editor.app
|
|
29
|
+
|
|
30
|
+
const text = this.editTarget
|
|
31
|
+
text.visible = false
|
|
32
|
+
|
|
33
|
+
this.isHTMLText = !(text instanceof Text) // HTMLText
|
|
34
|
+
this._keyEvent = config.keyEvent
|
|
35
|
+
config.keyEvent = false
|
|
36
|
+
|
|
37
|
+
const div = this.editDom = document.createElement('div')
|
|
38
|
+
const { style } = div
|
|
39
|
+
div.contentEditable = 'true'
|
|
40
|
+
style.position = 'fixed' // 防止文本输入到边界时产生滚动
|
|
41
|
+
style.transformOrigin = 'left top'
|
|
42
|
+
style.boxSizing = 'border-box'
|
|
43
|
+
|
|
44
|
+
if (this.isHTMLText) {
|
|
45
|
+
|
|
46
|
+
div.innerHTML = text.text
|
|
47
|
+
this.textScale = 1
|
|
48
|
+
|
|
49
|
+
} else {
|
|
50
|
+
|
|
51
|
+
div.innerText = text.text
|
|
52
|
+
|
|
53
|
+
const { scaleX, scaleY } = text.worldTransform
|
|
54
|
+
this.textScale = Math.max(Math.abs(scaleX), Math.abs(scaleY))
|
|
55
|
+
|
|
56
|
+
const fontSize = text.fontSize * this.textScale
|
|
57
|
+
if (fontSize < 12) this.textScale *= 12 / fontSize
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const { view } = editor.app;
|
|
62
|
+
(this.inBody = view instanceof HTMLCanvasElement) ? document.body.appendChild(div) : (view as HTMLDivElement).appendChild(div)
|
|
63
|
+
|
|
64
|
+
// events
|
|
65
|
+
|
|
66
|
+
this.eventIds = [
|
|
67
|
+
editor.app.on_(PointerEvent.DOWN, (e: PointerEvent) => {
|
|
68
|
+
let { target } = e.origin, find: boolean
|
|
69
|
+
while (target) {
|
|
70
|
+
if (target === div) find = true
|
|
71
|
+
target = target.parentElement
|
|
72
|
+
}
|
|
73
|
+
if (!find) editor.closeInnerEditor()
|
|
74
|
+
})
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
this.onFocus = this.onFocus.bind(this)
|
|
78
|
+
this.onInput = this.onInput.bind(this)
|
|
79
|
+
this.onUpdate = this.onUpdate.bind(this)
|
|
80
|
+
this.onEscape = this.onEscape.bind(this)
|
|
81
|
+
|
|
82
|
+
div.addEventListener("focus", this.onFocus)
|
|
83
|
+
div.addEventListener("input", this.onInput)
|
|
84
|
+
window.addEventListener('keydown', this.onEscape)
|
|
85
|
+
window.addEventListener('scroll', this.onUpdate)
|
|
86
|
+
|
|
87
|
+
// select
|
|
88
|
+
|
|
89
|
+
const selection = window.getSelection()
|
|
90
|
+
const range = document.createRange()
|
|
91
|
+
|
|
92
|
+
if (this.config.selectAll) {
|
|
93
|
+
range.selectNodeContents(div)
|
|
94
|
+
} else {
|
|
95
|
+
const node = div.childNodes[0]
|
|
96
|
+
range.setStartAfter(node)
|
|
97
|
+
range.setEndAfter(node)
|
|
98
|
+
range.collapse(true)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
selection.removeAllRanges()
|
|
102
|
+
selection.addRange(range)
|
|
103
|
+
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
protected onInput(): void {
|
|
107
|
+
const { editDom } = this
|
|
108
|
+
this.editTarget.text = this.isHTMLText ? editDom.innerHTML : editDom.innerText.replace(/\n\n/, '\n')
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
protected onFocus(): void {
|
|
112
|
+
this.editDom.style.outline = 'none'
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
protected onEscape(e: KeyboardEvent): void {
|
|
116
|
+
if (e.code === 'Escape') this.editor.closeInnerEditor()
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
public onUpdate() {
|
|
120
|
+
const { editTarget: text, textScale } = this
|
|
121
|
+
const { style } = this.editDom
|
|
122
|
+
const { x, y } = this.inBody ? text.app.clientBounds : text.app.tree.clientBounds
|
|
123
|
+
const { a, b, c, d, e, f } = new Matrix(text.worldTransform).scale(1 / textScale)
|
|
124
|
+
|
|
125
|
+
style.transform = `matrix(${a},${b},${c},${d},${e},${f})`
|
|
126
|
+
style.left = x - window.scrollX + 'px'
|
|
127
|
+
style.top = y - window.scrollY + 'px'
|
|
128
|
+
style.width = text.width * textScale + (text.__.__autoWidth ? 20 : 0) + 'px'
|
|
129
|
+
style.height = text.height * textScale + (text.__.__autoHeight ? 20 : 0) + 'px'
|
|
130
|
+
this.isHTMLText || updateStyle(this.editDom, text, this.textScale)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
public onUnload(): void {
|
|
134
|
+
const { editTarget: text, editor, editDom: dom } = this
|
|
135
|
+
if (text) {
|
|
136
|
+
this.onInput()
|
|
137
|
+
text.visible = true
|
|
138
|
+
|
|
139
|
+
editor.app.config.keyEvent = this._keyEvent
|
|
140
|
+
editor.off_(this.eventIds)
|
|
141
|
+
|
|
142
|
+
dom.removeEventListener("focus", this.onFocus)
|
|
143
|
+
dom.removeEventListener("input", this.onInput)
|
|
144
|
+
window.removeEventListener('keydown', this.onEscape)
|
|
145
|
+
window.removeEventListener('scroll', this.onUpdate)
|
|
146
|
+
|
|
147
|
+
dom.remove()
|
|
148
|
+
this.editDom = this.eventIds = undefined
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { TextEditor } from './TextEditor'
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { ColorConvert, inviteCode } from '@leafer-ui/core'
|
|
2
|
+
import { IFill, IText, IRGB } from '@leafer-ui/interface'
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
export const textCaseMap = {
|
|
6
|
+
'none': 'none',
|
|
7
|
+
'title': 'capitalize',
|
|
8
|
+
'upper': 'uppercase',
|
|
9
|
+
'lower': 'lowercase',
|
|
10
|
+
'small-caps': 'small-caps'
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const verticalAlignMap = {
|
|
14
|
+
'top': 'flex-start',
|
|
15
|
+
'middle': 'center',
|
|
16
|
+
'bottom': 'flex-end'
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const codes = inviteCode
|
|
20
|
+
|
|
21
|
+
export function updateStyle(textDom: HTMLDivElement, text: IText, textScale: number): void {
|
|
22
|
+
const { style } = textDom
|
|
23
|
+
const { fill, padding, textWrap, textOverflow, textDecoration } = text
|
|
24
|
+
|
|
25
|
+
style.fontFamily = text.fontFamily
|
|
26
|
+
style.fontSize = text.fontSize * textScale + 'px'
|
|
27
|
+
setFill(style, fill)
|
|
28
|
+
|
|
29
|
+
style.fontStyle = text.italic ? 'italic' : 'normal'
|
|
30
|
+
style.fontWeight = text.fontWeight as string
|
|
31
|
+
style.textDecoration = textDecoration === 'delete' ? 'line-through' : textDecoration
|
|
32
|
+
style.textTransform = textCaseMap[text.textCase]
|
|
33
|
+
|
|
34
|
+
style.textAlign = text.textAlign
|
|
35
|
+
style.display = 'flex'
|
|
36
|
+
style.flexDirection = 'column'
|
|
37
|
+
style.justifyContent = verticalAlignMap[text.verticalAlign]
|
|
38
|
+
|
|
39
|
+
style.lineHeight = (text.__.__lineHeight || 0) * textScale + 'px'
|
|
40
|
+
style.letterSpacing = (text.__.__letterSpacing || 0) * textScale + 'px'
|
|
41
|
+
if (textWrap === 'none') {
|
|
42
|
+
style.whiteSpace = 'nowrap'
|
|
43
|
+
} else if (textWrap === 'break') {
|
|
44
|
+
style.wordBreak = 'break-all'
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
style.textIndent = (text.paraIndent || 0) * textScale + 'px'
|
|
48
|
+
style.padding = padding instanceof Array ? padding.map(item => item * textScale + 'px').join(' ') : (padding || 0) * textScale + 'px'
|
|
49
|
+
style.textOverflow = textOverflow === 'show' ? '' : (textOverflow === 'hide' ? 'clip' : textOverflow)
|
|
50
|
+
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function setFill(style: CSSStyleDeclaration, fill: IFill): void {
|
|
54
|
+
let color: string = 'black'
|
|
55
|
+
|
|
56
|
+
if (fill instanceof Array) fill = fill[0]
|
|
57
|
+
|
|
58
|
+
if (typeof fill === 'object') {
|
|
59
|
+
|
|
60
|
+
switch (fill.type) {
|
|
61
|
+
case 'solid':
|
|
62
|
+
color = ColorConvert.string(fill.color)
|
|
63
|
+
break
|
|
64
|
+
case 'image':
|
|
65
|
+
break
|
|
66
|
+
case 'linear':
|
|
67
|
+
case 'radial':
|
|
68
|
+
case 'angular':
|
|
69
|
+
const stop = fill.stops[0]
|
|
70
|
+
color = ColorConvert.string(typeof stop === 'string' ? stop : stop.color)
|
|
71
|
+
break
|
|
72
|
+
default:
|
|
73
|
+
if ((fill as IRGB).r !== undefined) color = ColorConvert.string(fill)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
} else {
|
|
77
|
+
color = fill
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
style.color = color
|
|
81
|
+
}
|
package/types/index.d.ts
CHANGED