Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inserting inline elements #2474

Merged
merged 2 commits into from
Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/lexical-link/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import type {
DOMConversionMap,
DOMConversionOutput,
EditorConfig,
GridSelection,
LexicalCommand,
LexicalNode,
NodeKey,
NodeSelection,
RangeSelection,
SerializedElementNode,
} from 'lexical';
Expand All @@ -21,6 +23,7 @@ import {addClassNamesToElement} from '@lexical/utils';
import {
$getSelection,
$isElementNode,
$isRangeSelection,
$setSelection,
createCommand,
ElementNode,
Expand Down Expand Up @@ -133,6 +136,25 @@ export class LinkNode extends ElementNode {
isInline(): true {
return true;
}

extractWithChild(
child: LexicalNode,
selection: RangeSelection | NodeSelection | GridSelection,
destination: 'clone' | 'html',
): boolean {
if (!$isRangeSelection(selection)) {
return false;
}

const anchorNode = selection.anchor.getNode();
const focusNode = selection.focus.getNode();

return (
this.isParentOf(anchorNode) &&
this.isParentOf(focusNode) &&
selection.getTextContent().length > 0
);
}
}

function convertAnchorElement(domNode: Node): DOMConversionOutput {
Expand Down
78 changes: 78 additions & 0 deletions packages/lexical-playground/__tests__/e2e/CopyAndPaste.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1809,4 +1809,82 @@ test.describe('CopyAndPaste', () => {
`,
);
});

test('HTML Copy + paste in front of or after a link', async ({
page,
isPlainText,
}) => {
test.skip(isPlainText);
await focusEditor(page);
await pasteFromClipboard(page, {
'text/html': `text<a href="https://test.com/1">link</a>text`,
});
await moveToEditorBeginning(page);
await pasteFromClipboard(page, {
'text/html': 'before',
});
await moveToEditorEnd(page);
await pasteFromClipboard(page, {
'text/html': 'after',
});
await assertHTML(
page,
html`
<p
class="PlaygroundEditorTheme__paragraph PlaygroundEditorTheme__ltr"
dir="ltr">
<span data-lexical-text="true">beforetext</span>
<a
href="https://test.com/1"
class="PlaygroundEditorTheme__link PlaygroundEditorTheme__ltr"
dir="ltr">
<span data-lexical-text="true">link</span>
</a>
<span data-lexical-text="true">textafter</span>
</p>
`,
);
});

test('HTML Copy + paste link by selecting its (partial) content', async ({
page,
isPlainText,
}) => {
test.skip(isPlainText);
await focusEditor(page);
await pasteFromClipboard(page, {
'text/html': `text<a href="https://test.com/">link</a>text`,
});
await moveLeft(page, 5);
await page.keyboard.down('Shift');
await moveLeft(page, 2);
await page.keyboard.up('Shift');
const clipboard = await copyToClipboard(page);
await moveToEditorEnd(page);
await pasteFromClipboard(page, clipboard);

await assertHTML(
page,
html`
<p
class="PlaygroundEditorTheme__paragraph PlaygroundEditorTheme__ltr"
dir="ltr">
<span data-lexical-text="true">text</span>
<a
href="https://test.com/"
class="PlaygroundEditorTheme__link PlaygroundEditorTheme__ltr"
dir="ltr">
<span data-lexical-text="true">link</span>
</a>
<span data-lexical-text="true">text</span>
<a
href="https://test.com/"
class="PlaygroundEditorTheme__link PlaygroundEditorTheme__ltr"
dir="ltr">
<span data-lexical-text="true">in</span>
</a>
</p>
`,
);
});
});
41 changes: 41 additions & 0 deletions packages/lexical-playground/__tests__/e2e/Selection.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
*/

import {
moveToLineBeginning,
pressShiftEnter,
} from '../keyboardShortcuts/index.mjs';
import {
assertHTML,
click,
evaluate,
expect,
focusEditor,
html,
initialize,
insertImageCaption,
insertSampleImage,
selectFromFormatDropdown,
sleep,
test,
} from '../utils/index.mjs';
Expand Down Expand Up @@ -94,4 +101,38 @@ test.describe('Selection', () => {
expect(await hasSelection('.image-caption-container')).toBe(true);
expect(await hasSelection('.editor-shell')).toBe(false);
});

test('can wrap post-linebreak nodes into new element', async ({
page,
isPlainText,
}) => {
test.skip(isPlainText);
await focusEditor(page);
await page.keyboard.type('Line1');
await pressShiftEnter(page);
await page.keyboard.type('Line2');
await page.keyboard.down('Shift');
await moveToLineBeginning(page);
await page.keyboard.up('Shift');
await selectFromFormatDropdown(page, '.code');
await assertHTML(
page,
html`
<p
class="PlaygroundEditorTheme__paragraph PlaygroundEditorTheme__ltr"
dir="ltr">
<span data-lexical-text="true">Line1</span>
<br />
<code
class="PlaygroundEditorTheme__code PlaygroundEditorTheme__ltr"
spellcheck="false"
dir="ltr"
data-highlight-language="javascript"
data-gutter="1">
<span data-lexical-text="true">Line2</span>
</code>
</p>
`,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,9 @@ export async function toggleItalic(page) {
await page.keyboard.press('i');
await keyUpCtrlOrMeta(page);
}

export async function pressShiftEnter(page) {
await page.keyboard.down('Shift');
await page.keyboard.press('Enter');
await page.keyboard.up('Shift');
}
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,6 @@ function BlockFormatDropDown({
} else {
const textContent = selection.getTextContent();
const codeNode = $createCodeNode();
selection.removeText();
selection.insertNodes([codeNode]);
selection.insertRawText(textContent);
}
Expand Down
20 changes: 16 additions & 4 deletions packages/lexical/src/LexicalSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,8 @@ export class RangeSelection implements BaseSelection {
} else if (
!$isElementNode(node) ||
($isElementNode(node) && node.isInline()) ||
($isDecoratorNode(target) && target.isTopLevel())
($isDecoratorNode(target) && target.isTopLevel()) ||
$isLineBreakNode(target)
) {
lastNodeInserted = node;
target = target.insertAfter(node);
Expand Down Expand Up @@ -1399,13 +1400,18 @@ export class RangeSelection implements BaseSelection {
}
}
if (siblings.length !== 0) {
const originalTarget = target;
for (let i = siblings.length - 1; i >= 0; i--) {
const sibling = siblings[i];
const prevParent = sibling.getParentOrThrow();
if ($isElementNode(target) && !$isElementNode(sibling)) {
target.append(sibling);
if ($isElementNode(target) && !$isBlockElementNode(sibling)) {
if (originalTarget === target) {
target.append(sibling);
} else {
target.insertBefore(sibling);
}
target = sibling;
} else if (!$isElementNode(target) && !$isElementNode(sibling)) {
} else if (!$isElementNode(target) && !$isBlockElementNode(sibling)) {
target.insertBefore(sibling);
target = sibling;
} else {
Expand Down Expand Up @@ -2176,6 +2182,12 @@ function internalResolveSelectionPoints(
return [resolvedAnchorPoint, resolvedFocusPoint];
}

function $isBlockElementNode(
node: LexicalNode | null | undefined,
): node is ElementNode {
return $isElementNode(node) && !node.isInline();
}

// This is used to make a selection when the existing
// selection is null, i.e. forcing selection on the editor
// when it current exists outside the editor.
Expand Down