Skip to content

Commit

Permalink
Remove collapseAtStart
Browse files Browse the repository at this point in the history
  • Loading branch information
zurfyx committed May 8, 2024
1 parent 5b20765 commit 2a42618
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 91 deletions.
1 change: 0 additions & 1 deletion packages/lexical-code/flow/LexicalCode.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ declare export class CodeNode extends ElementNode {
selection: RangeSelection,
restoreSelection?: boolean,
): null | ParagraphNode | CodeHighlightNode | TabNode;
collapseAtStart(): true;
setLanguage(language: string): void;
getLanguage(): string | void;
}
22 changes: 7 additions & 15 deletions packages/lexical-code/src/CodeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,25 @@ export class CodeNode extends ElementNode {

return isMultiLine
? {
conversion: convertPreElement,
conversion: $convertPreElement,
priority: 1,
}
: null;
},
div: (node: Node) => ({
conversion: convertDivElement,
conversion: $convertDivElement,
priority: 1,
}),
pre: (node: Node) => ({
conversion: convertPreElement,
conversion: $convertPreElement,
priority: 0,
}),
table: (node: Node) => {
const table = node;
// domNode is a <table> since we matched it by nodeName
if (isGitHubCodeTable(table as HTMLTableElement)) {
return {
conversion: convertTableElement,
conversion: $convertTableElement,
priority: 3,
};
}
Expand Down Expand Up @@ -294,14 +294,6 @@ export class CodeNode extends ElementNode {
return false;
}

collapseAtStart(): boolean {
const paragraph = $createParagraphNode();
const children = this.getChildren();
children.forEach((child) => paragraph.append(child));
this.replace(paragraph);
return true;
}

setLanguage(language: string): void {
const writable = this.getWritable();
writable.__language = mapToPrismLanguage(language);
Expand All @@ -324,15 +316,15 @@ export function $isCodeNode(
return node instanceof CodeNode;
}

function convertPreElement(domNode: Node): DOMConversionOutput {
function $convertPreElement(domNode: Node): DOMConversionOutput {
let language;
if (isHTMLElement(domNode)) {
language = domNode.getAttribute(LANGUAGE_DATA_ATTRIBUTE);
}
return {node: $createCodeNode(language)};
}

function convertDivElement(domNode: Node): DOMConversionOutput {
function $convertDivElement(domNode: Node): DOMConversionOutput {
// domNode is a <div> since we matched it by nodeName
const div = domNode as HTMLDivElement;
const isCode = isCodeElement(div);
Expand All @@ -346,7 +338,7 @@ function convertDivElement(domNode: Node): DOMConversionOutput {
};
}

function convertTableElement(): DOMConversionOutput {
function $convertTableElement(): DOMConversionOutput {
return {node: $createCodeNode()};
}

Expand Down
2 changes: 0 additions & 2 deletions packages/lexical-rich-text/flow/LexicalRichText.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ declare export class QuoteNode extends ElementNode {
selection: RangeSelection,
restoreSelection?: boolean,
): ParagraphNode;
collapseAtStart(): true;
}
declare export function $createQuoteNode(): QuoteNode;
declare export function $isQuoteNode(
Expand All @@ -50,7 +49,6 @@ declare export class HeadingNode extends ElementNode {
selection: RangeSelection,
restoreSelection?: boolean,
): ParagraphNode;
collapseAtStart(): true;
}
declare export function $createHeadingNode(
headingTag: HeadingTagType,
Expand Down
42 changes: 12 additions & 30 deletions packages/lexical-rich-text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class QuoteNode extends ElementNode {
static importDOM(): DOMConversionMap | null {
return {
blockquote: (node: Node) => ({
conversion: convertBlockquoteElement,
conversion: $convertBlockquoteElement,
priority: 0,
}),
};
Expand Down Expand Up @@ -194,14 +194,6 @@ export class QuoteNode extends ElementNode {
this.insertAfter(newBlock, restoreSelection);
return newBlock;
}

collapseAtStart(): true {
const paragraph = $createParagraphNode();
const children = this.getChildren();
children.forEach((child) => paragraph.append(child));
this.replace(paragraph);
return true;
}
}

export function $createQuoteNode(): QuoteNode {
Expand Down Expand Up @@ -259,27 +251,27 @@ export class HeadingNode extends ElementNode {
static importDOM(): DOMConversionMap | null {
return {
h1: (node: Node) => ({
conversion: convertHeadingElement,
conversion: $convertHeadingElement,
priority: 0,
}),
h2: (node: Node) => ({
conversion: convertHeadingElement,
conversion: $convertHeadingElement,
priority: 0,
}),
h3: (node: Node) => ({
conversion: convertHeadingElement,
conversion: $convertHeadingElement,
priority: 0,
}),
h4: (node: Node) => ({
conversion: convertHeadingElement,
conversion: $convertHeadingElement,
priority: 0,
}),
h5: (node: Node) => ({
conversion: convertHeadingElement,
conversion: $convertHeadingElement,
priority: 0,
}),
h6: (node: Node) => ({
conversion: convertHeadingElement,
conversion: $convertHeadingElement,
priority: 0,
}),
p: (node: Node) => {
Expand Down Expand Up @@ -370,16 +362,6 @@ export class HeadingNode extends ElementNode {
return newElement;
}

collapseAtStart(): true {
const newElement = !this.isEmpty()
? $createHeadingNode(this.getTag())
: $createParagraphNode();
const children = this.getChildren();
children.forEach((child) => newElement.append(child));
this.replace(newElement);
return true;
}

extractWithChild(): boolean {
return true;
}
Expand All @@ -392,7 +374,7 @@ function isGoogleDocsTitle(domNode: Node): boolean {
return false;
}

function convertHeadingElement(element: HTMLElement): DOMConversionOutput {
function $convertHeadingElement(element: HTMLElement): DOMConversionOutput {
const nodeName = element.nodeName.toLowerCase();
let node = null;
if (
Expand All @@ -411,7 +393,7 @@ function convertHeadingElement(element: HTMLElement): DOMConversionOutput {
return {node};
}

function convertBlockquoteElement(element: HTMLElement): DOMConversionOutput {
function $convertBlockquoteElement(element: HTMLElement): DOMConversionOutput {
const node = $createQuoteNode();
if (element.style !== null) {
node.setFormat(element.style.textAlign as ElementFormatType);
Expand Down Expand Up @@ -494,7 +476,7 @@ export function eventFiles(
return [hasFiles, Array.from(dataTransfer.files), hasContent];
}

function handleIndentAndOutdent(
function $handleIndentAndOutdent(
indentOrOutdent: (block: ElementNode) => void,
): boolean {
const selection = $getSelection();
Expand Down Expand Up @@ -696,7 +678,7 @@ export function registerRichText(editor: LexicalEditor): () => void {
editor.registerCommand(
INDENT_CONTENT_COMMAND,
() => {
return handleIndentAndOutdent((block) => {
return $handleIndentAndOutdent((block) => {
const indent = block.getIndent();
block.setIndent(indent + 1);
});
Expand All @@ -706,7 +688,7 @@ export function registerRichText(editor: LexicalEditor): () => void {
editor.registerCommand(
OUTDENT_CONTENT_COMMAND,
() => {
return handleIndentAndOutdent((block) => {
return $handleIndentAndOutdent((block) => {
const indent = block.getIndent();
if (indent > 0) {
block.setIndent(indent - 1);
Expand Down
4 changes: 0 additions & 4 deletions packages/lexical-table/flow/LexicalTable.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ declare export class TableCellNode extends ElementNode {
insertNewAfter(
selection: RangeSelection,
): null | ParagraphNode | TableCellNode;
collapseAtStart(): true;
getColSpan(): number;
setColSpan(colSpan: number): this;
getRowSpan(): number;
Expand All @@ -71,7 +70,6 @@ declare export class TableCellNode extends ElementNode {
toggleHeaderStyle(headerState: TableCellHeaderState): TableCellNode;
hasHeader(): boolean;
updateDOM(prevNode: TableCellNode): boolean;
collapseAtStart(): true;
canBeEmpty(): false;
}
declare export function $createTableCellNode(
Expand Down Expand Up @@ -101,7 +99,6 @@ declare export class TableNode extends ElementNode {
createDOM(config: EditorConfig): HTMLElement;
updateDOM(prevNode: TableNode, dom: HTMLElement): boolean;
insertNewAfter(selection: RangeSelection): null | ParagraphNode | TableNode;
collapseAtStart(): true;
getCordsFromCellNode(
tableCellNode: TableCellNode,
table: TableDOMTable,
Expand Down Expand Up @@ -132,7 +129,6 @@ declare export class TableRowNode extends ElementNode {
insertNewAfter(
selection: RangeSelection,
): null | ParagraphNode | TableRowNode;
collapseAtStart(): true;
}
declare export function $createTableRowNode(): TableRowNode;
declare export function $isTableRowNode(
Expand Down
12 changes: 5 additions & 7 deletions packages/lexical-table/src/LexicalTableCellNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ export class TableCellNode extends ElementNode {
static importDOM(): DOMConversionMap | null {
return {
td: (node: Node) => ({
conversion: convertTableCellNodeElement,
conversion: $convertTableCellNodeElement,
priority: 0,
}),
th: (node: Node) => ({
conversion: convertTableCellNodeElement,
conversion: $convertTableCellNodeElement,
priority: 0,
}),
};
Expand Down Expand Up @@ -279,10 +279,6 @@ export class TableCellNode extends ElementNode {
return true;
}

collapseAtStart(): true {
return true;
}

canBeEmpty(): false {
return false;
}
Expand All @@ -292,7 +288,7 @@ export class TableCellNode extends ElementNode {
}
}

export function convertTableCellNodeElement(
export function $convertTableCellNodeElement(
domNode: Node,
): DOMConversionOutput {
const domNode_ = domNode as HTMLTableCellElement;
Expand Down Expand Up @@ -387,6 +383,8 @@ export function convertTableCellNodeElement(
node: tableCellNode,
};
}
/** @deprecated renamed to $convertTableCellNodeElement by @lexical/eslint-plugin rules-of-lexical */
export const convertTableCellNodeElement = $convertTableCellNodeElement;

export function $createTableCellNode(
headerState: TableCellHeaderState,
Expand Down
1 change: 0 additions & 1 deletion packages/lexical/flow/Lexical.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,6 @@ declare export class ParagraphNode extends ElementNode {
selection: RangeSelection,
restoreSelection?: boolean,
): ParagraphNode;
collapseAtStart(): boolean;
static importJSON(
serializedParagraphNode: SerializedParagraphNode,
): ParagraphNode;
Expand Down
30 changes: 3 additions & 27 deletions packages/lexical/src/nodes/LexicalParagraphNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
isHTMLElement,
} from '../LexicalUtils';
import {ElementNode} from './LexicalElementNode';
import {$isTextNode, TextFormatType} from './LexicalTextNode';
import {TextFormatType} from './LexicalTextNode';

export type SerializedParagraphNode = Spread<
{
Expand Down Expand Up @@ -98,7 +98,7 @@ export class ParagraphNode extends ElementNode {
static importDOM(): DOMConversionMap | null {
return {
p: (node: Node) => ({
conversion: convertParagraphElement,
conversion: $convertParagraphElement,
priority: 0,
}),
};
Expand Down Expand Up @@ -164,33 +164,9 @@ export class ParagraphNode extends ElementNode {
this.insertAfter(newElement, restoreSelection);
return newElement;
}

collapseAtStart(): boolean {
const children = this.getChildren();
// If we have an empty (trimmed) first paragraph and try and remove it,
// delete the paragraph as long as we have another sibling to go to
if (
children.length === 0 ||
($isTextNode(children[0]) && children[0].getTextContent().trim() === '')
) {
const nextSibling = this.getNextSibling();
if (nextSibling !== null) {
this.selectNext();
this.remove();
return true;
}
const prevSibling = this.getPreviousSibling();
if (prevSibling !== null) {
this.selectPrevious();
this.remove();
return true;
}
}
return false;
}
}

function convertParagraphElement(element: HTMLElement): DOMConversionOutput {
function $convertParagraphElement(element: HTMLElement): DOMConversionOutput {
const node = $createParagraphNode();
if (element.style) {
node.setFormat(element.style.textAlign as ElementFormatType);
Expand Down
4 changes: 0 additions & 4 deletions packages/lexical/src/nodes/LexicalRootNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ export class RootNode extends ElementNode {
version: 1,
};
}

collapseAtStart(): true {
return true;
}
}

export function $createRootNode(): RootNode {
Expand Down

0 comments on commit 2a42618

Please sign in to comment.