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

fix: end-of-line cursor issue in Safari #6282

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
47 changes: 36 additions & 11 deletions packages/lexical/src/LexicalReconciler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
import type {NodeKey, NodeMap} from './LexicalNode';
import type {ElementNode} from './nodes/LexicalElementNode';

import {IS_IOS, IS_SAFARI} from 'shared/environment';
import invariant from 'shared/invariant';
import normalizeClassNames from 'shared/normalizeClassNames';

Expand Down Expand Up @@ -234,8 +235,9 @@ function $createNode(
if (insertDOM != null) {
parentDOM.insertBefore(dom, insertDOM);
} else {
// @ts-expect-error: internal field
const possibleLineBreak = parentDOM.__lexicalLineBreak;
const possibleLineBreak =
// @ts-expect-error: internal field
parentDOM.__lexicalLineBreakImg || parentDOM.__lexicalLineBreak;

if (possibleLineBreak != null) {
parentDOM.insertBefore(dom, possibleLineBreak);
Expand Down Expand Up @@ -330,24 +332,47 @@ function reconcileElementTerminatingLineBreak(

if (prevLineBreak) {
if (!nextLineBreak) {
// @ts-expect-error: internal field
const element = dom.__lexicalLineBreak;

if (element != null) {
dom.removeChild(element);
}

// @ts-expect-error: internal field
dom.__lexicalLineBreak = null;
const removeElement = (fieldName: string) => {
const element = dom[fieldName];
if (element != null) {
dom.removeChild(element);
dom[fieldName] = null;
}
};
removeElement('__lexicalLineBreak');
removeElement('__lexicalLineBreakImg');
}
} else if (nextLineBreak) {
const element = document.createElement('br');
// Workaround for a bug in Safari where the cursor cannot be placed at the
// end of a line.
if (IS_SAFARI || IS_IOS) {
insertCursorFixElement(dom, element);
}
// @ts-expect-error: internal field
dom.__lexicalLineBreak = element;
dom.appendChild(element);
}
}

function insertCursorFixElement(
dom: HTMLElement,
lineBreakElement: HTMLBRElement,
): void {
const element = document.createElement('img');
element.alt = '';

// prevent conflict with other css rules
const styles = element.style;
styles.setProperty('display', 'inline', 'important');
styles.setProperty('border', 'none', 'important');
styles.setProperty('margin', '0', 'important');

dom.appendChild(element);
// @ts-expect-error: internal field
dom.__lexicalLineBreakImg = element;
}

function reconcileParagraphFormat(element: ElementNode): void {
if (
$isParagraphNode(element) &&
Expand Down
Loading