Repository navigation
[lexical-extension][lexical-playground] Feature: Insert paragraph on click after the last block - #8549
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| const editor = buildEditorFromExtensions({ | ||
| $initialEditorState: () => { | ||
| const root = $getRoot(); | ||
| // Block-level TestDecoratorNode at the end of the document, to | ||
| // exercise the same path Equation / PageBreak hit in the playground. | ||
| const decorator = $createTestDecoratorNode().setIsInline(false); | ||
| root.append( | ||
| $createParagraphNode().append($createTextNode('hello')), | ||
| decorator, | ||
| ); | ||
| }, | ||
| dependencies: [ | ||
| RichTextExtension, | ||
| configExtension(ClickAfterLastBlockExtension, { | ||
| disabled: options.disabled ?? false, | ||
| }), | ||
| ], | ||
| name: 'click-after-last-block-test', | ||
| nodes: [TestDecoratorNode], | ||
| }); | ||
| const root = document.createElement('div'); | ||
| document.body.appendChild(root); | ||
| editor.setRootElement(root); | ||
| return {editor, root}; |
There was a problem hiding this comment.
All of the try/finally stuff in these tests is kinda ugly, the easiest thing to clean that up would be to do this setup in register and only return the editor.
| const editor = buildEditorFromExtensions({ | |
| $initialEditorState: () => { | |
| const root = $getRoot(); | |
| // Block-level TestDecoratorNode at the end of the document, to | |
| // exercise the same path Equation / PageBreak hit in the playground. | |
| const decorator = $createTestDecoratorNode().setIsInline(false); | |
| root.append( | |
| $createParagraphNode().append($createTextNode('hello')), | |
| decorator, | |
| ); | |
| }, | |
| dependencies: [ | |
| RichTextExtension, | |
| configExtension(ClickAfterLastBlockExtension, { | |
| disabled: options.disabled ?? false, | |
| }), | |
| ], | |
| name: 'click-after-last-block-test', | |
| nodes: [TestDecoratorNode], | |
| }); | |
| const root = document.createElement('div'); | |
| document.body.appendChild(root); | |
| editor.setRootElement(root); | |
| return {editor, root}; | |
| return buildEditorFromExtensions({ | |
| $initialEditorState: () => { | |
| const root = $getRoot(); | |
| // Block-level TestDecoratorNode at the end of the document, to | |
| // exercise the same path Equation / PageBreak hit in the playground. | |
| const decorator = $createTestDecoratorNode().setIsInline(false); | |
| root.append( | |
| $createParagraphNode().append($createTextNode('hello')), | |
| decorator, | |
| ); | |
| }, | |
| dependencies: [ | |
| RichTextExtension, | |
| configExtension(ClickAfterLastBlockExtension, { | |
| disabled: options.disabled ?? false, | |
| }), | |
| ], | |
| name: 'click-after-last-block-test', | |
| nodes: [TestDecoratorNode], | |
| register() { | |
| const root = document.createElement('div'); | |
| document.body.appendChild(root); | |
| editor.setRootElement(root); | |
| return () => root.remove(); | |
| }, | |
| }); |
The call sites would use const root = editor.getRootElement(); (and assert it's non-null with ! or a proper assert) and using editor = setUpEditor();
There was a problem hiding this comment.
Done. Folded the two inline-buildEditorFromExtensions tests into setUpEditor too — only thing they varied was the initial state, exposed as a withInitialChildren option.
| function markLexicalHandled(event: Event): void { | ||
| (event as Event & LexicalHandledEvent)._lexicalHandled = true; |
There was a problem hiding this comment.
| function markLexicalHandled(event: Event): void { | |
| (event as Event & LexicalHandledEvent)._lexicalHandled = true; | |
| function markLexicalHandled(event: Event & LexicalHandledEvent): void { | |
| event._lexicalHandled = true; |
It would be better to just do an @internal export of stopLexicalPropagation though and use that. Will be easier to refactor later if we aren't directly manipulating untyped private API from here.
There was a problem hiding this comment.
Went with the @internal export — added stopLexicalPropagation to lexical core's index.ts and Lexical.js.flow, dropped the LexicalHandledEvent interface and markLexicalHandled wrapper from the extension.
| let result = false; | ||
| editor.getEditorState().read( | ||
| () => { | ||
| const lastChild = $getRoot().getLastChild(); | ||
| if (lastChild === null) { | ||
| return; | ||
| } | ||
| const lastChildDOM = editor.getElementByKey(lastChild.getKey()); | ||
| if (lastChildDOM === null) { | ||
| return; | ||
| } | ||
| // Exclusive lower edge — clicks at exactly the bottom pixel fall | ||
| // through to native handling, which is what users expect when | ||
| // they click on a block's visible bottom border. | ||
| if (event.clientY <= lastChildDOM.getBoundingClientRect().bottom) { | ||
| return; | ||
| } | ||
| if ($shouldInsertAfter(lastChild)) { | ||
| result = true; | ||
| } | ||
| }, | ||
| {editor}, | ||
| ); | ||
| return result; |
There was a problem hiding this comment.
| let result = false; | |
| editor.getEditorState().read( | |
| () => { | |
| const lastChild = $getRoot().getLastChild(); | |
| if (lastChild === null) { | |
| return; | |
| } | |
| const lastChildDOM = editor.getElementByKey(lastChild.getKey()); | |
| if (lastChildDOM === null) { | |
| return; | |
| } | |
| // Exclusive lower edge — clicks at exactly the bottom pixel fall | |
| // through to native handling, which is what users expect when | |
| // they click on a block's visible bottom border. | |
| if (event.clientY <= lastChildDOM.getBoundingClientRect().bottom) { | |
| return; | |
| } | |
| if ($shouldInsertAfter(lastChild)) { | |
| result = true; | |
| } | |
| }, | |
| {editor}, | |
| ); | |
| return result; | |
| return editor.getEditorState().read( | |
| () => { | |
| const lastChild = $getRoot().getLastChild(); | |
| if (lastChild === null) { | |
| return false; | |
| } | |
| const lastChildDOM = editor.getElementByKey(lastChild.getKey()); | |
| if (lastChildDOM === null) { | |
| return false; | |
| } | |
| // Exclusive lower edge — clicks at exactly the bottom pixel fall | |
| // through to native handling, which is what users expect when | |
| // they click on a block's visible bottom border. | |
| if (event.clientY <= lastChildDOM.getBoundingClientRect().bottom) { | |
| return false; | |
| } | |
| if ($shouldInsertAfter(lastChild)) { | |
| return true; | |
| } | |
| }, | |
| {editor}, | |
| ); |
There was a problem hiding this comment.
Done — shouldClaimClick returns the read result directly, and the trailing if ($shouldInsertAfter(lastChild)) { return true; } is now return $shouldInsertAfter(lastChild) so the function stays typed as boolean.
a521bd3 to
ccb78d3
Compare
| : () => { | ||
| $getRoot().clear(); | ||
| }, |
There was a problem hiding this comment.
| : () => { | |
| $getRoot().clear(); | |
| }, | |
| : () => {}, |
There's nothing here to clear in this case
…click after the last block Adds ClickAfterLastBlockExtension that intercepts clicks below the last child of the root and inserts a new empty paragraph when that child is a non-editable block. Defaults match DecoratorNode and shadow-root ElementNodes; the playground extends the predicate to include CodeNode so all three node types listed in meta#8544 are covered without pulling lexical-code-core into the core extension. Closes meta#8544
ccb78d3 to
973e46e
Compare
Description
Closes #8544. When a
DecoratorNode, a shadow-rootElementNodelikeTableNode, or aCodeNodesits at the end of the document, clicking the empty area below it currently puts the selection somewhere unexpected —nullfor a bare decorator, the end of a cell for a table, the end of the last line for a code block. Most editors (Notion, etc.) interpret that gesture as "make a paragraph here and let me type", and that's what this PR ships.ClickAfterLastBlockExtensionintercepts the click — both mousedown (to cancel the browser's native caret placement before it picks the previous text block) and click (to mark_lexicalHandledso lexical core skips its default onClick, then runeditor.updateto insert and select an empty paragraph). The two-phase shape was the only way to eliminate a one-frame cursor flicker on the previous text block; comments at the listener spot out the side effects to be aware of — focus transition ownership now lives with the extension, and lexical core'sisSelectionChangeFromMouseDownflag never gets set for these clicks.The trigger condition lives in a
$shouldInsertAfterconfig callback, defaulting to$defaultShouldInsertAfterwhich matchesDecoratorNodeand shadow-rootElementNode. The default is exported so consumers extending the behavior — the playground composes it with$isCodeNodeto cover the third type listed in the issue — don't re-derive the check.@lexical/code-coreis intentionally kept out of@lexical/extension's dependencies; the composition lives at the call site.Test plan
pnpm vitest run --project unit -t ClickAfterLastBlock— 5 unit tests: positive insertion, predicate-false skip,disabled: true, read-only mode, empty-root no-op.pnpm tsc --noEmit -p tsconfig.jsonclean.pnpm flowclean.prettier --check/eslintclean on changed files.