+/**
+ * Insert a new node after a details node, if inside a details node that's
+ * the last element, and if the cursor is at the last block within the details node.
+ */
+function insertAfterDetails(editor: LexicalEditor, event: KeyboardEvent|null): boolean {
+ const scenario = getDetailsScenario(editor);
+ if (scenario === null || scenario.detailsSibling) {
+ return false;
+ }
+
+ editor.update(() => {
+ const newParagraph = $createParagraphNode();
+ scenario.parentDetails.insertAfter(newParagraph);
+ newParagraph.select();
+ });
+ event?.preventDefault();
+
+ return true;
+}
+
+/**
+ * If within a details block, move after it, creating a new node if required, if we're on
+ * the last empty block element within the details node.
+ */
+function moveAfterDetailsOnEmptyLine(editor: LexicalEditor, event: KeyboardEvent|null): boolean {
+ const scenario = getDetailsScenario(editor);
+ if (scenario === null) {
+ return false;
+ }
+
+ if (scenario.parentBlock.getTextContent() !== '') {
+ return false;
+ }
+
+ event?.preventDefault()
+
+ const nextSibling = scenario.parentDetails.getNextSibling();
+ editor.update(() => {
+ if (nextSibling) {
+ nextSibling.selectStart();
+ } else {
+ const newParagraph = $createParagraphNode();
+ scenario.parentDetails.insertAfter(newParagraph);
+ newParagraph.select();
+ }
+ scenario.parentBlock.remove();
+ });
+
+ return true;
+}
+
+/**
+ * Get the common nodes used for a details node scenario, relative to current selection.
+ * Returns null if not found, or if the parent block is not the last in the parent details node.
+ */
+function getDetailsScenario(editor: LexicalEditor): {
+ parentDetails: DetailsNode;
+ parentBlock: LexicalNode;
+ detailsSibling: LexicalNode | null
+} | null {
+ const selection = getLastSelection(editor);
+ const firstNode = selection?.getNodes()[0];
+ if (!firstNode) {
+ return null;
+ }
+
+ const block = $getNearestNodeBlockParent(firstNode);
+ const details = $getParentOfType(firstNode, $isDetailsNode);
+ if (!$isDetailsNode(details) || block === null) {
+ return null;
+ }
+
+ if (block.getKey() !== details.getLastChild()?.getKey()) {
+ return null;
+ }
+
+ const nextSibling = details.getNextSibling();
+ return {
+ parentDetails: details,
+ parentBlock: block,
+ detailsSibling: nextSibling,
+ }
+}
+
+/**
+ * Inset the nodes within selection when a range of nodes is selected
+ * or if a list node is selected.
+ */