@hufe921/canvas-editor 0.9.15 → 0.9.17

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
@@ -26,17 +26,16 @@ new Editor(document.querySelector(".canvas-editor"), [
26
26
  ])
27
27
  ```
28
28
 
29
- ## snapshot
29
+ ## next features
30
30
 
31
- ![image](https://github.com/Hufe921/canvas-editor/blob/main/src/assets/snapshots/main_v0.9.8.png)
31
+ 1. improve performance
32
+ 2. page header and footer
33
+ 3. control rules
34
+ 4. table paging
32
35
 
33
- ## next features
36
+ ## snapshot
34
37
 
35
- 1. support mac os
36
- 2. drag text (optimization)
37
- 3. page header and footer
38
- 4. control rules
39
- 5. table paging
38
+ ![image](https://github.com/Hufe921/canvas-editor/blob/main/src/assets/snapshots/main_v0.9.8.png)
40
39
 
41
40
  ## install
42
41
 
@@ -23,7 +23,7 @@ var __publicField = (obj, key, value) => {
23
23
  return value;
24
24
  };
25
25
  var index = "";
26
- const version = "0.9.15";
26
+ const version = "0.9.17";
27
27
  const ZERO = "\u200B";
28
28
  const WRAP = "\n";
29
29
  var RowFlex;
@@ -131,6 +131,12 @@ function mergeObject(source, target) {
131
131
  return target;
132
132
  }
133
133
  const CURSOR_AGENT_HEIGHT = 12;
134
+ const defaultCursorOption = {
135
+ width: 1,
136
+ color: "#000000",
137
+ dragWidth: 2,
138
+ dragColor: "#0000FF"
139
+ };
134
140
  const EDITOR_COMPONENT = "editor-component";
135
141
  const EDITOR_PREFIX = "ce";
136
142
  var ElementType;
@@ -3928,8 +3934,8 @@ class CursorAgent {
3928
3934
  _compositionstart() {
3929
3935
  this.canvasEvent.compositionstart();
3930
3936
  }
3931
- _compositionend() {
3932
- this.canvasEvent.compositionend();
3937
+ _compositionend(evt) {
3938
+ this.canvasEvent.compositionend(evt);
3933
3939
  }
3934
3940
  }
3935
3941
  class Cursor {
@@ -3955,12 +3961,18 @@ class Cursor {
3955
3961
  getAgentDom() {
3956
3962
  return this.cursorAgent.getAgentCursorDom();
3957
3963
  }
3958
- drawCursor() {
3959
- const isReadonly = this.draw.isReadonly();
3964
+ getAgentDomValue() {
3965
+ return this.getAgentDom().value;
3966
+ }
3967
+ clearAgentDomValue() {
3968
+ return this.getAgentDom().value = "";
3969
+ }
3970
+ drawCursor(payload) {
3960
3971
  const cursorPosition = this.position.getCursorPosition();
3961
3972
  if (!cursorPosition)
3962
3973
  return;
3963
- const { scale } = this.options;
3974
+ const { scale, cursor } = this.options;
3975
+ const { color, width, isBlink = true } = __spreadValues(__spreadValues({}, cursor), payload);
3964
3976
  const height = this.draw.getHeight();
3965
3977
  const pageGap = this.draw.getPageGap();
3966
3978
  const { metrics, coordinate: { leftTop, rightTop }, ascent, pageNo } = cursorPosition;
@@ -3977,13 +3989,21 @@ class Cursor {
3977
3989
  const cursorLeft = rightTop[0];
3978
3990
  agentCursorDom.style.left = `${cursorLeft}px`;
3979
3991
  agentCursorDom.style.top = `${cursorTop + cursorHeight - CURSOR_AGENT_HEIGHT * scale}px`;
3992
+ const isReadonly = this.draw.isReadonly();
3993
+ this.cursorDom.style.width = `${width}px`;
3994
+ this.cursorDom.style.backgroundColor = color;
3980
3995
  this.cursorDom.style.left = `${cursorLeft}px`;
3981
3996
  this.cursorDom.style.top = `${cursorTop}px`;
3982
3997
  this.cursorDom.style.display = isReadonly ? "none" : "block";
3983
3998
  this.cursorDom.style.height = `${cursorHeight}px`;
3984
- setTimeout(() => {
3985
- this.cursorDom.classList.add(`${EDITOR_PREFIX}-cursor--animation`);
3986
- }, 200);
3999
+ const animationClassName = `${EDITOR_PREFIX}-cursor--animation`;
4000
+ if (isBlink) {
4001
+ setTimeout(() => {
4002
+ this.cursorDom.classList.add(animationClassName);
4003
+ }, 200);
4004
+ } else {
4005
+ this.cursorDom.classList.remove(animationClassName);
4006
+ }
3987
4007
  }
3988
4008
  recoveryCursor() {
3989
4009
  this.cursorDom.style.display = "none";
@@ -4399,6 +4419,8 @@ function isMod(evt) {
4399
4419
  }
4400
4420
  function keydown(evt, host) {
4401
4421
  var _a;
4422
+ if (host.isComposing)
4423
+ return;
4402
4424
  const draw = host.getDraw();
4403
4425
  const position = draw.getPosition();
4404
4426
  const cursorPosition = position.getCursorPosition();
@@ -4660,23 +4682,25 @@ function input(data2, host) {
4660
4682
  return;
4661
4683
  const position = draw.getPosition();
4662
4684
  const cursorPosition = position.getCursorPosition();
4663
- if (!data2 || !cursorPosition || host.isCompositing)
4685
+ if (!data2 || !cursorPosition)
4664
4686
  return;
4665
4687
  const control = draw.getControl();
4666
4688
  if (control.isPartRangeInControlOutside()) {
4667
4689
  return;
4668
4690
  }
4691
+ const isComposing = host.isComposing;
4692
+ if (!isComposing) {
4693
+ const cursor = draw.getCursor();
4694
+ cursor.clearAgentDomValue();
4695
+ } else {
4696
+ removeComposingInput(host);
4697
+ }
4669
4698
  const activeControl = control.getActiveControl();
4670
4699
  const { TEXT, HYPERLINK, SUBSCRIPT, SUPERSCRIPT, DATE } = ElementType;
4671
4700
  const text = data2.replaceAll(`
