@visactor/vrender-core 0.22.0-vstory.1 → 0.22.0-vstory.10

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.
Files changed (90) hide show
  1. package/cjs/canvas/util.js +1 -2
  2. package/cjs/color-string/store.js +2 -1
  3. package/cjs/core/global.d.ts +0 -3
  4. package/cjs/core/global.js +1 -7
  5. package/cjs/core/global.js.map +1 -1
  6. package/cjs/graphic/builtin-symbol/utils.js +1 -1
  7. package/cjs/graphic/builtin-symbol/utils.js.map +1 -1
  8. package/cjs/graphic/config.js +1 -0
  9. package/cjs/graphic/config.js.map +1 -1
  10. package/cjs/graphic/graphic.d.ts +1 -1
  11. package/cjs/graphic/graphic.js +5 -5
  12. package/cjs/graphic/graphic.js.map +1 -1
  13. package/cjs/graphic/group.js +1 -1
  14. package/cjs/graphic/group.js.map +1 -1
  15. package/cjs/graphic/richtext/line.js +13 -1
  16. package/cjs/graphic/richtext/line.js.map +1 -1
  17. package/cjs/graphic/richtext/paragraph.d.ts +9 -1
  18. package/cjs/graphic/richtext/paragraph.js +50 -11
  19. package/cjs/graphic/richtext/paragraph.js.map +1 -1
  20. package/cjs/graphic/richtext/utils.js +4 -4
  21. package/cjs/graphic/richtext/utils.js.map +1 -1
  22. package/cjs/graphic/richtext.d.ts +2 -1
  23. package/cjs/graphic/richtext.js +10 -1
  24. package/cjs/graphic/richtext.js.map +1 -1
  25. package/cjs/interface/global.d.ts +0 -1
  26. package/cjs/interface/global.js.map +1 -1
  27. package/cjs/interface/graphic/richText.d.ts +11 -0
  28. package/cjs/interface/graphic/richText.js.map +1 -1
  29. package/cjs/interface/graphic.d.ts +1 -1
  30. package/cjs/interface/graphic.js.map +1 -1
  31. package/cjs/plugins/builtin-plugin/edit-module.d.ts +2 -1
  32. package/cjs/plugins/builtin-plugin/edit-module.js +20 -15
  33. package/cjs/plugins/builtin-plugin/edit-module.js.map +1 -1
  34. package/cjs/plugins/builtin-plugin/richtext-edit-plugin.d.ts +36 -10
  35. package/cjs/plugins/builtin-plugin/richtext-edit-plugin.js +322 -132
  36. package/cjs/plugins/builtin-plugin/richtext-edit-plugin.js.map +1 -1
  37. package/cjs/render/contributions/render/contributions/image-contribution-render.d.ts +1 -1
  38. package/cjs/render/contributions/render/contributions/image-contribution-render.js +2 -2
  39. package/cjs/render/contributions/render/contributions/image-contribution-render.js.map +1 -1
  40. package/cjs/render/contributions/render/draw-contribution.js +3 -2
  41. package/cjs/render/contributions/render/draw-contribution.js.map +1 -1
  42. package/cjs/render/contributions/render/image-render.js.map +1 -1
  43. package/cjs/resource-loader/loader.js +1 -1
  44. package/cjs/resource-loader/loader.js.map +1 -1
  45. package/dist/index.es.js +498 -110
  46. package/es/canvas/util.js +1 -2
  47. package/es/color-string/store.js +2 -1
  48. package/es/core/global.d.ts +0 -3
  49. package/es/core/global.js +1 -8
  50. package/es/core/global.js.map +1 -1
  51. package/es/graphic/builtin-symbol/utils.js +1 -1
  52. package/es/graphic/builtin-symbol/utils.js.map +1 -1
  53. package/es/graphic/config.js +1 -0
  54. package/es/graphic/config.js.map +1 -1
  55. package/es/graphic/graphic.d.ts +1 -1
  56. package/es/graphic/graphic.js +5 -5
  57. package/es/graphic/graphic.js.map +1 -1
  58. package/es/graphic/group.js +1 -1
  59. package/es/graphic/group.js.map +1 -1
  60. package/es/graphic/richtext/line.js +13 -1
  61. package/es/graphic/richtext/line.js.map +1 -1
  62. package/es/graphic/richtext/paragraph.d.ts +9 -1
  63. package/es/graphic/richtext/paragraph.js +50 -11
  64. package/es/graphic/richtext/paragraph.js.map +1 -1
  65. package/es/graphic/richtext/utils.js +4 -4
  66. package/es/graphic/richtext/utils.js.map +1 -1
  67. package/es/graphic/richtext.d.ts +2 -1
  68. package/es/graphic/richtext.js +10 -1
  69. package/es/graphic/richtext.js.map +1 -1
  70. package/es/interface/global.d.ts +0 -1
  71. package/es/interface/global.js.map +1 -1
  72. package/es/interface/graphic/richText.d.ts +11 -0
  73. package/es/interface/graphic/richText.js.map +1 -1
  74. package/es/interface/graphic.d.ts +1 -1
  75. package/es/interface/graphic.js.map +1 -1
  76. package/es/plugins/builtin-plugin/edit-module.d.ts +2 -1
  77. package/es/plugins/builtin-plugin/edit-module.js +17 -13
  78. package/es/plugins/builtin-plugin/edit-module.js.map +1 -1
  79. package/es/plugins/builtin-plugin/richtext-edit-plugin.d.ts +36 -10
  80. package/es/plugins/builtin-plugin/richtext-edit-plugin.js +320 -131
  81. package/es/plugins/builtin-plugin/richtext-edit-plugin.js.map +1 -1
  82. package/es/render/contributions/render/contributions/image-contribution-render.d.ts +1 -1
  83. package/es/render/contributions/render/contributions/image-contribution-render.js +2 -2
  84. package/es/render/contributions/render/contributions/image-contribution-render.js.map +1 -1
  85. package/es/render/contributions/render/draw-contribution.js +2 -2
  86. package/es/render/contributions/render/draw-contribution.js.map +1 -1
  87. package/es/render/contributions/render/image-render.js.map +1 -1
  88. package/es/resource-loader/loader.js +1 -1
  89. package/es/resource-loader/loader.js.map +1 -1
  90. package/package.json +3 -3
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: !0
5
- }), exports.RichTextEditPlugin = exports.FORMAT_ELEMENT_COMMAND = exports.FORMAT_TEXT_COMMAND = void 0;
5
+ }), exports.RichTextEditPlugin = exports.FORMAT_ELEMENT_COMMAND = exports.FORMAT_ALL_TEXT_COMMAND = exports.FORMAT_TEXT_COMMAND = void 0;
6
6
 
