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

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 (88) hide show
  1. package/cjs/core/global.d.ts +0 -3
  2. package/cjs/core/global.js +1 -7
  3. package/cjs/core/global.js.map +1 -1
  4. package/cjs/graphic/builtin-symbol/utils.js +1 -1
  5. package/cjs/graphic/builtin-symbol/utils.js.map +1 -1
  6. package/cjs/graphic/config.js +1 -0
  7. package/cjs/graphic/config.js.map +1 -1
  8. package/cjs/graphic/graphic.d.ts +1 -1
  9. package/cjs/graphic/graphic.js +9 -9
  10. package/cjs/graphic/graphic.js.map +1 -1
  11. package/cjs/graphic/group.d.ts +1 -0
  12. package/cjs/graphic/group.js +6 -1
  13. package/cjs/graphic/group.js.map +1 -1
  14. package/cjs/graphic/richtext/line.js +13 -1
  15. package/cjs/graphic/richtext/line.js.map +1 -1
  16. package/cjs/graphic/richtext/paragraph.d.ts +9 -1
  17. package/cjs/graphic/richtext/paragraph.js +50 -11
  18. package/cjs/graphic/richtext/paragraph.js.map +1 -1
  19. package/cjs/graphic/richtext/utils.js +4 -4
  20. package/cjs/graphic/richtext/utils.js.map +1 -1
  21. package/cjs/graphic/richtext.d.ts +2 -1
  22. package/cjs/graphic/richtext.js +10 -1
  23. package/cjs/graphic/richtext.js.map +1 -1
  24. package/cjs/interface/global.d.ts +0 -1
  25. package/cjs/interface/global.js.map +1 -1
  26. package/cjs/interface/graphic/richText.d.ts +11 -0
  27. package/cjs/interface/graphic/richText.js.map +1 -1
  28. package/cjs/interface/graphic.d.ts +1 -1
  29. package/cjs/interface/graphic.js.map +1 -1
  30. package/cjs/plugins/builtin-plugin/edit-module.d.ts +2 -1
  31. package/cjs/plugins/builtin-plugin/edit-module.js +20 -15
  32. package/cjs/plugins/builtin-plugin/edit-module.js.map +1 -1
  33. package/cjs/plugins/builtin-plugin/richtext-edit-plugin.d.ts +37 -11
  34. package/cjs/plugins/builtin-plugin/richtext-edit-plugin.js +329 -136
  35. package/cjs/plugins/builtin-plugin/richtext-edit-plugin.js.map +1 -1
  36. package/cjs/render/contributions/render/contributions/image-contribution-render.d.ts +1 -1
  37. package/cjs/render/contributions/render/contributions/image-contribution-render.js +2 -2
  38. package/cjs/render/contributions/render/contributions/image-contribution-render.js.map +1 -1
  39. package/cjs/render/contributions/render/draw-contribution.js +3 -2
  40. package/cjs/render/contributions/render/draw-contribution.js.map +1 -1
  41. package/cjs/render/contributions/render/image-render.js.map +1 -1
  42. package/cjs/resource-loader/loader.js +1 -1
  43. package/cjs/resource-loader/loader.js.map +1 -1
  44. package/dist/index.es.js +521 -113
  45. package/es/core/global.d.ts +0 -3
  46. package/es/core/global.js +1 -8
  47. package/es/core/global.js.map +1 -1
  48. package/es/graphic/builtin-symbol/utils.js +1 -1
  49. package/es/graphic/builtin-symbol/utils.js.map +1 -1
  50. package/es/graphic/config.js +1 -0
  51. package/es/graphic/config.js.map +1 -1
  52. package/es/graphic/graphic.d.ts +1 -1
  53. package/es/graphic/graphic.js +10 -10
  54. package/es/graphic/graphic.js.map +1 -1
  55. package/es/graphic/group.d.ts +1 -0
  56. package/es/graphic/group.js +6 -1
  57. package/es/graphic/group.js.map +1 -1
  58. package/es/graphic/richtext/line.js +13 -1
  59. package/es/graphic/richtext/line.js.map +1 -1
  60. package/es/graphic/richtext/paragraph.d.ts +9 -1
  61. package/es/graphic/richtext/paragraph.js +50 -11
  62. package/es/graphic/richtext/paragraph.js.map +1 -1
  63. package/es/graphic/richtext/utils.js +4 -4
  64. package/es/graphic/richtext/utils.js.map +1 -1
  65. package/es/graphic/richtext.d.ts +2 -1
  66. package/es/graphic/richtext.js +10 -1
  67. package/es/graphic/richtext.js.map +1 -1
  68. package/es/interface/global.d.ts +0 -1
  69. package/es/interface/global.js.map +1 -1
  70. package/es/interface/graphic/richText.d.ts +11 -0
  71. package/es/interface/graphic/richText.js.map +1 -1
  72. package/es/interface/graphic.d.ts +1 -1
  73. package/es/interface/graphic.js.map +1 -1
  74. package/es/plugins/builtin-plugin/edit-module.d.ts +2 -1
  75. package/es/plugins/builtin-plugin/edit-module.js +17 -13
  76. package/es/plugins/builtin-plugin/edit-module.js.map +1 -1
  77. package/es/plugins/builtin-plugin/richtext-edit-plugin.d.ts +37 -11
  78. package/es/plugins/builtin-plugin/richtext-edit-plugin.js +327 -135
  79. package/es/plugins/builtin-plugin/richtext-edit-plugin.js.map +1 -1
  80. package/es/render/contributions/render/contributions/image-contribution-render.d.ts +1 -1
  81. package/es/render/contributions/render/contributions/image-contribution-render.js +2 -2
  82. package/es/render/contributions/render/contributions/image-contribution-render.js.map +1 -1
  83. package/es/render/contributions/render/draw-contribution.js +2 -2
  84. package/es/render/contributions/render/draw-contribution.js.map +1 -1
  85. package/es/render/contributions/render/image-render.js.map +1 -1
  86. package/es/resource-loader/loader.js +1 -1
  87. package/es/resource-loader/loader.js.map +1 -1
  88. 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,24 +31,26 @@ 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
  }
