@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,11 +2,11 @@ import { isObject, merge } from "@visactor/vutils";
2
2
 
3
3
  import { Generator } from "../../common/generator";
4
4
 
5
- import { createGroup, createLine, createRect, RichText } from "../../graphic";
5
+ import { createGroup, createLine, createRect, createRichText, getRichTextBounds, RichText } from "../../graphic";
6
6
 
7
7
  import { DefaultTicker, DefaultTimeline } from "../../animate";
8
8
 
9
- import { EditModule, findConfigIndexByCursorIdx } from "./edit-module";
9
+ import { EditModule, findConfigIndexByCursorIdx, getDefaultCharacterConfig } from "./edit-module";
10
10
 
11
11
  import { application } from "../../application";
12
12
 
@@ -36,6 +36,7 @@ class Selection {
36
36
  if (!this.rt) return null;
37
37
  let idx = Math.round(cursorIdx);
38
38
  const config = this.rt.attribute.textConfig;
39
+ if (!config.length) return null;
39
40
  for (let i = 0; i < config.length; i++) if ("\n" !== config[i].text && (idx--, idx < 0)) return config[i][key];
40
41
  return null !== (_a = config[Math.min(idx, config.length - 1)][key]) && void 0 !== _a ? _a : this.rt.attribute[key];
41
42
  }
