{"version":3,"file":"enterKey.mjs","sources":["../../../../../../../../admin/src/pages/EditView/components/FormInputs/BlocksInput/utils/enterKey.ts"],"sourcesContent":["import { type Text, Editor, Node, Transforms } from 'slate';\n\nconst isText = (node: unknown): node is Text => {\n return Node.isNode(node) && !Editor.isEditor(node) && node.type === 'text';\n};\n\n/**\n * Inserts a line break the first time the user presses enter, and exits the node the second time.\n */\nconst pressEnterTwiceToExit = (editor: Editor) => {\n /**\n * To determine if we should break out of the node, check 2 things:\n * 1. If the cursor is at the end of the node\n * 2. If the last line of the node is empty\n */\n const nodeEntry = Editor.above(editor, {\n match: (node) => !Editor.isEditor(node) && !['link', 'text'].includes(node.type),\n });\n if (!nodeEntry || !editor.selection) {\n return;\n }\n const [node, nodePath] = nodeEntry;\n const isNodeEnd = Editor.isEnd(editor, editor.selection.anchor, nodePath);\n const lastTextNode = node.children.at(-1);\n const isEmptyLine = isText(lastTextNode) && lastTextNode.text.endsWith('\\n');\n\n if (isNodeEnd && isEmptyLine) {\n // Remove the last line break\n Transforms.delete(editor, { distance: 1, unit: 'character', reverse: true });\n // Break out of the node by creating a new paragraph\n Transforms.insertNodes(editor, {\n type: 'paragraph',\n children: [{ type: 'text', text: '' }],\n });\n return;\n }\n\n // Otherwise insert a new line within the node\n Transforms.insertText(editor, '\\n');\n\n // If there's nothing after the cursor, disable modifiers\n if (isNodeEnd) {\n ['bold', 'italic', 'underline', 'strikethrough', 'code'].forEach((modifier) => {\n Editor.removeMark(editor, modifier);\n });\n }\n};\n\nexport { pressEnterTwiceToExit };\n"],"names":["isText","node","Node","isNode","Editor","isEditor","type","pressEnterTwiceToExit","editor","nodeEntry","above","match","includes","selection","nodePath","isNodeEnd","isEnd","anchor","lastTextNode","children","at","isEmptyLine","text","endsWith","Transforms","delete","distance","unit","reverse","insertNodes","insertText","forEach","modifier","removeMark"],"mappings":";;AAEA,MAAMA,SAAS,CAACC,IAAAA,GAAAA;IACd,OAAOC,IAAAA,CAAKC,MAAM,CAACF,IAAAA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