37
38
  getFormat(key) {
38
39
  return this.getAllFormat(key)[0];
39
40
  }
40
- getAllFormat(key) {
41
+ getAllFormat(key, includeUndefined) {
41
42
  const valSet = new Set, minCursorIdx = Math.min(this.selectionStartCursorIdx, this.curCursorIdx), maxCursorIdx = Math.max(this.selectionStartCursorIdx, this.curCursorIdx);
42
43
  if (minCursorIdx === maxCursorIdx) return [ this._getFormat(key, minCursorIdx) ];
43
44
  for (let i = Math.ceil(minCursorIdx); i <= Math.floor(maxCursorIdx); i++) {
44
45
  const val = this._getFormat(key, i);
45
- val && valSet.add(val);
46
+ (includeUndefined || void 0 !== val) && valSet.add(val);
46
47
  }
47
48
  return Array.from(valSet.values());
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;
@@ -171,7 +202,8 @@ class RichTextEditPlugin {
171
202
  const {lineInfo: lineInfo, columnInfo: columnInfo} = this.getColumnByIndex(cache, Math.round(this.curCursorIdx)), {lines: lines} = cache, totalCursorCount = lines.reduce(((total, line) => total + line.paragraphs.length), 0) - 1;
172
203
  if (x) {
173
204
  x > 0 && columnInfo === lineInfo.paragraphs[lineInfo.paragraphs.length - 2] && this.curCursorIdx < Math.round(this.curCursorIdx) ? this.curCursorIdx = this.curCursorIdx + .2 : x > 0 && columnInfo === lineInfo.paragraphs[lineInfo.paragraphs.length - 1] && this.curCursorIdx > Math.round(this.curCursorIdx) ? this.curCursorIdx = this.curCursorIdx + 1 - .2 : x < 0 && columnInfo === lineInfo.paragraphs[0] && this.curCursorIdx > Math.round(this.curCursorIdx) ? this.curCursorIdx = this.curCursorIdx - .2 : x < 0 && columnInfo === lineInfo.paragraphs[0] && this.curCursorIdx < Math.round(this.curCursorIdx) ? this.curCursorIdx = this.curCursorIdx - 1 + .2 : this.curCursorIdx += x,
174
- this.curCursorIdx < -.1 ? this.curCursorIdx = -.1 : this.curCursorIdx > totalCursorCount + .1 && (this.curCursorIdx = totalCursorCount + .1);
205
+ this.curCursorIdx < -.1 ? this.curCursorIdx = -.1 : this.curCursorIdx > totalCursorCount + .1 && (this.curCursorIdx = totalCursorCount + .1),
206
+ this.selectionStartCursorIdx = this.curCursorIdx;
175
207
  const pos = this.computedCursorPosByCursorIdx(this.curCursorIdx, this.currRt);
176
208
  this.setCursorAndTextArea(pos.x, pos.y1, pos.y2, this.currRt), this.hideSelection();
177
209
  }
@@ -195,60 +227,166 @@ class RichTextEditPlugin {
195
227
  }
196
228
  return !0;
197
229
  }
230
+ tryShowShadowPlaceholder() {
231
+ if (!this.currRt) return;
232
+ const shadowRoot = this.currRt.shadowRoot;
233
+ if (shadowRoot) {
234
+ const placeholder = shadowRoot.getElementsByType("richtext")[0];
235
+ placeholder && shadowRoot.removeChild(placeholder);
236
+ }
237
+ const {textConfig: textConfig, editOptions: editOptions} = this.currRt.attribute;
238
+ if (textConfig && textConfig.length) return;
239
+ if (!editOptions || !editOptions.placeholder) return;
240
+ 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,
241
+ edit_module_1.getDefaultCharacterConfig)(this.currRt.attribute)), {
242
+ text: placeholder
243
+ });
244
+ placeholderColor && (textConfigItem.fill = placeholderColor), placeholderFontFamily && (textConfigItem.fontFamily = placeholderFontFamily),
245
+ placeholderFontSize && (textConfigItem.fontSize = placeholderFontSize), this.shadowPlaceHolder = (0,
246
+ graphic_1.createRichText)(Object.assign(Object.assign({}, this.currRt.attribute), {
247
+ x: 0,
248
+ y: 0,
249
+ pickable: !1,
250
+ editable: !1,
251
+ editOptions: null,
252
+ angle: 0,
253
+ _debug_bounds: !1,
254
+ textConfig: [ textConfigItem ]
255
+ })), shadow.add(this.shadowPlaceHolder);
256
+ }
257
+ tryShowInputBounds() {
258
+ if (!this.currRt || !this.focusing) return;
259
+ const {editOptions: editOptions = {}} = this.currRt.attribute, {boundsStrokeWhenInput: boundsStrokeWhenInput} = editOptions;
260
+ if (!editOptions || !boundsStrokeWhenInput) return;
261
+ const {attribute: attribute} = this.currRt;
262
+ let b = this.currRt.AABBBounds, h = b.height();
263
+ if (!attribute.textConfig.length && this.editLine) {
264
+ const {points: points} = this.editLine.attribute;
265
+ h = points[1].y - points[0].y, b = (0, graphic_1.getRichTextBounds)(Object.assign({}, this.shadowPlaceHolder.attribute));
266
+ }
267
+ this.shadowBounds = this.shadowBounds || (0, graphic_1.createRect)({}), this.shadowBounds.setAttributes({
268
+ x: 0,
269
+ y: 0,
270
+ width: b.width(),
271
+ height: h,
272
+ fill: !1,
273
+ stroke: boundsStrokeWhenInput,
274
+ lineWidth: 1,
275
+ zIndex: -1
276
+ });
277
+ this.getShadow(this.currRt).add(this.shadowBounds), this.offsetLineBgAndShadowBounds();
278
+ }
279
+ trySyncPlaceholderToTextConfig() {
280
+ if (!this.currRt) return;
281
+ const {textConfig: textConfig, editOptions: editOptions} = this.currRt.attribute;
282
+ if (textConfig && textConfig.length) return;
283
+ if (!(editOptions && editOptions.placeholder && editOptions.syncPlaceHolderToTextConfig)) return;
284
+ const {placeholder: placeholder} = editOptions;
285
+ this.currRt.setAttributes({
286
+ textConfig: [ Object.assign({
287
+ text: placeholder
288
+ }, (0, edit_module_1.getDefaultCharacterConfig)(this.currRt.attribute)) ]
289
+ });
290
+ }
198
291
  deactivate(context) {
199
292
  context.stage.off("pointermove", this.handleMove), context.stage.off("pointerdown", this.handlePointerDown),
200
293
  context.stage.off("pointerup", this.handlePointerUp), context.stage.off("pointerleave", this.handlePointerUp),
201
294
  context.stage.off("dblclick", this.handleDBLClick), application_1.application.global.addEventListener("keydown", this.handleKeyDown);
202
295
  }
