Skip to content

Commit 2e12227

Browse files
committed
Don't memoize node selection to deal with Lexical freezing selected nodes in development
#540 #507 (comment)
1 parent 62ebdb5 commit 2e12227

File tree

8 files changed

+47
-65
lines changed

8 files changed

+47
-65
lines changed

app/assets/javascript/lexxy.js

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7856,26 +7856,19 @@ class Selection {
78567856
this.#containEditorFocus();
78577857
}
78587858

7859-
clear() {
7860-
this.current = null;
7861-
}
7862-
78637859
set current(selection) {
7864-
if (xr(selection)) {
7865-
this.editor.getEditorState().read(() => {
7866-
this._current = Lr();
7867-
this.#syncSelectedClasses();
7868-
});
7869-
} else {
7870-
this.editor.update(() => {
7871-
this.#syncSelectedClasses();
7872-
this._current = null;
7873-
});
7874-
}
7860+
this.editor.update(() => {
7861+
this.#syncSelectedClasses();
7862+
});
78757863
}
78767864

7877-
get current() {
7878-
return this._current
7865+
get hasNodeSelection() {
7866+
let result = false;
7867+
this.editor.getEditorState().read(() => {
7868+
const selection = Lr();
7869+
result = selection !== null && xr(selection);
7870+
});
7871+
return result
78797872
}
78807873

78817874
get cursorPosition() {
@@ -8068,18 +8061,18 @@ class Selection {
80688061
}
80698062

80708063
get #currentlySelectedKeys() {
8071-
if (this._currentlySelectedKeys) { return this._currentlySelectedKeys }
8064+
if (this.currentlySelectedKeys) { return this.currentlySelectedKeys }
80728065

8073-
this._currentlySelectedKeys = new Set();
8066+
this.currentlySelectedKeys = new Set();
80748067

80758068
const selection = Lr();
80768069
if (selection && xr(selection)) {
80778070
for (const node of selection.getNodes()) {
8078-
this._currentlySelectedKeys.add(node.getKey());
8071+
this.currentlySelectedKeys.add(node.getKey());
80798072
}
80808073
}
80818074

8082-
return this._currentlySelectedKeys
8075+
return this.currentlySelectedKeys
80838076
}
80848077

80858078
#processSelectionChangeCommands() {
@@ -8209,7 +8202,7 @@ class Selection {
82098202
this.#highlightNewItems();
82108203

82118204
this.previouslySelectedKeys = this.#currentlySelectedKeys;
8212-
this._currentlySelectedKeys = null;
8205+
this.currentlySelectedKeys = null;
82138206
}
82148207

82158208
#clearPreviouslyHighlightedItems() {
@@ -8231,31 +8224,31 @@ class Selection {
82318224
}
82328225

82338226
async #selectPreviousNode() {
8234-
if (this.current) {
8227+
if (this.hasNodeSelection) {
82358228
await this.#withCurrentNode((currentNode) => currentNode.selectPrevious());
82368229
} else {
82378230
this.#selectInLexical(this.nodeBeforeCursor);
82388231
}
82398232
}
82408233

82418234
async #selectNextNode() {
8242-
if (this.current) {
8235+
if (this.hasNodeSelection) {
82438236
await this.#withCurrentNode((currentNode) => currentNode.selectNext(0, 0));
82448237
} else {
82458238
this.#selectInLexical(this.nodeAfterCursor);
82468239
}
82478240
}
82488241

82498242
async #selectPreviousTopLevelNode() {
8250-
if (this.current) {
8243+
if (this.hasNodeSelection) {
82518244
await this.#withCurrentNode((currentNode) => currentNode.selectPrevious());
82528245
} else {
82538246
this.#selectInLexical(this.topLevelNodeBeforeCursor);
82548247
}
82558248
}
82568249

82578250
async #selectNextTopLevelNode() {
8258-
if (this.current) {
8251+
if (this.hasNodeSelection) {
82598252
await this.#withCurrentNode((currentNode) => currentNode.selectNext(0, 0));
82608253
} else {
82618254
this.#selectInLexical(this.topLevelNodeAfterCursor);
@@ -8264,10 +8257,9 @@ class Selection {
82648257

82658258
async #withCurrentNode(fn) {
82668259
await nextFrame();
8267-
if (this.current) {
8260+
if (this.hasNodeSelection) {
82688261
this.editor.update(() => {
8269-
this.clear();
8270-
fn(this.current.getNodes()[0]);
8262+
fn(Lr().getNodes()[0]);
82718263
this.editor.focus();
82728264
});
82738265
}
@@ -9171,8 +9163,8 @@ class Contents {
91719163
let focusNode = null;
91729164

91739165
this.editor.update(() => {
9174-
if (xr(this.#selection.current)) {
9175-
const nodesToRemove = this.#selection.current.getNodes();
9166+
if (xr(Lr())) {
9167+
const nodesToRemove = Lr().getNodes();
91769168
if (nodesToRemove.length === 0) return
91779169

91789170
focusNode = this.#findAdjacentNodeTo(nodesToRemove);
@@ -9184,7 +9176,6 @@ class Contents {
91849176

91859177
this.editor.update(() => {
91869178
this.#selectAfterDeletion(focusNode);
9187-
this.#selection.clear();
91889179
this.editor.focus();
91899180
});
91909181
}

app/assets/javascript/lexxy.js.br

76 Bytes
Binary file not shown.

app/assets/javascript/lexxy.js.gz

-31 Bytes
Binary file not shown.

app/assets/javascript/lexxy.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
-77 Bytes
Binary file not shown.
-27 Bytes
Binary file not shown.

src/editor/contents.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ export default class Contents {
278278
let focusNode = null
279279

280280
this.editor.update(() => {
281-
if ($isNodeSelection(this.#selection.current)) {
282-
const nodesToRemove = this.#selection.current.getNodes()
281+
if ($isNodeSelection($getSelection())) {
282+
const nodesToRemove = $getSelection().getNodes()
283283
if (nodesToRemove.length === 0) return
284284

285285
focusNode = this.#findAdjacentNodeTo(nodesToRemove)
@@ -291,7 +291,6 @@ export default class Contents {
291291

292292
this.editor.update(() => {
293293
this.#selectAfterDeletion(focusNode)
294-
this.#selection.clear()
295294
this.editor.focus()
296295
})
297296
}

src/editor/selection.js

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,19 @@ export default class Selection {
2424
this.#containEditorFocus()
2525
}
2626

27-
clear() {
28-
this.current = null
29-
}
30-
3127
set current(selection) {
32-
if ($isNodeSelection(selection)) {
33-
this.editor.getEditorState().read(() => {
34-
this._current = $getSelection()
35-
this.#syncSelectedClasses()
36-
})
37-
} else {
38-
this.editor.update(() => {
39-
this.#syncSelectedClasses()
40-
this._current = null
41-
})
42-
}
28+
this.editor.update(() => {
29+
this.#syncSelectedClasses()
30+
})
4331
}
4432

45-
get current() {
46-
return this._current
33+
get hasNodeSelection() {
34+
let result = false
35+
this.editor.getEditorState().read(() => {
36+
const selection = $getSelection()
37+
result = selection !== null && $isNodeSelection(selection)
38+
})
39+
return result
4740
}
4841

4942
get cursorPosition() {
@@ -236,18 +229,18 @@ export default class Selection {
236229
}
237230

238231
get #currentlySelectedKeys() {
239-
if (this._currentlySelectedKeys) { return this._currentlySelectedKeys }
232+
if (this.currentlySelectedKeys) { return this.currentlySelectedKeys }
240233

241-
this._currentlySelectedKeys = new Set()
234+
this.currentlySelectedKeys = new Set()
242235

243236
const selection = $getSelection()
244237
if (selection && $isNodeSelection(selection)) {
245238
for (const node of selection.getNodes()) {
246-
this._currentlySelectedKeys.add(node.getKey())
239+
this.currentlySelectedKeys.add(node.getKey())
247240
}
248241
}
249242

250-
return this._currentlySelectedKeys
243+
return this.currentlySelectedKeys
251244
}
252245

253246
#processSelectionChangeCommands() {
@@ -377,7 +370,7 @@ export default class Selection {
377370
this.#highlightNewItems()
378371

379372
this.previouslySelectedKeys = this.#currentlySelectedKeys
380-
this._currentlySelectedKeys = null
373+
this.currentlySelectedKeys = null
381374
}
382375

383376
#clearPreviouslyHighlightedItems() {
@@ -399,31 +392,31 @@ export default class Selection {
399392
}
400393

401394
async #selectPreviousNode() {
402-
if (this.current) {
395+
if (this.hasNodeSelection) {
403396
await this.#withCurrentNode((currentNode) => currentNode.selectPrevious())
404397
} else {
405398
this.#selectInLexical(this.nodeBeforeCursor)
406399
}
407400
}
408401

409402
async #selectNextNode() {
410-
if (this.current) {
403+
if (this.hasNodeSelection) {
411404
await this.#withCurrentNode((currentNode) => currentNode.selectNext(0, 0))
412405
} else {
413406
this.#selectInLexical(this.nodeAfterCursor)
414407
}
415408
}
416409

417410
async #selectPreviousTopLevelNode() {
418-
if (this.current) {
411+
if (this.hasNodeSelection) {
419412
await this.#withCurrentNode((currentNode) => currentNode.selectPrevious())
420413
} else {
421414
this.#selectInLexical(this.topLevelNodeBeforeCursor)
422415
}
423416
}
424417

425418
async #selectNextTopLevelNode() {
426-
if (this.current) {
419+
if (this.hasNodeSelection) {
427420
await this.#withCurrentNode((currentNode) => currentNode.selectNext(0, 0))
428421
} else {
429422
this.#selectInLexical(this.topLevelNodeAfterCursor)
@@ -432,10 +425,9 @@ export default class Selection {
432425

433426
async #withCurrentNode(fn) {
434427
await nextFrame()
435-
if (this.current) {
428+
if (this.hasNodeSelection) {
436429
this.editor.update(() => {
437-
this.clear()
438-
fn(this.current.getNodes()[0])
430+
fn($getSelection().getNodes()[0])
439431
this.editor.focus()
440432
})
441433
}

0 commit comments

Comments
 (0)