toggle-components-library 1.37.0-beta.4 → 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.4",
3
+ "version": "1.37.0-beta.6",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "serve": "vue-cli-service serve",
@@ -3,9 +3,11 @@
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="handleInput"
10
+ @keypress="handleKeypress"
9
11
  @keydown.enter.prevent="handleEnterKey"
10
12
  @paste.prevent="handlePaste"
11
13
  >{{ crumb.name }}</span>
@@ -22,7 +24,6 @@
22
24
 
23
25
  <script>
24
26
  export default {
25
- mixins: [],
26
27
  props: {
27
28
  isNuxt: {
28
29
  type: Boolean,
@@ -44,75 +45,105 @@ export default {
44
45
  }
45
46
  },
46
47
 
47
- data: function () {
48
+ data() {
48
49
  return {
49
- content: ''
50
- };
50
+ lastValidContent: ''
51
+ }
51
52
  },
52
53
 
53
54
  computed: {
54
55
  breadcrumb_computed() {
55
- return this.isNuxt ? this.breadcrumb : this.$route.meta.breadcrumb
56
- },
56
+ return this.isNuxt ? this.breadcrumb : this.$route.meta.breadcrumb;
57
+ }
58
+ },
59
+
60
+ mounted() {
61
+ this.updateContent();
57
62
  },
58
63
 
59
64
  methods: {
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;
71
+ }
72
+ }
73
+ },
74
+
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;
85
+ }
86
+ },
87
+
88
+ handleInput(event) {
89
+ const newText = event.target.textContent;
90
+
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
103
+ const range = document.createRange();
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
+ }
111
+ }
112
+ },
113
+
60
114
  handlePaste(event) {
61
- // Get plain text from clipboard and clean it
115
+ event.preventDefault();
62
116
  const text = (event.clipboardData || window.clipboardData).getData('text');
63
117
  const cleanText = text.replace(/[\r\n\s]+/g, ' ').trim();
64
118
 
65
- // Get current selection information
66
119
  const selection = window.getSelection();
120
+ const range = selection.getRangeAt(0);
121
+ const currentLength = this.$refs.editableSpan.textContent.length;
67
122
  const selectionLength = selection.toString().length;
68
- const currentLength = this.content.length;
69
123
 
70
- // Check if adding the pasted text would exceed maxChars
71
- if (currentLength - selectionLength + cleanText.length <= this.maxChars && selection.rangeCount) {
72
- const range = selection.getRangeAt(0);
124
+ const availableSpace = this.maxChars - (currentLength - selectionLength);
125
+ if (availableSpace > 0) {
126
+ const truncatedText = cleanText.slice(0, availableSpace);
73
127
  range.deleteContents();
74
- range.insertNode(document.createTextNode(cleanText));
75
-
76
- // Move cursor to end of inserted text
128
+ range.insertNode(document.createTextNode(truncatedText));
77
129
  range.collapse(false);
78
- selection.removeAllRanges();
79
- selection.addRange(range);
80
-
81
- // Update content
82
- this.content = event.target.innerText;
83
- this.$emit('update:lastCrumb', this.content);
130
+ this.lastValidContent = this.$refs.editableSpan.textContent;
131
+ this.$emit('update:lastCrumb', this.lastValidContent);
84
132
  }
85
133
  },
86
134
 
87
135
  handleEnterKey(event) {
88
136
  event.preventDefault();
89
137
  event.target.blur();
90
- },
91
-
92
-
93
- updateContent(event) {
94
- const newText = event.target.innerText;
95
-
96
- // If text exceeds maxChars, prevent the change
97
- 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
- event.target.innerText = this.content;
138
+ }
139
+ },
105
140
 
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);
112
- } else {
113
- this.content = newText;
114
- this.$emit('update:lastCrumb', this.content);
115
- }
141
+ watch: {
142
+ 'breadcrumb_computed': {
143
+ handler() {
144
+ this.$nextTick(this.updateContent);
145
+ },
146
+ deep: true
116
147
  }
117
148
  }
118
149
  }