203
- onFocus(e) {
204
- this.deFocus(), this.currRt = e.target;
296
+ onFocus(e, data) {
297
+ this.updateCbs && this.updateCbs.forEach((cb => cb("beforeOnfocus", this))), this.deFocus(!1),
298
+ this.focusing = !0;
205
299
  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);
300
+ if (!target || "richtext" !== target.type) return;
301
+ this.currRt = target, RichTextEditPlugin.tryUpdateRichtext(target);
302
+ const shadowRoot = this.getShadow(target), cache = target.getFrameCache();
303
+ if (cache) {
304
+ if (this.computeGlobalDelta(cache), shadowRoot.setAttributes({
305
+ shadowRootIdx: 1,
306
+ pickable: !1,
307
+ x: this.deltaX,
308
+ y: this.deltaY
309
+ }), !this.editLine) {
310
+ const line = (0, graphic_1.createLine)({
311
+ x: 0,
312
+ y: 0,
313
+ lineWidth: 1,
314
+ stroke: "black"
315
+ });
316
+ this.addAnimateToLine(line), this.editLine = line, this.ticker.start(!0);
317
+ const g = (0, graphic_1.createGroup)({
318
+ x: 0,
319
+ y: 0,
320
+ width: 0,
321
+ height: 0
322
+ });
323
+ this.editBg = g, shadowRoot.add(this.editLine), shadowRoot.add(this.editBg);
324
+ }
325
+ if (data = data || this.computedCursorPosByEvent(e, cache)) {
326
+ const {x: x, y1: y1, y2: y2, cursorIndex: cursorIndex} = data;
327
+ this.startCursorPos = {
328
+ x: x,
329
+ y: (y1 + y2) / 2
330
+ }, this.curCursorIdx = cursorIndex, this.selectionStartCursorIdx = cursorIndex,
331
+ this.setCursorAndTextArea(x, y1, y2, target);
332
+ } else {
333
+ const x = 0, y1 = 0, y2 = (0, graphic_1.getRichTextBounds)(Object.assign(Object.assign({}, target.attribute), {
334
+ textConfig: [ {
335
+ text: "a"
336
+ } ]
337
+ })).height();
338
+ this.startCursorPos = {
339
+ x: x,
340
+ y: (y1 + y2) / 2
341
+ }, this.curCursorIdx = -.1, this.selectionStartCursorIdx = -.1, this.setCursorAndTextArea(x, y1, y2, target);
342
+ }
343
+ this.tryShowShadowPlaceholder(), this.tryShowInputBounds(), this.currRt.addUpdateBoundTag();
238
344
  }
239
345
  }
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));
346
+ offsetLineBgAndShadowBounds() {
347
+ const rt = this.currRt, {textBaseline: textBaseline} = rt.attribute;
348
+ let b, dy = 0, attr = rt.attribute;
349
+ "middle" !== textBaseline && "bottom" !== textBaseline || (attr.textConfig.length || (attr = Object.assign(Object.assign({}, attr), {
350
+ textConfig: [ {
351
+ text: "a"
352
+ } ]
353
+ })), b = (0, graphic_1.getRichTextBounds)(attr)), "middle" === textBaseline ? dy = -b.height() / 2 : "bottom" === textBaseline && (dy = -b.height()),
354
+ this.editLine && this.editLine.setAttributes({
355
+ dy: dy
356
+ }), this.editBg && this.editBg.setAttributes({
357
+ dy: dy
358
+ }), this.shadowBounds && this.shadowBounds.setAttributes({
359
+ dy: dy
360
+ });
361
+ }
362
+ deFocus(trulyDeFocus = !1) {
363
+ const currRt = this.currRt;
364
+ if (!currRt) return;
365
+ this.updateCbs && this.updateCbs.forEach((cb => cb("beforeDefocus", this, {
366
+ trulyDeFocus: trulyDeFocus
367
+ }))), trulyDeFocus && (this.trySyncPlaceholderToTextConfig(), currRt.detachShadow()),
368
+ this.currRt = null, this.editLine && (this.editLine.parent && this.editLine.parent.removeChild(this.editLine),
369
+ this.editLine.release(), this.editLine = null, this.editBg.parent && this.editBg.parent.removeChild(this.editBg),
370
+ this.editBg.release(), this.editBg = null), trulyDeFocus && (this.shadowBounds && (this.shadowBounds.parent && this.shadowBounds.parent.removeChild(this.shadowBounds),
371
+ this.shadowBounds.release(), this.shadowBounds = null), this.shadowPlaceHolder && (this.shadowPlaceHolder.parent && this.shadowPlaceHolder.parent.removeChild(this.shadowPlaceHolder),
372
+ this.shadowPlaceHolder.release(), this.shadowPlaceHolder = null)), this.focusing = !1;
373
+ const textConfig = currRt.attribute.textConfig;
374
+ let lastConfig = textConfig[textConfig.length - 1], cleared = !1;
375
+ for (;lastConfig && "\n" === lastConfig.text; ) textConfig.pop(), lastConfig = textConfig[textConfig.length - 1],
376
+ cleared = !0;
377
+ cleared && currRt.setAttributes({
378
+ textConfig: textConfig
379
+ }), currRt.removeEventListener("pointerleave", this.handleLeave);
245
380
  }