@@ -55,6 +56,8 @@ class Selection {
55
56
 
56
57
  export const FORMAT_TEXT_COMMAND = "FORMAT_TEXT_COMMAND";
57
58
 
59
+ export const FORMAT_ALL_TEXT_COMMAND = "FORMAT_ALL_TEXT_COMMAND";
60
+
58
61
  export const FORMAT_ELEMENT_COMMAND = "FORMAT_ELEMENT_COMMAND";
59
62
 
60
63
  export class RichTextEditPlugin {
@@ -74,43 +77,59 @@ export class RichTextEditPlugin {
74
77
  }
75
78
  constructor() {
76
79
  this.name = "RichTextEditPlugin", this.activeEvent = "onRegister", this._uid = Generator.GenAutoIncrementId(),
77
- this.key = this.name + this._uid, this.editing = !1, this.pointerDown = !1, this.handleKeyDown = e => {
80
+ this.key = this.name + this._uid, this.editing = !1, this.focusing = !1, this.pointerDown = !1,
81
+ this.formatTextCommandCb = (payload, p) => {
82
+ const rt = p.currRt;
83
+ if (!rt) return;
84
+ const selectionData = p.getSelection();
85
+ if (!selectionData) return;
86
+ const {selectionStartCursorIdx: selectionStartCursorIdx, curCursorIdx: curCursorIdx} = selectionData, minCursorIdx = Math.min(selectionStartCursorIdx, curCursorIdx), maxCursorIdx = Math.max(selectionStartCursorIdx, curCursorIdx), minConfigIdx = findConfigIndexByCursorIdx(rt.attribute.textConfig, minCursorIdx), maxConfigIdx = findConfigIndexByCursorIdx(rt.attribute.textConfig, maxCursorIdx), config = rt.attribute.textConfig.slice(minConfigIdx, maxConfigIdx);
87
+ this._formatTextCommand(payload, config, rt);
88
+ }, this.formatAllTextCommandCb = (payload, p) => {
89
+ const rt = p.currRt;
90
+ if (!rt) return;
91
+ const config = rt.attribute.textConfig;
92
+ this._formatTextCommand(payload, config, rt);
93
+ }, this.handleKeyDown = e => {
78
94
  this.currRt && this.editing && (this.copyToClipboard(e) || this.fullSelection(e) || this.directKey(e));
79
95
  }, this.handleInput = (text, isComposing, cursorIdx, rt) => {
80
- this.hideSelection(), this.updateCbs.forEach((cb => cb("input", this)));
96
+ this.currRt && (this.tryShowShadowPlaceholder(), this.tryShowInputBounds(), this.hideSelection(),
97
+ this.updateCbs.forEach((cb => cb("input", this))));
81
98
  }, this.handleChange = (text, isComposing, cursorIdx, rt) => {
82
- this.curCursorIdx = cursorIdx, this.selectionStartCursorIdx = cursorIdx;
99
+ if (!this.currRt) return;
100
+ this.tryShowShadowPlaceholder(), this.tryShowInputBounds(), this.curCursorIdx = cursorIdx,
101
+ this.selectionStartCursorIdx = cursorIdx;
83
102
  const p = this.computedCursorPosByCursorIdx(cursorIdx, rt);
84
103
  this.setCursorAndTextArea(p.x, p.y1, p.y2, rt), this.hideSelection(), this.updateCbs.forEach((cb => cb("change", this)));
85
- }, this.handleFocusIn = () => {}, this.handleFocusOut = () => {
86
- this.editing = !1, this.deFocus(), this.pointerDown = !1, this.triggerRender(),
87
- this.updateCbs.forEach((cb => cb("defocus", this)));
104
+ }, this.handleFocusIn = () => {
105
+ throw new Error("不会走到这里 handleFocusIn");
106
+ }, this.handleFocusOut = () => {
107
+ throw new Error("不会走到这里 handleFocusOut");
88
108
  }, this.handleMove = e => {
89
- this.isRichtext(e) && (this.currRt = e.target, this.handleEnter(e), e.target.once("pointerleave", this.handleLeave),
90
- this.tryShowSelection(e, !1));
91
- }, this.handleEnter = e => {
109
+ this.currRt && !this.currRt.attribute.editable && this.deFocus(!0), this.isEditableRichtext(e) ? (this.handleEnter(),
110
+ e.target.once("pointerleave", this.handleLeave), this.tryShowSelection(e, !1)) : this.handleLeave();
111
+ }, this.handleEnter = () => {
92
112
  this.editing = !0, this.pluginService.stage.setCursor("text");
93
- }, this.handleLeave = e => {
113
+ }, this.handleLeave = () => {
94
114
  this.editing = !1, this.pluginService.stage.setCursor("default");
95
115
  }, this.handlePointerDown = e => {
96
- this.editing ? this.onFocus(e) : this.deFocus(), this.triggerRender(), this.pointerDown = !0,
97
- this.updateCbs.forEach((cb => cb(this.editing ? "onfocus" : "defocus", this)));
116
+ this.editing && this.isEditableRichtext(e) ? this.onFocus(e) : this.deFocus(!0),
117
+ this.triggerRender(), this.pointerDown = !0, this.updateCbs.forEach((cb => cb(this.editing ? "onfocus" : "defocus", this)));
98
118
  }, this.handlePointerUp = e => {
99
119
  this.pointerDown = !1;
100
120
  }, this.handleDBLClick = e => {
101
121
  this.editing && this.tryShowSelection(e, !0);
102
122
  }, this.commandCbs = new Map, this.commandCbs.set(FORMAT_TEXT_COMMAND, [ this.formatTextCommandCb ]),
103
- this.updateCbs = [], this.timeline = new DefaultTimeline, this.ticker = new DefaultTicker([ this.timeline ]),
123
+ this.commandCbs.set(FORMAT_ALL_TEXT_COMMAND, [ this.formatAllTextCommandCb ]), this.updateCbs = [],
124
+ this.timeline = new DefaultTimeline, this.ticker = new DefaultTicker([ this.timeline ]),
104
125
  this.deltaX = 0, this.deltaY = 0;
105
126
  }
106
- formatTextCommandCb(payload, p) {
107
- const rt = p.currRt;
108
- if (!rt) return;
109
- const selectionData = p.getSelection();
110
- if (!selectionData) return;
111
- const {selectionStartCursorIdx: selectionStartCursorIdx, curCursorIdx: curCursorIdx} = selectionData, minCursorIdx = Math.min(selectionStartCursorIdx, curCursorIdx), maxCursorIdx = Math.max(selectionStartCursorIdx, curCursorIdx), minConfigIdx = findConfigIndexByCursorIdx(rt.attribute.textConfig, minCursorIdx), maxConfigIdx = findConfigIndexByCursorIdx(rt.attribute.textConfig, maxCursorIdx), config = rt.attribute.textConfig.slice(minConfigIdx, maxConfigIdx);
127
+ _formatTextCommand(payload, config, rt) {
112
128
  "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)) : isObject(payload) && config.forEach((item => merge(item, payload))),
113
129
  rt.setAttributes(rt.attribute);
130
+ const cache = rt.getFrameCache();
131
+ cache && (this.selectionRangeByCursorIdx(this.selectionStartCursorIdx, this.curCursorIdx, cache),
132
+ this.tryShowInputBounds());
114
133
  }
115
134
  dispatchCommand(command, payload) {
116
135
  const cbs = this.commandCbs.get(command);
@@ -119,9 +138,17 @@ export class RichTextEditPlugin {
119
138
  registerCommand(command, cb) {
120
139
  (this.commandCbs.get(command) || []).push(cb);
121
140
  }
141
+ removeCommand(command, cb) {
142
+ const cbs = this.commandCbs.get(command) || [], idx = cbs.indexOf(cb);
143
+ idx > -1 && cbs.splice(idx, 1);
144
+ }
122
145
  registerUpdateListener(cb) {
123
146
  (this.updateCbs || []).push(cb);
124
147
  }
148
+ removeUpdateListener(cb) {
149
+ const cbs = this.updateCbs || [], idx = cbs.indexOf(cb);
150
+ idx > -1 && cbs.splice(idx, 1);
151
+ }
125
152
  activate(context) {
126
153
  this.pluginService = context, this.editModule = new EditModule, context.stage.on("pointermove", this.handleMove),
127
154
  context.stage.on("pointerdown", this.handlePointerDown), context.stage.on("pointerup", this.handlePointerUp),
@@ -143,8 +170,10 @@ export class RichTextEditPlugin {
143
170
  if (!cache) return;
144
171
  const {lines: lines} = cache, totalCursorCount = lines.reduce(((total, line) => total + line.paragraphs.length), 0) - 1;
145
172
  startIdx > endIdx && ([startIdx, endIdx] = [ endIdx, startIdx ]), startIdx = Math.min(Math.max(startIdx, -.1), totalCursorCount + .1),
146
- endIdx = Math.min(Math.max(endIdx, -.1), totalCursorCount + .1), this.curCursorIdx = endIdx,
147
- this.selectionStartCursorIdx = startIdx;
173
+ endIdx = Math.min(Math.max(endIdx, -.1), totalCursorCount + .1), this.selectionRangeByCursorIdx(startIdx, endIdx, cache);
174
+ }
175
+ selectionRangeByCursorIdx(startCursorIdx, endCursorIdx, cache) {
176
+ this.curCursorIdx = endCursorIdx, this.selectionStartCursorIdx = startCursorIdx;
148
177
  const {x: x, y1: y1, y2: y2} = this.computedCursorPosByCursorIdx(this.selectionStartCursorIdx, this.currRt);
149
178
  this.startCursorPos = {
150
179
  x: x,
@@ -159,7 +188,10 @@ export class RichTextEditPlugin {
159
188
  if (!currRt) return;
160
189
  const cache = currRt.getFrameCache();
161
190
  if (!cache) return;
162
- const {lines: lines} = cache, totalCursorCount = lines.reduce(((total, line) => total + line.paragraphs.length), 0) - 1;
191
+ const {lines: lines} = cache;
192
+ if (0 === lines.length) return;
193
+ if (!lines[0].paragraphs || 0 === lines[0].paragraphs.length) return;
194
+ const totalCursorCount = lines.reduce(((total, line) => total + line.paragraphs.length), 0) - 1;
163
195
  return this.selectionRange(-.1, totalCursorCount + .1), e.preventDefault(), !0;
164
196
  }
165
197
  return !1;
@@ -197,60 +229,162 @@ export class RichTextEditPlugin {
197
229
  }
198
230
  return !0;
199
231
  }
232
+ tryShowShadowPlaceholder() {
233
+ if (!this.currRt) return;
234
+ const shadowRoot = this.currRt.shadowRoot;
235
+ if (shadowRoot) {
236
+ const placeholder = shadowRoot.getElementsByType("richtext")[0];
237
+ placeholder && shadowRoot.removeChild(placeholder);
238
+ }
239
+ const {textConfig: textConfig, editOptions: editOptions} = this.currRt.attribute;
240
+ if (textConfig && textConfig.length) return;
241
+ if (!editOptions || !editOptions.placeholder) return;
242
+ 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({}, getDefaultCharacterConfig(this.currRt.attribute)), {
243
+ text: placeholder
244
+ });
245
+ placeholderColor && (textConfigItem.fill = placeholderColor), placeholderFontFamily && (textConfigItem.fontFamily = placeholderFontFamily),
246
+ placeholderFontSize && (textConfigItem.fontSize = placeholderFontSize), this.shadowPlaceHolder = 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 = getRichTextBounds(Object.assign({}, this.shadowPlaceHolder.attribute));
266
+ }
267
+ this.shadowBounds = this.shadowBounds || 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
+ }, getDefaultCharacterConfig(this.currRt.attribute)) ]
289
+ });
290
+ }
200
291
  deactivate(context) {
201
292
  context.stage.off("pointermove", this.handleMove), context.stage.off("pointerdown", this.handlePointerDown),
202
293
  context.stage.off("pointerup", this.handlePointerUp), context.stage.off("pointerleave", this.handlePointerUp),
203
294
  context.stage.off("dblclick", this.handleDBLClick), application.global.addEventListener("keydown", this.handleKeyDown);
204
295
  }
205
- onFocus(e) {
206
- 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;
207
299
  const target = e.target;
208
- RichTextEditPlugin.tryUpdateRichtext(target);
209
- const shadowRoot = target.attachShadow(), cache = target.getFrameCache();
210
- if (!cache) return;
211
- if (this.computeGlobalDelta(cache), shadowRoot.setAttributes({
212
- shadowRootIdx: 1,
213
- pickable: !1,
214
- x: this.deltaX,
215
- y: this.deltaY
216
- }), !this.editLine) {
217
- const line = createLine({
218
- x: 0,
219
- y: 0,
220
- lineWidth: 1,
221
- stroke: "black"
222
- });
223
- this.addAnimateToLine(line), this.editLine = line, this.ticker.start(!0);
224
- const g = createGroup({
225
- x: 0,
226
- y: 0,
227
- width: 0,
228
- height: 0
229
- });
230
- this.editBg = g, shadowRoot.add(this.editLine), shadowRoot.add(this.editBg);
231
- }
232
- const data = this.computedCursorPosByEvent(e, cache);
233
- if (data) {
234
- const {x: x, y1: y1, y2: y2, cursorIndex: cursorIndex} = data;
235
- this.startCursorPos = {
236
- x: x,
237
- y: (y1 + y2) / 2
238
- }, this.curCursorIdx = cursorIndex, this.selectionStartCursorIdx = cursorIndex,
239
- 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 = 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 = 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 = 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();
240
344
  }
241
345
  }
242
- deFocus() {
243
- const target = this.currRt;
244
- target && (target.detachShadow(), this.currRt = null, this.editLine && (this.editLine.parent.removeChild(this.editLine),
245
- this.editLine.release(), this.editLine = null, this.editBg.parent.removeChild(this.editBg),
246
- 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 = 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);
247
380
  }
248
381
  addAnimateToLine(line) {
249
382
  line.animates && line.animates.forEach((animate => {
250
383
  animate.stop(), animate.release();
251
384
  }));
252
- const animate = line.animate();
253
- animate.setTimeline(this.timeline), animate.to({
385
+ line.animate({
386
+ timeline: this.timeline
387
+ }).to({
254
388
  opacity: 1
255
389
  }, 10, "linear").wait(700).to({
256
390
  opacity: 0
@@ -283,67 +417,75 @@ export class RichTextEditPlugin {
283
417
  x: currCursorData.x,
284
418
  y: (currCursorData.y1 + currCursorData.y2) / 2
285
419
  }, line0Info = this.getLineByPoint(cache, startCursorPos), line1Info = this.getLineByPoint(cache, endCursorPos);
286
- if ((startCursorPos.y > endCursorPos.y || startCursorPos.y === endCursorPos.y && startCursorPos.x > endCursorPos.x) && ([startCursorPos, endCursorPos] = [ endCursorPos, startCursorPos ],
287
- [line1Info, line0Info] = [ line0Info, line1Info ]), this.hideSelection(), line0Info === line1Info) this.editBg.setAttributes({
288
- x: startCursorPos.x,
289
- y: line0Info.top,
290
- width: endCursorPos.x - startCursorPos.x,
291
- height: line0Info.height,
292
- fill: "#336df4",
293
- fillOpacity: .2
294
- }); else {
295
- this.editBg.setAttributes({
296
- x: 0,
420
+ if (line0Info && line1Info) {
421
+ if ((startCursorPos.y > endCursorPos.y || startCursorPos.y === endCursorPos.y && startCursorPos.x > endCursorPos.x) && ([startCursorPos, endCursorPos] = [ endCursorPos, startCursorPos ],
422
+ [line1Info, line0Info] = [ line0Info, line1Info ]), this.hideSelection(), line0Info === line1Info) this.editBg.setAttributes({
423
+ x: startCursorPos.x,
297
424
  y: line0Info.top,
298
- width: 0,
299
- height: 0
300
- });
301
- const startIdx = cache.lines.findIndex((item => item === line0Info)), endIdx = cache.lines.findIndex((item => item === line1Info));
302
- let y = 0;
303
- for (let i = startIdx; i <= endIdx; i++) {
304
- const line = cache.lines[i];
305
- if (i === startIdx) {
306
- const p = line.paragraphs[line.paragraphs.length - 1];
307
- this.editBg.add(createRect({
308
- x: startCursorPos.x,
309
- y: y,
310
- width: p.left + p.width - startCursorPos.x,
311
- height: line.height,
312
- fill: "#336df4",
313
- fillOpacity: .2
314
- }));
315
- } else if (i === endIdx) {
316
- const p = line.paragraphs[0];
317
- this.editBg.add(createRect({
318
- x: p.left,
319
- y: y,
320
- width: endCursorPos.x - p.left,
321
- height: line.height,
322
- fill: "#336df4",
323
- fillOpacity: .2
324
- }));
325
- } else {
326
- const p0 = line.paragraphs[0], p1 = line.paragraphs[line.paragraphs.length - 1];
327
- this.editBg.add(createRect({
328
- x: p0.left,
329
- y: y,
330
- width: p1.left + p1.width - p0.left,
331
- height: line.height,
332
- fill: "#336df4",
333
- fillOpacity: .2
334
- }));
425
+ width: endCursorPos.x - startCursorPos.x,
426
+ height: line0Info.height,
427
+ fill: "#336df4",
428
+ fillOpacity: .2
429
+ }); else {
430
+ this.editBg.setAttributes({
431
+ x: 0,
432
+ y: line0Info.top,
433
+ width: 0,
434
+ height: 0
435
+ });
436
+ const startIdx = cache.lines.findIndex((item => item === line0Info)), endIdx = cache.lines.findIndex((item => item === line1Info));
437
+ let y = 0;
438
+ for (let i = startIdx; i <= endIdx; i++) {
439
+ const line = cache.lines[i];
440
+ if (i === startIdx) {
441
+ const p = line.paragraphs[line.paragraphs.length - 1];
442
+ this.editBg.add(createRect({
443
+ x: startCursorPos.x,
444
+ y: y,
445
+ width: p.left + p.width - startCursorPos.x,
446
+ height: line.height,
447
+ fill: "#336df4",
448
+ fillOpacity: .2
449
+ }));
450
+ } else if (i === endIdx) {
451
+ const p = line.paragraphs[0];
452
+ this.editBg.add(createRect({
453
+ x: p.left,
454
+ y: y,
455
+ width: endCursorPos.x - p.left,
456
+ height: line.height,
457
+ fill: "#336df4",
458
+ fillOpacity: .2
459
+ }));
460
+ } else {
461
+ const p0 = line.paragraphs[0], p1 = line.paragraphs[line.paragraphs.length - 1];
462
+ this.editBg.add(createRect({
463
+ x: p0.left,
464
+ y: y,
465
+ width: p1.left + p1.width - p0.left,
466
+ height: line.height,
467
+ fill: "#336df4",
468
+ fillOpacity: .2
469
+ }));
470
+ }
471
+ y += line.height;
335
472
  }
336
- y += line.height;
337
473
  }
474
+ this.setCursorAndTextArea(currCursorData.x, currCursorData.y1, currCursorData.y2, this.currRt),
475
+ this.triggerRender(), this.updateCbs.forEach((cb => cb("selection", this)));
338
476
  }
339
- this.setCursorAndTextArea(currCursorData.x, currCursorData.y1 + 2, currCursorData.y2 - 2, this.currRt),
340
- this.triggerRender(), this.updateCbs.forEach((cb => cb("selection", this)));
341
477
  }
342
478
  hideSelection() {
343
479
  this.editBg && (this.editBg.removeAllChild(), this.editBg.setAttributes({
344
480
  fill: "transparent"
345
481
  }));
346
482
  }
483
+ getShadow(rt) {
484
+ const sr = rt.shadowRoot || rt.attachShadow();
485
+ return sr.setAttributes({
486
+ boundsMode: "empty"
487
+ }), sr;
488
+ }
347
489
  getLineByPoint(cache, p1) {
348
490
  let lineInfo = cache.lines[0];
349
491
  for (let i = 0; i < cache.lines.length && !(lineInfo.top <= p1.y && lineInfo.top + lineInfo.height >= p1.y); i++) lineInfo = cache.lines[i + 1];
@@ -375,7 +517,10 @@ export class RichTextEditPlugin {
375
517
  return -1;
376
518
  }
377
519
  isRichtext(e) {
378
- return !(!e.target || "richtext" !== e.target.type || !e.target.attribute.editable);
520
+ return !(!e.target || "richtext" !== e.target.type);
521
+ }
522
+ isEditableRichtext(e) {
523
+ return this.isRichtext(e) && !!e.target.attribute.editable;
379
524
  }
380
525
  triggerRender() {
381
526
  this.pluginService.stage.renderNextFrame();
@@ -391,8 +536,15 @@ export class RichTextEditPlugin {
391
536
  x: 0,
392
537
  y: 0
393
538
  };
394
- return e.target.globalTransMatrix.transformPoint(p, p1), p1.x -= this.deltaX, p1.y -= this.deltaY,
395
- p1;
539
+ e.target.globalTransMatrix.transformPoint(p, p1), p1.x -= this.deltaX, p1.y -= this.deltaY;
540
+ const rt = this.currRt, {textBaseline: textBaseline} = rt.attribute;
541
+ let dy = 0;
542
+ if ("middle" === textBaseline) {
543
+ dy = getRichTextBounds(rt.attribute).height() / 2;
544
+ } else if ("bottom" === textBaseline) {
545
+ dy = getRichTextBounds(rt.attribute).height();
546
+ }
547
+ return p1.y += dy, p1;
396
548
  }
397
549
  setCursorAndTextArea(x, y1, y2, rt) {
398
550
  this.editLine.setAttributes({
@@ -413,15 +565,14 @@ export class RichTextEditPlugin {
413
565
  y: y1
414
566
  }, out);
415
567
  const {left: left, top: top} = this.pluginService.stage.window.getBoundingClientRect();
416
- out.x += left, out.y += top, this.editModule.moveTo(out.x, out.y, rt, this.curCursorIdx, this.selectionStartCursorIdx);
568
+ out.x += left, out.y += top, this.offsetLineBgAndShadowBounds(), this.editModule.moveTo(out.x, out.y, rt, this.curCursorIdx, this.selectionStartCursorIdx);
417
569
  }
418
570
  computedCursorPosByEvent(e, cache) {
419
571
  const p1 = this.getEventPosition(e), lineInfo = this.getLineByPoint(cache, p1);
420
572
  if (!lineInfo) return;
421
573
  const {columnInfo: columnInfo, delta: delta} = this.getColumnAndIndexByLinePoint(lineInfo, p1);
422
574
  if (!columnInfo) return;
423
- let y1 = lineInfo.top, y2 = lineInfo.top + lineInfo.height;
424
- y1 += 2, y2 -= 2;
575
+ const y1 = lineInfo.top, y2 = lineInfo.top + lineInfo.height;
425
576
  let cursorIndex = this.getColumnIndex(cache, columnInfo);
426
577
  cursorIndex += delta;
427
578
  return {
@@ -436,17 +587,31 @@ export class RichTextEditPlugin {
436
587
  computedCursorPosByCursorIdx(cursorIdx, rt) {
437
588
  var _a, _b, _c;
438
589
  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;
439
- if (!column) return {
440
- x: 0,
441
- y1: 0,
442
- y2: height
443
- };
444
- const {lineInfo: lineInfo, columnInfo: columnInfo} = column;
445
- let y1 = lineInfo.top, y2 = lineInfo.top + lineInfo.height;
446
- return y1 += 2, y2 -= 2, {
590
+ if (!column) {
591
+ if (!cache.lines.length) {
592
+ return {
593
+ x: 0,
594
+ y1: 0,
595
+ y2: getRichTextBounds(Object.assign(Object.assign({}, rt.attribute), {
596
+ textConfig: [ {
597
+ text: "a"
598
+ } ]
599
+ })).height()
600
+ };
601
+ }
602
+ return {
603
+ x: 0,
604
+ y1: 0,
605
+ y2: height
606
+ };
607
+ }
608
+ const {lineInfo: lineInfo, columnInfo: columnInfo} = column, y1 = lineInfo.top, y2 = lineInfo.top + lineInfo.height;
609
+ return {
447
610
  x: columnInfo.left + (leftRight < 0 ? 0 : columnInfo.width),
448
611
  y1: y1,
449
- y2: y2
612
+ y2: y2,
613
+ lineInfo: lineInfo,
614
+ columnInfo: columnInfo
450
615
  };
451
616
  }
452
617
  getColumnByIndex(cache, index) {
@@ -469,5 +634,29 @@ export class RichTextEditPlugin {
469
634
  getSelection(defaultAll = !1) {
470
635
  return this.currRt ? null != this.selectionStartCursorIdx && null != this.curCursorIdx ? new Selection(this.selectionStartCursorIdx, this.curCursorIdx, this.currRt) : defaultAll ? RichTextEditPlugin.CreateSelection(this.currRt) : null : null;
471
636
  }
637
+ forceFocus(params) {
638
+ const {target: target, e: e, cursorIndex: cursorIndex} = params;
639
+ target && (this.currRt = target, e ? this._forceFocusByEvent(e) : this._forceFocusByCursorIndex(null != cursorIndex ? cursorIndex : -.1));
640
+ }
641
+ _forceFocusByEvent(e) {
642
+ this.handleEnter(), this.handlePointerDown(e), this.handlePointerUp(e);
643
+ }
644
+ _forceFocusByCursorIndex(cursorIndex) {
645
+ const richtext = this.currRt;
646
+ if (!richtext) return;
647
+ let x = 0, y1 = 0, y2 = 2, lineInfo = null, columnInfo = null;
648
+ const data = this.computedCursorPosByCursorIdx(cursorIndex, richtext);
649
+ x = data.x, y1 = data.y1, y2 = data.y2, lineInfo = data.lineInfo, columnInfo = data.columnInfo,
650
+ this.onFocus({
651
+ target: this.currRt
652
+ }, {
653
+ x: x,
654
+ y1: y1,
655
+ y2: y2,
656
+ cursorIndex: cursorIndex,
657
+ lineInfo: lineInfo,
658
+ columnInfo: columnInfo
659
+ });
660
+ }
472
661
  }
473
662
  //# sourceMappingURL=richtext-edit-plugin.js.map