@hufe921/canvas-editor 0.9.17 → 0.9.18

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.
@@ -23,7 +23,7 @@ var __publicField = (obj, key, value) => {
23
23
  return value;
24
24
  };
25
25
  var index = "";
26
- const version = "0.9.17";
26
+ const version = "0.9.18";
27
27
  const ZERO = "\u200B";
28
28
  const WRAP = "\n";
29
29
  var RowFlex;
@@ -130,6 +130,11 @@ function mergeObject(source, target) {
130
130
  }
131
131
  return target;
132
132
  }
133
+ function nextTick(fn) {
134
+ setTimeout(() => {
135
+ fn();
136
+ }, 0);
137
+ }
133
138
  const CURSOR_AGENT_HEIGHT = 12;
134
139
  const defaultCursorOption = {
135
140
  width: 1,
@@ -235,11 +240,16 @@ var ImageDisplay;
235
240
  })(ImageDisplay || (ImageDisplay = {}));
236
241
  class ImageParticle {
237
242
  constructor(draw) {
243
+ __publicField(this, "draw");
238
244
  __publicField(this, "options");
239
245
  __publicField(this, "imageCache");
246
+ this.draw = draw;
240
247
  this.options = draw.getOptions();
241
248
  this.imageCache = new Map();
242
249
  }
250
+ addImageObserver(promise) {
251
+ this.draw.getImageObserver().add(promise);
252
+ }
243
253
  render(ctx, element, x, y) {
244
254
  const { scale } = this.options;
245
255
  const width = element.width * scale;
@@ -248,12 +258,19 @@ class ImageParticle {
248
258
  const img = this.imageCache.get(element.id);
249
259
  ctx.drawImage(img, x, y, width, height);
250
260
  } else {
251
- const img = new Image();
252
- img.src = element.value;
253
- img.onload = () => {
254
- ctx.drawImage(img, x, y, width, height);
255
- this.imageCache.set(element.id, img);
256
- };
261
+ const imageLoadPromise = new Promise((resolve, reject) => {
262
+ const img = new Image();
263
+ img.src = element.value;
264
+ img.onload = () => {
265
+ ctx.drawImage(img, x, y, width, height);
266
+ this.imageCache.set(element.id, img);
267
+ resolve(element);
268
+ };
269
+ img.onerror = (error) => {
270
+ reject(error);
271
+ };
272
+ });
273
+ this.addImageObserver(imageLoadPromise);
257
274
  }
258
275
  }
259
276
  }
@@ -3201,12 +3218,19 @@ class LaTexParticle extends ImageParticle {
3201
3218
  const img = this.imageCache.get(element.value);
3202
3219
  ctx.drawImage(img, x, y, width, height);
3203
3220
  } else {
3204
- const img = new Image();
3205
- img.src = element.laTexSVG;
3206
- img.onload = () => {
3207
- ctx.drawImage(img, x, y, width, height);
3208
- this.imageCache.set(element.value, img);
3209
- };
3221
+ const laTexLoadPromise = new Promise((resolve, reject) => {
3222
+ const img = new Image();
3223
+ img.src = element.laTexSVG;
3224
+ img.onload = () => {
3225
+ ctx.drawImage(img, x, y, width, height);
3226
+ this.imageCache.set(element.value, img);
3227
+ resolve(element);
3228
+ };
3229
+ img.onerror = (error) => {
3230
+ reject(error);
3231
+ };
3232
+ });
3233
+ this.addImageObserver(laTexLoadPromise);
3210
3234
  }
3211
3235
  }
3212
3236
  }
@@ -4260,7 +4284,7 @@ function mousedown(evt, host) {
4260
4284
  curIndex,
4261
4285
  isSubmitHistory: isSetCheckbox,
4262
4286
  isSetCursor: !isDirectHitImage && !isDirectHitCheckbox,
4263
- isComputeRowList: false
4287
+ isCompute: false
4264
4288
  });
4265
4289
  }
4266
4290
  const previewer = draw.getPreviewer();
@@ -4410,7 +4434,7 @@ function mousemove(evt, host) {
4410
4434
  draw.render({
4411
4435
  isSubmitHistory: false,
4412
4436
  isSetCursor: false,
4413
- isComputeRowList: false
4437
+ isCompute: false
4414
4438
  });
4415
4439
  }
4416
4440
  const isApple = typeof navigator !== "undefined" && /Mac OS X/.test(navigator.userAgent);
@@ -4530,7 +4554,7 @@ function keydown(evt, host) {
4530
4554
  curIndex: isCollapsed2 ? anchorStartIndex : void 0,
4531
4555
  isSetCursor: isCollapsed2,
4532
4556
  isSubmitHistory: false,
4533
- isComputeRowList: false
4557
+ isCompute: false
4534
4558
  });
4535
4559
  evt.preventDefault();
4536
4560
  }
@@ -4564,7 +4588,7 @@ function keydown(evt, host) {
4564
4588
  curIndex: isCollapsed2 ? anchorStartIndex : void 0,
4565
4589
  isSetCursor: isCollapsed2,
4566
4590
  isSubmitHistory: false,
4567
- isComputeRowList: false
4591
+ isCompute: false
4568
4592
  });
4569
4593
  evt.preventDefault();
4570
4594
  }
@@ -4633,7 +4657,7 @@ function keydown(evt, host) {
4633
4657
  curIndex: isCollapsed2 ? anchorStartIndex : void 0,
4634
4658
  isSetCursor: isCollapsed2,
4635
4659
  isSubmitHistory: false,
4636
- isComputeRowList: false
4660
+ isCompute: false
4637
4661
  });