7
7
  const vutils_1 = require("@visactor/vutils"), generator_1 = require("../../common/generator"), graphic_1 = require("../../graphic"), animate_1 = require("../../animate"), edit_module_1 = require("./edit-module"), application_1 = require("../../application"), utils_1 = require("../../graphic/richtext/utils");
8
8
 
@@ -31,6 +31,7 @@ class Selection {
31
31
  if (!this.rt) return null;
32
32
  let idx = Math.round(cursorIdx);
33
33
  const config = this.rt.attribute.textConfig;
34
+ if (!config.length) return null;
34
35
  for (let i = 0; i < config.length; i++) if ("\n" !== config[i].text && (idx--, idx < 0)) return config[i][key];
35
36
  return null !== (_a = config[Math.min(idx, config.length - 1)][key]) && void 0 !== _a ? _a : this.rt.attribute[key];
36
37
  }
@@ -48,7 +49,8 @@ class Selection {
48
49
  }
49
50
  }
50
51
 
51
- exports.FORMAT_TEXT_COMMAND = "FORMAT_TEXT_COMMAND", exports.FORMAT_ELEMENT_COMMAND = "FORMAT_ELEMENT_COMMAND";
52
+ exports.FORMAT_TEXT_COMMAND = "FORMAT_TEXT_COMMAND", exports.FORMAT_ALL_TEXT_COMMAND = "FORMAT_ALL_TEXT_COMMAND",
53
+ exports.FORMAT_ELEMENT_COMMAND = "FORMAT_ELEMENT_COMMAND";
52
54
 
