41
41
@dragover =" onOver"
42
42
@dragleave =" onOut"
43
43
@drop =" onDrop"
44
+ @selectionchange =" onSelectionChange"
44
45
/>
45
46
</div >
46
47
</div >
@@ -89,20 +90,19 @@ export default {
89
90
emits: [" focus" , " input" , " submit" ],
90
91
data () {
91
92
return {
92
- over: false
93
+ over: false ,
94
+ selectionRange: null
93
95
};
94
96
},
95
97
computed: {
96
98
uploadOptions () {
97
- const selectionRange = this .selectionRange ();
98
-
99
99
return {
100
100
url: this .$panel .urls .api + " /" + this .endpoints .field + " /upload" ,
101
101
multiple: false ,
102
102
on: {
103
- cancel: async () => await this .setSelectionRange (selectionRange ),
103
+ cancel: async () => await this .restoreSelection ( ),
104
104
done: async (files ) => {
105
- await this .setSelectionRange (selectionRange );
105
+ await this .restoreSelection ( );
106
106
await this .insertUpload (files);
107
107
}
108
108
}
@@ -129,37 +129,33 @@ export default {
129
129
},
130
130
methods: {
131
131
dialog (dialog ) {
132
- const selectionRange = this .selectionRange ();
133
-
134
132
this .$panel .dialog .open ({
135
133
component: " k-toolbar-" + dialog + " -dialog" ,
136
134
props: {
137
135
value: this .parseSelection ()
138
136
},
139
137
on: {
140
- cancel: async () => await this .setSelectionRange (selectionRange ),
138
+ cancel: async () => await this .restoreSelection ( ),
141
139
submit: async (text ) => {
142
140
this .$panel .dialog .close ();
143
- await this .setSelectionRange (selectionRange );
141
+ await this .restoreSelection ( );
144
142
await this .insert (text);
145
143
}
146
144
}
147
145
});
148
146
},
149
147
file () {
150
- const selectionRange = this .selectionRange ();
151
-
152
148
this .$panel .dialog .open ({
153
149
component: " k-files-dialog" ,
154
150
props: {
155
151
endpoint: this .endpoints .field + " /files" ,
156
152
multiple: false
157
153
},
158
154
on: {
159
- cancel: async () => await this .setSelectionRange (selectionRange ),
155
+ cancel: async () => await this .restoreSelection ( ),
160
156
submit: async (file ) => {
161
157
this .$panel .dialog .close ();
162
- await this .setSelectionRange (selectionRange );
158
+ await this .restoreSelection ( );
163
159
await this .insertFile (file);
164
160
}
165
161
}
@@ -184,13 +180,12 @@ export default {
184
180
document .execCommand (" insertText" , false , text);
185
181
186
182
if (input .value === current) {
187
- const { start , end } = this .selectionRange ();
183
+ const { start , end } = this .selectionRange ;
184
+
188
185
const mode = start === end ? " end" : " select" ;
189
186
input .setRangeText (text, start, end, mode);
190
187
}
191
188
192
- await this .$nextTick ();
193
-
194
189
this .$emit (" input" , input .value );
195
190
196
191
return input .value ;
@@ -256,6 +251,12 @@ export default {
256
251
this .over = true ;
257
252
}
258
253
},
254
+ onSelectionChange () {
255
+ this .selectionRange = {
256
+ start: this .$refs .input .selectionStart ,
257
+ end: this .$refs .input .selectionEnd
258
+ };
259
+ },
259
260
onShortcut ($event ) {
260
261
if (
261
262
this .buttons !== false &&
@@ -302,17 +303,24 @@ export default {
302
303
async prepend (text ) {
303
304
return this .insert (text + " " + this .selection ());
304
305
},
306
+ async restoreSelection () {
307
+ if (this .selectionRange ) {
308
+ this .$refs .input .setSelectionRange (
309
+ this .selectionRange .start ,
310
+ this .selectionRange .end
311
+ );
312
+ }
313
+
314
+ await this .$nextTick ();
315
+ },
305
316
/**
306
- * @deprecated 5.0.0 Use `setSelectionRange ` instead
317
+ * @deprecated 5.0.0 Use `restoreSelection ` instead
307
318
*/
308
319
restoreSelectionCallback () {
309
- // store selection
310
- const selectionRange = this .selectionRange ();
311
-
312
320
// restore selection as `insert` method
313
321
// depends on it
314
322
return async (callback ) => {
315
- await this .setSelectionRange (selectionRange );
323
+ await this .restoreSelection ( );
316
324
317
325
if (callback) {
318
326
callback ();
@@ -323,19 +331,13 @@ export default {
323
331
this .$refs .select ();
324
332
},
325
333
selection () {
326
- const { start , end } = this .selectionRange ();
334
+ if (! this .selectionRange ) {
335
+ return " " ;
336
+ }
337
+
338
+ const { start , end } = this .selectionRange ;
327
339
return this .$refs .input .value .substring (start, end);
328
340
},
329
- selectionRange () {
330
- return {
331
- start: this .$refs .input .selectionStart ,
332
- end: this .$refs .input .selectionEnd
333
- };
334
- },
335
- async setSelectionRange ({ start, end }) {
336
- this .$refs .input .setSelectionRange (start, end);
337
- return this .$nextTick ();
338
- },
339
341
async toggle (before , after ) {
340
342
after ?? = before;
341
343
const selection = this .selection ();
0 commit comments