Skip to content

Commit 849bc4d

Browse files
committed
Lexical: Improved nested details interaction
- Set to open by default on insert. - Updated selection handling not to always fully cascade to lowest editable child on selection, so parents can be reliably selected. - Updated mouse handling to treat details panes like the root element, inserting within-details where relevant.
1 parent ee994fa commit 849bc4d

File tree

8 files changed

+96
-23
lines changed

8 files changed

+96
-23
lines changed

resources/js/wysiwyg/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {createEditor, LexicalEditor} from 'lexical';
1+
import {createEditor} from 'lexical';
22
import {createEmptyHistoryState, registerHistory} from '@lexical/history';
33
import {registerRichText} from '@lexical/rich-text';
44
import {mergeRegister} from '@lexical/utils';
@@ -89,6 +89,9 @@ export function createPageEditorInstance(container: HTMLElement, htmlContent: st
8989
window.debugEditorState = () => {
9090
return editor.getEditorState().toJSON();
9191
};
92+
context.manager.onSelectionChange((selection) => {
93+
console.log(selection, context.editor.getEditorState());
94+
});
9295

9396
registerCommonNodeMutationListeners(context);
9497

resources/js/wysiwyg/lexical/core/LexicalNode.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,14 @@ export class LexicalNode {
383383
return isSelected;
384384
}
385385

386+
/**
387+
* Indicate if this node should be selected directly instead of the default
388+
* where the selection would descend to the nearest initial child element.
389+
*/
390+
shouldSelectDirectly(): boolean {
391+
return false;
392+
}
393+
386394
/**
387395
* Returns this nodes key.
388396
*/

resources/js/wysiwyg/lexical/core/LexicalSelection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,12 @@ export class RangeSelection implements BaseSelection {
476476
const startOffset = firstPoint.offset;
477477
const endOffset = lastPoint.offset;
478478

479-
if ($isElementNode(firstNode)) {
479+
if ($isElementNode(firstNode) && !firstNode.shouldSelectDirectly()) {
480480
const firstNodeDescendant =
481481
firstNode.getDescendantByIndex<ElementNode>(startOffset);
482482
firstNode = firstNodeDescendant != null ? firstNodeDescendant : firstNode;
483483
}
484-
if ($isElementNode(lastNode)) {
484+
if ($isElementNode(lastNode) && !lastNode.shouldSelectDirectly()) {
485485
let lastNodeDescendant =
486486
lastNode.getDescendantByIndex<ElementNode>(endOffset);
487487
// We don't want to over-select, as node selection infers the child before
@@ -499,7 +499,7 @@ export class RangeSelection implements BaseSelection {
499499
let nodes: Array<LexicalNode>;
500500

501501
if (firstNode.is(lastNode)) {
502-
if ($isElementNode(firstNode) && firstNode.getChildrenSize() > 0) {
502+
if ($isElementNode(firstNode) && firstNode.getChildrenSize() > 0 && !firstNode.shouldSelectDirectly()) {
503503
nodes = [];
504504
} else {
505505
nodes = [firstNode];

resources/js/wysiwyg/lexical/core/nodes/LexicalElementNode.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,20 @@ export class ElementNode extends LexicalNode {
150150
}
151151
return node;
152152
}
153+
getFirstSelectableDescendant<T extends LexicalNode>(): null | T {
154+
if (this.shouldSelectDirectly()) {
155+
return null;
156+
}
157+
let node = this.getFirstChild<T>();
158+
while ($isElementNode(node) && !node.shouldSelectDirectly()) {
159+
const child = node.getFirstChild<T>();
160+
if (child === null) {
161+
break;
162+
}
163+
node = child;
164+
}
165+
return node;
166+
}
153167
getLastDescendant<T extends LexicalNode>(): null | T {
154168
let node = this.getLastChild<T>();
155169
while ($isElementNode(node)) {
@@ -161,6 +175,20 @@ export class ElementNode extends LexicalNode {
161175
}
162176
return node;
163177
}
178+
getLastSelectableDescendant<T extends LexicalNode>(): null | T {
179+
if (this.shouldSelectDirectly()) {
180+
return null;
181+
}
182+
let node = this.getLastChild<T>();
183+
while ($isElementNode(node) && !node.shouldSelectDirectly()) {
184+
const child = node.getLastChild<T>();
185+
if (child === null) {
186+
break;
187+
}
188+
node = child;
189+
}
190+
return node;
191+
}
164192
getDescendantByIndex<T extends LexicalNode>(index: number): null | T {
165193
const children = this.getChildren<T>();
166194
const childrenLength = children.length;
@@ -319,11 +347,11 @@ export class ElementNode extends LexicalNode {
319347
return selection;
320348
}
321349
selectStart(): RangeSelection {
322-
const firstNode = this.getFirstDescendant();
350+
const firstNode = this.getFirstSelectableDescendant();
323351
return firstNode ? firstNode.selectStart() : this.select();
324352
}
325353
selectEnd(): RangeSelection {
326-
const lastNode = this.getLastDescendant();
354+
const lastNode = this.getLastSelectableDescendant();
327355
return lastNode ? lastNode.selectEnd() : this.select();
328356
}
329357
clear(): this {

resources/js/wysiwyg/lexical/rich-text/LexicalDetailsNode.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ export class DetailsNode extends ElementNode {
7575

7676
if (this.__open) {
7777
el.setAttribute('open', 'true');
78+
el.removeAttribute('contenteditable');
79+
} else {
80+
el.setAttribute('contenteditable', 'false');
7881
}
7982

8083
const summary = document.createElement('summary');
@@ -84,7 +87,7 @@ export class DetailsNode extends ElementNode {
8487
event.preventDefault();
8588
_editor.update(() => {
8689
this.select();
87-
})
90+
});
8891
});
8992

9093
el.append(summary);
@@ -96,6 +99,11 @@ export class DetailsNode extends ElementNode {
9699

97100
if (prevNode.__open !== this.__open) {
98101
dom.toggleAttribute('open', this.__open);
102+
if (this.__open) {
103+
dom.removeAttribute('contenteditable');
104+
} else {
105+
dom.setAttribute('contenteditable', 'false');
106+
}
99107
}
100108

101109
return prevNode.__id !== this.__id
@@ -144,6 +152,7 @@ export class DetailsNode extends ElementNode {
144152
}
145153

146154
element.removeAttribute('open');
155+
element.removeAttribute('contenteditable');
147156

148157
return {element};
149158
}
@@ -165,6 +174,10 @@ export class DetailsNode extends ElementNode {
165174
return node;
166175
}
167176

177+
shouldSelectDirectly(): boolean {
178+
return true;
179+
}
180+
168181
}
169182

170183
export function $createDetailsNode() {

resources/js/wysiwyg/services/mouse-handling.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
11
import {EditorUiContext} from "../ui/framework/core";
22
import {
3-
$createParagraphNode, $getRoot,
4-
$getSelection,
3+
$createParagraphNode, $getNearestNodeFromDOMNode, $getRoot,
54
$isDecoratorNode, CLICK_COMMAND,
6-
COMMAND_PRIORITY_LOW, KEY_ARROW_DOWN_COMMAND, KEY_ARROW_UP_COMMAND,
7-
KEY_BACKSPACE_COMMAND,
8-
KEY_DELETE_COMMAND,
9-
KEY_ENTER_COMMAND, KEY_TAB_COMMAND,
10-
LexicalEditor,
5+
COMMAND_PRIORITY_LOW, ElementNode,
116
LexicalNode
127
} from "lexical";
138
import {$isImageNode} from "@lexical/rich-text/LexicalImageNode";
149
import {$isMediaNode} from "@lexical/rich-text/LexicalMediaNode";
15-
import {getLastSelection} from "../utils/selection";
16-
import {$getNearestNodeBlockParent, $getParentOfType, $selectOrCreateAdjacent} from "../utils/nodes";
17-
import {$setInsetForSelection} from "../utils/lists";
18-
import {$isListItemNode} from "@lexical/list";
19-
import {$isDetailsNode, DetailsNode} from "@lexical/rich-text/LexicalDetailsNode";
2010
import {$isDiagramNode} from "../utils/diagrams";
2111
import {$isTableNode} from "@lexical/table";
12+
import {$isDetailsNode} from "@lexical/rich-text/LexicalDetailsNode";
2213

2314
function isHardToEscapeNode(node: LexicalNode): boolean {
24-
return $isDecoratorNode(node) || $isImageNode(node) || $isMediaNode(node) || $isDiagramNode(node) || $isTableNode(node);
15+
return $isDecoratorNode(node)
16+
|| $isImageNode(node)
17+
|| $isMediaNode(node)
18+
|| $isDiagramNode(node)
19+
|| $isTableNode(node)
20+
|| $isDetailsNode(node);
21+
}
22+
23+
function $getContextNode(event: MouseEvent): ElementNode {
24+
if (event.target instanceof HTMLElement) {
25+
const nearestDetails = event.target.closest('details');
26+
if (nearestDetails) {
27+
const detailsNode = $getNearestNodeFromDOMNode(nearestDetails);
28+
if ($isDetailsNode(detailsNode)) {
29+
return detailsNode;
30+
}
31+
}
32+
}
33+
return $getRoot();
2534
}
2635

2736
function insertBelowLastNode(context: EditorUiContext, event: MouseEvent): boolean {
28-
const lastNode = $getRoot().getLastChild();
37+
const contextNode = $getContextNode(event);
38+
const lastNode = contextNode.getLastChild();
2939
if (!lastNode || !isHardToEscapeNode(lastNode)) {
3040
return false;
3141
}
@@ -40,7 +50,7 @@ function insertBelowLastNode(context: EditorUiContext, event: MouseEvent): boole
4050
if (isClickBelow) {
4151
context.editor.update(() => {
4252
const newNode = $createParagraphNode();
43-
$getRoot().append(newNode);
53+
contextNode.append(newNode);
4454
newNode.select();
4555
});
4656
return true;
@@ -49,7 +59,6 @@ function insertBelowLastNode(context: EditorUiContext, event: MouseEvent): boole
4959
return false;
5060
}
5161

52-
5362
export function registerMouseHandling(context: EditorUiContext): () => void {
5463
const unregisterClick = context.editor.registerCommand(CLICK_COMMAND, (event): boolean => {
5564
insertBelowLastNode(context, event);

resources/js/wysiwyg/ui/defaults/buttons/objects.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ export const details: EditorButtonDefinition = {
193193
.filter(n => n !== null) as ElementNode[];
194194
const uniqueTopLevels = [...new Set(topLevels)];
195195

196+
detailsNode.setOpen(true);
197+
196198
if (uniqueTopLevels.length > 0) {
197199
uniqueTopLevels[0].insertAfter(detailsNode);
198200
} else {

resources/sass/_editor.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,16 @@ body.editor-is-fullscreen {
437437
.editor-node-resizer.active .editor-node-resizer-ghost {
438438
display: block;
439439
}
440+
.editor-content-area details[contenteditable="false"],
441+
.editor-content-area summary[contenteditable="false"] {
442+
user-select: none;
443+
}
444+
.editor-content-area details[contenteditable="false"] > details * {
445+
pointer-events: none;
446+
}
447+
.editor-content-area details summary {
448+
caret-color: transparent;
449+
}
440450

441451
.editor-table-marker {
442452
position: fixed;

0 commit comments

Comments
 (0)