toggle-components-library 1.37.0-beta.5 → 1.37.0-beta.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "toggle-components-library",
3
- "version": "1.37.0-beta.5",
3
+ "version": "1.37.0-beta.6",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "serve": "vue-cli-service serve",
@@ -6,11 +6,11 @@
6
6
  ref="editableSpan"
7
7
  contenteditable="true"
8
8
  class="toggle-breadcrumb-editable-input"
9
- @input="updateContent($event)"
9
+ @input="handleInput"
10
+ @keypress="handleKeypress"
10
11
  @keydown.enter.prevent="handleEnterKey"
11
12
  @paste.prevent="handlePaste"
12
- v-text="initialContent"
13
- ></span>
13
+ >{{ crumb.name }}</span>
14
14
  </template>
15
15
  <template v-else>
16
16
  <router-link :to="{ name: crumb.link}" class="back-product" v-if="crumb.link && !isNuxt">{{ crumb.name }}</router-link>
@@ -24,7 +24,6 @@
24
24
 
25
25
  <script>
26
26
  export default {
27
- mixins: [],
28
27
  props: {
29
28
  isNuxt: {
30
29
  type: Boolean,
@@ -48,10 +47,8 @@ export default {
48
47
 
49
48
  data() {
50
49
  return {
51
- content: '',
52
- initialContent: '',
53
- lastSelection: null
54
- };
50
+ lastValidContent: ''
51
+ }
55
52
  },
56
53
 
57
54
  computed: {
@@ -61,96 +58,93 @@ export default {
61
58
  },
62
59
 
63
60
  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
- }
61
+ this.updateContent();
69
62
  },
70
63
 
71
64
  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
- };
65
+ updateContent() {
66
+ if (this.$refs.editableSpan && this.breadcrumb_computed?.length) {
67
+ const lastCrumb = this.breadcrumb_computed[this.breadcrumb_computed.length - 1];
68
+ if (this.$refs.editableSpan.textContent !== lastCrumb.name) {
69
+ this.$refs.editableSpan.textContent = lastCrumb.name;
70
+ this.lastValidContent = lastCrumb.name;
81
71
  }
82
72
  }
83
73
  },
84
74
 
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 || '';
75
+ handleKeypress(event) {
76
+ if (event.key.length !== 1) return;
77
+
78
+ const currentLength = this.$refs.editableSpan.textContent.length;
79
+ const selection = window.getSelection();
80
+ const selectedLength = selection.toString().length;
81
+
82
+ if (currentLength - selectedLength >= this.maxChars) {
83
+ event.preventDefault();
84
+ return false;
91
85
  }
86
+ },
87
+
88
+ handleInput(event) {
89
+ const newText = event.target.textContent;
92
90
 
93
- try {
91
+ if (newText.length <= this.maxChars) {
92
+ this.lastValidContent = newText;
93
+ this.$emit('update:lastCrumb', newText);
94
+ } else {
95
+ // Get cursor position before restoring text
96
+ const selection = window.getSelection();
97
+ const cursorPosition = selection.getRangeAt(0).startOffset;
98
+
99
+ // Restore previous content
100
+ event.target.textContent = this.lastValidContent;
101
+
102
+ // Restore cursor position
94
103
  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);
104
+ const textNode = event.target.firstChild;
105
+ if (textNode) {
106
+ range.setStart(textNode, Math.min(cursorPosition, this.lastValidContent.length));
107
+ range.collapse(true);
108
+ selection.removeAllRanges();
109
+ selection.addRange(range);
110
+ }
108
111
  }
109
112
  },
110
113
 
111
114
  handlePaste(event) {
115
+ event.preventDefault();
112
116
  const text = (event.clipboardData || window.clipboardData).getData('text');
113
117
  const cleanText = text.replace(/[\r\n\s]+/g, ' ').trim();
114
118
 
115
- this.saveSelection();
116
119
  const selection = window.getSelection();
120
+ const range = selection.getRangeAt(0);
121
+ const currentLength = this.$refs.editableSpan.textContent.length;
117
122
  const selectionLength = selection.toString().length;
118
- const currentLength = this.content.length;
119
123
 
120
- if (currentLength - selectionLength + cleanText.length <= this.maxChars && selection.rangeCount) {
121
- const range = selection.getRangeAt(0);
124
+ const availableSpace = this.maxChars - (currentLength - selectionLength);
125
+ if (availableSpace > 0) {
126
+ const truncatedText = cleanText.slice(0, availableSpace);
122
127
  range.deleteContents();
123
- range.insertNode(document.createTextNode(cleanText));
124
-
128
+ range.insertNode(document.createTextNode(truncatedText));
125
129
  range.collapse(false);
126
- selection.removeAllRanges();
127
- selection.addRange(range);
128
-
129
- this.content = event.target.innerText;
130
- this.$emit('update:lastCrumb', this.content);
130
+ this.lastValidContent = this.$refs.editableSpan.textContent;
131
+ this.$emit('update:lastCrumb', this.lastValidContent);
131
132
  }
132
133
  },
133
134
 
134
135
  handleEnterKey(event) {
135
136
  event.preventDefault();
136
137
  event.target.blur();
137
- },
138
+ }
139
+ },
138
140
 
139
- updateContent(event) {
140
- this.saveSelection();
141
- const newText = event.target.innerText;
142
-
143
- if (newText.length > this.maxChars) {
144
- event.target.innerText = this.content;
145
- this.restoreSelection();
146
- } else {
147
- this.content = newText;
148
- this.$emit('update:lastCrumb', this.content);
149
- this.$nextTick(() => {
150
- this.restoreSelection();
151
- });
152
- }
141
+ watch: {
142
+ 'breadcrumb_computed': {
143
+ handler() {
144
+ this.$nextTick(this.updateContent);
145
+ },
146
+ deep: true
153
147
  }
154
148
  }
155
- };
149
+ }
156
150
  </script>