246
381
  addAnimateToLine(line) {
247
- line.animates && line.animates.forEach((animate => {
382
+ line.setAttributes({
383
+ opacity: 1
384
+ }), line.animates && line.animates.forEach((animate => {
248
385
  animate.stop(), animate.release();
249
386
  }));
250
- const animate = line.animate();
251
- animate.setTimeline(this.timeline), animate.to({
387
+ line.animate({
388
+ timeline: this.timeline
389
+ }).to({
252
390
  opacity: 1
253
391
  }, 10, "linear").wait(700).to({
254
392
  opacity: 0
@@ -281,67 +419,75 @@ class RichTextEditPlugin {
281
419
  x: currCursorData.x,
282
420
  y: (currCursorData.y1 + currCursorData.y2) / 2
283
421
  }, 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,
422
+ if (line0Info && line1Info) {
423
+ if ((startCursorPos.y > endCursorPos.y || startCursorPos.y === endCursorPos.y && startCursorPos.x > endCursorPos.x) && ([startCursorPos, endCursorPos] = [ endCursorPos, startCursorPos ],
424
+ [line1Info, line0Info] = [ line0Info, line1Info ]), this.hideSelection(), line0Info === line1Info) this.editBg.setAttributes({
425
+ x: startCursorPos.x,
295
426
  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
- }));
427
+ width: endCursorPos.x - startCursorPos.x,
428
+ height: line0Info.height,
429
+ fill: "#336df4",
430
+ fillOpacity: .2
431
+ }); else {
432
+ this.editBg.setAttributes({
433
+ x: 0,
434
+ y: line0Info.top,
435
+ width: 0,
436
+ height: 0
437
+ });
438
+ const startIdx = cache.lines.findIndex((item => item === line0Info)), endIdx = cache.lines.findIndex((item => item === line1Info));
439
+ let y = 0;
440
+ for (let i = startIdx; i <= endIdx; i++) {
441
+ const line = cache.lines[i];
442
+ if (i === startIdx) {
443
+ const p = line.paragraphs[line.paragraphs.length - 1];
444
+ this.editBg.add((0, graphic_1.createRect)({
445
+ x: startCursorPos.x,
446
+ y: y,
447
+ width: p.left + p.width - startCursorPos.x,
448
+ height: line.height,
449
+ fill: "#336df4",
450
+ fillOpacity: .2
451
+ }));
452
+ } else if (i === endIdx) {
453
+ const p = line.paragraphs[0];
454
+ this.editBg.add((0, graphic_1.createRect)({
455
+ x: p.left,
456
+ y: y,
457
+ width: endCursorPos.x - p.left,
458
+ height: line.height,
459
+ fill: "#336df4",
460
+ fillOpacity: .2
461
+ }));
462
+ } else {
463
+ const p0 = line.paragraphs[0], p1 = line.paragraphs[line.paragraphs.length - 1];
464
+ this.editBg.add((0, graphic_1.createRect)({
465
+ x: p0.left,
466
+ y: y,
467
+ width: p1.left + p1.width - p0.left,
468
+ height: line.height,
469
+ fill: "#336df4",
470
+ fillOpacity: .2
471
+ }));
472
+ }
473
+ y += line.height;
333
474
  }
334
- y += line.height;
335
475
  }
476
+ this.setCursorAndTextArea(currCursorData.x, currCursorData.y1, currCursorData.y2, this.currRt),
477
+ this.triggerRender(), this.updateCbs.forEach((cb => cb("selection", this)));
336
478
  }
337
- this.setCursorAndTextArea(currCursorData.x, currCursorData.y1 + 2, currCursorData.y2 - 2, this.currRt),
338
- this.triggerRender(), this.updateCbs.forEach((cb => cb("selection", this)));
339
479
  }
340
480
  hideSelection() {
341
481
  this.editBg && (this.editBg.removeAllChild(), this.editBg.setAttributes({
342
482
  fill: "transparent"
343
483
  }));
344
484
  }
485
+ getShadow(rt) {
486
+ const sr = rt.shadowRoot || rt.attachShadow();
487
+ return sr.setAttributes({
488
+ boundsMode: "empty"
489
+ }), sr;
490
+ }
345
491
  getLineByPoint(cache, p1) {
346
492
  let lineInfo = cache.lines[0];
347
493
  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 +519,10 @@ class RichTextEditPlugin {
373
519
  return -1;
374
520
  }
375
521
  isRichtext(e) {
376
- return !(!e.target || "richtext" !== e.target.type || !e.target.attribute.editable);
522
+ return !(!e.target || "richtext" !== e.target.type);
523
+ }
524
+ isEditableRichtext(e) {
525
+ return this.isRichtext(e) && !!e.target.attribute.editable;
377
526
  }
378
527
  triggerRender() {
379
528
  this.pluginService.stage.renderNextFrame();
@@ -389,8 +538,15 @@ class RichTextEditPlugin {
389
538
  x: 0,
390
539
  y: 0
391
540
  };
392
- return e.target.globalTransMatrix.transformPoint(p, p1), p1.x -= this.deltaX, p1.y -= this.deltaY,
393
- p1;
541
+ e.target.globalTransMatrix.transformPoint(p, p1), p1.x -= this.deltaX, p1.y -= this.deltaY;
542
+ const rt = this.currRt, {textBaseline: textBaseline} = rt.attribute;
543
+ let dy = 0;
544
+ if ("middle" === textBaseline) {
545
+ dy = (0, graphic_1.getRichTextBounds)(rt.attribute).height() / 2;
546
+ } else if ("bottom" === textBaseline) {
547
+ dy = (0, graphic_1.getRichTextBounds)(rt.attribute).height();
548
+ }
549
+ return p1.y += dy, p1;
394
550
  }
395
551
  setCursorAndTextArea(x, y1, y2, rt) {
396
552
  this.editLine.setAttributes({
@@ -411,15 +567,14 @@ class RichTextEditPlugin {
411
567
  y: y1
412
568
  }, out);
413
569
  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);
570
+ out.x += left, out.y += top, this.offsetLineBgAndShadowBounds(), this.editModule.moveTo(out.x, out.y, rt, this.curCursorIdx, this.selectionStartCursorIdx);
415
571
  }
416
572
  computedCursorPosByEvent(e, cache) {
417
573
  const p1 = this.getEventPosition(e), lineInfo = this.getLineByPoint(cache, p1);
418
574
  if (!lineInfo) return;
419
575
  const {columnInfo: columnInfo, delta: delta} = this.getColumnAndIndexByLinePoint(lineInfo, p1);
420
576
  if (!columnInfo) return;
421
- let y1 = lineInfo.top, y2 = lineInfo.top + lineInfo.height;
422
- y1 += 2, y2 -= 2;
577
+ const y1 = lineInfo.top, y2 = lineInfo.top + lineInfo.height;
423
578
  let cursorIndex = this.getColumnIndex(cache, columnInfo);
424
579
  cursorIndex += delta;
425
580
  return {
@@ -434,17 +589,31 @@ class RichTextEditPlugin {
434
589
  computedCursorPosByCursorIdx(cursorIdx, rt) {
435
590
  var _a, _b, _c;
436
591
  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, {
592
+ if (!column) {
593
+ if (!cache.lines.length) {
594
+ return {
595
+ x: 0,
596
+ y1: 0,
597
+ y2: (0, graphic_1.getRichTextBounds)(Object.assign(Object.assign({}, rt.attribute), {
598
+ textConfig: [ {
599
+ text: "a"
600
+ } ]
601
+ })).height()
602
+ };
603
+ }
604
+ return {
605
+ x: 0,
606
+ y1: 0,
607
+ y2: height
608
+ };
609
+ }
610
+ const {lineInfo: lineInfo, columnInfo: columnInfo} = column, y1 = lineInfo.top, y2 = lineInfo.top + lineInfo.height;
611
+ return {
445
612
  x: columnInfo.left + (leftRight < 0 ? 0 : columnInfo.width),
446
613
  y1: y1,
447
- y2: y2
614
+ y2: y2,
615
+ lineInfo: lineInfo,
616
+ columnInfo: columnInfo
448
617
  };
449
618
  }
450
619
  getColumnByIndex(cache, index) {
@@ -467,6 +636,30 @@ class RichTextEditPlugin {
467
636
  getSelection(defaultAll = !1) {
468
637
  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
638
  }
639
+ forceFocus(params) {
640
+ const {target: target, e: e, cursorIndex: cursorIndex} = params;
641
+ target && (this.currRt = target, e ? this._forceFocusByEvent(e) : this._forceFocusByCursorIndex(null != cursorIndex ? cursorIndex : -.1));
642
+ }
643
+ _forceFocusByEvent(e) {
644
+ this.handleEnter(), this.handlePointerDown(e), this.handlePointerUp(e);
645
+ }
646
+ _forceFocusByCursorIndex(cursorIndex) {
647
+ const richtext = this.currRt;
648
+ if (!richtext) return;
649
+ let x = 0, y1 = 0, y2 = 2, lineInfo = null, columnInfo = null;
650
+ const data = this.computedCursorPosByCursorIdx(cursorIndex, richtext);
651
+ x = data.x, y1 = data.y1, y2 = data.y2, lineInfo = data.lineInfo, columnInfo = data.columnInfo,
652
+ this.onFocus({
653
+ target: this.currRt
654
+ }, {
655
+ x: x,
656
+ y1: y1,
657
+ y2: y2,
658
+ cursorIndex: cursorIndex,
659
+ lineInfo: lineInfo,
660
+ columnInfo: columnInfo
661
+ });
662
+ }
470
663
  }
471
664
 
472
665
  exports.RichTextEditPlugin = RichTextEditPlugin;