Skip to content

Commit f117a1a

Browse files
committed
fix: 🐛 Avoid unnecessary code selection changes
1 parent 858097d commit f117a1a

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

webview/webview.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class WebVisualEditor {
22
codeEdits = [];
3+
eventFromEditor = false;
34
operation = '';
45
keyboard = {
56
// Combined state
@@ -241,7 +242,7 @@ class WebVisualEditor {
241242
}
242243

243244
// Select element
244-
select(element, emit = true) {
245+
select(element) {
245246
if (this.selected.has(element)) { return; }
246247
if (this.selected.values().some(s => s.contains(element) || element.contains(s))) {
247248
return;
@@ -259,15 +260,15 @@ class WebVisualEditor {
259260
}
260261
if (this.movers.size > 1) { this.toolbarGroupAlign.removeAttribute('disabled'); }
261262
}
262-
if (emit) { this.emitSelectionChange(); }
263+
if (!this.eventFromEditor) { this.emitSelectionChange(); }
263264
}
264265
// Deselect element
265266
deselect(element = null) {
266-
if (!element) {
267+
if (!this.eventFromEditor && !element) {
267268
this.selected.values().forEach(el => { this.deselect(el); });
268269
return;
269270
}
270-
if (!this.selected.has(element)) { return; }
271+
if (!this.selected.has(element) || this.eventFromEditor) { return; }
271272
if (this.codeEdits.some(edit => (
272273
edit.element !== element && (edit.element.contains(element) || element.contains(edit.element))
273274
))) {
@@ -658,6 +659,9 @@ document.addEventListener('DOMContentLoaded', async () => {
658659
case 'codeRanges':
659660
app.userElements.forEach((element, index) => {
660661
const { start, end } = data[index];
662+
if (!start || !end) {
663+
return;
664+
}
661665
element.setAttribute('data-wve-code-start', start);
662666
element.setAttribute('data-wve-code-end', end);
663667
});
@@ -673,8 +677,10 @@ document.addEventListener('DOMContentLoaded', async () => {
673677
return collected;
674678
}, []);
675679
if (selecting.length === 0) { return; }
680+
app.eventFromEditor = true;
676681
app.deselect();
677-
selecting.forEach(el => app.select(el, false));
682+
selecting.forEach(el => app.select(el));
683+
app.eventFromEditor = false;
678684
break;
679685
}
680686
});

0 commit comments

Comments
 (0)