@mixd-id/web-scaffold 0.1.230406332 → 0.1.230406334

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,7 +1,7 @@
1
1
  {
2
2
  "name": "@mixd-id/web-scaffold",
3
3
  "private": false,
4
- "version": "0.1.230406332",
4
+ "version": "0.1.230406334",
5
5
  "scripts": {
6
6
  "dev": "vite serve",
7
7
  "build": "vite build",
@@ -6,6 +6,8 @@
6
6
  ref="html"
7
7
  v-html="html"
8
8
  :class="itemClass"
9
+ @keydown="onKeyDown"
10
+ @blur="saveSelection($refs.html)"
9
11
  @input="onInput"></p>
10
12
 
11
13
  <button v-if="variant === 'minimal'" type="button" class="p-3" @click="$refs.modal.open()">
@@ -59,7 +61,8 @@
59
61
  </template>
60
62
 
61
63
  <script>
62
- import groupBy from "lodash/groupBy";
64
+
65
+ import {restoreSelection, saveSelection} from "../utils/selection";
63
66
 
64
67
  export default{
65
68
 
@@ -69,6 +72,8 @@ export default{
69
72
 
70
73
  addFn: Function,
71
74
 
75
+ itemFn: Function,
76
+
72
77
  modelValue: String,
73
78
 
74
79
  items: Array,
@@ -82,7 +87,12 @@ export default{
82
87
  computed:{
83
88
 
84
89
  groupedItems(){
85
- return groupBy(this.items ?? [], 'group')
90
+ return this.items.reduce((acc, item) => {
91
+ const key = item.group ?? ''
92
+ if(!acc[key]) acc[key] = []
93
+ acc[key].push(item)
94
+ return acc
95
+ }, {})
86
96
  },
87
97
 
88
98
  viewedItems(){
@@ -99,6 +109,8 @@ export default{
99
109
  },
100
110
 
101
111
  methods: {
112
+ restoreSelection,
113
+ saveSelection,
102
114
 
103
115
  insertElement(newNode){
104
116
  let selection = window.getSelection();
@@ -123,12 +135,10 @@ export default{
123
135
  selection.addRange(range);
124
136
  },
125
137
 
126
- add(item){
127
- this.$refs.modal.close()
138
+ addItem(item){
139
+ this.restoreSelection()
128
140
 
129
- if(typeof this.addFn === 'function'){
130
- item = this.addFn(item)
131
- }
141
+ console.log(item)
132
142
 
133
143
  const el = document.createElement('span')
134
144
  el.setAttribute('contenteditable', 'false')
@@ -140,6 +150,42 @@ export default{
140
150
  this.onInput()
141
151
  },
142
152
 
153
+ add(item){
154
+ this.$refs.modal.close()
155
+
156
+ if(typeof this.addFn === 'function'){
157
+
158
+ if(this.addFn.constructor.name === 'AsyncFunction'){
159
+ this.addFn(item).then(newItem => {
160
+ if(newItem === false) return
161
+ item = newItem ? newItem : item
162
+ this.addItem(item)
163
+ })
164
+ return
165
+ }
166
+
167
+ const newItem = this.addFn(item)
168
+ if(newItem === false) return
169
+ item = newItem ? newItem : item
170
+ }
171
+
172
+ this.addItem(item)
173
+ },
174
+
175
+ onKeyDown(e){
176
+ if (e.key === 'Enter') {
177
+ e.preventDefault();
178
+ document.execCommand('insertLineBreak');
179
+
180
+ const selection = window.getSelection();
181
+ const range = selection.getRangeAt(0);
182
+ range.setStartAfter(range.endContainer);
183
+ range.collapse(true);
184
+ selection.removeAllRanges();
185
+ selection.addRange(range);
186
+ }
187
+ },
188
+
143
189
  remove(e){
144
190
  e.target.parentNode.removeChild(e.target)
145
191
  },
@@ -148,14 +194,11 @@ export default{
148
194
 
149
195
  const arr = []
150
196
  for(let i = 0 ; i < this.$refs.html.childNodes.length ; i++){
151
- if(this.$refs.html.childNodes[i].nodeType === 1 && this.$refs.html.childNodes[i].classList.contains(this.$style.tag)){
152
- arr.push(this.$refs.html.childNodes[i].getAttribute('data-value'))
153
- }
154
- else if(this.$refs.html.childNodes[i].nodeType === 3){
155
- arr.push(this.$refs.html.childNodes[i].textContent)
197
+ if(this.$refs.html.childNodes[i].nodeType === 3){
198
+ arr.push(this.$refs.html.childNodes[i].nodeValue)
156
199
  }
157
- else{
158
- arr.push((arr.length > 0 ? "\n\n" : '') + this.$refs.html.childNodes[i].innerText)
200
+ else if(this.$refs.html.childNodes[i].nodeType === 1 && this.$refs.html.childNodes[i].classList.contains(this.$style.tag)){
201
+ arr.push(this.$refs.html.childNodes[i].getAttribute('data-value'))
159
202
  }
160
203
  }
161
204
 
@@ -179,10 +222,19 @@ export default{
179
222
  }
180
223
 
181
224
  let html = val ?? ''
182
- if(Array.isArray(this.items)){
183
- for(let item of this.items){
184
- html = html.replaceAll(item.value, `<span class="${this.$style.tag}" contenteditable="false" @click="remove" data-value="${item.value}">${item.text ?? item.value}</span>`, 'gi')
185
- }
225
+
226
+ let customItems = []
227
+ if(typeof this.itemFn === 'function'){
228
+ customItems = this.itemFn(html)
229
+ }
230
+
231
+ const items = [
232
+ ...Array.isArray(this.items) ? this.items : [],
233
+ ...customItems
234
+ ]
235
+
236
+ for(let item of items){
237
+ html = html.replaceAll(item.value, `<span title="${item.value.replaceAll('"', '')}" class="${this.$style.tag}" contenteditable="false" data-value="${item.value}">${item.text ?? item.value}</span>`, 'gi')
186
238
  }
187
239
 
188
240
  this.html = html