e-virt-table 1.4.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.es.js CHANGED
@@ -955,7 +955,11 @@ class ie extends ee {
955
955
  maxLineClamp: this.maxLineClamp,
956
956
  cacheTextKey: d,
957
957
  layoutCallback: (f) => {
958
- this.ctx.emit("registerTextLayout", `${this.rowIndex}-${this.colIndex}`, f);
958
+ this.ctx.emit(
959
+ "registerTextLayout",
960
+ `${this.cellType}-${this.rowIndex}-${this.colIndex}`,
961
+ f
962
+ );
959
963
  }
960
964
  }
961
965
  ), this.ellipsis;
@@ -2521,6 +2525,9 @@ class Ae {
2521
2525
  if (!e) throw new Error("canvas context not found");
2522
2526
  this.ctx = e;
2523
2527
  }
2528
+ getCtx() {
2529
+ return this.ctx;
2530
+ }
2524
2531
  clearTextCache() {
2525
2532
  this.textCacheMap.clear();
2526
2533
  }
@@ -4288,6 +4295,9 @@ class $e extends ee {
4288
4295
  cacheTextKey: o,
4289
4296
  textCallback: (n) => {
4290
4297
  this.textInfo = n;
4298
+ },
4299
+ layoutCallback: (n) => {
4300
+ this.ctx.emit("registerTextLayout", `${this.cellType}-${this.colIndex}-${this.level}`, n);
4291
4301
  }
4292
4302
  }
4293
4303
  );
@@ -5310,7 +5320,7 @@ class Ze {
5310
5320
  this.ctx.disableHoverIconClick = !1;
5311
5321
  }, 0);
