@mixd-id/web-scaffold 0.1.230406332 → 0.1.230406333

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.230406333",
5
5
  "scripts": {
6
6
  "dev": "vite serve",
7
7
  "build": "vite build",
@@ -6,6 +6,7 @@
6
6
  ref="html"
7
7
  v-html="html"
8
8
  :class="itemClass"
9
+ @blur="saveSelection($refs.html)"
9
10
  @input="onInput"></p>
10
11
 
11
12
  <button v-if="variant === 'minimal'" type="button" class="p-3" @click="$refs.modal.open()">
@@ -59,7 +60,8 @@
59
60
  </template>
60
61
 
61
62
  <script>
62
- import groupBy from "lodash/groupBy";
63
+
64
+ import {restoreSelection, saveSelection} from "../utils/selection";
63
65
 
64
66
  export default{
65
67
 
@@ -69,6 +71,8 @@ export default{
69
71
 
70
72
  addFn: Function,
71
73
 
74
+ itemFn: Function,
75
+
72
76
  modelValue: String,
73
77
 
74
78
  items: Array,
@@ -82,7 +86,12 @@ export default{
82
86
  computed:{
83
87
 
84
88
  groupedItems(){
85
- return groupBy(this.items ?? [], 'group')
89
+ return this.items.reduce((acc, item) => {
90
+ const key = item.group ?? ''
91
+ if(!acc[key]) acc[key] = []
92
+ acc[key].push(item)
93
+ return acc
94
+ }, {})
86
95
  },
87
96
 
88
97
  viewedItems(){
@@ -99,6 +108,8 @@ export default{
99
108
  },
100
109
 
101
110
  methods: {
111
+ restoreSelection,
112
+ saveSelection,
102
113
 
103
114
  insertElement(newNode){
104
115
  let selection = window.getSelection();
@@ -123,12 +134,8 @@ export default{
123
134
  selection.addRange(range);
124
135
  },
125
136
 
126
- add(item){
127
- this.$refs.modal.close()
128
-
129
- if(typeof this.addFn === 'function'){
130
- item = this.addFn(item)
131
- }
137
+ addItem(item){
138
+ this.restoreSelection()
132
139
 
133
140
  const el = document.createElement('span')
134
141
  el.setAttribute('contenteditable', 'false')
@@ -140,6 +147,28 @@ export default{
140
147
  this.onInput()
141
148
  },
142
149
 
150
+ add(item){
151
+ this.$refs.modal.close()
152
+
153
+ if(typeof this.addFn === 'function'){
154
+
155
+ if(this.addFn.constructor.name === 'AsyncFunction'){
156
+ this.addFn(item).then(newItem => {
157
+ if(newItem === false) return
158
+ item = newItem ? newItem : item
159
+ this.addItem(item)
160
+ })
161
+ return
162
+ }
163
+
164
+ const newItem = this.addFn(item)
165
+ if(newItem === false) return
166
+ item = newItem ? newItem : item
167
+ }
168
+
169
+ this.addItem(item)
170
+ },
171
+
143
172
  remove(e){
144
173
  e.target.parentNode.removeChild(e.target)
145
174
  },
@@ -179,10 +208,19 @@ export default{
179
208
  }
180
209
 
181
210
  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
- }
211
+
212
+ let customItems = []
213
+ if(typeof this.itemFn === 'function'){
214
+ customItems = this.itemFn(html)
215
+ }
216
+
217
+ const items = [
218
+ ...Array.isArray(this.items) ? this.items : [],
219
+ ...customItems
220
+ ]
221
+
222
+ for(let item of items){
223
+ 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
224
  }
187
225
 
188
226
  this.html = html