4672
4701
  `, ZERO);
4673
- const cursor = draw.getCursor();
4674
- const agentDom = cursor.getAgentDom();
4675
- agentDom.value = "";
4676
- const { index: index2 } = cursorPosition;
4677
4702
  const rangeManager = draw.getRange();
4678
4703
  const { startIndex, endIndex } = rangeManager.getRange();
4679
- const isCollapsed = startIndex === endIndex;
4680
4704
  const positionContext = position.getPositionContext();
4681
4705
  let restArg = {};
4682
4706
  if (positionContext.isTable) {
@@ -4698,26 +4722,46 @@ function input(data2, host) {
4698
4722
  }
4699
4723
  });
4700
4724
  }
4725
+ if (isComposing) {
4726
+ newElement.underline = true;
4727
+ }
4701
4728
  return newElement;
4702
4729
  });
4703
4730
  let curIndex;
4704
4731
  if (activeControl && ((_a = elementList[endIndex + 1]) == null ? void 0 : _a.controlId) === element.controlId) {
4705
4732
  curIndex = control.setValue(inputData);
4706
4733
  } else {
4707
- let start = 0;
4708
- if (isCollapsed) {
4709
- start = index2 + 1;
4710
- } else {
4711
- start = startIndex + 1;
4712
- elementList.splice(startIndex + 1, endIndex - startIndex);
4734
+ const start = startIndex + 1;
4735
+ if (startIndex !== endIndex) {
4736
+ elementList.splice(start, endIndex - startIndex);
4713
4737
  }
4714
4738
  for (let i = 0; i < inputData.length; i++) {
4715
4739
  elementList.splice(start + i, 0, inputData[i]);
4716
4740
  }
4717
- curIndex = (isCollapsed ? index2 : startIndex) + inputData.length;
4741
+ curIndex = startIndex + inputData.length;
4718
4742
  }
4719
4743
  rangeManager.setRange(curIndex, curIndex);
4720
- draw.render({ curIndex });
4744
+ draw.render({
4745
+ curIndex,
4746
+ isSubmitHistory: !isComposing
4747
+ });
4748
+ if (isComposing) {
4749
+ host.compositionInfo = {
4750
+ elementList,
4751
+ value: text,
4752
+ startIndex: curIndex - inputData.length,
4753
+ endIndex: curIndex
4754
+ };
4755
+ }
4756
+ }
4757
+ function removeComposingInput(host) {
4758
+ if (!host.compositionInfo)
4759
+ return;
4760
+ const { elementList, startIndex, endIndex } = host.compositionInfo;
4761
+ elementList.splice(startIndex + 1, endIndex - startIndex);
4762
+ const rangeManager = host.getDraw().getRange();
4763
+ rangeManager.setRange(startIndex, startIndex);
4764
+ host.compositionInfo = null;
4721
4765
  }
4722
4766
  function cut(host) {
4723
4767
  const draw = host.getDraw();
@@ -4871,10 +4915,26 @@ var click = {
4871
4915
  threeClick
4872
4916
  };
4873
4917
  function compositionstart(host) {
4874
- host.isCompositing = true;
4918
+ host.isComposing = true;
4875
4919
  }
4876
- function compositionend(host) {
4877
- host.isCompositing = false;
4920
+ function compositionend(host, evt) {
4921
+ host.isComposing = false;
4922
+ removeComposingInput(host);
4923
+ const draw = host.getDraw();
4924
+ const cursor = draw.getCursor();
4925
+ if (!evt.data) {
4926
+ const agentText = cursor.getAgentDomValue();
4927
+ if (agentText) {
4928
+ input(agentText, host);
4929
+ } else {
4930
+ const rangeManager = draw.getRange();
4931
+ const { endIndex: curIndex } = rangeManager.getRange();
4932
+ draw.render({
4933
+ curIndex
4934
+ });
4935
+ }
4936
+ }
4937
+ cursor.clearAgentDomValue();
4878
4938
  }
4879
4939
  var composition = {
4880
4940
  compositionstart,
@@ -4908,7 +4968,12 @@ function dragover(evt, host) {
4908
4968
  position.setCursorPosition(positionList[curIndex]);
4909
4969
  }
4910
4970
  const cursor = draw.getCursor();
4911
- cursor.drawCursor();
4971
+ const { cursor: { dragColor, dragWidth } } = draw.getOptions();
4972
+ cursor.drawCursor({
4973
+ width: dragWidth,
4974
+ color: dragColor,
4975
+ isBlink: false
4976
+ });
4912
4977
  }
4913
4978
  var drag = {
4914
4979
  dragover
@@ -4916,7 +4981,8 @@ var drag = {
4916
4981
  class CanvasEvent {
4917
4982
  constructor(draw) {
4918
4983
  __publicField(this, "isAllowSelection");
4919
- __publicField(this, "isCompositing");
4984
+ __publicField(this, "isComposing");
4985
+ __publicField(this, "compositionInfo");
4920
4986
  __publicField(this, "isAllowDrag");
4921
4987
  __publicField(this, "isAllowDrop");
4922
4988
  __publicField(this, "cacheRange");
@@ -4934,7 +5000,8 @@ class CanvasEvent {
4934
5000
  this.range = this.draw.getRange();
4935
5001
  this.position = this.draw.getPosition();
4936
5002
  this.isAllowSelection = false;
4937
- this.isCompositing = false;
5003
+ this.isComposing = false;
5004
+ this.compositionInfo = null;
4938
5005
  this.isAllowDrag = false;
4939
5006
  this.isAllowDrop = false;
4940
5007
  this.cacheRange = null;
@@ -5033,8 +5100,8 @@ class CanvasEvent {
5033
5100
  compositionstart() {
5034
5101
  composition.compositionstart(this);
5035
5102
  }
5036
- compositionend() {
5037
- composition.compositionend(this);
5103
+ compositionend(evt) {
5104
+ composition.compositionend(this, evt);
5038
5105
  }
5039
5106
  drop(evt) {
5040
5107
  drop(evt, this);
@@ -5056,6 +5123,7 @@ class GlobalEvent {
5056
5123
  __publicField(this, "hyperlinkParticle");
5057
5124
  __publicField(this, "control");
5058
5125
  __publicField(this, "dateParticle");
5126
+ __publicField(this, "dprMediaQueryList");
5059
5127
  __publicField(this, "recoverEffect", (evt) => {
5060
5128
  if (!this.cursor)
5061
5129
  return;
@@ -5109,6 +5177,9 @@ class GlobalEvent {
5109
5177
  (_a = this.cursor) == null ? void 0 : _a.drawCursor();
5110
5178
  }
5111
5179
  });
5180
+ __publicField(this, "_handleDprChange", () => {
5181
+ this.draw.setPageDevicePixel();
5182
+ });
5112
5183
  this.draw = draw;
5113
5184
  this.canvas = draw.getPage();
5114
5185
  this.options = draw.getOptions();
@@ -5120,6 +5191,7 @@ class GlobalEvent {
5120
5191
  this.hyperlinkParticle = draw.getHyperlinkParticle();
5121
5192
  this.dateParticle = draw.getDateParticle();
5122
5193
  this.control = draw.getControl();
5194
+ this.dprMediaQueryList = window.matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`);
5123
5195
  }
