modern-text 1.10.4 → 1.10.6

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.
@@ -70,6 +70,7 @@ class TextEditor extends HTMLElement {
70
70
  set text(newText) {
71
71
  if (newText) {
72
72
  this._text?.off("update", this._update);
73
+ this.reset();
73
74
  newText.on("update", this._update);
74
75
  this._text = newText;
75
76
  this._setTextInput(this.getPlaintext());
@@ -555,6 +556,7 @@ class TextEditor extends HTMLElement {
555
556
  selectAll() {
556
557
  this._textarea.focus();
557
558
  this._textarea.select();
559
+ this._updateSelectionByDom();
558
560
  }
559
561
  attributeChangedCallback(name, _oldValue, newValue) {
560
562
  this[name] = newValue;
@@ -570,53 +572,51 @@ class TextEditor extends HTMLElement {
570
572
  );
571
573
  }
572
574
  _renderSelectRange() {
573
- if (this.selection[0] !== this._prevSelection[0] || this.selection[1] !== this._prevSelection[1]) {
574
- const isVertical = this.text.isVertical;
575
- const boxesGroupsMap = {};
576
- this._selectedChars.forEach((char) => {
577
- if (char.isLastSelected) {
578
- return;
579
- }
580
- const key = isVertical ? char.left : char.top;
581
- if (!boxesGroupsMap[key]) {
582
- boxesGroupsMap[key] = [];
583
- }
584
- boxesGroupsMap[key].push({
585
- x: char.left - this.text.glyphBox.left,
586
- y: char.top - this.text.glyphBox.top,
587
- w: char.width,
588
- h: char.height
589
- });
575
+ const isVertical = this.text.isVertical;
576
+ const boxesGroupsMap = {};
577
+ this._selectedChars.forEach((char) => {
578
+ if (char.isLastSelected) {
579
+ return;
580
+ }
581
+ const key = isVertical ? char.left : char.top;
582
+ if (!boxesGroupsMap[key]) {
583
+ boxesGroupsMap[key] = [];
584
+ }
585
+ boxesGroupsMap[key].push({
586
+ x: char.left - this.text.glyphBox.left,
587
+ y: char.top - this.text.glyphBox.top,
588
+ w: char.width,
589
+ h: char.height
590
590
  });
591
- const boxesGroups = Object.values(boxesGroupsMap);
592
- const sourceLen = this._selection.children.length;
593
- const targetLen = boxesGroups.length;
594
- const len = Math.max(sourceLen, targetLen);
595
- const deleted = [];
596
- for (let i = 0; i < len; i++) {
597
- let element = this._selection.children.item(i);
598
- const boxes = boxesGroups[i];
599
- if (!boxes) {
600
- deleted.push(element);
601
- continue;
602
- } else if (!element) {
603
- element = document.createElement("div");
604
- this._selection.append(element);
605
- }
606
- const min = {
607
- x: Math.min(...boxes.map((v) => v.x)),
608
- y: Math.min(...boxes.map((v) => v.y))
609
- };
610
- const max = {
611
- x: Math.max(...boxes.map((v) => v.x + v.w)),
612
- y: Math.max(...boxes.map((v) => v.y + v.h))
613
- };
614
- element.style.width = `${max.x - min.x}px`;
615
- element.style.height = `${max.y - min.y}px`;
616
- element.style.transform = `translate(${min.x}px, ${min.y}px)`;
591
+ });
592
+ const boxesGroups = Object.values(boxesGroupsMap);
593
+ const sourceLen = this._selection.children.length;
594
+ const targetLen = boxesGroups.length;
595
+ const len = Math.max(sourceLen, targetLen);
596
+ const deleted = [];
597
+ for (let i = 0; i < len; i++) {
598
+ let element = this._selection.children.item(i);
599
+ const boxes = boxesGroups[i];
600
+ if (!boxes) {
601
+ deleted.push(element);
602
+ continue;
603
+ } else if (!element) {
604
+ element = document.createElement("div");
605
+ this._selection.append(element);
617
606
  }
618
- deleted.forEach((el) => el?.remove());
607
+ const min = {
608
+ x: Math.min(...boxes.map((v) => v.x)),
609
+ y: Math.min(...boxes.map((v) => v.y))
610
+ };
611
+ const max = {
612
+ x: Math.max(...boxes.map((v) => v.x + v.w)),
613
+ y: Math.max(...boxes.map((v) => v.y + v.h))
614
+ };
615
+ element.style.width = `${max.x - min.x}px`;
616
+ element.style.height = `${max.y - min.y}px`;
617
+ element.style.transform = `translate(${min.x}px, ${min.y}px)`;
619
618
  }
619
+ deleted.forEach((el) => el?.remove());
620
620
  }
621
621
  _renderCursor() {
622
622
  if (this._showCursor && this._cursorPosition && this._selectedChars.length === 0) {
@@ -636,6 +636,9 @@ class TextEditor extends HTMLElement {
636
636
  }
637
637
  this._timer = setTimeout(() => this._cursor.classList.add("blink"), 500);
638
638
  }
639
+ reset() {
640
+ this.selection = [];
641
+ }
639
642
  }
640
643
  __decorateClass([
641
644
  modernIdoc.property({ fallback: 0 })
@@ -80,6 +80,7 @@ declare class TextEditor extends HTMLElement implements PropertyAccessor {
80
80
  protected _emit(type: string, detail?: any): boolean;
81
81
  protected _renderSelectRange(): void;
82
82
  protected _renderCursor(): void;
83
+ reset(): void;
83
84
  }
84
85
 
85
86
  export { TextEditor };
@@ -80,6 +80,7 @@ declare class TextEditor extends HTMLElement implements PropertyAccessor {
80
80
  protected _emit(type: string, detail?: any): boolean;
81
81
  protected _renderSelectRange(): void;
82
82
  protected _renderCursor(): void;
83
+ reset(): void;
83
84
  }
84
85
 
85
86
  export { TextEditor };
@@ -80,6 +80,7 @@ declare class TextEditor extends HTMLElement implements PropertyAccessor {
80
80
  protected _emit(type: string, detail?: any): boolean;
81
81
  protected _renderSelectRange(): void;
82
82
  protected _renderCursor(): void;
83
+ reset(): void;
83
84
  }
84
85
 
85
86
  export { TextEditor };
@@ -68,6 +68,7 @@ class TextEditor extends HTMLElement {
68
68
  set text(newText) {
69
69
  if (newText) {
70
70
  this._text?.off("update", this._update);
71
+ this.reset();
71
72
  newText.on("update", this._update);
72
73
  this._text = newText;
73
74
  this._setTextInput(this.getPlaintext());
@@ -553,6 +554,7 @@ class TextEditor extends HTMLElement {
553
554
  selectAll() {
554
555
  this._textarea.focus();
555
556
  this._textarea.select();
557
+ this._updateSelectionByDom();
556
558
  }
557
559
  attributeChangedCallback(name, _oldValue, newValue) {
558
560
  this[name] = newValue;
@@ -568,53 +570,51 @@ class TextEditor extends HTMLElement {
568
570
  );
569
571
  }
570
572
  _renderSelectRange() {
571
- if (this.selection[0] !== this._prevSelection[0] || this.selection[1] !== this._prevSelection[1]) {
572
- const isVertical = this.text.isVertical;
573
- const boxesGroupsMap = {};
574
- this._selectedChars.forEach((char) => {
575
- if (char.isLastSelected) {
576
- return;
577
- }
578
- const key = isVertical ? char.left : char.top;
579
- if (!boxesGroupsMap[key]) {
580
- boxesGroupsMap[key] = [];
581
- }
582
- boxesGroupsMap[key].push({
583
- x: char.left - this.text.glyphBox.left,
584
- y: char.top - this.text.glyphBox.top,
585
- w: char.width,
586
- h: char.height
587
- });
573
+ const isVertical = this.text.isVertical;
574
+ const boxesGroupsMap = {};
575
+ this._selectedChars.forEach((char) => {
576
+ if (char.isLastSelected) {
577
+ return;
578
+ }
579
+ const key = isVertical ? char.left : char.top;
580
+ if (!boxesGroupsMap[key]) {
581
+ boxesGroupsMap[key] = [];
582
+ }
583
+ boxesGroupsMap[key].push({
584
+ x: char.left - this.text.glyphBox.left,
585
+ y: char.top - this.text.glyphBox.top,
586
+ w: char.width,
587
+ h: char.height
588
588
  });
589
- const boxesGroups = Object.values(boxesGroupsMap);
590
- const sourceLen = this._selection.children.length;
591
- const targetLen = boxesGroups.length;
592
- const len = Math.max(sourceLen, targetLen);
593
- const deleted = [];
594
- for (let i = 0; i < len; i++) {
595
- let element = this._selection.children.item(i);
596
- const boxes = boxesGroups[i];
597
- if (!boxes) {
598
- deleted.push(element);
599
- continue;
600
- } else if (!element) {
601
- element = document.createElement("div");
602
- this._selection.append(element);
603
- }
604
- const min = {
605
- x: Math.min(...boxes.map((v) => v.x)),
606
- y: Math.min(...boxes.map((v) => v.y))
607
- };
608
- const max = {
609
- x: Math.max(...boxes.map((v) => v.x + v.w)),
610
- y: Math.max(...boxes.map((v) => v.y + v.h))
611
- };
612
- element.style.width = `${max.x - min.x}px`;
613
- element.style.height = `${max.y - min.y}px`;
614
- element.style.transform = `translate(${min.x}px, ${min.y}px)`;
589
+ });
590
+ const boxesGroups = Object.values(boxesGroupsMap);
591
+ const sourceLen = this._selection.children.length;
592
+ const targetLen = boxesGroups.length;
593
+ const len = Math.max(sourceLen, targetLen);
594
+ const deleted = [];
595
+ for (let i = 0; i < len; i++) {
596
+ let element = this._selection.children.item(i);
597
+ const boxes = boxesGroups[i];
598
+ if (!boxes) {
599
+ deleted.push(element);
600
+ continue;
601
+ } else if (!element) {
602
+ element = document.createElement("div");
603
+ this._selection.append(element);
615
604
  }
616
- deleted.forEach((el) => el?.remove());
605
+ const min = {
606
+ x: Math.min(...boxes.map((v) => v.x)),
607
+ y: Math.min(...boxes.map((v) => v.y))
608
+ };
609
+ const max = {
610
+ x: Math.max(...boxes.map((v) => v.x + v.w)),
611
+ y: Math.max(...boxes.map((v) => v.y + v.h))
612
+ };
613
+ element.style.width = `${max.x - min.x}px`;
614
+ element.style.height = `${max.y - min.y}px`;
615
+ element.style.transform = `translate(${min.x}px, ${min.y}px)`;
617
616
  }
617
+ deleted.forEach((el) => el?.remove());
618
618
  }
619
619
  _renderCursor() {
620
620
  if (this._showCursor && this._cursorPosition && this._selectedChars.length === 0) {
@@ -634,6 +634,9 @@ class TextEditor extends HTMLElement {
634
634
  }
635
635
  this._timer = setTimeout(() => this._cursor.classList.add("blink"), 500);
636
636
  }
637
+ reset() {
638
+ this.selection = [];
639
+ }
637
640
  }
638
641
  __decorateClass([
639
642
  property({ fallback: 0 })
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modern-text",
3
3
  "type": "module",
4
- "version": "1.10.4",
4
+ "version": "1.10.6",
5
5
  "packageManager": "pnpm@10.18.1",
6
6
  "description": "Measure and render text in a way that describes the DOM.",
7
7
  "author": "wxm",
@@ -56,18 +56,18 @@
56
56
  "dependencies": {
57
57
  "diff": "^8.0.2",
58
58
  "modern-font": "^0.4.4",
59
- "modern-idoc": "^0.10.5",
60
- "modern-path2d": "^1.4.11"
59
+ "modern-idoc": "^0.10.8",
60
+ "modern-path2d": "^1.4.16"
61
61
  },
62
62
  "devDependencies": {
63
- "@antfu/eslint-config": "^6.2.0",
64
- "@types/node": "^24.10.0",
65
- "bumpp": "^10.3.1",
63
+ "@antfu/eslint-config": "^6.7.3",
64
+ "@types/node": "^25.0.3",
65
+ "bumpp": "^10.3.2",
66
66
  "conventional-changelog-cli": "^5.0.0",
67
- "eslint": "^9.39.0",
67
+ "eslint": "^9.39.2",
68
68
  "typescript": "^5.9.3",
69
69
  "unbuild": "^3.6.1",
70
- "vite": "^7.1.12",
71
- "vitest": "^4.0.6"
70
+ "vite": "^7.3.0",
71
+ "vitest": "^4.0.16"
72
72
  }
73
73
  }