5312
5322
  }), this.ctx.on("cellHeaderHoverChange", (t) => {
5313
- this.ctx.mousedown && this.selectCols(t);
5323
+ this.ctx.textSelecting || this.ctx.mousedown && this.selectCols(t);
5314
5324
  }), this.ctx.on("cellHoverChange", (t) => {
5315
5325
  this.ctx.textSelecting || this.ctx.mousedown && this.selectCols(t);
5316
5326
  }), this.ctx.on("cellHeaderMousedown", (t, e) => {
@@ -7080,8 +7090,6 @@ class ki {
7080
7090
  return;
7081
7091
  }
7082
7092
  if (t.key === "Enter" && !this.ctx.editing) {
7083
- if (this.isTextSelectionActive())
7084
- return;
7085
7093
  t.preventDefault(), this.startEdit();
7086
7094
  return;
7087
7095
  }
@@ -7120,9 +7128,9 @@ class ki {
7120
7128
  t.preventDefault();
7121
7129
  return;
7122
7130
  }
7123
- this.isTextSelectionActive() || this.startEdit(!0);
7131
+ this.startEdit(!0);
7124
7132
  }), this.ctx.on("cellClick", (t) => {
7125
- if (this.ctx.stageElement.style.cursor === "pointer" || this.isTextSelectionActive() || !this.isInSelectorRange(t.rowIndex, t.colIndex))
7133
+ if (this.ctx.stageElement.style.cursor === "pointer" || !this.isInSelectorRange(t.rowIndex, t.colIndex))
7126
7134
  return;
7127
7135
  const { xArr: e, yArr: i } = this.ctx.selector, s = JSON.stringify(e) + JSON.stringify(i);
7128
7136
  if (this.focusInput(), this.selectorArrStr === s && this.cellTarget) {
@@ -8071,48 +8079,56 @@ class Wi {
8071
8079
  r(this, "dragging", !1);
8072
8080
  this.ctx = t, this.init();
8073
8081
  }
8082
+ /** 是否允许进行文字选择(开启配置且不在编辑/查找状态) */
8083
+ get canSelect() {
8084
+ return this.ctx.config.ENABLE_TEXT_SELECTION && !this.ctx.editing && !this.ctx.finding;
8085
+ }
8086
+ /**
8087
+ * 在所有已注册的文字 layout(body/header/footer)中命中鼠标位置,返回命中的单元格 key 与字符索引。
8088
+ * 由于固定列/表头会后绘制并覆盖普通单元格,这里取最后一个命中的 layout 作为最顶层结果。
8089
+ */
8090
+ hitTestLayouts(t) {
8091
+ const { offsetX: e, offsetY: i } = this.ctx.getOffset(t);
8092
+ let s = null;
8093
+ for (const [o, n] of this.layouts) {
8094
+ const a = this.hitTestText(n, e, i);
8095
+ a !== null && (s = { cellKey: o, index: a });
8096
+ }
8097
+ return s;
8098
+ }
8099
+ /** 同步选中文本并触发重绘 */
8100
+ syncSelection() {
8101
+ this.ctx.textSelectionStr = this.getSelectedText(), this.ctx.emit("drawView");
8102
+ }
8074
8103
  init() {
8075
8104
  this.ctx.on("registerTextLayout", (t, e) => {
8076
8105
  this.registerLayout(t, e);
8077
8106
  }), this.ctx.on("mousedown", (t) => {
8078
- if (!this.ctx.config.ENABLE_TEXT_SELECTION || !this.ctx.isTarget(t) || this.ctx.editing || this.ctx.finding)
8079
- return;
8080
- const e = this.ctx.focusCell;
8081
- if (!e || e.cellType !== "body") {
8082
- this.clearSelection();
8107
+ if (!this.canSelect || !this.ctx.isTarget(t))
8083
8108
  return;
8084
- }
8085
- const i = this.getCellLayout(e);
8086
- if (!i) {
8109
+ this.ctx.containerElement.contains(document.activeElement) || this.ctx.containerElement.focus({ preventScroll: !0 });
8110
+ const e = this.hitTestLayouts(t);
8111
+ if (!e) {
8087
8112
  this.clearSelection();
8088
8113
  return;
8089
8114
  }
8090
- const { offsetX: s, offsetY: o } = this.ctx.getOffset(t), n = this.hitTestText(i, s, o);
8091
- if (n === null) {
8092
- this.clearSelection();
8093
- return;
8094
- }
8095
- this.activeCellKey = this.getCellKey(e), this.selectionStart = n, this.selectionEnd = n, this.dragging = !0, this.ctx.textSelecting = !0, this.ctx.textSelectionStr = this.getSelectedText(), this.ctx.emit("drawView");
8115
+ this.activeCellKey = e.cellKey, this.selectionStart = e.index, this.selectionEnd = e.index, this.dragging = !0, this.ctx.textSelecting = !0, this.syncSelection();
8096
8116
  }), this.ctx.on("mousemove", (t) => {
8097
- if (!this.ctx.config.ENABLE_TEXT_SELECTION || !this.dragging)
8117
+ if (!this.canSelect || !this.dragging)
8098
8118
  return;
8099
- const e = this.ctx.focusCell;
8119
+ const e = this.layouts.get(this.activeCellKey);
8100
8120
  if (!e)
8101
8121
  return;
8102
- const i = this.getCellLayout(e);
8103
- if (!i)
8104
- return;
8105
- const { offsetX: s, offsetY: o } = this.ctx.getOffset(t), n = this.hitTestText(i, s, o);
8106
- n !== null && (this.selectionEnd = n, this.ctx.textSelectionStr = this.getSelectedText(), this.ctx.emit("drawView"));
8122
+ const { offsetX: i, offsetY: s } = this.ctx.getOffset(t), o = this.hitTestText(e, i, s);
8123
+ o !== null && (this.selectionEnd = o, this.syncSelection());
8107
8124
  }), this.ctx.on("mouseup", () => {
8108
8125
  this.dragging = !1, this.ctx.textSelecting = !1, this.ctx.textSelectionStr = this.getSelectedText(), this.ctx.textSelectionStr && this.ctx.emit("drawView");
8109
8126
  }), this.ctx.on("keydown", (t) => {
8110
- if (this.ctx.config.ENABLE_TEXT_SELECTION && !(this.ctx.editing || this.ctx.finding) && (t.ctrlKey && t.code === "KeyC" || t.metaKey && t.code === "KeyC")) {
8111
- const e = this.getSelectedText();
8112
- if (!e)
8113
- return;
8114
- t.preventDefault(), navigator.clipboard && navigator.clipboard.writeText(e).catch((i) => console.error("Copy Failure:", i));
8115
- }
8127
+ var i;
8128
+ if (console.log("keydown"), !this.canSelect || !((t.ctrlKey || t.metaKey) && t.code === "KeyC"))
8129
+ return;
8130
+ const e = this.getSelectedText();
8131
+ e && (t.preventDefault(), (i = navigator.clipboard) == null || i.writeText(e).catch((s) => console.error("Copy Failure:", s)));
8116
8132
  }), this.ctx.on("outsideMousedown", () => {
8117
8133
  this.clearSelection();
8118
8134
  });
@@ -8149,12 +8165,6 @@ class Wi {
8149
8165
  const e = this.layouts.get(this.activeCellKey);
8150
8166
  e && this.drawTextSelection(e, t);
8151
8167
  }
8152
- getCanvasCtx() {
8153
- const t = this.ctx.canvasElement.getContext("2d");
8154
- if (!t)
8155
- throw new Error("canvas context not found");
8156
- return t;
8157
- }
8158
8168
  /**
8159
8169
  * 绘制文字选中高亮
8160
8170
  */
@@ -8162,29 +8172,22 @@ class Wi {
8162
8172
  if (e.start >= e.end)
8163
8173
  return;
8164
8174
  const o = /* @__PURE__ */ new Map();
8165
- t.glyphs.forEach((a) => {
8166
- a.index < 0 || a.index < e.start || a.index >= e.end || (o.has(a.line) || o.set(a.line, []), o.get(a.line).push(a));
8167
- });
8168
- const n = this.getCanvasCtx();
8169
- n.save(), n.fillStyle = s, o.forEach((a) => {
8175
+ for (const a of t.glyphs) {
8176
+ if (a.index < e.start || a.index >= e.end)
8177
+ continue;
8178
+ const l = o.get(a.line);
8179
+ l ? l.push(a) : o.set(a.line, [a]);
8180
+ }
8181
+ if (!o.size)
8182
+ return;
8183
+ const n = this.ctx.paint.getCtx();
8184
+ n.save(), n.font = t.font, n.textBaseline = "top", n.textAlign = "left", o.forEach((a) => {
8170
8185
  const l = a[0], c = a[a.length - 1];
8171
- n.fillRect(l.x, l.y, c.x + c.width - l.x, l.height);
8172
- }), n.font = t.font, n.textBaseline = "top", n.textAlign = "left", n.fillStyle = i, t.lines.forEach((a) => {
8173
- const l = a.items.filter(
8174
- (g) => g.sourceIndex >= 0 && g.sourceIndex >= e.start && g.sourceIndex < e.end
8175
- );
8176
- if (!l.length)
8177
- return;
8178
- const c = l[0], d = a.items.indexOf(c), f = l.map((g) => g.char).join(""), u = a.items.slice(0, d).map((g) => g.char).join(""), x = a.drawX + n.measureText(u).width;
8179
- n.fillText(f, x, a.drawY);
8186
+ n.fillStyle = s, n.fillRect(l.x, l.y, c.x + c.width - l.x, l.height), n.fillStyle = i;
8187
+ for (const d of a)
8188
+ n.fillText(d.char, d.x, d.y);
8180
8189
  }), n.restore();
8181
8190
  }
8182
- getCellKey(t) {
8183
- return `${t.rowIndex}-${t.colIndex}`;
8184
- }
8185
- getCellLayout(t) {
8186
- return this.layouts.get(this.getCellKey(t));
8187
- }
8188
8191
  getSelectionRange() {
8189
8192
  return this.selectionStart === null || this.selectionEnd === null ? null : {
8190
8193
  start: Math.min(this.selectionStart, this.selectionEnd),
@@ -8261,7 +8264,7 @@ const gt = class gt {
8261
8264
  };
8262
8265
  }
8263
8266
  doDraw(t = !1) {
8264
- this.header.update(), this.footer.update(), this.body.update(), this.ctx.paint.clear(), this.textSelector.clearLayouts(), this.body.draw(), this.textSelector.draw(), this.footer.draw(), this.header.draw(), this.body.drawDragRowTip(), this.scroller.draw(), this.empty.draw(), this.loading.draw(), t || this.overlayer.draw();
8267
+ this.header.update(), this.footer.update(), this.body.update(), this.ctx.paint.clear(), this.textSelector.clearLayouts(), this.body.draw(), this.footer.draw(), this.header.draw(), this.textSelector.draw(), this.body.drawDragRowTip(), this.scroller.draw(), this.empty.draw(), this.loading.draw(), t || this.overlayer.draw();
8265
8268
  }
8266
8269
  draw(t = !1) {
8267
8270
  this.animationFrameId && cancelAnimationFrame(this.animationFrameId), this.animationFrameId = requestAnimationFrame(() => {