53
55
  class RichTextEditPlugin {
54
56
  static tryUpdateRichtext(richtext) {
@@ -67,46 +69,62 @@ class RichTextEditPlugin {
67
69
  }
68
70
  constructor() {
69
71
  this.name = "RichTextEditPlugin", this.activeEvent = "onRegister", this._uid = generator_1.Generator.GenAutoIncrementId(),
70
- this.key = this.name + this._uid, this.editing = !1, this.pointerDown = !1, this.handleKeyDown = e => {
72
+ this.key = this.name + this._uid, this.editing = !1, this.focusing = !1, this.pointerDown = !1,
73
+ this.formatTextCommandCb = (payload, p) => {
74
+ const rt = p.currRt;
75
+ if (!rt) return;
76
+ const selectionData = p.getSelection();
77
+ if (!selectionData) return;
78
+ const {selectionStartCursorIdx: selectionStartCursorIdx, curCursorIdx: curCursorIdx} = selectionData, minCursorIdx = Math.min(selectionStartCursorIdx, curCursorIdx), maxCursorIdx = Math.max(selectionStartCursorIdx, curCursorIdx), minConfigIdx = (0,
79
+ edit_module_1.findConfigIndexByCursorIdx)(rt.attribute.textConfig, minCursorIdx), maxConfigIdx = (0,
80
+ edit_module_1.findConfigIndexByCursorIdx)(rt.attribute.textConfig, maxCursorIdx), config = rt.attribute.textConfig.slice(minConfigIdx, maxConfigIdx);
81
+ this._formatTextCommand(payload, config, rt);
82
+ }, this.formatAllTextCommandCb = (payload, p) => {
83
+ const rt = p.currRt;
84
+ if (!rt) return;
85
+ const config = rt.attribute.textConfig;
86
+ this._formatTextCommand(payload, config, rt);
87
+ }, this.handleKeyDown = e => {
71
88
  this.currRt && this.editing && (this.copyToClipboard(e) || this.fullSelection(e) || this.directKey(e));
72
89
  }, this.handleInput = (text, isComposing, cursorIdx, rt) => {
73
- this.hideSelection(), this.updateCbs.forEach((cb => cb("input", this)));
90
+ this.currRt && (this.tryShowShadowPlaceholder(), this.tryShowInputBounds(), this.hideSelection(),
91
+ this.updateCbs.forEach((cb => cb("input", this))));
74
92
  }, this.handleChange = (text, isComposing, cursorIdx, rt) => {
75
- this.curCursorIdx = cursorIdx, this.selectionStartCursorIdx = cursorIdx;
93
+ if (!this.currRt) return;
94
+ this.tryShowShadowPlaceholder(), this.tryShowInputBounds(), this.curCursorIdx = cursorIdx,
95
+ this.selectionStartCursorIdx = cursorIdx;
76
96
  const p = this.computedCursorPosByCursorIdx(cursorIdx, rt);
77
97
  this.setCursorAndTextArea(p.x, p.y1, p.y2, rt), this.hideSelection(), this.updateCbs.forEach((cb => cb("change", this)));
78
- }, this.handleFocusIn = () => {}, this.handleFocusOut = () => {
79
- this.editing = !1, this.deFocus(), this.pointerDown = !1, this.triggerRender(),
80
- this.updateCbs.forEach((cb => cb("defocus", this)));
98
+ }, this.handleFocusIn = () => {
99
+ throw new Error("不会走到这里 handleFocusIn");
100
+ }, this.handleFocusOut = () => {
101
+ throw new Error("不会走到这里 handleFocusOut");
81
102
  }, this.handleMove = e => {
82
- this.isRichtext(e) && (this.currRt = e.target, this.handleEnter(e), e.target.once("pointerleave", this.handleLeave),
83
- this.tryShowSelection(e, !1));
84
- }, this.handleEnter = e => {
103
+ this.currRt && !this.currRt.attribute.editable && this.deFocus(!0), this.isEditableRichtext(e) ? (this.handleEnter(),
104
+ e.target.once("pointerleave", this.handleLeave), this.tryShowSelection(e, !1)) : this.handleLeave();
105
+ }, this.handleEnter = () => {
85
106
  this.editing = !0, this.pluginService.stage.setCursor("text");
86
- }, this.handleLeave = e => {
107
+ }, this.handleLeave = () => {
87
108
  this.editing = !1, this.pluginService.stage.setCursor("default");
88
109
  }, this.handlePointerDown = e => {
89
- this.editing ? this.onFocus(e) : this.deFocus(), this.triggerRender(), this.pointerDown = !0,
90
- this.updateCbs.forEach((cb => cb(this.editing ? "onfocus" : "defocus", this)));
110
+ this.editing && this.isEditableRichtext(e) ? this.onFocus(e) : this.deFocus(!0),
111
+ this.triggerRender(), this.pointerDown = !0, this.updateCbs.forEach((cb => cb(this.editing ? "onfocus" : "defocus", this)));
91
112
  }, this.handlePointerUp = e => {
92
113
  this.pointerDown = !1;
93
114
  }, this.handleDBLClick = e => {
94
115
  this.editing && this.tryShowSelection(e, !0);
95
116
  }, this.commandCbs = new Map, this.commandCbs.set(exports.FORMAT_TEXT_COMMAND, [ this.formatTextCommandCb ]),
117
+ this.commandCbs.set(exports.FORMAT_ALL_TEXT_COMMAND, [ this.formatAllTextCommandCb ]),
96
118
  this.updateCbs = [], this.timeline = new animate_1.DefaultTimeline, this.ticker = new animate_1.DefaultTicker([ this.timeline ]),
97
119
  this.deltaX = 0, this.deltaY = 0;
98
120
  }
99
- formatTextCommandCb(payload, p) {
100
- const rt = p.currRt;
101
- if (!rt) return;
102
- const selectionData = p.getSelection();
103
- if (!selectionData) return;
104
- const {selectionStartCursorIdx: selectionStartCursorIdx, curCursorIdx: curCursorIdx} = selectionData, minCursorIdx = Math.min(selectionStartCursorIdx, curCursorIdx), maxCursorIdx = Math.max(selectionStartCursorIdx, curCursorIdx), minConfigIdx = (0,
105
- edit_module_1.findConfigIndexByCursorIdx)(rt.attribute.textConfig, minCursorIdx), maxConfigIdx = (0,
106
- edit_module_1.findConfigIndexByCursorIdx)(rt.attribute.textConfig, maxCursorIdx), config = rt.attribute.textConfig.slice(minConfigIdx, maxConfigIdx);
121
+ _formatTextCommand(payload, config, rt) {
107
122
  "bold" === payload ? config.forEach((item => item.fontWeight = "bold")) : "italic" === payload ? config.forEach((item => item.fontStyle = "italic")) : "underline" === payload ? config.forEach((item => item.underline = !0)) : "lineThrough" === payload ? config.forEach((item => item.lineThrough = !0)) : (0,
108
123
  vutils_1.isObject)(payload) && config.forEach((item => (0, vutils_1.merge)(item, payload))),
109
124
  rt.setAttributes(rt.attribute);
125
+ const cache = rt.getFrameCache();
126
+ cache && (this.selectionRangeByCursorIdx(this.selectionStartCursorIdx, this.curCursorIdx, cache),
127
+ this.tryShowInputBounds());
110
128
  }
111
129
  dispatchCommand(command, payload) {
112
130
  const cbs = this.commandCbs.get(command);
@@ -115,9 +133,17 @@ class RichTextEditPlugin {
115
133
  registerCommand(command, cb) {
116
134
  (this.commandCbs.get(command) || []).push(cb);
117
135
  }
136
+ removeCommand(command, cb) {
137
+ const cbs = this.commandCbs.get(command) || [], idx = cbs.indexOf(cb);
138
+ idx > -1 && cbs.splice(idx, 1);
139
+ }
118
140
  registerUpdateListener(cb) {
119
141
  (this.updateCbs || []).push(cb);
120
142
  }
143
+ removeUpdateListener(cb) {
144
+ const cbs = this.updateCbs || [], idx = cbs.indexOf(cb);
145
+ idx > -1 && cbs.splice(idx, 1);
146
+ }
121
147
  activate(context) {
122
148
  this.pluginService = context, this.editModule = new edit_module_1.EditModule, context.stage.on("pointermove", this.handleMove),
123
149
  context.stage.on("pointerdown", this.handlePointerDown), context.stage.on("pointerup", this.handlePointerUp),
@@ -141,8 +167,10 @@ class RichTextEditPlugin {
141
167
  if (!cache) return;
142
168
  const {lines: lines} = cache, totalCursorCount = lines.reduce(((total, line) => total + line.paragraphs.length), 0) - 1;
143
169
  startIdx > endIdx && ([startIdx, endIdx] = [ endIdx, startIdx ]), startIdx = Math.min(Math.max(startIdx, -.1), totalCursorCount + .1),
144
- endIdx = Math.min(Math.max(endIdx, -.1), totalCursorCount + .1), this.curCursorIdx = endIdx,
145
- this.selectionStartCursorIdx = startIdx;
170
+ endIdx = Math.min(Math.max(endIdx, -.1), totalCursorCount + .1), this.selectionRangeByCursorIdx(startIdx, endIdx, cache);
171
+ }
172
+ selectionRangeByCursorIdx(startCursorIdx, endCursorIdx, cache) {
173
+ this.curCursorIdx = endCursorIdx, this.selectionStartCursorIdx = startCursorIdx;
146
174
  const {x: x, y1: y1, y2: y2} = this.computedCursorPosByCursorIdx(this.selectionStartCursorIdx, this.currRt);
147
175
  this.startCursorPos = {
148
176
  x: x,
@@ -157,7 +185,10 @@ class RichTextEditPlugin {
157
185
  if (!currRt) return;
158
186
  const cache = currRt.getFrameCache();
159
187
  if (!cache) return;
160
- const {lines: lines} = cache, totalCursorCount = lines.reduce(((total, line) => total + line.paragraphs.length), 0) - 1;
188
+ const {lines: lines} = cache;
189
+ if (0 === lines.length) return;
190
+ if (!lines[0].paragraphs || 0 === lines[0].paragraphs.length) return;
191
+ const totalCursorCount = lines.reduce(((total, line) => total + line.paragraphs.length), 0) - 1;
161
192
  return this.selectionRange(-.1, totalCursorCount + .1), e.preventDefault(), !0;
162
193
  }
163
194
  return !1;
@@ -195,60 +226,164 @@ class RichTextEditPlugin {
195
226
  }
196
227
  return !0;
197
228
  }
229
+ tryShowShadowPlaceholder() {
230
+ if (!this.currRt) return;
231
+ const shadowRoot = this.currRt.shadowRoot;
232
+ if (shadowRoot) {
233
+ const placeholder = shadowRoot.getElementsByType("richtext")[0];
234
+ placeholder && shadowRoot.removeChild(placeholder);
235
+ }
236
+ const {textConfig: textConfig, editOptions: editOptions} = this.currRt.attribute;
237
+ if (textConfig && textConfig.length) return;
238
+ if (!editOptions || !editOptions.placeholder) return;
239
+ const {placeholder: placeholder, placeholderColor: placeholderColor = "rgba(0, 0, 0, 0.6)", placeholderFontFamily: placeholderFontFamily, placeholderFontSize: placeholderFontSize} = editOptions, shadow = this.getShadow(this.currRt), textConfigItem = Object.assign(Object.assign({}, (0,
240
+ edit_module_1.getDefaultCharacterConfig)(this.currRt.attribute)), {
241
+ text: placeholder
242
+ });
243
+ placeholderColor && (textConfigItem.fill = placeholderColor), placeholderFontFamily && (textConfigItem.fontFamily = placeholderFontFamily),
244
+ placeholderFontSize && (textConfigItem.fontSize = placeholderFontSize), this.shadowPlaceHolder = (0,
245
+ graphic_1.createRichText)(Object.assign(Object.assign({}, this.currRt.attribute), {
246
+ x: 0,
247
+ y: 0,
248
+ pickable: !1,
249
+ editable: !1,
250
+ editOptions: null,
251
+ angle: 0,
252
+ _debug_bounds: !1,
253
+ textConfig: [ textConfigItem ]
254
+ })), shadow.add(this.shadowPlaceHolder);
255
+ }
256
+ tryShowInputBounds() {
257
+ if (!this.currRt || !this.focusing) return;
258
+ const {editOptions: editOptions = {}} = this.currRt.attribute, {boundsStrokeWhenInput: boundsStrokeWhenInput} = editOptions;
259
+ if (!editOptions || !boundsStrokeWhenInput) return;
260
+ const {attribute: attribute} = this.currRt;
261
+ let b = this.currRt.AABBBounds, h = b.height();
262
+ if (!attribute.textConfig.length && this.editLine) {
263
+ const {points: points} = this.editLine.attribute;
264
+ h = points[1].y - points[0].y, b = (0, graphic_1.getRichTextBounds)(Object.assign({}, this.shadowPlaceHolder.attribute));
265
+ }
266
+ this.shadowBounds = this.shadowBounds || (0, graphic_1.createRect)({}), this.shadowBounds.setAttributes({
267
+ x: 0,
268
+ y: 0,
269
+ width: b.width(),
270
+ height: h,
271
+ fill: !1,
272
+ stroke: boundsStrokeWhenInput,
273
+ lineWidth: 1,
274
+ zIndex: -1
275
+ });
276
+ this.getShadow(this.currRt).add(this.shadowBounds), this.offsetLineBgAndShadowBounds();
277
+ }
278
+ trySyncPlaceholderToTextConfig() {
279
+ if (!this.currRt) return;
280
+ const {textConfig: textConfig, editOptions: editOptions} = this.currRt.attribute;
281
+ if (textConfig && textConfig.length) return;
282
+ if (!(editOptions && editOptions.placeholder && editOptions.syncPlaceHolderToTextConfig)) return;
283
+ const {placeholder: placeholder} = editOptions;
284
+ this.currRt.setAttributes({
285
+ textConfig: [ Object.assign({
286
+ text: placeholder
287
+ }, (0, edit_module_1.getDefaultCharacterConfig)(this.currRt.attribute)) ]
288
+ });
289
+ }
198
290
  deactivate(context) {
199
291
  context.stage.off("pointermove", this.handleMove), context.stage.off("pointerdown", this.handlePointerDown),
200
292
  context.stage.off("pointerup", this.handlePointerUp), context.stage.off("pointerleave", this.handlePointerUp),
201
293
  context.stage.off("dblclick", this.handleDBLClick), application_1.application.global.addEventListener("keydown", this.handleKeyDown);
202
294
  }
203
- onFocus(e) {
204
- this.deFocus(), this.currRt = e.target;
295
+ onFocus(e, data) {
296
+ this.updateCbs && this.updateCbs.forEach((cb => cb("beforeOnfocus", this))), this.deFocus(!1),
297
+ this.focusing = !0;
205
298
  const target = e.target;
206
- RichTextEditPlugin.tryUpdateRichtext(target);
207
- const shadowRoot = target.attachShadow(), cache = target.getFrameCache();
208
- if (!cache) return;
209
- if (this.computeGlobalDelta(cache), shadowRoot.setAttributes({
210
- shadowRootIdx: 1,
211
- pickable: !1,
212
- x: this.deltaX,
213
- y: this.deltaY
214
- }), !this.editLine) {
215
- const line = (0, graphic_1.createLine)({
216
- x: 0,
217
- y: 0,
218
- lineWidth: 1,
219
- stroke: "black"
220
- });
221
- this.addAnimateToLine(line), this.editLine = line, this.ticker.start(!0);
222
- const g = (0, graphic_1.createGroup)({
223
- x: 0,
224
- y: 0,
225
- width: 0,
226
- height: 0
227
- });
228
- this.editBg = g, shadowRoot.add(this.editLine), shadowRoot.add(this.editBg);
229
- }
230
- const data = this.computedCursorPosByEvent(e, cache);
231
- if (data) {
232
- const {x: x, y1: y1, y2: y2, cursorIndex: cursorIndex} = data;
233
- this.startCursorPos = {
234
- x: x,
235
- y: (y1 + y2) / 2
236
- }, this.curCursorIdx = cursorIndex, this.selectionStartCursorIdx = cursorIndex,
237
- this.setCursorAndTextArea(x, y1, y2, target);
299
+ if (!target || "richtext" !== target.type) return;
300
+ this.currRt = target, RichTextEditPlugin.tryUpdateRichtext(target);
301
+ const shadowRoot = this.getShadow(target), cache = target.getFrameCache();
302
+ if (cache) {
303
+ if (this.computeGlobalDelta(cache), shadowRoot.setAttributes({
304
+ shadowRootIdx: 1,
305
+ pickable: !1,
306
+ x: this.deltaX,
307
+ y: this.deltaY
308
+ }), !this.editLine) {
309
+ const line = (0, graphic_1.createLine)({
310
+ x: 0,
311
+ y: 0,
312
+ lineWidth: 1,
313
+ stroke: "black"
314
+ });
315
+ this.addAnimateToLine(line), this.editLine = line, this.ticker.start(!0);
316
+ const g = (0, graphic_1.createGroup)({
317
+ x: 0,
318
+ y: 0,
319
+ width: 0,
320
+ height: 0
321
+ });
322
+ this.editBg = g, shadowRoot.add(this.editLine), shadowRoot.add(this.editBg);
323
+ }
324
+ if (data = data || this.computedCursorPosByEvent(e, cache)) {
325
+ const {x: x, y1: y1, y2: y2, cursorIndex: cursorIndex} = data;
326
+ this.startCursorPos = {
327
+ x: x,
328
+ y: (y1 + y2) / 2
329
+ }, this.curCursorIdx = cursorIndex, this.selectionStartCursorIdx = cursorIndex,
330
+ this.setCursorAndTextArea(x, y1, y2, target);
331
+ } else {
332
+ const x = 0, y1 = 0, y2 = (0, graphic_1.getRichTextBounds)(Object.assign(Object.assign({}, target.attribute), {
333
+ textConfig: [ {
334
+ text: "a"
335
+ } ]
336
+ })).height();
337
+ this.startCursorPos = {
338
+ x: x,
339
+ y: (y1 + y2) / 2
340
+ }, this.curCursorIdx = -.1, this.selectionStartCursorIdx = -.1, this.setCursorAndTextArea(x, y1, y2, target);
341
+ }
342
+ this.tryShowShadowPlaceholder(), this.tryShowInputBounds(), this.currRt.addUpdateBoundTag();
238
343
  }
239
344
  }
240
- deFocus() {
241
- const target = this.currRt;
242
- target && (target.detachShadow(), this.currRt = null, this.editLine && (this.editLine.parent.removeChild(this.editLine),
243
- this.editLine.release(), this.editLine = null, this.editBg.parent.removeChild(this.editBg),
244
- this.editBg.release(), this.editBg = null));
345
+ offsetLineBgAndShadowBounds() {
346
+ const rt = this.currRt, {textBaseline: textBaseline} = rt.attribute;
347
+ let b, dy = 0, attr = rt.attribute;
348
+ "middle" !== textBaseline && "bottom" !== textBaseline || (attr.textConfig.length || (attr = Object.assign(Object.assign({}, attr), {
349
+ textConfig: [ {
350
+ text: "a"
351
+ } ]
352
+ })), b = (0, graphic_1.getRichTextBounds)(attr)), "middle" === textBaseline ? dy = -b.height() / 2 : "bottom" === textBaseline && (dy = -b.height()),
353
+ this.editLine && this.editLine.setAttributes({
354
+ dy: dy
355
+ }), this.editBg && this.editBg.setAttributes({
356
+ dy: dy
357
+ }), this.shadowBounds && this.shadowBounds.setAttributes({
358
+ dy: dy
359
+ });
360
+ }
361
+ deFocus(trulyDeFocus = !1) {
362
+ const currRt = this.currRt;
363
+ if (!currRt) return;
364
+ this.updateCbs && this.updateCbs.forEach((cb => cb("beforeDefocus", this, {
365
+ trulyDeFocus: trulyDeFocus
366
+ }))), trulyDeFocus && (this.trySyncPlaceholderToTextConfig(), currRt.detachShadow()),
367
+ this.currRt = null, this.editLine && (this.editLine.parent && this.editLine.parent.removeChild(this.editLine),
368
+ this.editLine.release(), this.editLine = null, this.editBg.parent && this.editBg.parent.removeChild(this.editBg),
369
+ this.editBg.release(), this.editBg = null), trulyDeFocus && (this.shadowBounds && (this.shadowBounds.parent && this.shadowBounds.parent.removeChild(this.shadowBounds),
370
+ this.shadowBounds.release(), this.shadowBounds = null), this.shadowPlaceHolder && (this.shadowPlaceHolder.parent && this.shadowPlaceHolder.parent.removeChild(this.shadowPlaceHolder),
371
+ this.shadowPlaceHolder.release(), this.shadowPlaceHolder = null)), this.focusing = !1;
372
+ const textConfig = currRt.attribute.textConfig;
373
+ let lastConfig = textConfig[textConfig.length - 1], cleared = !1;
374
+ for (;lastConfig && "\n" === lastConfig.text; ) textConfig.pop(), lastConfig = textConfig[textConfig.length - 1],
375
+ cleared = !0;
376
+ cleared && currRt.setAttributes({
377
+ textConfig: textConfig
378
+ }), currRt.removeEventListener("pointerleave", this.handleLeave);
245
379
  }
246
380
  addAnimateToLine(line) {
247
381
  line.animates && line.animates.forEach((animate => {
248
382
  animate.stop(), animate.release();
249
383
  }));
250
- const animate = line.animate();
251
- animate.setTimeline(this.timeline), animate.to({
384
+ line.animate({
385
+ timeline: this.timeline
386
+ }).to({
252
387
  opacity: 1
253
388
  }, 10, "linear").wait(700).to({
254
389
  opacity: 0
@@ -281,67 +416,75 @@ class RichTextEditPlugin {
281
416
  x: currCursorData.x,
282
417
  y: (currCursorData.y1 + currCursorData.y2) / 2
283
418
  }, line0Info = this.getLineByPoint(cache, startCursorPos), line1Info = this.getLineByPoint(cache, endCursorPos);
284
- if ((startCursorPos.y > endCursorPos.y || startCursorPos.y === endCursorPos.y && startCursorPos.x > endCursorPos.x) && ([startCursorPos, endCursorPos] = [ endCursorPos, startCursorPos ],
285
- [line1Info, line0Info] = [ line0Info, line1Info ]), this.hideSelection(), line0Info === line1Info) this.editBg.setAttributes({
286
- x: startCursorPos.x,
287
- y: line0Info.top,
288
- width: endCursorPos.x - startCursorPos.x,
289
- height: line0Info.height,
290
- fill: "#336df4",
291
- fillOpacity: .2
292
- }); else {
293
- this.editBg.setAttributes({
294
- x: 0,
419
+ if (line0Info && line1Info) {
420
+ if ((startCursorPos.y > endCursorPos.y || startCursorPos.y === endCursorPos.y && startCursorPos.x > endCursorPos.x) && ([startCursorPos, endCursorPos] = [ endCursorPos, startCursorPos ],
421
+ [line1Info, line0Info] = [ line0Info, line1Info ]), this.hideSelection(), line0Info === line1Info) this.editBg.setAttributes({
422
+ x: startCursorPos.x,
295
423
  y: line0Info.top,
296
- width: 0,
297
- height: 0
298
- });
299
- const startIdx = cache.lines.findIndex((item => item === line0Info)), endIdx = cache.lines.findIndex((item => item === line1Info));
300
- let y = 0;
301
- for (let i = startIdx; i <= endIdx; i++) {
302
- const line = cache.lines[i];
303
- if (i === startIdx) {
304
- const p = line.paragraphs[line.paragraphs.length - 1];
305
- this.editBg.add((0, graphic_1.createRect)({
306
- x: startCursorPos.x,
307
- y: y,
308
- width: p.left + p.width - startCursorPos.x,
309
- height: line.height,
310
- fill: "#336df4",
311
- fillOpacity: .2
312
- }));
313
- } else if (i === endIdx) {
314
- const p = line.paragraphs[0];
315
- this.editBg.add((0, graphic_1.createRect)({
316
- x: p.left,
317
- y: y,
318
- width: endCursorPos.x - p.left,
319
- height: line.height,
320
- fill: "#336df4",
321
- fillOpacity: .2
322
- }));
323
- } else {
324
- const p0 = line.paragraphs[0], p1 = line.paragraphs[line.paragraphs.length - 1];
325
- this.editBg.add((0, graphic_1.createRect)({
326
- x: p0.left,
327
- y: y,
328
- width: p1.left + p1.width - p0.left,
329
- height: line.height,
330
- fill: "#336df4",
331
- fillOpacity: .2
332
- }));
424
+ width: endCursorPos.x - startCursorPos.x,
425
+ height: line0Info.height,
426
+ fill: "#336df4",
427
+ fillOpacity: .2
428
+ }); else {
429
+ this.editBg.setAttributes({
430
+ x: 0,
431
+ y: line0Info.top,
432
+ width: 0,
433
+ height: 0
434
+ });
435
+ const startIdx = cache.lines.findIndex((item => item === line0Info)), endIdx = cache.lines.findIndex((item => item === line1Info));
436
+ let y = 0;
437
+ for (let i = startIdx; i <= endIdx; i++) {
438
+ const line = cache.lines[i];
439
+ if (i === startIdx) {
440
+ const p = line.paragraphs[line.paragraphs.length - 1];
441
+ this.editBg.add((0, graphic_1.createRect)({
442
+ x: startCursorPos.x,
443
+ y: y,
444
+ width: p.left + p.width - startCursorPos.x,
445
+ height: line.height,
446
+ fill: "#336df4",
447
+ fillOpacity: .2
448
+ }));
449
+ } else if (i === endIdx) {
450
+ const p = line.paragraphs[0];
451
+ this.editBg.add((0, graphic_1.createRect)({
452
+ x: p.left,
453
+ y: y,
454
+ width: endCursorPos.x - p.left,
455
+ height: line.height,
456
+ fill: "#336df4",
457
+ fillOpacity: .2
458
+ }));
459
+ } else {
460
+ const p0 = line.paragraphs[0], p1 = line.paragraphs[line.paragraphs.length - 1];
461
+ this.editBg.add((0, graphic_1.createRect)({
462
+ x: p0.left,
463
+ y: y,
464
+ width: p1.left + p1.width - p0.left,
465
+ height: line.height,
466
+ fill: "#336df4",
467
+ fillOpacity: .2
468
+ }));
469
+ }
470
+ y += line.height;
333
471
  }
334
- y += line.height;
335
472
  }
473
+ this.setCursorAndTextArea(currCursorData.x, currCursorData.y1, currCursorData.y2, this.currRt),
474
+ this.triggerRender(), this.updateCbs.forEach((cb => cb("selection", this)));
336
475
  }
337
- this.setCursorAndTextArea(currCursorData.x, currCursorData.y1 + 2, currCursorData.y2 - 2, this.currRt),
338
- this.triggerRender(), this.updateCbs.forEach((cb => cb("selection", this)));
339
476
  }
340
477
  hideSelection() {
341
478
  this.editBg && (this.editBg.removeAllChild(), this.editBg.setAttributes({
342
479
  fill: "transparent"
343
480
  }));
344
481
  }
482
+ getShadow(rt) {
483
+ const sr = rt.shadowRoot || rt.attachShadow();
484
+ return sr.setAttributes({
485
+ boundsMode: "empty"
486
+ }), sr;
487
+ }
345
488
  getLineByPoint(cache, p1) {
346
489
  let lineInfo = cache.lines[0];
347
490
  for (let i = 0; i < cache.lines.length && !(lineInfo.top <= p1.y && lineInfo.top + lineInfo.height >= p1.y); i++) lineInfo = cache.lines[i + 1];
@@ -373,7 +516,10 @@ class RichTextEditPlugin {
373
516
  return -1;
374
517
  }
375
518
  isRichtext(e) {
376
- return !(!e.target || "richtext" !== e.target.type || !e.target.attribute.editable);
519
+ return !(!e.target || "richtext" !== e.target.type);
520
+ }
521
+ isEditableRichtext(e) {
522
+ return this.isRichtext(e) && !!e.target.attribute.editable;
377
523
  }
378
524
  triggerRender() {
379
525
  this.pluginService.stage.renderNextFrame();
@@ -389,8 +535,15 @@ class RichTextEditPlugin {
389
535
  x: 0,
390
536
  y: 0
391
537
  };
392
- return e.target.globalTransMatrix.transformPoint(p, p1), p1.x -= this.deltaX, p1.y -= this.deltaY,
393
- p1;
538
+ e.target.globalTransMatrix.transformPoint(p, p1), p1.x -= this.deltaX, p1.y -= this.deltaY;
539
+ const rt = this.currRt, {textBaseline: textBaseline} = rt.attribute;
540
+ let dy = 0;
541
+ if ("middle" === textBaseline) {
542
+ dy = (0, graphic_1.getRichTextBounds)(rt.attribute).height() / 2;
543
+ } else if ("bottom" === textBaseline) {
544
+ dy = (0, graphic_1.getRichTextBounds)(rt.attribute).height();
545
+ }
546
+ return p1.y += dy, p1;
394
547
  }
395
548
  setCursorAndTextArea(x, y1, y2, rt) {
396
549
  this.editLine.setAttributes({
@@ -411,15 +564,14 @@ class RichTextEditPlugin {
411
564
  y: y1
412
565
  }, out);
413
566
  const {left: left, top: top} = this.pluginService.stage.window.getBoundingClientRect();
414
- out.x += left, out.y += top, this.editModule.moveTo(out.x, out.y, rt, this.curCursorIdx, this.selectionStartCursorIdx);
567
+ out.x += left, out.y += top, this.offsetLineBgAndShadowBounds(), this.editModule.moveTo(out.x, out.y, rt, this.curCursorIdx, this.selectionStartCursorIdx);
415
568
  }
416
569
  computedCursorPosByEvent(e, cache) {
417
570
  const p1 = this.getEventPosition(e), lineInfo = this.getLineByPoint(cache, p1);
418
571
  if (!lineInfo) return;
419
572
  const {columnInfo: columnInfo, delta: delta} = this.getColumnAndIndexByLinePoint(lineInfo, p1);
420
573
  if (!columnInfo) return;
421
- let y1 = lineInfo.top, y2 = lineInfo.top + lineInfo.height;
422
- y1 += 2, y2 -= 2;
574
+ const y1 = lineInfo.top, y2 = lineInfo.top + lineInfo.height;
423
575
  let cursorIndex = this.getColumnIndex(cache, columnInfo);
424
576
  cursorIndex += delta;
425
577
  return {
@@ -434,17 +586,31 @@ class RichTextEditPlugin {
434
586
  computedCursorPosByCursorIdx(cursorIdx, rt) {
435
587
  var _a, _b, _c;
436
588
  const idx = Math.round(cursorIdx), leftRight = cursorIdx - idx, cache = rt.getFrameCache(), column = this.getColumnByIndex(cache, idx), height = null !== (_a = rt.attribute.fontSize) && void 0 !== _a ? _a : null === (_c = null === (_b = rt.attribute.textConfig) || void 0 === _b ? void 0 : _b[0]) || void 0 === _c ? void 0 : _c.fontSize;
437
- if (!column) return {
438
- x: 0,
439
- y1: 0,
440
- y2: height
441
- };
442
- const {lineInfo: lineInfo, columnInfo: columnInfo} = column;
443
- let y1 = lineInfo.top, y2 = lineInfo.top + lineInfo.height;
444
- return y1 += 2, y2 -= 2, {
589
+ if (!column) {
590
+ if (!cache.lines.length) {
591
+ return {
592
+ x: 0,
593
+ y1: 0,
594
+ y2: (0, graphic_1.getRichTextBounds)(Object.assign(Object.assign({}, rt.attribute), {
595
+ textConfig: [ {
596
+ text: "a"
597
+ } ]
598
+ })).height()
599
+ };
600
+ }
601
+ return {
602
+ x: 0,
603
+ y1: 0,
604
+ y2: height
605
+ };
606
+ }
607
+ const {lineInfo: lineInfo, columnInfo: columnInfo} = column, y1 = lineInfo.top, y2 = lineInfo.top + lineInfo.height;
608
+ return {
445
609
  x: columnInfo.left + (leftRight < 0 ? 0 : columnInfo.width),
446
610
  y1: y1,
447
- y2: y2
611
+ y2: y2,
612
+ lineInfo: lineInfo,
613
+ columnInfo: columnInfo
448
614
  };
449
615
  }
450
616
  getColumnByIndex(cache, index) {
@@ -467,6 +633,30 @@ class RichTextEditPlugin {
467
633
  getSelection(defaultAll = !1) {
468
634
  return this.currRt ? null != this.selectionStartCursorIdx && null != this.curCursorIdx ? new Selection(this.selectionStartCursorIdx, this.curCursorIdx, this.currRt) : defaultAll ? RichTextEditPlugin.CreateSelection(this.currRt) : null : null;
469
635
  }
636
+ forceFocus(params) {
637
+ const {target: target, e: e, cursorIndex: cursorIndex} = params;
638
+ target && (this.currRt = target, e ? this._forceFocusByEvent(e) : this._forceFocusByCursorIndex(null != cursorIndex ? cursorIndex : -.1));
639
+ }
640
+ _forceFocusByEvent(e) {
641
+ this.handleEnter(), this.handlePointerDown(e), this.handlePointerUp(e);
642
+ }
643
+ _forceFocusByCursorIndex(cursorIndex) {
644
+ const richtext = this.currRt;
645
+ if (!richtext) return;
646
+ let x = 0, y1 = 0, y2 = 2, lineInfo = null, columnInfo = null;
647
+ const data = this.computedCursorPosByCursorIdx(cursorIndex, richtext);
648
+ x = data.x, y1 = data.y1, y2 = data.y2, lineInfo = data.lineInfo, columnInfo = data.columnInfo,
649
+ this.onFocus({
650
+ target: this.currRt
651
+ }, {
652
+ x: x,
653
+ y1: y1,
654
+ y2: y2,
655
+ cursorIndex: cursorIndex,
656
+ lineInfo: lineInfo,
657
+ columnInfo: columnInfo
658
+ });
659
+ }
470
660
  }
471
661
 
472
662
  exports.RichTextEditPlugin = RichTextEditPlugin;