@hufe921/canvas-editor 0.9.11 → 0.9.12

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/README.md CHANGED
@@ -4,8 +4,9 @@
4
4
 
5
5
  ## tips
6
6
 
7
- 1. The render layer by svg is under development, see [feature/svg](https://github.com/Hufe921/canvas-editor/tree/feature/svg)
8
- 2. The export pdf feature is available now, see [feature/pdf](https://github.com/Hufe921/canvas-editor/tree/feature/pdf)
7
+ 1. [docs](https://hufe.club/canvas-editor-docs/)
8
+ 2. The render layer by svg is under development, see [feature/svg](https://github.com/Hufe921/canvas-editor/tree/feature/svg)
9
+ 3. The export pdf feature is available now, see [feature/pdf](https://github.com/Hufe921/canvas-editor/tree/feature/pdf)
9
10
 
10
11
  ## usage
11
12
 
@@ -23,7 +23,7 @@ var __publicField = (obj, key, value) => {
23
23
  return value;
24
24
  };
25
25
  var index = "";
26
- const version = "0.9.11";
26
+ const version = "0.9.12";
27
27
  const ZERO = "\u200B";
28
28
  const WRAP = "\n";
29
29
  var RowFlex;
@@ -3557,11 +3557,25 @@ function writeClipboardItem(text, html) {
3557
3557
  return;
3558
3558
  const plainText = new Blob([text], { type: "text/plain" });
3559
3559
  const htmlText = new Blob([html], { type: "text/html" });
3560
- const item = new ClipboardItem({
3561
- [plainText.type]: plainText,
3562
- [htmlText.type]: htmlText
3563
- });
3564
- window.navigator.clipboard.write([item]);
3560
+ if (window.ClipboardItem) {
3561
+ const item = new ClipboardItem({
3562
+ [plainText.type]: plainText,
3563
+ [htmlText.type]: htmlText
3564
+ });
3565
+ window.navigator.clipboard.write([item]);
3566
+ } else {
3567
+ const fakeElement = document.createElement("div");
3568
+ fakeElement.setAttribute("contenteditable", "true");
3569
+ fakeElement.innerHTML = html;
3570
+ document.body.append(fakeElement);
3571
+ const selection = window.getSelection();
3572
+ const range = document.createRange();
3573
+ range.selectNodeContents(fakeElement);
3574
+ selection == null ? void 0 : selection.removeAllRanges();
3575
+ selection == null ? void 0 : selection.addRange(range);
3576
+ document.execCommand("copy");
3577
+ fakeElement.remove();
3578
+ }
3565
3579
  }
3566
3580
  function writeElementList(elementList, options) {
3567
3581
  const clipboardDom = document.createElement("div");
@@ -4346,32 +4360,84 @@ function keydown(evt, host) {
4346
4360
  if (isReadonly)
4347
4361
  return;
4348
4362
  if (index2 > 0) {
4349
- const curIndex = index2 - 1;
4350
- rangeManager.setRange(curIndex, curIndex);
4363
+ const curIndex = startIndex - 1;
4364
+ let anchorStartIndex = curIndex;
4365
+ let anchorEndIndex = curIndex;
4366
+ const cursorPosition2 = position.getCursorPosition();
4367
+ if (evt.shiftKey && cursorPosition2) {
4368
+ if (startIndex !== endIndex) {
4369
+ if (startIndex === cursorPosition2.index) {
4370
+ anchorStartIndex = startIndex;
4371
+ anchorEndIndex = endIndex - 1;
4372
+ } else {
4373
+ anchorStartIndex = curIndex;
4374
+ anchorEndIndex = endIndex;
4375
+ }
4376
+ } else {
4377
+ anchorEndIndex = endIndex;
4378
+ }
4379
+ }
4380
+ if (!~anchorStartIndex || !~anchorEndIndex)
4381
+ return;
4382
+ rangeManager.setRange(anchorStartIndex, anchorEndIndex);
4383
+ const isCollapsed2 = anchorStartIndex === anchorEndIndex;
4351
4384
  draw.render({
4352
- curIndex,
4385
+ curIndex: isCollapsed2 ? anchorStartIndex : void 0,
4386
+ isSetCursor: isCollapsed2,
4353
4387
  isSubmitHistory: false,
4354
4388
  isComputeRowList: false
4355
4389
  });
4390
+ evt.preventDefault();
4356
4391
  }
4357
4392
  } else if (evt.key === KeyMap.Right) {
4358
4393
  if (isReadonly)
4359
4394
  return;
4360
4395
  if (index2 < positionList.length - 1) {
4361
- const curIndex = index2 + 1;
4362
- rangeManager.setRange(curIndex, curIndex);
4396
+ const curIndex = endIndex + 1;
4397
+ let anchorStartIndex = curIndex;
4398
+ let anchorEndIndex = curIndex;
4399
+ const cursorPosition2 = position.getCursorPosition();
4400
+ if (evt.shiftKey && cursorPosition2) {
4401
+ if (startIndex !== endIndex) {
4402
+ if (startIndex === cursorPosition2.index) {
4403
+ anchorStartIndex = startIndex;
4404
+ anchorEndIndex = curIndex;
4405
+ } else {
4406
+ anchorStartIndex = startIndex + 1;
4407
+ anchorEndIndex = endIndex;
4408
+ }
4409
+ } else {
4410
+ anchorStartIndex = startIndex;
4411
+ }
4412
+ }
4413
+ const maxElementListIndex = elementList.length - 1;
4414
+ if (anchorStartIndex > maxElementListIndex || anchorEndIndex > maxElementListIndex)
4415
+ return;
4416
+ rangeManager.setRange(anchorStartIndex, anchorEndIndex);
4417
+ const isCollapsed2 = anchorStartIndex === anchorEndIndex;
4363
4418
  draw.render({
4364
- curIndex,
4419
+ curIndex: isCollapsed2 ? anchorStartIndex : void 0,
4420
+ isSetCursor: isCollapsed2,
4365
4421
  isSubmitHistory: false,
4366
4422
  isComputeRowList: false
4367
4423
  });
4424
+ evt.preventDefault();
4368
4425
  }
4369
4426
  } else if (evt.key === KeyMap.Up || evt.key === KeyMap.Down) {
4370
4427
  if (isReadonly)
4371
4428
  return;
4372
- const { rowNo, index: index22, coordinate: { leftTop, rightTop } } = cursorPosition;
4373
- if (evt.key === KeyMap.Up && rowNo !== 0 || evt.key === KeyMap.Down && rowNo !== draw.getRowCount()) {
4374
- const probablePosition = evt.key === KeyMap.Up ? positionList.slice(0, index22).filter((p) => p.rowNo === rowNo - 1) : positionList.slice(index22, positionList.length - 1).filter((p) => p.rowNo === rowNo + 1);
4429
+ let anchorPosition = cursorPosition;
4430
+ const isUp = evt.key === KeyMap.Up;
4431
+ if (evt.shiftKey) {
4432
+ if (startIndex === cursorPosition.index) {
4433
+ anchorPosition = positionList[endIndex];
4434
+ } else {
4435
+ anchorPosition = positionList[startIndex];
4436
+ }
4437
+ }
4438
+ const { rowNo, index: index22, pageNo, coordinate: { leftTop, rightTop } } = anchorPosition;
4439
+ if (isUp && rowNo !== 0 || !isUp && rowNo !== draw.getRowCount()) {
4440
+ const probablePosition = isUp ? positionList.slice(0, index22).filter((p) => p.rowNo === rowNo - 1 && pageNo === p.pageNo) : positionList.slice(index22, positionList.length - 1).filter((p) => p.rowNo === rowNo + 1 && pageNo === p.pageNo);
4375
4441
  let maxIndex = 0;
4376
4442
  let maxDistance = 0;
4377
4443
  for (let p = 0; p < probablePosition.length; p++) {
@@ -4381,12 +4447,14 @@ function keydown(evt, host) {
4381
4447
  if (curDistance > maxDistance) {
4382
4448
  maxIndex = position2.index;
4383
4449
  maxDistance = curDistance;
4450
+ break;
4384
4451
  }
4385
4452
  } else if (position2.coordinate.leftTop[0] <= leftTop[0] && position2.coordinate.rightTop[0] >= leftTop[0]) {
4386
4453
  const curDistance = position2.coordinate.rightTop[0] - leftTop[0];
4387
4454
  if (curDistance > maxDistance) {
4388
4455
  maxIndex = position2.index;
4389
4456
  maxDistance = curDistance;
4457
+ break;
4390
4458
  }
4391
4459
  }
4392
4460
  if (p === probablePosition.length - 1 && maxIndex === 0) {
@@ -4394,9 +4462,31 @@ function keydown(evt, host) {
4394
4462
  }
4395
4463
  }
4396
4464
  const curIndex = maxIndex;
4397
- rangeManager.setRange(curIndex, curIndex);
4465
+ let anchorStartIndex = curIndex;
4466
+ let anchorEndIndex = curIndex;
4467
+ if (evt.shiftKey) {
4468
+ if (startIndex !== endIndex) {
4469
+ if (startIndex === cursorPosition.index) {
4470
+ anchorStartIndex = startIndex;
4471
+ } else {
4472
+ anchorEndIndex = endIndex;
4473
+ }
4474
+ } else {
4475
+ if (isUp) {
4476
+ anchorEndIndex = endIndex;
4477
+ } else {
4478
+ anchorStartIndex = startIndex;
4479
+ }
4480
+ }
4481
+ }
4482
+ if (anchorStartIndex > anchorEndIndex) {
4483
+ [anchorStartIndex, anchorEndIndex] = [anchorEndIndex, anchorStartIndex];
4484
+ }
4485
+ rangeManager.setRange(anchorStartIndex, anchorEndIndex);
4486
+ const isCollapsed2 = anchorStartIndex === anchorEndIndex;
4398
4487
  draw.render({
4399
- curIndex,
4488
+ curIndex: isCollapsed2 ? anchorStartIndex : void 0,
4489
+ isSetCursor: isCollapsed2,
4400
4490
  isSubmitHistory: false,
4401
4491
  isComputeRowList: false
4402
4492
  });
@@ -4526,13 +4616,15 @@ function cut(host) {
4526
4616
  if (startIndex === endIndex) {
4527
4617
  const position = draw.getPosition();
4528
4618
  const positionList = position.getPositionList();
4529
- const curRowNo = positionList[startIndex].rowNo;
4619
+ const startPosition = positionList[startIndex];
4620
+ const curRowNo = startPosition.rowNo;
4621
+ const curPageNo = startPosition.pageNo;
4530
4622
  const cutElementIndexList = [];
4531
4623
  for (let p = 0; p < positionList.length; p++) {
4532
4624
  const position2 = positionList[p];
4533
- if (position2.rowNo > curRowNo)
4625
+ if (position2.pageNo > curPageNo)
4534
4626
  break;
4535
- if (position2.rowNo === curRowNo) {
4627
+ if (position2.pageNo === curPageNo && position2.rowNo === curRowNo) {
4536
4628
  cutElementIndexList.push(p);
4537
4629
  }
4538
4630
  }
@@ -5496,6 +5588,7 @@ class Margin {
5496
5588
  const margins = this.draw.getMargins();
5497
5589
  const marginIndicatorSize = this.draw.getMarginIndicatorSize();
5498
5590
  ctx.save();
5591
+ ctx.translate(0.5, 0.5);
5499
5592
  ctx.strokeStyle = marginIndicatorColor;
5500
5593
  ctx.beginPath();
5501
5594
  const leftTopPoint = [margins[3], margins[0]];