5124
5196
  register() {
5125
5197
  this.cursor = this.draw.getCursor();
@@ -5132,6 +5204,7 @@ class GlobalEvent {
5132
5204
  document.addEventListener("mouseup", this.setCanvasEventAbility);
5133
5205
  document.addEventListener("wheel", this.setPageScale, { passive: false });
5134
5206
  document.addEventListener("visibilitychange", this._handleVisibilityChange);
5207
+ this.dprMediaQueryList.addEventListener("change", this._handleDprChange);
5135
5208
  }
5136
5209
  removeEvent() {
5137
5210
  window.removeEventListener("blur", this.recoverEffect);
@@ -5140,6 +5213,7 @@ class GlobalEvent {
5140
5213
  document.removeEventListener("mouseup", this.setCanvasEventAbility);
5141
5214
  document.removeEventListener("wheel", this.setPageScale);
5142
5215
  document.removeEventListener("visibilitychange", this._handleVisibilityChange);
5216
+ this.dprMediaQueryList.removeEventListener("change", this._handleDprChange);
5143
5217
  }
5144
5218
  }
5145
5219
  class HistoryManager {
@@ -6100,46 +6174,12 @@ class PageNumber {
6100
6174
  class ScrollObserver {
6101
6175
  constructor(draw) {
6102
6176
  __publicField(this, "draw");
6103
- __publicField(this, "options");
6104
- __publicField(this, "pageContainer");
6105
- __publicField(this, "pageHeight");
6106
6177
  __publicField(this, "_observer", debounce(() => {
6107
- const rect = this.pageContainer.getBoundingClientRect();
6108
- const top = Math.abs(rect.top);
6109
- const bottom = top + window.innerHeight;
6110
- const pageList = this.draw.getPageList();
6111
- const visiblePageNoList = [];
6112
- let intersectionPageNo = 0;
6113
- let intersectionMaxHeight = 0;
6114
- for (let i = 0; i < pageList.length; i++) {
6115
- const curTop = i * this.pageHeight;
6116
- const curBottom = (i + 1) * this.pageHeight;
6117
- if (curTop > bottom)
6118
- break;
6119
- if (curBottom < top)
6120
- continue;
6121
- let intersectionHeight = 0;
6122
- if (curTop < top && curBottom < bottom) {
6123
- intersectionHeight = curBottom - top;
6124
- } else if (curTop > top && curBottom > bottom) {
6125
- intersectionHeight = bottom - curTop;
6126
- } else {
6127
- intersectionHeight = rect.height;
6128
- }
6129
- if (intersectionHeight > intersectionMaxHeight) {
6130
- intersectionMaxHeight = intersectionHeight;
6131
- intersectionPageNo = i;
6132
- }
6133
- visiblePageNoList.push(i);
6134
- }
6178
+ const { intersectionPageNo, visiblePageNoList } = this.getPageVisibleInfo();
6135
6179
  this.draw.setIntersectionPageNo(intersectionPageNo);
6136
6180
  this.draw.setVisiblePageNoList(visiblePageNoList);
6137
6181
  }, 150));
6138
6182
  this.draw = draw;
6139
- this.options = draw.getOptions();
6140
- this.pageContainer = draw.getPageContainer();
6141
- const { height, pageGap } = this.options;
6142
- this.pageHeight = height + pageGap;
6143
6183
  setTimeout(() => {
6144
6184
  if (!window.scrollY) {
6145
6185
  this._observer();
@@ -6153,6 +6193,37 @@ class ScrollObserver {
6153
6193
  removeEvent() {
6154
6194
  document.removeEventListener("scroll", this._observer);
6155
6195
  }
6196
+ getElementVisibleInfo(element) {
6197
+ const rect = element.getBoundingClientRect();
6198
+ const viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight);
6199
+ const visibleHeight = Math.min(rect.bottom, viewHeight) - Math.max(rect.top, 0);
6200
+ return {
6201
+ intersectionHeight: visibleHeight > 0 ? visibleHeight : 0
6202
+ };
6203
+ }
6204
+ getPageVisibleInfo() {
6205
+ const pageList = this.draw.getPageList();
6206
+ const visiblePageNoList = [];
6207
+ let intersectionPageNo = 0;
6208
+ let intersectionMaxHeight = 0;
6209
+ for (let i = 0; i < pageList.length; i++) {
6210
+ const curPage = pageList[i];
6211
+ const { intersectionHeight } = this.getElementVisibleInfo(curPage);
6212
+ if (intersectionMaxHeight && !intersectionHeight)
6213
+ break;
6214
+ if (intersectionHeight) {
6215
+ visiblePageNoList.push(i);
6216
+ }
6217
+ if (intersectionHeight > intersectionMaxHeight) {
6218
+ intersectionMaxHeight = intersectionHeight;
6219
+ intersectionPageNo = i;
6220
+ }
6221
+ }
6222
+ return {
6223
+ intersectionPageNo,
6224
+ visiblePageNoList
6225
+ };
6226
+ }
6156
6227
  }
6157
6228
  var MoveDirection;
6158
6229
  (function(MoveDirection2) {
@@ -8937,16 +9008,20 @@ class Draw {
8937
9008
  });
8938
9009
  }
8939
9010
  setPageScale(payload) {
9011
+ const dpr = window.devicePixelRatio;
8940
9012
  this.options.scale = payload;
8941
9013
  const width = this.getWidth();
8942
9014
  const height = this.getHeight();
8943
9015
  this.container.style.width = `${width}px`;
8944
- this.pageList.forEach((p) => {
9016
+ this.pageList.forEach((p, i) => {
8945
9017
  p.width = width;
8946
9018
  p.height = height;
8947
9019
  p.style.width = `${width}px`;
8948
9020
  p.style.height = `${height}px`;
8949
9021
  p.style.marginBottom = `${this.getPageGap()}px`;
9022
+ p.width = width * dpr;
9023
+ p.height = height * dpr;
9024
+ this.ctxList[i].scale(dpr, dpr);
8950
9025
  });
8951
9026
  this.render({
8952
9027
  isSubmitHistory: false,
@@ -8956,6 +9031,20 @@ class Draw {
8956
9031
  this.listener.pageScaleChange(payload);
8957
9032
  }
8958
9033
  }
9034
+ setPageDevicePixel() {
9035
+ const dpr = window.devicePixelRatio;
9036
+ const width = this.getWidth();
9037
+ const height = this.getHeight();
9038
+ this.pageList.forEach((p, i) => {
9039
+ p.width = width * dpr;
9040
+ p.height = height * dpr;
9041
+ this.ctxList[i].scale(dpr, dpr);
9042
+ });
9043
+ this.render({
9044
+ isSubmitHistory: false,
9045
+ isSetCursor: false
9046
+ });
9047
+ }
8959
9048
  setPaperSize(width, height) {
8960
9049
  this.options.width = width;
8961
9050
  this.options.height = height;
@@ -11420,7 +11509,7 @@ const controlMenus = [
11420
11509
  const globalMenus = [
11421
11510
  {
11422
11511
  i18nPath: "contextmenu.global.cut",
11423
- shortCut: "Ctrl + X",
11512
+ shortCut: `${isApple ? "\u2318" : "Ctrl"} + X`,
11424
11513
  when: (payload) => {
11425
11514
  return !payload.isReadonly;
11426
11515
  },
@@ -11430,7 +11519,7 @@ const globalMenus = [
11430
11519
  },
11431
11520
  {
11432
11521
  i18nPath: "contextmenu.global.copy",
11433
- shortCut: "Ctrl + C",
11522
+ shortCut: `${isApple ? "\u2318" : "Ctrl"} + C`,
11434
11523
  when: (payload) => {
11435
11524
  return payload.editorHasSelection;
11436
11525
  },
@@ -11440,7 +11529,7 @@ const globalMenus = [
11440
11529
  },
11441
11530
  {
11442
11531
  i18nPath: "contextmenu.global.paste",
11443
- shortCut: "Ctrl + V",
11532
+ shortCut: `${isApple ? "\u2318" : "Ctrl"} + V`,
11444
11533
  when: (payload) => {
11445
11534
  return !payload.isReadonly && payload.editorTextFocus;
11446
11535
  },
@@ -11450,7 +11539,7 @@ const globalMenus = [
11450
11539
  },
11451
11540
  {
11452
11541
  i18nPath: "contextmenu.global.selectAll",
11453
- shortCut: "Ctrl + A",
11542
+ shortCut: `${isApple ? "\u2318" : "Ctrl"} + A`,
11454
11543
  when: (payload) => {
11455
11544
  return payload.editorTextFocus;
11456
11545
  },
@@ -12047,6 +12136,7 @@ class Editor {
12047
12136
  const waterMarkOptions = __spreadValues(__spreadValues({}, defaultWatermarkOption), options.watermark);
12048
12137
  const controlOptions = __spreadValues(__spreadValues({}, defaultControlOption), options.control);
12049
12138
  const checkboxOptions = __spreadValues(__spreadValues({}, defaultCheckboxOption), options.checkbox);
12139
+ const cursorOptions = __spreadValues(__spreadValues({}, defaultCursorOption), options.cursor);
12050
12140
  const editorOptions = __spreadProps(__spreadValues({
12051
12141
  mode: EditorMode.EDIT,
12052
12142
  defaultType: "TEXT",
@@ -12085,7 +12175,8 @@ class Editor {
12085
12175
  header: headerOptions,
12086
12176
  watermark: waterMarkOptions,
12087
12177
  control: controlOptions,
12088
- checkbox: checkboxOptions
12178
+ checkbox: checkboxOptions,
12179
+ cursor: cursorOptions
12089
12180
  });
12090
12181
  formatElementList(elementList, {
12091
12182
  editorOptions