Skip to content

Commit bb504ff

Browse files
refact: Convert selectionRange into a data prop
1 parent c7ed2aa commit bb504ff

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

panel/src/components/Forms/Input/TextareaInput.vue

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
@dragover="onOver"
4242
@dragleave="onOut"
4343
@drop="onDrop"
44+
@selectionchange="onSelectionChange"
4445
/>
4546
</div>
4647
</div>
@@ -89,20 +90,19 @@ export default {
8990
emits: ["focus", "input", "submit"],
9091
data() {
9192
return {
92-
over: false
93+
over: false,
94+
selectionRange: null
9395
};
9496
},
9597
computed: {
9698
uploadOptions() {
97-
const selectionRange = this.selectionRange();
98-
9999
return {
100100
url: this.$panel.urls.api + "/" + this.endpoints.field + "/upload",
101101
multiple: false,
102102
on: {
103-
cancel: async () => await this.setSelectionRange(selectionRange),
103+
cancel: async () => await this.restoreSelection(),
104104
done: async (files) => {
105-
await this.setSelectionRange(selectionRange);
105+
await this.restoreSelection();
106106
await this.insertUpload(files);
107107
}
108108
}
@@ -129,37 +129,33 @@ export default {
129129
},
130130
methods: {
131131
dialog(dialog) {
132-
const selectionRange = this.selectionRange();
133-
134132
this.$panel.dialog.open({
135133
component: "k-toolbar-" + dialog + "-dialog",
136134
props: {
137135
value: this.parseSelection()
138136
},
139137
on: {
140-
cancel: async () => await this.setSelectionRange(selectionRange),
138+
cancel: async () => await this.restoreSelection(),
141139
submit: async (text) => {
142140
this.$panel.dialog.close();
143-
await this.setSelectionRange(selectionRange);
141+
await this.restoreSelection();
144142
await this.insert(text);
145143
}
146144
}
147145
});
148146
},
149147
file() {
150-
const selectionRange = this.selectionRange();
151-
152148
this.$panel.dialog.open({
153149
component: "k-files-dialog",
154150
props: {
155151
endpoint: this.endpoints.field + "/files",
156152
multiple: false
157153
},
158154
on: {
159-
cancel: async () => await this.setSelectionRange(selectionRange),
155+
cancel: async () => await this.restoreSelection(),
160156
submit: async (file) => {
161157
this.$panel.dialog.close();
162-
await this.setSelectionRange(selectionRange);
158+
await this.restoreSelection();
163159
await this.insertFile(file);
164160
}
165161
}
@@ -184,13 +180,12 @@ export default {
184180
document.execCommand("insertText", false, text);
185181
186182
if (input.value === current) {
187-
const { start, end } = this.selectionRange();
183+
const { start, end } = this.selectionRange;
184+
188185
const mode = start === end ? "end" : "select";
189186
input.setRangeText(text, start, end, mode);
190187
}
191188
192-
await this.$nextTick();
193-
194189
this.$emit("input", input.value);
195190
196191
return input.value;
@@ -256,6 +251,12 @@ export default {
256251
this.over = true;
257252
}
258253
},
254+
onSelectionChange() {
255+
this.selectionRange = {
256+
start: this.$refs.input.selectionStart,
257+
end: this.$refs.input.selectionEnd
258+
};
259+
},
259260
onShortcut($event) {
260261
if (
261262
this.buttons !== false &&
@@ -302,17 +303,24 @@ export default {
302303
async prepend(text) {
303304
return this.insert(text + " " + this.selection());
304305
},
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+
},
305316
/**
306-
* @deprecated 5.0.0 Use `setSelectionRange` instead
317+
* @deprecated 5.0.0 Use `restoreSelection` instead
307318
*/
308319
restoreSelectionCallback() {
309-
// store selection
310-
const selectionRange = this.selectionRange();
311-
312320
// restore selection as `insert` method
313321
// depends on it
314322
return async (callback) => {
315-
await this.setSelectionRange(selectionRange);
323+
await this.restoreSelection();
316324
317325
if (callback) {
318326
callback();
@@ -323,19 +331,13 @@ export default {
323331
this.$refs.select();
324332
},
325333
selection() {
326-
const { start, end } = this.selectionRange();
334+
if (!this.selectionRange) {
335+
return "";
336+
}
337+
338+
const { start, end } = this.selectionRange;
327339
return this.$refs.input.value.substring(start, end);
328340
},
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-
},
339341
async toggle(before, after) {
340342
after ??= before;
341343
const selection = this.selection();

0 commit comments

Comments
 (0)