@hufe921/canvas-editor 0.9.11 → 0.9.13

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.13";
27
27
  const ZERO = "\u200B";
28
28
  const WRAP = "\n";
29
29
  var RowFlex;
@@ -187,6 +187,25 @@ const TEXTLIKE_ELEMENT_TYPE = [
187
187
  ElementType.CONTROL,
188
188
  ElementType.DATE
189
189
  ];
190
+ var ControlType;
191
+ (function(ControlType2) {
192
+ ControlType2["TEXT"] = "text";
193
+ ControlType2["SELECT"] = "select";
194
+ ControlType2["CHECKBOX"] = "checkbox";
195
+ })(ControlType || (ControlType = {}));
196
+ var ControlComponent;
197
+ (function(ControlComponent2) {
198
+ ControlComponent2["PREFIX"] = "prefix";
199
+ ControlComponent2["POSTFIX"] = "postfix";
200
+ ControlComponent2["PLACEHOLDER"] = "placeholder";
201
+ ControlComponent2["VALUE"] = "value";
202
+ ControlComponent2["CHECKBOX"] = "checkbox";
203
+ })(ControlComponent || (ControlComponent = {}));
204
+ var ImageDisplay;
205
+ (function(ImageDisplay2) {
206
+ ImageDisplay2["INLINE"] = "inline";
207
+ ImageDisplay2["BLOCK"] = "block";
208
+ })(ImageDisplay || (ImageDisplay = {}));
190
209
  class ImageParticle {
191
210
  constructor(draw) {
192
211
  __publicField(this, "options");
@@ -3178,25 +3197,6 @@ const defaultControlOption = {
3178
3197
  prefix: "{",
3179
3198
  postfix: "}"
3180
3199
  };
3181
- var ControlType;
3182
- (function(ControlType2) {
3183
- ControlType2["TEXT"] = "text";
3184
- ControlType2["SELECT"] = "select";
3185
- ControlType2["CHECKBOX"] = "checkbox";
3186
- })(ControlType || (ControlType = {}));
3187
- var ControlComponent;
3188
- (function(ControlComponent2) {
3189
- ControlComponent2["PREFIX"] = "prefix";
3190
- ControlComponent2["POSTFIX"] = "postfix";
3191
- ControlComponent2["PLACEHOLDER"] = "placeholder";
3192
- ControlComponent2["VALUE"] = "value";
3193
- ControlComponent2["CHECKBOX"] = "checkbox";
3194
- })(ControlComponent || (ControlComponent = {}));
3195
- var ImageDisplay;
3196
- (function(ImageDisplay2) {
3197
- ImageDisplay2["INLINE"] = "inline";
3198
- ImageDisplay2["BLOCK"] = "block";
3199
- })(ImageDisplay || (ImageDisplay = {}));
3200
3200
  function formatElementList(elementList, options) {
3201
3201
  var _a;
3202
3202
  const { isHandleFirstElement, editorOptions } = __spreadValues({
@@ -3557,16 +3557,30 @@ 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");
3568
3582
  function buildDomFromElementList(payload) {
3569
- var _a, _b, _c, _d;
3583
+ var _a, _b, _c, _d, _e;
3570
3584
  for (let e = 0; e < payload.length; e++) {
3571
3585
  const element = payload[e];
3572
3586
  if (element.type === ElementType.TABLE) {
@@ -3579,6 +3593,8 @@ function writeElementList(elementList, options) {
3579
3593
  const tdDom = document.createElement("td");
3580
3594
  tdDom.style.border = "1px solid";
3581
3595
  const td = tr.tdList[d];
3596
+ tdDom.colSpan = td.colspan;
3597
+ tdDom.rowSpan = td.rowspan;
3582
3598
  tdDom.innerText = ((_a = td.value[0]) == null ? void 0 : _a.value) || "";
3583
3599
  trDom.append(tdDom);
3584
3600
  }
@@ -3603,12 +3619,19 @@ function writeElementList(elementList, options) {
3603
3619
  } else if (element.type === ElementType.SEPARATOR) {
3604
3620
  const hr = document.createElement("hr");
3605
3621
  clipboardDom.append(hr);
3622
+ } else if (element.type === ElementType.CHECKBOX) {
3623
+ const checkbox = document.createElement("input");
3624
+ checkbox.type = "checkbox";
3625
+ if ((_b = element.checkbox) == null ? void 0 : _b.value) {
3626
+ checkbox.setAttribute("checked", "true");
3627
+ }
3628
+ clipboardDom.append(checkbox);
3606
3629
  } else if (!element.type || element.type === ElementType.LATEX || TEXTLIKE_ELEMENT_TYPE.includes(element.type)) {
3607
3630
  let text2 = "";
3608
3631
  if (element.type === ElementType.CONTROL) {
3609
- text2 = ((_c = (_b = element.control.value) == null ? void 0 : _b[0]) == null ? void 0 : _c.value) || "";
3632
+ text2 = ((_d = (_c = element.control.value) == null ? void 0 : _c[0]) == null ? void 0 : _d.value) || "";
3610
3633
  } else if (element.type === ElementType.DATE) {
3611
- text2 = ((_d = element.valueList) == null ? void 0 : _d.map((v) => v.value).join("")) || "";
3634
+ text2 = ((_e = element.valueList) == null ? void 0 : _e.map((v) => v.value).join("")) || "";
3612
3635
  } else {
3613
3636
  text2 = element.value;
3614
3637
  }
@@ -3646,13 +3669,14 @@ function writeElementList(elementList, options) {
3646
3669
  return;
3647
3670
  writeClipboardItem(text, html);
3648
3671
  }
3649
- function getElementListByHTML(htmlText) {
3672
+ function getElementListByHTML(htmlText, options) {
3650
3673
  const elementList = [];
3651
3674
  function findTextNode(dom) {
3675
+ var _a;
3652
3676
  if (dom.nodeType === 3) {
3653
3677
  const style = window.getComputedStyle(dom.parentNode);
3654
3678
  const value = dom.textContent;
3655
- if (value) {
3679
+ if (value && ((_a = dom.parentNode) == null ? void 0 : _a.nodeName) !== "STYLE") {
3656
3680
  elementList.push({
3657
3681
  value,
3658
3682
  color: style.color,
@@ -3682,6 +3706,72 @@ function getElementListByHTML(htmlText) {
3682
3706
  url: aElement.href
3683
3707
  });
3684
3708
  }
3709
+ } else if (node.nodeName === "HR") {
3710
+ elementList.push({
3711
+ value: "\n",
3712
+ type: ElementType.SEPARATOR
3713
+ });
3714
+ } else if (node.nodeName === "IMG") {
3715
+ const { src, width, height } = node;
3716
+ if (src && width && height) {
3717
+ elementList.push({
3718
+ width,
3719
+ height,
3720
+ value: src,
3721
+ type: ElementType.IMAGE
3722
+ });
3723
+ }
3724
+ } else if (node.nodeName === "TABLE") {
3725
+ const tableElement = node;
3726
+ const element = {
3727
+ type: ElementType.TABLE,
3728
+ value: "\n",
3729
+ colgroup: [],
3730
+ trList: []
3731
+ };
3732
+ let tdCount = 0;
3733
+ tableElement.querySelectorAll("tr").forEach((trElement, trIndex) => {
3734
+ const trHeightStr = window.getComputedStyle(trElement).height.replace("px", "");
3735
+ const tr = {
3736
+ height: Number(trHeightStr),
3737
+ tdList: []
3738
+ };
3739
+ trElement.querySelectorAll("td").forEach((tdElement) => {
3740
+ const colspan = tdElement.colSpan;
3741
+ const rowspan = tdElement.rowSpan;
3742
+ if (trIndex === 0) {
3743
+ tdCount += colspan;
3744
+ }
3745
+ const td = {
3746
+ colspan,
3747
+ rowspan,
3748
+ value: [{
3749
+ value: tdElement.innerText
3750
+ }]
3751
+ };
3752
+ tr.tdList.push(td);
3753
+ });
3754
+ element.trList.push(tr);
3755
+ });
3756
+ if (tdCount) {
3757
+ const width = Math.ceil(options.innerWidth / tdCount);
3758
+ for (let i = 0; i < tdCount; i++) {
3759
+ element.colgroup.push({
3760
+ width
3761
+ });
3762
+ }
3763
+ }
3764
+ if (element.colgroup) {
3765
+ elementList.push(element);
3766
+ }
3767
+ } else if (node.nodeName === "INPUT" && node.type === ControlComponent.CHECKBOX) {
3768
+ elementList.push({
3769
+ type: ElementType.CHECKBOX,
3770
+ value: "",
3771
+ checkbox: {
3772
+ value: node.checked
3773
+ }
3774
+ });
3685
3775
  } else {
3686
3776
  findTextNode(node);
3687
3777
  if (node.nodeType === 1 && n !== childNodes.length - 1) {
@@ -3764,7 +3854,9 @@ class CursorAgent {
3764
3854
  }
3765
3855
  if (item.type === "text/html" && isHTML) {
3766
3856
  item.getAsString((htmlText) => {
3767
- const elementList = getElementListByHTML(htmlText);
3857
+ const elementList = getElementListByHTML(htmlText, {
3858
+ innerWidth: this.draw.getOriginalInnerWidth()
3859
+ });
3768
3860
  this.draw.insertElementList(elementList);
3769
3861
  });
3770
3862
  }
@@ -4346,32 +4438,84 @@ function keydown(evt, host) {
4346
4438
  if (isReadonly)
4347
4439
  return;
4348
4440
  if (index2 > 0) {
4349
- const curIndex = index2 - 1;
4350
- rangeManager.setRange(curIndex, curIndex);
4441
+ const curIndex = startIndex - 1;
4442
+ let anchorStartIndex = curIndex;
4443
+ let anchorEndIndex = curIndex;
4444
+ const cursorPosition2 = position.getCursorPosition();
4445
+ if (evt.shiftKey && cursorPosition2) {
4446
+ if (startIndex !== endIndex) {
4447
+ if (startIndex === cursorPosition2.index) {
4448
+ anchorStartIndex = startIndex;
4449
+ anchorEndIndex = endIndex - 1;
4450
+ } else {
4451
+ anchorStartIndex = curIndex;
4452
+ anchorEndIndex = endIndex;
4453
+ }
4454
+ } else {
4455
+ anchorEndIndex = endIndex;
4456
+ }
4457
+ }
4458
+ if (!~anchorStartIndex || !~anchorEndIndex)
4459
+ return;
4460
+ rangeManager.setRange(anchorStartIndex, anchorEndIndex);
4461
+ const isCollapsed2 = anchorStartIndex === anchorEndIndex;
4351
4462
  draw.render({
4352
- curIndex,
4463
+ curIndex: isCollapsed2 ? anchorStartIndex : void 0,
4464
+ isSetCursor: isCollapsed2,
4353
4465
  isSubmitHistory: false,
4354
4466
  isComputeRowList: false
4355
4467
  });
4468
+ evt.preventDefault();
4356
4469
  }
4357
4470
  } else if (evt.key === KeyMap.Right) {
4358
4471
  if (isReadonly)
4359
4472
  return;
4360
- if (index2 < positionList.length - 1) {
4361
- const curIndex = index2 + 1;
4362
- rangeManager.setRange(curIndex, curIndex);
4473
+ if (index2 < positionList.length) {
4474
+ const curIndex = endIndex + 1;
4475
+ let anchorStartIndex = curIndex;
4476
+ let anchorEndIndex = curIndex;
4477
+ const cursorPosition2 = position.getCursorPosition();
4478
+ if (evt.shiftKey && cursorPosition2) {
4479
+ if (startIndex !== endIndex) {
4480
+ if (startIndex === cursorPosition2.index) {
4481
+ anchorStartIndex = startIndex;
4482
+ anchorEndIndex = curIndex;
4483
+ } else {
4484
+ anchorStartIndex = startIndex + 1;
4485
+ anchorEndIndex = endIndex;
4486
+ }
4487
+ } else {
4488
+ anchorStartIndex = startIndex;
4489
+ }
4490
+ }
4491
+ const maxElementListIndex = elementList.length - 1;
4492
+ if (anchorStartIndex > maxElementListIndex || anchorEndIndex > maxElementListIndex)
4493
+ return;
4494
+ rangeManager.setRange(anchorStartIndex, anchorEndIndex);
4495
+ const isCollapsed2 = anchorStartIndex === anchorEndIndex;
4363
4496
  draw.render({
4364
- curIndex,
4497
+ curIndex: isCollapsed2 ? anchorStartIndex : void 0,
4498
+ isSetCursor: isCollapsed2,
4365
4499
  isSubmitHistory: false,
4366
4500
  isComputeRowList: false
4367
4501
  });
4502
+ evt.preventDefault();
4368
4503
  }
4369
4504
  } else if (evt.key === KeyMap.Up || evt.key === KeyMap.Down) {
4370
4505
  if (isReadonly)
4371
4506
  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);
4507
+ let anchorPosition = cursorPosition;
4508
+ const isUp = evt.key === KeyMap.Up;
4509
+ if (evt.shiftKey) {
4510
+ if (startIndex === cursorPosition.index) {
4511
+ anchorPosition = positionList[endIndex];
4512
+ } else {
4513
+ anchorPosition = positionList[startIndex];
4514
+ }
4515
+ }
4516
+ const { rowNo, index: index22, pageNo, coordinate: { leftTop, rightTop } } = anchorPosition;
4517
+ if (isUp && rowNo !== 0 || !isUp && rowNo !== draw.getRowCount()) {
4518
+ 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
4519
  let maxIndex = 0;
4376
4520
  let maxDistance = 0;
4377
4521
  for (let p = 0; p < probablePosition.length; p++) {
@@ -4381,12 +4525,14 @@ function keydown(evt, host) {
4381
4525
  if (curDistance > maxDistance) {
4382
4526
  maxIndex = position2.index;
4383
4527
  maxDistance = curDistance;
4528
+ break;
4384
4529
  }
4385
4530
  } else if (position2.coordinate.leftTop[0] <= leftTop[0] && position2.coordinate.rightTop[0] >= leftTop[0]) {
4386
4531
  const curDistance = position2.coordinate.rightTop[0] - leftTop[0];
4387
4532
  if (curDistance > maxDistance) {
4388
4533
  maxIndex = position2.index;
4389
4534
  maxDistance = curDistance;
4535
+ break;
4390
4536
  }
4391
4537
  }
4392
4538
  if (p === probablePosition.length - 1 && maxIndex === 0) {
@@ -4394,9 +4540,31 @@ function keydown(evt, host) {
4394
4540
  }
4395
4541
  }
4396
4542
  const curIndex = maxIndex;
4397
- rangeManager.setRange(curIndex, curIndex);
4543
+ let anchorStartIndex = curIndex;
4544
+ let anchorEndIndex = curIndex;
4545
+ if (evt.shiftKey) {
4546
+ if (startIndex !== endIndex) {
4547
+ if (startIndex === cursorPosition.index) {
4548
+ anchorStartIndex = startIndex;
4549
+ } else {
4550
+ anchorEndIndex = endIndex;
4551
+ }
4552
+ } else {
4553
+ if (isUp) {
4554
+ anchorEndIndex = endIndex;
4555
+ } else {
4556
+ anchorStartIndex = startIndex;
4557
+ }
4558
+ }
4559
+ }
4560
+ if (anchorStartIndex > anchorEndIndex) {
4561
+ [anchorStartIndex, anchorEndIndex] = [anchorEndIndex, anchorStartIndex];
4562
+ }
4563
+ rangeManager.setRange(anchorStartIndex, anchorEndIndex);
4564
+ const isCollapsed2 = anchorStartIndex === anchorEndIndex;
4398
4565
  draw.render({
4399
- curIndex,
4566
+ curIndex: isCollapsed2 ? anchorStartIndex : void 0,
4567
+ isSetCursor: isCollapsed2,
4400
4568
  isSubmitHistory: false,
4401
4569
  isComputeRowList: false
4402
4570
  });
@@ -4526,13 +4694,15 @@ function cut(host) {
4526
4694
  if (startIndex === endIndex) {
4527
4695
  const position = draw.getPosition();
4528
4696
  const positionList = position.getPositionList();
4529
- const curRowNo = positionList[startIndex].rowNo;
4697
+ const startPosition = positionList[startIndex];
4698
+ const curRowNo = startPosition.rowNo;
4699
+ const curPageNo = startPosition.pageNo;
4530
4700
  const cutElementIndexList = [];
4531
4701
  for (let p = 0; p < positionList.length; p++) {
4532
4702
  const position2 = positionList[p];
4533
- if (position2.rowNo > curRowNo)
4703
+ if (position2.pageNo > curPageNo)
4534
4704
  break;
4535
- if (position2.rowNo === curRowNo) {
4705
+ if (position2.pageNo === curPageNo && position2.rowNo === curRowNo) {
4536
4706
  cutElementIndexList.push(p);
4537
4707
  }
4538
4708
  }
@@ -5496,6 +5666,7 @@ class Margin {
5496
5666
  const margins = this.draw.getMargins();
5497
5667
  const marginIndicatorSize = this.draw.getMarginIndicatorSize();
5498
5668
  ctx.save();
5669
+ ctx.translate(0.5, 0.5);
5499
5670
  ctx.strokeStyle = marginIndicatorColor;
5500
5671
  ctx.beginPath();
5501
5672
  const leftTopPoint = [margins[3], margins[0]];
@@ -11282,11 +11453,11 @@ class ContextMenu {
11282
11453
  this._addEvent();
11283
11454
  }
11284
11455
  _addEvent() {
11285
- document.addEventListener("contextmenu", this._proxyContextMenuEvent);
11456
+ this.container.addEventListener("contextmenu", this._proxyContextMenuEvent);
11286
11457
  document.addEventListener("mousedown", this._handleEffect);
11287
11458
  }
11288
11459
  removeEvent() {
11289
- document.removeEventListener("contextmenu", this._proxyContextMenuEvent);
11460
+ this.container.removeEventListener("contextmenu", this._proxyContextMenuEvent);
11290
11461
  document.removeEventListener("mousedown", this._handleEffect);
11291
11462
  }
11292
11463
  _getContext() {