4638
4662
  }
4639
4663
  } else if (isMod(evt) && evt.key === KeyMap.Z) {
@@ -4869,7 +4893,7 @@ function dblclick(host) {
4869
4893
  draw.render({
4870
4894
  isSubmitHistory: false,
4871
4895
  isSetCursor: false,
4872
- isComputeRowList: false
4896
+ isCompute: false
4873
4897
  });
4874
4898
  }
4875
4899
  function threeClick(host) {
@@ -4907,7 +4931,7 @@ function threeClick(host) {
4907
4931
  draw.render({
4908
4932
  isSubmitHistory: false,
4909
4933
  isSetCursor: false,
4910
- isComputeRowList: false
4934
+ isCompute: false
4911
4935
  });
4912
4936
  }
4913
4937
  var click = {
@@ -5064,7 +5088,7 @@ class CanvasEvent {
5064
5088
  this.draw.render({
5065
5089
  isSubmitHistory: false,
5066
5090
  isSetCursor: false,
5067
- isComputeRowList: false
5091
+ isCompute: false
5068
5092
  });
5069
5093
  }
5070
5094
  mousemove(evt) {
@@ -5285,6 +5309,94 @@ class Position {
5285
5309
  setPositionList(payload) {
5286
5310
  this.positionList = payload;
5287
5311
  }
5312
+ computePageRowPosition(payload) {
5313
+ const { positionList, rowList, pageNo, startX, startY, startIndex, innerWidth } = payload;
5314
+ const { scale, tdPadding } = this.options;
5315
+ let x = startX;
5316
+ let y = startY;
5317
+ let index2 = startIndex;
5318
+ for (let i = 0; i < rowList.length; i++) {
5319
+ const curRow = rowList[i];
5320
+ if (curRow.rowFlex === RowFlex.CENTER) {
5321
+ x += (innerWidth - curRow.width) / 2;
5322
+ } else if (curRow.rowFlex === RowFlex.RIGHT) {
5323
+ x += innerWidth - curRow.width;
5324
+ }
5325
+ const tablePreX = x;
5326
+ const tablePreY = y;
5327
+ for (let j = 0; j < curRow.elementList.length; j++) {
5328
+ const element = curRow.elementList[j];
5329
+ const metrics = element.metrics;
5330
+ const offsetY = element.imgDisplay !== ImageDisplay.INLINE && element.type === ElementType.IMAGE || element.type === ElementType.LATEX ? curRow.ascent - metrics.height : curRow.ascent;
5331
+ const positionItem = {
5332
+ pageNo,
5333
+ index: index2,
5334
+ value: element.value,
5335
+ rowNo: i,
5336
+ metrics,
5337
+ ascent: offsetY,
5338
+ lineHeight: curRow.height,
5339
+ isLastLetter: j === curRow.elementList.length - 1,
5340
+ coordinate: {
5341
+ leftTop: [x, y],
5342
+ leftBottom: [x, y + curRow.height],
5343
+ rightTop: [x + metrics.width, y],
5344
+ rightBottom: [x + metrics.width, y + curRow.height]
5345
+ }
5346
+ };
5347
+ positionList.push(positionItem);
5348
+ index2++;
5349
+ x += metrics.width;
5350
+ if (element.type === ElementType.TABLE) {
5351
+ const tdGap = tdPadding * 2;
5352
+ for (let t = 0; t < element.trList.length; t++) {
5353
+ const tr = element.trList[t];
5354
+ for (let d = 0; d < tr.tdList.length; d++) {
5355
+ const td = tr.tdList[d];
5356
+ td.positionList = [];
5357
+ const drawRowResult = this.computePageRowPosition({
5358
+ positionList: td.positionList,
5359
+ rowList: td.rowList,
5360
+ pageNo,
5361
+ startIndex: 0,
5362
+ startX: (td.x + tdPadding) * scale + tablePreX,
5363
+ startY: td.y * scale + tablePreY,
5364
+ innerWidth: (td.width - tdGap) * scale
5365
+ });
5366
+ x = drawRowResult.x;
5367
+ y = drawRowResult.y;
5368
+ }
5369
+ }
5370
+ x = tablePreX;
5371
+ y = tablePreY;
5372
+ }
5373
+ }
5374
+ x = startX;
5375
+ y += curRow.height;
5376
+ }
5377
+ return { x, y, index: index2 };
5378
+ }
5379
+ computePositionList() {
5380
+ this.positionList = [];
5381
+ const innerWidth = this.draw.getInnerWidth();
5382
+ const pageRowList = this.draw.getPageRowList();
5383
+ const margins = this.draw.getMargins();
5384
+ const startX = margins[3];
5385
+ const startY = margins[0];
5386
+ for (let i = 0; i < pageRowList.length; i++) {
5387
+ const rowList = pageRowList[i];
5388
+ const startIndex = rowList[0].startIndex;
5389
+ this.computePageRowPosition({
5390
+ positionList: this.positionList,
5391
+ rowList,
5392
+ pageNo: i,
5393
+ startIndex,
5394
+ startX,
5395
+ startY,
5396
+ innerWidth
5397
+ });
5398
+ }
5399
+ }
5288
5400
  setCursorPosition(position) {
5289
5401
  this.cursorPosition = position;
5290
5402
  }
@@ -6036,7 +6148,10 @@ class Search {
6036
6148
  const searchMatchIndexList = this.getSearchNavigateIndexList();
6037
6149
  if (searchMatchIndexList.includes(s)) {
6038
6150
  ctx.fillStyle = searchNavigateMatchColor;
6039
- this.searchNavigateScrollIntoView(position);
6151
+ const preSearchMatch = this.searchMatchList[s - 1];
6152
+ if (!preSearchMatch || preSearchMatch.groupId !== searchMatch.groupId) {
6153
+ this.searchNavigateScrollIntoView(position);
6154
+ }
6040
6155
  } else {
6041
6156
  ctx.fillStyle = searchMatchColor;
6042
6157
  }
@@ -6234,13 +6349,11 @@ var MoveDirection;
6234
6349
  })(MoveDirection || (MoveDirection = {}));
6235
6350
  class SelectionObserver {
6236
6351
  constructor() {
6237
- __publicField(this, "threshold");
6352
+ __publicField(this, "step", 5);
6353
+ __publicField(this, "thresholdPoints", [70, 40, 10, 20]);
6238
6354
  __publicField(this, "requestAnimationFrameId");
6239
- __publicField(this, "tippingPoints");
6240
6355
  __publicField(this, "isMousedown");
6241
6356
  __publicField(this, "isMoving");
6242
- __publicField(this, "clientWidth");
6243
- __publicField(this, "clientHeight");
6244
6357
  __publicField(this, "_mousedown", () => {
6245
6358
  this.isMousedown = true;
6246
6359
  });
@@ -6252,25 +6365,23 @@ class SelectionObserver {
6252
6365
  if (!this.isMousedown)
6253
6366
  return;
6254
6367
  const { x, y } = evt;
6255
- if (y < this.tippingPoints[0]) {
6368
+ const clientWidth = document.documentElement.clientWidth;
6369
+ const clientHeight = document.documentElement.clientHeight;
6370
+ if (y < this.thresholdPoints[0]) {
6256
6371
  this._startMove(MoveDirection.UP);
6257
- } else if (this.clientHeight - y <= this.tippingPoints[1]) {
6372
+ } else if (clientHeight - y <= this.thresholdPoints[1]) {
6258
6373
  this._startMove(MoveDirection.DOWN);
6259
- } else if (x < this.tippingPoints[2]) {
6374
+ } else if (x < this.thresholdPoints[2]) {
6260
6375
  this._startMove(MoveDirection.LEFT);
6261
- } else if (this.clientWidth - x < this.tippingPoints[3]) {
6376
+ } else if (clientWidth - x < this.thresholdPoints[3]) {
6262
6377
  this._startMove(MoveDirection.RIGHT);
6263
6378
  } else {
6264
6379
  this._stopMove();
6265
6380
  }
6266
6381
  });
6267
- this.threshold = 10;
6268
- this.tippingPoints = [70, 40, 10, 20];
6269
- this.requestAnimationFrameId = -1;
6382
+ this.requestAnimationFrameId = null;
6270
6383
  this.isMousedown = false;
6271
6384
  this.isMoving = false;
6272
- this.clientWidth = 0;
6273
- this.clientHeight = 0;
6274
6385
  this._addEvent();
6275
6386
  }
6276
6387
  _addEvent() {
@@ -6284,34 +6395,31 @@ class SelectionObserver {
6284
6395
  document.removeEventListener("mouseup", this._mouseup);
6285
6396
  }
6286
6397
  _move(direction) {
6287
- const x = window.screenX;
6288
- const y = window.screenY;
6398
+ const x = window.scrollX;
6399
+ const y = window.scrollY;
6289
6400
  if (direction === MoveDirection.DOWN) {
6290
- window.scrollBy(x, y + this.threshold);
6401
+ window.scrollTo(x, y + this.step);
6291
6402
  } else if (direction === MoveDirection.UP) {
6292
- window.scrollBy(x, y - this.threshold);
6403
+ window.scrollTo(x, y - this.step);
6293
6404
  } else if (direction === MoveDirection.LEFT) {
6294
- window.scrollBy(x - this.threshold, y);
6405
+ window.scrollTo(x - this.step, y);
6295
6406
  } else {
6296
- window.scrollBy(x + this.threshold, y);
6407
+ window.scrollTo(x + this.step, y);
6297
6408
  }
6298
6409
  this.requestAnimationFrameId = window.requestAnimationFrame(this._move.bind(this, direction));
6299
6410
  }
6300
6411
  _startMove(direction) {
6301
6412
  if (this.isMoving)
6302
6413
  return;
6303
- this.clientWidth = document.documentElement.clientWidth;
6304
- this.clientHeight = document.documentElement.clientHeight;
6305
6414
  this.isMoving = true;
6306
- this.requestAnimationFrameId = window.requestAnimationFrame(this._move.bind(this, direction));
6415
+ this._move(direction);
6307
6416
  }
6308
6417
  _stopMove() {
6309
- if (!this.isMoving)
6310
- return;
6311
- if (~this.requestAnimationFrameId) {
6418
+ if (this.requestAnimationFrameId) {
6312
6419
  window.cancelAnimationFrame(this.requestAnimationFrameId);
6420
+ this.requestAnimationFrameId = null;
6421
+ this.isMoving = false;
6313
6422
  }
6314
- this.isMoving = false;
6315
6423
  }
6316
6424
  }
6317
6425
  class TableParticle {
@@ -7386,8 +7494,9 @@ class Control {
7386
7494
  if (nextIndex === elementList.length) {
7387
7495
  rightIndex = nextIndex - 1;
7388
7496
  }
7389
- if (!~leftIndex || !~rightIndex)
7497
+ if (!~leftIndex && !~rightIndex)
7390
7498
  return startIndex;
7499
+ leftIndex = ~leftIndex ? leftIndex : 0;
7391
7500
  elementList.splice(leftIndex + 1, rightIndex - leftIndex);
7392
7501
  return leftIndex;
7393
7502
  }
@@ -8662,6 +8771,21 @@ class I18n {
8662
8771
  return value;
8663
8772
  }
8664
8773
  }
8774
+ class ImageObserver {
8775
+ constructor() {
8776
+ __publicField(this, "promiseList");
8777
+ this.promiseList = [];
8778
+ }
8779
+ add(payload) {
8780
+ this.promiseList.push(payload);
8781
+ }
8782
+ clearAll() {
8783
+ this.promiseList = [];
8784
+ }
8785
+ allSettled() {
8786
+ return Promise.allSettled(this.promiseList);
8787
+ }
8788
+ }
8665
8789
  class Draw {
8666
8790
  constructor(rootContainer, options, elementList, listener) {
8667
8791
  __publicField(this, "container");
@@ -8707,11 +8831,14 @@ class Draw {
8707
8831
  __publicField(this, "workerManager");
8708
8832
  __publicField(this, "scrollObserver");
8709
8833
  __publicField(this, "selectionObserver");
8834
+ __publicField(this, "imageObserver");
8710
8835
  __publicField(this, "rowList");
8836
+ __publicField(this, "pageRowList");
8711
8837
  __publicField(this, "painterStyle");
8712
8838
  __publicField(this, "painterOptions");
8713
8839
  __publicField(this, "visiblePageNoList");
8714
8840
  __publicField(this, "intersectionPageNo");
8841
+ __publicField(this, "lazyRenderIntersectionObserver");
8715
8842
  this.container = this._wrapContainer(rootContainer);
8716
8843
  this.pageList = [];
8717
8844
  this.ctxList = [];
@@ -8753,6 +8880,7 @@ class Draw {
8753
8880
  this.control = new Control(this);
8754
8881
  this.scrollObserver = new ScrollObserver(this);
8755
8882
  this.selectionObserver = new SelectionObserver();
8883
+ this.imageObserver = new ImageObserver();
8756
8884
  this.canvasEvent = new CanvasEvent(this);
8757
8885
  this.cursor = new Cursor(this, this.canvasEvent);
8758
8886
  this.canvasEvent.register();
@@ -8760,10 +8888,12 @@ class Draw {
8760
8888
  this.globalEvent.register();
8761
8889
  this.workerManager = new WorkerManager(this);
8762
8890
  this.rowList = [];
8891
+ this.pageRowList = [];
8763
8892
  this.painterStyle = null;
8764
8893
  this.painterOptions = null;
8765
8894
  this.visiblePageNoList = [];
8766
8895
  this.intersectionPageNo = 0;
8896
+ this.lazyRenderIntersectionObserver = null;
8767
8897
  this.render({ isSetCursor: false });
8768
8898
  }
8769
8899
  getMode() {
@@ -8858,6 +8988,12 @@ class Draw {
8858
8988
  getPageList() {
8859
8989
  return this.pageList;
8860
8990
  }
8991
+ getRowList() {
8992
+ return this.rowList;
8993
+ }
8994
+ getPageRowList() {
8995
+ return this.pageRowList;
8996
+ }
8861
8997
  getCtx() {
8862
8998
  return this.ctxList[this.pageNo];
8863
8999
  }
@@ -8888,8 +9024,11 @@ class Draw {
8888
9024
  if (!payload.length)
8889
9025
  return;
8890
9026
  const activeControl = this.control.getActiveControl();
8891
- if (activeControl)
8892
- return;
9027
+ if (activeControl) {
9028
+ const element = activeControl.getElement();
9029
+ if (element.controlComponent !== ControlComponent.POSTFIX)
9030
+ return;
9031
+ }
8893
9032
  const isPartRangeInControlOutside = this.control.isPartRangeInControlOutside();
8894
9033
  if (isPartRangeInControlOutside)
8895
9034
  return;
@@ -8955,13 +9094,23 @@ class Draw {
8955
9094
  getWorkerManager() {
8956
9095
  return this.workerManager;
8957
9096
  }
9097
+ getImageObserver() {
9098
+ return this.imageObserver;
9099
+ }
8958
9100
  getI18n() {
8959
9101
  return this.i18n;
8960
9102
  }
8961
9103
  getRowCount() {
8962
9104
  return this.rowList.length;
8963
9105
  }
8964
- getDataURL() {
9106
+ async getDataURL() {
9107
+ this.render({
9108
+ isLazy: false,
9109
+ isCompute: false,
9110
+ isSetCursor: false,
9111
+ isSubmitHistory: false
9112
+ });
9113
+ await this.imageObserver.allSettled();
8965
9114
  return this.pageList.map((c) => c.toDataURL());
8966
9115
  }
8967
9116
  getPainterStyle() {
@@ -9126,7 +9275,6 @@ class Draw {
9126
9275
  var _a, _b, _c, _d;
9127
9276
  const { defaultSize, defaultRowMargin, scale, tdPadding, defaultTabWidth } = this.options;
9128
9277
  const defaultBasicRowMarginHeight = this.getDefaultBasicRowMarginHeight();
9129
- const tdGap = tdPadding * 2;
9130
9278
  const canvas = document.createElement("canvas");
9131
9279
  const ctx = canvas.getContext("2d");
9132
9280
  const rowList = [];
@@ -9136,6 +9284,7 @@ class Draw {
9136
9284
  height: 0,
9137
9285
  ascent: 0,
9138
9286
  elementList: [],
9287
+ startIndex: 0,
9139
9288
  rowFlex: (_a = elementList == null ? void 0 : elementList[1]) == null ? void 0 : _a.rowFlex
9140
9289
  });
9141
9290
  }
@@ -9167,6 +9316,7 @@ class Draw {
9167
9316
  }
9168
9317
  metrics.boundingBoxAscent = 0;
9169
9318
  } else if (element.type === ElementType.TABLE) {
9319
+ const tdGap = tdPadding * 2;
9170
9320
  this.tableParticle.computeRowColInfo(element);
9171
9321
  const trList = element.trList;
9172
9322
  for (let t = 0; t < trList.length; t++) {
@@ -9320,6 +9470,7 @@ class Draw {
9320
9470
  rowList.push({
9321
9471
  width: metrics.width,
9322
9472
  height,
9473
+ startIndex: i,
9323
9474
  elementList: [rowElement],
9324
9475
  ascent,
9325
9476
  rowFlex: (_d = elementList[i + 1]) == null ? void 0 : _d.rowFlex,
@@ -9340,6 +9491,44 @@ class Draw {
9340
9491
  }
9341
9492
  return rowList;
9342
9493
  }
9494
+ _computePageList() {
9495
+ var _a;
9496
+ const pageRowList = [[]];
9497
+ const { pageMode } = this.options;
9498
+ const height = this.getHeight();
9499
+ const margins = this.getMargins();
9500
+ const marginHeight = margins[0] + margins[2];
9501
+ let pageHeight = marginHeight;
9502
+ let pageNo = 0;
9503
+ if (pageMode === PageMode.CONTINUITY) {
9504
+ pageRowList[0] = this.rowList;
9505
+ pageHeight += this.rowList.reduce((pre, cur) => pre + cur.height, 0);
9506
+ const dpr = window.devicePixelRatio;
9507
+ const pageDom = this.pageList[0];
9508
+ const pageDomHeight = Number(pageDom.style.height.replace("px", ""));
9509
+ if (pageHeight > pageDomHeight) {
9510
+ pageDom.style.height = `${pageHeight}px`;
9511
+ pageDom.height = pageHeight * dpr;
9512
+ } else {
9513
+ const reduceHeight = pageHeight < height ? height : pageHeight;
9514
+ pageDom.style.height = `${reduceHeight}px`;
9515
+ pageDom.height = reduceHeight * dpr;
9516
+ }
9517
+ } else {
9518
+ for (let i = 0; i < this.rowList.length; i++) {
9519
+ const row = this.rowList[i];
9520
+ if (row.height + pageHeight > height || ((_a = this.rowList[i - 1]) == null ? void 0 : _a.isPageBreak)) {
9521
+ pageHeight = marginHeight + row.height;
9522
+ pageRowList.push([row]);
9523
+ pageNo++;
9524
+ } else {
9525
+ pageHeight += row.height;
9526
+ pageRowList[pageNo].push(row);
9527
+ }
9528
+ }
9529
+ }
9530
+ return pageRowList;
9531
+ }
9343
9532
  _drawRichText(ctx) {
9344
9533
  this.underline.render(ctx);
9345
9534
  this.strikeout.render(ctx);
@@ -9347,22 +9536,12 @@ class Draw {
9347
9536
  this.textParticle.complete();
9348
9537
  }
9349
9538
  _drawRow(ctx, payload) {
9350
- const { positionList, rowList, pageNo, startX, startY, startIndex, innerWidth } = payload;
9539
+ const { rowList, pageNo, positionList, startIndex } = payload;
9351
9540
  const { scale, tdPadding } = this.options;
9352
9541
  const { isCrossRowCol, tableId } = this.range.getRange();
9353
- const tdGap = tdPadding * 2;
9354
- let x = startX;
9355
- let y = startY;
9356
9542
  let index2 = startIndex;
9357
9543
  for (let i = 0; i < rowList.length; i++) {
9358
9544
  const curRow = rowList[i];
9359
- if (curRow.rowFlex === RowFlex.CENTER) {
9360
- x += (innerWidth - curRow.width) / 2;
9361
- } else if (curRow.rowFlex === RowFlex.RIGHT) {
9362
- x += innerWidth - curRow.width;
9363
- }
9364
- const tablePreX = x;
9365
- const tablePreY = y;
9366
9545
  const rangeRecord = {
9367
9546
  x: 0,
9368
9547
  y: 0,
@@ -9373,24 +9552,7 @@ class Draw {
9373
9552
  for (let j = 0; j < curRow.elementList.length; j++) {
9374
9553
  const element = curRow.elementList[j];
9375
9554
  const metrics = element.metrics;
9376
- const offsetY = element.imgDisplay !== ImageDisplay.INLINE && element.type === ElementType.IMAGE || element.type === ElementType.LATEX ? curRow.ascent - metrics.height : curRow.ascent;
9377
- const positionItem = {
9378
- pageNo,
9379
- index: index2,
9380
- value: element.value,
9381
- rowNo: i,
9382
- metrics,
9383
- ascent: offsetY,
9384
- lineHeight: curRow.height,
9385
- isLastLetter: j === curRow.elementList.length - 1,
9386
- coordinate: {
9387
- leftTop: [x, y],
9388
- leftBottom: [x, y + curRow.height],
9389
- rightTop: [x + metrics.width, y],
9390
- rightBottom: [x + metrics.width, y + curRow.height]
9391
- }
9392
- };
9393
- positionList.push(positionItem);
9555
+ const { ascent: offsetY, coordinate: { leftTop: [x, y] } } = positionList[curRow.startIndex + j];
9394
9556
  if (element.type === ElementType.IMAGE) {
9395
9557
  this._drawRichText(ctx);
9396
9558
  this.imageParticle.render(ctx, element, x, y + offsetY);
@@ -9479,42 +9641,33 @@ class Draw {
9479
9641
  }
9480
9642
  }
9481
9643
  index2++;
9482
- x += metrics.width;
9483
9644
  if (element.type === ElementType.TABLE) {
9645
+ const tdGap = tdPadding * 2;
9484
9646
  for (let t = 0; t < element.trList.length; t++) {
9485
9647
  const tr = element.trList[t];
9486
9648
  for (let d = 0; d < tr.tdList.length; d++) {
9487
9649
  const td = tr.tdList[d];
9488
- td.positionList = [];
9489
- const drawRowResult = this._drawRow(ctx, {
9650
+ this._drawRow(ctx, {
9490
9651
  positionList: td.positionList,
9491
9652
  rowList: td.rowList,
9492
9653
  pageNo,
9493
9654
  startIndex: 0,
9494
- startX: (td.x + tdPadding) * scale + tablePreX,
9495
- startY: td.y * scale + tablePreY,
9496
9655
  innerWidth: (td.width - tdGap) * scale
9497
9656
  });
9498
- x = drawRowResult.x;
9499
- y = drawRowResult.y;
9500
9657
  }
9501
9658
  }
9502
- x = tablePreX;
9503
- y = tablePreY;
9504
9659
  }
9505
9660
  }
9506
9661
  this._drawRichText(ctx);
9507
9662
  if (rangeRecord.width && rangeRecord.height) {
9508
- const { x: x2, y: y2, width, height } = rangeRecord;
9509
- this.range.render(ctx, x2, y2, width, height);
9663
+ const { x, y, width, height } = rangeRecord;
9664
+ this.range.render(ctx, x, y, width, height);
9510
9665
  }
9511
9666
  if (isCrossRowCol && tableRangeElement && tableRangeElement.id === tableId) {
9667
+ const { coordinate: { leftTop: [x, y] } } = positionList[curRow.startIndex];
9512
9668
  this.tableParticle.drawRange(ctx, tableRangeElement, x, y);
9513
9669
  }
9514
- x = startX;
9515
- y += curRow.height;
9516
9670
  }
9517
- return { x, y, index: index2 };
9518
9671
  }
9519
9672
  _clearPage(pageNo) {
9520
9673
  const ctx = this.ctxList[pageNo];
@@ -9524,28 +9677,19 @@ class Draw {
9524
9677
  }
9525
9678
  _drawPage(positionList, rowList, pageNo) {
9526
9679
  const { pageMode } = this.options;
9527
- const margins = this.getMargins();
9528
9680
  const innerWidth = this.getInnerWidth();
9529
9681
  const ctx = this.ctxList[pageNo];
9530
9682
  this._clearPage(pageNo);
9531
9683
  this.background.render(ctx);
9532
- const leftTopPoint = [margins[3], margins[0]];
9533
9684
  this.margin.render(ctx);
9534
- let x = leftTopPoint[0];
9535
- let y = leftTopPoint[1];
9536
- let index2 = positionList.length;
9537
- const drawRowResult = this._drawRow(ctx, {
9685
+ const index2 = rowList[0].startIndex;
9686
+ this._drawRow(ctx, {
9538
9687
  positionList,
9539
9688
  rowList,
9540
9689
  pageNo,
9541
9690
  startIndex: index2,
9542
- startX: x,
9543
- startY: y,
9544
9691
  innerWidth
9545
9692
  });
9546
- x = drawRowResult.x;
9547
- y = drawRowResult.y;
9548
- index2 = drawRowResult.index;
9549
9693
  this.header.render(ctx);
9550
9694
  this.pageNumber.render(ctx, pageNo);
9551
9695
  if (this.search.getSearchKeyword()) {
@@ -9555,76 +9699,67 @@ class Draw {
9555
9699
  this.waterMark.render(ctx);
9556
9700
  }
9557
9701
  }
9702
+ _lazyRender() {
9703
+ var _a;
9704
+ const positionList = this.position.getOriginalPositionList();
9705
+ (_a = this.lazyRenderIntersectionObserver) == null ? void 0 : _a.disconnect();
9706
+ this.lazyRenderIntersectionObserver = new IntersectionObserver((entries) => {
9707
+ entries.forEach((entry) => {
9708
+ if (entry.isIntersecting) {
9709
+ const index2 = Number(entry.target.dataset.index);
9710
+ this._drawPage(positionList, this.pageRowList[index2], index2);
9711
+ }
9712
+ });
9713
+ });
9714
+ this.pageList.forEach((el) => {
9715
+ this.lazyRenderIntersectionObserver.observe(el);
9716
+ });
9717
+ }
9718
+ _immediateRender() {
9719
+ const positionList = this.position.getOriginalPositionList();
9720
+ for (let i = 0; i < this.pageRowList.length; i++) {
9721
+ this._drawPage(positionList, this.pageRowList[i], i);
9722
+ }
9723
+ }
9558
9724
  render(payload) {
9559
- var _a, _b;
9560
- const { pageMode } = this.options;
9561
- const { isSubmitHistory = true, isSetCursor = true, isComputeRowList = true } = payload || {};
9725
+ var _a;
9726
+ const { isSubmitHistory = true, isSetCursor = true, isCompute = true, isLazy = true } = payload || {};
9562
9727
  let { curIndex } = payload || {};
9563
- const height = this.getHeight();
9564
9728
  const innerWidth = this.getInnerWidth();
9565
- if (isComputeRowList) {
9729
+ if (isCompute) {
9566
9730
  this.rowList = this._computeRowList(innerWidth, this.elementList);
9731
+ this.pageRowList = this._computePageList();
9732
+ this.position.computePositionList();
9567
9733
  const searchKeyword = this.search.getSearchKeyword();
9568
9734
  if (searchKeyword) {
9569
9735
  this.search.compute(searchKeyword);
9570
9736
  }
9571
9737
  }
9738
+ this.imageObserver.clearAll();
9572
9739
  this.cursor.recoveryCursor();
9573
- this.position.setPositionList([]);
9574
9740
  const positionList = this.position.getOriginalPositionList();
9575
- const margins = this.getMargins();
9576
- const marginHeight = margins[0] + margins[2];
9577
- let pageHeight = marginHeight;
9578
- let pageNo = 0;
9579
- const pageRowList = [[]];
9580
- if (pageMode === PageMode.CONTINUITY) {
9581
- pageRowList[0] = this.rowList;
9582
- pageHeight += this.rowList.reduce((pre, cur) => pre + cur.height, 0);
9583
- const dpr = window.devicePixelRatio;
9584
- const pageDom = this.pageList[0];
9585
- const pageDomHeight = Number(pageDom.style.height.replace("px", ""));
9586
- if (pageHeight > pageDomHeight) {
9587
- pageDom.style.height = `${pageHeight}px`;
9588
- pageDom.height = pageHeight * dpr;
9589
- } else {
9590
- const reduceHeight = pageHeight < height ? height : pageHeight;
9591
- pageDom.style.height = `${reduceHeight}px`;
9592
- pageDom.height = reduceHeight * dpr;
9593
- }
9594
- } else {
9595
- for (let i = 0; i < this.rowList.length; i++) {
9596
- const row = this.rowList[i];
9597
- if (row.height + pageHeight > height || ((_a = this.rowList[i - 1]) == null ? void 0 : _a.isPageBreak)) {
9598
- pageHeight = marginHeight + row.height;
9599
- pageRowList.push([row]);
9600
- pageNo++;
9601
- } else {
9602
- pageHeight += row.height;
9603
- pageRowList[pageNo].push(row);
9604
- }
9605
- }
9606
- }
9607
- for (let i = 0; i < pageRowList.length; i++) {
9741
+ for (let i = 0; i < this.pageRowList.length; i++) {
9608
9742
  if (!this.pageList[i]) {
9609
9743
  this._createPage(i);
9610
9744
  }
9611
- const rowList = pageRowList[i];
9612
- this._drawPage(positionList, rowList, i);
9613
9745
  }
9614
- setTimeout(() => {
9615
- const curPageCount = pageRowList.length;
9616
- const prePageCount = this.pageList.length;
9617
- if (prePageCount > curPageCount) {
9618
- const deleteCount = prePageCount - curPageCount;
9619
- this.ctxList.splice(curPageCount, deleteCount);
9620
- this.pageList.splice(curPageCount, deleteCount).forEach((page) => page.remove());
9621
- }
9622
- });
9746
+ const curPageCount = this.pageRowList.length;
9747
+ const prePageCount = this.pageList.length;
9748
+ if (prePageCount > curPageCount) {
9749
+ const deleteCount = prePageCount - curPageCount;
9750
+ this.ctxList.splice(curPageCount, deleteCount);
9751
+ this.pageList.splice(curPageCount, deleteCount).forEach((page) => page.remove());
9752
+ }
9753
+ if (isLazy) {
9754
+ this._lazyRender();
9755
+ } else {
9756
+ this._immediateRender();
9757
+ }
9623
9758
  if (isSetCursor) {
9624
9759
  const positionContext = this.position.getPositionContext();
9625
9760
  if (positionContext.isTable) {
9626
9761
  const { index: index2, trIndex, tdIndex } = positionContext;
9627
- const tablePositionList = (_b = this.elementList[index2].trList) == null ? void 0 : _b[trIndex].tdList[tdIndex].positionList;
9762
+ const tablePositionList = (_a = this.elementList[index2].trList) == null ? void 0 : _a[trIndex].tdList[tdIndex].positionList;
9628
9763
  if (curIndex === void 0 && tablePositionList) {
9629
9764
  curIndex = tablePositionList.length - 1;
9630
9765
  }
@@ -9642,19 +9777,19 @@ class Draw {
9642
9777
  const self = this;
9643
9778
  const oldElementList = deepClone(this.elementList);
9644
9779
  const { startIndex, endIndex } = this.range.getRange();
9645
- const pageNo2 = this.pageNo;
9780
+ const pageNo = this.pageNo;
9646
9781
  const oldPositionContext = deepClone(this.position.getPositionContext());
9647
9782
  this.historyManager.execute(function() {
9648
- self.setPageNo(pageNo2);
9783
+ self.setPageNo(pageNo);
9649
9784
  self.position.setPositionContext(oldPositionContext);
9650
9785
  self.elementList = deepClone(oldElementList);
9651
9786
  self.range.setRange(startIndex, endIndex);
9652
9787
  self.render({ curIndex, isSubmitHistory: false });
9653
9788
  });
9654
9789
  }
9655
- setTimeout(() => {
9790
+ nextTick(() => {
9656
9791
  if (this.listener.pageSizeChange) {
9657
- this.listener.pageSizeChange(pageRowList.length);
9792
+ this.listener.pageSizeChange(this.pageRowList.length);
9658
9793
  }
9659
9794
  if (this.listener.contentChange && isSubmitHistory) {
9660
9795
  this.listener.contentChange();
@@ -10148,7 +10283,7 @@ class CommandAdapt {
10148
10283
  const isCollapsed = startIndex === endIndex;
10149
10284
  this.draw.render({
10150
10285
  curIndex: isCollapsed ? startIndex : void 0,
10151
- isComputeRowList: false,
10286
+ isCompute: false,
10152
10287
  isSubmitHistory: false,
10153
10288
  isSetCursor: isCollapsed
10154
10289
  });
@@ -10370,7 +10505,10 @@ class CommandAdapt {
10370
10505
  selection.forEach((el) => {
10371
10506
  el.color = payload;
10372
10507
  });
10373
- this.draw.render({ isSetCursor: false });
10508
+ this.draw.render({
10509
+ isSetCursor: false,
10510
+ isCompute: false
10511
+ });
10374
10512
  }
10375
10513
  highlight(payload) {
10376
10514
  const isReadonly = this.draw.isReadonly();
@@ -10382,7 +10520,10 @@ class CommandAdapt {
10382
10520
  selection.forEach((el) => {
10383
10521
  el.highlight = payload;
10384
10522
  });
10385
- this.draw.render({ isSetCursor: false });
10523
+ this.draw.render({
10524
+ isSetCursor: false,
10525
+ isCompute: false
10526
+ });
10386
10527
  }
10387
10528
  rowFlex(payload) {
10388
10529
  const isReadonly = this.draw.isReadonly();
@@ -11099,7 +11240,7 @@ class CommandAdapt {
11099
11240
  const { endIndex } = this.range.getRange();
11100
11241
  this.draw.render({
11101
11242
  curIndex: endIndex,
11102
- isComputeRowList: false
11243
+ isCompute: false
11103
11244
  });
11104
11245
  }
11105
11246
  editHyperlink(payload) {
@@ -11116,7 +11257,7 @@ class CommandAdapt {
11116
11257
  const { endIndex } = this.range.getRange();
11117
11258
  this.draw.render({
11118
11259
  curIndex: endIndex,
11119
- isComputeRowList: false
11260
+ isCompute: false
11120
11261
  });
11121
11262
  }
11122
11263
  separator(payload) {
@@ -11179,7 +11320,8 @@ class CommandAdapt {
11179
11320
  options.watermark.font = payload.font || font;
11180
11321
  this.draw.render({
11181
11322
  isSetCursor: false,
11182
- isSubmitHistory: false
11323
+ isSubmitHistory: false,
11324
+ isCompute: false
11183
11325
  });
11184
11326
  }
11185
11327
  deleteWatermark() {
@@ -11191,7 +11333,8 @@ class CommandAdapt {
11191
11333
  options.watermark = __spreadValues({}, defaultWatermarkOption);
11192
11334
  this.draw.render({
11193
11335
  isSetCursor: false,
11194
- isSubmitHistory: false
11336
+ isSubmitHistory: false,
11337
+ isCompute: false
11195
11338
  });
11196
11339
  }
11197
11340
  }
@@ -11236,7 +11379,9 @@ class CommandAdapt {
11236
11379
  return;
11237
11380
  this.draw.render({
11238
11381
  isSetCursor: false,
11239
- isSubmitHistory: false
11382
+ isSubmitHistory: false,
11383
+ isCompute: false,
11384
+ isLazy: false
11240
11385
  });
11241
11386
  }
11242
11387
  searchNavigateNext() {
@@ -11245,7 +11390,9 @@ class CommandAdapt {
11245
11390
  return;
11246
11391
  this.draw.render({
11247
11392
  isSetCursor: false,
11248
- isSubmitHistory: false
11393
+ isSubmitHistory: false,
11394
+ isCompute: false,
11395
+ isLazy: false
11249
11396
  });
11250
11397
  }
11251
11398
  getSearchNavigateInfo() {
@@ -11348,12 +11495,13 @@ class CommandAdapt {
11348
11495
  curIndex: firstIndex
11349
11496
  });
11350
11497
  }
11351
- print() {
11498
+ async print() {
11352
11499
  const { width, height, scale } = this.options;
11353
11500
  if (scale !== 1) {
11354
11501
  this.draw.setPageScale(1);
11355
11502
  }
11356
- printImageBase64(this.draw.getDataURL(), width, height);
11503
+ const base64List = await this.draw.getDataURL();
11504
+ printImageBase64(base64List, width, height);
11357
11505
  if (scale !== 1) {
11358
11506
  this.draw.setPageScale(scale);
11359
11507
  }