toggle-components-library 1.37.0-beta.4 → 1.37.0-beta.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "toggle-components-library",
3
- "version": "1.37.0-beta.4",
3
+ "version": "1.37.0-beta.5",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "serve": "vue-cli-service serve",
@@ -3,12 +3,14 @@
3
3
  <div v-for="(crumb, index) in breadcrumb_computed" :key="index">
4
4
  <template v-if="index === breadcrumb_computed.length - 1 && editable">
5
5
  <span
6
+ ref="editableSpan"
6
7
  contenteditable="true"
7
8
  class="toggle-breadcrumb-editable-input"
8
- @input="updateContent($event);"
9
+ @input="updateContent($event)"
9
10
  @keydown.enter.prevent="handleEnterKey"
10
11
  @paste.prevent="handlePaste"
11
- >{{ crumb.name }}</span>
12
+ v-text="initialContent"
13
+ ></span>
12
14
  </template>
13
15
  <template v-else>
14
16
  <router-link :to="{ name: crumb.link}" class="back-product" v-if="crumb.link && !isNuxt">{{ crumb.name }}</router-link>
@@ -44,41 +46,86 @@ export default {
44
46
  }
45
47
  },
46
48
 
47
- data: function () {
49
+ data() {
48
50
  return {
49
- content: ''
51
+ content: '',
52
+ initialContent: '',
53
+ lastSelection: null
50
54
  };
51
55
  },
52
56
 
53
57
  computed: {
54
58
  breadcrumb_computed() {
55
- return this.isNuxt ? this.breadcrumb : this.$route.meta.breadcrumb
56
- },
59
+ return this.isNuxt ? this.breadcrumb : this.$route.meta.breadcrumb;
60
+ }
61
+ },
62
+
63
+ mounted() {
64
+ if (this.breadcrumb_computed && this.breadcrumb_computed.length > 0) {
65
+ const lastCrumb = this.breadcrumb_computed[this.breadcrumb_computed.length - 1];
66
+ this.content = lastCrumb.name;
67
+ this.initialContent = lastCrumb.name;
68
+ }
57
69
  },
58
70
 
59
71
  methods: {
72
+ saveSelection() {
73
+ if (window.getSelection) {
74
+ const sel = window.getSelection();
75
+ if (sel.getRangeAt && sel.rangeCount) {
76
+ const range = sel.getRangeAt(0);
77
+ this.lastSelection = {
78
+ start: range.startOffset,
79
+ end: range.endOffset
80
+ };
81
+ }
82
+ }
83
+ },
84
+
85
+ restoreSelection() {
86
+ if (!this.lastSelection || !this.$refs.editableSpan) return;
87
+
88
+ const el = this.$refs.editableSpan;
89
+ if (!el.firstChild && typeof el.textContent === 'string') {
90
+ el.textContent = this.content || '';
91
+ }
92
+
93
+ try {
94
+ const range = document.createRange();
95
+ const sel = window.getSelection();
96
+ const textNode = el.firstChild || el;
97
+
98
+ // Ensure we don't exceed the text length
99
+ const start = Math.min(this.lastSelection.start, textNode.length);
100
+ const end = Math.min(this.lastSelection.end, textNode.length);
101
+
102
+ range.setStart(textNode, start);
103
+ range.setEnd(textNode, end);
104
+ sel.removeAllRanges();
105
+ sel.addRange(range);
106
+ } catch (e) {
107
+ console.warn('Failed to restore selection:', e);
108
+ }
109
+ },
110
+
60
111
  handlePaste(event) {
61
- // Get plain text from clipboard and clean it
62
112
  const text = (event.clipboardData || window.clipboardData).getData('text');
63
113
  const cleanText = text.replace(/[\r\n\s]+/g, ' ').trim();
64
114
 
65
- // Get current selection information
115
+ this.saveSelection();
66
116
  const selection = window.getSelection();
67
117
  const selectionLength = selection.toString().length;
68
118
  const currentLength = this.content.length;
69
119
 
70
- // Check if adding the pasted text would exceed maxChars
71
120
  if (currentLength - selectionLength + cleanText.length <= this.maxChars && selection.rangeCount) {
72
121
  const range = selection.getRangeAt(0);
73
122
  range.deleteContents();
74
123
  range.insertNode(document.createTextNode(cleanText));
75
124
 
76
- // Move cursor to end of inserted text
77
125
  range.collapse(false);
78
126
  selection.removeAllRanges();
79
127
  selection.addRange(range);
80
128
 
81
- // Update content
82
129
  this.content = event.target.innerText;
83
130
  this.$emit('update:lastCrumb', this.content);
84
131
  }
@@ -89,31 +136,21 @@ export default {
89
136
  event.target.blur();
90
137
  },
91
138
 
92
-
93
139
  updateContent(event) {
140
+ this.saveSelection();
94
141
  const newText = event.target.innerText;
95
142
 
96
- // If text exceeds maxChars, prevent the change
97
143
  if (newText.length > this.maxChars) {
98
- // Save current selection
99
- const selection = window.getSelection();
100
- const range = selection.getRangeAt(0);
101
- const cursorOffset = range.startOffset;
102
-
103
- // Restore previous content
104
144
  event.target.innerText = this.content;
105
-
106
- // Restore cursor position
107
- const newRange = document.createRange();
108
- newRange.setStart(event.target.firstChild || event.target, Math.min(cursorOffset, this.content.length));
109
- newRange.collapse(true);
110
- selection.removeAllRanges();
111
- selection.addRange(newRange);
145
+ this.restoreSelection();
112
146
  } else {
113
147
  this.content = newText;
114
148
  this.$emit('update:lastCrumb', this.content);
149
+ this.$nextTick(() => {
150
+ this.restoreSelection();
151
+ });
115
152
  }
116
153
  }
117
154
  }
118
- }
155
+ };
119
156
  </script>